Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I initialize a SparseVector in Eigen

Tags:

c++

eigen

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.

like image 408
Tarek Avatar asked Sep 22 '11 15:09

Tarek


1 Answers

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?

like image 64
HostileFork says dont trust SE Avatar answered Sep 29 '22 02:09

HostileFork says dont trust SE