1// This file is part of Eigen, a lightweight C++ template library
2// for linear algebra.
3//
4// Copyright (C) 2009, 2010, 2013 Jitse Niesen <jitse@maths.leeds.ac.uk>
5// Copyright (C) 2011, 2013 Chen-Pang He <jdh8@ms63.hinet.net>
6//
7// This Source Code Form is subject to the terms of the Mozilla
8// Public License v. 2.0. If a copy of the MPL was not distributed
9// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
10
11#ifndef EIGEN_MATRIX_EXPONENTIAL
12#define EIGEN_MATRIX_EXPONENTIAL
13
14#include "StemFunction.h"
15
16namespace Eigen {
17namespace internal {
18
19/** \brief Scaling operator.
20 *
21 * This struct is used by CwiseUnaryOp to scale a matrix by \f$ 2^{-s} \f$.
22 */
23template <typename RealScalar>
24struct MatrixExponentialScalingOp
25{
26  /** \brief Constructor.
27   *
28   * \param[in] squarings  The integer \f$ s \f$ in this document.
29   */
30  MatrixExponentialScalingOp(int squarings) : m_squarings(squarings) { }
31
32
33  /** \brief Scale a matrix coefficient.
34   *
35   * \param[in,out] x  The scalar to be scaled, becoming \f$ 2^{-s} x \f$.
36   */
37  inline const RealScalar operator() (const RealScalar& x) const
38  {
39    using std::ldexp;
40    return ldexp(x, -m_squarings);
41  }
42
43  typedef std::complex<RealScalar> ComplexScalar;
44
45  /** \brief Scale a matrix coefficient.
46   *
47   * \param[in,out] x  The scalar to be scaled, becoming \f$ 2^{-s} x \f$.
48   */
49  inline const ComplexScalar operator() (const ComplexScalar& x) const
50  {
51    using std::ldexp;
52    return ComplexScalar(ldexp(x.real(), -m_squarings), ldexp(x.imag(), -m_squarings));
53  }
54
55  private:
56    int m_squarings;
57};
58
59/** \brief Compute the (3,3)-Pad&eacute; approximant to the exponential.
60 *
61 *  After exit, \f$ (V+U)(V-U)^{-1} \f$ is the Pad&eacute;
62 *  approximant of \f$ \exp(A) \f$ around \f$ A = 0 \f$.
63 */
64template <typename MatA, typename MatU, typename MatV>
65void matrix_exp_pade3(const MatA& A, MatU& U, MatV& V)
66{
67  typedef typename MatA::PlainObject MatrixType;
68  typedef typename NumTraits<typename traits<MatA>::Scalar>::Real RealScalar;
69  const RealScalar b[] = {120.L, 60.L, 12.L, 1.L};
70  const MatrixType A2 = A * A;
71  const MatrixType tmp = b[3] * A2 + b[1] * MatrixType::Identity(A.rows(), A.cols());
72  U.noalias() = A * tmp;
73  V = b[2] * A2 + b[0] * MatrixType::Identity(A.rows(), A.cols());
74}
75
76/** \brief Compute the (5,5)-Pad&eacute; approximant to the exponential.
77 *
78 *  After exit, \f$ (V+U)(V-U)^{-1} \f$ is the Pad&eacute;
79 *  approximant of \f$ \exp(A) \f$ around \f$ A = 0 \f$.
80 */
81template <typename MatA, typename MatU, typename MatV>
82void matrix_exp_pade5(const MatA& A, MatU& U, MatV& V)
83{
84  typedef typename MatA::PlainObject MatrixType;
85  typedef typename NumTraits<typename traits<MatrixType>::Scalar>::Real RealScalar;
86  const RealScalar b[] = {30240.L, 15120.L, 3360.L, 420.L, 30.L, 1.L};
87  const MatrixType A2 = A * A;
88  const MatrixType A4 = A2 * A2;
89  const MatrixType tmp = b[5] * A4 + b[3] * A2 + b[1] * MatrixType::Identity(A.rows(), A.cols());
90  U.noalias() = A * tmp;
91  V = b[4] * A4 + b[2] * A2 + b[0] * MatrixType::Identity(A.rows(), A.cols());
92}
93
94/** \brief Compute the (7,7)-Pad&eacute; approximant to the exponential.
95 *
96 *  After exit, \f$ (V+U)(V-U)^{-1} \f$ is the Pad&eacute;
97 *  approximant of \f$ \exp(A) \f$ around \f$ A = 0 \f$.
98 */
99template <typename MatA, typename MatU, typename MatV>
100void matrix_exp_pade7(const MatA& A, MatU& U, MatV& V)
101{
102  typedef typename MatA::PlainObject MatrixType;
103  typedef typename NumTraits<typename traits<MatrixType>::Scalar>::Real RealScalar;
104  const RealScalar b[] = {17297280.L, 8648640.L, 1995840.L, 277200.L, 25200.L, 1512.L, 56.L, 1.L};
105  const MatrixType A2 = A * A;
106  const MatrixType A4 = A2 * A2;
107  const MatrixType A6 = A4 * A2;
108  const MatrixType tmp = b[7] * A6 + b[5] * A4 + b[3] * A2
109    + b[1] * MatrixType::Identity(A.rows(), A.cols());
110  U.noalias() = A * tmp;
111  V = b[6] * A6 + b[4] * A4 + b[2] * A2 + b[0] * MatrixType::Identity(A.rows(), A.cols());
112
113}
114
115/** \brief Compute the (9,9)-Pad&eacute; approximant to the exponential.
116 *
117 *  After exit, \f$ (V+U)(V-U)^{-1} \f$ is the Pad&eacute;
118 *  approximant of \f$ \exp(A) \f$ around \f$ A = 0 \f$.
119 */
120template <typename MatA, typename MatU, typename MatV>
121void matrix_exp_pade9(const MatA& A, MatU& U, MatV& V)
122{
123  typedef typename MatA::PlainObject MatrixType;
124  typedef typename NumTraits<typename traits<MatrixType>::Scalar>::Real RealScalar;
125  const RealScalar b[] = {17643225600.L, 8821612800.L, 2075673600.L, 302702400.L, 30270240.L,
126                          2162160.L, 110880.L, 3960.L, 90.L, 1.L};
127  const MatrixType A2 = A * A;
128  const MatrixType A4 = A2 * A2;
129  const MatrixType A6 = A4 * A2;
130  const MatrixType A8 = A6 * A2;
131  const MatrixType tmp = b[9] * A8 + b[7] * A6 + b[5] * A4 + b[3] * A2
132    + b[1] * MatrixType::Identity(A.rows(), A.cols());
133  U.noalias() = A * tmp;
134  V = b[8] * A8 + b[6] * A6 + b[4] * A4 + b[2] * A2 + b[0] * MatrixType::Identity(A.rows(), A.cols());
135}
136
137/** \brief Compute the (13,13)-Pad&eacute; approximant to the exponential.
138 *
139 *  After exit, \f$ (V+U)(V-U)^{-1} \f$ is the Pad&eacute;
140 *  approximant of \f$ \exp(A) \f$ around \f$ A = 0 \f$.
141 */
142template <typename MatA, typename MatU, typename MatV>
143void matrix_exp_pade13(const MatA& A, MatU& U, MatV& V)
144{
145  typedef typename MatA::PlainObject MatrixType;
146  typedef typename NumTraits<typename traits<MatrixType>::Scalar>::Real RealScalar;
147  const RealScalar b[] = {64764752532480000.L, 32382376266240000.L, 7771770303897600.L,
148                          1187353796428800.L, 129060195264000.L, 10559470521600.L, 670442572800.L,
149                          33522128640.L, 1323241920.L, 40840800.L, 960960.L, 16380.L, 182.L, 1.L};
150  const MatrixType A2 = A * A;
151  const MatrixType A4 = A2 * A2;
152  const MatrixType A6 = A4 * A2;
153  V = b[13] * A6 + b[11] * A4 + b[9] * A2; // used for temporary storage
154  MatrixType tmp = A6 * V;
155  tmp += b[7] * A6 + b[5] * A4 + b[3] * A2 + b[1] * MatrixType::Identity(A.rows(), A.cols());
156  U.noalias() = A * tmp;
157  tmp = b[12] * A6 + b[10] * A4 + b[8] * A2;
158  V.noalias() = A6 * tmp;
159  V += b[6] * A6 + b[4] * A4 + b[2] * A2 + b[0] * MatrixType::Identity(A.rows(), A.cols());
160}
161
162/** \brief Compute the (17,17)-Pad&eacute; approximant to the exponential.
163 *
164 *  After exit, \f$ (V+U)(V-U)^{-1} \f$ is the Pad&eacute;
165 *  approximant of \f$ \exp(A) \f$ around \f$ A = 0 \f$.
166 *
167 *  This function activates only if your long double is double-double or quadruple.
168 */
169#if LDBL_MANT_DIG > 64
170template <typename MatA, typename MatU, typename MatV>
171void matrix_exp_pade17(const MatA& A, MatU& U, MatV& V)
172{
173  typedef typename MatA::PlainObject MatrixType;
174  typedef typename NumTraits<typename traits<MatrixType>::Scalar>::Real RealScalar;
175  const RealScalar b[] = {830034394580628357120000.L, 415017197290314178560000.L,
176                          100610229646136770560000.L, 15720348382208870400000.L,
177                          1774878043152614400000.L, 153822763739893248000.L, 10608466464820224000.L,
178                          595373117923584000.L, 27563570274240000.L, 1060137318240000.L,
179                          33924394183680.L, 899510451840.L, 19554575040.L, 341863200.L, 4651200.L,
180                          46512.L, 306.L, 1.L};
181  const MatrixType A2 = A * A;
182  const MatrixType A4 = A2 * A2;
183  const MatrixType A6 = A4 * A2;
184  const MatrixType A8 = A4 * A4;
185  V = b[17] * A8 + b[15] * A6 + b[13] * A4 + b[11] * A2; // used for temporary storage
186  MatrixType tmp = A8 * V;
187  tmp += b[9] * A8 + b[7] * A6 + b[5] * A4 + b[3] * A2
188    + b[1] * MatrixType::Identity(A.rows(), A.cols());
189  U.noalias() = A * tmp;
190  tmp = b[16] * A8 + b[14] * A6 + b[12] * A4 + b[10] * A2;
191  V.noalias() = tmp * A8;
192  V += b[8] * A8 + b[6] * A6 + b[4] * A4 + b[2] * A2
193    + b[0] * MatrixType::Identity(A.rows(), A.cols());
194}
195#endif
196
197template <typename MatrixType, typename RealScalar = typename NumTraits<typename traits<MatrixType>::Scalar>::Real>
198struct matrix_exp_computeUV
199{
200  /** \brief Compute Pad&eacute; approximant to the exponential.
201    *
202    * Computes \c U, \c V and \c squarings such that \f$ (V+U)(V-U)^{-1} \f$ is a Pad&eacute;
203    * approximant of \f$ \exp(2^{-\mbox{squarings}}M) \f$ around \f$ M = 0 \f$, where \f$ M \f$
204    * denotes the matrix \c arg. The degree of the Pad&eacute; approximant and the value of squarings
205    * are chosen such that the approximation error is no more than the round-off error.
206    */
207  static void run(const MatrixType& arg, MatrixType& U, MatrixType& V, int& squarings);
208};
209
210template <typename MatrixType>
211struct matrix_exp_computeUV<MatrixType, float>
212{
213  template <typename ArgType>
214  static void run(const ArgType& arg, MatrixType& U, MatrixType& V, int& squarings)
215  {
216    using std::frexp;
217    using std::pow;
218    const float l1norm = arg.cwiseAbs().colwise().sum().maxCoeff();
219    squarings = 0;
220    if (l1norm < 4.258730016922831e-001f) {
221      matrix_exp_pade3(arg, U, V);
222    } else if (l1norm < 1.880152677804762e+000f) {
223      matrix_exp_pade5(arg, U, V);
224    } else {
225      const float maxnorm = 3.925724783138660f;
226      frexp(l1norm / maxnorm, &squarings);
227      if (squarings < 0) squarings = 0;
228      MatrixType A = arg.unaryExpr(MatrixExponentialScalingOp<float>(squarings));
229      matrix_exp_pade7(A, U, V);
230    }
231  }
232};
233
234template <typename MatrixType>
235struct matrix_exp_computeUV<MatrixType, double>
236{
237  template <typename ArgType>
238  static void run(const ArgType& arg, MatrixType& U, MatrixType& V, int& squarings)
239  {
240    using std::frexp;
241    using std::pow;
242    const double l1norm = arg.cwiseAbs().colwise().sum().maxCoeff();
243    squarings = 0;
244    if (l1norm < 1.495585217958292e-002) {
245      matrix_exp_pade3(arg, U, V);
246    } else if (l1norm < 2.539398330063230e-001) {
247      matrix_exp_pade5(arg, U, V);
248    } else if (l1norm < 9.504178996162932e-001) {
249      matrix_exp_pade7(arg, U, V);
250    } else if (l1norm < 2.097847961257068e+000) {
251      matrix_exp_pade9(arg, U, V);
252    } else {
253      const double maxnorm = 5.371920351148152;
254      frexp(l1norm / maxnorm, &squarings);
255      if (squarings < 0) squarings = 0;
256      MatrixType A = arg.unaryExpr(MatrixExponentialScalingOp<double>(squarings));
257      matrix_exp_pade13(A, U, V);
258    }
259  }
260};
261
262template <typename MatrixType>
263struct matrix_exp_computeUV<MatrixType, long double>
264{
265  template <typename ArgType>
266  static void run(const ArgType& arg, MatrixType& U, MatrixType& V, int& squarings)
267  {
268#if   LDBL_MANT_DIG == 53   // double precision
269    matrix_exp_computeUV<MatrixType, double>::run(arg, U, V, squarings);
270
271#else
272
273    using std::frexp;
274    using std::pow;
275    const long double l1norm = arg.cwiseAbs().colwise().sum().maxCoeff();
276    squarings = 0;
277
278#if LDBL_MANT_DIG <= 64   // extended precision
279
280    if (l1norm < 4.1968497232266989671e-003L) {
281      matrix_exp_pade3(arg, U, V);
282    } else if (l1norm < 1.1848116734693823091e-001L) {
283      matrix_exp_pade5(arg, U, V);
284    } else if (l1norm < 5.5170388480686700274e-001L) {
285      matrix_exp_pade7(arg, U, V);
286    } else if (l1norm < 1.3759868875587845383e+000L) {
287      matrix_exp_pade9(arg, U, V);
288    } else {
289      const long double maxnorm = 4.0246098906697353063L;
290      frexp(l1norm / maxnorm, &squarings);
291      if (squarings < 0) squarings = 0;
292      MatrixType A = arg.unaryExpr(MatrixExponentialScalingOp<long double>(squarings));
293      matrix_exp_pade13(A, U, V);
294    }
295
296#elif LDBL_MANT_DIG <= 106  // double-double
297
298    if (l1norm < 3.2787892205607026992947488108213e-005L) {
299      matrix_exp_pade3(arg, U, V);
300    } else if (l1norm < 6.4467025060072760084130906076332e-003L) {
301      matrix_exp_pade5(arg, U, V);
302    } else if (l1norm < 6.8988028496595374751374122881143e-002L) {
303      matrix_exp_pade7(arg, U, V);
304    } else if (l1norm < 2.7339737518502231741495857201670e-001L) {
305      matrix_exp_pade9(arg, U, V);
306    } else if (l1norm < 1.3203382096514474905666448850278e+000L) {
307      matrix_exp_pade13(arg, U, V);
308    } else {
309      const long double maxnorm = 3.2579440895405400856599663723517L;
310      frexp(l1norm / maxnorm, &squarings);
311      if (squarings < 0) squarings = 0;
312      MatrixType A = arg.unaryExpr(MatrixExponentialScalingOp<long double>(squarings));
313      matrix_exp_pade17(A, U, V);
314    }
315
316#elif LDBL_MANT_DIG <= 112  // quadruple precison
317
318    if (l1norm < 1.639394610288918690547467954466970e-005L) {
319      matrix_exp_pade3(arg, U, V);
320    } else if (l1norm < 4.253237712165275566025884344433009e-003L) {
321      matrix_exp_pade5(arg, U, V);
322    } else if (l1norm < 5.125804063165764409885122032933142e-002L) {
323      matrix_exp_pade7(arg, U, V);
324    } else if (l1norm < 2.170000765161155195453205651889853e-001L) {
325      matrix_exp_pade9(arg, U, V);
326    } else if (l1norm < 1.125358383453143065081397882891878e+000L) {
327      matrix_exp_pade13(arg, U, V);
328    } else {
329      frexp(l1norm / maxnorm, &squarings);
330      if (squarings < 0) squarings = 0;
331      MatrixType A = arg.unaryExpr(MatrixExponentialScalingOp<long double>(squarings));
332      matrix_exp_pade17(A, U, V);
333    }
334
335#else
336
337    // this case should be handled in compute()
338    eigen_assert(false && "Bug in MatrixExponential");
339
340#endif
341#endif  // LDBL_MANT_DIG
342  }
343};
344
345
346/* Computes the matrix exponential
347 *
348 * \param arg    argument of matrix exponential (should be plain object)
349 * \param result variable in which result will be stored
350 */
351template <typename ArgType, typename ResultType>
352void matrix_exp_compute(const ArgType& arg, ResultType &result)
353{
354  typedef typename ArgType::PlainObject MatrixType;
355#if LDBL_MANT_DIG > 112 // rarely happens
356  typedef typename traits<MatrixType>::Scalar Scalar;
357  typedef typename NumTraits<Scalar>::Real RealScalar;
358  typedef typename std::complex<RealScalar> ComplexScalar;
359  if (sizeof(RealScalar) > 14) {
360    result = arg.matrixFunction(internal::stem_function_exp<ComplexScalar>);
361    return;
362  }
363#endif
364  MatrixType U, V;
365  int squarings;
366  matrix_exp_computeUV<MatrixType>::run(arg, U, V, squarings); // Pade approximant is (U+V) / (-U+V)
367  MatrixType numer = U + V;
368  MatrixType denom = -U + V;
369  result = denom.partialPivLu().solve(numer);
370  for (int i=0; i<squarings; i++)
371    result *= result;   // undo scaling by repeated squaring
372}
373
374} // end namespace Eigen::internal
375
376/** \ingroup MatrixFunctions_Module
377  *
378  * \brief Proxy for the matrix exponential of some matrix (expression).
379  *
380  * \tparam Derived  Type of the argument to the matrix exponential.
381  *
382  * This class holds the argument to the matrix exponential until it is assigned or evaluated for
383  * some other reason (so the argument should not be changed in the meantime). It is the return type
384  * of MatrixBase::exp() and most of the time this is the only way it is used.
385  */
386template<typename Derived> struct MatrixExponentialReturnValue
387: public ReturnByValue<MatrixExponentialReturnValue<Derived> >
388{
389    typedef typename Derived::Index Index;
390  public:
391    /** \brief Constructor.
392      *
393      * \param src %Matrix (expression) forming the argument of the matrix exponential.
394      */
395    MatrixExponentialReturnValue(const Derived& src) : m_src(src) { }
396
397    /** \brief Compute the matrix exponential.
398      *
399      * \param result the matrix exponential of \p src in the constructor.
400      */
401    template <typename ResultType>
402    inline void evalTo(ResultType& result) const
403    {
404      const typename internal::nested_eval<Derived, 10>::type tmp(m_src);
405      internal::matrix_exp_compute(tmp, result);
406    }
407
408    Index rows() const { return m_src.rows(); }
409    Index cols() const { return m_src.cols(); }
410
411  protected:
412    const typename internal::ref_selector<Derived>::type m_src;
413};
414
415namespace internal {
416template<typename Derived>
417struct traits<MatrixExponentialReturnValue<Derived> >
418{
419  typedef typename Derived::PlainObject ReturnType;
420};
421}
422
423template <typename Derived>
424const MatrixExponentialReturnValue<Derived> MatrixBase<Derived>::exp() const
425{
426  eigen_assert(rows() == cols());
427  return MatrixExponentialReturnValue<Derived>(derived());
428}
429
430} // end namespace Eigen
431
432#endif // EIGEN_MATRIX_EXPONENTIAL
433