home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-385-Vol-1of3.iso / n / newmat06.zip / JACOBI.CXX < prev    next >
C/C++ Source or Header  |  1992-11-29  |  3KB  |  94 lines

  1. //$$jacobi.cxx                           jacobi eigenvalue analysis
  2.  
  3. // Copyright (C) 1991,2: R B Davies
  4.  
  5. #define WANT_MATH
  6.  
  7. #include "include.h"
  8. #include "newmat.h"
  9. #include "precisio.h"
  10. #include "newmatrm.h"
  11.  
  12.  
  13.  
  14. void Jacobi(const SymmetricMatrix& X, DiagonalMatrix& D, SymmetricMatrix& A,
  15.    Matrix& V, Boolean eivec)
  16. {
  17.    Tracer et("Jacobi");
  18.    int n = X.Nrows(); DiagonalMatrix B(n), Z(n); D.ReDimension(n); A = X;
  19.    if (eivec) { V.ReDimension(n,n); D = 1.0; V = D; }
  20.    B << A; D = B; Z = 0.0; A.Inject(Z);
  21.    for (int i=1; i<=50; i++)
  22.    {
  23.       Real sm=0.0; Real* a = A.Store(); int p = A.Storage();
  24.       while (p--) sm += fabs(*a++);            // have previously zeroed diags
  25.       if (sm==0.0) return;
  26.       Real tresh = (i<4) ? 0.2 * sm / square(n) : 0.0; a = A.Store();
  27.       for (p = 0; p < n; p++)
  28.       {
  29.      Real* ap1 = a + (p*(p+1))/2;
  30.          Real& zp = Z.element(p); Real& dp = D.element(p);
  31.      for (int q = p+1; q < n; q++)
  32.      {
  33.         Real* ap = ap1; Real* aq = a + (q*(q+1))/2;
  34.             Real& zq = Z.element(q); Real& dq = D.element(q);
  35.             Real& apq = A.element(q,p);
  36.             Real g = 100 * fabs(apq); Real adp = fabs(dp); Real adq = fabs(dq);
  37.         if (i>4 && adp+g==adp && adq+g==adq) apq = 0.0;
  38.         else if (fabs(apq) > tresh)
  39.         {
  40.            Real t; Real h = dq - dp; Real ah = fabs(h);
  41.            if (ah+g==ah) t = apq / h;
  42.            else
  43.            {
  44.           Real theta = 0.5 * h / apq;
  45.           t = 1.0 / ( fabs(theta) + sqrt(1.0 + square(theta)) );
  46.           if (theta<0.0) t = -t;
  47.            }
  48.            Real c = 1.0 / sqrt(1.0 + square(t)); Real s = t * c;
  49.            Real tau = s / (1.0 + c); h = t * apq;
  50.                zp -= h; zq += h; dp -= h; dq += h; apq = 0.0;
  51.            int j = p;
  52.            while (j--)
  53.            {
  54.           g = *ap; h = *aq;
  55.           *ap++ = g-s*(h+g*tau); *aq++ = h+s*(g-h*tau);
  56.            }
  57.            int ip = p+1; j = q-ip; ap += ip++; aq++;
  58.            while (j--)
  59.            {
  60.           g = *ap; h = *aq;
  61.                   *ap = g-s*(h+g*tau); *aq++ = h+s*(g-h*tau);
  62.           ap += ip++;
  63.            }
  64.            int iq = q+1; j = n-iq; ap += ip++; aq += iq++;
  65.            while (j--)
  66.            {
  67.           g = *ap; h = *aq;
  68.           *ap = g-s*(h+g*tau); *aq = h+s*(g-h*tau);
  69.           ap += ip++; aq += iq++;
  70.            }
  71.            if (eivec)
  72.            {
  73.                   RectMatrixCol VP(V,p); RectMatrixCol VQ(V,q);
  74.                   Rotate(VP, VQ, tau, s);
  75.            }
  76.         }
  77.      }
  78.       }
  79.       B = B + Z; D = B; Z = 0.0;
  80.    }
  81.    Throw(ConvergenceException(X));
  82. }
  83.  
  84. void Jacobi(const SymmetricMatrix& X, DiagonalMatrix& D)
  85. { SymmetricMatrix A; Matrix V; Jacobi(X,D,A,V,FALSE); }
  86.  
  87. void Jacobi(const SymmetricMatrix& X, DiagonalMatrix& D, SymmetricMatrix& A)
  88. { Matrix V; Jacobi(X,D,A,V,FALSE); }
  89.  
  90. void Jacobi(const SymmetricMatrix& X, DiagonalMatrix& D, Matrix& V)
  91. { SymmetricMatrix A; Jacobi(X,D,A,V,TRUE); }
  92.  
  93.  
  94.