我开发了以下一段效果很好的代码:
#include "header.h"
int test_DGGEV_11_14a(){
const int n=3;
double a[n][n]={1,1,1,2,3,4,3,5,2};
double b[n][n]={-10,-3,12,14,14,12,16,16,18};
//double a[n][n]={1,7,3,2,9,12,5,22,7};
//double b[n][n]={1,7,3,2,9,12,5,22,7};
/*const int n=2;
double a[n][n]={1e-16,0,0,1e-15};
double b[n][n]={1e-16,0,0,1e-15};*/
lapack_int info;
double alphar[n]={0.0};
double alphai[n]={0.0};
double beta[n]={0.0};
double vl[n][n]={0.0};
double vr[n][n]={0.0};
info=LAPACKE_dggev(LAPACK_ROW_MAJOR,'V','V',n,*a,n,*b,n,alphar,alphai,beta,*vl,n,*vr,n);
std::cout<<"right eigen vector (what we want):\n";
for(int i=int(0);i<n;i++){
for(int j=int(0);j<n;j++){
printf("%1f ",vr[i][j]);
}
printf("\n");
}
std::cout<<"left eigen vector:\n";
for(int i=int(0);i<n;i++){
for(int j=int(0);j<n;j++){
printf("%1f ",vl[i][j]);
}
printf("\n");
}
std::cout<<"eigen values:\n";
for(int i=int(0);i<n;i++){
if(beta[i]>DBL_MIN || beta[i]<-DBL_MIN){
printf("%1f ",alphar[i]/beta[i]);
printf("\n");
}else{
printf("%1f ","beta is zero");
printf("\n");
}
}
return info;
}
我修改了上面正确的代码,对大矩阵使用LAPACKE DGGEV例程,修改后的代码如下所示:
#include "header.h"
int test_DGGEV_11_17a(){
const int r=342;
const int c=342;
double**a=NULL;//stiffness
a=new double *[r];
for(int i=int(0);i<r;i++)
a[i]=new double[c];
readFile("Input_Files/OUTPUT_sub_2_stiffness.txt",a,r,c);
writeFile("Output_Files/K.txt",a,r,c);//to check if readFile was OK
double**b=NULL;//mass
b=new double*[r];
for(int i=int(0);i<r;i++)
b[i]=new double[c];
readFile("Input_Files/OUTPUT_sub_2_mass.txt",b,r,c);
writeFile("Output_Files/M.txt",b,r,c);//to check if readFile was OK
const int n=r;//r=c=n
lapack_int info=110;
double alphar[n]={0.0};
double alphai[n]={0.0};
double beta[n]={0.0};
//double vl[n][n]={0.0};//generates stack overflow
double**vl=NULL;
vl=new double*[r];
for(int i=int(0);i<r;i++)
vl[i]=new double[c];
for(int i=int(0);i<r;i++)
for(int j=int(0);j<c;j++)
vl[i][j]=0.0;
//double vr[n][n]={0.0};//generates stack overflow
double**vr=NULL;
vr=new double*[r];
for(int i=int(0);i<r;i++)
vr[i]=new double[c];
for(int i=int(0);i<r;i++)
for(int j=int(0);j<c;j++)
vr[i][j]=0.0;
info=LAPACKE_dggev(LAPACK_ROW_MAJOR,'V','V',n,*a,n,*b,n,alphar,alphai,beta,*vl,n,*vr,n);
return info;
}
在上面修改过的代码中(对于大型矩阵),我必须从堆中分配内存,否则堆栈会溢出。问题是,当我通过new
从堆分配内存时,我得到以下与堆相关的异常并发生在dbgheap.c
内(调试CRT堆函数):
有人知道为什么会发生此异常吗?也许这与LAPACKE DLL使用不同的堆进行分配有关......我不知道。
编辑:
堆栈跟踪是这样的:
编辑:
最后通过用1D数组替换所有2D数组,解决了问题。以下代码是更正后的代码,没有任何错误。请参阅&#34; Ilya Kobelevskiy&#34;的回答。有关此解决方案的详细信息。
int test_DGGEV_11_18a(){
const int r=342;
const int c=342;
double*a=NULL;//stiffness
a=new double [r*c];
for(int i=int(0);i<r*c;i++)
a[i]=0.0;
readFile_1Darray("Input_Files/OUTPUT_sub_2_stiffness.txt",a,r,c);
writeFile_1Darray("Output_Files/K.txt",a,r,c);//to check if readFile was OK
double*b=NULL;//mass
b=new double[r*c];
for(int i=int(0);i<r*c;i++)
b[i]=0.0;
readFile_1Darray("Input_Files/OUTPUT_sub_2_mass.txt",b,r,c);
writeFile_1Darray("Output_Files/M.txt",b,r,c);//to check if readFile was OK
const int n=r;//r=c=n
lapack_int info=110;
//double alphar[n]={0.0};
double*alphar=NULL;
alphar=new double[n];
for(int i=int(0);i<n;i++)
alphar[i]=0.0;
//double alphai[n]={0.0};
double*alphai=NULL;
alphai=new double[n];
for(int i=int(0);i<n;i++)
alphai[i]=0.0;
//double beta[n]={0.0};
double*beta=NULL;
beta=new double[n];
for(int i=int(0);i++;)
beta[i]=0.0;
//double vl[n][n]={0.0};//generates stack overflow
double*vl=NULL;
vl=new double[r*c];
for(int i=int(0);i<r*c;i++)
vl[i]=0.0;
//double vr[n][n]={0.0};//generates stack overflow
double*vr=NULL;
vr=new double[r*c];
for(int i=int(0);i<r*c;i++)
vr[i]=0.0;
info=LAPACKE_dggev(LAPACK_ROW_MAJOR,'V','V',n,a,n,b,n,alphar,alphai,beta,vl,n,vr,n);
std::cout<<"info returned by LAPACKE_dggev:\t"<<info<<'\n';
double*eigValueReal=NULL;
eigValueReal=new double[n];
for(int i=int(0);i<n;i++)
eigValueReal[i]=0.0;
for(int i=int(0);i<n;i++)
eigValueReal[i]=alphar[i]/beta[i];
write1Darray("Output_Files/eigValueReal_LAPACKE_DGGEV.txt",eigValueReal,n);
write1Darray("Output_Files/beta.txt",beta,n);
writeFile_1Darray("Output_Files/eigVectorRight_LAPACKE_DGGEV.txt",vr,r,c);
delete[] a;
delete[] b;
delete[] alphar;
delete[] alphai;
delete[] beta;
delete[] vl;
delete[] vr;
delete[] eigValueReal;
return info;
}
答案 0 :(得分:1)
根据documentation LAPACKE_dggev期望double *作为输入,因此所有矩阵都需要存储为线性数组。
而不是
double**a=NULL;//stiffness
a=new double *[r];
for(int i=int(0);i<r;i++)
a[i]=new double[c];
你应该使用
double*a=new double[c*r];//stiffness
所有otehr矩阵的类似变化。矩阵元素可以作为[i,j]的[i * c + j]访问。现在,您的代码可能存在其他问题,但这很明显可能导致您看到的错误。