// This file is part of Eigen, a lightweight C++ template library
// for linear algebra.
//
// Copyright (C) 2011, 2013 Jitse Niesen <jitse@maths.leeds.ac.uk>
// Copyright (C) 2011 Chen-Pang He <jdh8@ms63.hinet.net>
//
// This Source Code Form is subject to the terms of the Mozilla
// Public License v. 2.0. If a copy of the MPL was not distributed
// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
#ifndef EIGEN_MATRIX_LOGARITHM
#define EIGEN_MATRIX_LOGARITHM
namespace Eigen {
namespace internal {
template <
typename Scalar>
struct matrix_log_min_pade_degree
{
static const int value =
3 ;
};
template <
typename Scalar>
struct matrix_log_max_pade_degree
{
typedef typename NumTraits<Scalar>::Real RealScalar;
static const int value = std::numeric_limits<RealScalar>::digits<=
24 ?
5 :
// single precision
std::numeric_limits<RealScalar>::digits<=
53 ?
7 :
// double precision
std::numeric_limits<RealScalar>::digits<=
64 ?
8 :
// extended precision
std::numeric_limits<RealScalar>::digits<=
106 ?
10 :
// double-double
11 ;
// quadruple precision
};
/** \brief Compute logarithm of 2x2 triangular matrix. */
template <
typename MatrixType>
void matrix_log_compute_2x2(
const MatrixType& A, MatrixType& result)
{
typedef typename MatrixType::Scalar Scalar;
typedef typename MatrixType::RealScalar RealScalar;
using std::abs;
using std::ceil;
using std::imag;
using std::log;
Scalar logA00 = log(A(
0 ,
0 ));
Scalar logA11 = log(A(
1 ,
1 ));
result(
0 ,
0 ) = logA00;
result(
1 ,
0 ) = Scalar(
0 );
result(
1 ,
1 ) = logA11;
Scalar y = A(
1 ,
1 ) - A(
0 ,
0 );
if (y==Scalar(
0 ))
{
result(
0 ,
1 ) = A(
0 ,
1 ) / A(
0 ,
0 );
}
else if ((abs(A(
0 ,
0 )) < RealScalar(
0 .
5 )*abs(A(
1 ,
1 ))) || (abs(A(
0 ,
0 )) >
2 *abs(A(
1 ,
1 ))))
{
result(
0 ,
1 ) = A(
0 ,
1 ) * (logA11 - logA00) / y;
}
else
{
// computation in previous branch is inaccurate if A(1,1) \approx A(0,0)
RealScalar unwindingNumber = ceil((imag(logA11 - logA00) - RealScalar(EIGEN_PI)) / RealSc
alar(2 *EIGEN_PI));
result(0 ,1 ) = A(0 ,1 ) * (numext::log1p(y/A(0 ,0 )) + Scalar(0 ,RealScalar(2 *EIGEN_PI)*unwindingNumber)) / y;
}
}
/* \brief Get suitable degree for Pade approximation. (specialized for RealScalar = float) */
inline int matrix_log_get_pade_degree(float normTminusI)
{
const float maxNormForPade[] = { 2 .5111573934555054 e-1 /* degree = 3 */ , 4.0535837411880493e-1,
5 .3149729967117310 e-1 };
const int minPadeDegree = matrix_log_min_pade_degree<float >::value;
const int maxPadeDegree = matrix_log_max_pade_degree<float >::value;
int degree = minPadeDegree;
for (; degree <= maxPadeDegree; ++degree)
if (normTminusI <= maxNormForPade[degree - minPadeDegree])
break ;
return degree;
}
/* \brief Get suitable degree for Pade approximation. (specialized for RealScalar = double) */
inline int matrix_log_get_pade_degree(double normTminusI)
{
const double maxNormForPade[] = { 1 .6206284795015624 e-2 /* degree = 3 */ , 5.3873532631381171e-2,
1 .1352802267628681 e-1 , 1 .8662860613541288 e-1 , 2 .642960831111435 e-1 };
const int minPadeDegree = matrix_log_min_pade_degree<double >::value;
const int maxPadeDegree = matrix_log_max_pade_degree<double >::value;
int degree = minPadeDegree;
for (; degree <= maxPadeDegree; ++degree)
if (normTminusI <= maxNormForPade[degree - minPadeDegree])
break ;
return degree;
}
/* \brief Get suitable degree for Pade approximation. (specialized for RealScalar = long double) */
inline int matrix_log_get_pade_degree(long double normTminusI)
{
#if LDBL_MANT_DIG == 53 // double precision
const long double maxNormForPade[] = { 1 .6206284795015624 e-2 L /* degree = 3 */ , 5.3873532631381171e-2L,
1 .1352802267628681 e-1 L, 1 .8662860613541288 e-1 L, 2 .642960831111435 e-1 L };
#elif LDBL_MANT_DIG <= 64 // extended precision
const long double maxNormForPade[] = { 5 .48256690357782863103 e-3 L /* degree = 3 */, 2.34559162387971167321e-2L,
5 .84603923897347449857 e-2 L, 1 .08486423756725170223 e-1 L, 1 .68385767881294446649 e-1 L,
2 .32777776523703892094 e-1 L };
#elif LDBL_MANT_DIG <= 106 // double-double
const long double maxNormForPade[] = { 8 .58970550342939562202529664318890 e-5 L /* degree = 3 */,
9 .34074328446359654039446552677759 e-4 L, 4 .26117194647672175773064114582860 e-3 L,
1 .21546224740281848743149666560464 e-2 L, 2 .61100544998339436713088248557444 e-2 L,
4 .66170074627052749243018566390567 e-2 L, 7 .32585144444135027565872014932387 e-2 L,
1 .05026503471351080481093652651105 e-1 L };
#else // quadruple precision
const long double maxNormForPade[] = { 4 .7419931187193005048501568167858103 e-5 L /* degree = 3 */,
5 .8853168473544560470387769480192666 e-4 L, 2 .9216120366601315391789493628113520 e-3 L,
8 .8415758124319434347116734705174308 e-3 L, 1 .9850836029449446668518049562565291 e-2 L,
3 .6688019729653446926585242192447447 e-2 L, 5 .9290962294020186998954055264528393 e-2 L,
8 .6998436081634343903250580992127677 e-2 L, 1 .1880960220216759245467951592883642 e-1 L };
#endif
const int minPadeDegree = matrix_log_min_pade_degree<long double >::value;
const int maxPadeDegree = matrix_log_max_pade_degree<long double >::value;
int degree = minPadeDegree;
for (; degree <= maxPadeDegree; ++degree)
if (normTminusI <= maxNormForPade[degree - minPadeDegree])
break ;
return degree;
}
/* \brief Compute Pade approximation to matrix logarithm */
template <typename MatrixType>
void matrix_log_compute_pade(MatrixType& result, const MatrixType& T, int degree)
{
typedef typename NumTraits<typename MatrixType::Scalar>::Real RealScalar;
const int minPadeDegree = 3 ;
const int maxPadeDegree = 11 ;
assert(degree >= minPadeDegree && degree <= maxPadeDegree);
// FIXME this creates float-conversion-warnings if these are enabled.
// Either manually convert each value, or disable the warning locally
const RealScalar nodes[][maxPadeDegree] = {
{ 0 .1127016653792583114820734600217600 L, 0 .5000000000000000000000000000000000 L, // degree 3
0 .8872983346207416885179265399782400 L },
{ 0 .0694318442029737123880267555535953 L, 0 .3300094782075718675986671204483777 L, // degree 4
0 .6699905217924281324013328795516223 L, 0 .9305681557970262876119732444464048 L },
{ 0 .0469100770306680036011865608503035 L, 0 .2307653449471584544818427896498956 L, // degree 5
0 .5000000000000000000000000000000000 L, 0 .7692346550528415455181572103501044 L,
0 .9530899229693319963988134391496965 L },
{ 0 .0337652428984239860938492227530027 L, 0 .1693953067668677431693002024900473 L, // degree 6
0 .3806904069584015456847491391596440 L, 0 .6193095930415984543152508608403560 L,
0 .8306046932331322568306997975099527 L, 0 .9662347571015760139061507772469973 L },
{ 0 .0254460438286207377369051579760744 L, 0 .1292344072003027800680676133596058 L, // degree 7
0 .2970774243113014165466967939615193 L, 0 .5000000000000000000000000000000000 L,
0 .7029225756886985834533032060384807 L, 0 .8707655927996972199319323866403942 L,
0 .9745539561713792622630948420239256 L },
{ 0 .0198550717512318841582195657152635 L, 0 .1016667612931866302042230317620848 L, // degree 8
0 .2372337950418355070911304754053768 L, 0 .4082826787521750975302619288199080 L,
0 .5917173212478249024697380711800920 L, 0 .7627662049581644929088695245946232 L,
0 .8983332387068133697957769682379152 L, 0 .9801449282487681158417804342847365 L },
{ 0 .0159198802461869550822118985481636 L, 0 .0819844463366821028502851059651326 L, // degree 9
0 .1933142836497048013456489803292629 L, 0 .3378732882980955354807309926783317 L,
0 .5000000000000000000000000000000000 L, 0 .6621267117019044645192690073216683 L,
0 .8066857163502951986543510196707371 L, 0 .9180155536633178971497148940348674 L,
0 .9840801197538130449177881014518364 L },
{ 0 .0130467357414141399610179939577740 L, 0 .0674683166555077446339516557882535 L, // degree 10
0 .1602952158504877968828363174425632 L, 0 .2833023029353764046003670284171079 L,
0 .4255628305091843945575869994351400 L, 0 .5744371694908156054424130005648600 L,
0 .7166976970646235953996329715828921 L, 0 .8397047841495122031171636825574368 L,
0 .9325316833444922553660483442117465 L, 0 .9869532642585858600389820060422260 L },
{ 0 .0108856709269715035980309994385713 L, 0 .0564687001159523504624211153480364 L, // degree 11
0 .1349239972129753379532918739844233 L, 0 .2404519353965940920371371652706952 L,
0 .3652284220238275138342340072995692 L, 0 .5000000000000000000000000000000000 L,
0 .6347715779761724861657659927004308 L, 0 .7595480646034059079628628347293048 L,
0 .8650760027870246620467081260155767 L, 0 .9435312998840476495375788846519636 L,
0 .9891143290730284964019690005614287 L } };
const RealScalar weights[][maxPadeDegree] = {
{ 0 .2777777777777777777777777777777778 L, 0 .4444444444444444444444444444444444 L, // degree 3
0 .2777777777777777777777777777777778 L },
{ 0 .1739274225687269286865319746109997 L, 0 .3260725774312730713134680253890003 L, // degree 4
0 .3260725774312730713134680253890003 L, 0 .1739274225687269286865319746109997 L },
{ 0 .1184634425280945437571320203599587 L, 0 .2393143352496832340206457574178191 L, // degree 5
0 .2844444444444444444444444444444444 L, 0 .2393143352496832340206457574178191 L,
0 .1184634425280945437571320203599587 L },
{ 0 .0856622461895851725201480710863665 L, 0 .1803807865240693037849167569188581 L, // degree 6
0 .2339569672863455236949351719947755 L, 0 .2339569672863455236949351719947755 L,
0 .1803807865240693037849167569188581 L, 0 .0856622461895851725201480710863665 L },
{ 0 .0647424830844348466353057163395410 L, 0 .1398526957446383339507338857118898 L, // degree 7
0 .1909150252525594724751848877444876 L, 0 .2089795918367346938775510204081633 L,
0 .1909150252525594724751848877444876 L, 0 .1398526957446383339507338857118898 L,
0 .0647424830844348466353057163395410 L },
{ 0 .0506142681451881295762656771549811 L, 0 .1111905172266872352721779972131204 L, // degree 8
0 .1568533229389436436689811009933007 L, 0 .1813418916891809914825752246385978 L,
0 .1813418916891809914825752246385978 L, 0 .1568533229389436436689811009933007 L,
0 .1111905172266872352721779972131204 L, 0 .0506142681451881295762656771549811 L },
{ 0 .0406371941807872059859460790552618 L, 0 .0903240803474287020292360156214564 L, // degree 9
0 .1303053482014677311593714347093164 L, 0 .1561735385200014200343152032922218 L,
0 .1651196775006298815822625346434870 L, 0 .1561735385200014200343152032922218 L,
0 .1303053482014677311593714347093164 L, 0 .0903240803474287020292360156214564 L,
0 .0406371941807872059859460790552618 L },
{ 0 .0333356721543440687967844049466659 L, 0 .0747256745752902965728881698288487 L, // degree 10
0 .1095431812579910219977674671140816 L, 0 .1346333596549981775456134607847347 L,
0 .1477621123573764350869464973256692 L, 0 .1477621123573764350869464973256692 L,
0 .1346333596549981775456134607847347 L, 0 .1095431812579910219977674671140816 L,
0 .0747256745752902965728881698288487 L, 0 .0333356721543440687967844049466659 L },
{ 0 .0278342835580868332413768602212743 L, 0 .0627901847324523123173471496119701 L, // degree 11
0 .0931451054638671257130488207158280 L, 0 .1165968822959952399592618524215876 L,
0 .1314022722551233310903444349452546 L, 0 .1364625433889503153572417641681711 L,
0 .1314022722551233310903444349452546 L, 0 .1165968822959952399592618524215876 L,
0 .0931451054638671257130488207158280 L, 0 .0627901847324523123173471496119701 L,
0 .0278342835580868332413768602212743 L } };
MatrixType TminusI = T - MatrixType::Identity(T.rows(), T.rows());
result.setZero(T.rows(), T.rows());
for (int k = 0 ; k < degree; ++k) {
RealScalar weight = weights[degree-minPadeDegree][k];
RealScalar node = nodes[degree-minPadeDegree][k];
result += weight * (MatrixType::Identity(T.rows(), T.rows()) + node * TminusI)
.template triangularView<Upper>().solve(TminusI);
}
}
/** \brief Compute logarithm of triangular matrices with size > 2.
* \details This uses a inverse scale-and-square algorithm. */
template <typename MatrixType>
void matrix_log_compute_big(const MatrixType& A, MatrixType& result)
{
typedef typename MatrixType::Scalar Scalar;
typedef typename NumTraits<Scalar>::Real RealScalar;
using std::pow;
int numberOfSquareRoots = 0 ;
int numberOfExtraSquareRoots = 0 ;
int degree;
MatrixType T = A, sqrtT;
const int maxPadeDegree = matrix_log_max_pade_degree<Scalar>::value;
const RealScalar maxNormForPade = RealScalar(
maxPadeDegree<= 5 ? 5 .3149729967117310 e-1 L: // single precision
maxPadeDegree<= 7 ? 2 .6429608311114350 e-1 L: // double precision
maxPadeDegree<= 8 ? 2 .32777776523703892094 e-1 L: // extended precision
maxPadeDegree<=10 ? 1 .05026503471351080481093652651105 e-1 L: // double-double
1 .1880960220216759245467951592883642 e-1 L); // quadruple precision
while (true ) {
RealScalar normTminusI = (T - MatrixType::Identity(T.rows(), T.rows())).cwiseAbs().colwise().sum().maxCoeff();
if (normTminusI < maxNormForPade) {
degree = matrix_log_get_pade_degree(normTminusI);
int degree2 = matrix_log_get_pade_degree(normTminusI / RealScalar(2 ));
if ((degree - degree2 <= 1 ) || (numberOfExtraSquareRoots == 1 ))
break ;
++numberOfExtraSquareRoots;
}
matrix_sqrt_triangular(T, sqrtT);
T = sqrtT.template triangularView<Upper>();
++numberOfSquareRoots;
}
matrix_log_compute_pade(result, T, degree);
result *= pow(RealScalar(2 ), RealScalar(numberOfSquareRoots)); // TODO replace by bitshift if possible
}
/** \ingroup MatrixFunctions_Module
* \class MatrixLogarithmAtomic
* \brief Helper class for computing matrix logarithm of atomic matrices.
*
* Here, an atomic matrix is a triangular matrix whose diagonal entries are close to each other.
*
* \sa class MatrixFunctionAtomic, MatrixBase::log()
*/
template <typename MatrixType>
class MatrixLogarithmAtomic
{
public :
/** \brief Compute matrix logarithm of atomic matrix
* \param[in] A argument of matrix logarithm, should be upper triangular and atomic
* \returns The logarithm of \p A.
*/
MatrixType compute(const MatrixType& A);
};
template <typename MatrixType>
MatrixType MatrixLogarithmAtomic<MatrixType>::compute(const MatrixType& A)
{
using std::log;
MatrixType result(A.rows(), A.rows());
if (A.rows() == 1 )
result(0 ,0 ) = log(A(0 ,0 ));
else if (A.rows() == 2 )
matrix_log_compute_2x2(A, result);
else
matrix_log_compute_big(A, result);
return result;
}
} // end of namespace internal
/** \ingroup MatrixFunctions_Module
*
* \brief Proxy for the matrix logarithm of some matrix (expression).
*
* \tparam Derived Type of the argument to the matrix function.
*
* This class holds the argument to the matrix function until it is
* assigned or evaluated for some other reason (so the argument
* should not be changed in the meantime). It is the return type of
* MatrixBase::log() and most of the time this is the only way it
* is used.
*/
template <typename Derived> class MatrixLogarithmReturnValue
: public ReturnByValue<MatrixLogarithmReturnValue<Derived> >
{
public :
typedef typename Derived::Scalar Scalar;
typedef typename Derived::Index Index;
protected :
typedef typename internal::ref_selector<Derived>::type DerivedNested;
public :
/** \brief Constructor.
*
* \param[in] A %Matrix (expression) forming the argument of the matrix logarithm.
*/
explicit MatrixLogarithmReturnValue(const Derived& A) : m_A(A) { }
/** \brief Compute the matrix logarithm.
*
* \param[out] result Logarithm of \c A, where \c A is as specified in the constructor.
*/
template <typename ResultType>
inline void evalTo(ResultType& result) const
{
typedef typename internal::nested_eval<Derived, 10 >::type DerivedEvalType;
typedef typename internal::remove_all<DerivedEvalType>::type DerivedEvalTypeClean;
typedef internal::traits<DerivedEvalTypeClean> Traits;
typedef std::complex<typename NumTraits<Scalar>::Real> ComplexScalar;
typedef Matrix<ComplexScalar, Dynamic, Dynamic, 0 , Traits::RowsAtCompileTime, Traits::ColsAtCompileTime> DynMatrixType;
typedef internal::MatrixLogarithmAtomic<DynMatrixType> AtomicType;
AtomicType atomic;
internal::matrix_function_compute<typename DerivedEvalTypeClean::PlainObject>::run(m_A, atomic, result);
}
Index rows() const { return m_A.rows(); }
Index cols() const { return m_A.cols(); }
private :
const DerivedNested m_A;
};
namespace internal {
template <typename Derived>
struct traits<MatrixLogarithmReturnValue<Derived> >
{
typedef typename Derived::PlainObject ReturnType;
};
}
/********** MatrixBase method **********/
template <typename Derived>
const MatrixLogarithmReturnValue<Derived> MatrixBase<Derived>::log() const
{
eigen_assert(rows() == cols());
return MatrixLogarithmReturnValue<Derived>(derived());
}
} // end namespace Eigen
#endif // EIGEN_MATRIX_LOGARITHM
Messung V0.5 in Prozent C=89 H=89 G=88
¤ Dauer der Verarbeitung: 0.12 Sekunden
(vorverarbeitet am 2026-06-06)
¤
*© Formatika GbR, Deutschland