Reputation: 200
I'm using Eigen::SparseMatrix
for a large matrix of a system of linear equations, and currently I set it up by using setFromTriplets()
.
My problem is that I cannot find out whether it's possible to init a symmetric matrix efficiently by that. For now I have to keep in memory both upper and lower parts of it (in a list of triplets, which is much worse, since there is not only a value itself, but there are its indices either).
It'd be better if that function could immediately copy non-diagonal elements upon an encounter. Or is there already such functionality?
Also maybe there is a property of the matrix to be symmetric, so that even after initialization it keeps in memory only one of the parts, and all other algorithms take this property into consideration?
Upvotes: 0
Views: 565
Reputation: 18809
If you have a matrix S
with just the upper (or lower) triangular half set, you can use
S.selfadjointView<Eigen::Upper>(); // or Eigen::Lower
to get a selfadjoint view of the matrix. For real-valued matrices this is equivalent to a symmetric matrix generated by copying the transposed half to the unset half.
This can be used in most expressions which work with Sparse matrices without requiring to explicitly store both halves of the matrix.
Minimal usage demonstration: https://godbolt.org/z/6UyDGZ
Also, all Cholesky-related solvers naturally just consider one half of the input matrix.
Upvotes: 2