MOAB  4.9.3pre
EigenSolver.h
Go to the documentation of this file.
00001 // This file is part of Eigen, a lightweight C++ template library
00002 // for linear algebra.
00003 //
00004 // Copyright (C) 2008 Gael Guennebaud <[email protected]>
00005 // Copyright (C) 2010,2012 Jitse Niesen <[email protected]>
00006 //
00007 // This Source Code Form is subject to the terms of the Mozilla
00008 // Public License v. 2.0. If a copy of the MPL was not distributed
00009 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
00010 
00011 #ifndef EIGEN_EIGENSOLVER_H
00012 #define EIGEN_EIGENSOLVER_H
00013 
00014 #include "./RealSchur.h"
00015 
00016 namespace Eigen { 
00017 
00064 template<typename _MatrixType> class EigenSolver
00065 {
00066   public:
00067 
00069     typedef _MatrixType MatrixType;
00070 
00071     enum {
00072       RowsAtCompileTime = MatrixType::RowsAtCompileTime,
00073       ColsAtCompileTime = MatrixType::ColsAtCompileTime,
00074       Options = MatrixType::Options,
00075       MaxRowsAtCompileTime = MatrixType::MaxRowsAtCompileTime,
00076       MaxColsAtCompileTime = MatrixType::MaxColsAtCompileTime
00077     };
00078 
00080     typedef typename MatrixType::Scalar Scalar;
00081     typedef typename NumTraits<Scalar>::Real RealScalar;
00082     typedef Eigen::Index Index; 
00083 
00090     typedef std::complex<RealScalar> ComplexScalar;
00091 
00097     typedef Matrix<ComplexScalar, ColsAtCompileTime, 1, Options & ~RowMajor, MaxColsAtCompileTime, 1> EigenvalueType;
00098 
00104     typedef Matrix<ComplexScalar, RowsAtCompileTime, ColsAtCompileTime, Options, MaxRowsAtCompileTime, MaxColsAtCompileTime> EigenvectorsType;
00105 
00113     EigenSolver() : m_eivec(), m_eivalues(), m_isInitialized(false), m_realSchur(), m_matT(), m_tmp() {}
00114 
00121     explicit EigenSolver(Index size)
00122       : m_eivec(size, size),
00123         m_eivalues(size),
00124         m_isInitialized(false),
00125         m_eigenvectorsOk(false),
00126         m_realSchur(size),
00127         m_matT(size, size), 
00128         m_tmp(size)
00129     {}
00130 
00146     template<typename InputType>
00147     explicit EigenSolver(const EigenBase<InputType>& matrix, bool computeEigenvectors = true)
00148       : m_eivec(matrix.rows(), matrix.cols()),
00149         m_eivalues(matrix.cols()),
00150         m_isInitialized(false),
00151         m_eigenvectorsOk(false),
00152         m_realSchur(matrix.cols()),
00153         m_matT(matrix.rows(), matrix.cols()), 
00154         m_tmp(matrix.cols())
00155     {
00156       compute(matrix.derived(), computeEigenvectors);
00157     }
00158 
00179     EigenvectorsType eigenvectors() const;
00180 
00199     const MatrixType& pseudoEigenvectors() const
00200     {
00201       eigen_assert(m_isInitialized && "EigenSolver is not initialized.");
00202       eigen_assert(m_eigenvectorsOk && "The eigenvectors have not been computed together with the eigenvalues.");
00203       return m_eivec;
00204     }
00205 
00224     MatrixType pseudoEigenvalueMatrix() const;
00225 
00244     const EigenvalueType& eigenvalues() const
00245     {
00246       eigen_assert(m_isInitialized && "EigenSolver is not initialized.");
00247       return m_eivalues;
00248     }
00249 
00277     template<typename InputType>
00278     EigenSolver& compute(const EigenBase<InputType>& matrix, bool computeEigenvectors = true);
00279 
00281     ComputationInfo info() const
00282     {
00283       eigen_assert(m_isInitialized && "EigenSolver is not initialized.");
00284       return m_info;
00285     }
00286 
00288     EigenSolver& setMaxIterations(Index maxIters)
00289     {
00290       m_realSchur.setMaxIterations(maxIters);
00291       return *this;
00292     }
00293 
00295     Index getMaxIterations()
00296     {
00297       return m_realSchur.getMaxIterations();
00298     }
00299 
00300   private:
00301     void doComputeEigenvectors();
00302 
00303   protected:
00304     
00305     static void check_template_parameters()
00306     {
00307       EIGEN_STATIC_ASSERT_NON_INTEGER(Scalar);
00308       EIGEN_STATIC_ASSERT(!NumTraits<Scalar>::IsComplex, NUMERIC_TYPE_MUST_BE_REAL);
00309     }
00310     
00311     MatrixType m_eivec;
00312     EigenvalueType m_eivalues;
00313     bool m_isInitialized;
00314     bool m_eigenvectorsOk;
00315     ComputationInfo m_info;
00316     RealSchur<MatrixType> m_realSchur;
00317     MatrixType m_matT;
00318 
00319     typedef Matrix<Scalar, ColsAtCompileTime, 1, Options & ~RowMajor, MaxColsAtCompileTime, 1> ColumnVectorType;
00320     ColumnVectorType m_tmp;
00321 };
00322 
00323 template<typename MatrixType>
00324 MatrixType EigenSolver<MatrixType>::pseudoEigenvalueMatrix() const
00325 {
00326   eigen_assert(m_isInitialized && "EigenSolver is not initialized.");
00327   Index n = m_eivalues.rows();
00328   MatrixType matD = MatrixType::Zero(n,n);
00329   for (Index i=0; i<n; ++i)
00330   {
00331     if (internal::isMuchSmallerThan(numext::imag(m_eivalues.coeff(i)), numext::real(m_eivalues.coeff(i))))
00332       matD.coeffRef(i,i) = numext::real(m_eivalues.coeff(i));
00333     else
00334     {
00335       matD.template block<2,2>(i,i) <<  numext::real(m_eivalues.coeff(i)), numext::imag(m_eivalues.coeff(i)),
00336                                        -numext::imag(m_eivalues.coeff(i)), numext::real(m_eivalues.coeff(i));
00337       ++i;
00338     }
00339   }
00340   return matD;
00341 }
00342 
00343 template<typename MatrixType>
00344 typename EigenSolver<MatrixType>::EigenvectorsType EigenSolver<MatrixType>::eigenvectors() const
00345 {
00346   eigen_assert(m_isInitialized && "EigenSolver is not initialized.");
00347   eigen_assert(m_eigenvectorsOk && "The eigenvectors have not been computed together with the eigenvalues.");
00348   Index n = m_eivec.cols();
00349   EigenvectorsType matV(n,n);
00350   for (Index j=0; j<n; ++j)
00351   {
00352     if (internal::isMuchSmallerThan(numext::imag(m_eivalues.coeff(j)), numext::real(m_eivalues.coeff(j))) || j+1==n)
00353     {
00354       // we have a real eigen value
00355       matV.col(j) = m_eivec.col(j).template cast<ComplexScalar>();
00356       matV.col(j).normalize();
00357     }
00358     else
00359     {
00360       // we have a pair of complex eigen values
00361       for (Index i=0; i<n; ++i)
00362       {
00363         matV.coeffRef(i,j)   = ComplexScalar(m_eivec.coeff(i,j),  m_eivec.coeff(i,j+1));
00364         matV.coeffRef(i,j+1) = ComplexScalar(m_eivec.coeff(i,j), -m_eivec.coeff(i,j+1));
00365       }
00366       matV.col(j).normalize();
00367       matV.col(j+1).normalize();
00368       ++j;
00369     }
00370   }
00371   return matV;
00372 }
00373 
00374 template<typename MatrixType>
00375 template<typename InputType>
00376 EigenSolver<MatrixType>& 
00377 EigenSolver<MatrixType>::compute(const EigenBase<InputType>& matrix, bool computeEigenvectors)
00378 {
00379   check_template_parameters();
00380   
00381   using std::sqrt;
00382   using std::abs;
00383   using numext::isfinite;
00384   eigen_assert(matrix.cols() == matrix.rows());
00385 
00386   // Reduce to real Schur form.
00387   m_realSchur.compute(matrix.derived(), computeEigenvectors);
00388   
00389   m_info = m_realSchur.info();
00390 
00391   if (m_info == Success)
00392   {
00393     m_matT = m_realSchur.matrixT();
00394     if (computeEigenvectors)
00395       m_eivec = m_realSchur.matrixU();
00396   
00397     // Compute eigenvalues from matT
00398     m_eivalues.resize(matrix.cols());
00399     Index i = 0;
00400     while (i < matrix.cols()) 
00401     {
00402       if (i == matrix.cols() - 1 || m_matT.coeff(i+1, i) == Scalar(0)) 
00403       {
00404         m_eivalues.coeffRef(i) = m_matT.coeff(i, i);
00405         if(!(isfinite)(m_eivalues.coeffRef(i)))
00406         {
00407           m_isInitialized = true;
00408           m_eigenvectorsOk = false;
00409           m_info = NumericalIssue;
00410           return *this;
00411         }
00412         ++i;
00413       }
00414       else
00415       {
00416         Scalar p = Scalar(0.5) * (m_matT.coeff(i, i) - m_matT.coeff(i+1, i+1));
00417         Scalar z;
00418         // Compute z = sqrt(abs(p * p + m_matT.coeff(i+1, i) * m_matT.coeff(i, i+1)));
00419         // without overflow
00420         {
00421           Scalar t0 = m_matT.coeff(i+1, i);
00422           Scalar t1 = m_matT.coeff(i, i+1);
00423           Scalar maxval = numext::maxi<Scalar>(abs(p),numext::maxi<Scalar>(abs(t0),abs(t1)));
00424           t0 /= maxval;
00425           t1 /= maxval;
00426           Scalar p0 = p/maxval;
00427           z = maxval * sqrt(abs(p0 * p0 + t0 * t1));
00428         }
00429         
00430         m_eivalues.coeffRef(i)   = ComplexScalar(m_matT.coeff(i+1, i+1) + p, z);
00431         m_eivalues.coeffRef(i+1) = ComplexScalar(m_matT.coeff(i+1, i+1) + p, -z);
00432         if(!((isfinite)(m_eivalues.coeffRef(i)) && (isfinite)(m_eivalues.coeffRef(i+1))))
00433         {
00434           m_isInitialized = true;
00435           m_eigenvectorsOk = false;
00436           m_info = NumericalIssue;
00437           return *this;
00438         }
00439         i += 2;
00440       }
00441     }
00442     
00443     // Compute eigenvectors.
00444     if (computeEigenvectors)
00445       doComputeEigenvectors();
00446   }
00447 
00448   m_isInitialized = true;
00449   m_eigenvectorsOk = computeEigenvectors;
00450 
00451   return *this;
00452 }
00453 
00454 // Complex scalar division.
00455 template<typename Scalar>
00456 std::complex<Scalar> cdiv(const Scalar& xr, const Scalar& xi, const Scalar& yr, const Scalar& yi)
00457 {
00458   using std::abs;
00459   Scalar r,d;
00460   if (abs(yr) > abs(yi))
00461   {
00462       r = yi/yr;
00463       d = yr + r*yi;
00464       return std::complex<Scalar>((xr + r*xi)/d, (xi - r*xr)/d);
00465   }
00466   else
00467   {
00468       r = yr/yi;
00469       d = yi + r*yr;
00470       return std::complex<Scalar>((r*xr + xi)/d, (r*xi - xr)/d);
00471   }
00472 }
00473 
00474 
00475 template<typename MatrixType>
00476 void EigenSolver<MatrixType>::doComputeEigenvectors()
00477 {
00478   using std::abs;
00479   const Index size = m_eivec.cols();
00480   const Scalar eps = NumTraits<Scalar>::epsilon();
00481 
00482   // inefficient! this is already computed in RealSchur
00483   Scalar norm(0);
00484   for (Index j = 0; j < size; ++j)
00485   {
00486     norm += m_matT.row(j).segment((std::max)(j-1,Index(0)), size-(std::max)(j-1,Index(0))).cwiseAbs().sum();
00487   }
00488   
00489   // Backsubstitute to find vectors of upper triangular form
00490   if (norm == Scalar(0))
00491   {
00492     return;
00493   }
00494 
00495   for (Index n = size-1; n >= 0; n--)
00496   {
00497     Scalar p = m_eivalues.coeff(n).real();
00498     Scalar q = m_eivalues.coeff(n).imag();
00499 
00500     // Scalar vector
00501     if (q == Scalar(0))
00502     {
00503       Scalar lastr(0), lastw(0);
00504       Index l = n;
00505 
00506       m_matT.coeffRef(n,n) = 1.0;
00507       for (Index i = n-1; i >= 0; i--)
00508       {
00509         Scalar w = m_matT.coeff(i,i) - p;
00510         Scalar r = m_matT.row(i).segment(l,n-l+1).dot(m_matT.col(n).segment(l, n-l+1));
00511 
00512         if (m_eivalues.coeff(i).imag() < Scalar(0))
00513         {
00514           lastw = w;
00515           lastr = r;
00516         }
00517         else
00518         {
00519           l = i;
00520           if (m_eivalues.coeff(i).imag() == Scalar(0))
00521           {
00522             if (w != Scalar(0))
00523               m_matT.coeffRef(i,n) = -r / w;
00524             else
00525               m_matT.coeffRef(i,n) = -r / (eps * norm);
00526           }
00527           else // Solve real equations
00528           {
00529             Scalar x = m_matT.coeff(i,i+1);
00530             Scalar y = m_matT.coeff(i+1,i);
00531             Scalar denom = (m_eivalues.coeff(i).real() - p) * (m_eivalues.coeff(i).real() - p) + m_eivalues.coeff(i).imag() * m_eivalues.coeff(i).imag();
00532             Scalar t = (x * lastr - lastw * r) / denom;
00533             m_matT.coeffRef(i,n) = t;
00534             if (abs(x) > abs(lastw))
00535               m_matT.coeffRef(i+1,n) = (-r - w * t) / x;
00536             else
00537               m_matT.coeffRef(i+1,n) = (-lastr - y * t) / lastw;
00538           }
00539 
00540           // Overflow control
00541           Scalar t = abs(m_matT.coeff(i,n));
00542           if ((eps * t) * t > Scalar(1))
00543             m_matT.col(n).tail(size-i) /= t;
00544         }
00545       }
00546     }
00547     else if (q < Scalar(0) && n > 0) // Complex vector
00548     {
00549       Scalar lastra(0), lastsa(0), lastw(0);
00550       Index l = n-1;
00551 
00552       // Last vector component imaginary so matrix is triangular
00553       if (abs(m_matT.coeff(n,n-1)) > abs(m_matT.coeff(n-1,n)))
00554       {
00555         m_matT.coeffRef(n-1,n-1) = q / m_matT.coeff(n,n-1);
00556         m_matT.coeffRef(n-1,n) = -(m_matT.coeff(n,n) - p) / m_matT.coeff(n,n-1);
00557       }
00558       else
00559       {
00560         std::complex<Scalar> cc = cdiv<Scalar>(Scalar(0),-m_matT.coeff(n-1,n),m_matT.coeff(n-1,n-1)-p,q);
00561         m_matT.coeffRef(n-1,n-1) = numext::real(cc);
00562         m_matT.coeffRef(n-1,n) = numext::imag(cc);
00563       }
00564       m_matT.coeffRef(n,n-1) = Scalar(0);
00565       m_matT.coeffRef(n,n) = Scalar(1);
00566       for (Index i = n-2; i >= 0; i--)
00567       {
00568         Scalar ra = m_matT.row(i).segment(l, n-l+1).dot(m_matT.col(n-1).segment(l, n-l+1));
00569         Scalar sa = m_matT.row(i).segment(l, n-l+1).dot(m_matT.col(n).segment(l, n-l+1));
00570         Scalar w = m_matT.coeff(i,i) - p;
00571 
00572         if (m_eivalues.coeff(i).imag() < Scalar(0))
00573         {
00574           lastw = w;
00575           lastra = ra;
00576           lastsa = sa;
00577         }
00578         else
00579         {
00580           l = i;
00581           if (m_eivalues.coeff(i).imag() == RealScalar(0))
00582           {
00583             std::complex<Scalar> cc = cdiv(-ra,-sa,w,q);
00584             m_matT.coeffRef(i,n-1) = numext::real(cc);
00585             m_matT.coeffRef(i,n) = numext::imag(cc);
00586           }
00587           else
00588           {
00589             // Solve complex equations
00590             Scalar x = m_matT.coeff(i,i+1);
00591             Scalar y = m_matT.coeff(i+1,i);
00592             Scalar vr = (m_eivalues.coeff(i).real() - p) * (m_eivalues.coeff(i).real() - p) + m_eivalues.coeff(i).imag() * m_eivalues.coeff(i).imag() - q * q;
00593             Scalar vi = (m_eivalues.coeff(i).real() - p) * Scalar(2) * q;
00594             if ((vr == Scalar(0)) && (vi == Scalar(0)))
00595               vr = eps * norm * (abs(w) + abs(q) + abs(x) + abs(y) + abs(lastw));
00596 
00597             std::complex<Scalar> cc = cdiv(x*lastra-lastw*ra+q*sa,x*lastsa-lastw*sa-q*ra,vr,vi);
00598             m_matT.coeffRef(i,n-1) = numext::real(cc);
00599             m_matT.coeffRef(i,n) = numext::imag(cc);
00600             if (abs(x) > (abs(lastw) + abs(q)))
00601             {
00602               m_matT.coeffRef(i+1,n-1) = (-ra - w * m_matT.coeff(i,n-1) + q * m_matT.coeff(i,n)) / x;
00603               m_matT.coeffRef(i+1,n) = (-sa - w * m_matT.coeff(i,n) - q * m_matT.coeff(i,n-1)) / x;
00604             }
00605             else
00606             {
00607               cc = cdiv(-lastra-y*m_matT.coeff(i,n-1),-lastsa-y*m_matT.coeff(i,n),lastw,q);
00608               m_matT.coeffRef(i+1,n-1) = numext::real(cc);
00609               m_matT.coeffRef(i+1,n) = numext::imag(cc);
00610             }
00611           }
00612 
00613           // Overflow control
00614           Scalar t = numext::maxi<Scalar>(abs(m_matT.coeff(i,n-1)),abs(m_matT.coeff(i,n)));
00615           if ((eps * t) * t > Scalar(1))
00616             m_matT.block(i, n-1, size-i, 2) /= t;
00617 
00618         }
00619       }
00620       
00621       // We handled a pair of complex conjugate eigenvalues, so need to skip them both
00622       n--;
00623     }
00624     else
00625     {
00626       eigen_assert(0 && "Internal bug in EigenSolver (INF or NaN has not been detected)"); // this should not happen
00627     }
00628   }
00629 
00630   // Back transformation to get eigenvectors of original matrix
00631   for (Index j = size-1; j >= 0; j--)
00632   {
00633     m_tmp.noalias() = m_eivec.leftCols(j+1) * m_matT.col(j).segment(0, j+1);
00634     m_eivec.col(j) = m_tmp;
00635   }
00636 }
00637 
00638 } // end namespace Eigen
00639 
00640 #endif // EIGEN_EIGENSOLVER_H
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines