/// <summary> /// Change column represenation to row representation. /// </summary> /// <param name="columnItems"></param> /// <param name="originalItems"></param> /// <returns></returns> private static AType ReStructure(List <AType> columnItems, AType originalItems) { AType result = AArray.Create(ATypes.AArray); AType row; for (int i = 0; i < originalItems.Shape[0]; i++) { row = AArray.Create(ATypes.AArray); for (int j = 0; j < originalItems.Shape[1]; j++) { row.AddWithNoUpdate(columnItems[j][i].Clone()); } row.Length = originalItems.Shape[1]; row.Shape = new List <int>() { row.Length }; row.Rank = 1; row.Type = row.Length > 0 ? row[0].Type : (originalItems.MixedType() ? ATypes.ANull : originalItems.Type); result.AddWithNoUpdate(row); } result.Length = originalItems.Length; result.Shape = new List <int>() { result.Length }; result.Shape.AddRange(originalItems.Shape.GetRange(1, originalItems.Shape.Count - 1)); result.Rank = originalItems.Rank; result.Type = result.Length > 0 ? result[0].Type : (originalItems.MixedType() ? ATypes.ANull : originalItems.Type); return(result); }
/// <summary> /// Rotate the input array the correspond direction with number. /// </summary> /// <param name="number"></param> /// <param name="item"></param> /// <returns></returns> private static AType RotateArray(int number, AType item) { AType result; if (number == 0) { result = item; } else { result = AArray.Create(ATypes.AArray); if (item.Length > 0) { if (item.Length <= Math.Abs(number)) { number = number % item.Length; } if (number < 0) { number = item.Length + number; } for (int i = number; i <= item.Length - 1; i++) { result.AddWithNoUpdate(item[i]); } for (int i = 0; i < number; i++) { result.AddWithNoUpdate(item[i]); } } result.Length = item.Length; result.Shape = new List <int>() { result.Length }; result.Rank = 1; result.Type = result.Length > 0 ? result[0].Type : (item.MixedType() ? ATypes.ANull : item.Type); } return(result); }
private static AType CreateEnclosedNull(AType inputItem) { AType result = AArray.Create(inputItem.MixedType() ? ATypes.ANull : inputItem.Type); result.Length = 0; result.Shape = new List <int>() { result.Length }; if (inputItem.Rank > 1) { result.Shape.AddRange(inputItem.Shape.GetRange(1, inputItem.Shape.Count - 1)); } result.Rank = inputItem.Rank; return(ABox.Create(result)); }
/// <summary> /// Create the result array with corrpesond shape. /// </summary> /// <param name="items"></param> /// <param name="shape"></param> /// <param name="pathVector"></param> /// <param name="transposeVector"></param> /// <returns></returns> private AType CreateArray(AType items, LinkedList <int> shape, List <int> pathVector, int[] transposeVector) { List <int> newPathVector; LinkedList <int> cutShape = null; AType result = AArray.Create(ATypes.AArray); for (int i = 0; i < shape.First(); i++) { newPathVector = new List <int>(); newPathVector.AddRange(pathVector); newPathVector.Add(i); if (shape.Count > 1) { cutShape = new LinkedList <int>(shape); cutShape.RemoveFirst(); result.AddWithNoUpdate(CreateArray(items, cutShape, newPathVector, transposeVector)); } else { result.AddWithNoUpdate(GetItem(items, newPathVector, transposeVector)); } } result.Length = shape.First(); result.Shape = new List <int>() { result.Length }; if (shape.Count > 1) { result.Shape.AddRange(cutShape.ToList()); } result.Rank = result.Shape.Count; result.Type = result.Length > 0 ? result[0].Type : (items.MixedType() ? ATypes.ANull : items.Type); return(result); }
private AType Compute(AType items, byte[] expandVector) { AType result = AArray.Create(ATypes.AType); int index = 0; // get the filler element based on the right argument AType fillElementShape = items.Rank > 1 ? items.Shape.GetRange(1, items.Shape.Count - 1).ToAArray() : null; AType filler = Utils.FillElement(items.Type, fillElementShape); for (int i = 0; i < expandVector.Length; i++) { if (expandVector[i] == 1) { result.AddWithNoUpdate(items[items.Length > 1 ? index++ : 0].Clone()); } else { result.AddWithNoUpdate(filler.Clone()); } } result.Length = expandVector.Length; result.Shape = new List <int>() { expandVector.Length }; if (items.Rank > 1) { result.Shape.AddRange(items.Shape.GetRange(1, items.Shape.Count - 1)); } result.Rank = items.Rank; result.Type = result.Length > 0 ? result[0].Type : (items.MixedType() ? ATypes.ANull : items.Type); return(result); }
private AType Compute(AType items, int cutValue) { AType result; if (items.IsArray) { int absoluteCutValue = Math.Abs(cutValue); int firstAxisLength; // compute the first axis with corrpespond formula if (cutValue > 0) { if (items.Length % cutValue != 0) { throw new Error.Length(LengthErrorText); } firstAxisLength = items.Length / cutValue; } // y <= 0 else { if (items.Length < absoluteCutValue - 1) { throw new Error.Length(LengthErrorText); } firstAxisLength = cutValue + items.Length + 1; } result = AArray.Create(ATypes.AType); if (items.Length > 0) { int counter = 0; for (int i = 0; i < firstAxisLength; i++) { AType item = AArray.Create(ATypes.AArray); // select the correspond element if y > 0: get the following element else iota abs_y + i # items. for (int j = 0; j < absoluteCutValue; j++) { item.AddWithNoUpdate(items[cutValue > 0 ? counter++ : i + j].Clone()); } item.Length = absoluteCutValue; item.Shape = new List<int>() { absoluteCutValue }; item.Shape.AddRange(items[0].Shape); item.Rank = items.Rank; item.Type = item.Length > 0 ? item[0].Type : (items.MixedType() ? ATypes.ANull : items.Type); result.AddWithNoUpdate(item); } } result.Length = firstAxisLength; result.Shape = new List<int>() { firstAxisLength }; result.Shape.Add(absoluteCutValue); if (items.Rank > 1) { result.Shape.AddRange(items.Shape.GetRange(1, items.Shape.Count - 1)); } result.Rank = 1 + items.Rank; result.Type = result.Length > 0 ? result[0].Type : (items.MixedType() ? ATypes.ANull : items.Type); } else { result = AArray.Create(items.Type, items); } return result; }
/// <summary> /// Change column represenation to row representation. /// </summary> /// <param name="columnItems"></param> /// <param name="originalItems"></param> /// <returns></returns> private static AType ReStructure(List<AType> columnItems, AType originalItems) { AType result = AArray.Create(ATypes.AArray); AType row; for (int i = 0; i < originalItems.Shape[0]; i++) { row = AArray.Create(ATypes.AArray); for (int j = 0; j < originalItems.Shape[1]; j++) { row.AddWithNoUpdate(columnItems[j][i].Clone()); } row.Length = originalItems.Shape[1]; row.Shape = new List<int>() { row.Length }; row.Rank = 1; row.Type = row.Length > 0 ? row[0].Type : (originalItems.MixedType() ? ATypes.ANull : originalItems.Type); result.AddWithNoUpdate(row); } result.Length = originalItems.Length; result.Shape = new List<int>() { result.Length }; result.Shape.AddRange(originalItems.Shape.GetRange(1, originalItems.Shape.Count - 1)); result.Rank = originalItems.Rank; result.Type = result.Length > 0 ? result[0].Type : (originalItems.MixedType() ? ATypes.ANull : originalItems.Type); return result; }
/// <summary> /// Rotate the input array the correspond direction with number. /// </summary> /// <param name="number"></param> /// <param name="item"></param> /// <returns></returns> private static AType RotateArray(int number, AType item) { AType result; if (number == 0) { result = item; } else { result = AArray.Create(ATypes.AArray); if (item.Length > 0) { if (item.Length <= Math.Abs(number)) { number = number % item.Length; } if (number < 0) { number = item.Length + number; } for (int i = number; i <= item.Length - 1; i++) { result.AddWithNoUpdate(item[i]); } for (int i = 0; i < number; i++) { result.AddWithNoUpdate(item[i]); } } result.Length = item.Length; result.Shape = new List<int>() { result.Length }; result.Rank = 1; result.Type = result.Length > 0 ? result[0].Type : (item.MixedType() ? ATypes.ANull : item.Type); } return result; }
/// <summary> /// Argument is a nested vector than we: >(0#x), >(1#x),..., >(-1+#x)#x /// </summary> /// <param name="argument"></param> /// <returns></returns> private AType NestedVector(AType argument) { AType result = AArray.Create(ATypes.AArray); // disclose the first item of the argument, get type, rank and shape AType disclosedItem = MonadicFunctionInstance.Disclose.Execute(argument[0]); AType disclosedArray = disclosedItem.IsArray ? disclosedItem : AArray.Create(disclosedItem.Type, disclosedItem); ATypes type = disclosedArray.Type; int rank = disclosedArray.Rank; List <int> shape = disclosedArray.Shape; bool convertToFloat = false; // first Float null item bool FloatNull = (disclosedArray.Length == 0 && type == ATypes.AFloat); // add first item to the result int length = Catenate(disclosedArray, result); for (int i = 1; i < argument.Length; i++) { //Disclose ith item. disclosedItem = MonadicFunctionInstance.Disclose.Execute(argument[i]); // TODO: do we need these checks? disclosedArray = disclosedItem.IsArray ? disclosedItem : AArray.Create(disclosedItem.Type, disclosedItem); if (disclosedArray.Length == 0) { // skip empty items continue; } if (type != disclosedArray.Type) { // if one item of the argument is Float then all item will be Float // if and only if other items is Integers or Floats. if (type == ATypes.AFloat && disclosedArray.Type == ATypes.AInteger) { if (FloatNull) { type = ATypes.AInteger; } else { convertToFloat = true; } } else if (type == ATypes.AInteger && disclosedArray.Type == ATypes.AFloat) { convertToFloat = true; } else if (type == ATypes.ANull) { type = disclosedArray.Type; } else if (!type.MixedType() || !disclosedArray.MixedType()) { // type is differnce so we throw Type error throw new Error.Type(TypeErrorText); } } FloatNull = false; if (rank != disclosedArray.Rank) { throw new Error.Rank(RankErrorText); } // mismatch error arise if actual item has bigger rank than 1 and has a different shape if (!shape.SequenceEqual(disclosedArray.Shape)) { if (shape.Count > 1 || disclosedArray.Shape.Count > 1) { throw new Error.Mismatch(MismatchErrorText); } } length += Catenate(disclosedArray, result); } // set result properties result.Length = length; result.Rank = rank; result.Shape = new List <int>() { length }; if (rank > 1) { result.Shape.AddRange(shape.GetRange(1, shape.Count - 1)); } // convert items to Float, if this flag is true. if (convertToFloat) { result.ConvertToFloat(); } else { result.Type = type; } return(result); }
private AType Compute(AType right, int dropCounter) { if (right.IsArray && dropCounter == 0) { return right.Clone(); } AType result = AArray.Create(ATypes.AType); if (right.IsArray) { if (right.Length - Math.Abs(dropCounter) > 0) { if (dropCounter > 0) { for (int i = dropCounter; i < right.Length; i++) { result.AddWithNoUpdate(right[i].Clone()); } } else { for (int i = 0; i < right.Length + dropCounter; i++) { result.AddWithNoUpdate(right[i].Clone()); } } result.Length = right.Length - Math.Abs(dropCounter); result.Shape = new List<int>() { result.Length }; if (right.Rank > 1) { result.Shape.AddRange(right.Shape.GetRange(1, right.Shape.Count - 1)); } result.Rank = right.Rank; result.Type = result[0].Type; } else { result.Type = right.MixedType() ? ATypes.ANull : right.Type; } } else { if (dropCounter == 0) { result.Add(right.Clone()); result.Length = 1; result.Shape = new List<int>() { 1 }; result.Type = right.Type; } else { result.Type = right.MixedType() ? ATypes.ANull : right.Type; } } return result; }
private AType Compute(AType items, byte[] expandVector) { AType result = AArray.Create(ATypes.AType); int index = 0; // get the filler element based on the right argument AType fillElementShape = items.Rank > 1 ? items.Shape.GetRange(1, items.Shape.Count - 1).ToAArray() : null; AType filler = Utils.FillElement(items.Type, fillElementShape); for (int i = 0; i < expandVector.Length; i++) { if (expandVector[i] == 1) { result.AddWithNoUpdate(items[items.Length > 1 ? index++ : 0].Clone()); } else { result.AddWithNoUpdate(filler.Clone()); } } result.Length = expandVector.Length; result.Shape = new List<int>() { expandVector.Length }; if (items.Rank > 1) { result.Shape.AddRange(items.Shape.GetRange(1, items.Shape.Count - 1)); } result.Rank = items.Rank; result.Type = result.Length > 0 ? result[0].Type : (items.MixedType() ? ATypes.ANull : items.Type); return result; }
/// <summary> /// Create the result array with corrpesond shape. /// </summary> /// <param name="items"></param> /// <param name="shape"></param> /// <param name="pathVector"></param> /// <param name="transposeVector"></param> /// <returns></returns> private AType CreateArray(AType items, LinkedList<int> shape, List<int> pathVector, int[] transposeVector) { List<int> newPathVector; LinkedList<int> cutShape = null; AType result = AArray.Create(ATypes.AArray); for (int i = 0; i < shape.First(); i++) { newPathVector = new List<int>(); newPathVector.AddRange(pathVector); newPathVector.Add(i); if (shape.Count > 1) { cutShape = new LinkedList<int>(shape); cutShape.RemoveFirst(); result.AddWithNoUpdate(CreateArray(items, cutShape, newPathVector, transposeVector)); } else { result.AddWithNoUpdate(GetItem(items, newPathVector, transposeVector)); } } result.Length = shape.First(); result.Shape = new List<int>() { result.Length }; if (shape.Count > 1) { result.Shape.AddRange(cutShape.ToList()); } result.Rank = result.Shape.Count; result.Type = result.Length > 0 ? result[0].Type : (items.MixedType() ? ATypes.ANull : items.Type); return result; }
private static AType CreateEnclosedNull(AType inputItem) { AType result = AArray.Create(inputItem.MixedType() ? ATypes.ANull : inputItem.Type); result.Length = 0; result.Shape = new List<int>() { result.Length }; if (inputItem.Rank > 1) { result.Shape.AddRange(inputItem.Shape.GetRange(1, inputItem.Shape.Count - 1)); } result.Rank = inputItem.Rank; return ABox.Create(result); }
private AType DiscloseNestedVector(AType argument, out List <int> shape) { //TODO: Is Clone method correct here? AType item = argument[0].NestedItem.Clone(); ATypes type = item.Type; shape = item.Shape; int rank = item.Rank; if (CheckFloat(argument)) { //Convert the first item to AFloat. if (item.Type == ATypes.AInteger) { item = Utils.ConvertToFloat(item); type = ATypes.AFloat; } if (item.Type != ATypes.AFloat) { throw new Error.Type(TypeErrorText); } } AType result = AArray.Create(type, item); for (int i = 1; i < argument.Length; i++) { //TODO: Is Clone method correct here? item = argument[i].NestedItem.Clone(); // Uniform rules! if (item.Type != type) { if (type == ATypes.AFloat && item.Type == ATypes.AInteger) { item = Utils.ConvertToFloat(item); } else { if (!type.MixedType() || !item.MixedType()) { throw new Error.Type(TypeErrorText); } } } if (item.Rank != rank) { throw new Error.Rank(RankErrorText); } if (!item.Shape.SequenceEqual(shape)) { throw new Error.Mismatch(MismatchErrorText); } result.AddWithNoUpdate(item); } result.Length = argument.Length; result.Shape = new List <int>() { result.Length }; result.Shape.AddRange(shape); result.Rank = 1 + rank; return(result); }
private AType Compute(AType items, int cutValue) { AType result; if (items.IsArray) { int absoluteCutValue = Math.Abs(cutValue); int firstAxisLength; // compute the first axis with corrpespond formula if (cutValue > 0) { if (items.Length % cutValue != 0) { throw new Error.Length(LengthErrorText); } firstAxisLength = items.Length / cutValue; } // y <= 0 else { if (items.Length < absoluteCutValue - 1) { throw new Error.Length(LengthErrorText); } firstAxisLength = cutValue + items.Length + 1; } result = AArray.Create(ATypes.AType); if (items.Length > 0) { int counter = 0; for (int i = 0; i < firstAxisLength; i++) { AType item = AArray.Create(ATypes.AArray); // select the correspond element if y > 0: get the following element else iota abs_y + i # items. for (int j = 0; j < absoluteCutValue; j++) { item.AddWithNoUpdate(items[cutValue > 0 ? counter++ : i + j].Clone()); } item.Length = absoluteCutValue; item.Shape = new List <int>() { absoluteCutValue }; item.Shape.AddRange(items[0].Shape); item.Rank = items.Rank; item.Type = item.Length > 0 ? item[0].Type : (items.MixedType() ? ATypes.ANull : items.Type); result.AddWithNoUpdate(item); } } result.Length = firstAxisLength; result.Shape = new List <int>() { firstAxisLength }; result.Shape.Add(absoluteCutValue); if (items.Rank > 1) { result.Shape.AddRange(items.Shape.GetRange(1, items.Shape.Count - 1)); } result.Rank = 1 + items.Rank; result.Type = result.Length > 0 ? result[0].Type : (items.MixedType() ? ATypes.ANull : items.Type); } else { result = AArray.Create(items.Type, items); } return(result); }
private AType Compute(AType items, int desiredCount) { AType result = AArray.Create(items.Type); if (desiredCount > 0) { // Check how many we can copy from the right argument int count = (desiredCount <= items.Length) ? desiredCount : items.Length; int remainder = desiredCount - count; for (int i = 0; i < count; i++) { result.AddWithNoUpdate(items[i].Clone()); } // Check if there is some leftover we need to fill if (remainder > 0) { AType filler = FillElement(items); for (; remainder > 0; remainder--) { result.AddWithNoUpdate(filler.Clone()); } } } else { // set the start point, which is the difference between the length of the items and // the count we want to take from the end of the list // NOTE: + is used because in this case the 'desiredCount' variable is a negative number int start = items.Length + desiredCount; if (start < 0) { // This case we need to add fill elements to the start of the array AType filler = FillElement(items); for(;start < 0; start++) { result.AddWithNoUpdate(filler.Clone()); } // Now the 'start' is 0 } for (; start < items.Length; start++) { result.AddWithNoUpdate(items[start].Clone()); } } result.Length = Math.Abs(desiredCount); result.Shape = new List<int>() { result.Length }; if (items.Rank > 1) { result.Shape.AddRange(items.Shape.GetRange(1, items.Shape.Count - 1)); } result.Rank = items.Rank; if (desiredCount == 0) { result.Type = items.MixedType() ? ATypes.ANull : items.Type; } else { result.Type = result[0].Type; } return result; }
private AType Compute(AType items, int desiredCount) { AType result = AArray.Create(items.Type); if (desiredCount > 0) { // Check how many we can copy from the right argument int count = (desiredCount <= items.Length) ? desiredCount : items.Length; int remainder = desiredCount - count; for (int i = 0; i < count; i++) { result.AddWithNoUpdate(items[i].Clone()); } // Check if there is some leftover we need to fill if (remainder > 0) { AType filler = FillElement(items); for (; remainder > 0; remainder--) { result.AddWithNoUpdate(filler.Clone()); } } } else { // set the start point, which is the difference between the length of the items and // the count we want to take from the end of the list // NOTE: + is used because in this case the 'desiredCount' variable is a negative number int start = items.Length + desiredCount; if (start < 0) { // This case we need to add fill elements to the start of the array AType filler = FillElement(items); for (; start < 0; start++) { result.AddWithNoUpdate(filler.Clone()); } // Now the 'start' is 0 } for (; start < items.Length; start++) { result.AddWithNoUpdate(items[start].Clone()); } } result.Length = Math.Abs(desiredCount); result.Shape = new List <int>() { result.Length }; if (items.Rank > 1) { result.Shape.AddRange(items.Shape.GetRange(1, items.Shape.Count - 1)); } result.Rank = items.Rank; if (desiredCount == 0) { result.Type = items.MixedType() ? ATypes.ANull : items.Type; } else { result.Type = result[0].Type; } return(result); }
private AType Compute(AType right, int dropCounter) { if (right.IsArray && dropCounter == 0) { return(right.Clone()); } AType result = AArray.Create(ATypes.AType); if (right.IsArray) { if (right.Length - Math.Abs(dropCounter) > 0) { if (dropCounter > 0) { for (int i = dropCounter; i < right.Length; i++) { result.AddWithNoUpdate(right[i].Clone()); } } else { for (int i = 0; i < right.Length + dropCounter; i++) { result.AddWithNoUpdate(right[i].Clone()); } } result.Length = right.Length - Math.Abs(dropCounter); result.Shape = new List <int>() { result.Length }; if (right.Rank > 1) { result.Shape.AddRange(right.Shape.GetRange(1, right.Shape.Count - 1)); } result.Rank = right.Rank; result.Type = result[0].Type; } else { result.Type = right.MixedType() ? ATypes.ANull : right.Type; } } else { if (dropCounter == 0) { result.Add(right.Clone()); result.Length = 1; result.Shape = new List <int>() { 1 }; result.Type = right.Type; } else { result.Type = right.MixedType() ? ATypes.ANull : right.Type; } } return(result); }