How to wrap Eigen::SparseMatrix over preexistant 3

2019-05-28 03:13发布

问题:

NOTE: I allready asked this question, but it was closed because of "too broad" without much explanation. I can't see how this question could be more specific (it deals with a specific class of a specific library for a specific usage...), so I assume that it was something like a "moderator's mistake" and ask it again...

I would like to perfom sparse matrix/matrix multiplication using Eigen on sparse matrices. These matrices are already defined in the code I am working on in standard 3-arrays compressed row/column strorage.

Then I would like to use the Eigen::SparseMatrix class as a wrapper on these arrays (assuming that internally Eigen uses such a 3-arrays storage) in order to avoid to duplicate matrices in memory. I would like to do something like the following:

Eigen::SparseMatrix smin0(n,m);
Eigen::SparseMatrix smin1(m,l);
Eigen::SparseMatrix smout(n,l);

smin0.set_innerPtr(myInnerPtr0);
smin0.set_outerPtr(myOuterPtr0);
smin0.set_valuePtr(myValuePtr0);

smin1.set_innerPtr(myInnerPtr1);
smin1.set_outerPtr(myOuterPtr1);
smin1.set_valuePtr(myValuePtr1);

smout=smin0*smin1;

int *myOutInnerPtr=smout.innerIndexPtr();
int *myOutOuterPtr=smout.outerIndexPtr();
double *myOutValuePtr=smout.valuePtr();

Is it possible and if yes, how?

Many Thanks

回答1:

As ggael pointed out, you can use Eigen::MappedSparseMatrix for that.

The reason you can't just overwrite the internal pointers of a SparseMatrix is that this would cause problems when the SparseMatrix deallocates them, but you allocated them in a different way then Eigen does (and how Eigen internally allocates memory is an implementation detail you should not really rely on in your code).