BlockRealMatrix extends AbstractRealMatrix implements Serializable { @Override public double[] preMultiply(final double[] v) throws DimensionMismatchException { if (v.length != rows) { throw new DimensionMismatchException(v.length, rows); } final double[] out = new double[columns]; for (int jBlock = 0; jBlock < blockColumns; ++jBlock) { final int jWidth = blockWidth(jBlock); final int jWidth2 = jWidth + jWidth; final int jWidth3 = jWidth2 + jWidth; final int jWidth4 = jWidth3 + jWidth; final int qStart = jBlock * BLOCK_SIZE; final int qEnd = FastMath.min(qStart + BLOCK_SIZE, columns); for (int iBlock = 0; iBlock < blockRows; ++iBlock) { final double[] block = blocks[iBlock * blockColumns + jBlock]; final int pStart = iBlock * BLOCK_SIZE; final int pEnd = FastMath.min(pStart + BLOCK_SIZE, rows); for (int q = qStart; q < qEnd; ++q) { int k = q - qStart; double sum = 0; int p = pStart; while (p < pEnd - 3) { sum += block[k] * v[p] + block[k + jWidth] * v[p + 1] + block[k + jWidth2] * v[p + 2] + block[k + jWidth3] * v[p + 3]; k += jWidth4; p += 4; } while (p < pEnd) { sum += block[k] * v[p++]; k += jWidth; } out[q] += sum; } } } 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 testPremultiplyVector() { RealMatrix m = new BlockRealMatrix(testData); assertClose(m.preMultiply(testVector), preMultTest, normTolerance); assertClose(m.preMultiply(new ArrayRealVector(testVector).toArray()), preMultTest, normTolerance); m = new BlockRealMatrix(bigSingular); try { m.preMultiply(testVector); Assert.fail("expecting MathIllegalArgumentException"); } catch (MathIllegalArgumentException ex) { } }
@Test public void testPremultiply() { RealMatrix m3 = new BlockRealMatrix(d3); RealMatrix m4 = new BlockRealMatrix(d4); RealMatrix m5 = new BlockRealMatrix(d5); assertClose(m4.preMultiply(m3), m5, entryTolerance); BlockRealMatrix m = new BlockRealMatrix(testData); BlockRealMatrix mInv = new BlockRealMatrix(testDataInv); BlockRealMatrix identity = new BlockRealMatrix(id); assertClose(m.preMultiply(mInv), identity, entryTolerance); assertClose(mInv.preMultiply(m), identity, entryTolerance); assertClose(m.preMultiply(identity), m, entryTolerance); assertClose(identity.preMultiply(mInv), mInv, entryTolerance); try { m.preMultiply(new BlockRealMatrix(bigSingular)); Assert.fail("Expecting illegalArgumentException"); } catch (MathIllegalArgumentException ex) { } }