private int GetCutNumber(AType right, AType left) { if (!(left.Type == ATypes.AFloat || left.Type == ATypes.AInteger || left.Type == ATypes.ANull)) { throw new Error.Type(this.TypeErrorText); } AType scalar; int cutValue; // get the first scalar value with length check on if (!left.TryFirstScalar(out scalar, true)) { throw new Error.Nonce(this.NonceErrorText); } // check if the scalar is a whole number and set the desired count of items if (!left.ConvertToRestrictedWholeNumber(out cutValue)) { throw new Error.Type(this.TypeErrorText); } if (right.Rank > 8) { throw new Error.MaxRank(MaxRankErrorText); } if (right.Rank == 0 && cutValue != 1) { throw new Error.Rank(RankErrorText); } return(cutValue); }
private int GetCutNumber(AType right, AType left) { if (!(left.Type == ATypes.AFloat || left.Type == ATypes.AInteger || left.Type == ATypes.ANull)) { throw new Error.Type(this.TypeErrorText); } AType scalar; int cutValue; // get the first scalar value with length check on if (!left.TryFirstScalar(out scalar, true)) { throw new Error.Nonce(this.NonceErrorText); } // check if the scalar is a whole number and set the desired count of items if (!left.ConvertToRestrictedWholeNumber(out cutValue)) { throw new Error.Type(this.TypeErrorText); } if (right.Rank > 8) { throw new Error.MaxRank(MaxRankErrorText); } if (right.Rank == 0 && cutValue != 1) { throw new Error.Rank(RankErrorText); } return cutValue; }
/// <summary> /// Convert argument to integer. /// </summary> /// <param name="argument"></param> /// <returns></returns> protected AType ConvertToInt(AType argument) { if (argument.IsArray) { AType result = AArray.Create(ATypes.AInteger); foreach (AType item in argument) { result.AddWithNoUpdate(ConvertToInt(item)); } result.Length = argument.Length; result.Shape = new List <int>(argument.Shape); result.Rank = argument.Rank; return(result); } else { int result; if (argument.ConvertToRestrictedWholeNumber(out result)) { return(AInteger.Create(result)); } else { throw new Error.Type(TypeErrorText); } } }
protected override AType Process(AType argument) { int result; if (!argument.ConvertToRestrictedWholeNumber(out result)) { throw new Error.Type(TypeErrorText); } return AInteger.Create((result != 0) ? 1 : 0); }
private AType CalculateOr(AType right, AType left) { int a, b; if (right.ConvertToRestrictedWholeNumber(out b) && left.ConvertToRestrictedWholeNumber(out a)) { int result = (a != 0 || b != 0) ? 1 : 0; return AInteger.Create(result); } throw new Error.Type(this.TypeErrorText); }
protected override AType Process(AType argument) { int result; if (!argument.ConvertToRestrictedWholeNumber(out result)) { throw new Error.Type(TypeErrorText); } return(AInteger.Create((result != 0) ? 1 : 0)); }
private AType CalculateOr(AType right, AType left) { int a, b; if (right.ConvertToRestrictedWholeNumber(out b) && left.ConvertToRestrictedWholeNumber(out a)) { int result = (a != 0 || b != 0) ? 1 : 0; return(AInteger.Create(result)); } throw new Error.Type(this.TypeErrorText); }
/// <summary> /// Extract the byte representation from the given number, if it is correct. /// </summary> /// <param name="number"></param> /// <returns>0 or 1.</returns> private byte ExtractExpandArgument(AType number) { int result; if (!number.ConvertToRestrictedWholeNumber(out result)) { throw new Error.Type(TypeErrorText); } if (result != 0 && result != 1) { throw new Error.Domain(DomainErrorText); } return((byte)result); }
private int ExtractInteger(AType item) { int element; if (!item.ConvertToRestrictedWholeNumber(out element)) { throw new Error.Type(TypeErrorText); } if (element < 0) { throw new Error.Domain(DomainErrorText); } return(element); }
/// <summary> /// Extract the byte representation from the given number, if it is correct. /// </summary> /// <param name="number"></param> /// <returns>0 or 1.</returns> private byte ExtractExpandArgument(AType number) { int result; if (!number.ConvertToRestrictedWholeNumber(out result)) { throw new Error.Type(TypeErrorText); } if (result != 0 && result != 1) { throw new Error.Domain(DomainErrorText); } return (byte)result; }
private static void PerformIndexAssign(AType target, AType value) { if (target.Rank > 0) { for (int i = 0; i < target.Length; i++) { PerformIndexAssign(target[i], value.IsArray ? value[i] : value); } } else { AValue result; if (target.Type == value.Type) { result = value.Clone().Data; } else if (target.Type == ATypes.AInteger && value.Type == ATypes.AFloat) { int number; if (!value.ConvertToRestrictedWholeNumber(out number)) { throw new Error.Type("assign"); } result = AInteger.Create(number).Data; } else if (target.Type == ATypes.AFloat && value.Type == ATypes.AInteger) { result = AFloat.Create(value.asInteger).Data; } else { throw new Error.Type("Assign"); } if (target.IsMemoryMappedFile) { ((IMapped)target.Data).Update(result); } else { target.Data = result; } } }
private static void PerformIndexAssign(AType target, AType value) { if (target.Rank > 0) { for (int i = 0; i < target.Length; i++) { PerformIndexAssign(target[i], value.IsArray ? value[i] : value); } } else { AValue result; if (target.Type == value.Type) { result = value.Clone().Data; } else if (target.Type == ATypes.AInteger && value.Type == ATypes.AFloat) { int number; if (!value.ConvertToRestrictedWholeNumber(out number)) { throw new Error.Type("assign"); } result = AInteger.Create(number).Data; } else if (target.Type == ATypes.AFloat && value.Type == ATypes.AInteger) { result = AFloat.Create(value.asInteger).Data; } else { throw new Error.Type("Assign"); } if (target.IsMemoryMappedFile) { ((IMapped)target.Data).Update(result); } else { target.Data = result; } } }
private PartitionJobInfo CreatePartitionInfo(AType right, AType left) { // left side type must be float, integer or null if (left.Type != ATypes.AFloat && left.Type != ATypes.AInteger && left.Type != ATypes.ANull || left.IsBox) { throw new Error.Type(TypeErrorText); } // if the right side is scalar then we throw Rank error exception if (right.Rank < 1) { throw new Error.Rank(RankErrorText); } int element; List <int> partitionVector = new List <int>(); int remainder = 0; int sum = 0; bool going = true; // TODO: check if this branch can be eliminated if the argument is raveled always if (left.IsArray) { // TODO: Check this: // if the left rank is higher than 1 then we ravel it to vector. AType raveled_y = left.Rank > 1 ? MonadicFunctionInstance.Ravel.Execute(left) : left; // get the integer list from the right side foreach (AType item in raveled_y) { // if the actual item is float then we convert it to (if it's a restricted whole number) if (!item.ConvertToRestrictedWholeNumber(out element)) { throw new Error.Type(TypeErrorText); } if (element < 0) { // negative item raise domain error throw new Error.Domain(DomainErrorText); } sum += element; if (right.Length > 0 && going) { // compute how many item we can take from the list at last if (sum > right.Length) { partitionVector.Add(right.Length - (sum - element)); going = false; } else { // collect the item to y list partitionVector.Add(element); if (sum == right.Length) { going = false; } } } else { // count the empty elements what we need remainder++; } } } else { // this case is when the left side is scalar if (!left.ConvertToRestrictedWholeNumber(out element)) { throw new Error.Type(TypeErrorText); } if (element < 0) { throw new Error.Domain(DomainErrorText); } else if (element != 0) { int round = right.Length / element; for (int i = 0; i < round; i++) { partitionVector.Add(element); } int rem = right.Length % element; if (rem != 0) { partitionVector.Add(rem); } } else { partitionVector.Add(0); } } PartitionJobInfo info = new PartitionJobInfo(remainder, partitionVector.ToArray()); return(info); }
private int ExtractInteger(AType item) { int element; if (!item.ConvertToRestrictedWholeNumber(out element)) { throw new Error.Type(TypeErrorText); } if (element < 0) { throw new Error.Domain(DomainErrorText); } return element; }
/// <summary> /// Convert argument to integer. /// </summary> /// <param name="argument"></param> /// <returns></returns> protected AType ConvertToInt(AType argument) { if (argument.IsArray) { AType result = AArray.Create(ATypes.AInteger); foreach (AType item in argument) { result.AddWithNoUpdate(ConvertToInt(item)); } result.Length = argument.Length; result.Shape = new List<int>(argument.Shape); result.Rank = argument.Rank; return result; } else { int result; if(argument.ConvertToRestrictedWholeNumber(out result)) { return AInteger.Create(result); } else { throw new Error.Type(TypeErrorText); } } }
private PartitionJobInfo CreatePartitionInfo(AType right, AType left) { // left side type must be float, integer or null if (left.Type != ATypes.AFloat && left.Type != ATypes.AInteger && left.Type != ATypes.ANull || left.IsBox) { throw new Error.Type(TypeErrorText); } // if the right side is scalar then we throw Rank error exception if (right.Rank < 1) { throw new Error.Rank(RankErrorText); } int element; List<int> partitionVector = new List<int>(); int remainder = 0; int sum = 0; bool going = true; // TODO: check if this branch can be eliminated if the argument is raveled always if (left.IsArray) { // TODO: Check this: // if the left rank is higher than 1 then we ravel it to vector. AType raveled_y = left.Rank > 1 ? MonadicFunctionInstance.Ravel.Execute(left) : left; // get the integer list from the right side foreach (AType item in raveled_y) { // if the actual item is float then we convert it to (if it's a restricted whole number) if (!item.ConvertToRestrictedWholeNumber(out element)) { throw new Error.Type(TypeErrorText); } if (element < 0) { // negative item raise domain error throw new Error.Domain(DomainErrorText); } sum += element; if (right.Length > 0 && going) { // compute how many item we can take from the list at last if (sum > right.Length) { partitionVector.Add(right.Length - (sum - element)); going = false; } else { // collect the item to y list partitionVector.Add(element); if (sum == right.Length) { going = false; } } } else { // count the empty elements what we need remainder++; } } } else { // this case is when the left side is scalar if (!left.ConvertToRestrictedWholeNumber(out element)) { throw new Error.Type(TypeErrorText); } if (element < 0) { throw new Error.Domain(DomainErrorText); } else if (element != 0) { int round = right.Length / element; for (int i = 0; i < round; i++) { partitionVector.Add(element); } int rem = right.Length % element; if (rem != 0) { partitionVector.Add(rem); } } else { partitionVector.Add(0); } } PartitionJobInfo info = new PartitionJobInfo(remainder, partitionVector.ToArray()); return info; }