问题
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.
回答1:
You can either use the c++11 auto
keyword to declare row_i
and 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;
回答2:
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 the Eigen::Map class but I may be wrong.
MappedSparseMatrix<double> mat(int rows, int cols, int nnz,
int* outerIndexPtr, int* innerIndexPtr,
Scalar* valuePtr);
Source
来源:https://stackoverflow.com/questions/33387750/extracting-blocks-rois-from-eigensparsematrix-without-copying