如何在Eigen中初始化SparseVector [英] How can I initialize a SparseVector in Eigen

查看:276
本文介绍了如何在Eigen中初始化SparseVector的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在Eigen中初始化SparseVector?以下代码:

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

给我以下错误


错误:在没有适当的operator()或没有将函数转换为指针到函数类型
vec(0)= 1.0;的情况下调用类类型的对象>

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

顺便说一下,

,vec [0] = 1.0也不起作用。

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

推荐答案

在查看文档时,我注意到 Scalar& coeffRef(Index i),它说:

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

返回给定索引i处的系数值的引用。此操作涉及log(rho * size)二进制搜索。如果系数尚不存在,则将排序插入到顺序缓冲区中。 (如果在i之上的非零数目很大,则此插入可能会非常昂贵。)

因此,以下操作应该有效:

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

不知道为什么这样做而不是使用数组重载。也许当它变成IS_STABLE时,他们会以更典型的C ++方式来做到这一点?

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?

这篇关于如何在Eigen中初始化SparseVector的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆