Ejemplo n.º 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 FieldMatrix <U> getInverse()
            {
                int             m        = pivot.Length;
                U               one      = field.getOne();
                FieldMatrix <U> identity = new Array2DRowFieldMatrix <U>(field, m, m);

                for (int i = 0; i < m; ++i)
                {
                    identity.setEntry(i, i, one);
                }
                return(solve(identity));
            }
        /// <summary>
        /// Compute the outer product.
        /// </summary>
        /// <param name="v">vector with which outer product should be computed</param>
        /// <returns>the matrix outer product between instance and v</returns>
        public FieldMatrix <T> outerProduct(ArrayFieldVector <T> v)
        {
            int             m    = data.Length;
            int             n    = v.data.Length;
            FieldMatrix <T> outp = new Array2DRowFieldMatrix <T>(field, m, n);

            for (int i = 0; i < m; i++)
            {
                for (int j = 0; j < n; j++)
                {
                    outp.setEntry(i, j, data[i].multiply(v.data[j]));
                }
            }
            return(outp);
        }
 /// <inheritdoc/>
 public FieldMatrix <T> outerProduct(FieldVector <T> v)
 {
     try
     {
         return(outerProduct((ArrayFieldVector <T>)v));
     }
     catch (InvalidCastException)
     {
         int             m    = data.Length;
         int             n    = v.getDimension();
         FieldMatrix <T> outp = new Array2DRowFieldMatrix <T>(field, m, n);
         for (int i = 0; i < m; i++)
         {
             for (int j = 0; j < n; j++)
             {
                 outp.setEntry(i, j, data[i].multiply(v.getEntry(j)));
             }
         }
         return(outp);
     }
 }
Ejemplo n.º 5
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));
        }