2
votes

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
I'm sure you already looked at the documentation, and saw that the format used for sparse matrices is slightly different, using 4 arrays instead of 3. You probably also noted that the small tutorial doesn't mention conversion methods from 3 arrays to 4. I suppose that the simplest way would be to batch insert values in the sparse matrix, which would let it build its own internal format by itself.didierc
You're looking for Eigen::MappedSparseMatrix: eigen.tuxfamily.org/dox/classEigen_1_1MappedSparseMatrix.htmlggael
Yes it looks loke what I am looking for. Is there a provided way to "set" the protected members of that class, or do I need to derive a class from it?janou195

1 Answers

0
votes

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).