10 #ifndef EIGEN_MATRIX_POWER
11 #define EIGEN_MATRIX_POWER
17 template<
typename MatrixType>
class MatrixPower;
40 template<
typename MatrixType>
60 template<
typename ResultType>
61 inline void evalTo(ResultType& result)
const
87 template<
typename MatrixType>
95 typedef typename MatrixType::Scalar
Scalar;
135 template<
typename MatrixType>
143 template<
typename MatrixType>
147 switch (m_A.rows()) {
151 res(0,0) =
pow(m_A(0,0), m_p);
154 compute2x2(
res, m_p);
161 template<
typename MatrixType>
168 res = (MatrixType::Identity(IminusT.rows(), IminusT.cols()) +
res).
template triangularView<Upper>()
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) {
184 if (m_A.coeff(
i-1,
i-1) == m_A.coeff(
i,
i))
186 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)))
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));
189 res.coeffRef(
i-1,
i) = computeSuperDiag(m_A.coeff(
i,
i), m_A.coeff(
i-1,
i-1),
p);
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;
200 digits <= 24? 4.3386528e-1L
201 : digits <= 53? 2.789358995219730e-1L
202 : digits <= 64? 2.4471944416607995472e-1L
203 : digits <= 106? 1.1016843812851143391275867258512e-1L
204 : 9.134603732914548552537150753385375e-2L);
205 MatrixType IminusT, sqrtT,
T = m_A.template triangularView<Upper>();
207 int degree, degree2, numberOfSquareRoots = 0;
208 bool hasExtraSquareRoot =
false;
210 for (
Index i=0;
i < m_A.cols(); ++
i)
214 IminusT = MatrixType::Identity(m_A.rows(), m_A.cols()) -
T;
215 normIminusT = IminusT.cwiseAbs().colwise().sum().maxCoeff();
216 if (normIminusT < maxNormForPade) {
217 degree = getPadeDegree(normIminusT);
218 degree2 = getPadeDegree(normIminusT/2);
219 if (degree - degree2 <= 1 || hasExtraSquareRoot)
221 hasExtraSquareRoot =
true;
224 T = sqrtT.template triangularView<Upper>();
225 ++numberOfSquareRoots;
227 computePade(degree, IminusT,
res);
229 for (; numberOfSquareRoots; --numberOfSquareRoots) {
230 compute2x2(
res, ldexp(m_p, -numberOfSquareRoots));
231 res =
res.template triangularView<Upper>() *
res;
233 compute2x2(
res, m_p);
236 template<
typename MatrixType>
239 const float maxNormForPade[] = { 2.8064004e-1f , 4.3386528e-1f };
241 for (; degree <= 4; ++degree)
242 if (normIminusT <= maxNormForPade[degree - 3])
247 template<
typename MatrixType>
250 const double maxNormForPade[] = { 1.884160592658218e-2 , 6.038881904059573e-2, 1.239917516308172e-1,
251 1.999045567181744e-1, 2.789358995219730e-1 };
253 for (; degree <= 7; ++degree)
254 if (normIminusT <= maxNormForPade[degree - 3])
259 template<
typename MatrixType>
262 #if LDBL_MANT_DIG == 53
263 const int maxPadeDegree = 7;
264 const double maxNormForPade[] = { 1.884160592658218e-2L , 6.038881904059573e-2L, 1.239917516308172e-1L,
265 1.999045567181744e-1L, 2.789358995219730e-1L };
266 #elif LDBL_MANT_DIG <= 64
267 const int maxPadeDegree = 8;
268 const long double maxNormForPade[] = { 6.3854693117491799460e-3L , 2.6394893435456973676e-2L,
269 6.4216043030404063729e-2L, 1.1701165502926694307e-1L, 1.7904284231268670284e-1L, 2.4471944416607995472e-1L };
270 #elif LDBL_MANT_DIG <= 106
271 const int maxPadeDegree = 10;
272 const double maxNormForPade[] = { 1.0007161601787493236741409687186e-4L ,
273 1.0007161601787493236741409687186e-3L, 4.7069769360887572939882574746264e-3L, 1.3220386624169159689406653101695e-2L,
274 2.8063482381631737920612944054906e-2L, 4.9625993951953473052385361085058e-2L, 7.7367040706027886224557538328171e-2L,
275 1.1016843812851143391275867258512e-1L };
277 const int maxPadeDegree = 10;
278 const double maxNormForPade[] = { 5.524506147036624377378713555116378e-5L ,
279 6.640600568157479679823602193345995e-4L, 3.227716520106894279249709728084626e-3L,
280 9.619593944683432960546978734646284e-3L, 2.134595382433742403911124458161147e-2L,
281 3.908166513900489428442993794761185e-2L, 6.266780814639442865832535460550138e-2L,
282 9.134603732914548552537150753385375e-2L };
285 for (; degree <= maxPadeDegree; ++degree)
286 if (normIminusT <=
static_cast<long double>(maxNormForPade[degree - 3]))
291 template<
typename MatrixType>
307 template<
typename MatrixType>
316 return 2 *
exp(
p * (
log(curr) +
log(prev)) / 2) *
sinh(
p *
w) / (curr - prev);
338 template<
typename MatrixType>
342 typedef typename MatrixType::Scalar
Scalar;
378 template<
typename ResultType>
387 MatrixType::RowsAtCompileTime, MatrixType::ColsAtCompileTime>
ComplexMatrix;
390 typename MatrixType::Nested
m_A;
429 template<
typename ResultType>
432 template<
typename ResultType>
435 template<
int Rows,
int Cols,
int Options,
int MaxRows,
int MaxCols>
441 template<
int Rows,
int Cols,
int Options,
int MaxRows,
int MaxCols>
448 template<
typename MatrixType>
449 template<
typename ResultType>
457 res(0,0) =
pow(m_A.coeff(0,0),
p);
464 computeIntPower(
res, intpart);
465 if (
p) computeFracPower(
res,
p);
469 template<
typename MatrixType>
480 if (!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();
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>
601 typedef typename Derived::PlainObject PlainObject;
602 typedef typename Derived::RealScalar RealScalar;
619 template<
typename ResultType>
620 inline void evalTo(ResultType& result)
const
621 { MatrixPower<PlainObject>(m_A.eval()).compute(result, m_p); }
623 Index rows()
const {
return m_A.rows(); }
624 Index cols()
const {
return m_A.cols(); }
628 const RealScalar m_p;
644 template<
typename Derived>
645 class MatrixComplexPowerReturnValue :
public ReturnByValue< MatrixComplexPowerReturnValue<Derived> >
648 typedef typename Derived::PlainObject PlainObject;
649 typedef typename std::complex<typename Derived::RealScalar> ComplexScalar;
657 MatrixComplexPowerReturnValue(
const Derived& A,
const ComplexScalar& p) : m_A(
A), m_p(
p)
669 template<
typename ResultType>
670 inline void evalTo(ResultType& result)
const
671 { result = (m_p * m_A.log()).
exp(); }
673 Index rows()
const {
return m_A.rows(); }
674 Index cols()
const {
return m_A.cols(); }
678 const ComplexScalar m_p;
683 template<
typename MatrixPowerType>
684 struct traits< MatrixPowerParenthesesReturnValue<MatrixPowerType> >
685 {
typedef typename MatrixPowerType::PlainObject ReturnType; };
687 template<
typename Derived>
688 struct traits< MatrixPowerReturnValue<Derived> >
689 {
typedef typename Derived::PlainObject ReturnType; };
691 template<
typename Derived>
692 struct traits< MatrixComplexPowerReturnValue<Derived> >
693 {
typedef typename Derived::PlainObject ReturnType; };
697 template<
typename Derived>
699 {
return MatrixPowerReturnValue<Derived>(derived(), p); }
701 template<
typename Derived>
703 {
return MatrixComplexPowerReturnValue<Derived>(derived(), p); }
SparseMatrix< double > A(n, n)
cout<< "Here is a random 4x4 matrix, A:"<< endl<< A<< endl<< endl;ComplexSchur< MatrixXcf > schurOfA(A, false)
cout<< "Here is the matrix m:"<< endl<< m<< endl;Matrix< ptrdiff_t, 3, 1 > res
Matrix< float, 1, Dynamic > MatrixType
JacobiRotation adjoint() const
void makeGivens(const Scalar &p, const Scalar &q, Scalar *r=0)
const MatrixPowerReturnValue< Derived > pow(const RealScalar &p) const
Class for computing matrix powers.
void computePade(int degree, const MatrixType &IminusT, ResultType &res) const
static int getPadeDegree(float normIminusT)
Block< MatrixType, Dynamic, Dynamic > ResultType
static ComplexScalar computeSuperDiag(const ComplexScalar &, const ComplexScalar &, RealScalar p)
MatrixType::Scalar Scalar
void compute2x2(ResultType &res, RealScalar p) const
MatrixPowerAtomic(const MatrixType &T, RealScalar p)
Constructor.
void compute(ResultType &res) const
Compute the matrix power.
void computeBig(ResultType &res) const
MatrixType::RealScalar RealScalar
std::complex< RealScalar > ComplexScalar
Proxy for the matrix power of some matrix.
MatrixType::RealScalar RealScalar
MatrixPowerParenthesesReturnValue(MatrixPower< MatrixType > &pow, RealScalar p)
Constructor.
void evalTo(ResultType &result) const
Compute the matrix power.
MatrixPower< MatrixType > & m_pow
Class for computing matrix powers.
Matrix< ComplexScalar, Dynamic, Dynamic, 0, MatrixType::RowsAtCompileTime, MatrixType::ColsAtCompileTime > ComplexMatrix
const MatrixPowerParenthesesReturnValue< MatrixType > operator()(RealScalar p)
Returns the matrix power.
ComplexMatrix m_fT
Store fractional power of m_T.
Index m_nulls
Rank deficiency of m_A.
MatrixPower(const MatrixType &A)
Constructor.
void split(RealScalar &p, RealScalar &intpart)
Split p into integral part and fractional part.
MatrixType::RealScalar RealScalar
void computeFracPower(ResultType &res, RealScalar p)
void computeIntPower(ResultType &res, RealScalar p)
void compute(ResultType &res, RealScalar p)
Compute the matrix power.
MatrixType::Scalar Scalar
MatrixType m_tmp
Temporary storage.
void initialize()
Perform Schur decomposition for fractional power.
RealScalar m_conditionNumber
Condition number of m_A.
std::complex< RealScalar > ComplexScalar
static void revertSchur(Matrix< ComplexScalar, Rows, Cols, Options, MaxRows, MaxCols > &res, const ComplexMatrix &T, const ComplexMatrix &U)
MatrixType::Nested m_A
Reference to the base of matrix power.
ComplexMatrix m_T
Store the result of Schur decomposition.
void matrix_sqrt_triangular(const MatrixType &arg, ResultType &result)
Compute matrix square root of triangular matrix.
bfloat16 fmod(const bfloat16 &a, const bfloat16 &b)
: TensorContractionSycl.h, provides various tensor contraction kernel for SYCL backend
Eigen::AutoDiffScalar< EIGEN_EXPR_BINARYOP_SCALAR_RETURN_TYPE(Eigen::internal::remove_all_t< DerType >, typename Eigen::internal::traits< Eigen::internal::remove_all_t< DerType >>::Scalar, product) > sinh(const Eigen::AutoDiffScalar< DerType > &x)
Eigen::AutoDiffScalar< EIGEN_EXPR_BINARYOP_SCALAR_RETURN_TYPE(Eigen::internal::remove_all_t< DerType >, typename Eigen::internal::traits< Eigen::internal::remove_all_t< DerType >>::Scalar, product) > log(const Eigen::AutoDiffScalar< DerType > &x)
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
Eigen::AutoDiffScalar< EIGEN_EXPR_BINARYOP_SCALAR_RETURN_TYPE(Eigen::internal::remove_all_t< DerType >, typename Eigen::internal::traits< Eigen::internal::remove_all_t< DerType >>::Scalar, product) > exp(const Eigen::AutoDiffScalar< DerType > &x)
Eigen::AutoDiffScalar< EIGEN_EXPR_BINARYOP_SCALAR_RETURN_TYPE(internal::remove_all_t< DerType >, typename internal::traits< internal::remove_all_t< DerType >>::Scalar, product) > pow(const Eigen::AutoDiffScalar< DerType > &x, const typename internal::traits< internal::remove_all_t< DerType >>::Scalar &y)
const Eigen::CwiseUnaryOp< Eigen::internal::scalar_ceil_op< typename Derived::Scalar >, const Derived > ceil(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_abs_op< typename Derived::Scalar >, const Derived > abs(const Eigen::ArrayBase< Derived > &x)
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_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)
adouble abs(const adouble &x)
adouble imag(const adouble &)