7

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.

Tarek
  • 1,060
  • 4
  • 17
  • 38

1 Answers1

3

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?

  • 3
    I think they wanted make users aware of a potentially costly op. – eudoxos Sep 24 '11 at 21:28
  • 1
    Yeah but this is just incorrect syntax. You don't generally _assign_ to the return value of a function call. – bobobobo Nov 06 '12 at 16:39
  • I agree with bobobobo that the current interface is just confusing. I found this website because the obvious syntax didn't work. That is a sign that the interface is a bit obtuse (not to mention verbose). – Joe Jul 21 '13 at 00:03