I wonder is there any good way to extract blocks/ROIs from Eigen::SparseMatrix? More precisely, what I want to extract is inner vectors.
What I want to do is like:
typedef Eigen::SparseMatrix<double,Eigen::RowMajor> SpMat;
// Prepare some sparse matrix
SpMat spmat;
// Extract lines from it
const SpMat& row_i = spmat.innerVector(i);
const SpMat& row_j = spmat.innerVector(j);
// Some calculation with row_i and row_j...
As I tested, the data of row_i
and row_j
is copied (!!) from spmat
.
However, obviously, it is inefficient.
The data (esp. row_i.m_data.m_values
& row_i.m_data.m_indices
) of inner vectors is continuous part of original data (spmat.m_data.m_values
& spmat.m_data.m_indices
resp.), so there should be smarter way.
I may be able to implement new method to do this, but it require me a tough digging into the source code. So I don't want to.
Any help is grateful! Thanks in advance.
You can try to use the
MappedSparseMatrix
class. It wraps an existing set of data and associated parameters (I think, I've never used it). I assume it works similarly to theEigen::Map
class but I may be wrong.Source
You can either use the c++11
auto
keyword to declarerow_i
androw_j
as true read-write expressions, or use the proper type:Moreover, not that by default a SparseMatrix is stored in column major, therefore an "inner-vector" is a column. If you want to reference rows, then you have to use a row-major storage layout: