10#ifndef EIGEN_MATRIX_POWER
11#define EIGEN_MATRIX_POWER
14#include "./InternalHeaderCheck.h"
18template <
typename MatrixType>
42template <
typename MatrixType>
45 typedef typename MatrixType::RealScalar RealScalar;
60 template <
typename ResultType>
61 inline void evalTo(ResultType& result)
const {
62 m_pow.compute(result, m_p);
65 Index rows()
const {
return m_pow.rows(); }
66 Index cols()
const {
return m_pow.cols(); }
69 MatrixPower<MatrixType>& m_pow;
88template <
typename MatrixType>
91 enum { RowsAtCompileTime = MatrixType::RowsAtCompileTime, MaxRowsAtCompileTime = MatrixType::MaxRowsAtCompileTime };
92 typedef typename MatrixType::Scalar Scalar;
93 typedef typename MatrixType::RealScalar RealScalar;
94 typedef internal::make_complex_t<Scalar> ComplexScalar;
97 const MatrixType& m_A;
100 void computePade(
int degree,
const MatrixType& IminusT, ResultType& res)
const;
101 void compute2x2(ResultType& res, RealScalar p)
const;
102 void computeBig(ResultType& res)
const;
103 static int getPadeDegree(
float normIminusT);
104 static int getPadeDegree(
double normIminusT);
105 static int getPadeDegree(
long double normIminusT);
106 static ComplexScalar computeSuperDiag(
const ComplexScalar&,
const ComplexScalar&, RealScalar p);
107 static RealScalar computeSuperDiag(RealScalar, RealScalar, RealScalar p);
129 void compute(ResultType& res)
const;
132template <
typename MatrixType>
134 eigen_assert(T.rows() == T.cols());
135 eigen_assert(p > -1 && p < 1);
138template <
typename MatrixType>
141 switch (m_A.rows()) {
145 res(0, 0) = pow(m_A(0, 0), m_p);
148 compute2x2(res, m_p);
155template <
typename MatrixType>
156void MatrixPowerAtomic<MatrixType>::computePade(
int degree,
const MatrixType& IminusT, ResultType& res)
const {
158 res = (m_p - RealScalar(degree)) / RealScalar(2 * i - 2) * IminusT;
161 res = (MatrixType::Identity(IminusT.rows(), IminusT.cols()) + res)
162 .
template triangularView<Upper>()
163 .solve((i == 1 ? -m_p
164 : i & 1 ? (-m_p - RealScalar(i / 2)) / RealScalar(2 * i)
165 : (m_p - RealScalar(i / 2)) / RealScalar(2 * i - 2)) *
169 res += MatrixType::Identity(IminusT.rows(), IminusT.cols());
173template <
typename MatrixType>
174void MatrixPowerAtomic<MatrixType>::compute2x2(ResultType& res, RealScalar p)
const {
177 res.coeffRef(0, 0) = pow(m_A.coeff(0, 0), p);
179 for (
Index i = 1; i < m_A.cols(); ++i) {
180 res.coeffRef(i, i) = pow(m_A.coeff(i, i), p);
181 if (m_A.coeff(i - 1, i - 1) == m_A.coeff(i, i))
182 res.coeffRef(i - 1, i) = p * pow(m_A.coeff(i, i), p - 1);
183 else if (2 *
abs(m_A.coeff(i - 1, i - 1)) <
abs(m_A.coeff(i, i)) ||
184 2 *
abs(m_A.coeff(i, i)) <
abs(m_A.coeff(i - 1, i - 1)))
185 res.coeffRef(i - 1, i) =
186 (res.coeff(i, i) - res.coeff(i - 1, i - 1)) / (m_A.coeff(i, i) - m_A.coeff(i - 1, i - 1));
188 res.coeffRef(i - 1, i) = computeSuperDiag(m_A.coeff(i, i), m_A.coeff(i - 1, i - 1), p);
189 res.coeffRef(i - 1, i) *= m_A.coeff(i - 1, i);
193template <
typename MatrixType>
194void MatrixPowerAtomic<MatrixType>::computeBig(ResultType& res)
const {
196 const int digits = std::numeric_limits<RealScalar>::digits;
197 const RealScalar maxNormForPade =
198 RealScalar(digits <= 24 ? 4.3386528e-1L
199 : digits <= 53 ? 2.789358995219730e-1L
200 : digits <= 64 ? 2.4471944416607995472e-1L
201 : digits <= 106 ? 1.1016843812851143391275867258512e-1L
202 : 9.134603732914548552537150753385375e-2L);
203 MatrixType IminusT, sqrtT, T = m_A.template triangularView<Upper>();
204 RealScalar normIminusT;
205 int degree, degree2, numberOfSquareRoots = 0;
206 bool hasExtraSquareRoot =
false;
208 for (
Index i = 0; i < m_A.cols(); ++i) eigen_assert(m_A(i, i) != RealScalar(0));
211 IminusT = MatrixType::Identity(m_A.rows(), m_A.cols()) - T;
212 normIminusT = IminusT.cwiseAbs().colwise().sum().maxCoeff();
213 if (normIminusT < maxNormForPade) {
214 degree = getPadeDegree(normIminusT);
215 degree2 = getPadeDegree(normIminusT / 2);
216 if (degree - degree2 <= 1 || hasExtraSquareRoot)
break;
217 hasExtraSquareRoot =
true;
220 T = sqrtT.template triangularView<Upper>();
221 ++numberOfSquareRoots;
223 computePade(degree, IminusT, res);
225 for (; numberOfSquareRoots; --numberOfSquareRoots) {
226 compute2x2(res, ldexp(m_p, -numberOfSquareRoots));
227 res = res.template triangularView<Upper>() * res;
229 compute2x2(res, m_p);
232template <
typename MatrixType>
233inline int MatrixPowerAtomic<MatrixType>::getPadeDegree(
float normIminusT) {
234 const float maxNormForPade[] = {2.8064004e-1f , 4.3386528e-1f};
236 for (; degree <= 4; ++degree)
237 if (normIminusT <= maxNormForPade[degree - 3])
break;
241template <
typename MatrixType>
242inline int MatrixPowerAtomic<MatrixType>::getPadeDegree(
double normIminusT) {
243 const double maxNormForPade[] = {1.884160592658218e-2 , 6.038881904059573e-2, 1.239917516308172e-1,
244 1.999045567181744e-1, 2.789358995219730e-1};
246 for (; degree <= 7; ++degree)
247 if (normIminusT <= maxNormForPade[degree - 3])
break;
251template <
typename MatrixType>
252inline int MatrixPowerAtomic<MatrixType>::getPadeDegree(
long double normIminusT) {
253#if LDBL_MANT_DIG == 53
254 const int maxPadeDegree = 7;
255 const double maxNormForPade[] = {1.884160592658218e-2L , 6.038881904059573e-2L, 1.239917516308172e-1L,
256 1.999045567181744e-1L, 2.789358995219730e-1L};
257#elif LDBL_MANT_DIG <= 64
258 const int maxPadeDegree = 8;
259 const long double maxNormForPade[] = {6.3854693117491799460e-3L ,
260 2.6394893435456973676e-2L,
261 6.4216043030404063729e-2L,
262 1.1701165502926694307e-1L,
263 1.7904284231268670284e-1L,
264 2.4471944416607995472e-1L};
265#elif LDBL_MANT_DIG <= 106
266 const int maxPadeDegree = 10;
267 const double maxNormForPade[] = {1.0007161601787493236741409687186e-4L ,
268 1.0007161601787493236741409687186e-3L,
269 4.7069769360887572939882574746264e-3L,
270 1.3220386624169159689406653101695e-2L,
271 2.8063482381631737920612944054906e-2L,
272 4.9625993951953473052385361085058e-2L,
273 7.7367040706027886224557538328171e-2L,
274 1.1016843812851143391275867258512e-1L};
276 const int maxPadeDegree = 10;
277 const double maxNormForPade[] = {5.524506147036624377378713555116378e-5L ,
278 6.640600568157479679823602193345995e-4L,
279 3.227716520106894279249709728084626e-3L,
280 9.619593944683432960546978734646284e-3L,
281 2.134595382433742403911124458161147e-2L,
282 3.908166513900489428442993794761185e-2L,
283 6.266780814639442865832535460550138e-2L,
284 9.134603732914548552537150753385375e-2L};
287 for (; degree <= maxPadeDegree; ++degree)
288 if (normIminusT <=
static_cast<long double>(maxNormForPade[degree - 3]))
break;
292template <
typename MatrixType>
293inline typename MatrixPowerAtomic<MatrixType>::ComplexScalar MatrixPowerAtomic<MatrixType>::computeSuperDiag(
294 const ComplexScalar& curr,
const ComplexScalar& prev, RealScalar p) {
300 ComplexScalar logCurr =
log(curr);
301 ComplexScalar logPrev =
log(prev);
302 RealScalar unwindingNumber =
303 ceil((numext::imag(logCurr - logPrev) - RealScalar(EIGEN_PI)) / RealScalar(2 * EIGEN_PI));
305 numext::log1p((curr - prev) / prev) / RealScalar(2) + ComplexScalar(0, RealScalar(EIGEN_PI) * unwindingNumber);
306 return RealScalar(2) *
exp(RealScalar(0.5) * p * (logCurr + logPrev)) *
sinh(p * w) / (curr - prev);
309template <
typename MatrixType>
310inline typename MatrixPowerAtomic<MatrixType>::RealScalar MatrixPowerAtomic<MatrixType>::computeSuperDiag(
311 RealScalar curr, RealScalar prev, RealScalar p) {
316 RealScalar w = numext::log1p((curr - prev) / prev) / RealScalar(2);
317 return 2 *
exp(p * (
log(curr) +
log(prev)) / 2) *
sinh(p * w) / (curr - prev);
339template <
typename MatrixType>
342 typedef typename MatrixType::Scalar Scalar;
343 typedef typename MatrixType::RealScalar RealScalar;
354 explicit MatrixPower(
const MatrixType& A) : m_A(A), m_conditionNumber(0), m_rank(A.cols()), m_nulls(0) {
355 eigen_assert(A.rows() == A.cols());
376 template <
typename ResultType>
377 void compute(ResultType& res, RealScalar p);
379 Index rows()
const {
return m_A.rows(); }
380 Index cols()
const {
return m_A.cols(); }
383 typedef internal::make_complex_t<Scalar> ComplexScalar;
384 typedef Matrix<ComplexScalar, Dynamic, Dynamic, 0, MatrixType::RowsAtCompileTime, MatrixType::ColsAtCompileTime>
388 typename MatrixType::Nested m_A;
394 ComplexMatrix m_T, m_U;
405 RealScalar m_conditionNumber;
422 void split(RealScalar& p, RealScalar& intpart);
427 template <
typename ResultType>
428 void computeIntPower(ResultType& res, RealScalar p);
430 template <
typename ResultType>
431 void computeFracPower(ResultType& res, RealScalar p);
433 template <
int Rows,
int Cols,
int Options,
int MaxRows,
int MaxCols>
434 static void revertSchur(Matrix<ComplexScalar, Rows, Cols, Options, MaxRows, MaxCols>& res,
const ComplexMatrix& T,
435 const ComplexMatrix& U);
437 template <
int Rows,
int Cols,
int Options,
int MaxRows,
int MaxCols>
438 static void revertSchur(Matrix<RealScalar, Rows, Cols, Options, MaxRows, MaxCols>& res,
const ComplexMatrix& T,
439 const ComplexMatrix& U);
442template <
typename MatrixType>
443template <
typename ResultType>
450 res(0, 0) = pow(m_A.coeff(0, 0), p);
456 res = MatrixType::Identity(rows(), cols());
457 computeIntPower(res, intpart);
458 if (p) computeFracPower(res, p);
462template <
typename MatrixType>
463void MatrixPower<MatrixType>::split(RealScalar& p, RealScalar& intpart) {
472 if (!m_conditionNumber && p) initialize();
475 if (p > RealScalar(0.5) && p > (1 - p) * pow(m_conditionNumber, p)) {
481template <
typename MatrixType>
482void MatrixPower<MatrixType>::initialize() {
485 ComplexScalar eigenvalue;
487 m_fT.resizeLike(m_A);
488 m_T = schurOfA.matrixT();
489 m_U = schurOfA.matrixU();
490 m_conditionNumber = m_T.diagonal().array().abs().maxCoeff() / m_T.diagonal().array().abs().minCoeff();
493 for (
Index i = cols() - 1; i >= 0; --i) {
494 if (m_rank <= 2)
return;
495 if (m_T.coeff(i, i) == RealScalar(0)) {
496 for (
Index j = i + 1; j < m_rank; ++j) {
497 eigenvalue = m_T.coeff(j, j);
498 rot.makeGivens(m_T.coeff(j - 1, j), eigenvalue);
499 m_T.applyOnTheRight(j - 1, j, rot);
500 m_T.applyOnTheLeft(j - 1, j, rot.adjoint());
501 m_T.coeffRef(j - 1, j - 1) = eigenvalue;
502 m_T.coeffRef(j, j) = RealScalar(0);
503 m_U.applyOnTheRight(j - 1, j, rot);
509 m_nulls = rows() - m_rank;
511 eigen_assert(m_T.bottomRightCorner(m_nulls, m_nulls).isZero() &&
512 "Base of matrix power should be invertible or with a semisimple zero eigenvalue.");
513 m_fT.bottomRows(m_nulls).fill(RealScalar(0));
517template <
typename MatrixType>
518template <
typename ResultType>
519void MatrixPower<MatrixType>::computeIntPower(ResultType& res, RealScalar p) {
522 RealScalar pp =
abs(p);
525 m_tmp = m_A.inverse();
530 if (fmod(pp, 2) >= 1) res = m_tmp * res;
537template <
typename MatrixType>
538template <
typename ResultType>
539void MatrixPower<MatrixType>::computeFracPower(ResultType& res, RealScalar p) {
541 eigen_assert(m_conditionNumber);
542 eigen_assert(m_rank + m_nulls == rows());
546 m_fT.topRightCorner(m_rank, m_nulls) = m_T.topLeftCorner(m_rank, m_rank)
547 .template triangularView<Upper>()
548 .solve(blockTp * m_T.topRightCorner(m_rank, m_nulls));
550 revertSchur(m_tmp, m_fT, m_U);
554template <
typename MatrixType>
555template <
int Rows,
int Cols,
int Options,
int MaxRows,
int MaxCols>
557 const ComplexMatrix& T,
const ComplexMatrix& U) {
558 res.noalias() = U * (T.template triangularView<Upper>() * U.adjoint());
561template <
typename MatrixType>
562template <
int Rows,
int Cols,
int Options,
int MaxRows,
int MaxCols>
564 const ComplexMatrix& T,
const ComplexMatrix& U) {
565 res.noalias() = (U * (T.template triangularView<Upper>() * U.adjoint())).
real();
581template <
typename Derived>
584 typedef typename Derived::PlainObject PlainObject;
585 typedef typename Derived::RealScalar RealScalar;
601 template <
typename ResultType>
602 inline void evalTo(ResultType& result)
const {
606 Index rows()
const {
return m_A.rows(); }
607 Index cols()
const {
return m_A.cols(); }
611 const RealScalar m_p;
627template <
typename Derived>
630 typedef typename Derived::PlainObject PlainObject;
631 typedef internal::make_complex_t<typename Derived::Scalar> ComplexScalar;
650 template <
typename ResultType>
651 inline void evalTo(ResultType& result)
const {
652 result = (m_p * m_A.log()).exp();
655 Index rows()
const {
return m_A.rows(); }
656 Index cols()
const {
return m_A.cols(); }
660 const ComplexScalar m_p;
665template <
typename MatrixPowerType>
666struct traits<MatrixPowerParenthesesReturnValue<MatrixPowerType> > {
667 typedef typename MatrixPowerType::PlainObject ReturnType;
670template <
typename Derived>
671struct traits<MatrixPowerReturnValue<Derived> > {
672 typedef typename Derived::PlainObject ReturnType;
675template <
typename Derived>
676struct traits<MatrixComplexPowerReturnValue<Derived> > {
677 typedef typename Derived::PlainObject ReturnType;
682template <
typename Derived>
687template <
typename Derived>
const MatrixComplexPowerReturnValue< Derived > pow(const internal::make_complex_t< Scalar > &p) const
Definition MatrixPower.h:688
Proxy for the matrix power of some matrix (expression).
Definition MatrixPower.h:628
MatrixComplexPowerReturnValue(const Derived &A, const ComplexScalar &p)
Constructor.
Definition MatrixPower.h:639
void evalTo(ResultType &result) const
Compute the matrix power.
Definition MatrixPower.h:651
Class for computing matrix powers.
Definition MatrixPower.h:89
MatrixPowerAtomic(const MatrixType &T, RealScalar p)
Constructor.
Definition MatrixPower.h:133
void compute(ResultType &res) const
Compute the matrix power.
Definition MatrixPower.h:139
Proxy for the matrix power of some matrix.
Definition MatrixPower.h:43
MatrixPowerParenthesesReturnValue(MatrixPower< MatrixType > &pow, RealScalar p)
Constructor.
Definition MatrixPower.h:53
void evalTo(ResultType &result) const
Compute the matrix power.
Definition MatrixPower.h:61
Proxy for the matrix power of some matrix (expression).
Definition MatrixPower.h:582
MatrixPowerReturnValue(const Derived &A, RealScalar p)
Constructor.
Definition MatrixPower.h:593
void evalTo(ResultType &result) const
Compute the matrix power.
Definition MatrixPower.h:602
Class for computing matrix powers.
Definition MatrixPower.h:340
MatrixPower(const MatrixType &A)
Constructor.
Definition MatrixPower.h:354
const MatrixPowerParenthesesReturnValue< MatrixType > operator()(RealScalar p)
Returns the matrix power.
Definition MatrixPower.h:365
void compute(ResultType &res, RealScalar p)
Compute the matrix power.
Definition MatrixPower.h:444
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.
const Eigen::CwiseUnaryOp< Eigen::internal::scalar_real_op< typename Derived::Scalar >, const Derived > real(const Eigen::ArrayBase< Derived > &x)
const Eigen::CwiseUnaryOp< Eigen::internal::scalar_exp_op< typename Derived::Scalar >, const Derived > exp(const Eigen::ArrayBase< Derived > &x)
const Eigen::CwiseUnaryOp< Eigen::internal::scalar_abs_op< typename Derived::Scalar >, const Derived > abs(const Eigen::ArrayBase< Derived > &x)
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
const Eigen::CwiseUnaryOp< Eigen::internal::scalar_log_op< typename Derived::Scalar >, const Derived > log(const Eigen::ArrayBase< Derived > &x)
const Eigen::CwiseUnaryOp< Eigen::internal::scalar_sinh_op< typename Derived::Scalar >, const Derived > sinh(const Eigen::ArrayBase< Derived > &x)
const Eigen::CwiseUnaryOp< Eigen::internal::scalar_floor_op< typename Derived::Scalar >, const Derived > floor(const Eigen::ArrayBase< Derived > &x)
const Eigen::CwiseUnaryOp< Eigen::internal::scalar_ceil_op< typename Derived::Scalar >, const Derived > ceil(const Eigen::ArrayBase< Derived > &x)