Exemplo n.º 1
0
        /**
         * Compute the sum of this matrix and {@code m}.
         *
         * @param m Matrix to be added.
         * @return {@code this} + {@code m}.
         * @throws MatrixDimensionMismatchException if {@code m} is not the same
         * size as {@code this}.
         */
        public OpenMapRealMatrix add(OpenMapRealMatrix m)
        {
            //MatrixUtils.checkAdditionCompatible(this, m);

            OpenMapRealMatrix @out = new OpenMapRealMatrix(this);

            for (OpenIntToDoubleHashMap.Iterator iterator = m.entries.iterator(); iterator.hasNext();)
            {
                iterator.advance();
                int row = iterator.key() / columns;
                int col = iterator.key() - row * columns;
                @out.setEntry(row, col, getEntry(row, col) + iterator.value());
            }

            return(@out);
        }
Exemplo n.º 2
0
        /**
         * Postmultiply this matrix by {@code m}.
         *
         * @param m Matrix to postmultiply by.
         * @return {@code this} * {@code m}.
         * @throws DimensionMismatchException if the number of rows of {@code m}
         * differ from the number of columns of {@code this} matrix.
         * @throws NumberIsTooLargeException if the total number of entries of the
         * product is larger than {@code Integer.MAX_VALUE}.
         */
        public OpenMapRealMatrix multiply(OpenMapRealMatrix m)
        {
            // Safety check.
            //MatrixUtils.checkMultiplicationCompatible(this, m);

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

            for (OpenIntToDoubleHashMap.Iterator iterator = entries.iterator(); iterator.hasNext();)
            {
                iterator.advance();
                double value = iterator.value();
                int    key   = iterator.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   = @out.computeKey(i, j);
                        double outValue =
                            @out.entries.get(outKey) + value * m.entries.get(rightKey);
                        if (outValue == 0.0)
                        {
                            @out.entries.remove(outKey);
                        }
                        else
                        {
                            @out.entries.put(outKey, outValue);
                        }
                    }
                }
            }

            return(@out);
        }
Exemplo n.º 3
0
        /**
         * Compute the outer product.
         *
         * @param v Vector with which outer product should be computed.
         * @return the matrix outer product between this instance and {@code v}.
         */
        public virtual RealMatrix outerProduct(RealVector v)
        {
            //throw new NotImplementedException();
            int        m = getDimension();
            int        n = v.getDimension();
            RealMatrix product;

            if (v is SparseRealVector || this is SparseRealVector)
            {
                product = new OpenMapRealMatrix(m, n);
            }
            else
            {
                product = new Array2DRowRealMatrix(m, n);
            }
            for (int i = 0; i < m; i++)
            {
                for (int j = 0; j < n; j++)
                {
                    product.setEntry(i, j, getEntry(i) * v.getEntry(j));
                }
            }
            return(product);
        }
Exemplo n.º 4
0
 /**
  * Build a matrix by copying another one.
  *
  * @param matrix matrix to copy.
  */
 public OpenMapRealMatrix(OpenMapRealMatrix matrix)
 {
     rows    = matrix.rows;
     columns = matrix.columns;
     entries = new OpenIntToDoubleHashMap(matrix.entries);
 }