/* This is a sample program to show how to use a function called jacobi to find the eigen values and eigen vectors of a n by n symmetric matrix. You enter the dimension n and a symmetric matrix will be generated and its eigen values and vectors will be found. To verify the result, it is further shown in the main program that the eigen vector matrix, a orthogonal matrix, indeed converts the original matrix to a diagonal matrix (with all eigen values on the main diagonal). You can enter some value less than 10 as an example to see this diagonalization. You need the Jacobi function included in this program to do KLT for feature election needed in project 7. When n=256, it takes less than 10 minutes to do 9 iterations to generate results with more than enough accuracy. Note that in order to run the function Jacobi properly, you need to use "matrix" and "vector" functions (available in this program) to allocate memery space for all matrices and vectors that are used in the Jacobi function. */ #include #include prtvtr(v,n) /* print an n by 1 vector */ float v[]; int n; { int i; for (i=0; i 4 && g == 0.0) goto l1; if (fabs(g1) <= tr) goto l2; h=d[i-1]-d[j-1]; if (2*fabs(g1) < fabs(h)){ t=2*g1/h; c=1.0/sqrt(1+t*t); s=t*c; } else { t=0.5*h/g1; c=fabs(t)/sqrt(1+t*t); if (t == 0) s=1.0; else s=c/t; } c=sqrt((1.0+c)/2); s=0.5*s/c; h=g1*s/c; ta=s/(1+c); z[i-1]=z[i-1]+h; z[j-1]=z[j-1]-h; d[i-1]=d[i-1]+h; d[j-1]=d[j-1]-h; ic=i; it=j; for (k=1; k<=n; k++) { g=a[ic]; h=a[it]; a[ic]=g+s*(h-g*ta); a[it]=h-s*(g+h*ta); if (k < i) ic+=n-k; else ic+=1; if (k < j) it+=n-k; else it+=1; } for (k=0; k