Page 1 of 1

### DSYEV returning wrong eigenvectors an values Posted: Thu Jun 16, 2016 3:59 pm
The DSYEV command returned incorrect eigenvalues and eigenvectors for a small 2*2 matrix. The matrix was
2 1
1 2.
Additionally instead of giving the eigenvectors back the program returned the original matrix. I looked through the bug list but could not find this kind of problem. I am using LAPACK 3.0 and my code is featured below:
#include <iostream>
#include <cstdlib>
using namespace std;
extern "C" void dsyev_(char * jobz, char * uplo, int*n, double * a, int * lda, double * w, double * work, int * lwork, int * info);
int main()
{
char jobz = 'v';
char uplo = 'u';
int n = 2;
int lda = 2;
double a[lda][n] = {{2,1},{1,2}};
double w[n];
int lwork = 10;
double work[lwork];
int info;
dsyev_(&jobz, &uplo, &n, &a[lda][n], &lda, &w[n], &work[lwork], &lwork, &info);
cout <<"The eigenvalues of the matrix are: " ;
for (int a=0; a<n; a++)
{
cout <<w[a] <<" ";
}
cout <<'\n' <<"The info is " <<info;
cout <<'\n' <<"The eigenvectors are " <<'\n';
for (int i=0; i<n; i++)
{
for (int j=0; j<lda; j++)
{
cout <<a[j][i];
}
cout <<'\n';
}
return 0;
}

My results were:
The eigenvalues of the matrix are: -1 -3.12825e-148
The info is 0
The eigenvectors are
21
12

Hopefully,
Harry

### Re: DSYEV returning wrong eigenvectors an values Posted: Tue Jun 28, 2016 4:12 pm
I figured out the answer! It turns out that I wasn't storing the vectors and outputting the properly. I changed the code to the following and it worked!:
#include <iostream>
#include <cstdlib>
#include <vector>
using namespace std;
extern "C" void dsyev_(char * jobz, char * uplo, int*n, double * a, int * lda, double * w, double * work, int * lwork, int * info);
int main()
{
char jobz = 'v';
char uplo = 'u';
int n = 2;
int lda = 2;
int lwork = 10;
int info;

vector<double> a;

a.push_back(2);
a.push_back(1);
a.push_back(1);
a.push_back(2);

double w[n];
double work[lwork];

dsyev_(&jobz, &uplo, &n, & *a.begin(), &lda, w, work, &lwork, &info);
cout <<"The eigenvalues of the matrix are: " ;
cout <<'\n' << w << " " << w ;
cout <<'\n' <<"The info is " <<info;
cout <<'\n' <<"The eigenvectors are " <<'\n';
cout <<'\n' << a << " " <<a << '\n' <<a << " " <<a;

return 0;
}