![]() |
Blaze
3.6
|
Diagonals provide a view on a specific diagonal of a dense or sparse matrix. As such, diagonals act as a reference to a specific diagonal. This reference is valid and can be used in every way any other vector can be used as long as the matrix containing the diagonal is not resized or entirely destroyed. 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 on the diagonal.
A reference to a dense or sparse diagonal can be created very conveniently via the diagonal()
function. Just as bands it can be included via the header file
The diagonal()
function expects just a single argument:
The diagonal()
function returns an expression representing the diagonal view. The type of this expression depends on the given diagonal arguments, primarily the type of the matrix and the compile time arguments. If the type is required, it can be determined via decltype
or via the DiagonalExprTrait
class template:
The resulting view can be treated as any other vector, i.e. it can be assigned to, it can be copied from, and it can be used in arithmetic operations. By default, diagonals are considered column vectors, but this setting can be changed via the defaultTransposeFlag
switch. The reference can also be used on both sides of an assignment: The diagonal can either be used as an alias to grant write access to a specific diagonal of a matrix primitive on the left-hand side of an assignment or to grant read-access to a specific diagonal 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 diagonal can be directly accessed with the subscript operator:
The numbering of the diagonal elements is
where N is the number of elements of the referenced diagonal. Alternatively, the elements of a diagonal can be traversed via iterators. Just as with vectors, in case of non-const diagonal, begin()
and end()
return an iterator, which allows to manipulate the elements, in case of constant diagonals an iterator to immutable elements is returned:
Inserting/accessing elements in a sparse diagonal can be done by several alternative functions. The following example demonstrates all options:
The current number of diagonal 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 diagonals are references to specific diagonals of a matrix, several operations are not possible, such as resizing and swapping. The following example shows this by means of a dense diagonal view:
Both dense and sparse diagonals can be used in all arithmetic operations that any other dense or sparse vector can be used in. The following example gives an impression of the use of dense diagonals within arithmetic operations. All operations (addition, subtraction, multiplication, scaling, ...) can be performed on all possible combinations of dense and sparse diagonals with fitting element types: