Exemple #1
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/>
 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));
            }
        }
 /// <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>
 /// 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);
 }
 /// <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>
 /// 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));
 }
 /// <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);
 }
Exemple #10
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];
            }
        }
        /// <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);
        }
Exemple #12
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/>
            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>
 /// 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));
 }
 /// <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>
        /// 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/>
        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));
        }
Exemple #21
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));
        }
Exemple #22
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);
        }
Exemple #23
0
        /// <inheritdoc/>
        public new void setSubMatrix(T[][] subMatrix, int row, int column)
        {
            if (data == null)
            {
                if (row > 0)
                {
                    throw new MathIllegalStateException(new LocalizedFormats("FIRST_ROWS_NOT_INITIALIZED_YET"), row);
                }
                if (column > 0)
                {
                    throw new MathIllegalStateException(new LocalizedFormats("FIRST_COLUMNS_NOT_INITIALIZED_YET"), column);
                }
                int nRows = subMatrix.Length;
                if (nRows == 0)
                {
                    throw new NoDataException(new LocalizedFormats("AT_LEAST_ONE_ROW"));
                }

                int nCols = subMatrix[0].Length;
                if (nCols == 0)
                {
                    throw new NoDataException(new LocalizedFormats("AT_LEAST_ONE_COLUMN"));
                }
                data = MathArrays.buildArray(getField(), subMatrix.Length, nCols);
                for (int i = 0; i < data.Length; ++i)
                {
                    if (subMatrix[i].Length != nCols)
                    {
                        throw new DimensionMismatchException(nCols, subMatrix[i].Length);
                    }
                    Array.Copy(subMatrix[i], 0, data[i + row], column, nCols);
                }
            }
            else
            {
                base.setSubMatrix(subMatrix, row, column);
            }
        }
        /// <inheritdoc/>
        public T[] operate(T[] v)
        {
            int nRows = getRowDimension();
            int nCols = getColumnDimension();

            if (v.Length != nCols)
            {
                throw new DimensionMismatchException(v.Length, nCols);
            }

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

            return(outp);
        }
Exemple #25
0
        /// <inheritdoc/>
        public new T[] preMultiply(T[] v)
        {
            int nRows = getRowDimension();
            int nCols = getColumnDimension();

            if (v.Length != nRows)
            {
                throw new DimensionMismatchException(v.Length, nRows);
            }

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

            return(outp);
        }
Exemple #26
0
 /// <summary>
 /// Create a new <c>FieldMatrix(T)</c> with the supplied row and column dimensions.
 /// </summary>
 /// <param name="field">Field to which the elements belong.</param>
 /// <param name="rowDimension">Number of rows in the new matrix.</param>
 /// <param name="columnDimension">Number of columns in the new matrix.</param>
 /// <exception cref="NotStrictlyPositiveException"> if row or column dimension
 /// is not positive.</exception>
 public Array2DRowFieldMatrix(Field <T> field, int rowDimension, int columnDimension)
     : base(field, rowDimension, columnDimension)
 {
     data = MathArrays.buildArray(field, rowDimension, columnDimension);
 }
 protected static U[][] buildArray <U>(Field <U> field, int rows, int columns) where U : FieldElement <U>
 {
     return(MathArrays.buildArray(field, rows, columns));
 }
 /// <summary>
 /// Construct a vector of zeroes.
 /// </summary>
 /// <param name="field">Field to which the elements belong.</param>
 /// <param name="size">Size of the vector.</param>
 public ArrayFieldVector(Field <T> field, int size)
 {
     this.field = field;
     this.data  = MathArrays.buildArray(field, size);
 }
 protected static U[] buildArray <U>(Field <U> field, int length) where U : FieldElement <U>
 {
     return(MathArrays.buildArray(field, length));
 }
            /// <inheritdoc/>
            public FieldMatrix <U> solve(FieldMatrix <U> b)
            {
                int m = pivot.Length;

                if (b.getRowDimension() != m)
                {
                    throw new DimensionMismatchException(b.getRowDimension(), m);
                }
                if (singular)
                {
                    throw new SingularMatrixException();
                }

                int nColB = b.getColumnDimension();

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

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

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

                return(new Array2DRowFieldMatrix <U>(field, bp, false));
            }