/** {@inheritDoc} */ @Override public RealMatrix multiply(final RealMatrix m) throws IllegalArgumentException { try { return multiply((OpenMapRealMatrix) m); } catch (ClassCastException cce) { // safety check MatrixUtils.checkMultiplicationCompatible(this, m); final int outCols = m.getColumnDimension(); final BlockRealMatrix out = new BlockRealMatrix(rowDimension, outCols); for (OpenIntToDoubleHashMap.Iterator iterator = entries.iterator(); iterator.hasNext(); ) { iterator.advance(); final double value = iterator.value(); final int key = iterator.key(); final int i = key / columnDimension; final int k = key % columnDimension; for (int j = 0; j < outCols; ++j) { out.addToEntry(i, j, value * m.getEntry(k, j)); } } return out; } }
/** * Returns the result of postmultiplying this by m. * * @param m matrix to postmultiply by * @return this * m * @throws IllegalArgumentException if columnDimension(this) != rowDimension(m) */ public OpenMapRealMatrix multiply(OpenMapRealMatrix m) throws IllegalArgumentException { // safety check MatrixUtils.checkMultiplicationCompatible(this, m); final int outCols = m.getColumnDimension(); OpenMapRealMatrix out = new OpenMapRealMatrix(rowDimension, outCols); for (OpenIntToDoubleHashMap.Iterator iterator = entries.iterator(); iterator.hasNext(); ) { iterator.advance(); final double value = iterator.value(); final int key = iterator.key(); final int i = key / columnDimension; final int k = key % columnDimension; for (int j = 0; j < outCols; ++j) { final int rightKey = m.computeKey(k, j); if (m.entries.containsKey(rightKey)) { final int outKey = out.computeKey(i, j); final 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; }
/** * Compute this minus <code>m</code>. * * @param m matrix to be subtracted * @return this - m * @throws IllegalArgumentException if m is not the same size as this */ public OpenMapRealMatrix subtract(OpenMapRealMatrix m) throws IllegalArgumentException { // safety check MatrixUtils.checkAdditionCompatible(this, m); final OpenMapRealMatrix out = new OpenMapRealMatrix(this); for (OpenIntToDoubleHashMap.Iterator iterator = m.entries.iterator(); iterator.hasNext(); ) { iterator.advance(); final int row = iterator.key() / columnDimension; final int col = iterator.key() - row * columnDimension; out.setEntry(row, col, getEntry(row, col) - iterator.value()); } return out; }