How can I initialize a SparseVector in Eigen

空扰寡人 提交于 2020-01-01 09:33:53

问题


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.


回答1:


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?



来源:https://stackoverflow.com/questions/7517318/how-can-i-initialize-a-sparsevector-in-eigen

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!