Blender V2.61 - r43446

jama_eig.h

Go to the documentation of this file.
00001 
00004 #ifndef JAMA_EIG_H
00005 #define JAMA_EIG_H
00006 
00007 
00008 #include "tnt_array1d.h"
00009 #include "tnt_array2d.h"
00010 #include "tnt_math_utils.h"
00011 
00012 #include <algorithm>
00013 // for min(), max() below
00014 
00015 #include <cmath>
00016 // for fabs() below
00017 
00018 using namespace TNT;
00019 using namespace std;
00020 
00021 // NT debugging
00022 //static int gEigenDebug=0;
00023 //if(gEigenDebug) std::cerr<<"n="<<n<<" m="<<m<<" l="<<l<<"\n"; 
00024 // m has to be smaller l! in line 262
00025 // gcc can get confused with abs calls, replaced by fabs
00026 
00027 namespace JAMA
00028 {
00029 
00080 template <class Real>
00081 class Eigenvalue
00082 {
00083 
00084 
00086     int n;
00087 
00088    int issymmetric; /* boolean*/
00089 
00092    TNT::Array1D<Real> d;         /* real part */
00093    TNT::Array1D<Real> e;         /* img part */
00094 
00096     TNT::Array2D<Real> V;
00097 
00101    TNT::Array2D<Real> H;
00102    
00103 
00107    TNT::Array1D<Real> ort;
00108 
00109 
00110    // Symmetric Householder reduction to tridiagonal form.
00111 
00112    void tred2() {
00113 
00114    //  This is derived from the Algol procedures tred2 by
00115    //  Bowdler, Martin, Reinsch, and Wilkinson, Handbook for
00116    //  Auto. Comp., Vol.ii-Linear Algebra, and the corresponding
00117    //  Fortran subroutine in EISPACK.
00118 
00119       for (int j = 0; j < n; j++) {
00120          d[j] = V[n-1][j];
00121       }
00122 
00123       // Householder reduction to tridiagonal form.
00124    
00125       for (int i = n-1; i > 0; i--) {
00126    
00127          // Scale to avoid under/overflow.
00128    
00129          Real scale = 0.0;
00130          Real h = 0.0;
00131          for (int k = 0; k < i; k++) {
00132             scale = scale + fabs(d[k]);
00133          }
00134          if (scale == 0.0) {
00135             e[i] = d[i-1];
00136             for (int j = 0; j < i; j++) {
00137                d[j] = V[i-1][j];
00138                V[i][j] = 0.0;
00139                V[j][i] = 0.0;
00140             }
00141          } else {
00142    
00143             // Generate Householder vector.
00144    
00145             for (int k = 0; k < i; k++) {
00146                d[k] /= scale;
00147                h += d[k] * d[k];
00148             }
00149             Real f = d[i-1];
00150             Real g = sqrt(h);
00151             if (f > 0) {
00152                g = -g;
00153             }
00154             e[i] = scale * g;
00155             h = h - f * g;
00156             d[i-1] = f - g;
00157             for (int j = 0; j < i; j++) {
00158                e[j] = 0.0;
00159             }
00160    
00161             // Apply similarity transformation to remaining columns.
00162    
00163             for (int j = 0; j < i; j++) {
00164                f = d[j];
00165                V[j][i] = f;
00166                g = e[j] + V[j][j] * f;
00167                for (int k = j+1; k <= i-1; k++) {
00168                   g += V[k][j] * d[k];
00169                   e[k] += V[k][j] * f;
00170                }
00171                e[j] = g;
00172             }
00173             f = 0.0;
00174             for (int j = 0; j < i; j++) {
00175                e[j] /= h;
00176                f += e[j] * d[j];
00177             }
00178             Real hh = f / (h + h);
00179             for (int j = 0; j < i; j++) {
00180                e[j] -= hh * d[j];
00181             }
00182             for (int j = 0; j < i; j++) {
00183                f = d[j];
00184                g = e[j];
00185                for (int k = j; k <= i-1; k++) {
00186                   V[k][j] -= (f * e[k] + g * d[k]);
00187                }
00188                d[j] = V[i-1][j];
00189                V[i][j] = 0.0;
00190             }
00191          }
00192          d[i] = h;
00193       }
00194    
00195       // Accumulate transformations.
00196    
00197       for (int i = 0; i < n-1; i++) {
00198          V[n-1][i] = V[i][i];
00199          V[i][i] = 1.0;
00200          Real h = d[i+1];
00201          if (h != 0.0) {
00202             for (int k = 0; k <= i; k++) {
00203                d[k] = V[k][i+1] / h;
00204             }
00205             for (int j = 0; j <= i; j++) {
00206                Real g = 0.0;
00207                for (int k = 0; k <= i; k++) {
00208                   g += V[k][i+1] * V[k][j];
00209                }
00210                for (int k = 0; k <= i; k++) {
00211                   V[k][j] -= g * d[k];
00212                }
00213             }
00214          }
00215          for (int k = 0; k <= i; k++) {
00216             V[k][i+1] = 0.0;
00217          }
00218       }
00219       for (int j = 0; j < n; j++) {
00220          d[j] = V[n-1][j];
00221          V[n-1][j] = 0.0;
00222       }
00223       V[n-1][n-1] = 1.0;
00224       e[0] = 0.0;
00225    } 
00226 
00227    // Symmetric tridiagonal QL algorithm.
00228    
00229    void tql2 () {
00230 
00231    //  This is derived from the Algol procedures tql2, by
00232    //  Bowdler, Martin, Reinsch, and Wilkinson, Handbook for
00233    //  Auto. Comp., Vol.ii-Linear Algebra, and the corresponding
00234    //  Fortran subroutine in EISPACK.
00235    
00236       for (int i = 1; i < n; i++) {
00237          e[i-1] = e[i];
00238       }
00239       e[n-1] = 0.0;
00240    
00241       Real f = 0.0;
00242       Real tst1 = 0.0;
00243       Real eps = pow(2.0,-52.0);
00244       for (int l = 0; l < n; l++) {
00245 
00246          // Find small subdiagonal element
00247    
00248          tst1 = max(tst1,fabs(d[l]) + fabs(e[l]));
00249          int m = l;
00250 
00251         // Original while-loop from Java code
00252          while (m < n) {
00253             if (fabs(e[m]) <= eps*tst1) {
00254                break;
00255             }
00256             m++;
00257          }
00258 
00259    
00260          // If m == l, d[l] is an eigenvalue,
00261          // otherwise, iterate.
00262    
00263          if (m > l) {
00264             int iter = 0;
00265             do {
00266                iter = iter + 1;  // (Could check iteration count here.)
00267    
00268                // Compute implicit shift
00269 
00270                Real g = d[l];
00271                Real p = (d[l+1] - g) / (2.0 * e[l]);
00272                Real r = hypot(p,1.0);
00273                if (p < 0) {
00274                   r = -r;
00275                }
00276                d[l] = e[l] / (p + r);
00277                d[l+1] = e[l] * (p + r);
00278                Real dl1 = d[l+1];
00279                Real h = g - d[l];
00280                for (int i = l+2; i < n; i++) {
00281                   d[i] -= h;
00282                }
00283                f = f + h;
00284    
00285                // Implicit QL transformation.
00286    
00287                p = d[m];
00288                Real c = 1.0;
00289                Real c2 = c;
00290                Real c3 = c;
00291                Real el1 = e[l+1];
00292                Real s = 0.0;
00293                Real s2 = 0.0;
00294                for (int i = m-1; i >= l; i--) {
00295                   c3 = c2;
00296                   c2 = c;
00297                   s2 = s;
00298                   g = c * e[i];
00299                   h = c * p;
00300                   r = hypot(p,e[i]);
00301                   e[i+1] = s * r;
00302                   s = e[i] / r;
00303                   c = p / r;
00304                   p = c * d[i] - s * g;
00305                   d[i+1] = h + s * (c * g + s * d[i]);
00306    
00307                   // Accumulate transformation.
00308    
00309                   for (int k = 0; k < n; k++) {
00310                      h = V[k][i+1];
00311                      V[k][i+1] = s * V[k][i] + c * h;
00312                      V[k][i] = c * V[k][i] - s * h;
00313                   }
00314                }
00315                p = -s * s2 * c3 * el1 * e[l] / dl1;
00316                e[l] = s * p;
00317                d[l] = c * p;
00318    
00319                // Check for convergence.
00320    
00321             } while (fabs(e[l]) > eps*tst1);
00322          }
00323          d[l] = d[l] + f;
00324          e[l] = 0.0;
00325       }
00326      
00327       // Sort eigenvalues and corresponding vectors.
00328    
00329       for (int i = 0; i < n-1; i++) {
00330          int k = i;
00331          Real p = d[i];
00332          for (int j = i+1; j < n; j++) {
00333             if (d[j] < p) {
00334                k = j;
00335                p = d[j];
00336             }
00337          }
00338          if (k != i) {
00339             d[k] = d[i];
00340             d[i] = p;
00341             for (int j = 0; j < n; j++) {
00342                p = V[j][i];
00343                V[j][i] = V[j][k];
00344                V[j][k] = p;
00345             }
00346          }
00347       }
00348    }
00349 
00350    // Nonsymmetric reduction to Hessenberg form.
00351 
00352    void orthes () {
00353    
00354       //  This is derived from the Algol procedures orthes and ortran,
00355       //  by Martin and Wilkinson, Handbook for Auto. Comp.,
00356       //  Vol.ii-Linear Algebra, and the corresponding
00357       //  Fortran subroutines in EISPACK.
00358    
00359       int low = 0;
00360       int high = n-1;
00361    
00362       for (int m = low+1; m <= high-1; m++) {
00363    
00364          // Scale column.
00365    
00366          Real scale = 0.0;
00367          for (int i = m; i <= high; i++) {
00368             scale = scale + fabs(H[i][m-1]);
00369          }
00370          if (scale != 0.0) {
00371    
00372             // Compute Householder transformation.
00373    
00374             Real h = 0.0;
00375             for (int i = high; i >= m; i--) {
00376                ort[i] = H[i][m-1]/scale;
00377                h += ort[i] * ort[i];
00378             }
00379             Real g = sqrt(h);
00380             if (ort[m] > 0) {
00381                g = -g;
00382             }
00383             h = h - ort[m] * g;
00384             ort[m] = ort[m] - g;
00385    
00386             // Apply Householder similarity transformation
00387             // H = (I-u*u'/h)*H*(I-u*u')/h)
00388    
00389             for (int j = m; j < n; j++) {
00390                Real f = 0.0;
00391                for (int i = high; i >= m; i--) {
00392                   f += ort[i]*H[i][j];
00393                }
00394                f = f/h;
00395                for (int i = m; i <= high; i++) {
00396                   H[i][j] -= f*ort[i];
00397                }
00398            }
00399    
00400            for (int i = 0; i <= high; i++) {
00401                Real f = 0.0;
00402                for (int j = high; j >= m; j--) {
00403                   f += ort[j]*H[i][j];
00404                }
00405                f = f/h;
00406                for (int j = m; j <= high; j++) {
00407                   H[i][j] -= f*ort[j];
00408                }
00409             }
00410             ort[m] = scale*ort[m];
00411             H[m][m-1] = scale*g;
00412          }
00413       }
00414    
00415       // Accumulate transformations (Algol's ortran).
00416 
00417       for (int i = 0; i < n; i++) {
00418          for (int j = 0; j < n; j++) {
00419             V[i][j] = (i == j ? 1.0 : 0.0);
00420          }
00421       }
00422 
00423       for (int m = high-1; m >= low+1; m--) {
00424          if (H[m][m-1] != 0.0) {
00425             for (int i = m+1; i <= high; i++) {
00426                ort[i] = H[i][m-1];
00427             }
00428             for (int j = m; j <= high; j++) {
00429                Real g = 0.0;
00430                for (int i = m; i <= high; i++) {
00431                   g += ort[i] * V[i][j];
00432                }
00433                // Double division avoids possible underflow
00434                g = (g / ort[m]) / H[m][m-1];
00435                for (int i = m; i <= high; i++) {
00436                   V[i][j] += g * ort[i];
00437                }
00438             }
00439          }
00440       }
00441    }
00442 
00443 
00444    // Complex scalar division.
00445 
00446    Real cdivr, cdivi;
00447    void cdiv(Real xr, Real xi, Real yr, Real yi) {
00448       Real r,d;
00449       if (fabs(yr) > fabs(yi)) {
00450          r = yi/yr;
00451          d = yr + r*yi;
00452          cdivr = (xr + r*xi)/d;
00453          cdivi = (xi - r*xr)/d;
00454       } else {
00455          r = yr/yi;
00456          d = yi + r*yr;
00457          cdivr = (r*xr + xi)/d;
00458          cdivi = (r*xi - xr)/d;
00459       }
00460    }
00461 
00462 
00463    // Nonsymmetric reduction from Hessenberg to real Schur form.
00464 
00465    void hqr2 () {
00466    
00467       //  This is derived from the Algol procedure hqr2,
00468       //  by Martin and Wilkinson, Handbook for Auto. Comp.,
00469       //  Vol.ii-Linear Algebra, and the corresponding
00470       //  Fortran subroutine in EISPACK.
00471    
00472       // Initialize
00473    
00474       int nn = this->n;
00475       int n = nn-1;
00476       int low = 0;
00477       int high = nn-1;
00478       Real eps = pow(2.0,-52.0);
00479       Real exshift = 0.0;
00480       Real p=0,q=0,r=0,s=0,z=0,t,w,x,y;
00481    
00482       // Store roots isolated by balanc and compute matrix norm
00483    
00484       Real norm = 0.0;
00485       for (int i = 0; i < nn; i++) {
00486          if ((i < low) || (i > high)) {
00487             d[i] = H[i][i];
00488             e[i] = 0.0;
00489          }
00490          for (int j = max(i-1,0); j < nn; j++) {
00491             norm = norm + fabs(H[i][j]);
00492          }
00493       }
00494    
00495       // Outer loop over eigenvalue index
00496    
00497       int iter = 0;
00498         int totIter = 0;
00499       while (n >= low) {
00500 
00501             // NT limit no. of iterations
00502             totIter++;
00503             if(totIter>100) {
00504                 //if(totIter>15) std::cout<<"!!!!iter ABORT !!!!!!! "<<totIter<<"\n"; 
00505                 // NT hack/fix, return large eigenvalues
00506                 for (int i = 0; i < nn; i++) {
00507                     d[i] = 10000.;
00508                     e[i] = 10000.;
00509                 }
00510                 return;
00511             }
00512    
00513          // Look for single small sub-diagonal element
00514    
00515          int l = n;
00516          while (l > low) {
00517             s = fabs(H[l-1][l-1]) + fabs(H[l][l]);
00518             if (s == 0.0) {
00519                s = norm;
00520             }
00521             if (fabs(H[l][l-1]) < eps * s) {
00522                break;
00523             }
00524             l--;
00525          }
00526        
00527          // Check for convergence
00528          // One root found
00529    
00530          if (l == n) {
00531             H[n][n] = H[n][n] + exshift;
00532             d[n] = H[n][n];
00533             e[n] = 0.0;
00534             n--;
00535             iter = 0;
00536    
00537          // Two roots found
00538    
00539          } else if (l == n-1) {
00540             w = H[n][n-1] * H[n-1][n];
00541             p = (H[n-1][n-1] - H[n][n]) / 2.0;
00542             q = p * p + w;
00543             z = sqrt(fabs(q));
00544             H[n][n] = H[n][n] + exshift;
00545             H[n-1][n-1] = H[n-1][n-1] + exshift;
00546             x = H[n][n];
00547    
00548             // Real pair
00549    
00550             if (q >= 0) {
00551                if (p >= 0) {
00552                   z = p + z;
00553                } else {
00554                   z = p - z;
00555                }
00556                d[n-1] = x + z;
00557                d[n] = d[n-1];
00558                if (z != 0.0) {
00559                   d[n] = x - w / z;
00560                }
00561                e[n-1] = 0.0;
00562                e[n] = 0.0;
00563                x = H[n][n-1];
00564                s = fabs(x) + fabs(z);
00565                p = x / s;
00566                q = z / s;
00567                r = sqrt(p * p+q * q);
00568                p = p / r;
00569                q = q / r;
00570    
00571                // Row modification
00572    
00573                for (int j = n-1; j < nn; j++) {
00574                   z = H[n-1][j];
00575                   H[n-1][j] = q * z + p * H[n][j];
00576                   H[n][j] = q * H[n][j] - p * z;
00577                }
00578    
00579                // Column modification
00580    
00581                for (int i = 0; i <= n; i++) {
00582                   z = H[i][n-1];
00583                   H[i][n-1] = q * z + p * H[i][n];
00584                   H[i][n] = q * H[i][n] - p * z;
00585                }
00586    
00587                // Accumulate transformations
00588    
00589                for (int i = low; i <= high; i++) {
00590                   z = V[i][n-1];
00591                   V[i][n-1] = q * z + p * V[i][n];
00592                   V[i][n] = q * V[i][n] - p * z;
00593                }
00594    
00595             // Complex pair
00596    
00597             } else {
00598                d[n-1] = x + p;
00599                d[n] = x + p;
00600                e[n-1] = z;
00601                e[n] = -z;
00602             }
00603             n = n - 2;
00604             iter = 0;
00605    
00606          // No convergence yet
00607    
00608          } else {
00609    
00610             // Form shift
00611    
00612             x = H[n][n];
00613             y = 0.0;
00614             w = 0.0;
00615             if (l < n) {
00616                y = H[n-1][n-1];
00617                w = H[n][n-1] * H[n-1][n];
00618             }
00619    
00620             // Wilkinson's original ad hoc shift
00621    
00622             if (iter == 10) {
00623                exshift += x;
00624                for (int i = low; i <= n; i++) {
00625                   H[i][i] -= x;
00626                }
00627                s = fabs(H[n][n-1]) + fabs(H[n-1][n-2]);
00628                x = y = 0.75 * s;
00629                w = -0.4375 * s * s;
00630             }
00631 
00632             // MATLAB's new ad hoc shift
00633 
00634             if (iter == 30) {
00635                 s = (y - x) / 2.0;
00636                 s = s * s + w;
00637                 if (s > 0) {
00638                     s = sqrt(s);
00639                     if (y < x) {
00640                        s = -s;
00641                     }
00642                     s = x - w / ((y - x) / 2.0 + s);
00643                     for (int i = low; i <= n; i++) {
00644                        H[i][i] -= s;
00645                     }
00646                     exshift += s;
00647                     x = y = w = 0.964;
00648                 }
00649             }
00650    
00651             iter = iter + 1;   // (Could check iteration count here.)
00652    
00653             // Look for two consecutive small sub-diagonal elements
00654    
00655             int m = n-2;
00656             while (m >= l) {
00657                z = H[m][m];
00658                r = x - z;
00659                s = y - z;
00660                p = (r * s - w) / H[m+1][m] + H[m][m+1];
00661                q = H[m+1][m+1] - z - r - s;
00662                r = H[m+2][m+1];
00663                s = fabs(p) + fabs(q) + fabs(r);
00664                p = p / s;
00665                q = q / s;
00666                r = r / s;
00667                if (m == l) {
00668                   break;
00669                }
00670                if (fabs(H[m][m-1]) * (fabs(q) + fabs(r)) <
00671                   eps * (fabs(p) * (fabs(H[m-1][m-1]) + fabs(z) +
00672                   fabs(H[m+1][m+1])))) {
00673                      break;
00674                }
00675                m--;
00676             }
00677    
00678             for (int i = m+2; i <= n; i++) {
00679                H[i][i-2] = 0.0;
00680                if (i > m+2) {
00681                   H[i][i-3] = 0.0;
00682                }
00683             }
00684    
00685             // Double QR step involving rows l:n and columns m:n
00686    
00687             for (int k = m; k <= n-1; k++) {
00688                int notlast = (k != n-1);
00689                if (k != m) {
00690                   p = H[k][k-1];
00691                   q = H[k+1][k-1];
00692                   r = (notlast ? H[k+2][k-1] : 0.0);
00693                   x = fabs(p) + fabs(q) + fabs(r);
00694                   if (x != 0.0) {
00695                      p = p / x;
00696                      q = q / x;
00697                      r = r / x;
00698                   }
00699                }
00700                if (x == 0.0) {
00701                   break;
00702                }
00703                s = sqrt(p * p + q * q + r * r);
00704                if (p < 0) {
00705                   s = -s;
00706                }
00707                if (s != 0) {
00708                   if (k != m) {
00709                      H[k][k-1] = -s * x;
00710                   } else if (l != m) {
00711                      H[k][k-1] = -H[k][k-1];
00712                   }
00713                   p = p + s;
00714                   x = p / s;
00715                   y = q / s;
00716                   z = r / s;
00717                   q = q / p;
00718                   r = r / p;
00719    
00720                   // Row modification
00721    
00722                   for (int j = k; j < nn; j++) {
00723                      p = H[k][j] + q * H[k+1][j];
00724                      if (notlast) {
00725                         p = p + r * H[k+2][j];
00726                         H[k+2][j] = H[k+2][j] - p * z;
00727                      }
00728                      H[k][j] = H[k][j] - p * x;
00729                      H[k+1][j] = H[k+1][j] - p * y;
00730                   }
00731    
00732                   // Column modification
00733    
00734                   for (int i = 0; i <= min(n,k+3); i++) {
00735                      p = x * H[i][k] + y * H[i][k+1];
00736                      if (notlast) {
00737                         p = p + z * H[i][k+2];
00738                         H[i][k+2] = H[i][k+2] - p * r;
00739                      }
00740                      H[i][k] = H[i][k] - p;
00741                      H[i][k+1] = H[i][k+1] - p * q;
00742                   }
00743    
00744                   // Accumulate transformations
00745    
00746                   for (int i = low; i <= high; i++) {
00747                      p = x * V[i][k] + y * V[i][k+1];
00748                      if (notlast) {
00749                         p = p + z * V[i][k+2];
00750                         V[i][k+2] = V[i][k+2] - p * r;
00751                      }
00752                      V[i][k] = V[i][k] - p;
00753                      V[i][k+1] = V[i][k+1] - p * q;
00754                   }
00755                }  // (s != 0)
00756             }  // k loop
00757          }  // check convergence
00758       }  // while (n >= low)
00759         //if(totIter>15) std::cout<<"!!!!iter "<<totIter<<"\n";
00760       
00761       // Backsubstitute to find vectors of upper triangular form
00762 
00763       if (norm == 0.0) {
00764          return;
00765       }
00766    
00767       for (n = nn-1; n >= 0; n--) {
00768          p = d[n];
00769          q = e[n];
00770    
00771          // Real vector
00772    
00773          if (q == 0) {
00774             int l = n;
00775             H[n][n] = 1.0;
00776             for (int i = n-1; i >= 0; i--) {
00777                w = H[i][i] - p;
00778                r = 0.0;
00779                for (int j = l; j <= n; j++) {
00780                   r = r + H[i][j] * H[j][n];
00781                }
00782                if (e[i] < 0.0) {
00783                   z = w;
00784                   s = r;
00785                } else {
00786                   l = i;
00787                   if (e[i] == 0.0) {
00788                      if (w != 0.0) {
00789                         H[i][n] = -r / w;
00790                      } else {
00791                         H[i][n] = -r / (eps * norm);
00792                      }
00793    
00794                   // Solve real equations
00795    
00796                   } else {
00797                      x = H[i][i+1];
00798                      y = H[i+1][i];
00799                      q = (d[i] - p) * (d[i] - p) + e[i] * e[i];
00800                      t = (x * s - z * r) / q;
00801                      H[i][n] = t;
00802                      if (fabs(x) > fabs(z)) {
00803                         H[i+1][n] = (-r - w * t) / x;
00804                      } else {
00805                         H[i+1][n] = (-s - y * t) / z;
00806                      }
00807                   }
00808    
00809                   // Overflow control
00810    
00811                   t = fabs(H[i][n]);
00812                   if ((eps * t) * t > 1) {
00813                      for (int j = i; j <= n; j++) {
00814                         H[j][n] = H[j][n] / t;
00815                      }
00816                   }
00817                }
00818             }
00819    
00820          // Complex vector
00821    
00822          } else if (q < 0) {
00823             int l = n-1;
00824 
00825             // Last vector component imaginary so matrix is triangular
00826    
00827             if (fabs(H[n][n-1]) > fabs(H[n-1][n])) {
00828                H[n-1][n-1] = q / H[n][n-1];
00829                H[n-1][n] = -(H[n][n] - p) / H[n][n-1];
00830             } else {
00831                cdiv(0.0,-H[n-1][n],H[n-1][n-1]-p,q);
00832                H[n-1][n-1] = cdivr;
00833                H[n-1][n] = cdivi;
00834             }
00835             H[n][n-1] = 0.0;
00836             H[n][n] = 1.0;
00837             for (int i = n-2; i >= 0; i--) {
00838                Real ra,sa,vr,vi;
00839                ra = 0.0;
00840                sa = 0.0;
00841                for (int j = l; j <= n; j++) {
00842                   ra = ra + H[i][j] * H[j][n-1];
00843                   sa = sa + H[i][j] * H[j][n];
00844                }
00845                w = H[i][i] - p;
00846    
00847                if (e[i] < 0.0) {
00848                   z = w;
00849                   r = ra;
00850                   s = sa;
00851                } else {
00852                   l = i;
00853                   if (e[i] == 0) {
00854                      cdiv(-ra,-sa,w,q);
00855                      H[i][n-1] = cdivr;
00856                      H[i][n] = cdivi;
00857                   } else {
00858    
00859                      // Solve complex equations
00860    
00861                      x = H[i][i+1];
00862                      y = H[i+1][i];
00863                      vr = (d[i] - p) * (d[i] - p) + e[i] * e[i] - q * q;
00864                      vi = (d[i] - p) * 2.0 * q;
00865                      if ((vr == 0.0) && (vi == 0.0)) {
00866                         vr = eps * norm * (fabs(w) + fabs(q) +
00867                         fabs(x) + fabs(y) + fabs(z));
00868                      }
00869                      cdiv(x*r-z*ra+q*sa,x*s-z*sa-q*ra,vr,vi);
00870                      H[i][n-1] = cdivr;
00871                      H[i][n] = cdivi;
00872                      if (fabs(x) > (fabs(z) + fabs(q))) {
00873                         H[i+1][n-1] = (-ra - w * H[i][n-1] + q * H[i][n]) / x;
00874                         H[i+1][n] = (-sa - w * H[i][n] - q * H[i][n-1]) / x;
00875                      } else {
00876                         cdiv(-r-y*H[i][n-1],-s-y*H[i][n],z,q);
00877                         H[i+1][n-1] = cdivr;
00878                         H[i+1][n] = cdivi;
00879                      }
00880                   }
00881    
00882                   // Overflow control
00883 
00884                   t = max(fabs(H[i][n-1]),fabs(H[i][n]));
00885                   if ((eps * t) * t > 1) {
00886                      for (int j = i; j <= n; j++) {
00887                         H[j][n-1] = H[j][n-1] / t;
00888                         H[j][n] = H[j][n] / t;
00889                      }
00890                   }
00891                }
00892             }
00893          }
00894       }
00895    
00896       // Vectors of isolated roots
00897    
00898       for (int i = 0; i < nn; i++) {
00899          if (i < low || i > high) {
00900             for (int j = i; j < nn; j++) {
00901                V[i][j] = H[i][j];
00902             }
00903          }
00904       }
00905    
00906       // Back transformation to get eigenvectors of original matrix
00907    
00908       for (int j = nn-1; j >= low; j--) {
00909          for (int i = low; i <= high; i++) {
00910             z = 0.0;
00911             for (int k = low; k <= min(j,high); k++) {
00912                z = z + V[i][k] * H[k][j];
00913             }
00914             V[i][j] = z;
00915          }
00916       }
00917    }
00918 
00919 public:
00920 
00921 
00926    Eigenvalue(const TNT::Array2D<Real> &A) {
00927       n = A.dim2();
00928       V = Array2D<Real>(n,n);
00929       d = Array1D<Real>(n);
00930       e = Array1D<Real>(n);
00931 
00932       issymmetric = 1;
00933       for (int j = 0; (j < n) && issymmetric; j++) {
00934          for (int i = 0; (i < n) && issymmetric; i++) {
00935             issymmetric = (A[i][j] == A[j][i]);
00936          }
00937       }
00938 
00939       if (issymmetric) {
00940          for (int i = 0; i < n; i++) {
00941             for (int j = 0; j < n; j++) {
00942                V[i][j] = A[i][j];
00943             }
00944          }
00945    
00946          // Tridiagonalize.
00947          tred2();
00948    
00949          // Diagonalize.
00950          tql2();
00951 
00952       } else {
00953          H = TNT::Array2D<Real>(n,n);
00954          ort = TNT::Array1D<Real>(n);
00955          
00956          for (int j = 0; j < n; j++) {
00957             for (int i = 0; i < n; i++) {
00958                H[i][j] = A[i][j];
00959             }
00960          }
00961    
00962          // Reduce to Hessenberg form.
00963          orthes();
00964    
00965          // Reduce Hessenberg to real Schur form.
00966          hqr2();
00967       }
00968    }
00969 
00970 
00975    void getV (TNT::Array2D<Real> &V_) {
00976       V_ = V;
00977       return;
00978    }
00979 
00984    void getRealEigenvalues (TNT::Array1D<Real> &d_) {
00985       d_ = d;
00986       return ;
00987    }
00988 
00994    void getImagEigenvalues (TNT::Array1D<Real> &e_) {
00995       e_ = e;
00996       return;
00997    }
00998 
00999    
01033    void getD (TNT::Array2D<Real> &D) {
01034       D = Array2D<Real>(n,n);
01035       for (int i = 0; i < n; i++) {
01036          for (int j = 0; j < n; j++) {
01037             D[i][j] = 0.0;
01038          }
01039          D[i][i] = d[i];
01040          if (e[i] > 0) {
01041             D[i][i+1] = e[i];
01042          } else if (e[i] < 0) {
01043             D[i][i-1] = e[i];
01044          }
01045       }
01046    }
01047 };
01048 
01049 } //namespace JAMA
01050 
01051 
01052 #endif
01053 // JAMA_EIG_H