我在Eigen中编写了一个非常简单的函数,它应该创建线性独立的向量。相反,它始终创建nullvectors。我希望解决方案非常简单,但我只是看不到它。
为什么地球上我的矢量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);
}
/* --- --- */
答案 0 :(得分:2)
将您的功能签名更改为:
int basisvector(int m, VectorXh & result) // note the ampersand
您正在做的是将值result
传递给函数。函数内部对result
参数所做的更改将在函数结束后丢失。