BlockRealMatrix extends AbstractRealMatrix implements Serializable { @Override public BlockRealMatrix multiply(final RealMatrix m) throws DimensionMismatchException { try { return multiply((BlockRealMatrix) m); } catch (ClassCastException cce) { MatrixUtils.checkMultiplicationCompatible(this, m); final BlockRealMatrix out = new BlockRealMatrix(rows, m.getColumnDimension()); int blockIndex = 0; for (int iBlock = 0; iBlock < out.blockRows; ++iBlock) { final int pStart = iBlock * BLOCK_SIZE; final int pEnd = FastMath.min(pStart + BLOCK_SIZE, rows); for (int jBlock = 0; jBlock < out.blockColumns; ++jBlock) { final int qStart = jBlock * BLOCK_SIZE; final int qEnd = FastMath.min(qStart + BLOCK_SIZE, m.getColumnDimension()); final double[] outBlock = out.blocks[blockIndex]; for (int kBlock = 0; kBlock < blockColumns; ++kBlock) { final int kWidth = blockWidth(kBlock); final double[] tBlock = blocks[iBlock * blockColumns + kBlock]; final int rStart = kBlock * BLOCK_SIZE; int k = 0; for (int p = pStart; p < pEnd; ++p) { final int lStart = (p - pStart) * kWidth; final int lEnd = lStart + kWidth; for (int q = qStart; q < qEnd; ++q) { double sum = 0; int r = rStart; for (int l = lStart; l < lEnd; ++l) { sum += tBlock[l] * m.getEntry(r, q); ++r; } outBlock[k] += sum; ++k; } } } ++blockIndex; } } return out; } } BlockRealMatrix(final int rows, final int columns);  BlockRealMatrix(final double[][] rawData);  BlockRealMatrix(final int rows, final int columns,
                           final double[][] blockData, final boolean copyArray); static double[][] toBlocksLayout(final double[][] rawData); static double[][] createBlocksLayout(final int rows, final int columns); @Override BlockRealMatrix createMatrix(final int rowDimension,
                                        final int columnDimension); @Override BlockRealMatrix copy(); @Override BlockRealMatrix add(final RealMatrix m); BlockRealMatrix add(final BlockRealMatrix m); @Override BlockRealMatrix subtract(final RealMatrix m); BlockRealMatrix subtract(final BlockRealMatrix m); @Override BlockRealMatrix scalarAdd(final double d); @Override RealMatrix scalarMultiply(final double d); @Override BlockRealMatrix multiply(final RealMatrix m); BlockRealMatrix multiply(BlockRealMatrix m); @Override double[][] getData(); @Override double getNorm(); @Override double getFrobeniusNorm(); @Override BlockRealMatrix getSubMatrix(final int startRow, final int endRow,
                                        final int startColumn,
                                        final int endColumn); @Override void setSubMatrix(final double[][] subMatrix, final int row,
                             final int column); @Override BlockRealMatrix getRowMatrix(final int row); @Override void setRowMatrix(final int row, final RealMatrix matrix); void setRowMatrix(final int row, final BlockRealMatrix matrix); @Override BlockRealMatrix getColumnMatrix(final int column); @Override void setColumnMatrix(final int column, final RealMatrix matrix); @Override RealVector getRowVector(final int row); @Override void setRowVector(final int row, final RealVector vector); @Override RealVector getColumnVector(final int column); @Override void setColumnVector(final int column, final RealVector vector); @Override double[] getRow(final int row); @Override void setRow(final int row, final double[] array); @Override double[] getColumn(final int column); @Override void setColumn(final int column, final double[] array); @Override double getEntry(final int row, final int column); @Override void setEntry(final int row, final int column, final double value); @Override void addToEntry(final int row, final int column,
                           final double increment); @Override void multiplyEntry(final int row, final int column,
                              final double factor); @Override BlockRealMatrix transpose(); @Override int getRowDimension(); @Override int getColumnDimension(); @Override double[] operate(final double[] v); @Override double[] preMultiply(final double[] v); @Override double walkInRowOrder(final RealMatrixChangingVisitor visitor); @Override double walkInRowOrder(final RealMatrixPreservingVisitor visitor); @Override double walkInRowOrder(final RealMatrixChangingVisitor visitor,
                                 final int startRow, final int endRow,
                                 final int startColumn, final int endColumn); @Override double walkInRowOrder(final RealMatrixPreservingVisitor visitor,
                                 final int startRow, final int endRow,
                                 final int startColumn, final int endColumn); @Override double walkInOptimizedOrder(final RealMatrixChangingVisitor visitor); @Override double walkInOptimizedOrder(final RealMatrixPreservingVisitor visitor); @Override double walkInOptimizedOrder(final RealMatrixChangingVisitor visitor,
                                       final int startRow, final int endRow,
                                       final int startColumn,
                                       final int endColumn); @Override double walkInOptimizedOrder(final RealMatrixPreservingVisitor visitor,
                                       final int startRow, final int endRow,
                                       final int startColumn,
                                       final int endColumn); static final int BLOCK_SIZE; }
@Test public void testMultiply() { BlockRealMatrix m = new BlockRealMatrix(testData); BlockRealMatrix mInv = new BlockRealMatrix(testDataInv); BlockRealMatrix identity = new BlockRealMatrix(id); BlockRealMatrix m2 = new BlockRealMatrix(testData2); assertClose(m.multiply(mInv), identity, entryTolerance); assertClose(mInv.multiply(m), identity, entryTolerance); assertClose(m.multiply(identity), m, entryTolerance); assertClose(identity.multiply(mInv), mInv, entryTolerance); assertClose(m2.multiply(identity), m2, entryTolerance); try { m.multiply(new BlockRealMatrix(bigSingular)); Assert.fail("Expecting illegalArgumentException"); } catch (MathIllegalArgumentException ex) { } }
@Test public void testMultiply2() { RealMatrix m3 = new BlockRealMatrix(d3); RealMatrix m4 = new BlockRealMatrix(d4); RealMatrix m5 = new BlockRealMatrix(d5); assertClose(m3.multiply(m4), m5, entryTolerance); }