Eigen::MatrixPower< MatrixType > Class Template Reference

Class for computing matrix powers. More...

#include <MatrixPower.h>

+ Inheritance diagram for Eigen::MatrixPower< MatrixType >:

Public Member Functions

 MatrixPower (const MatrixType &A)
 Constructor. More...
 
const MatrixPowerParenthesesReturnValue< MatrixTypeoperator() (RealScalar p)
 Returns the matrix power. More...
 
template<typename ResultType >
void compute (ResultType &res, RealScalar p)
 Compute the matrix power. More...
 
Index rows () const
 
Index cols () const
 

Private Types

typedef MatrixType::Scalar Scalar
 
typedef MatrixType::RealScalar RealScalar
 
typedef std::complex< RealScalarComplexScalar
 
typedef Matrix< ComplexScalar, Dynamic, Dynamic, 0, MatrixType::RowsAtCompileTime, MatrixType::ColsAtCompileTime > ComplexMatrix
 

Private Member Functions

void split (RealScalar &p, RealScalar &intpart)
 Split p into integral part and fractional part. More...
 
void initialize ()
 Perform Schur decomposition for fractional power. More...
 
template<typename ResultType >
void computeIntPower (ResultType &res, RealScalar p)
 
template<typename ResultType >
void computeFracPower (ResultType &res, RealScalar p)
 
- Private Member Functions inherited from Eigen::internal::noncopyable
EIGEN_DEVICE_FUNC noncopyable ()
 
EIGEN_DEVICE_FUNC ~noncopyable ()
 

Static Private Member Functions

template<int Rows, int Cols, int Options, int MaxRows, int MaxCols>
static void revertSchur (Matrix< ComplexScalar, Rows, Cols, Options, MaxRows, MaxCols > &res, const ComplexMatrix &T, const ComplexMatrix &U)
 
template<int Rows, int Cols, int Options, int MaxRows, int MaxCols>
static void revertSchur (Matrix< RealScalar, Rows, Cols, Options, MaxRows, MaxCols > &res, const ComplexMatrix &T, const ComplexMatrix &U)
 

Private Attributes

MatrixType::Nested m_A
 Reference to the base of matrix power. More...
 
MatrixType m_tmp
 Temporary storage. More...
 
ComplexMatrix m_T
 Store the result of Schur decomposition. More...
 
ComplexMatrix m_U
 
ComplexMatrix m_fT
 Store fractional power of m_T. More...
 
RealScalar m_conditionNumber
 Condition number of m_A. More...
 
Index m_rank
 Rank of m_A. More...
 
Index m_nulls
 Rank deficiency of m_A. More...
 

Detailed Description

template<typename MatrixType>
class Eigen::MatrixPower< MatrixType >

Class for computing matrix powers.

Template Parameters
MatrixTypetype of the base, expected to be an instantiation of the Matrix class template.

This class is capable of computing real/complex matrices raised to an arbitrary real power. Meanwhile, it saves the result of Schur decomposition if an non-integral power has even been calculated. Therefore, if you want to compute multiple (>= 2) matrix powers for the same matrix, using the class directly is more efficient than calling MatrixBase::pow().

Example:

#include <unsupported/Eigen/MatrixFunctions>
#include <iostream>
using namespace Eigen;
int main() {
Matrix4cd A = Matrix4cd::Random();
MatrixPower<Matrix4cd> Apow(A);
std::cout << "The matrix A is:\n"
<< A
<< "\n\n"
"A^3.1 is:\n"
<< Apow(3.1)
<< "\n\n"
"A^3.3 is:\n"
<< Apow(3.3)
<< "\n\n"
"A^3.7 is:\n"
<< Apow(3.7)
<< "\n\n"
"A^3.9 is:\n"
<< Apow(3.9) << std::endl;
return 0;
}
int main(int argc, char *argv[])
Definition: T_protectiveWall.cpp:194
Matrix< SCALARA, Dynamic, Dynamic, opt_A > A
Definition: bench_gemm.cpp:47
Namespace containing all symbols from the Eigen library.
Definition: bench_norm.cpp:70

Output:

 

Member Typedef Documentation

◆ ComplexMatrix

template<typename MatrixType >
typedef Matrix<ComplexScalar, Dynamic, Dynamic, 0, MatrixType::RowsAtCompileTime, MatrixType::ColsAtCompileTime> Eigen::MatrixPower< MatrixType >::ComplexMatrix
private

◆ ComplexScalar

template<typename MatrixType >
typedef std::complex<RealScalar> Eigen::MatrixPower< MatrixType >::ComplexScalar
private

◆ RealScalar

template<typename MatrixType >
typedef MatrixType::RealScalar Eigen::MatrixPower< MatrixType >::RealScalar
private

◆ Scalar

template<typename MatrixType >
typedef MatrixType::Scalar Eigen::MatrixPower< MatrixType >::Scalar
private

Constructor & Destructor Documentation

◆ MatrixPower()

template<typename MatrixType >
Eigen::MatrixPower< MatrixType >::MatrixPower ( const MatrixType A)
inlineexplicit

Constructor.

Parameters
[in]Athe base of the matrix power.

The class stores a reference to A, so it should not be changed (or destroyed) before evaluation.

354  : m_A(A), m_conditionNumber(0), m_rank(A.cols()), m_nulls(0) {
355  eigen_assert(A.rows() == A.cols());
356  }
#define eigen_assert(x)
Definition: Macros.h:910
Index m_rank
Rank of m_A.
Definition: MatrixPower.h:408
Index m_nulls
Rank deficiency of m_A.
Definition: MatrixPower.h:411
RealScalar m_conditionNumber
Condition number of m_A.
Definition: MatrixPower.h:405
MatrixType::Nested m_A
Reference to the base of matrix power.
Definition: MatrixPower.h:388

References Eigen::PlainObjectBase< Derived >::cols(), eigen_assert, and Eigen::PlainObjectBase< Derived >::rows().

Member Function Documentation

◆ cols()

◆ compute()

template<typename MatrixType >
template<typename ResultType >
void Eigen::MatrixPower< MatrixType >::compute ( ResultType &  res,
RealScalar  p 
)

Compute the matrix power.

Parameters
[in]pexponent, a real scalar.
[out]res\( A^p \) where A is specified in the constructor.
444  {
445  using std::pow;
446  switch (cols()) {
447  case 0:
448  break;
449  case 1:
450  res(0, 0) = pow(m_A.coeff(0, 0), p);
451  break;
452  default:
453  RealScalar intpart;
454  split(p, intpart);
455 
456  res = MatrixType::Identity(rows(), cols());
457  computeIntPower(res, intpart);
458  if (p) computeFracPower(res, p);
459  }
460 }
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
NumTraits< Scalar >::Real RealScalar
Definition: bench_gemm.cpp:46
Index cols() const
Definition: MatrixPower.h:380
void split(RealScalar &p, RealScalar &intpart)
Split p into integral part and fractional part.
Definition: MatrixPower.h:463
void computeFracPower(ResultType &res, RealScalar p)
Definition: MatrixPower.h:539
void computeIntPower(ResultType &res, RealScalar p)
Definition: MatrixPower.h:519
Index rows() const
Definition: MatrixPower.h:379
EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC bfloat16 pow(const bfloat16 &a, const bfloat16 &b)
Definition: BFloat16.h:625

References cols, p, Eigen::bfloat16_impl::pow(), res, rows, and oomph::DoubleVectorHelpers::split().

Referenced by Eigen::MatrixPowerReturnValue< Derived >::evalTo().

◆ computeFracPower()

template<typename MatrixType >
template<typename ResultType >
void Eigen::MatrixPower< MatrixType >::computeFracPower ( ResultType &  res,
RealScalar  p 
)
private
539  {
540  Block<ComplexMatrix, Dynamic, Dynamic> blockTp(m_fT, 0, 0, m_rank, m_rank);
543 
544  MatrixPowerAtomic<ComplexMatrix>(m_T.topLeftCorner(m_rank, m_rank), p).compute(blockTp);
545  if (m_nulls) {
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));
549  }
551  res = m_tmp * res;
552 }
ComplexMatrix m_fT
Store fractional power of m_T.
Definition: MatrixPower.h:397
ComplexMatrix m_U
Definition: MatrixPower.h:394
MatrixType m_tmp
Temporary storage.
Definition: MatrixPower.h:391
static void revertSchur(Matrix< ComplexScalar, Rows, Cols, Options, MaxRows, MaxCols > &res, const ComplexMatrix &T, const ComplexMatrix &U)
Definition: MatrixPower.h:556
ComplexMatrix m_T
Store the result of Schur decomposition.
Definition: MatrixPower.h:394

References Eigen::MatrixPowerAtomic< MatrixType >::compute(), eigen_assert, p, res, and rows.

◆ computeIntPower()

template<typename MatrixType >
template<typename ResultType >
void Eigen::MatrixPower< MatrixType >::computeIntPower ( ResultType &  res,
RealScalar  p 
)
private
519  {
520  using std::abs;
521  using std::fmod;
522  RealScalar pp = abs(p);
523 
524  if (p < 0)
525  m_tmp = m_A.inverse();
526  else
527  m_tmp = m_A;
528 
529  while (true) {
530  if (fmod(pp, 2) >= 1) res = m_tmp * res;
531  pp /= 2;
532  if (pp < 1) break;
533  m_tmp *= m_tmp;
534  }
535 }
AnnoyingScalar abs(const AnnoyingScalar &x)
Definition: AnnoyingScalar.h:135
EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC bfloat16 fmod(const bfloat16 &a, const bfloat16 &b)
Definition: BFloat16.h:648

References abs(), Eigen::bfloat16_impl::fmod(), p, and res.

◆ initialize()

template<typename MatrixType >
void Eigen::MatrixPower< MatrixType >::initialize
private

Perform Schur decomposition for fractional power.

482  {
483  const ComplexSchur<MatrixType> schurOfA(m_A);
484  JacobiRotation<ComplexScalar> rot;
485  ComplexScalar eigenvalue;
486 
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();
491 
492  // Move zero eigenvalues to the bottom right corner.
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);
504  }
505  --m_rank;
506  }
507  }
508 
509  m_nulls = rows() - m_rank;
510  if (m_nulls) {
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));
514  }
515 }
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)
MatrixType::RealScalar RealScalar
Definition: MatrixPower.h:343
std::complex< RealScalar > ComplexScalar
Definition: MatrixPower.h:383
EIGEN_DEVICE_FUNC constexpr EIGEN_STRONG_INLINE Scalar & coeffRef(Index rowId, Index colId)
Definition: PlainObjectBase.h:217
EIGEN_DEVICE_FUNC constexpr EIGEN_STRONG_INLINE const Scalar & coeff(Index rowId, Index colId) const
Definition: PlainObjectBase.h:198
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void resizeLike(const EigenBase< OtherDerived > &_other)
Definition: PlainObjectBase.h:372
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_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:83
std::ptrdiff_t j
Definition: tut_arithmetic_redux_minmax.cpp:2

References cols, eigen_assert, i, j, rot(), rows, and schurOfA().

◆ operator()()

template<typename MatrixType >
const MatrixPowerParenthesesReturnValue<MatrixType> Eigen::MatrixPower< MatrixType >::operator() ( RealScalar  p)
inline

Returns the matrix power.

Parameters
[in]pexponent, a real scalar.
Returns
The expression \( A^p \), where A is specified in the constructor.
365  {
366  return MatrixPowerParenthesesReturnValue<MatrixType>(*this, p);
367  }

References p.

◆ revertSchur() [1/2]

template<typename MatrixType >
template<int Rows, int Cols, int Options, int MaxRows, int MaxCols>
void Eigen::MatrixPower< MatrixType >::revertSchur ( Matrix< ComplexScalar, Rows, Cols, Options, MaxRows, MaxCols > &  res,
const ComplexMatrix T,
const ComplexMatrix U 
)
inlinestaticprivate
557  {
558  res.noalias() = U * (T.template triangularView<Upper>() * U.adjoint());
559 }
double U
Swimming speed.
Definition: two_d_variable_diff_adapt.cc:53

References res, and RachelsAdvectionDiffusion::U.

◆ revertSchur() [2/2]

template<typename MatrixType >
template<int Rows, int Cols, int Options, int MaxRows, int MaxCols>
void Eigen::MatrixPower< MatrixType >::revertSchur ( Matrix< RealScalar, Rows, Cols, Options, MaxRows, MaxCols > &  res,
const ComplexMatrix T,
const ComplexMatrix U 
)
inlinestaticprivate
564  {
565  res.noalias() = (U * (T.template triangularView<Upper>() * U.adjoint())).real();
566 }

References res, and RachelsAdvectionDiffusion::U.

◆ rows()

◆ split()

template<typename MatrixType >
void Eigen::MatrixPower< MatrixType >::split ( RealScalar p,
RealScalar intpart 
)
private

Split p into integral part and fractional part.

Parameters
[in]pThe exponent.
[out]pThe fractional part ranging in \( (-1, 1) \).
[out]intpartThe integral part.

Only if the fractional part is nonzero, it calls initialize().

463  {
464  using std::floor;
465  using std::pow;
466 
467  intpart = floor(p);
468  p -= intpart;
469 
470  // Perform Schur decomposition if it is not yet performed and the power is
471  // not an integer.
472  if (!m_conditionNumber && p) initialize();
473 
474  // Choose the more stable of intpart = floor(p) and intpart = ceil(p).
475  if (p > RealScalar(0.5) && p > (1 - p) * pow(m_conditionNumber, p)) {
476  --p;
477  ++intpart;
478  }
479 }
void initialize()
Perform Schur decomposition for fractional power.
Definition: MatrixPower.h:482
EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC bfloat16 floor(const bfloat16 &a)
Definition: BFloat16.h:643

References Eigen::bfloat16_impl::floor(), p, and Eigen::bfloat16_impl::pow().

Member Data Documentation

◆ m_A

template<typename MatrixType >
MatrixType::Nested Eigen::MatrixPower< MatrixType >::m_A
private

Reference to the base of matrix power.

Referenced by Eigen::MatrixPower< MatrixType >::cols(), and Eigen::MatrixPower< MatrixType >::rows().

◆ m_conditionNumber

template<typename MatrixType >
RealScalar Eigen::MatrixPower< MatrixType >::m_conditionNumber
private

Condition number of m_A.

It is initialized as 0 to avoid performing unnecessary Schur decomposition, which is the bottleneck.

◆ m_fT

template<typename MatrixType >
ComplexMatrix Eigen::MatrixPower< MatrixType >::m_fT
private

Store fractional power of m_T.

◆ m_nulls

template<typename MatrixType >
Index Eigen::MatrixPower< MatrixType >::m_nulls
private

Rank deficiency of m_A.

◆ m_rank

template<typename MatrixType >
Index Eigen::MatrixPower< MatrixType >::m_rank
private

Rank of m_A.

◆ m_T

template<typename MatrixType >
ComplexMatrix Eigen::MatrixPower< MatrixType >::m_T
private

Store the result of Schur decomposition.

◆ m_tmp

template<typename MatrixType >
MatrixType Eigen::MatrixPower< MatrixType >::m_tmp
private

Temporary storage.

◆ m_U

template<typename MatrixType >
ComplexMatrix Eigen::MatrixPower< MatrixType >::m_U
private

The documentation for this class was generated from the following file: