/* ATK IV - Numerical Programming (2006) */ /* Excercise 9.2 - Optional Homework Problem */ #include<stdio.h> #include<math.h> #include<stdlib.h> #define SIZE 4 /* Because LAPACK is done in FORTRAN we have to do some tricks. */ /* The function we want to use is ssyev. The cc and gcc compilers */ /* know how to use FORTRAN functions in C programs. All we have */ /* do is to introduce the function in C formalism. Additionally, */ /* we have include _ sign after the function in order that the */ /* compiler knows that we are calling a FORTRAN function. Also, */ /* due to the differences in arrays between C and FORTRAN, we */ /* must insert our variables as pointers. */ /* The program can be compiled with command: */ /* */ /* cc e0903.c -o e0903 -llapack -lblas */ /* */ /* This includes the necessary FORTRAN libraries to the program. */ /* To understand fully what happens here, you should check the */ /* information that is found in the web. It is not in anyway */ /* essential for the understanding the rest of the course. */ extern int ssyev_(char *jobz, char *uplo, int *n, float *a, int *lda, float *w, float *work, int *lwork, int *info); int main(void){ float a[SIZE*SIZE], x[SIZE]; char uplo,jobz; float *work; int lwork, info; int i,j,n; FILE *file; file=fopen("matrix_sym.dat","rt"); fscanf(file,"%d\n",&n); uplo = 'U'; jobz = 'V'; /* routines below need space to work in: */ lwork = 64*n; /* allocate memory, lwork doubles */ work = malloc(lwork*sizeof(double)); for(i=0;i<=n*n-1;i++){ fscanf(file,"%f",&a[i]); } ssyev_(&jobz,&uplo,&n,a,&n,x,work,&lwork,&info); for(i=0;i<=n-1;i++){ printf("\nEigenvalue: %f\n",x[i]); printf("\nCorresponding eigenvector: "); for(j=i*n;j<=(i+1)*n-1;j++){ printf("%f ",a[j]); } printf("\n"); } printf("\n"); return 0; } /* Arguments ========= UPLO (input) CHARACTER*1 = 'U': Upper triangle of A is stored; = 'L': Lower triangle of A is stored. N (input) INTEGER The order of the matrix A. N >= 0. A (input/output) DOUBLE PRECISION array, dimension (LDA,N) On entry, the symmetric matrix A. If UPLO = 'U', the leading N-by-N upper triangular part of A contains the upper triangular part of the matrix A, and the strictly lower triangular part of A is not referenced. If UPLO = 'L', the leading N-by-N lower triangular part of A contains the lower triangular part of the matrix A, and the strictly upper triangular part of A is not referenced. On exit, if UPLO = 'U', the diagonal and first superdiagonal of A are overwritten by the corresponding elements of the tridiagonal matrix T, and the elements above the first superdiagonal, with the array TAU, represent the orthogonal matrix Q as a product of elementary reflectors; if UPLO = 'L', the diagonal and first subdiagonal of A are over- written by the corresponding elements of the tridiagonal matrix T, and the elements below the first subdiagonal, with the array TAU, represent the orthogonal matrix Q as a product of elementary reflectors. See Further Details. LDA (input) INTEGER The leading dimension of the array A. LDA >= max(1,N). X (output) DOUBLE PRECISION array, dimension (N) The diagonal elements of the tridiagonal matrix T: D(i) = A(i,i). WORK (workspace/output) DOUBLE PRECISION array, dimension (LWORK) On exit, if INFO = 0, WORK(1) returns the optimal LWORK. LWORK (input) INTEGER The dimension of the array WORK. LWORK >= 1. For optimum performance LWORK >= N*NB, where NB is the optimal blocksize. If LWORK = -1, then a workspace query is assumed; the routine only calculates the optimal size of the WORK array, returns this value as the first entry of the WORK array, and no error message related to LWORK is issued by XERBLA. INFO (output) INTEGER = 0: successful exit < 0: if INFO = -i, the i-th argument had an illegal value */