/// <summary> /// Reconduct the argument to vectors. /// </summary> /// <param name="argument"></param> /// <param name="environment"></param> /// <returns></returns> private AType DiscloseNestedArray(AType argument, Aplus environment) { AType result; if (argument.IsArray) { List <int> insideShape; if (argument.Rank > 1) { result = MonadicFunctionInstance.Ravel.Execute(argument, environment); result = DiscloseNestedVector(result, out insideShape); List <int> newShape = new List <int>(argument.Shape); newShape.AddRange(insideShape); result = DyadicFunctionInstance.Reshape.Execute(result, newShape.ToAArray(), environment); } else { result = DiscloseNestedVector(argument, out insideShape); } } else { // Get the nested scalar result = argument.NestedItem; } if (result.Rank > 9) { throw new Error.MaxRank(MaxRankErrorText); } return(result); }
/// <summary> /// Search the <see cref="left"/> argument in the <see cref="right"/> argument. /// </summary> /// <param name="right">The data where the search should be performed.</param> /// <param name="left">Element to search in the right argument.</param> /// <param name="environment"></param> /// <returns></returns> public override AType Execute(AType right, AType left, Aplus environment = null) { if (right.Length == 0) { switch (left.Rank) { case 0: return(AInteger.Create(0)); default: return(DyadicFunctionInstance.Reshape.Execute(AInteger.Create(0), left.Shape.ToAArray())); } } else if (!(left.IsNumber && right.IsNumber) && (right.Type != left.Type)) { throw new Error.Type(this.TypeErrorText); } SearchInfo searchInfo = new SearchInfo() { // convert the right argument to an array (make life easier..) SearchWhere = right.IsArray ? right : AArray.Create(right.Type, right), ErrorText = this.LengthErrorText }; if (left.Rank < searchInfo.SearchWhere[0].Rank) { // The item we are looking for have a lesser rank than the items of the right argument throw new Error.Rank(this.RankErrorText); } AType result; if (!left.IsArray) { // Process a simple scalar result = ProcessScalar(left, searchInfo); } else { searchInfo.Result = AArray.Create(ATypes.AInteger); ProcessMatrix(left, searchInfo); searchInfo.Result.UpdateInfo(); // Check how many elements we need to return int elementCount = left.Shape.Count - Math.Max(right.Rank - 1, 0); if ((elementCount == 0) && searchInfo.Result.TryFirstScalar(out result)) { // need to return a simple scalar // 'result' contains the desired value } else { // reshape the result to the required shape List <int> desiredShape = left.Shape.GetRange(0, elementCount); result = DyadicFunctionInstance.Reshape.Execute(searchInfo.Result, desiredShape.ToAArray()); } } return(result); }
public override AType Execute(AType right, AType left, Aplus environment = null) { AType typeToCast; if (!left.TryFirstScalar(out typeToCast, true)) { throw new Error.Domain(this.DomainErrorText); } int sourceTypeSize = GetTypeSize(right.Type); ATypes typeToConvert; ConverterFunction converterFunction; int destinationTypeSize; switch (typeToCast.asString) { case "int": converterFunction = ConvertToInt; typeToConvert = ATypes.AInteger; break; case "float": converterFunction = ConvertToFloat; typeToConvert = ATypes.AFloat; break; case "char": converterFunction = ConvertToChar; typeToConvert = ATypes.AChar; break; default: throw new Error.Domain(this.DomainErrorText); } destinationTypeSize = GetTypeSize(typeToConvert); int convertSizeRatio; double shapeModifier; if (sourceTypeSize >= destinationTypeSize) { convertSizeRatio = sourceTypeSize / destinationTypeSize; shapeModifier = sourceTypeSize / destinationTypeSize; } else { convertSizeRatio = destinationTypeSize / sourceTypeSize; // check if the last dimension of the right argument is convertable to the type specified in the left argument if (right.Shape.Count == 0 || right.Shape[right.Shape.Count - 1] % convertSizeRatio != 0) { throw new Error.Length(this.LengthErrorText); } shapeModifier = destinationTypeSize / sourceTypeSize; } AType result; if (right.Shape.Contains(0)) { List<int> desiredShape = new List<int>(right.Shape); int length = desiredShape.Count - 1; desiredShape[length] = (int)(desiredShape[length] * shapeModifier); AType reshapeShape = desiredShape.ToAArray(); result = Utils.FillElement(typeToConvert, reshapeShape); } else { result = converterFunction(right); } return result; }
public override AType Execute(AType right, AType left, Aplus environment = null) { AType typeToCast; if (!left.TryFirstScalar(out typeToCast, true)) { throw new Error.Domain(this.DomainErrorText); } int sourceTypeSize = GetTypeSize(right.Type); ATypes typeToConvert; ConverterFunction converterFunction; int destinationTypeSize; switch (typeToCast.asString) { case "int": converterFunction = ConvertToInt; typeToConvert = ATypes.AInteger; break; case "float": converterFunction = ConvertToFloat; typeToConvert = ATypes.AFloat; break; case "char": converterFunction = ConvertToChar; typeToConvert = ATypes.AChar; break; default: throw new Error.Domain(this.DomainErrorText); } destinationTypeSize = GetTypeSize(typeToConvert); int convertSizeRatio; double shapeModifier; if (sourceTypeSize >= destinationTypeSize) { convertSizeRatio = sourceTypeSize / destinationTypeSize; shapeModifier = sourceTypeSize / destinationTypeSize; } else { convertSizeRatio = destinationTypeSize / sourceTypeSize; // check if the last dimension of the right argument is convertable to the type specified in the left argument if (right.Shape.Count == 0 || right.Shape[right.Shape.Count - 1] % convertSizeRatio != 0) { throw new Error.Length(this.LengthErrorText); } shapeModifier = destinationTypeSize / sourceTypeSize; } AType result; if (right.Shape.Contains(0)) { List <int> desiredShape = new List <int>(right.Shape); int length = desiredShape.Count - 1; desiredShape[length] = (int)(desiredShape[length] * shapeModifier); AType reshapeShape = desiredShape.ToAArray(); result = Utils.FillElement(typeToConvert, reshapeShape); } else { result = converterFunction(right); } return(result); }
/// <summary> /// Reconduct the argument to vectors. /// </summary> /// <param name="argument"></param> /// <param name="environment"></param> /// <returns></returns> private AType DiscloseNestedArray(AType argument, Aplus environment) { AType result; if (argument.IsArray) { List<int> insideShape; if (argument.Rank > 1) { result = MonadicFunctionInstance.Ravel.Execute(argument, environment); result = DiscloseNestedVector(result, out insideShape); List<int> newShape = new List<int>(argument.Shape); newShape.AddRange(insideShape); result = DyadicFunctionInstance.Reshape.Execute(result, newShape.ToAArray(), environment); } else { result = DiscloseNestedVector(argument, out insideShape); } } else { // Get the nested scalar result = argument.NestedItem; } if (result.Rank > 9) { throw new Error.MaxRank(MaxRankErrorText); } return result; }