예제 #1
0
 protected Boolean test(
     double[] values,
     int begin,
     int length)
 {
     return(MathArrays.verifyValues(values, begin, length, false));
 }
 /// <inheritdoc/>
 public FieldVector <T> append(T inp)
 {
     T[] outp = MathArrays.buildArray(field, data.Length + 1);
     Array.Copy(data, 0, outp, 0, data.Length);
     outp[data.Length] = inp;
     return(new ArrayFieldVector <T>(field, outp, false));
 }
        /// <inheritdoc/>
        public FieldVector <T> preMultiply(FieldVector <T> v)
        {
            try
            {
                return(new ArrayFieldVector <T>(field, preMultiply(((ArrayFieldVector <T>)v).getDataRef()), false));
            }
            catch (InvalidCastException)
            {
                int nRows = getRowDimension();
                int nCols = getColumnDimension();
                if (v.getDimension() != nRows)
                {
                    throw new DimensionMismatchException(v.getDimension(), nRows);
                }

                T[] outp = MathArrays.buildArray(field, nCols);
                for (int col = 0; col < nCols; ++col)
                {
                    T sum = field.getZero();
                    for (int i = 0; i < nRows; ++i)
                    {
                        sum = sum.add(getEntry(i, col).multiply(v.getEntry(i)));
                    }
                    outp[col] = sum;
                }

                return(new ArrayFieldVector <T>(field, outp, false));
            }
        }
예제 #4
0
        /// <summary>
        /// Postmultiplying this matrix by <c>m</c>.
        /// </summary>
        /// <param name="m">Matrix to postmultiply by.</param>
        /// <returns><c>this</c> * m.</returns>
        /// <exception cref="DimensionMismatchException"> if the number of columns of this
        /// matrix is not equal to the number of rows of <c>m</c>.</exception>
        public Array2DRowFieldMatrix <T> multiply(Array2DRowFieldMatrix <T> m)
        {
            // safety check
            checkMultiplicationCompatible(m);

            int nRows = this.getRowDimension();
            int nCols = m.getColumnDimension();
            int nSum  = this.getColumnDimension();

            T[][] outData = MathArrays.buildArray(getField(), nRows, nCols);
            for (int row = 0; row < nRows; row++)
            {
                T[] dataRow    = data[row];
                T[] outDataRow = outData[row];
                for (int col = 0; col < nCols; col++)
                {
                    T sum = getField().getZero();
                    for (int i = 0; i < nSum; i++)
                    {
                        sum = sum.add(dataRow[i].multiply(m.data[i][col]));
                    }
                    outDataRow[col] = sum;
                }
            }

            return(new Array2DRowFieldMatrix <T>(getField(), outData, false));
        }
        /// <inheritdoc/>
        /// <exception cref="DimensionMismatchException"> if number of free parameters
        /// or orders do not match</exception>
        public DerivativeStructure linearCombination(DerivativeStructure[] a, DerivativeStructure[] b)
        {
            // compute an accurate value, taking care of cancellations
            double[] aDouble = new double[a.Length];
            for (int i = 0; i < a.Length; ++i)
            {
                aDouble[i] = a[i].getValue();
            }
            double[] bDouble = new double[b.Length];
            for (int i = 0; i < b.Length; ++i)
            {
                bDouble[i] = b[i].getValue();
            }
            double accurateValue = MathArrays.linearCombination(aDouble, bDouble);

            // compute a simple value, with all partial derivatives
            DerivativeStructure simpleValue = a[0].getField().getZero();

            for (int i = 0; i < a.Length; ++i)
            {
                simpleValue = simpleValue.add(a[i].multiply(b[i]));
            }

            // create a result with accurate value and all derivatives (not necessarily as accurate as the value)
            double[] all = simpleValue.getAllDerivatives();
            all[0] = accurateValue;
            return(new DerivativeStructure(simpleValue.getFreeParameters(), simpleValue.getOrder(), all));
        }
            /// <inheritdoc cref="Compare"/>
            public int compare(int[] c1, int[] c2)
            {
                if (c1.Length != k)
                {
                    throw new DimensionMismatchException(c1.Length, k);
                }
                if (c2.Length != k)
                {
                    throw new DimensionMismatchException(c2.Length, k);
                }

                // Method "lexNorm" works with ordered arrays.
                int[] c1s = MathArrays.copyOf(c1);
                Array.Sort(c1s);
                int[] c2s = MathArrays.copyOf(c2);
                Array.Sort(c2s);

                long v1 = lexNorm(c1s);
                long v2 = lexNorm(c2s);

                if (v1 < v2)
                {
                    return(-1);
                }
                else if (v1 > v2)
                {
                    return(1);
                }
                else
                {
                    return(0);
                }
            }
 /// <inheritdoc/>
 public FieldVector <T> mapMultiply(T d)
 {
     T[] outp = MathArrays.buildArray(field, data.Length);
     for (int i = 0; i < data.Length; i++)
     {
         outp[i] = data[i].multiply(d);
     }
     return(new ArrayFieldVector <T>(field, outp, false));
 }
 /// <summary>
 /// Element-by-element multiplication.
 /// </summary>
 /// <param name="v">vector by which instance elements must be multiplied</param>
 /// <returns>vector containing <c>this[i] * v[i]</c> for all <c>i</c></returns>
 /// <exception cref="DimensionMismatchException"> if <c>v</c> is not the same size as
 /// <c>this</c></exception>
 public ArrayFieldVector <T> ebeMultiply(ArrayFieldVector <T> v)
 {
     checkVectorDimensions(v.data.Length);
     T[] outp = MathArrays.buildArray(field, data.Length);
     for (int i = 0; i < data.Length; i++)
     {
         outp[i] = data[i].multiply(v.data[i]);
     }
     return(new ArrayFieldVector <T>(field, outp, false));
 }
 /// <inheritdoc/>
 public FieldVector <T> mapDivide(T d)
 {
     MathUtils.checkNotNull(d);
     T[] outp = MathArrays.buildArray(field, data.Length);
     for (int i = 0; i < data.Length; i++)
     {
         outp[i] = data[i].divide(d);
     }
     return(new ArrayFieldVector <T>(field, outp, false));
 }
 /// <summary>
 /// Construct a vector from another vector, using a deep copy.
 /// </summary>
 /// <param name="v">Vector to copy.</param>
 /// <exception cref="NullArgumentException"> if <c>v</c> is <c>null</c>.</exception>
 public ArrayFieldVector(FieldVector <T> v)
 {
     MathUtils.checkNotNull(v);
     field = v.getField();
     data  = MathArrays.buildArray(field, v.getDimension());
     for (int i = 0; i < data.Length; ++i)
     {
         data[i] = v.getEntry(i);
     }
 }
 /// <summary>
 /// Construct a vector by appending one vector to another vector.
 /// </summary>
 /// <param name="v1">First vector (will be put in front of the new vector).</param>
 /// <param name="v2">Second vector (will be put at back of the new vector).</param>
 /// <exception cref="NullArgumentException"> if <c>v1</c> or <c>v2</c> is
 /// <c>null</c>.</exception>
 public ArrayFieldVector(T[] v1, FieldVector <T> v2)
 {
     MathUtils.checkNotNull(v1);
     MathUtils.checkNotNull(v2);
     field = v2.getField();
     T[] v2Data = (v2 is ArrayFieldVector <T>) ? ((ArrayFieldVector <T>)v2).data : v2.toArray();
     data = MathArrays.buildArray(field, v1.Length + v2Data.Length);
     Array.Copy(v1, 0, data, 0, v1.Length);
     Array.Copy(v2Data, 0, data, v1.Length, v2Data.Length);
 }
 /// <summary>
 /// Construct a vector from part of a array.
 /// </summary>
 /// <param name="field">Field to which the elements belong.</param>
 /// <param name="d">Array.</param>
 /// <param name="pos">Position of the first entry.</param>
 /// <param name="size">Number of entries to copy.</param>
 /// <exception cref="NullArgumentException"> if <c>d</c> is <c>null</c>.</exception>
 /// <exception cref="NumberIsTooLargeException"> if the size of <c>d</c> is less
 /// than <c>pos + size</c>.</exception>
 public ArrayFieldVector(Field <T> field, T[] d, int pos, int size)
 {
     MathUtils.checkNotNull(d);
     if (d.Length < pos + size)
     {
         throw new NumberIsTooLargeException <Int32, Int32>(pos + size, d.Length, true);
     }
     this.field = field;
     data       = MathArrays.buildArray(field, size);
     Array.Copy(d, pos, data, 0, size);
 }
예제 #13
0
        /// <summary>
        /// Create a new (column) <c>FieldMatrix<T></c> using <c>v</c> as the
        /// data for the unique column of the created matrix.
        /// The input array is copied.
        /// </summary>
        /// <param name="field">Field to which the elements belong.</param>
        /// <param name="v">Column vector holding data for new matrix.</param>
        public Array2DRowFieldMatrix(Field <T> field, T[] v)
            : base(field)
        {
            int nRows = v.Length;

            data = MathArrays.buildArray(getField(), nRows, 1);
            for (int row = 0; row < nRows; row++)
            {
                data[row][0] = v[row];
            }
        }
예제 #14
0
        /// <summary>
        /// Creates an integrator from the given {@code points} and {@code weights}.
        /// The integration interval is defined by the first and last value of
        /// {@code points} which must be sorted in increasing order.
        /// </summary>
        /// <param name="points"> Integration points. </param>
        /// <param name="weights"> Weights of the corresponding integration nodes. </param>
        /// <exception cref="NonMonotonicSequenceException"> if the {@code points} are not
        /// sorted in increasing order. </exception>
        /// <exception cref="DimensionMismatchException"> if points and weights don't have the same length </exception>
        public GaussIntegrator(double[] points, double[] weights)
        {
            if (points.Length != weights.Length)
            {
                throw new DimensionMismatchException(points.Length, weights.Length);
            }

            MathArrays.CheckOrder(points, MathArrays.OrderDirection.INCREASING, true, true);

            this.points  = (double[])points.Clone();
            this.weights = (double[])weights.Clone();
        }
        /// <inheritdoc/>
        public T[] getColumn(int column)
        {
            checkColumnIndex(column);
            int nRows = getRowDimension();

            T[] outp = MathArrays.buildArray(field, nRows);
            for (int i = 0; i < nRows; ++i)
            {
                outp[i] = getEntry(i, column);
            }
            return(outp);
        }
예제 #16
0
        /// <summary>
        /// Get a fresh copy of the underlying data array.
        /// </summary>
        /// <returns>a copy of the underlying data array.</returns>
        private T[][] copyOut()
        {
            int nRows = this.getRowDimension();

            T[][] outp = MathArrays.buildArray(getField(), nRows, getColumnDimension());
            // can't copy 2-d array in one shot, otherwise get row references
            for (int i = 0; i < nRows; i++)
            {
                Array.Copy(data[i], 0, outp[i], 0, data[i].Length);
            }
            return(outp);
        }
 /// <summary>
 /// Construct a vector by appending one vector to another vector.
 /// </summary>
 /// <param name="field">Field to which the elements belong.</param>
 /// <param name="v1">First vector (will be put in front of the new vector).</param>
 /// <param name="v2">Second vector (will be put at back of the new vector).</param>
 /// <exception cref="NullArgumentException"> if <c>v1</c> or <c>v2</c> is
 /// <c>null</c>.</exception>
 /// <exception cref="ZeroException"> if both arrays are empty.</exception>
 /// <remarks>
 /// See <see cref="ArrayFieldVector(FieldElement[], FieldElement[])"/>
 /// </remarks>
 public ArrayFieldVector(Field <T> field, T[] v1, T[] v2)
 {
     MathUtils.checkNotNull(v1);
     MathUtils.checkNotNull(v2);
     if (v1.Length + v2.Length == 0)
     {
         throw new ZeroException(new LocalizedFormats("VECTOR_MUST_HAVE_AT_LEAST_ONE_ELEMENT"));
     }
     data = MathArrays.buildArray(field, v1.Length + v2.Length);
     Array.Copy(v1, 0, data, 0, v1.Length);
     Array.Copy(v2, 0, data, v1.Length, v2.Length);
     this.field = field;
 }
        /// <inheritdoc/>
        public T[] getRow(int row)
        {
            checkRowIndex(row);
            int nCols = getColumnDimension();

            T[] outp = MathArrays.buildArray(field, nCols);
            for (int i = 0; i < nCols; ++i)
            {
                outp[i] = getEntry(row, i);
            }

            return(outp);
        }
        /// <inheritdoc/>
        /// <exception cref="DimensionMismatchException"> if number of free parameters
        /// or orders do not match</exception>
        public DerivativeStructure linearCombination(double a1, DerivativeStructure b1, double a2, DerivativeStructure b2)
        {
            // compute an accurate value, taking care of cancellations
            double accurateValue = MathArrays.linearCombination(a1, b1.getValue(),
                                                                a2, b2.getValue());

            // compute a simple value, with all partial derivatives
            DerivativeStructure simpleValue = b1.multiply(a1).add(b2.multiply(a2));

            // create a result with accurate value and all derivatives (not necessarily as accurate as the value)
            double[] all = simpleValue.getAllDerivatives();
            all[0] = accurateValue;
            return(new DerivativeStructure(getFreeParameters(), getOrder(), all));
        }
            /// <inheritdoc/>
            public FieldVector <U> solve(FieldVector <U> b)
            {
                try
                {
                    return(solve((ArrayFieldVector <U>)b));
                }
                catch (InvalidCastException)
                {
                    int m = pivot.Length;
                    if (b.getDimension() != m)
                    {
                        throw new DimensionMismatchException(b.getDimension(), m);
                    }
                    if (singular)
                    {
                        throw new SingularMatrixException();
                    }

                    // Apply permutations to b
                    U[] bp = MathArrays.buildArray(field, m);
                    for (int row = 0; row < m; row++)
                    {
                        bp[row] = b.getEntry(pivot[row]);
                    }

                    // Solve LY = b
                    for (int col = 0; col < m; col++)
                    {
                        U bpCol = bp[col];
                        for (int i = col + 1; i < m; i++)
                        {
                            bp[i] = bp[i].subtract(bpCol.multiply(lu[i][col]));
                        }
                    }

                    // Solve UX = Y
                    for (int col = m - 1; col >= 0; col--)
                    {
                        bp[col] = bp[col].divide(lu[col][col]);
                        U bpCol = bp[col];
                        for (int i = 0; i < col; i++)
                        {
                            bp[i] = bp[i].subtract(bpCol.multiply(lu[i][col]));
                        }
                    }

                    return(new ArrayFieldVector <U>(field, bp, false));
                }
            }
        /// <inheritdoc/>
        public T[][] getData()
        {
            T[][] data = MathArrays.buildArray(field, getRowDimension(), getColumnDimension());

            for (int i = 0; i < data.Length; ++i)
            {
                T[] dataI = data[i];
                for (int j = 0; j < dataI.Length; ++j)
                {
                    dataI[j] = getEntry(i, j);
                }
            }

            return(data);
        }
        /// <summary>
        /// Test for the equality of two derivative structures.
        /// <para>
        /// Derivative structures are considered equal if they have the same number
        /// of free parameters, the same derivation order, and the same derivatives.
        /// </para>
        /// </summary>
        /// <param name="other">Object to test for equality to this</param>
        /// <returns>true if two derivative structures are equal</returns>
        public override Boolean Equals(Object other)
        {
            if (this == other)
            {
                return(true);
            }

            if (other is DerivativeStructure)
            {
                DerivativeStructure rhs = (DerivativeStructure)other;
                return((getFreeParameters() == rhs.getFreeParameters()) &&
                       (getOrder() == rhs.getOrder()) &&
                       MathArrays.equals(data, rhs.data));
            }
            return(false);
        }
        /// <inheritdoc/>
        /// <remarks>
        /// This method calls <see cref="MathArrays.shuffle(int[],RandomGenerator)"/>
        /// in order to create a random shuffle of the set
        /// of natural numbers <c>{ 0, 1, ..., n - 1 </c>}.
        /// </remarks>
        /// <exception cref="NumberIsTooLargeException"> if <c>k > n</c>.</exception>
        /// <exception cref="NotStrictlyPositiveException"> if <c>k <= 0</c>.</exception>
        public int[] nextPermutation(int n, int k)
        {
            if (k > n)
            {
                throw new NumberIsTooLargeException <Int32, Int32>(new LocalizedFormats("PERMUTATION_EXCEEDS_N"), k, n, true);
            }
            if (k <= 0)
            {
                throw new NotStrictlyPositiveException <Int32>(new LocalizedFormats("PERMUTATION_SIZE"), k);
            }

            int[] index = MathArrays.natural(n);
            MathArrays.shuffle(index, getRandomGenerator());

            // Return a new array containing the first "k" entries of "index".
            return(MathArrays.copyOf(index, k));
        }
 /// <inheritdoc/>
 public FieldVector <T> ebeMultiply(FieldVector <T> v)
 {
     try
     {
         return(ebeMultiply((ArrayFieldVector <T>)v));
     }
     catch (InvalidCastException)
     {
         checkVectorDimensions(v);
         T[] outp = MathArrays.buildArray(field, data.Length);
         for (int i = 0; i < data.Length; i++)
         {
             outp[i] = data[i].multiply(v.getEntry(i));
         }
         return(new ArrayFieldVector <T>(field, outp, false));
     }
 }
 /// <summary>
 /// Element-by-element division.
 /// </summary>
 /// <param name="v">vector by which instance elements must be divided</param>
 /// <returns>a vector containing <c>this[i] / v[i]</c> for all <c>i</c></returns>
 /// <exception cref="DimensionMismatchException"> if <c>v</c> is not the same size as
 /// <c>this</c></exception>
 /// <exception cref="MathArithmeticException"> if one entry of <c>v</c> is zero.
 /// </exception>
 public ArrayFieldVector <T> ebeDivide(ArrayFieldVector <T> v)
 {
     checkVectorDimensions(v.data.Length);
     T[] outp = MathArrays.buildArray(field, data.Length);
     for (int i = 0; i < data.Length; i++)
     {
         try
         {
             outp[i] = data[i].divide(v.data[i]);
         }
         catch (MathArithmeticException)
         {
             throw new MathArithmeticException(new LocalizedFormats("INDEX"), i);
         }
     }
     return(new ArrayFieldVector <T>(field, outp, false));
 }
        /// <summary>
        /// Returns <c>dimension x dimension</c> identity matrix.
        /// </summary>
        /// <typeparam name="T">the type of the field elements</typeparam>
        /// <param name="field">field to which the elements belong</param>
        /// <param name="dimension">dimension of identity matrix to generate</param>
        /// <returns>identity matrix</returns>
        /// <exception cref="IllegalArgumentException"> if dimension is not positive</exception>
        public static FieldMatrix <T> createFieldIdentityMatrix <T>(Field <T> field, int dimension) where T : FieldElement <T>
        {
            T zero = field.getZero();
            T one  = field.getOne();

            T[][] d = MathArrays.buildArray(field, dimension, dimension);
            for (int row = 0; row < dimension; row++)
            {
                T[] dRow = d[row];
                for (int i = 0; i < row; ++i)
                {
                    dRow[i] = zero;
                }
                dRow[row] = one;
            }
            return(new Array2DRowFieldMatrix <T>(field, d, false));
        }
        /// <inheritdoc cref="GetEnumerator"/>
        public IEnumerator <int[]> iterator()
        {
            if (k == 0 ||
                k == n)
            {
                return(new SingletonIterator(MathArrays.natural(k)));
            }

            switch (iterationOrder)
            {
            case IterationOrder.LEXICOGRAPHIC:
                return(new LexicographicIterator(n, k));

            default:
                throw new MathInternalError();     // Should never happen.
            }
        }
        /// <inheritdoc/>
        public FieldVector <T> mapInv()
        {
            T[] outp = MathArrays.buildArray(field, data.Length);
            T   one  = field.getOne();

            for (int i = 0; i < data.Length; i++)
            {
                try
                {
                    outp[i] = one.divide(data[i]);
                }
                catch (MathArithmeticException)
                {
                    throw new MathArithmeticException(new LocalizedFormats("INDEX"), i);
                }
            }
            return(new ArrayFieldVector <T>(field, outp, false));
        }
예제 #29
0
        /// <summary>
        /// Subtract <c>m</c> from this matrix.
        /// </summary>
        /// <param name="m">Matrix to be subtracted.</param>
        /// <returns><c>this</c> + m.</returns>
        /// <exception cref="MatrixDimensionMismatchException"> if <c>m</c> is not the same
        /// size as this matrix.</exception>
        public Array2DRowFieldMatrix <T> subtract(Array2DRowFieldMatrix <T> m)
        {
            // safety check
            checkSubtractionCompatible(m);

            int rowCount    = getRowDimension();
            int columnCount = getColumnDimension();

            T[][] outData = MathArrays.buildArray(getField(), rowCount, columnCount);
            for (int row = 0; row < rowCount; row++)
            {
                T[] dataRow    = data[row];
                T[] mRow       = m.data[row];
                T[] outDataRow = outData[row];
                for (int col = 0; col < columnCount; col++)
                {
                    outDataRow[col] = dataRow[col].subtract(mRow[col]);
                }
            }

            return(new Array2DRowFieldMatrix <T>(getField(), outData, false));
        }
예제 #30
0
        /// <inheritdoc/>
        public new T[] operate(T[] v)
        {
            int nRows = this.getRowDimension();
            int nCols = this.getColumnDimension();

            if (v.Length != nCols)
            {
                throw new DimensionMismatchException(v.Length, nCols);
            }
            T[] outp = MathArrays.buildArray(getField(), nRows);
            for (int row = 0; row < nRows; row++)
            {
                T[] dataRow = data[row];
                T   sum     = getField().getZero();
                for (int i = 0; i < nCols; i++)
                {
                    sum = sum.add(dataRow[i].multiply(v[i]));
                }
                outp[row] = sum;
            }
            return(outp);
        }