10 #ifndef EIGEN_MATRIX_POWER 11 #define EIGEN_MATRIX_POWER 17 template<
typename MatrixType>
22 typedef typename MatrixType::Index
Index;
27 template<
typename ResultType>
28 inline void evalTo(ResultType& res)
const 40 template<
typename MatrixType>
45 RowsAtCompileTime = MatrixType::RowsAtCompileTime,
46 MaxRowsAtCompileTime = MatrixType::MaxRowsAtCompileTime
48 typedef typename MatrixType::Scalar
Scalar;
51 typedef typename MatrixType::Index
Index;
54 const MatrixType&
m_A;
57 void computePade(
int degree,
const MatrixType& IminusT, MatrixType& res)
const;
58 void compute2x2(MatrixType& res, RealScalar p)
const;
59 void computeBig(MatrixType& res)
const;
60 static int getPadeDegree(
float normIminusT);
61 static int getPadeDegree(
double normIminusT);
62 static int getPadeDegree(
long double normIminusT);
63 static ComplexScalar computeSuperDiag(
const ComplexScalar&,
const ComplexScalar&, RealScalar p);
64 static RealScalar computeSuperDiag(RealScalar, RealScalar, RealScalar p);
68 void compute(MatrixType& res)
const;
71 template<
typename MatrixType>
76 template<
typename MatrixType>
94 template<
typename MatrixType>
98 res = (
m_p-degree) / ((i-1)<<1) * IminusT;
100 res = (MatrixType::Identity(IminusT.rows(), IminusT.cols()) + res).template triangularView<Upper>()
101 .solve((i==1 ? -
m_p : i&1 ? (-
m_p-(i>>1))/(i<<1) : (
m_p-(i>>1))/((i-1)<<1)) * IminusT).eval();
103 res += MatrixType::Identity(IminusT.rows(), IminusT.cols());
107 template<
typename MatrixType>
114 res.coeffRef(0,0) =
pow(
m_A.coeff(0,0), p);
116 for (
Index i=1; i <
m_A.cols(); ++i) {
117 res.coeffRef(i,i) =
pow(
m_A.coeff(i,i), p);
118 if (
m_A.coeff(i-1,i-1) ==
m_A.coeff(i,i))
119 res.coeffRef(i-1,i) = p *
pow(
m_A.coeff(i,i), p-1);
121 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));
124 res.coeffRef(i-1,i) *=
m_A.coeff(i-1,i);
128 template<
typename MatrixType>
131 const int digits = std::numeric_limits<RealScalar>::digits;
132 const RealScalar maxNormForPade = digits <= 24? 4.3386528e-1f:
133 digits <= 53? 2.789358995219730e-1:
134 digits <= 64? 2.4471944416607995472e-1
L:
135 digits <= 106? 1.1016843812851143391275867258512e-1
L:
136 9.134603732914548552537150753385375e-2
L;
137 MatrixType IminusT, sqrtT,
T =
m_A.template triangularView<Upper>();
139 int degree, degree2, numberOfSquareRoots = 0;
140 bool hasExtraSquareRoot =
false;
155 for (
Index i=0; i <
m_A.cols(); ++i)
159 IminusT = MatrixType::Identity(
m_A.rows(),
m_A.cols()) - T;
160 normIminusT = IminusT.cwiseAbs().colwise().sum().maxCoeff();
161 if (normIminusT < maxNormForPade) {
164 if (degree - degree2 <= 1 || hasExtraSquareRoot)
166 hasExtraSquareRoot =
true;
169 T = sqrtT.template triangularView<Upper>();
170 ++numberOfSquareRoots;
174 for (; numberOfSquareRoots; --numberOfSquareRoots) {
176 res = res.template triangularView<Upper>() * res;
181 template<
typename MatrixType>
184 const float maxNormForPade[] = { 2.8064004e-1f , 4.3386528e-1f };
186 for (; degree <= 4; ++degree)
187 if (normIminusT <= maxNormForPade[degree - 3])
192 template<
typename MatrixType>
195 const double maxNormForPade[] = { 1.884160592658218e-2 , 6.038881904059573e-2, 1.239917516308172e-1,
196 1.999045567181744e-1, 2.789358995219730e-1 };
198 for (; degree <= 7; ++degree)
199 if (normIminusT <= maxNormForPade[degree - 3])
204 template<
typename MatrixType>
207 #if LDBL_MANT_DIG == 53 208 const int maxPadeDegree = 7;
209 const double maxNormForPade[] = { 1.884160592658218e-2
L , 6.038881904059573e-2
L, 1.239917516308172e-1
L,
210 1.999045567181744e-1
L, 2.789358995219730e-1
L };
211 #elif LDBL_MANT_DIG <= 64 212 const int maxPadeDegree = 8;
213 const double maxNormForPade[] = { 6.3854693117491799460e-3
L , 2.6394893435456973676e-2
L,
214 6.4216043030404063729e-2
L, 1.1701165502926694307e-1
L, 1.7904284231268670284e-1
L, 2.4471944416607995472e-1
L };
215 #elif LDBL_MANT_DIG <= 106 216 const int maxPadeDegree = 10;
217 const double maxNormForPade[] = { 1.0007161601787493236741409687186e-4
L ,
218 1.0007161601787493236741409687186e-3
L, 4.7069769360887572939882574746264e-3
L, 1.3220386624169159689406653101695e-2
L,
219 2.8063482381631737920612944054906e-2
L, 4.9625993951953473052385361085058e-2
L, 7.7367040706027886224557538328171e-2
L,
220 1.1016843812851143391275867258512e-1
L };
222 const int maxPadeDegree = 10;
223 const double maxNormForPade[] = { 5.524506147036624377378713555116378e-5
L ,
224 6.640600568157479679823602193345995e-4
L, 3.227716520106894279249709728084626e-3
L,
225 9.619593944683432960546978734646284e-3
L, 2.134595382433742403911124458161147e-2
L,
226 3.908166513900489428442993794761185e-2
L, 6.266780814639442865832535460550138e-2
L,
227 9.134603732914548552537150753385375e-2
L };
230 for (; degree <= maxPadeDegree; ++degree)
231 if (normIminusT <= maxNormForPade[degree - 3])
236 template<
typename MatrixType>
247 template<
typename MatrixType>
251 RealScalar w = numext::atanh2(curr - prev, curr + prev);
274 template<
typename MatrixType>
280 ColsAtCompileTime = MatrixType::ColsAtCompileTime,
282 MaxColsAtCompileTime = MatrixType::MaxColsAtCompileTime
284 typedef typename MatrixType::Scalar
Scalar;
286 typedef typename MatrixType::Index
Index;
317 template<
typename ResultType>
318 void compute(ResultType& res, RealScalar p);
328 typename MatrixType::Nested
m_A;
330 ComplexMatrix m_T,
m_U, m_fT;
333 RealScalar modfAndInit(RealScalar, RealScalar*);
335 template<
typename ResultType>
336 void computeIntPower(ResultType&, RealScalar);
338 template<
typename ResultType>
339 void computeFracPower(ResultType&, RealScalar);
341 template<
int Rows,
int Cols,
int Options,
int MaxRows,
int MaxCols>
342 static void revertSchur(
344 const ComplexMatrix&
T,
345 const ComplexMatrix&
U);
347 template<
int Rows,
int Cols,
int Options,
int MaxRows,
int MaxCols>
348 static void revertSchur(
350 const ComplexMatrix&
T,
351 const ComplexMatrix&
U);
354 template<
typename MatrixType>
355 template<
typename ResultType>
365 RealScalar intpart, x = modfAndInit(p, &intpart);
366 computeIntPower(res, intpart);
367 computeFracPower(res, x);
371 template<
typename MatrixType>
377 *intpart = std::floor(x);
380 if (!m_conditionNumber && res) {
385 const RealArray absTdiag = m_T.diagonal().array().abs();
386 m_conditionNumber = absTdiag.maxCoeff() / absTdiag.minCoeff();
396 template<
typename MatrixType>
397 template<
typename ResultType>
402 if (p<0) m_tmp =
m_A.inverse();
405 res = MatrixType::Identity(rows(), cols());
407 if (std::fmod(pp, 2) >= 1)
414 template<
typename MatrixType>
415 template<
typename ResultType>
421 revertSchur(m_tmp, m_fT, m_U);
426 template<
typename MatrixType>
427 template<
int Rows,
int Cols,
int Options,
int MaxRows,
int MaxCols>
432 { res.noalias() = U * (T.template triangularView<Upper>() * U.adjoint()); }
434 template<
typename MatrixType>
435 template<
int Rows,
int Cols,
int Options,
int MaxRows,
int MaxCols>
440 { res.noalias() = (U * (T.template triangularView<Upper>() * U.adjoint())).real(); }
455 template<
typename Derived>
461 typedef typename Derived::Index
Index;
478 template<
typename ResultType>
479 inline void evalTo(ResultType& res)
const 493 template<
typename MatrixPowerType>
495 {
typedef typename MatrixPowerType::PlainObject
ReturnType; };
497 template<
typename Derived>
503 template<
typename Derived>
509 #endif // EIGEN_MATRIX_POWER const Eigen::CwiseUnaryOp< Eigen::internal::scalar_pow_op< typename Derived::Scalar >, const Derived > pow(const Eigen::ArrayBase< Derived > &x, const typename Derived::Scalar &exponent)
MatrixType::Scalar Scalar
Class for computing matrix powers.
USING_NAMESPACE_ACADO typedef TaylorVariable< Interval > T
Derived::PlainObject ReturnType
static void revertSchur(Matrix< ComplexScalar, Rows, Cols, Options, MaxRows, MaxCols > &res, const ComplexMatrix &T, const ComplexMatrix &U)
MatrixType::RealScalar RealScalar
MatrixType::RealScalar RealScalar
iterative scaling algorithm to equilibrate rows and column norms in matrices
IntermediateState pow(const Expression &arg1, const Expression &arg2)
RealScalar modfAndInit(RealScalar, RealScalar *)
const ImagReturnType imag() const
static ComplexScalar computeSuperDiag(const ComplexScalar &, const ComplexScalar &, RealScalar p)
MatrixPowerRetval(MatrixPower< MatrixType > &pow, RealScalar p)
MatrixPowerType::PlainObject ReturnType
MatrixPowerRetval & operator=(const MatrixPowerRetval &)
EIGEN_STRONG_INLINE const CwiseUnaryOp< internal::scalar_abs_op< Scalar >, const Derived > abs() const
void compute(ResultType &res, RealScalar p)
Compute the matrix power.
void computeIntPower(ResultType &, RealScalar)
std::complex< RealScalar > ComplexScalar
void computeFracPower(ResultType &, RealScalar)
const MatrixPowerReturnValue< Derived > pow(const RealScalar &p) const
MatrixPower< MatrixType > & m_pow
void evalTo(ResultType &res) const
Class for computing matrix square roots of upper triangular matrices.
const ComplexMatrixType & matrixT() const
Returns the triangular matrix in the Schur decomposition.
const MatrixPowerRetval< MatrixType > operator()(RealScalar p)
Returns the matrix power.
void computeBig(MatrixType &res) const
MatrixPower(const MatrixType &A)
Constructor.
Matrix< ComplexScalar, RowsAtCompileTime, ColsAtCompileTime, MatrixType::Options, MaxRowsAtCompileTime, MaxColsAtCompileTime > ComplexMatrix
void computePade(int degree, const MatrixType &IminusT, MatrixType &res) const
NumTraits< Scalar >::Real RealScalar
Derived::PlainObject PlainObject
void compute2x2(MatrixType &res, RealScalar p) const
General-purpose arrays with easy API for coefficient-wise operations.
Array< Scalar, RowsAtCompileTime, 1, ColMajor, MaxRowsAtCompileTime > ArrayType
MatrixPowerAtomic(const MatrixType &T, RealScalar p)
MatrixPowerReturnValue(const Derived &A, RealScalar p)
Constructor.
MatrixType::Scalar Scalar
void evalTo(ResultType &res) const
Compute the matrix power.
IntermediateState exp(const Expression &arg)
RealScalar m_conditionNumber
Derived::RealScalar RealScalar
The matrix class, also used for vectors and row-vectors.
void compute(MatrixType &res) const
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.
MatrixType::RealScalar RealScalar
IntermediateState log(const Expression &arg)