5

I was trying to iterate over the non zero elements of a row major sparse matrix, such as shown below:

Eigen::SparseMatrix<double,Eigen::RowMajor> Test(2, 3);
Test.insert(0, 1) = 34;
Test.insert(1, 2) = 56;
for (int k = 0; k < Test.outerSize(); ++k){
    for (Eigen::SparseMatrix<double>::InnerIterator it(Test, k); it; ++it){
        cout << it.row() <<"\t";
        cout << it.col() << "\t";
        cout << it.value() << endl;
    }
}

but I dont see the right values. Instead, I see random values for it.row(), a value of 1 for it.col() and some random value for it.value(), as shown below:

-17891602       1       -2.65698e+303

Changing RowMajor to ColumnMajor makes the code work as expected.

I am not sure what went wrong for the row major part ? Can someone please let me know what am I missing here ?

Thanks in advance

akd
  • 53
  • 1
  • 4

1 Answers1

7

I'm surprised that it compiles fine: the type of your iterator is not correct. It must be a SparseMatrix<double,Eigen::RowMajor>::InnerIterator.

ggael
  • 28,425
  • 2
  • 65
  • 71
  • 1
    ah !! I see. That makes sense. Let me file a bug with Eigen because I dont see any compilation error. Not sure if this is a user responsibility, but it would be good to let Eigen folks know. Many thanks for your help on this. – akd Mar 16 '14 at 00:37
  • 1
    Apparently this still happens with the last Eigen release. Was the bug indeed reported/considered? The problem lies in the user code, but a check in the iterator's constructor would help I guess. – BenC Jun 25 '15 at 09:17