MatrixPower.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) 2012, 2013 Chen-Pang He <jdh8@ms63.hinet.net>
00005 //
00006 // This Source Code Form is subject to the terms of the Mozilla
00007 // Public License v. 2.0. If a copy of the MPL was not distributed
00008 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
00009 
00010 #ifndef EIGEN_MATRIX_POWER
00011 #define EIGEN_MATRIX_POWER
00012 
00013 namespace Eigen {
00014 
00015 template<typename MatrixType> class MatrixPower;
00016 
00017 template<typename MatrixType>
00018 class MatrixPowerRetval : public ReturnByValue< MatrixPowerRetval<MatrixType> >
00019 {
00020   public:
00021     typedef typename MatrixType::RealScalar RealScalar;
00022     typedef typename MatrixType::Index Index;
00023 
00024     MatrixPowerRetval(MatrixPower<MatrixType>& pow, RealScalar p) : m_pow(pow), m_p(p)
00025     { }
00026 
00027     template<typename ResultType>
00028     inline void evalTo(ResultType& res) const
00029     { m_pow.compute(res, m_p); }
00030 
00031     Index rows() const { return m_pow.rows(); }
00032     Index cols() const { return m_pow.cols(); }
00033 
00034   private:
00035     MatrixPower<MatrixType>& m_pow;
00036     const RealScalar m_p;
00037     MatrixPowerRetval& operator=(const MatrixPowerRetval&);
00038 };
00039 
00040 template<typename MatrixType>
00041 class MatrixPowerAtomic
00042 {
00043   private:
00044     enum {
00045       RowsAtCompileTime = MatrixType::RowsAtCompileTime,
00046       MaxRowsAtCompileTime = MatrixType::MaxRowsAtCompileTime
00047     };
00048     typedef typename MatrixType::Scalar Scalar;
00049     typedef typename MatrixType::RealScalar RealScalar;
00050     typedef std::complex<RealScalar> ComplexScalar;
00051     typedef typename MatrixType::Index Index;
00052     typedef Array<Scalar, RowsAtCompileTime, 1, ColMajor, MaxRowsAtCompileTime> ArrayType;
00053 
00054     const MatrixType& m_A;
00055     RealScalar m_p;
00056 
00057     void computePade(int degree, const MatrixType& IminusT, MatrixType& res) const;
00058     void compute2x2(MatrixType& res, RealScalar p) const;
00059     void computeBig(MatrixType& res) const;
00060     static int getPadeDegree(float normIminusT);
00061     static int getPadeDegree(double normIminusT);
00062     static int getPadeDegree(long double normIminusT);
00063     static ComplexScalar computeSuperDiag(const ComplexScalar&, const ComplexScalar&, RealScalar p);
00064     static RealScalar computeSuperDiag(RealScalar, RealScalar, RealScalar p);
00065 
00066   public:
00067     MatrixPowerAtomic(const MatrixType& T, RealScalar p);
00068     void compute(MatrixType& res) const;
00069 };
00070 
00071 template<typename MatrixType>
00072 MatrixPowerAtomic<MatrixType>::MatrixPowerAtomic(const MatrixType& T, RealScalar p) :
00073   m_A(T), m_p(p)
00074 { eigen_assert(T.rows() == T.cols()); }
00075 
00076 template<typename MatrixType>
00077 void MatrixPowerAtomic<MatrixType>::compute(MatrixType& res) const
00078 {
00079   res.resizeLike(m_A);
00080   switch (m_A.rows()) {
00081     case 0:
00082       break;
00083     case 1:
00084       res(0,0) = std::pow(m_A(0,0), m_p);
00085       break;
00086     case 2:
00087       compute2x2(res, m_p);
00088       break;
00089     default:
00090       computeBig(res);
00091   }
00092 }
00093 
00094 template<typename MatrixType>
00095 void MatrixPowerAtomic<MatrixType>::computePade(int degree, const MatrixType& IminusT, MatrixType& res) const
00096 {
00097   int i = degree<<1;
00098   res = (m_p-degree) / ((i-1)<<1) * IminusT;
00099   for (--i; i; --i) {
00100     res = (MatrixType::Identity(IminusT.rows(), IminusT.cols()) + res).template triangularView<Upper>()
00101         .solve((i==1 ? -m_p : i&1 ? (-m_p-(i>>1))/(i<<1) : (m_p-(i>>1))/((i-1)<<1)) * IminusT).eval();
00102   }
00103   res += MatrixType::Identity(IminusT.rows(), IminusT.cols());
00104 }
00105 
00106 // This function assumes that res has the correct size (see bug 614)
00107 template<typename MatrixType>
00108 void MatrixPowerAtomic<MatrixType>::compute2x2(MatrixType& res, RealScalar p) const
00109 {
00110   using std::abs;
00111   using std::pow;
00112   
00113   res.coeffRef(0,0) = pow(m_A.coeff(0,0), p);
00114 
00115   for (Index i=1; i < m_A.cols(); ++i) {
00116     res.coeffRef(i,i) = pow(m_A.coeff(i,i), p);
00117     if (m_A.coeff(i-1,i-1) == m_A.coeff(i,i))
00118       res.coeffRef(i-1,i) = p * pow(m_A.coeff(i,i), p-1);
00119     else if (2*abs(m_A.coeff(i-1,i-1)) < abs(m_A.coeff(i,i)) || 2*abs(m_A.coeff(i,i)) < abs(m_A.coeff(i-1,i-1)))
00120       res.coeffRef(i-1,i) = (res.coeff(i,i)-res.coeff(i-1,i-1)) / (m_A.coeff(i,i)-m_A.coeff(i-1,i-1));
00121     else
00122       res.coeffRef(i-1,i) = computeSuperDiag(m_A.coeff(i,i), m_A.coeff(i-1,i-1), p);
00123     res.coeffRef(i-1,i) *= m_A.coeff(i-1,i);
00124   }
00125 }
00126 
00127 template<typename MatrixType>
00128 void MatrixPowerAtomic<MatrixType>::computeBig(MatrixType& res) const
00129 {
00130   const int digits = std::numeric_limits<RealScalar>::digits;
00131   const RealScalar maxNormForPade = digits <=  24? 4.3386528e-1f:                           // sigle precision
00132                                     digits <=  53? 2.789358995219730e-1:                    // double precision
00133                                     digits <=  64? 2.4471944416607995472e-1L:               // extended precision
00134                                     digits <= 106? 1.1016843812851143391275867258512e-1L:   // double-double
00135                                                    9.134603732914548552537150753385375e-2L; // quadruple precision
00136   MatrixType IminusT, sqrtT, T = m_A.template triangularView<Upper>();
00137   RealScalar normIminusT;
00138   int degree, degree2, numberOfSquareRoots = 0;
00139   bool hasExtraSquareRoot = false;
00140 
00141   /* FIXME
00142    * For singular T, norm(I - T) >= 1 but maxNormForPade < 1, leads to infinite
00143    * loop.  We should move 0 eigenvalues to bottom right corner.  We need not
00144    * worry about tiny values (e.g. 1e-300) because they will reach 1 if
00145    * repetitively sqrt'ed.
00146    *
00147    * If the 0 eigenvalues are semisimple, they can form a 0 matrix at the
00148    * bottom right corner.
00149    *
00150    * [ T  A ]^p   [ T^p  (T^-1 T^p A) ]
00151    * [      ]   = [                   ]
00152    * [ 0  0 ]     [  0         0      ]
00153    */
00154   for (Index i=0; i < m_A.cols(); ++i)
00155     eigen_assert(m_A(i,i) != RealScalar(0));
00156 
00157   while (true) {
00158     IminusT = MatrixType::Identity(m_A.rows(), m_A.cols()) - T;
00159     normIminusT = IminusT.cwiseAbs().colwise().sum().maxCoeff();
00160     if (normIminusT < maxNormForPade) {
00161       degree = getPadeDegree(normIminusT);
00162       degree2 = getPadeDegree(normIminusT/2);
00163       if (degree - degree2 <= 1 || hasExtraSquareRoot)
00164         break;
00165       hasExtraSquareRoot = true;
00166     }
00167     MatrixSquareRootTriangular<MatrixType>(T).compute(sqrtT);
00168     T = sqrtT.template triangularView<Upper>();
00169     ++numberOfSquareRoots;
00170   }
00171   computePade(degree, IminusT, res);
00172 
00173   for (; numberOfSquareRoots; --numberOfSquareRoots) {
00174     compute2x2(res, std::ldexp(m_p, -numberOfSquareRoots));
00175     res = res.template triangularView<Upper>() * res;
00176   }
00177   compute2x2(res, m_p);
00178 }
00179   
00180 template<typename MatrixType>
00181 inline int MatrixPowerAtomic<MatrixType>::getPadeDegree(float normIminusT)
00182 {
00183   const float maxNormForPade[] = { 2.8064004e-1f /* degree = 3 */ , 4.3386528e-1f };
00184   int degree = 3;
00185   for (; degree <= 4; ++degree)
00186     if (normIminusT <= maxNormForPade[degree - 3])
00187       break;
00188   return degree;
00189 }
00190 
00191 template<typename MatrixType>
00192 inline int MatrixPowerAtomic<MatrixType>::getPadeDegree(double normIminusT)
00193 {
00194   const double maxNormForPade[] = { 1.884160592658218e-2 /* degree = 3 */ , 6.038881904059573e-2, 1.239917516308172e-1,
00195       1.999045567181744e-1, 2.789358995219730e-1 };
00196   int degree = 3;
00197   for (; degree <= 7; ++degree)
00198     if (normIminusT <= maxNormForPade[degree - 3])
00199       break;
00200   return degree;
00201 }
00202 
00203 template<typename MatrixType>
00204 inline int MatrixPowerAtomic<MatrixType>::getPadeDegree(long double normIminusT)
00205 {
00206 #if   LDBL_MANT_DIG == 53
00207   const int maxPadeDegree = 7;
00208   const double maxNormForPade[] = { 1.884160592658218e-2L /* degree = 3 */ , 6.038881904059573e-2L, 1.239917516308172e-1L,
00209       1.999045567181744e-1L, 2.789358995219730e-1L };
00210 #elif LDBL_MANT_DIG <= 64
00211   const int maxPadeDegree = 8;
00212   const double maxNormForPade[] = { 6.3854693117491799460e-3L /* degree = 3 */ , 2.6394893435456973676e-2L,
00213       6.4216043030404063729e-2L, 1.1701165502926694307e-1L, 1.7904284231268670284e-1L, 2.4471944416607995472e-1L };
00214 #elif LDBL_MANT_DIG <= 106
00215   const int maxPadeDegree = 10;
00216   const double maxNormForPade[] = { 1.0007161601787493236741409687186e-4L /* degree = 3 */ ,
00217       1.0007161601787493236741409687186e-3L, 4.7069769360887572939882574746264e-3L, 1.3220386624169159689406653101695e-2L,
00218       2.8063482381631737920612944054906e-2L, 4.9625993951953473052385361085058e-2L, 7.7367040706027886224557538328171e-2L,
00219       1.1016843812851143391275867258512e-1L };
00220 #else
00221   const int maxPadeDegree = 10;
00222   const double maxNormForPade[] = { 5.524506147036624377378713555116378e-5L /* degree = 3 */ ,
00223       6.640600568157479679823602193345995e-4L, 3.227716520106894279249709728084626e-3L,
00224       9.619593944683432960546978734646284e-3L, 2.134595382433742403911124458161147e-2L,
00225       3.908166513900489428442993794761185e-2L, 6.266780814639442865832535460550138e-2L,
00226       9.134603732914548552537150753385375e-2L };
00227 #endif
00228   int degree = 3;
00229   for (; degree <= maxPadeDegree; ++degree)
00230     if (normIminusT <= maxNormForPade[degree - 3])
00231       break;
00232   return degree;
00233 }
00234 
00235 template<typename MatrixType>
00236 inline typename MatrixPowerAtomic<MatrixType>::ComplexScalar
00237 MatrixPowerAtomic<MatrixType>::computeSuperDiag(const ComplexScalar& curr, const ComplexScalar& prev, RealScalar p)
00238 {
00239   ComplexScalar logCurr = std::log(curr);
00240   ComplexScalar logPrev = std::log(prev);
00241   int unwindingNumber = std::ceil((numext::imag(logCurr - logPrev) - M_PI) / (2*M_PI));
00242   ComplexScalar w = numext::atanh2(curr - prev, curr + prev) + ComplexScalar(0, M_PI*unwindingNumber);
00243   return RealScalar(2) * std::exp(RealScalar(0.5) * p * (logCurr + logPrev)) * std::sinh(p * w) / (curr - prev);
00244 }
00245 
00246 template<typename MatrixType>
00247 inline typename MatrixPowerAtomic<MatrixType>::RealScalar
00248 MatrixPowerAtomic<MatrixType>::computeSuperDiag(RealScalar curr, RealScalar prev, RealScalar p)
00249 {
00250   RealScalar w = numext::atanh2(curr - prev, curr + prev);
00251   return 2 * std::exp(p * (std::log(curr) + std::log(prev)) / 2) * std::sinh(p * w) / (curr - prev);
00252 }
00253 
00273 template<typename MatrixType>
00274 class MatrixPower
00275 {
00276   private:
00277     enum {
00278       RowsAtCompileTime = MatrixType::RowsAtCompileTime,
00279       ColsAtCompileTime = MatrixType::ColsAtCompileTime,
00280       MaxRowsAtCompileTime = MatrixType::MaxRowsAtCompileTime,
00281       MaxColsAtCompileTime = MatrixType::MaxColsAtCompileTime
00282     };
00283     typedef typename MatrixType::Scalar Scalar;
00284     typedef typename MatrixType::RealScalar RealScalar;
00285     typedef typename MatrixType::Index Index;
00286 
00287   public:
00296     explicit MatrixPower(const MatrixType& A) : m_A(A), m_conditionNumber(0)
00297     { eigen_assert(A.rows() == A.cols()); }
00298 
00306     const MatrixPowerRetval<MatrixType> operator()(RealScalar p)
00307     { return MatrixPowerRetval<MatrixType>(*this, p); }
00308 
00316     template<typename ResultType>
00317     void compute(ResultType& res, RealScalar p);
00318     
00319     Index rows() const { return m_A.rows(); }
00320     Index cols() const { return m_A.cols(); }
00321 
00322   private:
00323     typedef std::complex<RealScalar> ComplexScalar;
00324     typedef Matrix<ComplexScalar, RowsAtCompileTime, ColsAtCompileTime, MatrixType::Options,
00325               MaxRowsAtCompileTime, MaxColsAtCompileTime> ComplexMatrix;
00326 
00327     typename MatrixType::Nested m_A;
00328     MatrixType m_tmp;
00329     ComplexMatrix m_T, m_U, m_fT;
00330     RealScalar m_conditionNumber;
00331 
00332     RealScalar modfAndInit(RealScalar, RealScalar*);
00333 
00334     template<typename ResultType>
00335     void computeIntPower(ResultType&, RealScalar);
00336 
00337     template<typename ResultType>
00338     void computeFracPower(ResultType&, RealScalar);
00339 
00340     template<int Rows, int Cols, int Options, int MaxRows, int MaxCols>
00341     static void revertSchur(
00342         Matrix<ComplexScalar, Rows, Cols, Options, MaxRows, MaxCols>& res,
00343         const ComplexMatrix& T,
00344         const ComplexMatrix& U);
00345 
00346     template<int Rows, int Cols, int Options, int MaxRows, int MaxCols>
00347     static void revertSchur(
00348         Matrix<RealScalar, Rows, Cols, Options, MaxRows, MaxCols>& res,
00349         const ComplexMatrix& T,
00350         const ComplexMatrix& U);
00351 };
00352 
00353 template<typename MatrixType>
00354 template<typename ResultType>
00355 void MatrixPower<MatrixType>::compute(ResultType& res, RealScalar p)
00356 {
00357   switch (cols()) {
00358     case 0:
00359       break;
00360     case 1:
00361       res(0,0) = std::pow(m_A.coeff(0,0), p);
00362       break;
00363     default:
00364       RealScalar intpart, x = modfAndInit(p, &intpart);
00365       computeIntPower(res, intpart);
00366       computeFracPower(res, x);
00367   }
00368 }
00369 
00370 template<typename MatrixType>
00371 typename MatrixPower<MatrixType>::RealScalar
00372 MatrixPower<MatrixType>::modfAndInit(RealScalar x, RealScalar* intpart)
00373 {
00374   typedef Array<RealScalar, RowsAtCompileTime, 1, ColMajor, MaxRowsAtCompileTime> RealArray;
00375 
00376   *intpart = std::floor(x);
00377   RealScalar res = x - *intpart;
00378 
00379   if (!m_conditionNumber && res) {
00380     const ComplexSchur<MatrixType> schurOfA(m_A);
00381     m_T = schurOfA.matrixT();
00382     m_U = schurOfA.matrixU();
00383     
00384     const RealArray absTdiag = m_T.diagonal().array().abs();
00385     m_conditionNumber = absTdiag.maxCoeff() / absTdiag.minCoeff();
00386   }
00387 
00388   if (res>RealScalar(0.5) && res>(1-res)*std::pow(m_conditionNumber, res)) {
00389     --res;
00390     ++*intpart;
00391   }
00392   return res;
00393 }
00394 
00395 template<typename MatrixType>
00396 template<typename ResultType>
00397 void MatrixPower<MatrixType>::computeIntPower(ResultType& res, RealScalar p)
00398 {
00399   RealScalar pp = std::abs(p);
00400 
00401   if (p<0)  m_tmp = m_A.inverse();
00402   else      m_tmp = m_A;
00403 
00404   res = MatrixType::Identity(rows(), cols());
00405   while (pp >= 1) {
00406     if (std::fmod(pp, 2) >= 1)
00407       res = m_tmp * res;
00408     m_tmp *= m_tmp;
00409     pp /= 2;
00410   }
00411 }
00412 
00413 template<typename MatrixType>
00414 template<typename ResultType>
00415 void MatrixPower<MatrixType>::computeFracPower(ResultType& res, RealScalar p)
00416 {
00417   if (p) {
00418     eigen_assert(m_conditionNumber);
00419     MatrixPowerAtomic<ComplexMatrix>(m_T, p).compute(m_fT);
00420     revertSchur(m_tmp, m_fT, m_U);
00421     res = m_tmp * res;
00422   }
00423 }
00424 
00425 template<typename MatrixType>
00426 template<int Rows, int Cols, int Options, int MaxRows, int MaxCols>
00427 inline void MatrixPower<MatrixType>::revertSchur(
00428     Matrix<ComplexScalar, Rows, Cols, Options, MaxRows, MaxCols>& res,
00429     const ComplexMatrix& T,
00430     const ComplexMatrix& U)
00431 { res.noalias() = U * (T.template triangularView<Upper>() * U.adjoint()); }
00432 
00433 template<typename MatrixType>
00434 template<int Rows, int Cols, int Options, int MaxRows, int MaxCols>
00435 inline void MatrixPower<MatrixType>::revertSchur(
00436     Matrix<RealScalar, Rows, Cols, Options, MaxRows, MaxCols>& res,
00437     const ComplexMatrix& T,
00438     const ComplexMatrix& U)
00439 { res.noalias() = (U * (T.template triangularView<Upper>() * U.adjoint())).real(); }
00440 
00454 template<typename Derived>
00455 class MatrixPowerReturnValue : public ReturnByValue< MatrixPowerReturnValue<Derived> >
00456 {
00457   public:
00458     typedef typename Derived::PlainObject PlainObject;
00459     typedef typename Derived::RealScalar RealScalar;
00460     typedef typename Derived::Index Index;
00461 
00468     MatrixPowerReturnValue(const Derived& A, RealScalar p) : m_A(A), m_p(p)
00469     { }
00470 
00477     template<typename ResultType>
00478     inline void evalTo(ResultType& res) const
00479     { MatrixPower<PlainObject>(m_A.eval()).compute(res, m_p); }
00480 
00481     Index rows() const { return m_A.rows(); }
00482     Index cols() const { return m_A.cols(); }
00483 
00484   private:
00485     const Derived& m_A;
00486     const RealScalar m_p;
00487     MatrixPowerReturnValue& operator=(const MatrixPowerReturnValue&);
00488 };
00489 
00490 namespace internal {
00491 
00492 template<typename MatrixPowerType>
00493 struct traits< MatrixPowerRetval<MatrixPowerType> >
00494 { typedef typename MatrixPowerType::PlainObject ReturnType; };
00495 
00496 template<typename Derived>
00497 struct traits< MatrixPowerReturnValue<Derived> >
00498 { typedef typename Derived::PlainObject ReturnType; };
00499 
00500 }
00501 
00502 template<typename Derived>
00503 const MatrixPowerReturnValue<Derived> MatrixBase<Derived>::pow(const RealScalar& p) const
00504 { return MatrixPowerReturnValue<Derived>(derived(), p); }
00505 
00506 } // namespace Eigen
00507 
00508 #endif // EIGEN_MATRIX_POWER


shape_reconstruction
Author(s): Roberto Martín-Martín
autogenerated on Sat Jun 8 2019 18:33:13