BlockFieldMatrix extends AbstractFieldMatrix<T> implements Serializable { @Override public void setSubMatrix(final T[][] subMatrix, final int row, final int column) throws DimensionMismatchException, OutOfRangeException, NoDataException, NullArgumentException { MathUtils.checkNotNull(subMatrix); final int refLength = subMatrix[0].length; if (refLength == 0) { throw new NoDataException(LocalizedFormats.AT_LEAST_ONE_COLUMN); } final int endRow = row + subMatrix.length - 1; final int endColumn = column + refLength - 1; checkSubMatrixIndex(row, endRow, column, endColumn); for (final T[] subRow : subMatrix) { if (subRow.length != refLength) { throw new DimensionMismatchException(refLength, subRow.length); } } final int blockStartRow = row / BLOCK_SIZE; final int blockEndRow = (endRow + BLOCK_SIZE) / BLOCK_SIZE; final int blockStartColumn = column / BLOCK_SIZE; final int blockEndColumn = (endColumn + BLOCK_SIZE) / BLOCK_SIZE; for (int iBlock = blockStartRow; iBlock < blockEndRow; ++iBlock) { final int iHeight = blockHeight(iBlock); final int firstRow = iBlock * BLOCK_SIZE; final int iStart = FastMath.max(row, firstRow); final int iEnd = FastMath.min(endRow + 1, firstRow + iHeight); for (int jBlock = blockStartColumn; jBlock < blockEndColumn; ++jBlock) { final int jWidth = blockWidth(jBlock); final int firstColumn = jBlock * BLOCK_SIZE; final int jStart = FastMath.max(column, firstColumn); final int jEnd = FastMath.min(endColumn + 1, firstColumn + jWidth); final int jLength = jEnd - jStart; final T[] block = blocks[iBlock * blockColumns + jBlock]; for (int i = iStart; i < iEnd; ++i) { System.arraycopy(subMatrix[i - row], jStart - column, block, (i - firstRow) * jWidth + (jStart - firstColumn), jLength); } } } } 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 testSetSubMatrix() { BlockFieldMatrix<Fraction> m = new BlockFieldMatrix<Fraction>(testData); m.setSubMatrix(detData2,1,1); FieldMatrix<Fraction> expected = new BlockFieldMatrix<Fraction> (new Fraction[][] {{new Fraction(1),new Fraction(2),new Fraction(3)},{new Fraction(2),new Fraction(1),new Fraction(3)},{new Fraction(1),new Fraction(2),new Fraction(4)}}); Assert.assertEquals(expected, m); m.setSubMatrix(detData2,0,0); expected = new BlockFieldMatrix<Fraction> (new Fraction[][] {{new Fraction(1),new Fraction(3),new Fraction(3)},{new Fraction(2),new Fraction(4),new Fraction(3)},{new Fraction(1),new Fraction(2),new Fraction(4)}}); Assert.assertEquals(expected, m); m.setSubMatrix(testDataPlus2,0,0); expected = new BlockFieldMatrix<Fraction> (new Fraction[][] {{new Fraction(3),new Fraction(4),new Fraction(5)},{new Fraction(4),new Fraction(7),new Fraction(5)},{new Fraction(3),new Fraction(2),new Fraction(10)}}); Assert.assertEquals(expected, m); BlockFieldMatrix<Fraction> matrix = new BlockFieldMatrix<Fraction>(new Fraction[][] { {new Fraction(1), new Fraction(2), new Fraction(3), new Fraction(4)}, {new Fraction(5), new Fraction(6), new Fraction(7), new Fraction(8)}, {new Fraction(9), new Fraction(0), new Fraction(1) , new Fraction(2)} }); matrix.setSubMatrix(new Fraction[][] { {new Fraction(3), new Fraction(4)}, {new Fraction(5), new Fraction(6)} }, 1, 1); expected = new BlockFieldMatrix<Fraction>(new Fraction[][] { {new Fraction(1), new Fraction(2), new Fraction(3),new Fraction(4)}, {new Fraction(5), new Fraction(3), new Fraction(4), new Fraction(8)}, {new Fraction(9), new Fraction(5) ,new Fraction(6), new Fraction(2)} }); Assert.assertEquals(expected, matrix); try { m.setSubMatrix(testData,1,1); Assert.fail("expecting OutOfRangeException"); } catch (OutOfRangeException e) { } try { m.setSubMatrix(testData,-1,1); Assert.fail("expecting OutOfRangeException"); } catch (OutOfRangeException e) { } try { m.setSubMatrix(testData,1,-1); Assert.fail("expecting OutOfRangeException"); } catch (OutOfRangeException e) { } try { m.setSubMatrix(null,1,1); Assert.fail("expecting NullArgumentException"); } catch (NullArgumentException e) { } try { m.setSubMatrix(new Fraction[][] {{new Fraction(1)}, {new Fraction(2), new Fraction(3)}}, 0, 0); Assert.fail("expecting MathIllegalArgumentException"); } catch (MathIllegalArgumentException e) { } try { m.setSubMatrix(new Fraction[][] {{}}, 0, 0); Assert.fail("expecting MathIllegalArgumentException"); } catch (MathIllegalArgumentException e) { } }