Question

How can I initialize a SparseVector in Eigen ? The following code:

#define EIGEN_YES_I_KNOW_SPARSE_MODULE_IS_NOT_STABLE_YET
#include <Eigen/Sparse>
using namespace Eigen;
SparseVector<float> vec(3);
main()
{
  vec(0)=1.0;
}

gives me the following error

error: call of an object of a class type without appropriate operator() or conversion functions to pointer-to-function type vec(0)=1.0;

by the way, vec[0]=1.0 doesn't work either.

Was it helpful?

Solution

Looking at the documentation I noticed Scalar& coeffRef(Index i), and it says:

Returns a reference to the coefficient value at given index i. This operation involes a log(rho*size) binary search. If the coefficient does not exist yet, then a sorted insertion into a sequential buffer is performed. (This insertion might be very costly if the number of nonzeros above i is large.)

So the following should work:

#define EIGEN_YES_I_KNOW_SPARSE_MODULE_IS_NOT_STABLE_YET
#include <Eigen/Sparse>
using namespace Eigen;
SparseVector<float> vec(3);
main()
{
    vec.coeffRef(0)=1.0;
}

Not sure why they did it that way instead of using array overloading. Perhaps when it becomes IS_STABLE then they'll do it in a more typical C++ way?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top