Suppose I write the following:
func(Eigen::SparseMatrixBase<double> & A){
for(int i = 0; i < A.outerSize(); i++)
for(Eigen::SparseMatrixBase<double>::InnerIterator it(A,i);it;++it)
// do something
}
This function does not work, since SparseMatrixBase
cannot initialize an inner iterator.
So, I define the function for both RowMajor
and ColMajor
, then I try to template these functions:
template<class Mat, class MatI>
func(Mat & A){
for(int i = 0; i < A.outerSize(); i++)
for(MatI it(A,i);it;++it)
//do something
func<Eigen::SparseMatrix<double,Eigen::RowMajor>,ditto::InnerIterator>(Arowmajor);
func<...,...>(Acolmajor);
And when I compile:
error: cannot convert Eigen::SparseMatrix<double,RowMajor> &
to Eigen::SparseMatrix<double,0,int> &
Then, I change the types:
func<Eigen::SparseMatrix<double,0,int>,ditto::InnerIterator>(Arowmajor)
And the error? the inverse of the previous:
error: cannot convert Eigen::SparseMatrix<double,0,int> &
to Eigen::SparseMatrix<double,RowMajor> &
What is the proper way to handle iteration and templating with the Eigen classes?