10 #ifndef EIGEN_MATRIX_POWER
11 #define EIGEN_MATRIX_POWER
18 template <
typename MatrixType>
42 template <
typename MatrixType>
60 template <
typename ResultType>
61 inline void evalTo(ResultType& result)
const {
88 template <
typename MatrixType>
132 template <
typename MatrixType>
138 template <
typename MatrixType>
141 switch (m_A.rows()) {
145 res(0, 0) =
pow(m_A(0, 0), m_p);
148 compute2x2(
res, m_p);
155 template <
typename MatrixType>
161 res = (MatrixType::Identity(IminusT.rows(), IminusT.cols()) +
res)
162 .
template triangularView<Upper>()
163 .solve((
i == 1 ? -m_p
169 res += MatrixType::Identity(IminusT.rows(), IminusT.cols());
173 template <
typename MatrixType>
177 res.coeffRef(0, 0) =
pow(m_A.coeff(0, 0),
p);
179 for (
Index i = 1;
i < m_A.cols(); ++
i) {
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);
193 template <
typename MatrixType>
196 const int digits = std::numeric_limits<RealScalar>::digits;
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>();
205 int degree, degree2, numberOfSquareRoots = 0;
206 bool hasExtraSquareRoot =
false;
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;
225 for (; numberOfSquareRoots; --numberOfSquareRoots) {
226 compute2x2(
res, ldexp(m_p, -numberOfSquareRoots));
227 res =
res.template triangularView<Upper>() *
res;
229 compute2x2(
res, m_p);
232 template <
typename MatrixType>
234 const float maxNormForPade[] = {2.8064004e-1f , 4.3386528e-1f};
237 if (normIminusT <= maxNormForPade[
degree - 3])
break;
241 template <
typename MatrixType>
243 const double maxNormForPade[] = {1.884160592658218e-2 , 6.038881904059573e-2, 1.239917516308172e-1,
244 1.999045567181744e-1, 2.789358995219730e-1};
247 if (normIminusT <= maxNormForPade[
degree - 3])
break;
251 template <
typename MatrixType>
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};
288 if (normIminusT <=
static_cast<long double>(maxNormForPade[
degree - 3]))
break;
292 template <
typename MatrixType>
309 template <
typename MatrixType>
317 return 2 *
exp(
p * (
log(curr) +
log(prev)) / 2) *
sinh(
p *
w) / (curr - prev);
339 template <
typename MatrixType>
376 template <
typename ResultType>
388 typename MatrixType::Nested
m_A;
427 template <
typename ResultType>
430 template <
typename ResultType>
433 template <
int Rows,
int Cols,
int Options,
int MaxRows,
int MaxCols>
437 template <
int Rows,
int Cols,
int Options,
int MaxRows,
int MaxCols>
442 template <
typename MatrixType>
443 template <
typename ResultType>
450 res(0, 0) =
pow(m_A.coeff(0, 0),
p);
457 computeIntPower(
res, intpart);
458 if (
p) computeFracPower(
res,
p);
462 template <
typename MatrixType>
472 if (!m_conditionNumber &&
p) initialize();
481 template <
typename MatrixType>
487 m_fT.resizeLike(m_A);
490 m_conditionNumber = m_T.diagonal().array().abs().maxCoeff() / m_T.diagonal().array().abs().minCoeff();
494 if (m_rank <= 2)
return;
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;
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.");
517 template <
typename MatrixType>
518 template <
typename ResultType>
525 m_tmp = m_A.inverse();
537 template <
typename MatrixType>
538 template <
typename ResultType>
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);
554 template <
typename MatrixType>
555 template <
int Rows,
int Cols,
int Options,
int MaxRows,
int MaxCols>
558 res.noalias() =
U * (
T.template triangularView<Upper>() *
U.adjoint());
561 template <
typename MatrixType>
562 template <
int Rows,
int Cols,
int Options,
int MaxRows,
int MaxCols>
565 res.noalias() = (
U * (
T.template triangularView<Upper>() *
U.adjoint())).real();
581 template <
typename Derived>
601 template <
typename ResultType>
602 inline void evalTo(ResultType& result)
const {
627 template <
typename Derived>
650 template <
typename ResultType>
651 inline void evalTo(ResultType& result)
const {
665 template <
typename MatrixPowerType>
670 template <
typename Derived>
675 template <
typename Derived>
682 template <
typename Derived>
687 template <
typename Derived>
689 return MatrixComplexPowerReturnValue<Derived>(derived(),
p);
AnnoyingScalar abs(const AnnoyingScalar &x)
Definition: AnnoyingScalar.h:135
AnnoyingScalar imag(const AnnoyingScalar &)
Definition: AnnoyingScalar.h:132
int i
Definition: BiCGSTAB_step_by_step.cpp:9
cout<< "Here is a random 4x4 matrix, A:"<< endl<< A<< endl<< endl;ComplexSchur< MatrixXcf > schurOfA(A, false)
Eigen::Triplet< double > T
Definition: EigenUnitTest.cpp:11
#define eigen_assert(x)
Definition: Macros.h:910
#define EIGEN_PI
Definition: MathFunctions.h:16
RowVector3d w
Definition: Matrix_resize_int.cpp:3
cout<< "Here is the matrix m:"<< endl<< m<< endl;Matrix< ptrdiff_t, 3, 1 > res
Definition: PartialRedux_count.cpp:3
float * p
Definition: Tutorial_Map_using.cpp:9
int rows
Definition: Tutorial_commainit_02.cpp:1
int cols
Definition: Tutorial_commainit_02.cpp:1
SCALAR Scalar
Definition: bench_gemm.cpp:45
NumTraits< Scalar >::Real RealScalar
Definition: bench_gemm.cpp:46
MatrixXf MatrixType
Definition: benchmark-blocking-sizes.cpp:52
Expression of a fixed-size or dynamic-size block.
Definition: Block.h:110
Rotation given by a cosine-sine pair.
Definition: Jacobi.h:38
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
Derived::PlainObject PlainObject
Definition: MatrixPower.h:630
Index cols() const
Definition: MatrixPower.h:656
std::complex< typename Derived::RealScalar > ComplexScalar
Definition: MatrixPower.h:631
const Derived & m_A
Definition: MatrixPower.h:659
void evalTo(ResultType &result) const
Compute the matrix power.
Definition: MatrixPower.h:651
Index rows() const
Definition: MatrixPower.h:655
const ComplexScalar m_p
Definition: MatrixPower.h:660
Class for computing matrix powers.
Definition: MatrixPower.h:89
void computePade(int degree, const MatrixType &IminusT, ResultType &res) const
Definition: MatrixPower.h:156
static int getPadeDegree(float normIminusT)
Definition: MatrixPower.h:233
RealScalar m_p
Definition: MatrixPower.h:98
@ RowsAtCompileTime
Definition: MatrixPower.h:91
@ MaxRowsAtCompileTime
Definition: MatrixPower.h:91
static ComplexScalar computeSuperDiag(const ComplexScalar &, const ComplexScalar &, RealScalar p)
Definition: MatrixPower.h:293
MatrixType::Scalar Scalar
Definition: MatrixPower.h:92
void compute2x2(ResultType &res, RealScalar p) const
Definition: MatrixPower.h:174
Block< MatrixType, Dynamic, Dynamic > ResultType
Definition: MatrixPower.h:95
MatrixPowerAtomic(const MatrixType &T, RealScalar p)
Constructor.
Definition: MatrixPower.h:133
void compute(ResultType &res) const
Compute the matrix power.
Definition: MatrixPower.h:139
void computeBig(ResultType &res) const
Definition: MatrixPower.h:194
MatrixType::RealScalar RealScalar
Definition: MatrixPower.h:93
const MatrixType & m_A
Definition: MatrixPower.h:97
std::complex< RealScalar > ComplexScalar
Definition: MatrixPower.h:94
Proxy for the matrix power of some matrix.
Definition: MatrixPower.h:43
MatrixType::RealScalar RealScalar
Definition: MatrixPower.h:45
Index rows() const
Definition: MatrixPower.h:65
Index cols() const
Definition: MatrixPower.h:66
const RealScalar m_p
Definition: MatrixPower.h:70
MatrixPowerParenthesesReturnValue(MatrixPower< MatrixType > &pow, RealScalar p)
Constructor.
Definition: MatrixPower.h:53
void evalTo(ResultType &result) const
Compute the matrix power.
Definition: MatrixPower.h:61
MatrixPower< MatrixType > & m_pow
Definition: MatrixPower.h:69
Proxy for the matrix power of some matrix (expression).
Definition: MatrixPower.h:582
Index cols() const
Definition: MatrixPower.h:607
MatrixPowerReturnValue(const Derived &A, RealScalar p)
Constructor.
Definition: MatrixPower.h:593
Derived::RealScalar RealScalar
Definition: MatrixPower.h:585
void evalTo(ResultType &result) const
Compute the matrix power.
Definition: MatrixPower.h:602
Index rows() const
Definition: MatrixPower.h:606
const Derived & m_A
Definition: MatrixPower.h:610
Derived::PlainObject PlainObject
Definition: MatrixPower.h:584
const RealScalar m_p
Definition: MatrixPower.h:611
Class for computing matrix powers.
Definition: MatrixPower.h:340
Matrix< ComplexScalar, Dynamic, Dynamic, 0, MatrixType::RowsAtCompileTime, MatrixType::ColsAtCompileTime > ComplexMatrix
Definition: MatrixPower.h:385
const MatrixPowerParenthesesReturnValue< MatrixType > operator()(RealScalar p)
Returns the matrix power.
Definition: MatrixPower.h:365
Index m_rank
Rank of m_A.
Definition: MatrixPower.h:408
Index cols() const
Definition: MatrixPower.h:380
ComplexMatrix m_fT
Store fractional power of m_T.
Definition: MatrixPower.h:397
Index m_nulls
Rank deficiency of m_A.
Definition: MatrixPower.h:411
MatrixPower(const MatrixType &A)
Constructor.
Definition: MatrixPower.h:354
void split(RealScalar &p, RealScalar &intpart)
Split p into integral part and fractional part.
Definition: MatrixPower.h:463
MatrixType::RealScalar RealScalar
Definition: MatrixPower.h:343
void computeFracPower(ResultType &res, RealScalar p)
Definition: MatrixPower.h:539
ComplexMatrix m_U
Definition: MatrixPower.h:394
void computeIntPower(ResultType &res, RealScalar p)
Definition: MatrixPower.h:519
void compute(ResultType &res, RealScalar p)
Compute the matrix power.
Definition: MatrixPower.h:444
MatrixType::Scalar Scalar
Definition: MatrixPower.h:342
MatrixType m_tmp
Temporary storage.
Definition: MatrixPower.h:391
void initialize()
Perform Schur decomposition for fractional power.
Definition: MatrixPower.h:482
RealScalar m_conditionNumber
Condition number of m_A.
Definition: MatrixPower.h:405
Index rows() const
Definition: MatrixPower.h:379
std::complex< RealScalar > ComplexScalar
Definition: MatrixPower.h:383
static void revertSchur(Matrix< ComplexScalar, Rows, Cols, Options, MaxRows, MaxCols > &res, const ComplexMatrix &T, const ComplexMatrix &U)
Definition: MatrixPower.h:556
MatrixType::Nested m_A
Reference to the base of matrix power.
Definition: MatrixPower.h:388
ComplexMatrix m_T
Store the result of Schur decomposition.
Definition: MatrixPower.h:394
The matrix class, also used for vectors and row-vectors.
Definition: Eigen/Eigen/src/Core/Matrix.h:186
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE EIGEN_CONSTEXPR Index cols() const EIGEN_NOEXCEPT
Definition: PlainObjectBase.h:192
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE EIGEN_CONSTEXPR Index rows() const EIGEN_NOEXCEPT
Definition: PlainObjectBase.h:191
Definition: ReturnByValue.h:50
EIGEN_BLAS_FUNC() rot(int *n, Scalar *px, int *incx, Scalar *py, int *incy, Scalar *pc, Scalar *ps)
Definition: level1_real_impl.h:88
EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC bfloat16 floor(const bfloat16 &a)
Definition: BFloat16.h:643
EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC bfloat16 ceil(const bfloat16 &a)
Definition: BFloat16.h:644
EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC bfloat16 fmod(const bfloat16 &a, const bfloat16 &b)
Definition: BFloat16.h:648
EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC bfloat16 exp(const bfloat16 &a)
Definition: BFloat16.h:615
EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC bfloat16 pow(const bfloat16 &a, const bfloat16 &b)
Definition: BFloat16.h:625
EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC bfloat16 log(const bfloat16 &a)
Definition: BFloat16.h:618
EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC bfloat16 sinh(const bfloat16 &a)
Definition: BFloat16.h:637
EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC bfloat16 log1p(const bfloat16 &a)
Definition: BFloat16.h:619
Namespace containing all symbols from the Eigen library.
Definition: bench_norm.cpp:70
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:83
void matrix_sqrt_triangular(const MatrixType &arg, ResultType &result)
Compute matrix square root of triangular matrix.
Definition: MatrixSquareRoot.h:194
double U
Swimming speed.
Definition: two_d_variable_diff_adapt.cc:53
const Mdouble degree
Definition: ExtendedMath.h:32
Definition: Eigen_Colamd.h:49
void split(const DoubleVector &in_vector, Vector< DoubleVector * > &out_vector_pt)
Definition: double_vector.cc:1413
internal::nested_eval< T, 1 >::type eval(const T &xpr)
Definition: sparse_permutations.cpp:47
Derived::PlainObject ReturnType
Definition: MatrixPower.h:677
MatrixPowerType::PlainObject ReturnType
Definition: MatrixPower.h:667
Derived::PlainObject ReturnType
Definition: MatrixPower.h:672
Definition: ForwardDeclarations.h:21
std::ptrdiff_t j
Definition: tut_arithmetic_redux_minmax.cpp:2