MOOS 0.2375
|
00001 //$$jacobi.cpp jacobi eigenvalue analysis 00002 00003 // Copyright (C) 1991,2,3,4: R B Davies 00004 00005 00006 //#define WANT_STREAM 00007 00008 00009 #define WANT_MATH 00010 00011 #include "include.h" 00012 #include "newmatap.h" 00013 #include "precisio.h" 00014 #include "newmatrm.h" 00015 00016 #ifdef use_namespace 00017 namespace NEWMAT { 00018 #endif 00019 00020 #ifdef DO_REPORT 00021 #define REPORT { static ExeCounter ExeCount(__LINE__,18); ++ExeCount; } 00022 #else 00023 #define REPORT {} 00024 #endif 00025 00026 00027 void Jacobi(const SymmetricMatrix& X, DiagonalMatrix& D, SymmetricMatrix& A, 00028 Matrix& V, bool eivec) 00029 { 00030 Real epsilon = FloatingPointPrecision::Epsilon(); 00031 Tracer et("Jacobi"); 00032 REPORT 00033 int n = X.Nrows(); DiagonalMatrix B(n), Z(n); D.ReSize(n); A = X; 00034 if (eivec) { REPORT V.ReSize(n,n); D = 1.0; V = D; } 00035 B << A; D = B; Z = 0.0; A.Inject(Z); 00036 bool converged = false; 00037 for (int i=1; i<=50; i++) 00038 { 00039 Real sm=0.0; Real* a = A.Store(); int p = A.Storage(); 00040 while (p--) sm += fabs(*a++); // have previously zeroed diags 00041 if (sm==0.0) { REPORT converged = true; break; } 00042 Real tresh = (i<4) ? 0.2 * sm / square(n) : 0.0; a = A.Store(); 00043 for (p = 0; p < n; p++) 00044 { 00045 Real* ap1 = a + (p*(p+1))/2; 00046 Real& zp = Z.element(p); Real& dp = D.element(p); 00047 for (int q = p+1; q < n; q++) 00048 { 00049 Real* ap = ap1; Real* aq = a + (q*(q+1))/2; 00050 Real& zq = Z.element(q); Real& dq = D.element(q); 00051 Real& apq = A.element(q,p); 00052 Real g = 100 * fabs(apq); Real adp = fabs(dp); Real adq = fabs(dq); 00053 00054 if (i>4 && g < epsilon*adp && g < epsilon*adq) { REPORT apq = 0.0; } 00055 else if (fabs(apq) > tresh) 00056 { 00057 REPORT 00058 Real t; Real h = dq - dp; Real ah = fabs(h); 00059 if (g < epsilon*ah) { REPORT t = apq / h; } 00060 else 00061 { 00062 REPORT 00063 Real theta = 0.5 * h / apq; 00064 t = 1.0 / ( fabs(theta) + sqrt(1.0 + square(theta)) ); 00065 if (theta<0.0) { REPORT t = -t; } 00066 } 00067 Real c = 1.0 / sqrt(1.0 + square(t)); Real s = t * c; 00068 Real tau = s / (1.0 + c); h = t * apq; 00069 zp -= h; zq += h; dp -= h; dq += h; apq = 0.0; 00070 int j = p; 00071 while (j--) 00072 { 00073 g = *ap; h = *aq; 00074 *ap++ = g-s*(h+g*tau); *aq++ = h+s*(g-h*tau); 00075 } 00076 int ip = p+1; j = q-ip; ap += ip++; aq++; 00077 while (j--) 00078 { 00079 g = *ap; h = *aq; 00080 *ap = g-s*(h+g*tau); *aq++ = h+s*(g-h*tau); 00081 ap += ip++; 00082 } 00083 if (q < n-1) // last loop is non-empty 00084 { 00085 int iq = q+1; j = n-iq; ap += ip++; aq += iq++; 00086 for (;;) 00087 { 00088 g = *ap; h = *aq; 00089 *ap = g-s*(h+g*tau); *aq = h+s*(g-h*tau); 00090 if (!(--j)) break; 00091 ap += ip++; aq += iq++; 00092 } 00093 } 00094 if (eivec) 00095 { 00096 REPORT 00097 RectMatrixCol VP(V,p); RectMatrixCol VQ(V,q); 00098 Rotate(VP, VQ, tau, s); 00099 } 00100 } 00101 } 00102 } 00103 B = B + Z; D = B; Z = 0.0; 00104 } 00105 if (!converged) Throw(ConvergenceException(X)); 00106 if (eivec) SortSV(D, V, true); 00107 else SortAscending(D); 00108 } 00109 00110 void Jacobi(const SymmetricMatrix& X, DiagonalMatrix& D) 00111 { REPORT SymmetricMatrix A; Matrix V; Jacobi(X,D,A,V,false); } 00112 00113 void Jacobi(const SymmetricMatrix& X, DiagonalMatrix& D, SymmetricMatrix& A) 00114 { REPORT Matrix V; Jacobi(X,D,A,V,false); } 00115 00116 void Jacobi(const SymmetricMatrix& X, DiagonalMatrix& D, Matrix& V) 00117 { REPORT SymmetricMatrix A; Jacobi(X,D,A,V,true); } 00118 00119 00120 #ifdef use_namespace 00121 } 00122 #endif 00123