Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Eigen Package Iterate over Row Major Sparse Matrix

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

like image 671
akd Avatar asked Mar 15 '14 08:03

akd


1 Answers

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

like image 108
ggael Avatar answered Oct 24 '22 15:10

ggael