10#ifndef EIGEN_MATRIX_POWER
11#define EIGEN_MATRIX_POWER
15template<
typename MatrixType>
class MatrixPower;
38template<
typename MatrixType>
42 typedef typename MatrixType::RealScalar RealScalar;
58 template<
typename ResultType>
59 inline void evalTo(ResultType& result)
const
60 { m_pow.compute(result, m_p); }
62 Index rows()
const {
return m_pow.rows(); }
63 Index cols()
const {
return m_pow.cols(); }
66 MatrixPower<MatrixType>& m_pow;
85template<
typename MatrixType>
90 RowsAtCompileTime = MatrixType::RowsAtCompileTime,
91 MaxRowsAtCompileTime = MatrixType::MaxRowsAtCompileTime
93 typedef typename MatrixType::Scalar Scalar;
94 typedef typename MatrixType::RealScalar RealScalar;
95 typedef std::complex<RealScalar> 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;
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);
133template<
typename MatrixType>
137 eigen_assert(T.rows() == T.cols());
138 eigen_assert(p > -1 && p < 1);
141template<
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>
163 res = (m_p-RealScalar(degree)) / RealScalar(2*i-2) * IminusT;
166 res = (MatrixType::Identity(IminusT.rows(), IminusT.cols()) + res).
template triangularView<Upper>()
167 .solve((i==1 ? -m_p : i&1 ? (-m_p-RealScalar(i/2))/RealScalar(2*i) : (m_p-RealScalar(i/2))/RealScalar(2*i-2)) * IminusT).eval();
169 res += MatrixType::Identity(IminusT.rows(), IminusT.cols());
173template<
typename MatrixType>
174void MatrixPowerAtomic<MatrixType>::compute2x2(ResultType& res, RealScalar p)
const
178 res.coeffRef(0,0) = pow(m_A.coeff(0,0), p);
180 for (
Index i=1; i < m_A.cols(); ++i) {
181 res.coeffRef(i,i) = pow(m_A.coeff(i,i), p);
182 if (m_A.coeff(i-1,i-1) == m_A.coeff(i,i))
183 res.coeffRef(i-1,i) = p * pow(m_A.coeff(i,i), p-1);
184 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)))
185 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));
187 res.coeffRef(i-1,i) = computeSuperDiag(m_A.coeff(i,i), m_A.coeff(i-1,i-1), p);
188 res.coeffRef(i-1,i) *= m_A.coeff(i-1,i);
192template<
typename MatrixType>
193void MatrixPowerAtomic<MatrixType>::computeBig(ResultType& res)
const
196 const int digits = std::numeric_limits<RealScalar>::digits;
197 const RealScalar maxNormForPade = RealScalar(
198 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)
209 eigen_assert(m_A(i,i) != RealScalar(0));
212 IminusT = MatrixType::Identity(m_A.rows(), m_A.cols()) - T;
213 normIminusT = IminusT.cwiseAbs().colwise().sum().maxCoeff();
214 if (normIminusT < maxNormForPade) {
215 degree = getPadeDegree(normIminusT);
216 degree2 = getPadeDegree(normIminusT/2);
217 if (degree - degree2 <= 1 || hasExtraSquareRoot)
219 hasExtraSquareRoot =
true;
222 T = sqrtT.template triangularView<Upper>();
223 ++numberOfSquareRoots;
225 computePade(degree, IminusT, res);
227 for (; numberOfSquareRoots; --numberOfSquareRoots) {
228 compute2x2(res, ldexp(m_p, -numberOfSquareRoots));
229 res = res.template triangularView<Upper>() * res;
231 compute2x2(res, m_p);
234template<
typename MatrixType>
235inline int MatrixPowerAtomic<MatrixType>::getPadeDegree(
float normIminusT)
237 const float maxNormForPade[] = { 2.8064004e-1f , 4.3386528e-1f };
239 for (; degree <= 4; ++degree)
240 if (normIminusT <= maxNormForPade[degree - 3])
245template<
typename MatrixType>
246inline int MatrixPowerAtomic<MatrixType>::getPadeDegree(
double normIminusT)
248 const double maxNormForPade[] = { 1.884160592658218e-2 , 6.038881904059573e-2, 1.239917516308172e-1,
249 1.999045567181744e-1, 2.789358995219730e-1 };
251 for (; degree <= 7; ++degree)
252 if (normIminusT <= maxNormForPade[degree - 3])
257template<
typename MatrixType>
258inline 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 , 2.6394893435456973676e-2L,
267 6.4216043030404063729e-2L, 1.1701165502926694307e-1L, 1.7904284231268670284e-1L, 2.4471944416607995472e-1L };
268#elif LDBL_MANT_DIG <= 106
269 const int maxPadeDegree = 10;
270 const double maxNormForPade[] = { 1.0007161601787493236741409687186e-4L ,
271 1.0007161601787493236741409687186e-3L, 4.7069769360887572939882574746264e-3L, 1.3220386624169159689406653101695e-2L,
272 2.8063482381631737920612944054906e-2L, 4.9625993951953473052385361085058e-2L, 7.7367040706027886224557538328171e-2L,
273 1.1016843812851143391275867258512e-1L };
275 const int maxPadeDegree = 10;
276 const double maxNormForPade[] = { 5.524506147036624377378713555116378e-5L ,
277 6.640600568157479679823602193345995e-4L, 3.227716520106894279249709728084626e-3L,
278 9.619593944683432960546978734646284e-3L, 2.134595382433742403911124458161147e-2L,
279 3.908166513900489428442993794761185e-2L, 6.266780814639442865832535460550138e-2L,
280 9.134603732914548552537150753385375e-2L };
283 for (; degree <= maxPadeDegree; ++degree)
284 if (normIminusT <= maxNormForPade[degree - 3])
289template<
typename MatrixType>
290inline typename MatrixPowerAtomic<MatrixType>::ComplexScalar
291MatrixPowerAtomic<MatrixType>::computeSuperDiag(
const ComplexScalar& curr,
const ComplexScalar& prev, RealScalar p)
298 ComplexScalar logCurr =
log(curr);
299 ComplexScalar logPrev =
log(prev);
300 RealScalar unwindingNumber =
ceil((numext::imag(logCurr - logPrev) - RealScalar(EIGEN_PI)) / RealScalar(2*EIGEN_PI));
301 ComplexScalar w = numext::log1p((curr-prev)/prev)/RealScalar(2) + ComplexScalar(0, RealScalar(EIGEN_PI)*unwindingNumber);
302 return RealScalar(2) *
exp(RealScalar(0.5) * p * (logCurr + logPrev)) *
sinh(p * w) / (curr - prev);
305template<
typename MatrixType>
306inline typename MatrixPowerAtomic<MatrixType>::RealScalar
307MatrixPowerAtomic<MatrixType>::computeSuperDiag(RealScalar curr, RealScalar prev, RealScalar p)
313 RealScalar w = numext::log1p((curr-prev)/prev)/RealScalar(2);
314 return 2 *
exp(p * (
log(curr) +
log(prev)) / 2) *
sinh(p * w) / (curr - prev);
336template<
typename MatrixType>
340 typedef typename MatrixType::Scalar Scalar;
341 typedef typename MatrixType::RealScalar RealScalar;
354 m_conditionNumber(0),
357 { 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 std::complex<RealScalar> ComplexScalar;
385 MatrixType::RowsAtCompileTime, MatrixType::ColsAtCompileTime> ComplexMatrix;
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(
435 Matrix<ComplexScalar, Rows, Cols, Options, MaxRows, MaxCols>& res,
436 const ComplexMatrix& T,
437 const ComplexMatrix& U);
439 template<
int Rows,
int Cols,
int Options,
int MaxRows,
int MaxCols>
440 static void revertSchur(
441 Matrix<RealScalar, Rows, Cols, Options, MaxRows, MaxCols>& res,
442 const ComplexMatrix& T,
443 const ComplexMatrix& U);
446template<
typename MatrixType>
447template<
typename ResultType>
455 res(0,0) = pow(m_A.coeff(0,0), p);
461 res = MatrixType::Identity(rows(), cols());
462 computeIntPower(res, intpart);
463 if (p) computeFracPower(res, p);
467template<
typename MatrixType>
478 if (!m_conditionNumber && p)
482 if (p > RealScalar(0.5) && p > (1-p) * pow(m_conditionNumber, p)) {
488template<
typename MatrixType>
489void MatrixPower<MatrixType>::initialize()
491 const ComplexSchur<MatrixType> schurOfA(m_A);
492 JacobiRotation<ComplexScalar> rot;
493 ComplexScalar eigenvalue;
495 m_fT.resizeLike(m_A);
496 m_T = schurOfA.matrixT();
497 m_U = schurOfA.matrixU();
498 m_conditionNumber = m_T.diagonal().array().abs().maxCoeff() / m_T.diagonal().array().abs().minCoeff();
501 for (
Index i = cols()-1; i>=0; --i) {
504 if (m_T.coeff(i,i) == RealScalar(0)) {
505 for (
Index j=i+1; j < m_rank; ++j) {
506 eigenvalue = m_T.coeff(j,j);
507 rot.makeGivens(m_T.coeff(j-1,j), eigenvalue);
508 m_T.applyOnTheRight(j-1, j, rot);
509 m_T.applyOnTheLeft(j-1, j, rot.adjoint());
510 m_T.coeffRef(j-1,j-1) = eigenvalue;
511 m_T.coeffRef(j,j) = RealScalar(0);
512 m_U.applyOnTheRight(j-1, j, rot);
518 m_nulls = rows() - m_rank;
520 eigen_assert(m_T.bottomRightCorner(m_nulls, m_nulls).isZero()
521 &&
"Base of matrix power should be invertible or with a semisimple zero eigenvalue.");
522 m_fT.bottomRows(m_nulls).fill(RealScalar(0));
526template<
typename MatrixType>
527template<
typename ResultType>
528void MatrixPower<MatrixType>::computeIntPower(ResultType& res, RealScalar p)
532 RealScalar pp =
abs(p);
535 m_tmp = m_A.inverse();
540 if (fmod(pp, 2) >= 1)
549template<
typename MatrixType>
550template<
typename ResultType>
551void MatrixPower<MatrixType>::computeFracPower(ResultType& res, RealScalar p)
553 Block<ComplexMatrix,Dynamic,Dynamic> blockTp(m_fT, 0, 0, m_rank, m_rank);
554 eigen_assert(m_conditionNumber);
555 eigen_assert(m_rank + m_nulls == rows());
557 MatrixPowerAtomic<ComplexMatrix>(m_T.topLeftCorner(m_rank, m_rank), p).compute(blockTp);
559 m_fT.topRightCorner(m_rank, m_nulls) = m_T.topLeftCorner(m_rank, m_rank).template triangularView<Upper>()
560 .solve(blockTp * m_T.topRightCorner(m_rank, m_nulls));
562 revertSchur(m_tmp, m_fT, m_U);
566template<
typename MatrixType>
567template<
int Rows,
int Cols,
int Options,
int MaxRows,
int MaxCols>
568inline void MatrixPower<MatrixType>::revertSchur(
569 Matrix<ComplexScalar, Rows, Cols, Options, MaxRows, MaxCols>& res,
570 const ComplexMatrix& T,
571 const ComplexMatrix& U)
572{ res.noalias() = U * (T.template triangularView<Upper>() * U.adjoint()); }
574template<
typename MatrixType>
575template<
int Rows,
int Cols,
int Options,
int MaxRows,
int MaxCols>
576inline void MatrixPower<MatrixType>::revertSchur(
577 Matrix<RealScalar, Rows, Cols, Options, MaxRows, MaxCols>& res,
578 const ComplexMatrix& T,
579 const ComplexMatrix& U)
580{ res.noalias() = (U * (T.template triangularView<Upper>() * U.adjoint())).real(); }
595template<
typename Derived>
599 typedef typename Derived::PlainObject PlainObject;
600 typedef typename Derived::RealScalar RealScalar;
617 template<
typename ResultType>
618 inline void evalTo(ResultType& result)
const
621 Index rows()
const {
return m_A.rows(); }
622 Index cols()
const {
return m_A.cols(); }
626 const RealScalar m_p;
642template<
typename Derived>
646 typedef typename Derived::PlainObject PlainObject;
647 typedef typename std::complex<typename Derived::RealScalar> ComplexScalar;
667 template<
typename ResultType>
668 inline void evalTo(ResultType& result)
const
669 { result = (m_p * m_A.log()).
exp(); }
671 Index rows()
const {
return m_A.rows(); }
672 Index cols()
const {
return m_A.cols(); }
676 const ComplexScalar m_p;
681template<
typename MatrixPowerType>
682struct traits< MatrixPowerParenthesesReturnValue<MatrixPowerType> >
683{
typedef typename MatrixPowerType::PlainObject ReturnType; };
685template<
typename Derived>
686struct traits< MatrixPowerReturnValue<Derived> >
687{
typedef typename Derived::PlainObject ReturnType; };
689template<
typename Derived>
690struct traits< MatrixComplexPowerReturnValue<Derived> >
691{
typedef typename Derived::PlainObject ReturnType; };
695template<
typename Derived>
697{
return MatrixPowerReturnValue<Derived>(derived(), p); }
699template<
typename Derived>
701{
return MatrixComplexPowerReturnValue<Derived>(derived(), p); }
const MatrixPowerReturnValue< Derived > pow(const RealScalar &p) const
Definition: MatrixPower.h:696
Proxy for the matrix power of some matrix (expression).
Definition: MatrixPower.h:644
MatrixComplexPowerReturnValue(const Derived &A, const ComplexScalar &p)
Constructor.
Definition: MatrixPower.h:655
void evalTo(ResultType &result) const
Compute the matrix power.
Definition: MatrixPower.h:668
Class for computing matrix powers.
Definition: MatrixPower.h:87
MatrixPowerAtomic(const MatrixType &T, RealScalar p)
Constructor.
Definition: MatrixPower.h:134
void compute(ResultType &res) const
Compute the matrix power.
Definition: MatrixPower.h:142
Proxy for the matrix power of some matrix.
Definition: MatrixPower.h:40
MatrixPowerParenthesesReturnValue(MatrixPower< MatrixType > &pow, RealScalar p)
Constructor.
Definition: MatrixPower.h:50
void evalTo(ResultType &result) const
Compute the matrix power.
Definition: MatrixPower.h:59
Proxy for the matrix power of some matrix (expression).
Definition: MatrixPower.h:597
MatrixPowerReturnValue(const Derived &A, RealScalar p)
Constructor.
Definition: MatrixPower.h:608
void evalTo(ResultType &result) const
Compute the matrix power.
Definition: MatrixPower.h:618
Class for computing matrix powers.
Definition: MatrixPower.h:338
MatrixPower(const MatrixType &A)
Constructor.
Definition: MatrixPower.h:352
const MatrixPowerParenthesesReturnValue< MatrixType > operator()(RealScalar p)
Returns the matrix power.
Definition: MatrixPower.h:366
void compute(ResultType &res, RealScalar p)
Compute the matrix power.
Definition: MatrixPower.h:448
void matrix_sqrt_triangular(const MatrixType &arg, ResultType &result)
Compute matrix square root of triangular matrix.
Definition: MatrixSquareRoot.h:204
Namespace containing all symbols from the Eigen library.
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)