10 #ifndef EIGEN_MATRIX_POWER 11 #define EIGEN_MATRIX_POWER 38 template<
typename MatrixType>
59 template<
typename ResultType>
60 inline void evalTo(ResultType& res)
const 86 template<
typename MatrixType>
91 RowsAtCompileTime = MatrixType::RowsAtCompileTime,
92 MaxRowsAtCompileTime = MatrixType::MaxRowsAtCompileTime
94 typedef typename MatrixType::Scalar
Scalar;
103 void computePade(
int degree,
const MatrixType& IminusT, ResultType& res)
const;
104 void compute2x2(ResultType& res, RealScalar p)
const;
105 void computeBig(ResultType& res)
const;
106 static int getPadeDegree(
float normIminusT);
107 static int getPadeDegree(
double normIminusT);
108 static int getPadeDegree(
long double normIminusT);
109 static ComplexScalar computeSuperDiag(
const ComplexScalar&,
const ComplexScalar&, RealScalar p);
110 static RealScalar computeSuperDiag(RealScalar, RealScalar, RealScalar p);
132 void compute(ResultType& res)
const;
135 template<
typename MatrixType>
143 template<
typename MatrixType>
147 switch (
m_A.rows()) {
161 template<
typename MatrixType>
165 res = (
m_p-degree) / (2*i-2) * IminusT;
168 res = (MatrixType::Identity(IminusT.rows(), IminusT.cols()) + res).template triangularView<Upper>()
169 .solve((i==1 ? -
m_p : i&1 ? (-
m_p-i/2)/(2*i) : (
m_p-i/2)/(2*i-2)) * IminusT).eval();
171 res += MatrixType::Identity(IminusT.rows(), IminusT.cols());
175 template<
typename MatrixType>
180 res.coeffRef(0,0) =
pow(
m_A.coeff(0,0), p);
182 for (
Index i=1; i <
m_A.cols(); ++i) {
183 res.coeffRef(i,i) =
pow(
m_A.coeff(i,i), p);
184 if (
m_A.coeff(i-1,i-1) ==
m_A.coeff(i,i))
185 res.coeffRef(i-1,i) = p *
pow(
m_A.coeff(i,i), p-1);
187 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));
190 res.coeffRef(i-1,i) *=
m_A.coeff(i-1,i);
194 template<
typename MatrixType>
198 const int digits = std::numeric_limits<RealScalar>::digits;
199 const RealScalar maxNormForPade = digits <= 24? 4.3386528e-1L
200 : digits <= 53? 2.789358995219730e-1L
201 : digits <= 64? 2.4471944416607995472e-1L
202 : digits <= 106? 1.1016843812851143391275867258512e-1L
203 : 9.134603732914548552537150753385375e-2L;
204 MatrixType IminusT, sqrtT, T =
m_A.template triangularView<Upper>();
206 int degree, degree2, numberOfSquareRoots = 0;
207 bool hasExtraSquareRoot =
false;
209 for (
Index i=0; i <
m_A.cols(); ++i)
213 IminusT = MatrixType::Identity(
m_A.rows(),
m_A.cols()) - T;
214 normIminusT = IminusT.cwiseAbs().colwise().sum().maxCoeff();
215 if (normIminusT < maxNormForPade) {
218 if (degree - degree2 <= 1 || hasExtraSquareRoot)
220 hasExtraSquareRoot =
true;
223 T = sqrtT.template triangularView<Upper>();
224 ++numberOfSquareRoots;
228 for (; numberOfSquareRoots; --numberOfSquareRoots) {
230 res = res.template triangularView<Upper>() * res;
235 template<
typename MatrixType>
238 const float maxNormForPade[] = { 2.8064004e-1
f , 4.3386528e-1
f };
240 for (; degree <= 4; ++degree)
241 if (normIminusT <= maxNormForPade[degree - 3])
246 template<
typename MatrixType>
249 const double maxNormForPade[] = { 1.884160592658218e-2 , 6.038881904059573e-2, 1.239917516308172e-1,
250 1.999045567181744e-1, 2.789358995219730e-1 };
252 for (; degree <= 7; ++degree)
253 if (normIminusT <= maxNormForPade[degree - 3])
258 template<
typename MatrixType>
261 #if LDBL_MANT_DIG == 53 262 const int maxPadeDegree = 7;
263 const double maxNormForPade[] = { 1.884160592658218e-2L , 6.038881904059573e-2L, 1.239917516308172e-1L,
264 1.999045567181744e-1L, 2.789358995219730e-1L };
265 #elif LDBL_MANT_DIG <= 64 266 const int maxPadeDegree = 8;
267 const long double maxNormForPade[] = { 6.3854693117491799460e-3L , 2.6394893435456973676e-2L,
268 6.4216043030404063729e-2L, 1.1701165502926694307e-1L, 1.7904284231268670284e-1L, 2.4471944416607995472e-1L };
269 #elif LDBL_MANT_DIG <= 106 270 const int maxPadeDegree = 10;
271 const double maxNormForPade[] = { 1.0007161601787493236741409687186e-4L ,
272 1.0007161601787493236741409687186e-3L, 4.7069769360887572939882574746264e-3L, 1.3220386624169159689406653101695e-2L,
273 2.8063482381631737920612944054906e-2L, 4.9625993951953473052385361085058e-2L, 7.7367040706027886224557538328171e-2L,
274 1.1016843812851143391275867258512e-1L };
276 const int maxPadeDegree = 10;
277 const double maxNormForPade[] = { 5.524506147036624377378713555116378e-5L ,
278 6.640600568157479679823602193345995e-4L, 3.227716520106894279249709728084626e-3L,
279 9.619593944683432960546978734646284e-3L, 2.134595382433742403911124458161147e-2L,
280 3.908166513900489428442993794761185e-2L, 6.266780814639442865832535460550138e-2L,
281 9.134603732914548552537150753385375e-2L };
284 for (; degree <= maxPadeDegree; ++degree)
285 if (normIminusT <= maxNormForPade[degree - 3])
290 template<
typename MatrixType>
306 template<
typename MatrixType>
315 return 2 *
exp(p * (
log(curr) +
log(prev)) / 2) *
sinh(p * w) / (curr - prev);
337 template<
typename MatrixType>
341 typedef typename MatrixType::Scalar
Scalar;
356 m_conditionNumber(0),
378 template<
typename ResultType>
387 MatrixType::RowsAtCompileTime, MatrixType::ColsAtCompileTime>
ComplexMatrix;
390 typename MatrixType::Nested
m_A;
424 void split(RealScalar& p, RealScalar& intpart);
429 template<
typename ResultType>
430 void computeIntPower(
ResultType& res, RealScalar p);
432 template<
typename ResultType>
433 void computeFracPower(
ResultType& res, RealScalar p);
435 template<
int Rows,
int Cols,
int Options,
int MaxRows,
int MaxCols>
436 static void revertSchur(
438 const ComplexMatrix& T,
439 const ComplexMatrix& U);
441 template<
int Rows,
int Cols,
int Options,
int MaxRows,
int MaxCols>
442 static void revertSchur(
444 const ComplexMatrix& T,
445 const ComplexMatrix& U);
448 template<
typename MatrixType>
449 template<
typename ResultType>
457 res(0,0) =
pow(
m_A.coeff(0,0), p);
463 res = MatrixType::Identity(rows(), cols());
464 computeIntPower(res, intpart);
465 if (p) computeFracPower(res, p);
469 template<
typename MatrixType>
480 if (!m_conditionNumber && p)
484 if (p >
RealScalar(0.5) && p > (1-p) *
pow(m_conditionNumber, p)) {
490 template<
typename MatrixType>
497 m_fT.resizeLike(
m_A);
500 m_conditionNumber = m_T.diagonal().array().abs().maxCoeff() / m_T.diagonal().array().abs().minCoeff();
503 for (
Index i = cols()-1; i>=0; --i) {
507 for (
Index j=i+1; j < m_rank; ++j) {
508 eigenvalue = m_T.
coeff(j,j);
510 m_T.applyOnTheRight(j-1, j, rot);
511 m_T.applyOnTheLeft(j-1, j, rot.
adjoint());
512 m_T.coeffRef(j-1,j-1) = eigenvalue;
514 m_U.applyOnTheRight(j-1, j, rot);
520 m_nulls = rows() - m_rank;
522 eigen_assert(m_T.bottomRightCorner(m_nulls, m_nulls).isZero()
523 &&
"Base of matrix power should be invertible or with a semisimple zero eigenvalue.");
528 template<
typename MatrixType>
529 template<
typename ResultType>
537 m_tmp =
m_A.inverse();
542 if (
fmod(pp, 2) >= 1)
551 template<
typename MatrixType>
552 template<
typename ResultType>
561 m_fT.topRightCorner(m_rank, m_nulls) = m_T.topLeftCorner(m_rank, m_rank).template triangularView<Upper>()
562 .solve(blockTp * m_T.topRightCorner(m_rank, m_nulls));
564 revertSchur(m_tmp, m_fT, m_U);
568 template<
typename MatrixType>
569 template<
int Rows,
int Cols,
int Options,
int MaxRows,
int MaxCols>
574 { res.noalias() = U * (T.template triangularView<Upper>() * U.adjoint()); }
576 template<
typename MatrixType>
577 template<
int Rows,
int Cols,
int Options,
int MaxRows,
int MaxCols>
582 { res.noalias() = (U * (T.template triangularView<Upper>() * U.adjoint())).real(); }
597 template<
typename Derived>
620 template<
typename ResultType>
645 template<
typename Derived>
671 template<
typename ResultType>
685 template<
typename MatrixPowerType>
687 {
typedef typename MatrixPowerType::PlainObject
ReturnType; };
689 template<
typename Derived>
693 template<
typename Derived>
699 template<
typename Derived>
703 template<
typename Derived>
709 #endif // EIGEN_MATRIX_POWER
EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half pow(const half &a, const half &b)
Index m_nulls
Rank deficiency of m_A.
MatrixType::Scalar Scalar
Class for computing matrix powers.
void computeFracPower(ResultType &res, RealScalar p)
void split(RealScalar &p, RealScalar &intpart)
Split p into integral part and fractional part.
Derived::PlainObject ReturnType
static void revertSchur(Matrix< ComplexScalar, Rows, Cols, Options, MaxRows, MaxCols > &res, const ComplexMatrix &T, const ComplexMatrix &U)
EIGEN_DEVICE_FUNC const ExpReturnType exp() const
MatrixType::Nested m_A
Reference to the base of matrix power.
void makeGivens(const Scalar &p, const Scalar &q, Scalar *z=0)
void computePade(int degree, const MatrixType &IminusT, ResultType &res) const
ROSCONSOLE_DECL void initialize()
const mpreal ldexp(const mpreal &v, mp_exp_t exp)
MatrixType::RealScalar RealScalar
static int f(const TensorMap< Tensor< int, 3 > > &tensor)
MatrixType m_tmp
Temporary storage.
EIGEN_DEVICE_FUNC const LogReturnType log() const
const MatrixPowerParenthesesReturnValue< MatrixType > operator()(RealScalar p)
Returns the matrix power.
Rotation given by a cosine-sine pair.
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const AbsReturnType abs() const
void evalTo(ResultType &res) const
Compute the matrix power.
Derived::PlainObject PlainObject
Proxy for the matrix power of some matrix (expression).
static ComplexScalar computeSuperDiag(const ComplexScalar &, const ComplexScalar &, RealScalar p)
void evalTo(ResultType &res) const
Compute the matrix power.
void compute2x2(ResultType &res, RealScalar p) const
void initialize()
Perform Schur decomposition for fractional power.
void compute(ResultType &res, RealScalar p)
Compute the matrix power.
Block< MatrixType, Dynamic, Dynamic > ResultType
EIGEN_DEVICE_FUNC const CeilReturnType ceil() const
Matrix< ComplexScalar, Dynamic, Dynamic, 0, MatrixType::RowsAtCompileTime, MatrixType::ColsAtCompileTime > ComplexMatrix
std::complex< RealScalar > ComplexScalar
const MatrixPowerReturnValue< Derived > pow(const RealScalar &p) const
EIGEN_DEVICE_FUNC const Log1pReturnType log1p() const
const mpreal fmod(const mpreal &x, const mpreal &y, mp_rnd_t rnd_mode=mpreal::get_default_rnd())
EIGEN_DEVICE_FUNC const SinhReturnType sinh() const
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Derived::PlainObject ReturnType
MatrixComplexPowerReturnValue(const Derived &A, const ComplexScalar &p)
Constructor.
void computeIntPower(ResultType &res, RealScalar p)
JacobiRotation adjoint() const
const ComplexMatrixType & matrixT() const
Returns the triangular matrix in the Schur decomposition.
MatrixPower(const MatrixType &A)
Constructor.
MatrixType::RealScalar RealScalar
TFSIMD_FORCE_INLINE const tfScalar & w() const
NumTraits< Scalar >::Real RealScalar
EIGEN_DEVICE_FUNC const FloorReturnType floor() const
Expression of a fixed-size or dynamic-size block.
void computeBig(ResultType &res) const
ComplexMatrix m_fT
Store fractional power of m_T.
Derived::PlainObject PlainObject
MatrixPowerType::PlainObject ReturnType
MatrixPowerAtomic(const MatrixType &T, RealScalar p)
Constructor.
EIGEN_DEVICE_FUNC const ImagReturnType imag() const
MatrixPowerReturnValue(const Derived &A, RealScalar p)
Constructor.
MatrixPower< MatrixType > & m_pow
void matrix_sqrt_triangular(const MatrixType &arg, ResultType &result)
Compute matrix square root of triangular matrix.
MatrixType::Scalar Scalar
void evalTo(ResultType &res) const
Compute the matrix power.
MatrixPowerParenthesesReturnValue(MatrixPower< MatrixType > &pow, RealScalar p)
Constructor.
Class for computing matrix powers.
RealScalar m_conditionNumber
Condition number of m_A.
Derived::RealScalar RealScalar
Proxy for the matrix power of some matrix.
The matrix class, also used for vectors and row-vectors.
std::complex< typename Derived::RealScalar > ComplexScalar
static int getPadeDegree(float normIminusT)
Proxy for the matrix power of some matrix (expression).
std::complex< RealScalar > ComplexScalar
const ComplexMatrixType & matrixU() const
Returns the unitary matrix in the Schur decomposition.
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Scalar & coeff(Index rowId, Index colId) const
void compute(ResultType &res) const
Compute the matrix power.
MatrixType::RealScalar RealScalar