为什么这个 LAPACK 程序在我直接提供矩阵时可以正常工作,但在我从文件中读取它时却不能?
Why does this LAPACK program work correctly when I provide the matrix directly, but not when I read it from a file?
下面是对矩阵A进行对角化的LAPACK代码,我以数组a的形式提供。这只是对官方示例的轻微修改,似乎会产生正确的结果。这是不切实际的,因为我必须直接提供数组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" );
}
}
替换
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;
}
更好的方法是使用适当的多维数组库而不是笨重的一维向量。有很多这样的库可用,我不确定哪个是最好的(C++ 标准库中缺少好的多维数组 class 是我经常使用 fortran 的主要原因之一) ,但 ndarray 看起来很有趣 - 它旨在模仿 python.
的优秀 numpy
数组模块的功能
下面是对矩阵A进行对角化的LAPACK代码,我以数组a的形式提供。这只是对官方示例的轻微修改,似乎会产生正确的结果。这是不切实际的,因为我必须直接提供数组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" );
}
}
替换
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;
}
更好的方法是使用适当的多维数组库而不是笨重的一维向量。有很多这样的库可用,我不确定哪个是最好的(C++ 标准库中缺少好的多维数组 class 是我经常使用 fortran 的主要原因之一) ,但 ndarray 看起来很有趣 - 它旨在模仿 python.
的优秀numpy
数组模块的功能