On this page
Quick reference guide for sparse matrices
In this page, we give a quick summary of the main operations available for sparse matrices in the class SparseMatrix. First, it is recommended to read the introductory tutorial at Sparse matrix manipulations. The important point to have in mind when working on sparse matrices is how they are stored : i.e either row major or column major. The default is column major. Most arithmetic operations on sparse matrices will assert that they have the same storage order.
Sparse Matrix Initialization
Category | Operations | Notes |
---|---|---|
Constructor |
|
Default is ColMajor |
Resize/Reserve |
|
Note that when calling reserve(), it is not required that nnz is the exact number of nonzero elements in the final matrix. However, an exact estimation will avoid multiple reallocations during the insertion phase. |
Assignment |
|
The copy constructor can be used to convert from a storage order to another |
Element-wise Insertion |
|
insert() assumes that the element does not already exist; otherwise, use coeffRef() |
Batch insertion |
|
A complete example is available at Triplet Insertion . |
Constant or Random Insertion |
|
Remove all non-zero coefficients |
Matrix properties
Beyond the basic functions rows() and cols(), there are some useful functions that are available to easily get some information from the matrix.
|
Arithmetic operations
It is easy to perform arithmetic operations on sparse matrices provided that the dimensions are adequate and that the matrices have the same storage order. Note that the evaluation can always be done in a matrix with a different storage order. In the following, sm denotes a sparse matrix, dm a dense matrix and dv a dense vector.
Operations | Code | Notes |
---|---|---|
add subtract |
|
sm1 and sm2 should have the same storage order |
scalar product |
|
Many combinations are possible if the dimensions and the storage order agree. |
Sparse Product |
|
|
transposition, adjoint |
|
Note that the transposition change the storage order. There is no support for transposeInPlace(). |
Permutation |
|
|
Component-wise ops |
|
sm1 and sm2 should have the same storage order |
Other supported operations
Code | Notes |
---|---|
Sub-matrices | |
|
Contrary to dense matrices, here all these methods are read-only. See Block operations and below for read-write sub-matrices. |
Range | |
|
A inner vector is either a row (for row-major) or a column (for column-major). As stated earlier, for a read-write sub-matrix (RW), the evaluation can be done in a matrix with different storage order. |
Triangular and selfadjoint views | |
|
Several combination between triangular views and blocks views are possible
|
Triangular solve | |
|
For general sparse solve, Use any suitable module described at Solving Sparse Linear Systems |
Low-level API | |
|
If the matrix is not in compressed form, makeCompressed() should be called before. Note that these functions are mostly provided for interoperability purposes with external libraries. A better access to the values of the matrix is done by using the InnerIterator class as described in the Tutorial Sparse section |
Mapping external buffers | |
|
As for dense matrices, class Map<SparseMatrixType> can be used to see external buffers as an Eigen's SparseMatrix object. |
© Eigen.
Licensed under the MPL2 License.
https://eigen.tuxfamily.org/dox/group__SparseQuickRefPage.html