当我直接提供矩阵时,为什么这个LAPACK程序能够正常工作,但是当我从文件中读取它时却没有?

时间:2015-07-02 14:03:18

标签: c++ lapack eigenvalue

下面是一个用于对矩阵A进行对角化的LAPACK代码,我以数组a的形式提供。这只是对官方示例的略微修改,似乎产生了正确的结果。这是不切实际的,因为我必须直接提供数组。

#include <stdlib.h>
#include <stdio.h>
#include <fstream>
#include <vector>


/* DSYEV prototype */
extern "C"{
void dsyev( char* jobz, char* uplo, int* n, double* a, int* lda,
                double* w, double* work, int* lwork, int* info );
}
/* Auxiliary routines prototypes */
extern "C"{ 
void print_matrix( char* desc, int m, int n, double* a, int lda );
}
/* Parameters */
#define N 5
#define LDA N

/* Main program */
int main() {
        /* Locals */
        int n = N, lda = LDA, info, lwork;
        double wkopt;
        double* work;
        /* Local arrays */
        double w[N];
        double a[LDA*N] = {
            1.96,  0.00,  0.00,  0.00,  0.00,
           -6.49,  3.80,  0.00,  0.00,  0.00,
           -0.47, -6.39,  4.17,  0.00,  0.00,
           -7.20,  1.50, -1.51,  5.70,  0.00,
           -0.65, -6.34,  2.67,  1.80, -7.10
        };
        /* Executable statements */
        printf( " DSYEV Example Program Results\n" );
        /* Query and allocate the optimal workspace */
        lwork = -1;
        dsyev( "Vectors", "Upper", &n, a, &lda, w, &wkopt, &lwork, &info );
        lwork = (int)wkopt;
        work = (double*)malloc( lwork*sizeof(double) );
        /* Solve eigenproblem */
        dsyev( "Vectors", "Upper", &n, a, &lda, w, work, &lwork, &info );
        /* Check for convergence */
        if( info > 0 ) {
                printf( "The algorithm failed to compute eigenvalues.\n" );
                exit( 1 );
        }
        /* Print eigenvalues */
        print_matrix( "Eigenvalues", 1, n, w, 1 );
        /* Print eigenvectors */
        print_matrix( "Eigenvectors (stored columnwise)", n, n, a, lda );
        /* Free workspace */
        free( (void*)work );
        exit( 0 );
} /* End of DSYEV Example */

/* Auxiliary routine: printing a matrix */
void print_matrix( char* desc, int m, int n, double* a, int lda ) {
        int i, j;
        printf( "\n %s\n", desc );
        for( i = 0; i < m; i++ ) {
                for( j = 0; j < n; j++ ) printf( " %6.2f", a[i+j*lda] );
                printf( "\n" );
        }
}

我只是想修改上面的代码,这样我就可以从文件中读取数据而不是直接提供它。为此,我编写了函数 read_covariance ,它从文件 peano_covariance.data 中读取数组。后一个数据文件的内容是:

1.96 0.00 0.00 0.00  0.00
-6.49 3.80 0.00 0.00 0.00
-0.47 -6.39 4.17 0.00 0.00
-7.20 1.50 -1.51 5.70 0.00
-0.65 -6.34 2.67 1.80 -7.10

下面是我的尝试,它会产生非常不正确的特征值和特征向量。

#include <stdlib.h>
#include <stdio.h>
#include <fstream>
#include <vector>


int read_covariance (std::vector<double> data)
  {
    double tmp;

    std::ifstream fin("peano_covariance.data");

    while(fin >> tmp)
    {
        data.push_back(tmp);
    }

    return 0;
}

/* DSYEV prototype */
extern "C"{
void dsyev( char* jobz, char* uplo, int* n, double* a, int* lda,
                double* w, double* work, int* lwork, int* info );
}
/* Auxiliary routines prototypes */
extern "C"{ 
void print_matrix( char* desc, int m, int n, double* a, int lda );
}
/* Parameters */
#define N 5
#define LDA N

/* Main program */
int main() {
        /* Locals */
        std::vector<double> data;
        int n = N, lda = LDA, info, lwork;
        double wkopt;
        double* work;
        /* Local arrays */
        double w[N];
        double a[LDA*N];
        read_covariance(data);

        std::copy(data.begin(), data.end(), a);
        /* Executable statements */
        printf( " DSYEV Example Program Results\n" );
        /* Query and allocate the optimal workspace */
        lwork = -1;
        dsyev( "Vectors", "Upper", &n, a, &lda, w, &wkopt, &lwork, &info );
        lwork = (int)wkopt;
        work = (double*)malloc( lwork*sizeof(double) );
        /* Solve eigenproblem */
        dsyev( "Vectors", "Upper", &n, a, &lda, w, work, &lwork, &info );
        /* Check for convergence */
        if( info > 0 ) {
                printf( "The algorithm failed to compute eigenvalues.\n" );
                exit( 1 );
        }
        /* Print eigenvalues */
        print_matrix( "Eigenvalues", 1, n, w, 1 );
        /* Print eigenvectors */
        print_matrix( "Eigenvectors (stored columnwise)", n, n, a, lda );
        /* Free workspace */
        free( (void*)work );
        exit( 0 );
} /* End of DSYEV Example */

/* Auxiliary routine: printing a matrix */
void print_matrix( char* desc, int m, int n, double* a, int lda ) {
        int i, j;
        printf( "\n %s\n", desc );
        for( i = 0; i < m; i++ ) {
                for( j = 0; j < n; j++ ) printf( " %e", a[i+j*lda] );
                printf( "\n" );
        }
}

1 个答案:

答案 0 :(得分:4)

替换

int read_covariance (std::vector<double> data)

int read_covariance (std::vector<double> & data)

您正在发送数组的副本而不是对它的引用。它是正在填充值的临时副本。这就是bg2b在评论中提到的内容。

但就个人而言,我宁愿写一些像

这样的东西
int read_covariance (const std::string & fname)
{
    std::ifstream in(fname.c_str());
    double val;
    std::vector<double> cov;
    while(in >> val) cov.push_back(val);
    return cov;
}

更好的方法是使用适当的多维数组库而不是笨重的1d向量。有太多这样的库可用,我不确定哪个是最好的(C ++标准库中缺少一个好的多维数组类是我经常使用fortran的主要原因之一),但ndarray看起来很有趣 - 它旨在模仿python的优秀numpy数组模块的功能。