2015-04-17 105 views
0

我是这个论坛的新手。我最近开始学习C++,因为它是我工作所需要的。我在Ubuntu 12.04上安装了GSL库和BLAS。我已经编写了计算矩阵指数的代码,它可以与gsl库一起工作。我现在写了C++代码来转换一个复杂的矩阵。这似乎很棘手。我首先需要调用gsl函数gsl_linalg_complex_LU_decomp()来定义矩阵的分解,然后调用另一个gsl函数来计算矩阵逆。但是,下面给出的程序不能在Ubuntu 12.04上编译。我使用的命令未定义引用`gsl_linalg_complex_LU_decomp_'

g++ MatInv.cpp -o MatInv.exe -lgsl -llapack. 

输出误差在终端上就是:

/tmp/ccUVd80t.o: In function `main':MatInv.cpp.text+0x638): undefined reference to `gsl_linalg_complex_LU_decomp_' 
collect2: ld returned 1 exit status 

我已经搜索网对于这个问题,但似乎无法找到正确的答案,我的问题。代码如下(再一次,我是C++的新手,所以你可能会发现这是一种非常原始的代码编写方式。)这里我只计算矩阵的分解。请让我知道这里有什么问题。谢谢你的帮助。

#include <iostream> 
#include <gsl/gsl_matrix_complex_double.h> 
#include <gsl/gsl_linalg.h> 
#include <gsl/gsl_complex.h> 
#include <gsl/gsl_complex_math.h> 
#include <gsl/gsl_cblas.h> 
#include <gsl/gsl_matrix.h> 
#include <cmath> 
#include <complex> 
#include <fstream> 


using namespace::std; 


typedef complex<double> cd; 


extern "C" int gsl_linalg_complex_LU_decomp_(gsl_matrix_complex *A, gsl_permutation *p, int signum); 


//************************************************************************** 


int main() 
{ 


//Here we define matrix A 


int dimA=3;   //dimensions of the matrix A 
cd im(0.,1.),u(1.,0.); 
cd **A,**B; 
A= new cd *[dimA]; 
B= new cd *[dimA]; 



for(int i=0; i<dimA; i++){ 
A[i]= new cd [dimA]; 
B[i]= new cd [dimA]; 
} 


for(int i=0; i< dimA; i++){ // Initializing the matrix 
for(int j=0; j<dimA; j++){ 
    A[i][j]=(0.,0.); 
    B[i][j]=(0.,0.); 
} 
} 


ofstream Lmat; 
Lmat.open("Mat.dat"); 
for(int i=0; i< dimA; i++){ 
for(int j=0; j< dimA; j++){ 
    if(i==j)A[i][j]=.1*(i+1)*u+.2*(j+1)*u; 
    else A[i][j]=.1*(i+1)*u+.2*(j+1)*im; 
    Lmat<<A[i][j]; 
} 
Lmat<<endl; 
} 


Lmat.close(); 


gsl_matrix_complex *gsl_A= gsl_matrix_complex_calloc(dimA,dimA); 


for(int i=0;i<dimA;i++){ 
for(int j=0;j<dimA;j++){ 
    double A_elem_re = A[i][j].real(); 
    double A_elem_im = A[i][j].imag(); 
    gsl_matrix_complex_set(gsl_A,i,j,gsl_complex_rect(A_elem_re,A_elem_im)); 
} 
} 


gsl_permutation *p=gsl_permutation_alloc(dimA); 
int signum; 


gsl_linalg_complex_LU_decomp_(gsl_A, p, signum); 




gsl_permutation_free (p); 


return 0; 
} 

回答

0

这里有几个问题。

首先,你不需要声明函数gsl_linalg_complex_LU_decomp这样你就可以摆脱线的

extern "C" int gsl_linalg_complex_LU_decomp_(gsl_matrix_complex *A, gsl_permutation *p, int signum); 

其次,当你对你有几个问题后调用这个函数,首先你需要有一个错误地强调它的名字,所以摆脱它,其次,参数signum需要是int *(即指向int)而不是int,就像你现在正在做的那样。

如果更换

gsl_linalg_complex_LU_decomp(gsl_A, p, &signum); 

该行代码应该编译罚款。

+0

谢谢,西蒙。它工作得很好!:-)但是我只是想知道,在我的其他代码中,我在调用GSL函数时强调了它,它工作正常。事实上,我从某个地方读到,需要从C++代码调用Fortran子例程。是对的吗?再次感谢。我在这浪费了两天时间。 :-( – OPP