Beispiel #1
0
        /// <summary>
        /// Compute the sum of <c>this</c> and <c>m</c>.
        /// </summary>
        /// <param name="m">Matrix to be added.</param>
        /// <returns><c>this + m</c>.</returns>
        /// <exception cref="MatrixDimensionMismatchException"> if <c>m</c> is not the same
        /// size as <c>this</c>.</exception>
        public DiagonalMatrix add(DiagonalMatrix m)
        {
            // Safety check.
            MatrixUtils.checkAdditionCompatible(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));
        }
        /// <summary>
        /// Subtract <c>m</c> from this matrix.
        /// </summary>
        /// <param name="m">Matrix to be subtracted.</param>
        /// <returns><c>this</c> - <c>m</c>.</returns>
        /// <exception cref="MatrixDimensionMismatchException"> if <c>m</c> is not the same
        /// size as <c>this</c>.</exception>
        public OpenMapRealMatrix subtract(OpenMapRealMatrix m)
        {
            MatrixUtils.checkAdditionCompatible(this, m);

            OpenMapRealMatrix outp = new OpenMapRealMatrix(this);

            for (OpenIntToDoubleHashMap.Iterator iterator = m.entries.iterator(); iterator.MoveNext();)
            {
                int row = iterator.Current.Key / columns;
                int col = iterator.Current.Key - row * columns;
                outp.setEntry(row, col, getEntry(row, col) - iterator.Current.Value);
            }

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

            int        rowCount    = getRowDimension();
            int        columnCount = getColumnDimension();
            RealMatrix outp        = createMatrix(rowCount, columnCount);

            for (int row = 0; row < rowCount; ++row)
            {
                for (int col = 0; col < columnCount; ++col)
                {
                    outp.setEntry(row, col, getEntry(row, col) + m.getEntry(row, col));
                }
            }

            return(outp);
        }
Beispiel #4
0
        /// <summary>
        /// Compute the sum of <c>this</c> and <c>m</c>.
        /// </summary>
        /// <param name="m">Matrix to be added.</param>
        /// <returns><c>this + m</c>.</returns>
        /// <exception cref="MatrixDimensionMismatchException"> if <c>m</c> is not the same
        /// size as <c>this</c>.</exception>
        public Array2DRowRealMatrix add(Array2DRowRealMatrix m)
        {
            // Safety check.
            MatrixUtils.checkAdditionCompatible(this, m);

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

            double[][] outData = new double[rowCount][];
            for (int row = 0; row < rowCount; row++)
            {
                double[] dataRow    = data[row];
                double[] mRow       = m.data[row];
                double[] outDataRow = outData[row];
                for (int col = 0; col < columnCount; col++)
                {
                    outDataRow[col] = dataRow[col] + mRow[col];
                }
            }

            return(new Array2DRowRealMatrix(outData, false));
        }