简体   繁体   中英

Error in CoeffRef of Eigen sparse matrix with elements type of a vector

I define a sparse matrix as Eigen::SparseMatrix< Eigen::Matrix<float, 3, 1> > , which means each element of the matrix is a 3x1 vector. However, when I am calling the function CoeffRef in order to assign a vector into the elements, I got the following error in SparseMatrix.h:

no operator "=" matches these operands. 

and the error comes from the function insert , while it assigns an int to an Eigen::Matrix< float, 3, 1> , which is m_data.value(p) = 0 (considering that m_data.value(p) is a vector 3x1 and 0 is an int).

It seems that in this line of code (line 1235 of SparseMatrix.h), they did not take into account the template type of matrix for the comparison.

I was wondering if you would have any sort of ideas to solve this error?

typedef Eigen::Matrix< float, 3, 1> Vec3f;

Eigen::SparseMatrix< Vec3f > lA( m, n);
lA.reserve( Eigen::VectorXi::Constant(m, 4) );

for( unsigned int i = 0; i < m; i++)
{

   Vec3f lVec( 0.0, 0.0, 1.0);

   lA.coeffRef(i, i) = lVec; // got the error here!
}

This is because coeffRef tries to initialize the newly created element to 0, but 0 cannot be assigned to a Vector3f . So the solution is to use an Array3f instead:

typedef Eigen::Array<float, 3, 1> Vec3f;

Of course, beware that operator* behave differently on Array than on vector and matrices.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM