Somayeh
Somayeh

Reputation: 15

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!
}

Upvotes: 1

Views: 856

Answers (1)

ggael
ggael

Reputation: 29205

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 Arraythan on vector and matrices.

Upvotes: 1

Related Questions