[4565] | 1 | //$$jacobi.cpp jacobi eigenvalue analysis |
---|
| 2 | |
---|
| 3 | // Copyright (C) 1991,2,3,4: R B Davies |
---|
| 4 | |
---|
| 5 | |
---|
| 6 | //#define WANT_STREAM |
---|
| 7 | |
---|
| 8 | |
---|
| 9 | #define WANT_MATH |
---|
| 10 | |
---|
| 11 | #include "include.h" |
---|
| 12 | #include "newmatap.h" |
---|
| 13 | #include "precisio.h" |
---|
| 14 | #include "newmatrm.h" |
---|
| 15 | |
---|
| 16 | #ifdef use_namespace |
---|
| 17 | namespace NEWMAT { |
---|
| 18 | #endif |
---|
| 19 | |
---|
| 20 | #ifdef DO_REPORT |
---|
| 21 | #define REPORT { static ExeCounter ExeCount(__LINE__,18); ++ExeCount; } |
---|
| 22 | #else |
---|
| 23 | #define REPORT {} |
---|
| 24 | #endif |
---|
| 25 | |
---|
| 26 | |
---|
| 27 | void Jacobi(const SymmetricMatrix& X, DiagonalMatrix& D, SymmetricMatrix& A, |
---|
| 28 | Matrix& V, bool eivec) |
---|
| 29 | { |
---|
| 30 | Real epsilon = FloatingPointPrecision::Epsilon(); |
---|
| 31 | Tracer et("Jacobi"); |
---|
| 32 | REPORT |
---|
| 33 | int n = X.Nrows(); DiagonalMatrix B(n), Z(n); D.ReSize(n); A = X; |
---|
| 34 | if (eivec) { REPORT V.ReSize(n,n); D = 1.0; V = D; } |
---|
| 35 | B << A; D = B; Z = 0.0; A.Inject(Z); |
---|
| 36 | bool converged = false; |
---|
| 37 | for (int i=1; i<=50; i++) |
---|
| 38 | { |
---|
| 39 | Real sm=0.0; Real* a = A.Store(); int p = A.Storage(); |
---|
| 40 | while (p--) sm += fabs(*a++); // have previously zeroed diags |
---|
| 41 | if (sm==0.0) { REPORT converged = true; break; } |
---|
| 42 | Real tresh = (i<4) ? 0.2 * sm / square(n) : 0.0; a = A.Store(); |
---|
| 43 | for (p = 0; p < n; p++) |
---|
| 44 | { |
---|
| 45 | Real* ap1 = a + (p*(p+1))/2; |
---|
| 46 | Real& zp = Z.element(p); Real& dp = D.element(p); |
---|
| 47 | for (int q = p+1; q < n; q++) |
---|
| 48 | { |
---|
| 49 | Real* ap = ap1; Real* aq = a + (q*(q+1))/2; |
---|
| 50 | Real& zq = Z.element(q); Real& dq = D.element(q); |
---|
| 51 | Real& apq = A.element(q,p); |
---|
| 52 | Real g = 100 * fabs(apq); Real adp = fabs(dp); Real adq = fabs(dq); |
---|
| 53 | |
---|
| 54 | if (i>4 && g < epsilon*adp && g < epsilon*adq) { REPORT apq = 0.0; } |
---|
| 55 | else if (fabs(apq) > tresh) |
---|
| 56 | { |
---|
| 57 | REPORT |
---|
| 58 | Real t; Real h = dq - dp; Real ah = fabs(h); |
---|
| 59 | if (g < epsilon*ah) { REPORT t = apq / h; } |
---|
| 60 | else |
---|
| 61 | { |
---|
| 62 | REPORT |
---|
| 63 | Real theta = 0.5 * h / apq; |
---|
| 64 | t = 1.0 / ( fabs(theta) + sqrt(1.0 + square(theta)) ); |
---|
| 65 | if (theta<0.0) { REPORT t = -t; } |
---|
| 66 | } |
---|
| 67 | Real c = 1.0 / sqrt(1.0 + square(t)); Real s = t * c; |
---|
| 68 | Real tau = s / (1.0 + c); h = t * apq; |
---|
| 69 | zp -= h; zq += h; dp -= h; dq += h; apq = 0.0; |
---|
| 70 | int j = p; |
---|
| 71 | while (j--) |
---|
| 72 | { |
---|
| 73 | g = *ap; h = *aq; |
---|
| 74 | *ap++ = g-s*(h+g*tau); *aq++ = h+s*(g-h*tau); |
---|
| 75 | } |
---|
| 76 | int ip = p+1; j = q-ip; ap += ip++; aq++; |
---|
| 77 | while (j--) |
---|
| 78 | { |
---|
| 79 | g = *ap; h = *aq; |
---|
| 80 | *ap = g-s*(h+g*tau); *aq++ = h+s*(g-h*tau); |
---|
| 81 | ap += ip++; |
---|
| 82 | } |
---|
| 83 | if (q < n-1) // last loop is non-empty |
---|
| 84 | { |
---|
| 85 | int iq = q+1; j = n-iq; ap += ip++; aq += iq++; |
---|
| 86 | for (;;) |
---|
| 87 | { |
---|
| 88 | g = *ap; h = *aq; |
---|
| 89 | *ap = g-s*(h+g*tau); *aq = h+s*(g-h*tau); |
---|
| 90 | if (!(--j)) break; |
---|
| 91 | ap += ip++; aq += iq++; |
---|
| 92 | } |
---|
| 93 | } |
---|
| 94 | if (eivec) |
---|
| 95 | { |
---|
| 96 | REPORT |
---|
| 97 | RectMatrixCol VP(V,p); RectMatrixCol VQ(V,q); |
---|
| 98 | Rotate(VP, VQ, tau, s); |
---|
| 99 | } |
---|
| 100 | } |
---|
| 101 | } |
---|
| 102 | } |
---|
| 103 | B = B + Z; D = B; Z = 0.0; |
---|
| 104 | } |
---|
| 105 | if (!converged) Throw(ConvergenceException(X)); |
---|
| 106 | if (eivec) SortSV(D, V, true); |
---|
| 107 | else SortAscending(D); |
---|
| 108 | } |
---|
| 109 | |
---|
| 110 | void Jacobi(const SymmetricMatrix& X, DiagonalMatrix& D) |
---|
| 111 | { REPORT SymmetricMatrix A; Matrix V; Jacobi(X,D,A,V,false); } |
---|
| 112 | |
---|
| 113 | void Jacobi(const SymmetricMatrix& X, DiagonalMatrix& D, SymmetricMatrix& A) |
---|
| 114 | { REPORT Matrix V; Jacobi(X,D,A,V,false); } |
---|
| 115 | |
---|
| 116 | void Jacobi(const SymmetricMatrix& X, DiagonalMatrix& D, Matrix& V) |
---|
| 117 | { REPORT SymmetricMatrix A; Jacobi(X,D,A,V,true); } |
---|
| 118 | |
---|
| 119 | |
---|
| 120 | #ifdef use_namespace |
---|
| 121 | } |
---|
| 122 | #endif |
---|
| 123 | |
---|