gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 02:15:16 30 4
gpt4 key购买 nike

我在 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/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com