c++ - 在 EIGEN 中错误地设置 vector

标签 c++ eigen

我在 Eigen 中编写了一个极其简单的函数,它应该创建线性无关的 vector 。相反,它始终创建空 vector 。我希望解决方案简单得令人尴尬,但我就是没有看到。

为什么我的 vector mpbv 的分量不都等于 cos(0) =1.00(在我的 pi 值的精度内)?

相反,我得到:

ludi@ludi-M17xR4:~/Desktop/tests$ g++ -O3 -w -o medrealbv.x medrealbv.cc -L/usr/local/lib -lgsl -lgslcblas && ./medrealbv.x
basisvector =
3.11621e-317
 2.0776e-317
6.95251e-310
ludi@ludi-M17xR4:~/Desktop/tests$ 

因此,每个组件的值都非常接近于零!我的程序如下。

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <Eigen/Dense>      
#define helix 3
#define pi 3.14159


using namespace Eigen;
typedef Matrix<double, helix, 1> VectorXh;

/*-- DECLARATIONS --*/
int basisvector(int m, VectorXh result);


/*--- MAIN ---*/
int main()
{
VectorXh mpbv;
basisvector(0, mpbv);

  std::cout << "basisvector =\n" << mpbv << std::endl;

return(0);
}

/* --- --- */
int basisvector(int m, VectorXh result)
{
    int k;
    double component;


    for(k=0;k<helix;k++)
    {
        component =cos(m*k*2.0*pi/helix);
        result(k) = component;      

    }



    return(0);
}

/* --- --- */

最佳答案

将您的函数签名更改为:

int basisvector(int m, VectorXh & result) // note the ampersand

您正在做的是将 result 按值传递给函数。在函数结束后,您在函数内部对 result 参数所做的更改将丢失。

关于c++ - 在 EIGEN 中错误地设置 vector ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34227966/

相关文章:

c++ - 特征的新手问题

c++ - Eigen 中的矩阵乘法非常慢

c++ - Eigen::Ref<> 作为成员变量

android - GoogleSource的Android NDK的Eigen库

c++ - 二维数组被函数修改后如何删除?

c++ - 奇怪的顶点着色器/像素着色器故障

c++ - 如何从模板中获取类型和值信息

C++ 模板函数,可以采用 Eigen::vector 或 std::vector

c++ - Qtabwidget 每个标签标题背景色

c++ - 非常非常基本 : Why won't my makefile work