![]() |
Rows provide views on a specific row of a dense or sparse matrix. As such, rows act as a reference to a specific row. This reference is valid and can be used in every way any other row vector can be used as long as the matrix containing the row is not resized or entirely destroyed. The row also acts as an alias to the row elements: 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 row.
A reference to a dense or sparse row can be created very conveniently via the row()
function. It can be included via the header file
The row index must be in the range from , where
M
is the total number of rows of the matrix, and can be specified both at compile time or at runtime:
The row()
function returns an expression representing the row view. The type of this expression depends on the given row arguments, primarily the type of the matrix and the compile time arguments. If the type is required, it can be determined via the decltype
specifier:
The resulting view can be treated as any other row vector, i.e. it can be assigned to, it can be copied from, and it can be used in arithmetic operations. The reference can also be used on both sides of an assignment: The row can either be used as an alias to grant write access to a specific row of a matrix primitive on the left-hand side of an assignment or to grant read-access to a specific row of a matrix primitive or expression on the right-hand side of an assignment. The following example demonstrates this in detail:
The elements of a row can be directly accessed with the subscript operator:
The numbering of the row elements is
where N is the number of columns of the referenced matrix. Alternatively, the elements of a row can be traversed via iterators. Just as with vectors, in case of non-const rows, begin()
and end()
return an iterator, which allows to manipulate the elements, in case of constant rows an iterator to immutable elements is returned:
Inserting/accessing elements in a sparse row can be done by several alternative functions. The following example demonstrates all options:
A row view can be used like any other row vector. This means that with only a few exceptions all Vector Operations and Arithmetic Operations can be used. For instance, the current number of elements can be obtained via the size()
function, the current capacity via the capacity()
function, and the number of non-zero elements via the nonZeros()
function. However, since rows are references to specific rows of a matrix, several operations are not possible on views, such as resizing and swapping. The following example shows this by means of a dense row view:
Both dense and sparse rows can be used in all arithmetic operations that any other dense or sparse row vector can be used in. The following example gives an impression of the use of dense rows within arithmetic operations. All operations (addition, subtraction, multiplication, scaling, ...) can be performed on all possible combinations of dense and sparse rows with fitting element types:
Especially noteworthy is that row views can be created for both row-major and column-major matrices. Whereas the interface of a row-major matrix only allows to traverse a row directly and the interface of a column-major matrix only allows to traverse a column, via views it is possible to traverse a row of a column-major matrix or a column of a row-major matrix. For instance:
However, please note that creating a row view on a matrix stored in a column-major fashion can result in a considerable performance decrease in comparison to a row view on a matrix with row-major storage format. This is due to the non-contiguous storage of the matrix elements. Therefore care has to be taken in the choice of the most suitable storage order:
Although Blaze performs the resulting vector/matrix multiplication as efficiently as possible using a row-major storage order for matrix A
would result in a more efficient evaluation.
Previous: Submatrices Next: Row Selections