11#ifndef EIGEN_MATRIX_POWER
12#define EIGEN_MATRIX_POWER
15#include "./InternalHeaderCheck.h"
19template <
typename MatrixType>
43template <
typename MatrixType>
46 typedef typename MatrixType::RealScalar RealScalar;
61 template <
typename ResultType>
62 inline void evalTo(ResultType& result)
const {
63 m_pow.compute(result, m_p);
66 Index rows()
const {
return m_pow.rows(); }
67 Index cols()
const {
return m_pow.cols(); }
70 MatrixPower<MatrixType>& m_pow;
89template <
typename MatrixType>
90class MatrixPowerAtomic {
92 enum { RowsAtCompileTime = MatrixType::RowsAtCompileTime, MaxRowsAtCompileTime = MatrixType::MaxRowsAtCompileTime };
93 typedef typename MatrixType::Scalar Scalar;
94 typedef typename MatrixType::RealScalar RealScalar;
95 typedef internal::make_complex_t<Scalar> ComplexScalar;
98 const MatrixType& m_A;
101 void computePade(
int degree,
const MatrixType& IminusT, ResultType& res)
const;
102 void compute2x2(ResultType& res, RealScalar p)
const;
103 void computeBig(ResultType& res)
const;
104 static int getPadeDegree(
float normIminusT);
105 static int getPadeDegree(
double normIminusT);
106 static int getPadeDegree(
long double normIminusT);
107 static ComplexScalar computeSuperDiag(
const ComplexScalar&,
const ComplexScalar&, RealScalar p);
108 static RealScalar computeSuperDiag(RealScalar, RealScalar, RealScalar p);
111 MatrixPowerAtomic(
const MatrixPowerAtomic&) =
delete;
112 MatrixPowerAtomic& operator=(
const MatrixPowerAtomic&) =
delete;
125 MatrixPowerAtomic(
const MatrixType& T, RealScalar p);
133 void compute(ResultType& res)
const;
136template <
typename MatrixType>
137MatrixPowerAtomic<MatrixType>::MatrixPowerAtomic(
const MatrixType& T, RealScalar p) : m_A(T), m_p(p) {
138 eigen_assert(T.rows() == T.cols());
139 eigen_assert(p > -1 && p < 1);
142template <
typename MatrixType>
145 switch (m_A.rows()) {
149 res(0, 0) = pow(m_A(0, 0), m_p);
152 compute2x2(res, m_p);
159template <
typename MatrixType>
160void MatrixPowerAtomic<MatrixType>::computePade(
int degree,
const MatrixType& IminusT, ResultType& res)
const {
162 res = (m_p - RealScalar(degree)) / RealScalar(2 * i - 2) * IminusT;
165 res = (MatrixType::Identity(IminusT.rows(), IminusT.cols()) + res)
166 .
template triangularView<Upper>()
167 .solve((i == 1 ? -m_p
168 : i & 1 ? (-m_p - RealScalar(i / 2)) / RealScalar(2 * i)
169 : (m_p - RealScalar(i / 2)) / RealScalar(2 * i - 2)) *
173 res += MatrixType::Identity(IminusT.rows(), IminusT.cols());
177template <
typename MatrixType>
178void MatrixPowerAtomic<MatrixType>::compute2x2(ResultType& res, RealScalar p)
const {
181 res.coeffRef(0, 0) = pow(m_A.coeff(0, 0), p);
183 for (Index i = 1; i < m_A.cols(); ++i) {
184 res.coeffRef(i, i) = pow(m_A.coeff(i, i), p);
185 Scalar a = m_A.coeff(i - 1, i - 1);
186 Scalar b = m_A.coeff(i, i);
190 if (abs(diff) <= RealScalar(2) * (std::numeric_limits<RealScalar>::epsilon)() * (std::max)(abs(a), abs(b)))
191 res.coeffRef(i - 1, i) = p * pow(b, p - 1);
192 else if (2 * abs(a) < abs(b) || 2 * abs(b) < abs(a))
193 res.coeffRef(i - 1, i) = (res.coeff(i, i) - res.coeff(i - 1, i - 1)) / diff;
195 res.coeffRef(i - 1, i) = computeSuperDiag(b, a, p);
196 res.coeffRef(i - 1, i) *= m_A.coeff(i - 1, i);
200template <
typename MatrixType>
201void MatrixPowerAtomic<MatrixType>::computeBig(ResultType& res)
const {
203 const int digits = std::numeric_limits<RealScalar>::digits;
204 const RealScalar maxNormForPade =
205 RealScalar(digits <= 24 ? 4.3386528e-1L
206 : digits <= 53 ? 2.789358995219730e-1L
207 : digits <= 64 ? 2.4471944416607995472e-1L
208 : digits <= 106 ? 1.1016843812851143391275867258512e-1L
209 : 9.134603732914548552537150753385375e-2L);
210 MatrixType IminusT, sqrtT, T = m_A.template triangularView<Upper>();
211 RealScalar normIminusT;
212 int degree, degree2, numberOfSquareRoots = 0;
213 bool hasExtraSquareRoot =
false;
215 for (Index i = 0; i < m_A.cols(); ++i) eigen_assert(m_A(i, i) != RealScalar(0));
218 IminusT = MatrixType::Identity(m_A.rows(), m_A.cols()) - T;
219 normIminusT = IminusT.cwiseAbs().colwise().sum().maxCoeff();
220 if (normIminusT < maxNormForPade) {
221 degree = getPadeDegree(normIminusT);
222 degree2 = getPadeDegree(normIminusT / 2);
223 if (degree - degree2 <= 1 || hasExtraSquareRoot)
break;
224 hasExtraSquareRoot =
true;
227 T = sqrtT.template triangularView<Upper>();
228 ++numberOfSquareRoots;
230 computePade(degree, IminusT, res);
232 for (; numberOfSquareRoots; --numberOfSquareRoots) {
233 compute2x2(res, ldexp(m_p, -numberOfSquareRoots));
234 res = res.template triangularView<Upper>() * res;
236 compute2x2(res, m_p);
239template <
typename MatrixType>
240inline int MatrixPowerAtomic<MatrixType>::getPadeDegree(
float normIminusT) {
241 const float maxNormForPade[] = {2.8064004e-1f , 4.3386528e-1f};
243 for (; degree <= 4; ++degree)
244 if (normIminusT <= maxNormForPade[degree - 3])
break;
248template <
typename MatrixType>
249inline int MatrixPowerAtomic<MatrixType>::getPadeDegree(
double normIminusT) {
250 const double maxNormForPade[] = {1.884160592658218e-2 , 6.038881904059573e-2, 1.239917516308172e-1,
251 1.999045567181744e-1, 2.789358995219730e-1};
253 for (; degree <= 7; ++degree)
254 if (normIminusT <= maxNormForPade[degree - 3])
break;
258template <
typename MatrixType>
259inline int MatrixPowerAtomic<MatrixType>::getPadeDegree(
long double normIminusT) {
260#if LDBL_MANT_DIG == 53
261 const int maxPadeDegree = 7;
262 const double maxNormForPade[] = {1.884160592658218e-2L , 6.038881904059573e-2L, 1.239917516308172e-1L,
263 1.999045567181744e-1L, 2.789358995219730e-1L};
264#elif LDBL_MANT_DIG <= 64
265 const int maxPadeDegree = 8;
266 const long double maxNormForPade[] = {6.3854693117491799460e-3L ,
267 2.6394893435456973676e-2L,
268 6.4216043030404063729e-2L,
269 1.1701165502926694307e-1L,
270 1.7904284231268670284e-1L,
271 2.4471944416607995472e-1L};
272#elif LDBL_MANT_DIG <= 106
273 const int maxPadeDegree = 10;
274 const double maxNormForPade[] = {1.0007161601787493236741409687186e-4L ,
275 1.0007161601787493236741409687186e-3L,
276 4.7069769360887572939882574746264e-3L,
277 1.3220386624169159689406653101695e-2L,
278 2.8063482381631737920612944054906e-2L,
279 4.9625993951953473052385361085058e-2L,
280 7.7367040706027886224557538328171e-2L,
281 1.1016843812851143391275867258512e-1L};
283 const int maxPadeDegree = 10;
284 const double maxNormForPade[] = {5.524506147036624377378713555116378e-5L ,
285 6.640600568157479679823602193345995e-4L,
286 3.227716520106894279249709728084626e-3L,
287 9.619593944683432960546978734646284e-3L,
288 2.134595382433742403911124458161147e-2L,
289 3.908166513900489428442993794761185e-2L,
290 6.266780814639442865832535460550138e-2L,
291 9.134603732914548552537150753385375e-2L};
294 for (; degree <= maxPadeDegree; ++degree)
295 if (normIminusT <=
static_cast<long double>(maxNormForPade[degree - 3]))
break;
299template <
typename MatrixType>
300inline typename MatrixPowerAtomic<MatrixType>::ComplexScalar MatrixPowerAtomic<MatrixType>::computeSuperDiag(
301 const ComplexScalar& curr,
const ComplexScalar& prev, RealScalar p) {
307 ComplexScalar logCurr = log(curr);
308 ComplexScalar logPrev = log(prev);
309 RealScalar unwindingNumber =
310 ceil((numext::imag(logCurr - logPrev) - RealScalar(EIGEN_PI)) / RealScalar(2 * EIGEN_PI));
312 numext::log1p((curr - prev) / prev) / RealScalar(2) + ComplexScalar(0, RealScalar(EIGEN_PI) * unwindingNumber);
313 return RealScalar(2) * exp(RealScalar(0.5) * p * (logCurr + logPrev)) * sinh(p * w) / (curr - prev);
316template <
typename MatrixType>
317inline typename MatrixPowerAtomic<MatrixType>::RealScalar MatrixPowerAtomic<MatrixType>::computeSuperDiag(
318 RealScalar curr, RealScalar prev, RealScalar p) {
323 RealScalar w = numext::log1p((curr - prev) / prev) / RealScalar(2);
324 return 2 * exp(p * (log(curr) + log(prev)) / 2) * sinh(p * w) / (curr - prev);
346template <
typename MatrixType>
349 typedef typename MatrixType::Scalar Scalar;
350 typedef typename MatrixType::RealScalar RealScalar;
353 MatrixPower(
const MatrixPower&) =
delete;
354 MatrixPower& operator=(
const MatrixPower&) =
delete;
364 explicit MatrixPower(
const MatrixType& A) : m_A(A), m_conditionNumber(0), m_rank(A.cols()), m_nulls(0) {
365 eigen_assert(A.rows() == A.cols());
386 template <
typename ResultType>
387 void compute(ResultType& res, RealScalar p);
389 Index rows()
const {
return m_A.rows(); }
390 Index cols()
const {
return m_A.cols(); }
393 typedef internal::make_complex_t<Scalar> ComplexScalar;
394 typedef Matrix<ComplexScalar, Dynamic, Dynamic, 0, MatrixType::RowsAtCompileTime, MatrixType::ColsAtCompileTime>
398 typename MatrixType::Nested m_A;
404 ComplexMatrix m_T, m_U;
415 RealScalar m_conditionNumber;
432 void split(RealScalar& p, RealScalar& intpart);
437 template <
typename ResultType>
438 void computeIntPower(ResultType& res, RealScalar p);
440 template <
typename ResultType>
441 void computeFracPower(ResultType& res, RealScalar p);
443 template <
int Rows,
int Cols,
int Options,
int MaxRows,
int MaxCols>
444 static void revertSchur(Matrix<ComplexScalar, Rows, Cols, Options, MaxRows, MaxCols>& res,
const ComplexMatrix& T,
445 const ComplexMatrix& U);
447 template <
int Rows,
int Cols,
int Options,
int MaxRows,
int MaxCols>
448 static void revertSchur(Matrix<RealScalar, Rows, Cols, Options, MaxRows, MaxCols>& res,
const ComplexMatrix& T,
449 const ComplexMatrix& U);
452template <
typename MatrixType>
453template <
typename ResultType>
460 res(0, 0) = pow(m_A.coeff(0, 0), p);
466 res = MatrixType::Identity(rows(), cols());
467 computeIntPower(res, intpart);
468 if (p) computeFracPower(res, p);
472template <
typename MatrixType>
473void MatrixPower<MatrixType>::split(RealScalar& p, RealScalar& intpart) {
482 if (!m_conditionNumber && p) initialize();
485 if (p > RealScalar(0.5) && p > (1 - p) * pow(m_conditionNumber, p)) {
491template <
typename MatrixType>
492void MatrixPower<MatrixType>::initialize() {
495 ComplexScalar eigenvalue;
497 m_fT.resizeLike(m_A);
498 m_T = schurOfA.matrixT();
499 m_U = schurOfA.matrixU();
500 m_conditionNumber = m_T.diagonal().array().abs().maxCoeff() / m_T.diagonal().array().abs().minCoeff();
503 for (Index i = cols() - 1; i >= 0; --i) {
504 if (m_rank <= 2)
return;
505 if (m_T.coeff(i, i) == RealScalar(0)) {
506 for (Index j = i + 1; j < m_rank; ++j) {
507 eigenvalue = m_T.coeff(j, j);
508 rot.makeGivens(m_T.coeff(j - 1, j), eigenvalue);
509 m_T.applyOnTheRight(j - 1, j, rot);
510 m_T.applyOnTheLeft(j - 1, j, rot.adjoint());
511 m_T.coeffRef(j - 1, j - 1) = eigenvalue;
512 m_T.coeffRef(j, j) = RealScalar(0);
513 m_U.applyOnTheRight(j - 1, j, rot);
519 m_nulls = rows() - m_rank;
521 eigen_assert(m_T.bottomRightCorner(m_nulls, m_nulls).isZero() &&
522 "Base of matrix power should be invertible or with a semisimple zero eigenvalue.");
523 m_fT.bottomRows(m_nulls).fill(RealScalar(0));
527template <
typename MatrixType>
528template <
typename ResultType>
529void MatrixPower<MatrixType>::computeIntPower(ResultType& res, RealScalar p) {
532 RealScalar pp = abs(p);
535 m_tmp = m_A.inverse();
540 if (fmod(pp, 2) >= 1) res = m_tmp * res;
547template <
typename MatrixType>
548template <
typename ResultType>
549void MatrixPower<MatrixType>::computeFracPower(ResultType& res, RealScalar p) {
551 eigen_assert(m_conditionNumber);
552 eigen_assert(m_rank + m_nulls == rows());
556 m_fT.topRightCorner(m_rank, m_nulls) = m_T.topLeftCorner(m_rank, m_rank)
557 .template triangularView<Upper>()
558 .solve(blockTp * m_T.topRightCorner(m_rank, m_nulls));
560 revertSchur(m_tmp, m_fT, m_U);
564template <
typename MatrixType>
565template <
int Rows,
int Cols,
int Options,
int MaxRows,
int MaxCols>
567 const ComplexMatrix& T,
const ComplexMatrix& U) {
568 res.noalias() = U * (T.template triangularView<Upper>() * U.adjoint());
571template <
typename MatrixType>
572template <
int Rows,
int Cols,
int Options,
int MaxRows,
int MaxCols>
574 const ComplexMatrix& T,
const ComplexMatrix& U) {
575 res.noalias() = (U * (T.template triangularView<Upper>() * U.adjoint())).real();
591template <
typename Derived>
594 typedef typename Derived::PlainObject PlainObject;
595 typedef typename Derived::RealScalar RealScalar;
611 template <
typename ResultType>
612 inline void evalTo(ResultType& result)
const {
616 Index rows()
const {
return m_A.rows(); }
617 Index cols()
const {
return m_A.cols(); }
621 const RealScalar m_p;
637template <
typename Derived>
640 typedef typename Derived::PlainObject PlainObject;
641 typedef internal::make_complex_t<typename Derived::Scalar> ComplexScalar;
660 template <
typename ResultType>
661 inline void evalTo(ResultType& result)
const {
662 result = (m_p * m_A.log()).exp();
665 Index rows()
const {
return m_A.rows(); }
666 Index cols()
const {
return m_A.cols(); }
670 const ComplexScalar m_p;
675template <
typename MatrixPowerType>
676struct traits<MatrixPowerParenthesesReturnValue<MatrixPowerType> > {
677 typedef typename MatrixPowerType::PlainObject ReturnType;
680template <
typename Derived>
681struct traits<MatrixPowerReturnValue<Derived> > {
682 typedef typename Derived::PlainObject ReturnType;
685template <
typename Derived>
686struct traits<MatrixComplexPowerReturnValue<Derived> > {
687 typedef typename Derived::PlainObject ReturnType;
692template <
typename Derived>
697template <
typename Derived>
const MatrixComplexPowerReturnValue< Derived > pow(const internal::make_complex_t< Scalar > &p) const
Definition MatrixPower.h:698
Proxy for the matrix power of some matrix (expression).
Definition MatrixPower.h:638
MatrixComplexPowerReturnValue(const Derived &A, const ComplexScalar &p)
Constructor.
Definition MatrixPower.h:649
void evalTo(ResultType &result) const
Compute the matrix power.
Definition MatrixPower.h:661
Class for computing matrix powers.
Definition MatrixPower.h:90
void compute(ResultType &res) const
Compute the matrix power.
Definition MatrixPower.h:143
Proxy for the matrix power of some matrix.
Definition MatrixPower.h:44
MatrixPowerParenthesesReturnValue(MatrixPower< MatrixType > &pow, RealScalar p)
Constructor.
Definition MatrixPower.h:54
void evalTo(ResultType &result) const
Compute the matrix power.
Definition MatrixPower.h:62
Proxy for the matrix power of some matrix (expression).
Definition MatrixPower.h:592
MatrixPowerReturnValue(const Derived &A, RealScalar p)
Constructor.
Definition MatrixPower.h:603
void evalTo(ResultType &result) const
Compute the matrix power.
Definition MatrixPower.h:612
Class for computing matrix powers.
Definition MatrixPower.h:347
MatrixPower(const MatrixType &A)
Constructor.
Definition MatrixPower.h:364
const MatrixPowerParenthesesReturnValue< MatrixType > operator()(RealScalar p)
Returns the matrix power.
Definition MatrixPower.h:375
void compute(ResultType &res, RealScalar p)
Compute the matrix power.
Definition MatrixPower.h:454
void matrix_sqrt_triangular(const MatrixType &arg, ResultType &result)
Compute matrix square root of triangular matrix.
Definition MatrixSquareRoot.h:194
Namespace containing all symbols from the Eigen library.