Below comes a LAPACK code for diagonalising a matrix A, which I provide in the form of an array a. It is but a slight modification of an official example and appears to produce correct results. It is impractical, because I have to provide the array a directly.
#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" );
}
}
I merely want to modify the above code, so that I can read the array from a file instead of providing it directly. To that end I wrote the function read_covariance that reads the array from a file peano_covariance.data. The contents of the latter data file are:
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
Below is my attempt, which produces very incorrect eigenvalues and eigenvectors.
#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" );
}
}
Replace
int read_covariance (std::vector<double> data)
with
int read_covariance (std::vector<double> & data)
You are sending in a copy of the array rather than a reference to it. It is the temporary copy that is being filled with values. This is what bg2b is referring to in his comment.
Personally, though, I would rather write something like
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;
}
Even better would be to use a proper multidimensional array library rather than unwieldy 1d vectors. There's a plethora of such libraries available, and I'm not sure which is the best one (the lack of a good multidimensional array class in the C++ standard library is one of the main reasons why I often use fortran instead), but ndarray looks interesting - it aims to mimic the features of the excellent numpy
array module for python.