BlockFieldMatrix extends AbstractFieldMatrix<T> implements Serializable { @Override public T[] preMultiply(final T[] v) throws DimensionMismatchException { if (v.length != rows) { throw new DimensionMismatchException(v.length, rows); } final T[] out = MathArrays.buildArray(getField(), columns); final T zero = getField().getZero(); 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 T[] 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; T sum = zero; int p = pStart; while (p < pEnd - 3) { sum = sum. add(block[k].multiply(v[p])). add(block[k + jWidth].multiply(v[p + 1])). add(block[k + jWidth2].multiply(v[p + 2])). add(block[k + jWidth3].multiply(v[p + 3])); k += jWidth4; p += 4; } while (p < pEnd) { sum = sum.add(block[k].multiply(v[p++])); k += jWidth; } out[q] = out[q].add(sum); } } } return out; } BlockFieldMatrix(final Field<T> field, final int rows,
                            final int columns);  BlockFieldMatrix(final T[][] rawData);  BlockFieldMatrix(final int rows, final int columns,
                            final T[][] blockData, final boolean copyArray); static T[][] toBlocksLayout(final T[][] rawData); static T[][] createBlocksLayout(final Field<T> field,
                                                                       final int rows, final int columns); @Override FieldMatrix<T> createMatrix(final int rowDimension,
                                       final int columnDimension); @Override FieldMatrix<T> copy(); @Override FieldMatrix<T> add(final FieldMatrix<T> m); BlockFieldMatrix<T> add(final BlockFieldMatrix<T> m); @Override FieldMatrix<T> subtract(final FieldMatrix<T> m); BlockFieldMatrix<T> subtract(final BlockFieldMatrix<T> m); @Override FieldMatrix<T> scalarAdd(final T d); @Override FieldMatrix<T> scalarMultiply(final T d); @Override FieldMatrix<T> multiply(final FieldMatrix<T> m); BlockFieldMatrix<T> multiply(BlockFieldMatrix<T> m); @Override T[][] getData(); @Override FieldMatrix<T> getSubMatrix(final int startRow, final int endRow,
                                       final int startColumn,
                                       final int endColumn); @Override void setSubMatrix(final T[][] subMatrix, final int row,
                             final int column); @Override FieldMatrix<T> getRowMatrix(final int row); @Override void setRowMatrix(final int row, final FieldMatrix<T> matrix); void setRowMatrix(final int row, final BlockFieldMatrix<T> matrix); @Override FieldMatrix<T> getColumnMatrix(final int column); @Override void setColumnMatrix(final int column, final FieldMatrix<T> matrix); @Override FieldVector<T> getRowVector(final int row); @Override void setRowVector(final int row, final FieldVector<T> vector); @Override FieldVector<T> getColumnVector(final int column); @Override void setColumnVector(final int column, final FieldVector<T> vector); @Override T[] getRow(final int row); @Override void setRow(final int row, final T[] array); @Override T[] getColumn(final int column); @Override void setColumn(final int column, final T[] array); @Override T getEntry(final int row, final int column); @Override void setEntry(final int row, final int column, final T value); @Override void addToEntry(final int row, final int column, final T increment); @Override void multiplyEntry(final int row, final int column, final T factor); @Override FieldMatrix<T> transpose(); @Override int getRowDimension(); @Override int getColumnDimension(); @Override T[] operate(final T[] v); @Override T[] preMultiply(final T[] v); @Override T walkInRowOrder(final FieldMatrixChangingVisitor<T> visitor); @Override T walkInRowOrder(final FieldMatrixPreservingVisitor<T> visitor); @Override T walkInRowOrder(final FieldMatrixChangingVisitor<T> visitor,
                            final int startRow, final int endRow,
                            final int startColumn, final int endColumn); @Override T walkInRowOrder(final FieldMatrixPreservingVisitor<T> visitor,
                            final int startRow, final int endRow,
                            final int startColumn, final int endColumn); @Override T walkInOptimizedOrder(final FieldMatrixChangingVisitor<T> visitor); @Override T walkInOptimizedOrder(final FieldMatrixPreservingVisitor<T> visitor); @Override T walkInOptimizedOrder(final FieldMatrixChangingVisitor<T> visitor,
                                  final int startRow, final int endRow,
                                  final int startColumn, final int endColumn); @Override T walkInOptimizedOrder(final FieldMatrixPreservingVisitor<T> visitor,
                                  final int startRow, final int endRow,
                                  final int startColumn, final int endColumn); static final int BLOCK_SIZE; }
@Test public void testPremultiply() { FieldMatrix<Fraction> m3 = new BlockFieldMatrix<Fraction>(d3); FieldMatrix<Fraction> m4 = new BlockFieldMatrix<Fraction>(d4); FieldMatrix<Fraction> m5 = new BlockFieldMatrix<Fraction>(d5); TestUtils.assertEquals(m4.preMultiply(m3), m5); BlockFieldMatrix<Fraction> m = new BlockFieldMatrix<Fraction>(testData); BlockFieldMatrix<Fraction> mInv = new BlockFieldMatrix<Fraction>(testDataInv); BlockFieldMatrix<Fraction> identity = new BlockFieldMatrix<Fraction>(id); TestUtils.assertEquals(m.preMultiply(mInv), identity); TestUtils.assertEquals(mInv.preMultiply(m), identity); TestUtils.assertEquals(m.preMultiply(identity), m); TestUtils.assertEquals(identity.preMultiply(mInv), mInv); try { m.preMultiply(new BlockFieldMatrix<Fraction>(bigSingular)); Assert.fail("Expecting illegalArgumentException"); } catch (MathIllegalArgumentException ex) { } }