我是C ++的新手,我正试图弄清楚如何使用LAPACK来找到无限带状矩阵的特征值(非谐振子问题)。我知道我正在正确地计算矩阵,因为我检查了值并且它们都匹配。但是,我不确定我是否正确地将值传递给子程序,或者我是否因为返回的特征值不是我所期待的而混淆了。我正在使用dsbtrd子例程来计算它。这是手册:http://www.netlib.org/lapack/explore-html/d0/d62/dsbtrd_8f.html
关于我可能出错的地方的任何想法?
#include <iostream>
#include <algorithm>
#include <string>
#include <math.h>
using namespace std;
//SUBROUTINE DSBTRD( VECT, UPLO, N, KD, AB, LDAB, D, E, Q, LDQ, WORK, INFO )
extern "C" {
void dsbtrd_(const char *vect, const char *uplo, int *n, int *kd, double *ab, int *ldab, double d[], double e[], double *q, int *ldq, double work[], int *info);
}
#define MAX 14
int main(){
// Values needed for dsbtrd
const char *vect = "V";
const char *uplo = "U";
int n;
int ldab = MAX;
int ldq = MAX;
int info;
double ab[MAX][ldab];
double d[MAX];
double e[MAX];
double q[MAX][ldq];
double work[MAX];
//other values needed
int i,j,delta;
double eps;
double a[MAX][MAX];
double g[MAX][MAX];
//Read in value of eps and n
cout <<"Enter epsilon: \n";
cin >> eps;
cout << "Epsilon = " << eps << "\n";
cout <<"Enter n: \n";
cin >> n;
cout << "n = " << n << "\n";
if(n >= MAX){
cerr << "n is great than max \n";
}
//Build matrix g
for(j = 0; j < n; j++){
for(i = 0; i < n; i++){
int m = min(i,j);
if(i == j){
g[i][j] = 1.5*(pow(m,2) + m +.5);
}else if( i == j + 2 || i == j - 2){
g[i][j] = (m + 1.5)*sqrt((m+1)*(m+2));
}else if(i == j + 4 || i == j -4){
g[i][j] = .25*sqrt((m+1)*(m+2)*(m+3)*(m+4));
}else{
g[i][j] = 0;
}
}
}
//Build the starting matrix a
//row i, column j
for(j = 0; j < n; j++){
for(i = 0; i < n; i++){
if(i == j){
delta = 1;
}else{
delta = 0;
}
a[i][j] = (i + .5)*delta + eps*g[i][j];
}
}
//Build the matrix ab
// ab(kd+1+i-j,j) = a(i,j) for max(1,j-kd)<=i<=j
int kd = n - 1;
for(j = 1; j <= n; j++){
for(i = max(1,j-kd); i <= j; i++){
ab[j-1][kd+i-j] = a[j-1][i-1];
}
}
//Solve for eigenvalues
dsbtrd_(vect, uplo, &n, &kd, &ab[0][0], &ldab, d, e, &q[0][0], &ldq, work, &info);
//Check for success
if(info == 0)
{
//Write answer
for(i = 0; i < n; i++){
cout << "Eigenvalue " << i << ": " << d[i] << "\n";
}
}
else
{
//Write error
cerr << "dsbtrd returned error " << info << "\n";
}
return info;
}
答案 0 :(得分:2)
DSBTRD不计算特征值。它将矩阵缩小为三角形;你正在拉出所得到的三对角矩阵的主对角线并假装那些是特征值,但它们不是。
您需要在生成的三对角形式上调用DSTERF(或其他几个例程之一)来获取特征值。
有关详细信息,请参阅LAPACK User's Guide。