11#ifndef EIGEN_MATRIX_POWER
12#define EIGEN_MATRIX_POWER
15#include "./InternalHeaderCheck.h"
19template <
typename MatrixType>
43template <
typename MatrixType>
46 using RealScalar =
typename MatrixType::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 using Scalar =
typename MatrixType::Scalar;
94 using RealScalar =
typename MatrixType::RealScalar;
95 using ComplexScalar = internal::make_complex_t<Scalar>;
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_INTERNAL_ASSERT_SQUARE(T);
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);
153 res.coeffRef(1, 0) = Scalar(0);
160template <
typename MatrixType>
161void MatrixPowerAtomic<MatrixType>::computePade(
int degree,
const MatrixType& IminusT, ResultType& res)
const {
163 res = (m_p - RealScalar(degree)) / RealScalar(2 * i - 2) * IminusT;
166 res = (MatrixType::Identity(IminusT.rows(), IminusT.cols()) + res)
167 .
template triangularView<Upper>()
168 .solve((i == 1 ? -m_p
169 : i & 1 ? (-m_p - RealScalar(i / 2)) / RealScalar(2 * i)
170 : (m_p - RealScalar(i / 2)) / RealScalar(2 * i - 2)) *
174 res += MatrixType::Identity(IminusT.rows(), IminusT.cols());
178template <
typename MatrixType>
179void MatrixPowerAtomic<MatrixType>::compute2x2(ResultType& res, RealScalar p)
const {
182 res.coeffRef(0, 0) = pow(m_A.coeff(0, 0), p);
184 for (Index i = 1; i < m_A.cols(); ++i) {
185 res.coeffRef(i, i) = pow(m_A.coeff(i, i), p);
186 Scalar a = m_A.coeff(i - 1, i - 1);
187 Scalar b = m_A.coeff(i, i);
191 if (abs(diff) <= RealScalar(2) * (std::numeric_limits<RealScalar>::epsilon)() * (std::max)(abs(a), abs(b)))
192 res.coeffRef(i - 1, i) = p * pow(b, p - 1);
193 else if (2 * abs(a) < abs(b) || 2 * abs(b) < abs(a))
194 res.coeffRef(i - 1, i) = (res.coeff(i, i) - res.coeff(i - 1, i - 1)) / diff;
196 res.coeffRef(i - 1, i) = computeSuperDiag(b, a, p);
197 res.coeffRef(i - 1, i) *= m_A.coeff(i - 1, i);
201template <
typename MatrixType>
202void MatrixPowerAtomic<MatrixType>::computeBig(ResultType& res)
const {
204 const int digits = std::numeric_limits<RealScalar>::digits;
205 const RealScalar maxNormForPade =
206 RealScalar(digits <= 24 ? 4.3386528e-1L
207 : digits <= 53 ? 2.789358995219730e-1L
208 : digits <= 64 ? 2.4471944416607995472e-1L
209 : digits <= 106 ? 1.1016843812851143391275867258512e-1L
210 : 9.134603732914548552537150753385375e-2L);
211 MatrixType IminusT, sqrtT, T = m_A.template triangularView<Upper>();
212 RealScalar normIminusT;
213 int degree, degree2, numberOfSquareRoots = 0;
214 bool hasExtraSquareRoot =
false;
216 for (Index i = 0; i < m_A.cols(); ++i) eigen_assert(m_A(i, i) != RealScalar(0));
221 if (!T.allFinite()) {
225 IminusT = MatrixType::Identity(m_A.rows(), m_A.cols()) - T;
226 normIminusT = IminusT.cwiseAbs().colwise().sum().maxCoeff();
227 if (normIminusT < maxNormForPade) {
228 degree = getPadeDegree(normIminusT);
229 degree2 = getPadeDegree(normIminusT / 2);
230 if (degree - degree2 <= 1 || hasExtraSquareRoot)
break;
231 hasExtraSquareRoot =
true;
234 T = sqrtT.template triangularView<Upper>();
235 ++numberOfSquareRoots;
237 computePade(degree, IminusT, res);
239 for (; numberOfSquareRoots; --numberOfSquareRoots) {
240 compute2x2(res, ldexp(m_p, -numberOfSquareRoots));
241 res = res.template triangularView<Upper>() * res;
243 compute2x2(res, m_p);
246template <
typename MatrixType>
247inline int MatrixPowerAtomic<MatrixType>::getPadeDegree(
float normIminusT) {
248 const float maxNormForPade[] = {2.8064004e-1f , 4.3386528e-1f};
250 for (; degree <= 4; ++degree)
251 if (normIminusT <= maxNormForPade[degree - 3])
break;
255template <
typename MatrixType>
256inline int MatrixPowerAtomic<MatrixType>::getPadeDegree(
double normIminusT) {
257 const double maxNormForPade[] = {1.884160592658218e-2 , 6.038881904059573e-2, 1.239917516308172e-1,
258 1.999045567181744e-1, 2.789358995219730e-1};
260 for (; degree <= 7; ++degree)
261 if (normIminusT <= maxNormForPade[degree - 3])
break;
265template <
typename MatrixType>
266inline int MatrixPowerAtomic<MatrixType>::getPadeDegree(
long double normIminusT) {
267#if LDBL_MANT_DIG == 53
268 const int maxPadeDegree = 7;
269 const double maxNormForPade[] = {1.884160592658218e-2L , 6.038881904059573e-2L, 1.239917516308172e-1L,
270 1.999045567181744e-1L, 2.789358995219730e-1L};
271#elif LDBL_MANT_DIG <= 64
272 const int maxPadeDegree = 8;
273 const long double maxNormForPade[] = {6.3854693117491799460e-3L ,
274 2.6394893435456973676e-2L,
275 6.4216043030404063729e-2L,
276 1.1701165502926694307e-1L,
277 1.7904284231268670284e-1L,
278 2.4471944416607995472e-1L};
279#elif LDBL_MANT_DIG <= 106
280 const int maxPadeDegree = 10;
281 const double maxNormForPade[] = {1.0007161601787493236741409687186e-4L ,
282 1.0007161601787493236741409687186e-3L,
283 4.7069769360887572939882574746264e-3L,
284 1.3220386624169159689406653101695e-2L,
285 2.8063482381631737920612944054906e-2L,
286 4.9625993951953473052385361085058e-2L,
287 7.7367040706027886224557538328171e-2L,
288 1.1016843812851143391275867258512e-1L};
290 const int maxPadeDegree = 10;
291 const double maxNormForPade[] = {5.524506147036624377378713555116378e-5L ,
292 6.640600568157479679823602193345995e-4L,
293 3.227716520106894279249709728084626e-3L,
294 9.619593944683432960546978734646284e-3L,
295 2.134595382433742403911124458161147e-2L,
296 3.908166513900489428442993794761185e-2L,
297 6.266780814639442865832535460550138e-2L,
298 9.134603732914548552537150753385375e-2L};
301 for (; degree <= maxPadeDegree; ++degree)
302 if (normIminusT <=
static_cast<long double>(maxNormForPade[degree - 3]))
break;
306template <
typename MatrixType>
307inline typename MatrixPowerAtomic<MatrixType>::ComplexScalar MatrixPowerAtomic<MatrixType>::computeSuperDiag(
308 const ComplexScalar& curr,
const ComplexScalar& prev, RealScalar p) {
314 ComplexScalar logCurr = log(curr);
315 ComplexScalar logPrev = log(prev);
316 RealScalar unwindingNumber =
317 ceil((numext::imag(logCurr - logPrev) - RealScalar(EIGEN_PI)) / RealScalar(2 * EIGEN_PI));
319 numext::log1p((curr - prev) / prev) / RealScalar(2) + ComplexScalar(0, RealScalar(EIGEN_PI) * unwindingNumber);
320 return RealScalar(2) * exp(RealScalar(0.5) * p * (logCurr + logPrev)) * sinh(p * w) / (curr - prev);
323template <
typename MatrixType>
324inline typename MatrixPowerAtomic<MatrixType>::RealScalar MatrixPowerAtomic<MatrixType>::computeSuperDiag(
325 RealScalar curr, RealScalar prev, RealScalar p) {
330 RealScalar w = numext::log1p((curr - prev) / prev) / RealScalar(2);
331 return 2 * exp(p * (log(curr) + log(prev)) / 2) * sinh(p * w) / (curr - prev);
353template <
typename MatrixType>
356 using Scalar =
typename MatrixType::Scalar;
357 using RealScalar =
typename MatrixType::RealScalar;
360 MatrixPower(
const MatrixPower&) =
delete;
361 MatrixPower& operator=(
const MatrixPower&) =
delete;
371 explicit MatrixPower(
const MatrixType& A) : m_A(A), m_conditionNumber(0), m_rank(A.cols()), m_nulls(0) {
372 EIGEN_INTERNAL_ASSERT_SQUARE(A);
393 template <
typename ResultType>
394 void compute(ResultType& res, RealScalar p);
396 Index rows()
const {
return m_A.rows(); }
397 Index cols()
const {
return m_A.cols(); }
400 using ComplexScalar = internal::make_complex_t<Scalar>;
401 using ComplexMatrix =
402 Matrix<ComplexScalar, Dynamic, Dynamic, 0, MatrixType::RowsAtCompileTime, MatrixType::ColsAtCompileTime>;
405 typename MatrixType::Nested m_A;
411 ComplexMatrix m_T, m_U;
422 RealScalar m_conditionNumber;
439 void split(RealScalar& p, RealScalar& intpart);
444 template <
typename ResultType>
445 void computeIntPower(ResultType& res, RealScalar p);
447 template <
typename ResultType>
448 void computeFracPower(ResultType& res, RealScalar p);
450 template <
int Rows,
int Cols,
int Options,
int MaxRows,
int MaxCols>
451 static void revertSchur(Matrix<ComplexScalar, Rows, Cols, Options, MaxRows, MaxCols>& res,
const ComplexMatrix& T,
452 const ComplexMatrix& U);
454 template <
int Rows,
int Cols,
int Options,
int MaxRows,
int MaxCols>
455 static void revertSchur(Matrix<RealScalar, Rows, Cols, Options, MaxRows, MaxCols>& res,
const ComplexMatrix& T,
456 const ComplexMatrix& U);
459template <
typename MatrixType>
460template <
typename ResultType>
469 res(0, 0) = pow(m_A.coeff(0, 0), p);
473 if (!(numext::isfinite)(p)) {
480 res = MatrixType::Identity(rows(), cols());
481 computeIntPower(res, intpart);
482 if (p) computeFracPower(res, p);
486template <
typename MatrixType>
487void MatrixPower<MatrixType>::split(RealScalar& p, RealScalar& intpart) {
496 if (!m_conditionNumber && p) initialize();
499 if (p > RealScalar(0.5) && p > (1 - p) * pow(m_conditionNumber, p)) {
505template <
typename MatrixType>
506void MatrixPower<MatrixType>::initialize() {
509 ComplexScalar eigenvalue;
511 m_fT.resizeLike(m_A);
512 m_T = schurOfA.matrixT();
513 m_U = schurOfA.matrixU();
514 m_conditionNumber = m_T.diagonal().array().abs().maxCoeff() / m_T.diagonal().array().abs().minCoeff();
517 for (Index i = cols() - 1; i >= 0; --i) {
518 if (m_T.coeff(i, i) == RealScalar(0)) {
519 for (Index j = i + 1; j < m_rank; ++j) {
520 eigenvalue = m_T.coeff(j, j);
521 rot.makeGivens(m_T.coeff(j - 1, j), eigenvalue);
522 m_T.applyOnTheRight(j - 1, j, rot);
523 m_T.applyOnTheLeft(j - 1, j, rot.adjoint());
524 m_T.coeffRef(j - 1, j - 1) = eigenvalue;
525 m_T.coeffRef(j, j) = RealScalar(0);
526 m_U.applyOnTheRight(j - 1, j, rot);
532 m_nulls = rows() - m_rank;
534 eigen_assert(m_T.bottomRightCorner(m_nulls, m_nulls).isZero() &&
535 "Base of matrix power should be invertible or with a semisimple zero eigenvalue.");
536 m_fT.bottomRows(m_nulls).fill(RealScalar(0));
540template <
typename MatrixType>
541template <
typename ResultType>
542void MatrixPower<MatrixType>::computeIntPower(ResultType& res, RealScalar p) {
545 RealScalar pp = abs(p);
548 m_tmp = m_A.inverse();
553 if (fmod(pp, 2) >= 1) res = m_tmp * res;
560template <
typename MatrixType>
561template <
typename ResultType>
562void MatrixPower<MatrixType>::computeFracPower(ResultType& res, RealScalar p) {
564 eigen_assert(m_conditionNumber);
565 eigen_assert(m_rank + m_nulls == rows());
568 if (m_nulls && m_rank) {
573 const RealScalar scale = pow(m_T.diagonal().head(m_rank).cwiseAbs().minCoeff(), 1 - p);
574 m_fT.topRightCorner(m_rank, m_nulls).noalias() =
575 m_T.topLeftCorner(m_rank, m_rank).template triangularView<Upper>().template solve<OnTheRight>(scale * blockTp) *
576 m_T.topRightCorner(m_rank, m_nulls);
577 m_fT.topRightCorner(m_rank, m_nulls) /= scale;
579 revertSchur(m_tmp, m_fT, m_U);
583template <
typename MatrixType>
584template <
int Rows,
int Cols,
int Options,
int MaxRows,
int MaxCols>
586 const ComplexMatrix& T,
const ComplexMatrix& U) {
587 res.noalias() = U * (T.template triangularView<Upper>() * U.adjoint());
590template <
typename MatrixType>
591template <
int Rows,
int Cols,
int Options,
int MaxRows,
int MaxCols>
593 const ComplexMatrix& T,
const ComplexMatrix& U) {
594 res.noalias() = (U * (T.template triangularView<Upper>() * U.adjoint())).real();
610template <
typename Derived>
613 using PlainObject =
typename Derived::PlainObject;
614 using RealScalar =
typename Derived::RealScalar;
630 template <
typename ResultType>
631 inline void evalTo(ResultType& result)
const {
635 Index rows()
const {
return m_A.rows(); }
636 Index cols()
const {
return m_A.cols(); }
640 const RealScalar m_p;
656template <
typename Derived>
659 using PlainObject =
typename Derived::PlainObject;
660 using ComplexScalar = internal::make_complex_t<typename Derived::Scalar>;
679 template <
typename ResultType>
680 inline void evalTo(ResultType& result)
const {
681 result = (m_p * m_A.log()).exp();
684 Index rows()
const {
return m_A.rows(); }
685 Index cols()
const {
return m_A.cols(); }
689 const ComplexScalar m_p;
694template <
typename MatrixPowerType>
695struct traits<MatrixPowerParenthesesReturnValue<MatrixPowerType> > {
696 using ReturnType =
typename MatrixPowerType::PlainObject;
699template <
typename Derived>
700struct traits<MatrixPowerReturnValue<Derived> > {
701 using ReturnType =
typename Derived::PlainObject;
704template <
typename Derived>
705struct traits<MatrixComplexPowerReturnValue<Derived> > {
706 using ReturnType =
typename Derived::PlainObject;
711template <
typename Derived>
716template <
typename Derived>
const MatrixComplexPowerReturnValue< Derived > pow(const internal::make_complex_t< Scalar > &p) const
Definition MatrixPower.h:717
Proxy for the matrix power of some matrix (expression).
Definition MatrixPower.h:657
MatrixComplexPowerReturnValue(const Derived &A, const ComplexScalar &p)
Constructor.
Definition MatrixPower.h:668
void evalTo(ResultType &result) const
Compute the matrix power.
Definition MatrixPower.h:680
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:611
MatrixPowerReturnValue(const Derived &A, RealScalar p)
Constructor.
Definition MatrixPower.h:622
void evalTo(ResultType &result) const
Compute the matrix power.
Definition MatrixPower.h:631
Class for computing matrix powers.
Definition MatrixPower.h:354
MatrixPower(const MatrixType &A)
Constructor.
Definition MatrixPower.h:371
const MatrixPowerParenthesesReturnValue< MatrixType > operator()(RealScalar p)
Returns the matrix power.
Definition MatrixPower.h:382
void compute(ResultType &res, RealScalar p)
Compute the matrix power.
Definition MatrixPower.h:461
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.