/// <inheritdoc/>
        /// <exception cref="NumberIsTooLargeException"> if <c>m</c> is an
        /// <c>OpenMapRealMatrix</c>, and the total number of entries of the product
        /// is larger than <c>Int32.MaxValue</c>.</exception>
        public new RealMatrix multiply(RealMatrix m)
        {
            try
            {
                return(multiply((OpenMapRealMatrix)m));
            }
            catch (InvalidCastException)
            {
                MatrixUtils.checkMultiplicationCompatible(this, m);

                int             outCols = m.getColumnDimension();
                BlockRealMatrix outp    = new BlockRealMatrix(rows, outCols);
                for (OpenIntToDoubleHashMap.Iterator iterator = entries.iterator(); iterator.MoveNext();)
                {
                    double value = iterator.Current.Value;
                    int    key   = iterator.Current.Key;
                    int    i     = key / columns;
                    int    k     = key % columns;
                    for (int j = 0; j < outCols; ++j)
                    {
                        outp.addToEntry(i, j, value * m.getEntry(k, j));
                    }
                }
                return(outp);
            }
        }
예제 #2
0
        /// <summary>
        /// Returns the result of postmultiplying <c>this</c> by <c>m</c>.
        /// </summary>
        /// <param name="m">matrix to postmultiply by</param>
        /// <returns><c>this * m</c></returns>
        /// <exception cref="DimensionMismatchException"> if
        /// <c>columnDimension(this) != rowDimension(m)</c></exception>
        public DiagonalMatrix multiply(DiagonalMatrix m)
        {
            MatrixUtils.checkMultiplicationCompatible(this, m);

            int dim = getRowDimension();

            double[] outData = new double[dim];
            for (int i = 0; i < dim; i++)
            {
                outData[i] = data[i] * m.data[i];
            }

            return(new DiagonalMatrix(outData, false));
        }
예제 #3
0
 /// <summary>
 /// Returns the result of postmultiplying <c>this</c> by <c>m</c>.
 /// </summary>
 /// <param name="m">matrix to postmultiply by</param>
 /// <returns><c>this * m</c></returns>
 /// <exception cref="DimensionMismatchException"> if
 /// <c>columnDimension(this) != rowDimension(m)</c></exception>
 public new RealMatrix multiply(RealMatrix m)
 {
     if (m is DiagonalMatrix)
     {
         return(multiply((DiagonalMatrix)m));
     }
     else
     {
         MatrixUtils.checkMultiplicationCompatible(this, m);
         int        nRows   = m.getRowDimension();
         int        nCols   = m.getColumnDimension();
         double[][] product = new double[nRows][];
         for (int r = 0; r < nRows; r++)
         {
             for (int c = 0; c < nCols; c++)
             {
                 product[r][c] = data[r] * m.getEntry(r, c);
             }
         }
         return(new Array2DRowRealMatrix(product, false));
     }
 }
예제 #4
0
        /// <summary>
        /// Returns the result of postmultiplying <c>this</c> by <c>m</c>.
        /// </summary>
        /// <param name="m">matrix to postmultiply by</param>
        /// <returns><c>this * m</c></returns>
        /// <exception cref="DimensionMismatchException"> if
        /// <c>columnDimension(this) != rowDimension(m)</c></exception>
        public Array2DRowRealMatrix multiply(Array2DRowRealMatrix m)
        {
            MatrixUtils.checkMultiplicationCompatible(this, m);

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

            double[][] outData = new double[nRows][];
            // Will hold a column of "m".
            double[]   mCol  = new double[nSum];
            double[][] mData = m.data;

            // Multiply.
            for (int col = 0; col < nCols; col++)
            {
                // Copy all elements of column "col" of "m" so that
                // will be in contiguous memory.
                for (int mRow = 0; mRow < nSum; mRow++)
                {
                    mCol[mRow] = mData[mRow][col];
                }

                for (int row = 0; row < nRows; row++)
                {
                    double[] dataRow = data[row];
                    double   sum     = 0;
                    for (int i = 0; i < nSum; i++)
                    {
                        sum += dataRow[i] * mCol[i];
                    }
                    outData[row][col] = sum;
                }
            }

            return(new Array2DRowRealMatrix(outData, false));
        }
        /// <summary>
        /// Postmultiply this matrix by <c>m</c>.
        /// </summary>
        /// <param name="m">Matrix to postmultiply by.</param>
        /// <returns><c>this</c> * <c>m</c>.</returns>
        /// <exception cref="DimensionMismatchException"> if the number of rows of <c>m</c>
        /// differ from the number of columns of <c>this</c> matrix.</exception>
        /// <exception cref="NumberIsTooLargeException"> if the total number of entries of the
        /// product is larger than <c>Int32.MaxValue</c>.</exception>
        public OpenMapRealMatrix multiply(OpenMapRealMatrix m)
        {
            // Safety check.
            MatrixUtils.checkMultiplicationCompatible(this, m);

            int outCols            = m.getColumnDimension();
            OpenMapRealMatrix outp = new OpenMapRealMatrix(rows, outCols);

            for (OpenIntToDoubleHashMap.Iterator iterator = entries.iterator(); iterator.MoveNext();)
            {
                double value = iterator.Current.Value;
                int    key   = iterator.Current.Key;
                int    i     = key / columns;
                int    k     = key % columns;
                for (int j = 0; j < outCols; ++j)
                {
                    int rightKey = m.computeKey(k, j);
                    if (m.entries.containsKey(rightKey))
                    {
                        int    outKey   = outp.computeKey(i, j);
                        double outValue =
                            outp.entries.get(outKey) + value * m.entries.get(rightKey);
                        if (outValue == 0.0)
                        {
                            outp.entries.remove(outKey);
                        }
                        else
                        {
                            outp.entries.put(outKey, outValue);
                        }
                    }
                }
            }

            return(outp);
        }
        /// <inheritdoc/>
        public RealMatrix multiply(RealMatrix m)
        {
            MatrixUtils.checkMultiplicationCompatible(this, m);

            int        nRows = getRowDimension();
            int        nCols = m.getColumnDimension();
            int        nSum  = getColumnDimension();
            RealMatrix outp  = createMatrix(nRows, nCols);

            for (int row = 0; row < nRows; ++row)
            {
                for (int col = 0; col < nCols; ++col)
                {
                    double sum = 0;
                    for (int i = 0; i < nSum; ++i)
                    {
                        sum += getEntry(row, i) * m.getEntry(i, col);
                    }
                    outp.setEntry(row, col, sum);
                }
            }

            return(outp);
        }