org.apache.commons.math4.linear

## Class BlockFieldMatrix<T extends FieldElement<T>>

• Type Parameters:
T - the type of the field elements
All Implemented Interfaces:
Serializable, AnyMatrix, FieldMatrix<T>

public class BlockFieldMatrix<T extends FieldElement<T>>
extends AbstractFieldMatrix<T>
implements Serializable
Cache-friendly implementation of FieldMatrix using a flat arrays to store square blocks of the matrix.

This implementation is specially designed to be cache-friendly. Square blocks are stored as small arrays and allow efficient traversal of data both in row major direction and columns major direction, one block at a time. This greatly increases performances for algorithms that use crossed directions loops like multiplication or transposition.

The size of square blocks is a static parameter. It may be tuned according to the cache size of the target computer processor. As a rule of thumbs, it should be the largest value that allows three blocks to be simultaneously cached (this is necessary for example for matrix multiplication). The default value is to use 36x36 blocks.

The regular blocks represent BLOCK_SIZE x BLOCK_SIZE squares. Blocks at right hand side and bottom side which may be smaller to fit matrix dimensions. The square blocks are flattened in row major order in single dimension arrays which are therefore BLOCK_SIZE2 elements long for regular blocks. The blocks are themselves organized in row major order.

As an example, for a block size of 36x36, a 100x60 matrix would be stored in 6 blocks. Block 0 would be a Field[1296] array holding the upper left 36x36 square, block 1 would be a Field[1296] array holding the upper center 36x36 square, block 2 would be a Field[1008] array holding the upper right 36x28 rectangle, block 3 would be a Field[864] array holding the lower left 24x36 rectangle, block 4 would be a Field[864] array holding the lower center 24x36 rectangle and block 5 would be a Field[672] array holding the lower right 24x28 rectangle.

The layout complexity overhead versus simple mapping of matrices to java arrays is negligible for small matrices (about 1%). The gain from cache efficiency leads to up to 3-fold improvements for matrices of moderate to large size.

Since:
2.0
Serialized Form
• ### Field Summary

Fields
Modifier and Type Field and Description
static int BLOCK_SIZE
Block size.
• ### Constructor Summary

Constructors
Constructor and Description
BlockFieldMatrix(Field<T> field, int rows, int columns)
Create a new matrix with the supplied row and column dimensions.
BlockFieldMatrix(int rows, int columns, T[][] blockData, boolean copyArray)
Create a new dense matrix copying entries from block layout data.
BlockFieldMatrix(T[][] rawData)
Create a new dense matrix copying entries from raw layout data.
• ### Method Summary

All Methods
Modifier and Type Method and Description
BlockFieldMatrix<T> add(BlockFieldMatrix<T> m)
Compute the sum of this and m.
FieldMatrix<T> add(FieldMatrix<T> m)
Compute the sum of this and m.
void addToEntry(int row, int column, T increment)
Change an entry in the specified row and column.
FieldMatrix<T> copy()
Make a (deep) copy of this.
static <T extends FieldElement<T>>T[][] createBlocksLayout(Field<T> field, int rows, int columns)
Create a data array in blocks layout.
FieldMatrix<T> createMatrix(int rowDimension, int columnDimension)
Create a new FieldMatrix of the same type as the instance with the supplied row and column dimensions.
T[] getColumn(int column)
Get the entries in column number col as an array.
int getColumnDimension()
Returns the number of columns in the matrix.
FieldMatrix<T> getColumnMatrix(int column)
Get the entries in column number column as a column matrix.
FieldVector<T> getColumnVector(int column)
Returns the entries in column number column as a vector.
T[][] getData()
Returns matrix entries as a two-dimensional array.
T getEntry(int row, int column)
Returns the entry in the specified row and column.
T[] getRow(int row)
Get the entries in row number row as an array.
int getRowDimension()
Returns the number of rows in the matrix.
FieldMatrix<T> getRowMatrix(int row)
Get the entries in row number row as a row matrix.
FieldVector<T> getRowVector(int row)
Get the entries in row number row as a vector.
FieldMatrix<T> getSubMatrix(int startRow, int endRow, int startColumn, int endColumn)
Get a submatrix.
BlockFieldMatrix<T> multiply(BlockFieldMatrix<T> m)
Returns the result of postmultiplying this by m.
FieldMatrix<T> multiply(FieldMatrix<T> m)
Postmultiply this matrix by m.
void multiplyEntry(int row, int column, T factor)
Change an entry in the specified row and column.
T[] operate(T[] v)
Returns the result of multiplying this by the vector v.
T[] preMultiply(T[] v)
Returns the (row) vector result of premultiplying this by the vector v.
FieldMatrix<T> scalarAdd(T d)
Increment each entry of this matrix.
FieldMatrix<T> scalarMultiply(T d)
Multiply each entry by d.
void setColumn(int column, T[] array)
Set the entries in column number column as a column matrix.
void setColumnMatrix(int column, FieldMatrix<T> matrix)
Set the entries in column number column as a column matrix.
void setColumnVector(int column, FieldVector<T> vector)
Set the entries in column number column as a vector.
void setEntry(int row, int column, T value)
Set the entry in the specified row and column.
void setRow(int row, T[] array)
Set the entries in row number row as a row matrix.
void setRowMatrix(int row, BlockFieldMatrix<T> matrix)
Sets the entries in row number row as a row matrix.
void setRowMatrix(int row, FieldMatrix<T> matrix)
Set the entries in row number row as a row matrix.
void setRowVector(int row, FieldVector<T> vector)
Set the entries in row number row as a vector.
void setSubMatrix(T[][] subMatrix, int row, int column)
Replace the submatrix starting at (row, column) using data in the input subMatrix array.
BlockFieldMatrix<T> subtract(BlockFieldMatrix<T> m)
Compute this - m.
FieldMatrix<T> subtract(FieldMatrix<T> m)
Subtract m from this matrix.
static <T extends FieldElement<T>>T[][] toBlocksLayout(T[][] rawData)
Convert a data array from raw layout to blocks layout.
FieldMatrix<T> transpose()
Returns the transpose of this matrix.
T walkInOptimizedOrder(FieldMatrixChangingVisitor<T> visitor)
Visit (and possibly change) all matrix entries using the fastest possible order.
T walkInOptimizedOrder(FieldMatrixChangingVisitor<T> visitor, int startRow, int endRow, int startColumn, int endColumn)
Visit (and possibly change) some matrix entries using the fastest possible order.
T walkInOptimizedOrder(FieldMatrixPreservingVisitor<T> visitor)
Visit (but don't change) all matrix entries using the fastest possible order.
T walkInOptimizedOrder(FieldMatrixPreservingVisitor<T> visitor, int startRow, int endRow, int startColumn, int endColumn)
Visit (but don't change) some matrix entries using the fastest possible order.
T walkInRowOrder(FieldMatrixChangingVisitor<T> visitor)
Visit (and possibly change) all matrix entries in row order.
T walkInRowOrder(FieldMatrixChangingVisitor<T> visitor, int startRow, int endRow, int startColumn, int endColumn)
Visit (and possibly change) some matrix entries in row order.
T walkInRowOrder(FieldMatrixPreservingVisitor<T> visitor)
Visit (but don't change) all matrix entries in row order.
T walkInRowOrder(FieldMatrixPreservingVisitor<T> visitor, int startRow, int endRow, int startColumn, int endColumn)
Visit (but don't change) some matrix entries in row order.
• ### Methods inherited from class org.apache.commons.math4.linear.AbstractFieldMatrix

checkAdditionCompatible, checkColumnIndex, checkMultiplicationCompatible, checkRowIndex, checkSubMatrixIndex, checkSubMatrixIndex, checkSubtractionCompatible, copySubMatrix, copySubMatrix, equals, extractField, extractField, getField, getSubMatrix, getTrace, hashCode, isSquare, operate, power, preMultiply, preMultiply, toString, walkInColumnOrder, walkInColumnOrder, walkInColumnOrder, walkInColumnOrder
• ### Methods inherited from class java.lang.Object

clone, finalize, getClass, notify, notifyAll, wait, wait, wait
• ### Field Detail

• #### BLOCK_SIZE

public static final int BLOCK_SIZE
Block size.
Constant Field Values
• ### Constructor Detail

• #### BlockFieldMatrix

public BlockFieldMatrix(Field<T> field,
int rows,
int columns)
throws NotStrictlyPositiveException
Create a new matrix with the supplied row and column dimensions.
Parameters:
field - Field to which the elements belong.
rows - Number of rows in the new matrix.
columns - Number of columns in the new matrix.
Throws:
NotStrictlyPositiveException - if row or column dimension is not positive.
• #### BlockFieldMatrix

public BlockFieldMatrix(T[][] rawData)
throws DimensionMismatchException
Create a new dense matrix copying entries from raw layout data.

The input array must already be in raw layout.

Calling this constructor is equivalent to call:

matrix = new BlockFieldMatrix(getField(), rawData.length, rawData[0].length,
toBlocksLayout(rawData), false);

Parameters:
rawData - Data for the new matrix, in raw layout.
Throws:
DimensionMismatchException - if the blockData shape is inconsistent with block layout.
BlockFieldMatrix(int, int, FieldElement[][], boolean)
• #### BlockFieldMatrix

public BlockFieldMatrix(int rows,
int columns,
T[][] blockData,
boolean copyArray)
throws DimensionMismatchException,
NotStrictlyPositiveException
Create a new dense matrix copying entries from block layout data.

The input array must already be in blocks layout.

Parameters:
rows - the number of rows in the new matrix
columns - the number of columns in the new matrix
blockData - data for new matrix
copyArray - if true, the input array will be copied, otherwise it will be referenced
Throws:
DimensionMismatchException - if the blockData shape is inconsistent with block layout.
NotStrictlyPositiveException - if row or column dimension is not positive.
createBlocksLayout(Field, int, int), toBlocksLayout(FieldElement[][]), BlockFieldMatrix(FieldElement[][])