MatrixExponential.h
1 // This file is part of Eigen, a lightweight C++ template library
2 // for linear algebra.
3 //
4 // Copyright (C) 2009, 2010 Jitse Niesen <jitse@maths.leeds.ac.uk>
5 // Copyright (C) 2011 Chen-Pang He <jdh8@ms63.hinet.net>
6 //
7 // Eigen is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU Lesser General Public
9 // License as published by the Free Software Foundation; either
10 // version 3 of the License, or (at your option) any later version.
11 //
12 // Alternatively, you can redistribute it and/or
13 // modify it under the terms of the GNU General Public License as
14 // published by the Free Software Foundation; either version 2 of
15 // the License, or (at your option) any later version.
16 //
17 // Eigen is distributed in the hope that it will be useful, but WITHOUT ANY
18 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19 // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the
20 // GNU General Public License for more details.
21 //
22 // You should have received a copy of the GNU Lesser General Public
23 // License and a copy of the GNU General Public License along with
24 // Eigen. If not, see <http://www.gnu.org/licenses/>.
25 
26 #ifndef EIGEN_MATRIX_EXPONENTIAL
27 #define EIGEN_MATRIX_EXPONENTIAL
28 
29 #include "StemFunction.h"
30 
31 namespace Eigen {
32 
33 #if defined(_MSC_VER) || defined(__FreeBSD__)
34  template <typename Scalar> Scalar log2(Scalar v) { using std::log; return log(v)/log(Scalar(2)); }
35 #endif
36 
37 
43 template <typename MatrixType>
45 
46  public:
47 
55  MatrixExponential(const MatrixType &M);
56 
61  template <typename ResultType>
62  void compute(ResultType &result);
63 
64  private:
65 
66  // Prevent copying
68  MatrixExponential& operator=(const MatrixExponential&);
69 
77  void pade3(const MatrixType &A);
78 
86  void pade5(const MatrixType &A);
87 
95  void pade7(const MatrixType &A);
96 
104  void pade9(const MatrixType &A);
105 
113  void pade13(const MatrixType &A);
114 
124  void pade17(const MatrixType &A);
125 
139  void computeUV(double);
140 
145  void computeUV(float);
146 
151  void computeUV(long double);
152 
153  typedef typename internal::traits<MatrixType>::Scalar Scalar;
154  typedef typename NumTraits<Scalar>::Real RealScalar;
155  typedef typename std::complex<RealScalar> ComplexScalar;
156 
158  typename internal::nested<MatrixType>::type m_M;
159 
161  MatrixType m_U;
162 
164  MatrixType m_V;
165 
167  MatrixType m_tmp1;
168 
170  MatrixType m_tmp2;
171 
173  MatrixType m_Id;
174 
176  int m_squarings;
177 
179  RealScalar m_l1norm;
180 };
181 
182 template <typename MatrixType>
184  m_M(M),
185  m_U(M.rows(),M.cols()),
186  m_V(M.rows(),M.cols()),
187  m_tmp1(M.rows(),M.cols()),
188  m_tmp2(M.rows(),M.cols()),
189  m_Id(MatrixType::Identity(M.rows(), M.cols())),
190  m_squarings(0),
191  m_l1norm(M.cwiseAbs().colwise().sum().maxCoeff())
192 {
193  /* empty body */
194 }
195 
196 template <typename MatrixType>
197 template <typename ResultType>
199 {
200 #if LDBL_MANT_DIG > 112 // rarely happens
201  if(sizeof(RealScalar) > 14) {
202  result = m_M.matrixFunction(StdStemFunctions<ComplexScalar>::exp);
203  return;
204  }
205 #endif
206  computeUV(RealScalar());
207  m_tmp1 = m_U + m_V; // numerator of Pade approximant
208  m_tmp2 = -m_U + m_V; // denominator of Pade approximant
209  result = m_tmp2.partialPivLu().solve(m_tmp1);
210  for (int i=0; i<m_squarings; i++)
211  result *= result; // undo scaling by repeated squaring
212 }
213 
214 template <typename MatrixType>
215 EIGEN_STRONG_INLINE void MatrixExponential<MatrixType>::pade3(const MatrixType &A)
216 {
217  const RealScalar b[] = {120., 60., 12., 1.};
218  m_tmp1.noalias() = A * A;
219  m_tmp2 = b[3]*m_tmp1 + b[1]*m_Id;
220  m_U.noalias() = A * m_tmp2;
221  m_V = b[2]*m_tmp1 + b[0]*m_Id;
222 }
223 
224 template <typename MatrixType>
225 EIGEN_STRONG_INLINE void MatrixExponential<MatrixType>::pade5(const MatrixType &A)
226 {
227  const RealScalar b[] = {30240., 15120., 3360., 420., 30., 1.};
228  MatrixType A2 = A * A;
229  m_tmp1.noalias() = A2 * A2;
230  m_tmp2 = b[5]*m_tmp1 + b[3]*A2 + b[1]*m_Id;
231  m_U.noalias() = A * m_tmp2;
232  m_V = b[4]*m_tmp1 + b[2]*A2 + b[0]*m_Id;
233 }
234 
235 template <typename MatrixType>
236 EIGEN_STRONG_INLINE void MatrixExponential<MatrixType>::pade7(const MatrixType &A)
237 {
238  const RealScalar b[] = {17297280., 8648640., 1995840., 277200., 25200., 1512., 56., 1.};
239  MatrixType A2 = A * A;
240  MatrixType A4 = A2 * A2;
241  m_tmp1.noalias() = A4 * A2;
242  m_tmp2 = b[7]*m_tmp1 + b[5]*A4 + b[3]*A2 + b[1]*m_Id;
243  m_U.noalias() = A * m_tmp2;
244  m_V = b[6]*m_tmp1 + b[4]*A4 + b[2]*A2 + b[0]*m_Id;
245 }
246 
247 template <typename MatrixType>
248 EIGEN_STRONG_INLINE void MatrixExponential<MatrixType>::pade9(const MatrixType &A)
249 {
250  const RealScalar b[] = {17643225600., 8821612800., 2075673600., 302702400., 30270240.,
251  2162160., 110880., 3960., 90., 1.};
252  MatrixType A2 = A * A;
253  MatrixType A4 = A2 * A2;
254  MatrixType A6 = A4 * A2;
255  m_tmp1.noalias() = A6 * A2;
256  m_tmp2 = b[9]*m_tmp1 + b[7]*A6 + b[5]*A4 + b[3]*A2 + b[1]*m_Id;
257  m_U.noalias() = A * m_tmp2;
258  m_V = b[8]*m_tmp1 + b[6]*A6 + b[4]*A4 + b[2]*A2 + b[0]*m_Id;
259 }
260 
261 template <typename MatrixType>
262 EIGEN_STRONG_INLINE void MatrixExponential<MatrixType>::pade13(const MatrixType &A)
263 {
264  const RealScalar b[] = {64764752532480000., 32382376266240000., 7771770303897600.,
265  1187353796428800., 129060195264000., 10559470521600., 670442572800.,
266  33522128640., 1323241920., 40840800., 960960., 16380., 182., 1.};
267  MatrixType A2 = A * A;
268  MatrixType A4 = A2 * A2;
269  m_tmp1.noalias() = A4 * A2;
270  m_V = b[13]*m_tmp1 + b[11]*A4 + b[9]*A2; // used for temporary storage
271  m_tmp2.noalias() = m_tmp1 * m_V;
272  m_tmp2 += b[7]*m_tmp1 + b[5]*A4 + b[3]*A2 + b[1]*m_Id;
273  m_U.noalias() = A * m_tmp2;
274  m_tmp2 = b[12]*m_tmp1 + b[10]*A4 + b[8]*A2;
275  m_V.noalias() = m_tmp1 * m_tmp2;
276  m_V += b[6]*m_tmp1 + b[4]*A4 + b[2]*A2 + b[0]*m_Id;
277 }
278 
279 #if LDBL_MANT_DIG > 64
280 template <typename MatrixType>
281 EIGEN_STRONG_INLINE void MatrixExponential<MatrixType>::pade17(const MatrixType &A)
282 {
283  const RealScalar b[] = {830034394580628357120000.L, 415017197290314178560000.L,
284  100610229646136770560000.L, 15720348382208870400000.L,
285  1774878043152614400000.L, 153822763739893248000.L, 10608466464820224000.L,
286  595373117923584000.L, 27563570274240000.L, 1060137318240000.L,
287  33924394183680.L, 899510451840.L, 19554575040.L, 341863200.L, 4651200.L,
288  46512.L, 306.L, 1.L};
289  MatrixType A2 = A * A;
290  MatrixType A4 = A2 * A2;
291  MatrixType A6 = A4 * A2;
292  m_tmp1.noalias() = A4 * A4;
293  m_V = b[17]*m_tmp1 + b[15]*A6 + b[13]*A4 + b[11]*A2; // used for temporary storage
294  m_tmp2.noalias() = m_tmp1 * m_V;
295  m_tmp2 += b[9]*m_tmp1 + b[7]*A6 + b[5]*A4 + b[3]*A2 + b[1]*m_Id;
296  m_U.noalias() = A * m_tmp2;
297  m_tmp2 = b[16]*m_tmp1 + b[14]*A6 + b[12]*A4 + b[10]*A2;
298  m_V.noalias() = m_tmp1 * m_tmp2;
299  m_V += b[8]*m_tmp1 + b[6]*A6 + b[4]*A4 + b[2]*A2 + b[0]*m_Id;
300 }
301 #endif
302 
303 template <typename MatrixType>
304 void MatrixExponential<MatrixType>::computeUV(float)
305 {
306  using std::max;
307  using std::pow;
308  using std::ceil;
309  if (m_l1norm < 4.258730016922831e-001) {
310  pade3(m_M);
311  } else if (m_l1norm < 1.880152677804762e+000) {
312  pade5(m_M);
313  } else {
314  const float maxnorm = 3.925724783138660f;
315  m_squarings = (max)(0, (int)ceil(log2(m_l1norm / maxnorm)));
316  MatrixType A = m_M / pow(Scalar(2), m_squarings);
317  pade7(A);
318  }
319 }
320 
321 template <typename MatrixType>
322 void MatrixExponential<MatrixType>::computeUV(double)
323 {
324  using std::max;
325  using std::pow;
326  using std::ceil;
327  if (m_l1norm < 1.495585217958292e-002) {
328  pade3(m_M);
329  } else if (m_l1norm < 2.539398330063230e-001) {
330  pade5(m_M);
331  } else if (m_l1norm < 9.504178996162932e-001) {
332  pade7(m_M);
333  } else if (m_l1norm < 2.097847961257068e+000) {
334  pade9(m_M);
335  } else {
336  const double maxnorm = 5.371920351148152;
337  m_squarings = (max)(0, (int)ceil(log2(m_l1norm / maxnorm)));
338  MatrixType A = m_M / pow(Scalar(2), m_squarings);
339  pade13(A);
340  }
341 }
342 
343 template <typename MatrixType>
344 void MatrixExponential<MatrixType>::computeUV(long double)
345 {
346  using std::max;
347  using std::pow;
348  using std::ceil;
349 #if LDBL_MANT_DIG == 53 // double precision
350  computeUV(double());
351 #elif LDBL_MANT_DIG <= 64 // extended precision
352  if (m_l1norm < 4.1968497232266989671e-003L) {
353  pade3(m_M);
354  } else if (m_l1norm < 1.1848116734693823091e-001L) {
355  pade5(m_M);
356  } else if (m_l1norm < 5.5170388480686700274e-001L) {
357  pade7(m_M);
358  } else if (m_l1norm < 1.3759868875587845383e+000L) {
359  pade9(m_M);
360  } else {
361  const long double maxnorm = 4.0246098906697353063L;
362  m_squarings = (max)(0, (int)ceil(log2(m_l1norm / maxnorm)));
363  MatrixType A = m_M / pow(Scalar(2), m_squarings);
364  pade13(A);
365  }
366 #elif LDBL_MANT_DIG <= 106 // double-double
367  if (m_l1norm < 3.2787892205607026992947488108213e-005L) {
368  pade3(m_M);
369  } else if (m_l1norm < 6.4467025060072760084130906076332e-003L) {
370  pade5(m_M);
371  } else if (m_l1norm < 6.8988028496595374751374122881143e-002L) {
372  pade7(m_M);
373  } else if (m_l1norm < 2.7339737518502231741495857201670e-001L) {
374  pade9(m_M);
375  } else if (m_l1norm < 1.3203382096514474905666448850278e+000L) {
376  pade13(m_M);
377  } else {
378  const long double maxnorm = 3.2579440895405400856599663723517L;
379  m_squarings = (max)(0, (int)ceil(log2(m_l1norm / maxnorm)));
380  MatrixType A = m_M / pow(Scalar(2), m_squarings);
381  pade17(A);
382  }
383 #elif LDBL_MANT_DIG <= 112 // quadruple precison
384  if (m_l1norm < 1.639394610288918690547467954466970e-005L) {
385  pade3(m_M);
386  } else if (m_l1norm < 4.253237712165275566025884344433009e-003L) {
387  pade5(m_M);
388  } else if (m_l1norm < 5.125804063165764409885122032933142e-002L) {
389  pade7(m_M);
390  } else if (m_l1norm < 2.170000765161155195453205651889853e-001L) {
391  pade9(m_M);
392  } else if (m_l1norm < 1.125358383453143065081397882891878e+000L) {
393  pade13(m_M);
394  } else {
395  const long double maxnorm = 2.884233277829519311757165057717815L;
396  m_squarings = (max)(0, (int)ceil(log2(m_l1norm / maxnorm)));
397  MatrixType A = m_M / pow(Scalar(2), m_squarings);
398  pade17(A);
399  }
400 #else
401  // this case should be handled in compute()
402  eigen_assert(false && "Bug in MatrixExponential");
403 #endif // LDBL_MANT_DIG
404 }
405 
418 template<typename Derived> struct MatrixExponentialReturnValue
419 : public ReturnByValue<MatrixExponentialReturnValue<Derived> >
420 {
421  typedef typename Derived::Index Index;
422  public:
428  MatrixExponentialReturnValue(const Derived& src) : m_src(src) { }
429 
435  template <typename ResultType>
436  inline void evalTo(ResultType& result) const
437  {
438  const typename Derived::PlainObject srcEvaluated = m_src.eval();
440  me.compute(result);
441  }
442 
443  Index rows() const { return m_src.rows(); }
444  Index cols() const { return m_src.cols(); }
445 
446  protected:
447  const Derived& m_src;
448  private:
450 };
451 
452 namespace internal {
453 template<typename Derived>
454 struct traits<MatrixExponentialReturnValue<Derived> >
455 {
456  typedef typename Derived::PlainObject ReturnType;
457 };
458 }
459 
460 template <typename Derived>
461 const MatrixExponentialReturnValue<Derived> MatrixBase<Derived>::exp() const
462 {
463  eigen_assert(rows() == cols());
464  return MatrixExponentialReturnValue<Derived>(derived());
465 }
466 
467 } // end namespace Eigen
468 
469 #endif // EIGEN_MATRIX_EXPONENTIAL