00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #ifndef EIGEN_MATRIX_EXPONENTIAL
00027 #define EIGEN_MATRIX_EXPONENTIAL
00028
00029 #include "StemFunction.h"
00030
00031 namespace Eigen {
00032
00033 #if defined(_MSC_VER) || defined(__FreeBSD__)
00034 template <typename Scalar> Scalar log2(Scalar v) { using std::log; return log(v)/log(Scalar(2)); }
00035 #endif
00036
00037
00043 template <typename MatrixType>
00044 class MatrixExponential {
00045
00046 public:
00047
00055 MatrixExponential(const MatrixType &M);
00056
00061 template <typename ResultType>
00062 void compute(ResultType &result);
00063
00064 private:
00065
00066
00067 MatrixExponential(const MatrixExponential&);
00068 MatrixExponential& operator=(const MatrixExponential&);
00069
00077 void pade3(const MatrixType &A);
00078
00086 void pade5(const MatrixType &A);
00087
00095 void pade7(const MatrixType &A);
00096
00104 void pade9(const MatrixType &A);
00105
00113 void pade13(const MatrixType &A);
00114
00124 void pade17(const MatrixType &A);
00125
00139 void computeUV(double);
00140
00145 void computeUV(float);
00146
00151 void computeUV(long double);
00152
00153 typedef typename internal::traits<MatrixType>::Scalar Scalar;
00154 typedef typename NumTraits<Scalar>::Real RealScalar;
00155 typedef typename std::complex<RealScalar> ComplexScalar;
00156
00158 typename internal::nested<MatrixType>::type m_M;
00159
00161 MatrixType m_U;
00162
00164 MatrixType m_V;
00165
00167 MatrixType m_tmp1;
00168
00170 MatrixType m_tmp2;
00171
00173 MatrixType m_Id;
00174
00176 int m_squarings;
00177
00179 RealScalar m_l1norm;
00180 };
00181
00182 template <typename MatrixType>
00183 MatrixExponential<MatrixType>::MatrixExponential(const MatrixType &M) :
00184 m_M(M),
00185 m_U(M.rows(),M.cols()),
00186 m_V(M.rows(),M.cols()),
00187 m_tmp1(M.rows(),M.cols()),
00188 m_tmp2(M.rows(),M.cols()),
00189 m_Id(MatrixType::Identity(M.rows(), M.cols())),
00190 m_squarings(0),
00191 m_l1norm(M.cwiseAbs().colwise().sum().maxCoeff())
00192 {
00193
00194 }
00195
00196 template <typename MatrixType>
00197 template <typename ResultType>
00198 void MatrixExponential<MatrixType>::compute(ResultType &result)
00199 {
00200 #if LDBL_MANT_DIG > 112 // rarely happens
00201 if(sizeof(RealScalar) > 14) {
00202 result = m_M.matrixFunction(StdStemFunctions<ComplexScalar>::exp);
00203 return;
00204 }
00205 #endif
00206 computeUV(RealScalar());
00207 m_tmp1 = m_U + m_V;
00208 m_tmp2 = -m_U + m_V;
00209 result = m_tmp2.partialPivLu().solve(m_tmp1);
00210 for (int i=0; i<m_squarings; i++)
00211 result *= result;
00212 }
00213
00214 template <typename MatrixType>
00215 EIGEN_STRONG_INLINE void MatrixExponential<MatrixType>::pade3(const MatrixType &A)
00216 {
00217 const RealScalar b[] = {120., 60., 12., 1.};
00218 m_tmp1.noalias() = A * A;
00219 m_tmp2 = b[3]*m_tmp1 + b[1]*m_Id;
00220 m_U.noalias() = A * m_tmp2;
00221 m_V = b[2]*m_tmp1 + b[0]*m_Id;
00222 }
00223
00224 template <typename MatrixType>
00225 EIGEN_STRONG_INLINE void MatrixExponential<MatrixType>::pade5(const MatrixType &A)
00226 {
00227 const RealScalar b[] = {30240., 15120., 3360., 420., 30., 1.};
00228 MatrixType A2 = A * A;
00229 m_tmp1.noalias() = A2 * A2;
00230 m_tmp2 = b[5]*m_tmp1 + b[3]*A2 + b[1]*m_Id;
00231 m_U.noalias() = A * m_tmp2;
00232 m_V = b[4]*m_tmp1 + b[2]*A2 + b[0]*m_Id;
00233 }
00234
00235 template <typename MatrixType>
00236 EIGEN_STRONG_INLINE void MatrixExponential<MatrixType>::pade7(const MatrixType &A)
00237 {
00238 const RealScalar b[] = {17297280., 8648640., 1995840., 277200., 25200., 1512., 56., 1.};
00239 MatrixType A2 = A * A;
00240 MatrixType A4 = A2 * A2;
00241 m_tmp1.noalias() = A4 * A2;
00242 m_tmp2 = b[7]*m_tmp1 + b[5]*A4 + b[3]*A2 + b[1]*m_Id;
00243 m_U.noalias() = A * m_tmp2;
00244 m_V = b[6]*m_tmp1 + b[4]*A4 + b[2]*A2 + b[0]*m_Id;
00245 }
00246
00247 template <typename MatrixType>
00248 EIGEN_STRONG_INLINE void MatrixExponential<MatrixType>::pade9(const MatrixType &A)
00249 {
00250 const RealScalar b[] = {17643225600., 8821612800., 2075673600., 302702400., 30270240.,
00251 2162160., 110880., 3960., 90., 1.};
00252 MatrixType A2 = A * A;
00253 MatrixType A4 = A2 * A2;
00254 MatrixType A6 = A4 * A2;
00255 m_tmp1.noalias() = A6 * A2;
00256 m_tmp2 = b[9]*m_tmp1 + b[7]*A6 + b[5]*A4 + b[3]*A2 + b[1]*m_Id;
00257 m_U.noalias() = A * m_tmp2;
00258 m_V = b[8]*m_tmp1 + b[6]*A6 + b[4]*A4 + b[2]*A2 + b[0]*m_Id;
00259 }
00260
00261 template <typename MatrixType>
00262 EIGEN_STRONG_INLINE void MatrixExponential<MatrixType>::pade13(const MatrixType &A)
00263 {
00264 const RealScalar b[] = {64764752532480000., 32382376266240000., 7771770303897600.,
00265 1187353796428800., 129060195264000., 10559470521600., 670442572800.,
00266 33522128640., 1323241920., 40840800., 960960., 16380., 182., 1.};
00267 MatrixType A2 = A * A;
00268 MatrixType A4 = A2 * A2;
00269 m_tmp1.noalias() = A4 * A2;
00270 m_V = b[13]*m_tmp1 + b[11]*A4 + b[9]*A2;
00271 m_tmp2.noalias() = m_tmp1 * m_V;
00272 m_tmp2 += b[7]*m_tmp1 + b[5]*A4 + b[3]*A2 + b[1]*m_Id;
00273 m_U.noalias() = A * m_tmp2;
00274 m_tmp2 = b[12]*m_tmp1 + b[10]*A4 + b[8]*A2;
00275 m_V.noalias() = m_tmp1 * m_tmp2;
00276 m_V += b[6]*m_tmp1 + b[4]*A4 + b[2]*A2 + b[0]*m_Id;
00277 }
00278
00279 #if LDBL_MANT_DIG > 64
00280 template <typename MatrixType>
00281 EIGEN_STRONG_INLINE void MatrixExponential<MatrixType>::pade17(const MatrixType &A)
00282 {
00283 const RealScalar b[] = {830034394580628357120000.L, 415017197290314178560000.L,
00284 100610229646136770560000.L, 15720348382208870400000.L,
00285 1774878043152614400000.L, 153822763739893248000.L, 10608466464820224000.L,
00286 595373117923584000.L, 27563570274240000.L, 1060137318240000.L,
00287 33924394183680.L, 899510451840.L, 19554575040.L, 341863200.L, 4651200.L,
00288 46512.L, 306.L, 1.L};
00289 MatrixType A2 = A * A;
00290 MatrixType A4 = A2 * A2;
00291 MatrixType A6 = A4 * A2;
00292 m_tmp1.noalias() = A4 * A4;
00293 m_V = b[17]*m_tmp1 + b[15]*A6 + b[13]*A4 + b[11]*A2;
00294 m_tmp2.noalias() = m_tmp1 * m_V;
00295 m_tmp2 += b[9]*m_tmp1 + b[7]*A6 + b[5]*A4 + b[3]*A2 + b[1]*m_Id;
00296 m_U.noalias() = A * m_tmp2;
00297 m_tmp2 = b[16]*m_tmp1 + b[14]*A6 + b[12]*A4 + b[10]*A2;
00298 m_V.noalias() = m_tmp1 * m_tmp2;
00299 m_V += b[8]*m_tmp1 + b[6]*A6 + b[4]*A4 + b[2]*A2 + b[0]*m_Id;
00300 }
00301 #endif
00302
00303 template <typename MatrixType>
00304 void MatrixExponential<MatrixType>::computeUV(float)
00305 {
00306 using std::max;
00307 using std::pow;
00308 using std::ceil;
00309 if (m_l1norm < 4.258730016922831e-001) {
00310 pade3(m_M);
00311 } else if (m_l1norm < 1.880152677804762e+000) {
00312 pade5(m_M);
00313 } else {
00314 const float maxnorm = 3.925724783138660f;
00315 m_squarings = (max)(0, (int)ceil(log2(m_l1norm / maxnorm)));
00316 MatrixType A = m_M / pow(Scalar(2), m_squarings);
00317 pade7(A);
00318 }
00319 }
00320
00321 template <typename MatrixType>
00322 void MatrixExponential<MatrixType>::computeUV(double)
00323 {
00324 using std::max;
00325 using std::pow;
00326 using std::ceil;
00327 if (m_l1norm < 1.495585217958292e-002) {
00328 pade3(m_M);
00329 } else if (m_l1norm < 2.539398330063230e-001) {
00330 pade5(m_M);
00331 } else if (m_l1norm < 9.504178996162932e-001) {
00332 pade7(m_M);
00333 } else if (m_l1norm < 2.097847961257068e+000) {
00334 pade9(m_M);
00335 } else {
00336 const double maxnorm = 5.371920351148152;
00337 m_squarings = (max)(0, (int)ceil(log2(m_l1norm / maxnorm)));
00338 MatrixType A = m_M / pow(Scalar(2), m_squarings);
00339 pade13(A);
00340 }
00341 }
00342
00343 template <typename MatrixType>
00344 void MatrixExponential<MatrixType>::computeUV(long double)
00345 {
00346 using std::max;
00347 using std::pow;
00348 using std::ceil;
00349 #if LDBL_MANT_DIG == 53 // double precision
00350 computeUV(double());
00351 #elif LDBL_MANT_DIG <= 64 // extended precision
00352 if (m_l1norm < 4.1968497232266989671e-003L) {
00353 pade3(m_M);
00354 } else if (m_l1norm < 1.1848116734693823091e-001L) {
00355 pade5(m_M);
00356 } else if (m_l1norm < 5.5170388480686700274e-001L) {
00357 pade7(m_M);
00358 } else if (m_l1norm < 1.3759868875587845383e+000L) {
00359 pade9(m_M);
00360 } else {
00361 const long double maxnorm = 4.0246098906697353063L;
00362 m_squarings = (max)(0, (int)ceil(log2(m_l1norm / maxnorm)));
00363 MatrixType A = m_M / pow(Scalar(2), m_squarings);
00364 pade13(A);
00365 }
00366 #elif LDBL_MANT_DIG <= 106 // double-double
00367 if (m_l1norm < 3.2787892205607026992947488108213e-005L) {
00368 pade3(m_M);
00369 } else if (m_l1norm < 6.4467025060072760084130906076332e-003L) {
00370 pade5(m_M);
00371 } else if (m_l1norm < 6.8988028496595374751374122881143e-002L) {
00372 pade7(m_M);
00373 } else if (m_l1norm < 2.7339737518502231741495857201670e-001L) {
00374 pade9(m_M);
00375 } else if (m_l1norm < 1.3203382096514474905666448850278e+000L) {
00376 pade13(m_M);
00377 } else {
00378 const long double maxnorm = 3.2579440895405400856599663723517L;
00379 m_squarings = (max)(0, (int)ceil(log2(m_l1norm / maxnorm)));
00380 MatrixType A = m_M / pow(Scalar(2), m_squarings);
00381 pade17(A);
00382 }
00383 #elif LDBL_MANT_DIG <= 112 // quadruple precison
00384 if (m_l1norm < 1.639394610288918690547467954466970e-005L) {
00385 pade3(m_M);
00386 } else if (m_l1norm < 4.253237712165275566025884344433009e-003L) {
00387 pade5(m_M);
00388 } else if (m_l1norm < 5.125804063165764409885122032933142e-002L) {
00389 pade7(m_M);
00390 } else if (m_l1norm < 2.170000765161155195453205651889853e-001L) {
00391 pade9(m_M);
00392 } else if (m_l1norm < 1.125358383453143065081397882891878e+000L) {
00393 pade13(m_M);
00394 } else {
00395 const long double maxnorm = 2.884233277829519311757165057717815L;
00396 m_squarings = (max)(0, (int)ceil(log2(m_l1norm / maxnorm)));
00397 MatrixType A = m_M / pow(Scalar(2), m_squarings);
00398 pade17(A);
00399 }
00400 #else
00401
00402 eigen_assert(false && "Bug in MatrixExponential");
00403 #endif // LDBL_MANT_DIG
00404 }
00405
00418 template<typename Derived> struct MatrixExponentialReturnValue
00419 : public ReturnByValue<MatrixExponentialReturnValue<Derived> >
00420 {
00421 typedef typename Derived::Index Index;
00422 public:
00428 MatrixExponentialReturnValue(const Derived& src) : m_src(src) { }
00429
00435 template <typename ResultType>
00436 inline void evalTo(ResultType& result) const
00437 {
00438 const typename Derived::PlainObject srcEvaluated = m_src.eval();
00439 MatrixExponential<typename Derived::PlainObject> me(srcEvaluated);
00440 me.compute(result);
00441 }
00442
00443 Index rows() const { return m_src.rows(); }
00444 Index cols() const { return m_src.cols(); }
00445
00446 protected:
00447 const Derived& m_src;
00448 private:
00449 MatrixExponentialReturnValue& operator=(const MatrixExponentialReturnValue&);
00450 };
00451
00452 namespace internal {
00453 template<typename Derived>
00454 struct traits<MatrixExponentialReturnValue<Derived> >
00455 {
00456 typedef typename Derived::PlainObject ReturnType;
00457 };
00458 }
00459
00460 template <typename Derived>
00461 const MatrixExponentialReturnValue<Derived> MatrixBase<Derived>::exp() const
00462 {
00463 eigen_assert(rows() == cols());
00464 return MatrixExponentialReturnValue<Derived>(derived());
00465 }
00466
00467 }
00468
00469 #endif // EIGEN_MATRIX_EXPONENTIAL