Search code examples
c++copysparse-matrixeigeneigen3

Extracting blocks/ROIs from Eigen::SparseMatrix without copying


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.


Solution

  • You can either use the c++11 auto keyword to declare row_iand row_j as true read-write expressions, or use the proper type:

    const auto row_i = spmap.innerVector(i); // C++11 version
    const SpMat::InnerVectorReturnType row_i = spmap.innerVector(i); // C++98 version
    

    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:

    typedef Eigen::SparseMatrix<double,RowMajor> SpMat;