我是這個論壇的新手。我最近開始學習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;
}
謝謝,西蒙。它工作得很好!:-)但是我只是想知道,在我的其他代碼中,我在調用GSL函數時強調了它,它工作正常。事實上,我從某個地方讀到,需要從C++代碼調用Fortran子例程。是對的嗎?再次感謝。我在這浪費了兩天時間。 :-( – OPP