![]() |
Submatrices provide views on a specific part of a dense or sparse matrix just as subvectors provide views on specific parts of vectors. As such, submatrices act as a reference to a specific block within a matrix. This reference is valid and can be used in evary way any other dense or sparse matrix can be used as long as the matrix containing the submatrix is not resized or entirely destroyed. The submatrix also acts as an alias to the matrix elements in the specified block: Changes made to the elements (e.g. modifying values, inserting or erasing elements) are immediately visible in the matrix and changes made via the matrix are immediately visible in the submatrix. Blaze provides two submatrix types: DenseSubmatrix and SparseSubmatrix.
The blaze::DenseSubmatrix template represents a view on a specific submatrix of a dense matrix primitive. It can be included via the header file
The type of the dense matrix is specified via the template parameter:
MT
specifies the type of the dense matrix primitive. DenseSubmatrix can be used with every dense matrix primitive, but does not work with any matrix expression type.
The blaze::SparseSubmatrix template represents a view on a specific submatrix of a sparse matrix primitive. It can be included via the header file
The type of the sparse matrix is specified via the template parameter:
MT
specifies the type of the sparse matrix primitive. SparseSubmatrix can be used with every sparse matrix primitive, but does not work with any matrix expression type.
A view on a submatrix can be created very conveniently via the submatrix()
function. This view can be treated as any other matrix, i.e. it can be assigned to, it can be copied from, and it can be used in arithmetic operations. A submatrix created from a row-major matrix will itself be a row-major matrix, a submatrix created from a column-major matrix will be a column-major matrix. The view can also be used on both sides of an assignment: The submatrix can either be used as an alias to grant write access to a specific submatrix of a dense matrix primitive on the left-hand side of an assignment or to grant read-access to a specific submatrix of a matrix primitive or expression on the right-hand side of an assignment. The following example demonstrates this in detail:
The current size of the matrix, i.e. the number of rows or columns can be obtained via the rows()
and columns()
functions, the current total capacity via the capacity()
function, and the number of non-zero elements via the nonZeros()
function. However, since submatrices are views on a specific submatrix of a matrix, several operations are not possible on views, such as resizing and swapping:
The elements of a submatrix can be directly accessed with the function call operator:
Alternatively, the elements of a submatrix can be traversed via (const) iterators. Just as with matrices, in case of non-const submatrices, begin()
and end()
return an Iterator, which allows a manipulation of the non-zero values, in case of constant submatrices a ConstIterator is returned:
Inserting/accessing elements in a sparse submatrix can be done by several alternative functions. The following example demonstrates all options:
Both dense and sparse submatrices can be used in all arithmetic operations that any other dense or sparse matrix can be used in. The following example gives an impression of the use of dense submatrices within arithmetic operations. All operations (addition, subtraction, multiplication, scaling, ...) can be performed on all possible combinations of dense and sparse matrices with fitting element types:
It is also possible to create a submatrix view on another submatrix. In this context it is important to remember that the type returned by the submatrix()
function is the same type as the type of the given submatrix, since the view on a submatrix is just another view on the underlying matrix:
As powerful and convenient submatrices can be, in terms of performance they may have some disadvantages in comparison to matrix primitives. Whereas matrix primitives are guaranteed to be properly aligned and therefore provide maximum performance in all operations, due to its enormous flexibility a view on a matrix might not be properly aligned. This may cause a performance penalty on some platforms and/or for some operations. Blaze tries to also handle special cases as efficiently as possible, but please remember that some overhead may still occur!