In the following code, I declare and initialize a 5x5 sparse matrix and set the (0,0) value to 1; matrix A is row major, matrix B is column major. I run over the outer index and through the inner iterator in each case identically. However, when I run the code - with matrix B, I get a "1" printed out - as expected; with matrix A I get an absurd number.
I encountered this while trying to implement a mean calculation for a sparse matrix. Here's a bit of sample code to demonstrate the problem as discussed above. Yes, I know some of the header stuff is unnecessary; I just nabbed it from another demo I was working on:
using namespace Eigen;
using namespace std;
for (int k = 0; k<A.outerSize(); ++k)
for (SparseMatrix<float>::InnerIterator it(A,k); it; ++it)
for (int k = 0; k<B.outerSize(); ++k)
for (SparseMatrix<float>::InnerIterator it(B,k); it; ++it)
(In reply to comment #0)
> SparseMatrix<float,RowMajor> A(5,5);
> for (int k = 0; k<A.outerSize(); ++k)
> for (SparseMatrix<float>::InnerIterator it(A,k); it; ++it)
You must use SparseMatrix<float, ColMajor>::InnerIterator.
I guess what happens is that A is converted to a temporary ColMajor matrix which gets invalid before it.value() is called.
We should catch that somehow, maybe by adding a constructor for invalid types which results in a static assertion.
I assume things like the following won't work properly, either:
typedef SparseMatrix<float> Sparse;
Sparse A, B; // initialize somehow
Sparse::InnerIterator(A+B, 0); // Iterator to a temporary
This has been fixed a few weeks ago for SparseMatrix:
and for SparseVector:
-- GitLab Migration Automatic Message --
This bug has been migrated to gitlab.com's GitLab instance and has been closed from further activity.
You can subscribe and participate further through the new bug through this link to our GitLab instance: https://gitlab.com/libeigen/eigen/issues/718.