Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:57:05

0001 // This file is part of Eigen, a lightweight C++ template library
0002 // for linear algebra.
0003 //
0004 // Copyright (C) 2011, 2013 Jitse Niesen <jitse@maths.leeds.ac.uk>
0005 // Copyright (C) 2011 Chen-Pang He <jdh8@ms63.hinet.net>
0006 //
0007 // This Source Code Form is subject to the terms of the Mozilla
0008 // Public License v. 2.0. If a copy of the MPL was not distributed
0009 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
0010 
0011 #ifndef EIGEN_MATRIX_LOGARITHM
0012 #define EIGEN_MATRIX_LOGARITHM
0013 
0014 namespace Eigen { 
0015 
0016 namespace internal { 
0017 
0018 template <typename Scalar>
0019 struct matrix_log_min_pade_degree 
0020 {
0021   static const int value = 3;
0022 };
0023 
0024 template <typename Scalar>
0025 struct matrix_log_max_pade_degree 
0026 {
0027   typedef typename NumTraits<Scalar>::Real RealScalar;
0028   static const int value = std::numeric_limits<RealScalar>::digits<= 24?  5:  // single precision
0029                            std::numeric_limits<RealScalar>::digits<= 53?  7:  // double precision
0030                            std::numeric_limits<RealScalar>::digits<= 64?  8:  // extended precision
0031                            std::numeric_limits<RealScalar>::digits<=106? 10:  // double-double
0032                                                                          11;  // quadruple precision
0033 };
0034 
0035 /** \brief Compute logarithm of 2x2 triangular matrix. */
0036 template <typename MatrixType>
0037 void matrix_log_compute_2x2(const MatrixType& A, MatrixType& result)
0038 {
0039   typedef typename MatrixType::Scalar Scalar;
0040   typedef typename MatrixType::RealScalar RealScalar;
0041   using std::abs;
0042   using std::ceil;
0043   using std::imag;
0044   using std::log;
0045 
0046   Scalar logA00 = log(A(0,0));
0047   Scalar logA11 = log(A(1,1));
0048 
0049   result(0,0) = logA00;
0050   result(1,0) = Scalar(0);
0051   result(1,1) = logA11;
0052 
0053   Scalar y = A(1,1) - A(0,0);
0054   if (y==Scalar(0))
0055   {
0056     result(0,1) = A(0,1) / A(0,0);
0057   }
0058   else if ((abs(A(0,0)) < RealScalar(0.5)*abs(A(1,1))) || (abs(A(0,0)) > 2*abs(A(1,1))))
0059   {
0060     result(0,1) = A(0,1) * (logA11 - logA00) / y;
0061   }
0062   else
0063   {
0064     // computation in previous branch is inaccurate if A(1,1) \approx A(0,0)
0065     RealScalar unwindingNumber = ceil((imag(logA11 - logA00) - RealScalar(EIGEN_PI)) / RealScalar(2*EIGEN_PI));
0066     result(0,1) = A(0,1) * (numext::log1p(y/A(0,0)) + Scalar(0,RealScalar(2*EIGEN_PI)*unwindingNumber)) / y;
0067   }
0068 }
0069 
0070 /* \brief Get suitable degree for Pade approximation. (specialized for RealScalar = float) */
0071 inline int matrix_log_get_pade_degree(float normTminusI)
0072 {
0073   const float maxNormForPade[] = { 2.5111573934555054e-1 /* degree = 3 */ , 4.0535837411880493e-1,
0074             5.3149729967117310e-1 };
0075   const int minPadeDegree = matrix_log_min_pade_degree<float>::value;
0076   const int maxPadeDegree = matrix_log_max_pade_degree<float>::value;
0077   int degree = minPadeDegree;
0078   for (; degree <= maxPadeDegree; ++degree) 
0079     if (normTminusI <= maxNormForPade[degree - minPadeDegree])
0080       break;
0081   return degree;
0082 }
0083 
0084 /* \brief Get suitable degree for Pade approximation. (specialized for RealScalar = double) */
0085 inline int matrix_log_get_pade_degree(double normTminusI)
0086 {
0087   const double maxNormForPade[] = { 1.6206284795015624e-2 /* degree = 3 */ , 5.3873532631381171e-2,
0088             1.1352802267628681e-1, 1.8662860613541288e-1, 2.642960831111435e-1 };
0089   const int minPadeDegree = matrix_log_min_pade_degree<double>::value;
0090   const int maxPadeDegree = matrix_log_max_pade_degree<double>::value;
0091   int degree = minPadeDegree;
0092   for (; degree <= maxPadeDegree; ++degree)
0093     if (normTminusI <= maxNormForPade[degree - minPadeDegree])
0094       break;
0095   return degree;
0096 }
0097 
0098 /* \brief Get suitable degree for Pade approximation. (specialized for RealScalar = long double) */
0099 inline int matrix_log_get_pade_degree(long double normTminusI)
0100 {
0101 #if   LDBL_MANT_DIG == 53         // double precision
0102   const long double maxNormForPade[] = { 1.6206284795015624e-2L /* degree = 3 */ , 5.3873532631381171e-2L,
0103             1.1352802267628681e-1L, 1.8662860613541288e-1L, 2.642960831111435e-1L };
0104 #elif LDBL_MANT_DIG <= 64         // extended precision
0105   const long double maxNormForPade[] = { 5.48256690357782863103e-3L /* degree = 3 */, 2.34559162387971167321e-2L,
0106             5.84603923897347449857e-2L, 1.08486423756725170223e-1L, 1.68385767881294446649e-1L,
0107             2.32777776523703892094e-1L };
0108 #elif LDBL_MANT_DIG <= 106        // double-double
0109   const long double maxNormForPade[] = { 8.58970550342939562202529664318890e-5L /* degree = 3 */,
0110             9.34074328446359654039446552677759e-4L, 4.26117194647672175773064114582860e-3L,
0111             1.21546224740281848743149666560464e-2L, 2.61100544998339436713088248557444e-2L,
0112             4.66170074627052749243018566390567e-2L, 7.32585144444135027565872014932387e-2L,
0113             1.05026503471351080481093652651105e-1L };
0114 #else                             // quadruple precision
0115   const long double maxNormForPade[] = { 4.7419931187193005048501568167858103e-5L /* degree = 3 */,
0116             5.8853168473544560470387769480192666e-4L, 2.9216120366601315391789493628113520e-3L,
0117             8.8415758124319434347116734705174308e-3L, 1.9850836029449446668518049562565291e-2L,
0118             3.6688019729653446926585242192447447e-2L, 5.9290962294020186998954055264528393e-2L,
0119             8.6998436081634343903250580992127677e-2L, 1.1880960220216759245467951592883642e-1L };
0120 #endif
0121   const int minPadeDegree = matrix_log_min_pade_degree<long double>::value;
0122   const int maxPadeDegree = matrix_log_max_pade_degree<long double>::value;
0123   int degree = minPadeDegree;
0124   for (; degree <= maxPadeDegree; ++degree)
0125     if (normTminusI <= maxNormForPade[degree - minPadeDegree])
0126       break;
0127   return degree;
0128 }
0129 
0130 /* \brief Compute Pade approximation to matrix logarithm */
0131 template <typename MatrixType>
0132 void matrix_log_compute_pade(MatrixType& result, const MatrixType& T, int degree)
0133 {
0134   typedef typename NumTraits<typename MatrixType::Scalar>::Real RealScalar;
0135   const int minPadeDegree = 3;
0136   const int maxPadeDegree = 11;
0137   assert(degree >= minPadeDegree && degree <= maxPadeDegree);
0138   // FIXME this creates float-conversion-warnings if these are enabled.
0139   // Either manually convert each value, or disable the warning locally
0140   const RealScalar nodes[][maxPadeDegree] = { 
0141     { 0.1127016653792583114820734600217600L, 0.5000000000000000000000000000000000L,  // degree 3
0142       0.8872983346207416885179265399782400L }, 
0143     { 0.0694318442029737123880267555535953L, 0.3300094782075718675986671204483777L,  // degree 4
0144       0.6699905217924281324013328795516223L, 0.9305681557970262876119732444464048L },
0145     { 0.0469100770306680036011865608503035L, 0.2307653449471584544818427896498956L,  // degree 5
0146       0.5000000000000000000000000000000000L, 0.7692346550528415455181572103501044L,
0147       0.9530899229693319963988134391496965L },
0148     { 0.0337652428984239860938492227530027L, 0.1693953067668677431693002024900473L,  // degree 6
0149       0.3806904069584015456847491391596440L, 0.6193095930415984543152508608403560L,
0150       0.8306046932331322568306997975099527L, 0.9662347571015760139061507772469973L },
0151     { 0.0254460438286207377369051579760744L, 0.1292344072003027800680676133596058L,  // degree 7
0152       0.2970774243113014165466967939615193L, 0.5000000000000000000000000000000000L,
0153       0.7029225756886985834533032060384807L, 0.8707655927996972199319323866403942L,
0154       0.9745539561713792622630948420239256L },
0155     { 0.0198550717512318841582195657152635L, 0.1016667612931866302042230317620848L,  // degree 8
0156       0.2372337950418355070911304754053768L, 0.4082826787521750975302619288199080L,
0157       0.5917173212478249024697380711800920L, 0.7627662049581644929088695245946232L,
0158       0.8983332387068133697957769682379152L, 0.9801449282487681158417804342847365L },
0159     { 0.0159198802461869550822118985481636L, 0.0819844463366821028502851059651326L,  // degree 9
0160       0.1933142836497048013456489803292629L, 0.3378732882980955354807309926783317L,
0161       0.5000000000000000000000000000000000L, 0.6621267117019044645192690073216683L,
0162       0.8066857163502951986543510196707371L, 0.9180155536633178971497148940348674L,
0163       0.9840801197538130449177881014518364L },
0164     { 0.0130467357414141399610179939577740L, 0.0674683166555077446339516557882535L,  // degree 10
0165       0.1602952158504877968828363174425632L, 0.2833023029353764046003670284171079L,
0166       0.4255628305091843945575869994351400L, 0.5744371694908156054424130005648600L,
0167       0.7166976970646235953996329715828921L, 0.8397047841495122031171636825574368L,
0168       0.9325316833444922553660483442117465L, 0.9869532642585858600389820060422260L },
0169     { 0.0108856709269715035980309994385713L, 0.0564687001159523504624211153480364L,  // degree 11
0170       0.1349239972129753379532918739844233L, 0.2404519353965940920371371652706952L,
0171       0.3652284220238275138342340072995692L, 0.5000000000000000000000000000000000L,
0172       0.6347715779761724861657659927004308L, 0.7595480646034059079628628347293048L,
0173       0.8650760027870246620467081260155767L, 0.9435312998840476495375788846519636L,
0174       0.9891143290730284964019690005614287L } };
0175 
0176   const RealScalar weights[][maxPadeDegree] = { 
0177     { 0.2777777777777777777777777777777778L, 0.4444444444444444444444444444444444L,  // degree 3
0178       0.2777777777777777777777777777777778L },
0179     { 0.1739274225687269286865319746109997L, 0.3260725774312730713134680253890003L,  // degree 4
0180       0.3260725774312730713134680253890003L, 0.1739274225687269286865319746109997L },
0181     { 0.1184634425280945437571320203599587L, 0.2393143352496832340206457574178191L,  // degree 5
0182       0.2844444444444444444444444444444444L, 0.2393143352496832340206457574178191L,
0183       0.1184634425280945437571320203599587L },
0184     { 0.0856622461895851725201480710863665L, 0.1803807865240693037849167569188581L,  // degree 6
0185       0.2339569672863455236949351719947755L, 0.2339569672863455236949351719947755L,
0186       0.1803807865240693037849167569188581L, 0.0856622461895851725201480710863665L },
0187     { 0.0647424830844348466353057163395410L, 0.1398526957446383339507338857118898L,  // degree 7
0188       0.1909150252525594724751848877444876L, 0.2089795918367346938775510204081633L,
0189       0.1909150252525594724751848877444876L, 0.1398526957446383339507338857118898L,
0190       0.0647424830844348466353057163395410L },
0191     { 0.0506142681451881295762656771549811L, 0.1111905172266872352721779972131204L,  // degree 8
0192       0.1568533229389436436689811009933007L, 0.1813418916891809914825752246385978L,
0193       0.1813418916891809914825752246385978L, 0.1568533229389436436689811009933007L,
0194       0.1111905172266872352721779972131204L, 0.0506142681451881295762656771549811L },
0195     { 0.0406371941807872059859460790552618L, 0.0903240803474287020292360156214564L,  // degree 9
0196       0.1303053482014677311593714347093164L, 0.1561735385200014200343152032922218L,
0197       0.1651196775006298815822625346434870L, 0.1561735385200014200343152032922218L,
0198       0.1303053482014677311593714347093164L, 0.0903240803474287020292360156214564L,
0199       0.0406371941807872059859460790552618L },
0200     { 0.0333356721543440687967844049466659L, 0.0747256745752902965728881698288487L,  // degree 10
0201       0.1095431812579910219977674671140816L, 0.1346333596549981775456134607847347L,
0202       0.1477621123573764350869464973256692L, 0.1477621123573764350869464973256692L,
0203       0.1346333596549981775456134607847347L, 0.1095431812579910219977674671140816L,
0204       0.0747256745752902965728881698288487L, 0.0333356721543440687967844049466659L },
0205     { 0.0278342835580868332413768602212743L, 0.0627901847324523123173471496119701L,  // degree 11
0206       0.0931451054638671257130488207158280L, 0.1165968822959952399592618524215876L,
0207       0.1314022722551233310903444349452546L, 0.1364625433889503153572417641681711L,
0208       0.1314022722551233310903444349452546L, 0.1165968822959952399592618524215876L,
0209       0.0931451054638671257130488207158280L, 0.0627901847324523123173471496119701L,
0210       0.0278342835580868332413768602212743L } };
0211 
0212   MatrixType TminusI = T - MatrixType::Identity(T.rows(), T.rows());
0213   result.setZero(T.rows(), T.rows());
0214   for (int k = 0; k < degree; ++k) {
0215     RealScalar weight = weights[degree-minPadeDegree][k];
0216     RealScalar node = nodes[degree-minPadeDegree][k];
0217     result += weight * (MatrixType::Identity(T.rows(), T.rows()) + node * TminusI)
0218                        .template triangularView<Upper>().solve(TminusI);
0219   }
0220 } 
0221 
0222 /** \brief Compute logarithm of triangular matrices with size > 2. 
0223   * \details This uses a inverse scale-and-square algorithm. */
0224 template <typename MatrixType>
0225 void matrix_log_compute_big(const MatrixType& A, MatrixType& result)
0226 {
0227   typedef typename MatrixType::Scalar Scalar;
0228   typedef typename NumTraits<Scalar>::Real RealScalar;
0229   using std::pow;
0230 
0231   int numberOfSquareRoots = 0;
0232   int numberOfExtraSquareRoots = 0;
0233   int degree;
0234   MatrixType T = A, sqrtT;
0235 
0236   const int maxPadeDegree = matrix_log_max_pade_degree<Scalar>::value;
0237   const RealScalar maxNormForPade = RealScalar(
0238                                     maxPadeDegree<= 5? 5.3149729967117310e-1L:                    // single precision
0239                                     maxPadeDegree<= 7? 2.6429608311114350e-1L:                    // double precision
0240                                     maxPadeDegree<= 8? 2.32777776523703892094e-1L:                // extended precision
0241                                     maxPadeDegree<=10? 1.05026503471351080481093652651105e-1L:    // double-double
0242                                                        1.1880960220216759245467951592883642e-1L); // quadruple precision
0243 
0244   while (true) {
0245     RealScalar normTminusI = (T - MatrixType::Identity(T.rows(), T.rows())).cwiseAbs().colwise().sum().maxCoeff();
0246     if (normTminusI < maxNormForPade) {
0247       degree = matrix_log_get_pade_degree(normTminusI);
0248       int degree2 = matrix_log_get_pade_degree(normTminusI / RealScalar(2));
0249       if ((degree - degree2 <= 1) || (numberOfExtraSquareRoots == 1)) 
0250         break;
0251       ++numberOfExtraSquareRoots;
0252     }
0253     matrix_sqrt_triangular(T, sqrtT);
0254     T = sqrtT.template triangularView<Upper>();
0255     ++numberOfSquareRoots;
0256   }
0257 
0258   matrix_log_compute_pade(result, T, degree);
0259   result *= pow(RealScalar(2), RealScalar(numberOfSquareRoots)); // TODO replace by bitshift if possible
0260 }
0261 
0262 /** \ingroup MatrixFunctions_Module
0263   * \class MatrixLogarithmAtomic
0264   * \brief Helper class for computing matrix logarithm of atomic matrices.
0265   *
0266   * Here, an atomic matrix is a triangular matrix whose diagonal entries are close to each other.
0267   *
0268   * \sa class MatrixFunctionAtomic, MatrixBase::log()
0269   */
0270 template <typename MatrixType>
0271 class MatrixLogarithmAtomic
0272 {
0273 public:
0274   /** \brief Compute matrix logarithm of atomic matrix
0275     * \param[in]  A  argument of matrix logarithm, should be upper triangular and atomic
0276     * \returns  The logarithm of \p A.
0277     */
0278   MatrixType compute(const MatrixType& A);
0279 };
0280 
0281 template <typename MatrixType>
0282 MatrixType MatrixLogarithmAtomic<MatrixType>::compute(const MatrixType& A)
0283 {
0284   using std::log;
0285   MatrixType result(A.rows(), A.rows());
0286   if (A.rows() == 1)
0287     result(0,0) = log(A(0,0));
0288   else if (A.rows() == 2)
0289     matrix_log_compute_2x2(A, result);
0290   else
0291     matrix_log_compute_big(A, result);
0292   return result;
0293 }
0294 
0295 } // end of namespace internal
0296 
0297 /** \ingroup MatrixFunctions_Module
0298   *
0299   * \brief Proxy for the matrix logarithm of some matrix (expression).
0300   *
0301   * \tparam Derived  Type of the argument to the matrix function.
0302   *
0303   * This class holds the argument to the matrix function until it is
0304   * assigned or evaluated for some other reason (so the argument
0305   * should not be changed in the meantime). It is the return type of
0306   * MatrixBase::log() and most of the time this is the only way it
0307   * is used.
0308   */
0309 template<typename Derived> class MatrixLogarithmReturnValue
0310 : public ReturnByValue<MatrixLogarithmReturnValue<Derived> >
0311 {
0312 public:
0313   typedef typename Derived::Scalar Scalar;
0314   typedef typename Derived::Index Index;
0315 
0316 protected:
0317   typedef typename internal::ref_selector<Derived>::type DerivedNested;
0318 
0319 public:
0320 
0321   /** \brief Constructor.
0322     *
0323     * \param[in]  A  %Matrix (expression) forming the argument of the matrix logarithm.
0324     */
0325   explicit MatrixLogarithmReturnValue(const Derived& A) : m_A(A) { }
0326   
0327   /** \brief Compute the matrix logarithm.
0328     *
0329     * \param[out]  result  Logarithm of \c A, where \c A is as specified in the constructor.
0330     */
0331   template <typename ResultType>
0332   inline void evalTo(ResultType& result) const
0333   {
0334     typedef typename internal::nested_eval<Derived, 10>::type DerivedEvalType;
0335     typedef typename internal::remove_all<DerivedEvalType>::type DerivedEvalTypeClean;
0336     typedef internal::traits<DerivedEvalTypeClean> Traits;
0337     typedef std::complex<typename NumTraits<Scalar>::Real> ComplexScalar;
0338     typedef Matrix<ComplexScalar, Dynamic, Dynamic, 0, Traits::RowsAtCompileTime, Traits::ColsAtCompileTime> DynMatrixType;
0339     typedef internal::MatrixLogarithmAtomic<DynMatrixType> AtomicType;
0340     AtomicType atomic;
0341     
0342     internal::matrix_function_compute<typename DerivedEvalTypeClean::PlainObject>::run(m_A, atomic, result);
0343   }
0344 
0345   Index rows() const { return m_A.rows(); }
0346   Index cols() const { return m_A.cols(); }
0347   
0348 private:
0349   const DerivedNested m_A;
0350 };
0351 
0352 namespace internal {
0353   template<typename Derived>
0354   struct traits<MatrixLogarithmReturnValue<Derived> >
0355   {
0356     typedef typename Derived::PlainObject ReturnType;
0357   };
0358 }
0359 
0360 
0361 /********** MatrixBase method **********/
0362 
0363 
0364 template <typename Derived>
0365 const MatrixLogarithmReturnValue<Derived> MatrixBase<Derived>::log() const
0366 {
0367   eigen_assert(rows() == cols());
0368   return MatrixLogarithmReturnValue<Derived>(derived());
0369 }
0370 
0371 } // end namespace Eigen
0372 
0373 #endif // EIGEN_MATRIX_LOGARITHM