Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Jun 14, 2005, 2:02:35 AM (20 years ago)
Author:
patrick
Message:

orxonox/trunk: FINALLY! MUAHHAHAHAA, i found a function to compute the eigenvec. just wanted to go to sleep, but…

File:
1 edited

Legend:

Unmodified
Added
Removed
  • orxonox/trunk/src/lib/collision_detection/lin_alg.h

    r4627 r4628  
    55    compute the eigenpairs (eigenvalues and eigenvectors) of a real symmetric matrix "A" by the Jacobi method
    66 */
     7
     8
     9/************************************************************
     10* This subroutine computes all eigenvalues and eigenvectors *
     11* of a real symmetric square matrix A(N,N). On output, ele- *
     12* ments of A above the diagonal are destroyed. D(N) returns *
     13* the eigenvalues of matrix A. V(N,N) contains, on output,  *
     14* the eigenvectors of A by columns. THe normalization to    *
     15* unity is made by main program before printing results.    *
     16* NROT returns the number of Jacobi matrix rotations which  *
     17* were required.                                            *
     18* --------------------------------------------------------- *
     19* Ref.:"NUMERICAL RECIPES IN FORTRAN, Cambridge University  *
     20*       Press, 1986, chap. 11, pages 346-348".              *
     21*                                                           *
     22*                         C++ version by J-P Moreau, Paris. *
     23************************************************************/
     24void JacobI(float **A,int N,float *D, float **V, int *NROT) {
     25float  *B, *Z;
     26double  c,g,h,s,sm,t,tau,theta,tresh;
     27int     i,j,ip,iq;
     28
     29void *vmblock1 = NULL;
     30
     31  //allocate vectors B, Z
     32  //vmblock1 = vminit();
     33  B = (float *) calloc(100, 32);
     34  Z = (float *) calloc(100, 32);
     35
     36  for (ip=1; ip<=N; ip++) {  //initialize V to identity matrix
     37    for (iq=1; iq<=N; iq++)  V[ip][iq]=0;
     38    V[ip][ip]=1;
     39  }
     40  for (ip=1; ip<=N; ip++) {
     41    B[ip]=A[ip][ip];
     42    D[ip]=B[ip];
     43    Z[ip]=0;
     44  }
     45  *NROT=0;
     46  for (i=1; i<=50; i++) {
     47    sm=0;
     48    for (ip=1; ip<N; ip++)    //sum off-diagonal elements
     49      for (iq=ip+1; iq<=N; iq++)
     50        sm=sm+fabs(A[ip][iq]);
     51    if (sm==0) {
     52      return;       //normal return
     53    }
     54    if (i < 4)
     55      tresh=0.2*sm*sm;
     56    else
     57      tresh=0;
     58    for (ip=1; ip<N; ip++) {
     59      for (iq=ip+1; iq<=N; iq++) {
     60        g=100*fabs(A[ip][iq]);
     61// after 4 sweeps, skip the rotation if the off-diagonal element is small
     62        if ((i > 4) && (fabs(D[ip])+g == fabs(D[ip])) && (fabs(D[iq])+g == fabs(D[iq])))
     63          A[ip][iq]=0;
     64        else if (fabs(A[ip][iq]) > tresh) {
     65          h=D[iq]-D[ip];
     66          if (fabs(h)+g == fabs(h))
     67            t=A[ip][iq]/h;
     68          else {
     69            theta=0.5*h/A[ip][iq];
     70            t=1/(fabs(theta)+sqrt(1.0+theta*theta));
     71            if (theta < 0)  t=-t;
     72          }
     73          c=1.0/sqrt(1.0+t*t);
     74          s=t*c;
     75          tau=s/(1.0+c);
     76          h=t*A[ip][iq];
     77          Z[ip] -= h;
     78          Z[iq] += h;
     79          D[ip] -= h;
     80          D[iq] += h;
     81          A[ip][iq]=0;
     82          for (j=1; j<ip; j++) {
     83            g=A[j][ip];
     84            h=A[j][iq];
     85            A[j][ip] = g-s*(h+g*tau);
     86            A[j][iq] = h+s*(g-h*tau);
     87          }
     88          for (j=ip+1; j<iq; j++) {
     89            g=A[ip][j];
     90            h=A[j][iq];
     91            A[ip][j] = g-s*(h+g*tau);
     92            A[j][iq] = h+s*(g-h*tau);
     93          }
     94          for (j=iq+1; j<=N; j++) {
     95            g=A[ip][j];
     96            h=A[iq][j];
     97            A[ip][j] = g-s*(h+g*tau);
     98            A[iq][j] = h+s*(g-h*tau);
     99          }
     100          for (j=1; j<=N; j++) {
     101            g=V[j][ip];
     102            h=V[j][iq];
     103            V[j][ip] = g-s*(h+g*tau);
     104            V[j][iq] = h+s*(g-h*tau);
     105          }
     106          *NROT=*NROT+1;
     107        } //end ((i.gt.4)...else if
     108      } // main iq loop
     109    } // main ip loop
     110    for (ip=1; ip<=N; ip++) {
     111      B[ip] += Z[ip];
     112      D[ip]=B[ip];
     113      Z[ip]=0;
     114    }
     115  } //end of main i loop
     116  printf("\n 50 iterations !\n");
     117  return;  //too many iterations
     118}
     119
     120
     121
     122
    7123
    8124#include "abstract_model.h"
     
    341457//   //allocate vectors B, Z
    342458//   vmblock1 = vminit();
    343 //   //B = (REAL *) vmalloc(vmblock1, VEKTOR,  100, 0);
    344 //   //Z = (REAL *) vmalloc(vmblock1, VEKTOR,  100, 0);
     459//   //B = (float *) vmalloc(vmblock1, VEKTOR,  100, 0);
     460//   //Z = (float *) vmalloc(vmblock1, VEKTOR,  100, 0);
    345461//
    346462//    //initialize V to identity matrix
Note: See TracChangeset for help on using the changeset viewer.