Hi,
I want to get a eigenvalues of symetric matrix, but I have no idea which routine i should use.
I want to call the function in C code to get the result.
Any help will be highly appreciated.
Julie wrote:Have a look at the LAPACK routines:
dsyev (for double, ssyev if you are using float).
They are also more advanced routines:
dysevr, dsyevd, dsyevx. You can have a look at them as well. You should have no problem to call the LAPACK library from C, just keep in mind that LAPACK is a Fortran library.
Julie
#include <stdio.h>
#include <stdlib.h>
extern void dsyev_( char *jobz, char *uplo, int *n, double *a, int *lda,
double *w, double *work, int *lwork, int *info );
int main(){
double *a=NULL, *work=NULL, *w=NULL;
int n,lwork,info;
n = 3;
a = (double *)malloc(n*n*sizeof(double));
w = (double *)malloc(n*sizeof(double));
a[0] = 2;
a[1] = 1; a[4] = 2;
a[2] = 1; a[5] = 1; a[8] = 2;
work = (double *)malloc(1*sizeof(double));
lwork=-1;
dsyev_( "N", "L", &n, a, &n, w, work, &lwork, &info );
lwork= work[0];
free(work);
work = (double *)malloc(lwork*sizeof(double));
dsyev_( "N", "L", &n, a, &n, w, work, &lwork, &info );
fprintf(stdout,"e1=%f\n",w[0]);
fprintf(stdout,"e2=%f\n",w[1]);
fprintf(stdout,"e3=%f\n",w[2]);
free(work);
free(w);
free(a);
return 0;
}
A = [ 2 1 1
1 2 1
1 1 2
]
Julie wrote:You need to input A in column major format. This means you describe A column by column. A needs to be of size at least N^2. Since A is symmetric, you only need to fill either the upper part or the lower part. (Well if you fill the whole matrix it is not a big deal). If you have filled the lower part of the matrix you set UPLO to 'L'. If you have filled the upper part then you set UPLO to 'L'.
Julie.
/tmp/ccUGZQ3o.o: In function `main':internet.cpp:(.text+0xfc): undefined reference to `dsyev_(char*, char*, int*, double*, int*, double*, double*, int*, int*)'
:internet.cpp:(.text+0x17e): undefined reference to `dsyev_(char*, char*, int*, double*, int*, double*, double*, int*, int*)'
collect2: ld returned 1 exit status
extern void dsyev_( char *jobz, char *uplo, int *n, double *a, int *lda,
double *w, double *work, int *lwork, int *info );
internet.cpp:25: warning: converting to ‘int’ from ‘double’
lwork= (int) work[0];
[internet.cpp:41:3: warning: no newline at end of file
g++ example_dsyev.cpp -o ${LAPACK_DIR}/liblapack.a ${BLASDIR}/libblas.a -lg2c -lm
g77 internet.cpp -o a.out /usr/lib/liblapack.a /usr/lib/libblas.a
Users browsing this forum: No registered users and 5 guests