0

我是 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;
}
4

1 回答 1

2

DSBTRD 不计算特征值。它将矩阵简化为三对角形式;您正在拉出生成的三对角矩阵的主对角线并假装这些是特征值,但它们不是。

您需要在生成的三对角形式上调用 DSTERF(或其他几个例程之一)以获取特征值。

有关详细信息,请参阅LAPACK 用户指南

于 2012-11-16T13:27:39.943 回答