在 GSL C++ 中使用复向量
Using complex vectors in GSL C++
我正在尝试使用 GSL 库来处理复杂的值。
在下面的代码中,我尝试创建一个复杂的向量,用相同的值填充它,然后读取它。
int N = 5;
gsl_vector_complex * Data = gsl_vector_complex_alloc(N);
gsl_complex temp = gsl_complex_rect(1,2);
gsl_vector_complex_set_all(Data,temp);
for(int i =0;i<N;i++)
{
cout << GSL_REAL(gsl_vector_complex_get(Data,i)) << " " << GSL_IMAG(gsl_vector_complex_get(Data,i)) << endl;
}
gsl_vector_complex_free(Data);
但是我得到的输出是错误的:
我试图用调试器跟踪内存的演变,看起来当我达到 3 时,我丢失了数据和临时中的所有元素:
这是我的收录:
#include <stdio.h>
#include <iostream>
#include <gsl/gsl_vector.h>
#include <gsl/gsl_complex.h>
#include <gsl/gsl_complex_math.h>
我认为您的编译工作流程有问题。事实上,我已经尝试通过编译重现你的问题
g++ complex.cpp -lgsl -lgslcblas
使用以下文件 complex.cpp:
#include <iostream>
#include <gsl/gsl_vector.h>
#include <gsl/gsl_complex.h>
#include <gsl/gsl_complex_math.h>
using namespace std;
int main () {
int N = 5;
gsl_vector_complex * Data = gsl_vector_complex_alloc( N );
gsl_complex temp = gsl_complex_rect( 1, 2 );
gsl_vector_complex_set_all( Data, temp );
for ( int i = 0; i < N ; i++ ) {
cout
<< GSL_REAL( gsl_vector_complex_get( Data, i ) )
<< " "
<< GSL_IMAG( gsl_vector_complex_get( Data, i ) )
<< endl;
}
gsl_vector_complex_free( Data );
}
我的结果是
1 2
1 2
1 2
1 2
1 2
正如任何人所期望的那样。
撇开不相关的评论:就编码风格而言,如果您使用GSL 的最新版本。
我正在尝试使用 GSL 库来处理复杂的值。 在下面的代码中,我尝试创建一个复杂的向量,用相同的值填充它,然后读取它。
int N = 5;
gsl_vector_complex * Data = gsl_vector_complex_alloc(N);
gsl_complex temp = gsl_complex_rect(1,2);
gsl_vector_complex_set_all(Data,temp);
for(int i =0;i<N;i++)
{
cout << GSL_REAL(gsl_vector_complex_get(Data,i)) << " " << GSL_IMAG(gsl_vector_complex_get(Data,i)) << endl;
}
gsl_vector_complex_free(Data);
但是我得到的输出是错误的:
我试图用调试器跟踪内存的演变,看起来当我达到 3 时,我丢失了数据和临时中的所有元素:
这是我的收录:
#include <stdio.h> #include <iostream> #include <gsl/gsl_vector.h> #include <gsl/gsl_complex.h> #include <gsl/gsl_complex_math.h>
我认为您的编译工作流程有问题。事实上,我已经尝试通过编译重现你的问题
g++ complex.cpp -lgsl -lgslcblas
使用以下文件 complex.cpp:
#include <iostream>
#include <gsl/gsl_vector.h>
#include <gsl/gsl_complex.h>
#include <gsl/gsl_complex_math.h>
using namespace std;
int main () {
int N = 5;
gsl_vector_complex * Data = gsl_vector_complex_alloc( N );
gsl_complex temp = gsl_complex_rect( 1, 2 );
gsl_vector_complex_set_all( Data, temp );
for ( int i = 0; i < N ; i++ ) {
cout
<< GSL_REAL( gsl_vector_complex_get( Data, i ) )
<< " "
<< GSL_IMAG( gsl_vector_complex_get( Data, i ) )
<< endl;
}
gsl_vector_complex_free( Data );
}
我的结果是
1 2
1 2
1 2
1 2
1 2
正如任何人所期望的那样。
撇开不相关的评论:就编码风格而言,如果您使用GSL 的最新版本。