Example #1
0
        private byte[] PrepareExpandVector(AType left)
        {
            // if the left side is User defined function, we throw Valence error.
            // this part belongs to Scan.
            if (left.Type == ATypes.AFunc)
            {
                throw new Error.Valence(ValenceErrorText);
            }

            if (!(left.Type == ATypes.AFloat || left.Type == ATypes.AInteger || left.Type == ATypes.ANull))
            {
                throw new Error.Type(TypeErrorText);
            }

            if (left.Rank > 1)
            {
                throw new Error.Rank(RankErrorText);
            }

            //int element;
            AType scalar;

            byte[] expandVector;

            if (left.TryFirstScalar(out scalar, true))
            {
                expandVector = new byte[] { ExtractExpandArgument(scalar) };
            }
            else
            {
                expandVector = left.Select(item => ExtractExpandArgument(item)).ToArray();
            }

            return(expandVector);
        }
Example #2
0
        private byte[] PrepareExpandVector(AType left)
        {
            // if the left side is User defined function, we throw Valence error.
            // this part belongs to Scan.
            if (left.Type == ATypes.AFunc)
            {
                throw new Error.Valence(ValenceErrorText);
            }

            if (!(left.Type == ATypes.AFloat || left.Type == ATypes.AInteger || left.Type == ATypes.ANull))
            {
                throw new Error.Type(TypeErrorText);
            }

            if (left.Rank > 1)
            {
                throw new Error.Rank(RankErrorText);
            }

            //int element;
            AType scalar;

            byte[] expandVector;

            if (left.TryFirstScalar(out scalar, true))
            {
                expandVector = new byte[] { ExtractExpandArgument(scalar) };
            }
            else
            {
                expandVector = left.Select(item => ExtractExpandArgument(item)).ToArray();
            }

            return expandVector;
        }
Example #3
0
        /// <summary>
        /// Extracts <see cref="DecodeInformation"/> from the arguments.
        /// </summary>
        /// <param name="left"></param>
        /// <param name="right"></param>
        /// <returns></returns>
        private DecodeInformation ExtractDecodeInformation(AType left, AType right)
        {
            // Error if the arguments are not numbers or Null
            if (!((left.IsNumber || left.Type == ATypes.ANull) && (right.IsNumber || right.Type == ATypes.ANull)))
            {
                throw new Error.Type(TypeErrorText);
            }

            // righ side must be array
            if (!right.IsArray)
            {
                throw new Error.Rank(RankErrorText);
            }

            // left side must be scalar or vector
            if (left.Rank > 1)
            {
                throw new Error.Rank(RankErrorText);
            }

            ATypes resultType =
                (left.Type == ATypes.AFloat || right.Type == ATypes.AFloat || right.Type == ATypes.ANull)
                ? ATypes.AFloat
                : ATypes.AInteger;

            double[] decodeValues;

            if (left.IsArray)
            {
                if (left.Length == 1)
                {
                    // one-element vector case, then we reshape it: (#x) rho y.
                    decodeValues = Enumerable.Repeat(left[0].asFloat, right.Length).ToArray();
                }
                else
                {
                    // left and right side length have to equal!
                    if (left.Length != right.Length)
                    {
                        throw new Error.Length(LengthErrorText);
                    }

                    decodeValues = left.Select(item => item.asFloat).ToArray();
                }
            }
            else
            {
                // scalar case, reshape it: (#x) rho y.
                decodeValues = Enumerable.Repeat(left.asFloat, right.Length).ToArray();
            }

            return(new DecodeInformation(resultType, decodeValues));
        }
Example #4
0
        private ReplicateJobInfo CreateReplicateJobInfo(AType right, AType left)
        {
            if (!(left.Type == ATypes.AFloat || left.Type == ATypes.AInteger || left.Type == ATypes.ANull))
            {
                throw new Error.Type(TypeErrorText);
            }

            if (left.Rank > 1)
            {
                throw new Error.Rank(RankErrorText);
            }

            int[] replicateVector;
            AType scalar;

            if (left.TryFirstScalar(out scalar, true))
            {
                replicateVector = new int[] { ExtractInteger(scalar) };
            }
            else
            {
                if (left.Length > 0)
                {
                    replicateVector = left.Select(item => ExtractInteger(item)).ToArray();
                }
                else
                {
                    replicateVector = new int[] { 0 };
                }

                // lenght check should be the first than parse the left side,
                // but the A+ follow that order.
                if (right.Length != 1 && left.Length != right.Length)
                {
                    throw new Error.Length(LengthErrorText);
                }
            }

            ReplicateJobInfo info = new ReplicateJobInfo(
                replicateVector,
                right.IsArray ? right : AArray.Create(right.Type, right)
            );

            return info;
        }
Example #5
0
        private ReplicateJobInfo CreateReplicateJobInfo(AType right, AType left)
        {
            if (!(left.Type == ATypes.AFloat || left.Type == ATypes.AInteger || left.Type == ATypes.ANull))
            {
                throw new Error.Type(TypeErrorText);
            }

            if (left.Rank > 1)
            {
                throw new Error.Rank(RankErrorText);
            }

            int[] replicateVector;
            AType scalar;

            if (left.TryFirstScalar(out scalar, true))
            {
                replicateVector = new int[] { ExtractInteger(scalar) };
            }
            else
            {
                if (left.Length > 0)
                {
                    replicateVector = left.Select(item => ExtractInteger(item)).ToArray();
                }
                else
                {
                    replicateVector = new int[] { 0 };
                }

                // lenght check should be the first than parse the left side,
                // but the A+ follow that order.
                if (right.Length != 1 && left.Length != right.Length)
                {
                    throw new Error.Length(LengthErrorText);
                }
            }

            ReplicateJobInfo info = new ReplicateJobInfo(
                replicateVector,
                right.IsArray ? right : AArray.Create(right.Type, right)
                );

            return(info);
        }
Example #6
0
        /// <summary>
        /// Type check, and extract data from left and right side.
        /// </summary>
        /// <param name="left"></param>
        /// <param name="right"></param>
        private EncodeInformation ExtractEncodeInformation(AType left, AType right)
        {
            // Error if the arguments are not numbers or Null
            if (!((left.IsNumber || left.Type == ATypes.ANull) && (right.IsNumber || right.Type == ATypes.ANull)))
            {
                throw new Error.Type(TypeErrorText);
            }

            // Left argument must be a scalar or vector.
            if (left.Rank > 1)
            {
                throw new Error.Rank(RankErrorText);
            }

            double[] encodeKeys;
            if (left.IsArray)
            {
                encodeKeys = left.Select(item => item.asFloat).ToArray();
            }
            else
            {
                encodeKeys = new double[] { left.asFloat };
            }

            ATypes resultingType;

            if (left.Type == ATypes.AFloat || right.Type == ATypes.AFloat ||
                left.Type == ATypes.ANull || right.Type == ATypes.ANull)
            {
                resultingType = ATypes.AFloat;
            }
            else
            {
                resultingType = ATypes.AInteger;
            }

            List <double> encodeValues = new List <double>();

            ExtractItems(encodeValues, right);

            EncodeInformation arguments = new EncodeInformation(encodeKeys, encodeValues.ToArray(), resultingType);

            return(arguments);
        }
Example #7
0
        /// <summary>
        /// Type check, and extract data from left and right side.
        /// </summary>
        /// <param name="left"></param>
        /// <param name="right"></param>
        private EncodeInformation ExtractEncodeInformation(AType left, AType right)
        {
            // Error if the arguments are not numbers or Null
            if (!((left.IsNumber || left.Type == ATypes.ANull) && (right.IsNumber || right.Type == ATypes.ANull))) 
            {
                throw new Error.Type(TypeErrorText);
            }

            // Left argument must be a scalar or vector.
            if (left.Rank > 1)
            {
                throw new Error.Rank(RankErrorText);
            }

            double[] encodeKeys;
            if (left.IsArray)
            {
                encodeKeys = left.Select(item => item.asFloat).ToArray();
            }
            else
            {
                encodeKeys = new double[] { left.asFloat };
            }

            ATypes resultingType;
            if (left.Type == ATypes.AFloat || right.Type == ATypes.AFloat ||
                left.Type == ATypes.ANull || right.Type == ATypes.ANull)
            {
                resultingType = ATypes.AFloat;
            }
            else
            {
                resultingType = ATypes.AInteger;
            }

            List<double> encodeValues = new List<double>();
            ExtractItems(encodeValues, right);

            EncodeInformation arguments = new EncodeInformation(encodeKeys, encodeValues.ToArray(), resultingType);
            return arguments;
        }
Example #8
0
        public override AType Execute(AType rightArgument, AType leftArgument, Aplus environment = null)
        {
            if (leftArgument.Type != ATypes.AInteger)
            {
                throw new Error.Type(this.TypeErrorText);
            }

            AType left  = Function.Monadic.MonadicFunctionInstance.Ravel.Execute(leftArgument);
            AType right = Function.Monadic.MonadicFunctionInstance.Ravel.Execute(rightArgument);

            AType result;

            switch (CheckVector(left))
            {
            case State.NullFound:
                // Found a zero in the list, create an emtpy list with correct shape
                result        = CreateResult(left, right);
                result.Shape  = new List <int>(left.Select(item => { return(item.asInteger); }));
                result.Rank   = result.Shape.Count;
                result.Length = result.Shape[0];
                break;

            case State.DomainError:
                throw new Error.Domain(this.DomainErrorText);

            case State.MaxRankError:
                throw new Error.MaxRank(this.MaxRankErrorText);

            default:
            case State.OK:
                result = CreateResult(left, right);
                break;
            }

            return(result);
        }
Example #9
0
        /// <summary>
        /// Extracts <see cref="DecodeInformation"/> from the arguments.
        /// </summary>
        /// <param name="left"></param>
        /// <param name="right"></param>
        /// <returns></returns>
        private DecodeInformation ExtractDecodeInformation(AType left, AType right)
        {
            // Error if the arguments are not numbers or Null
            if (!((left.IsNumber || left.Type == ATypes.ANull) && (right.IsNumber || right.Type == ATypes.ANull))) 
            {
                throw new Error.Type(TypeErrorText);
            }

            // righ side must be array
            if (!right.IsArray)
            {
                throw new Error.Rank(RankErrorText);
            }

            // left side must be scalar or vector
            if (left.Rank > 1)
            {
                throw new Error.Rank(RankErrorText);
            }

            ATypes resultType = 
                (left.Type == ATypes.AFloat || right.Type == ATypes.AFloat || right.Type == ATypes.ANull)
                ? ATypes.AFloat
                : ATypes.AInteger;

            double[] decodeValues;

            if (left.IsArray)
            {
                if (left.Length == 1)
                {
                    // one-element vector case, then we reshape it: (#x) rho y.
                    decodeValues = Enumerable.Repeat(left[0].asFloat, right.Length).ToArray();
                }
                else
                {
                    // left and right side length have to equal!
                    if (left.Length != right.Length)
                    {
                        throw new Error.Length(LengthErrorText);
                    }

                    decodeValues = left.Select(item => item.asFloat).ToArray();
                }
            }
            else
            {
                // scalar case, reshape it: (#x) rho y.
                decodeValues = Enumerable.Repeat(left.asFloat, right.Length).ToArray();
            }

            return new DecodeInformation(resultType, decodeValues);
        }
Example #10
0
        private AType SolveEquation(AType constants, AType equations)
        {
            AType lhs;
            AType rhs;

            if (constants.TryFirstScalar(out lhs, true) && equations.TryFirstScalar(out rhs, true))
            {
                // both left and right values are one element arrays.
                return AFloat.Create(lhs.asFloat / rhs.asFloat);
            }

            Matrix constantsArray = new SimpleMatrix(ExtractConstants(constants));
            Matrix originalEquations = new SimpleMatrix(FloatFromAType(equations));
            int[] rowsSequence;
            Matrix eliminatedConstants;
            GaussianElimination(originalEquations, constantsArray, out rowsSequence, out eliminatedConstants);

            AType result = AArray.Create(ATypes.AFloat);

            if (equations.Shape[0] == equations.Shape[1])
            {
                // square equation
                if (constants.Rank > 1)
                {
                    foreach (int item in rowsSequence)
                    {
                        AType subArray = AArray.Create(ATypes.AFloat);

                        for (int i = 0; i < eliminatedConstants.Columns; i++)
                        {
                            subArray.Add(AFloat.Create(eliminatedConstants[item, i]));
                        }

                        result.Add(subArray);
                    }
                }
                else
                {
                    foreach (int item in rowsSequence)
                    {
                        result.Add(AFloat.Create(eliminatedConstants[item, 0]));
                    }
                }
            }
            else
            {
                double[][] independentConstants = BuildIndependentConstants(rowsSequence, eliminatedConstants);
                double[] beta;
                double[] actualconstants;

                if (constants.Rank == 1)
                {
                    beta = independentConstants.Select(item => item[0]).ToArray();
                    actualconstants = constants.Select(item => item.asFloat).ToArray();

                    double[] solution = OverDeterminedEquationSolve(beta, actualconstants, originalEquations);

                    foreach (double item in solution)
                    {
                        result.Add(AFloat.Create(item));
                    }
                }
                else
                {
                    for (int objective = 0; objective < constants.Shape[1]; objective++)
                    {
                        beta = independentConstants.Select(item => item[objective]).ToArray();
                        actualconstants = constants.Select(item => item[objective].asFloat).ToArray();

                        double[] solution = OverDeterminedEquationSolve(beta, actualconstants, originalEquations);
                        AType solutionArray = AArray.Create(ATypes.AFloat);

                        foreach (double item in solution)
                        {
                            solutionArray.Add(AFloat.Create(item));
                        }

                        result.Add(solutionArray);
                    }
                }
            }

            return result;
        }
Example #11
0
        private AType SolveEquation(AType constants, AType equations)
        {
            AType lhs;
            AType rhs;

            if (constants.TryFirstScalar(out lhs, true) && equations.TryFirstScalar(out rhs, true))
            {
                // both left and right values are one element arrays.
                return(AFloat.Create(lhs.asFloat / rhs.asFloat));
            }

            Matrix constantsArray    = new SimpleMatrix(ExtractConstants(constants));
            Matrix originalEquations = new SimpleMatrix(FloatFromAType(equations));

            int[]  rowsSequence;
            Matrix eliminatedConstants;

            GaussianElimination(originalEquations, constantsArray, out rowsSequence, out eliminatedConstants);

            AType result = AArray.Create(ATypes.AFloat);

            if (equations.Shape[0] == equations.Shape[1])
            {
                // square equation
                if (constants.Rank > 1)
                {
                    foreach (int item in rowsSequence)
                    {
                        AType subArray = AArray.Create(ATypes.AFloat);

                        for (int i = 0; i < eliminatedConstants.Columns; i++)
                        {
                            subArray.Add(AFloat.Create(eliminatedConstants[item, i]));
                        }

                        result.Add(subArray);
                    }
                }
                else
                {
                    foreach (int item in rowsSequence)
                    {
                        result.Add(AFloat.Create(eliminatedConstants[item, 0]));
                    }
                }
            }
            else
            {
                double[][] independentConstants = BuildIndependentConstants(rowsSequence, eliminatedConstants);
                double[]   beta;
                double[]   actualconstants;

                if (constants.Rank == 1)
                {
                    beta            = independentConstants.Select(item => item[0]).ToArray();
                    actualconstants = constants.Select(item => item.asFloat).ToArray();

                    double[] solution = OverDeterminedEquationSolve(beta, actualconstants, originalEquations);

                    foreach (double item in solution)
                    {
                        result.Add(AFloat.Create(item));
                    }
                }
                else
                {
                    for (int objective = 0; objective < constants.Shape[1]; objective++)
                    {
                        beta            = independentConstants.Select(item => item[objective]).ToArray();
                        actualconstants = constants.Select(item => item[objective].asFloat).ToArray();

                        double[] solution      = OverDeterminedEquationSolve(beta, actualconstants, originalEquations);
                        AType    solutionArray = AArray.Create(ATypes.AFloat);

                        foreach (double item in solution)
                        {
                            solutionArray.Add(AFloat.Create(item));
                        }

                        result.Add(solutionArray);
                    }
                }
            }

            return(result);
        }