11 #ifndef EIGEN_MATRIX_EXPONENTIAL
12 #define EIGEN_MATRIX_EXPONENTIAL
14 #include "StemFunction.h"
16 #include "./InternalHeaderCheck.h"
25 template <
typename RealScalar>
26 struct MatrixExponentialScalingOp
32 MatrixExponentialScalingOp(
int squarings) : m_squarings(squarings) { }
39 inline const RealScalar operator() (
const RealScalar& x)
const
42 return ldexp(x, -m_squarings);
45 typedef std::complex<RealScalar> ComplexScalar;
51 inline const ComplexScalar operator() (
const ComplexScalar& x)
const
54 return ComplexScalar(ldexp(x.real(), -m_squarings), ldexp(x.imag(), -m_squarings));
66 template <
typename MatA,
typename MatU,
typename MatV>
67 void matrix_exp_pade3(
const MatA& A, MatU& U, MatV& V)
69 typedef typename MatA::PlainObject MatrixType;
70 typedef typename NumTraits<typename traits<MatA>::Scalar>::Real RealScalar;
71 const RealScalar b[] = {120.L, 60.L, 12.L, 1.L};
72 const MatrixType A2 = A * A;
73 const MatrixType tmp = b[3] * A2 + b[1] * MatrixType::Identity(A.rows(), A.cols());
74 U.noalias() = A * tmp;
75 V = b[2] * A2 + b[0] * MatrixType::Identity(A.rows(), A.cols());
83 template <
typename MatA,
typename MatU,
typename MatV>
84 void matrix_exp_pade5(
const MatA& A, MatU& U, MatV& V)
86 typedef typename MatA::PlainObject MatrixType;
87 typedef typename NumTraits<typename traits<MatrixType>::Scalar>::Real RealScalar;
88 const RealScalar b[] = {30240.L, 15120.L, 3360.L, 420.L, 30.L, 1.L};
89 const MatrixType A2 = A * A;
90 const MatrixType A4 = A2 * A2;
91 const MatrixType tmp = b[5] * A4 + b[3] * A2 + b[1] * MatrixType::Identity(A.rows(), A.cols());
92 U.noalias() = A * tmp;
93 V = b[4] * A4 + b[2] * A2 + b[0] * MatrixType::Identity(A.rows(), A.cols());
101 template <
typename MatA,
typename MatU,
typename MatV>
102 void matrix_exp_pade7(
const MatA& A, MatU& U, MatV& V)
104 typedef typename MatA::PlainObject MatrixType;
105 typedef typename NumTraits<typename traits<MatrixType>::Scalar>::Real RealScalar;
106 const RealScalar b[] = {17297280.L, 8648640.L, 1995840.L, 277200.L, 25200.L, 1512.L, 56.L, 1.L};
107 const MatrixType A2 = A * A;
108 const MatrixType A4 = A2 * A2;
109 const MatrixType A6 = A4 * A2;
110 const MatrixType tmp = b[7] * A6 + b[5] * A4 + b[3] * A2
111 + b[1] * MatrixType::Identity(A.rows(), A.cols());
112 U.noalias() = A * tmp;
113 V = b[6] * A6 + b[4] * A4 + b[2] * A2 + b[0] * MatrixType::Identity(A.rows(), A.cols());
122 template <
typename MatA,
typename MatU,
typename MatV>
123 void matrix_exp_pade9(
const MatA& A, MatU& U, MatV& V)
125 typedef typename MatA::PlainObject MatrixType;
126 typedef typename NumTraits<typename traits<MatrixType>::Scalar>::Real RealScalar;
127 const RealScalar b[] = {17643225600.L, 8821612800.L, 2075673600.L, 302702400.L, 30270240.L,
128 2162160.L, 110880.L, 3960.L, 90.L, 1.L};
129 const MatrixType A2 = A * A;
130 const MatrixType A4 = A2 * A2;
131 const MatrixType A6 = A4 * A2;
132 const MatrixType A8 = A6 * A2;
133 const MatrixType tmp = b[9] * A8 + b[7] * A6 + b[5] * A4 + b[3] * A2
134 + b[1] * MatrixType::Identity(A.rows(), A.cols());
135 U.noalias() = A * tmp;
136 V = b[8] * A8 + b[6] * A6 + b[4] * A4 + b[2] * A2 + b[0] * MatrixType::Identity(A.rows(), A.cols());
144 template <
typename MatA,
typename MatU,
typename MatV>
145 void matrix_exp_pade13(
const MatA& A, MatU& U, MatV& V)
147 typedef typename MatA::PlainObject MatrixType;
148 typedef typename NumTraits<typename traits<MatrixType>::Scalar>::Real RealScalar;
149 const RealScalar b[] = {64764752532480000.L, 32382376266240000.L, 7771770303897600.L,
150 1187353796428800.L, 129060195264000.L, 10559470521600.L, 670442572800.L,
151 33522128640.L, 1323241920.L, 40840800.L, 960960.L, 16380.L, 182.L, 1.L};
152 const MatrixType A2 = A * A;
153 const MatrixType A4 = A2 * A2;
154 const MatrixType A6 = A4 * A2;
155 V = b[13] * A6 + b[11] * A4 + b[9] * A2;
156 MatrixType tmp = A6 * V;
157 tmp += b[7] * A6 + b[5] * A4 + b[3] * A2 + b[1] * MatrixType::Identity(A.rows(), A.cols());
158 U.noalias() = A * tmp;
159 tmp = b[12] * A6 + b[10] * A4 + b[8] * A2;
160 V.noalias() = A6 * tmp;
161 V += b[6] * A6 + b[4] * A4 + b[2] * A2 + b[0] * MatrixType::Identity(A.rows(), A.cols());
171 #if LDBL_MANT_DIG > 64
172 template <
typename MatA,
typename MatU,
typename MatV>
173 void matrix_exp_pade17(
const MatA& A, MatU& U, MatV& V)
175 typedef typename MatA::PlainObject MatrixType;
176 typedef typename NumTraits<typename traits<MatrixType>::Scalar>::Real RealScalar;
177 const RealScalar b[] = {830034394580628357120000.L, 415017197290314178560000.L,
178 100610229646136770560000.L, 15720348382208870400000.L,
179 1774878043152614400000.L, 153822763739893248000.L, 10608466464820224000.L,
180 595373117923584000.L, 27563570274240000.L, 1060137318240000.L,
181 33924394183680.L, 899510451840.L, 19554575040.L, 341863200.L, 4651200.L,
182 46512.L, 306.L, 1.L};
183 const MatrixType A2 = A * A;
184 const MatrixType A4 = A2 * A2;
185 const MatrixType A6 = A4 * A2;
186 const MatrixType A8 = A4 * A4;
187 V = b[17] * A8 + b[15] * A6 + b[13] * A4 + b[11] * A2;
188 MatrixType tmp = A8 * V;
189 tmp += b[9] * A8 + b[7] * A6 + b[5] * A4 + b[3] * A2
190 + b[1] * MatrixType::Identity(A.rows(), A.cols());
191 U.noalias() = A * tmp;
192 tmp = b[16] * A8 + b[14] * A6 + b[12] * A4 + b[10] * A2;
193 V.noalias() = tmp * A8;
194 V += b[8] * A8 + b[6] * A6 + b[4] * A4 + b[2] * A2
195 + b[0] * MatrixType::Identity(A.rows(), A.cols());
199 template <typename MatrixType, typename RealScalar = typename NumTraits<typename traits<MatrixType>::Scalar>::Real>
200 struct matrix_exp_computeUV
209 static void run(
const MatrixType&
arg, MatrixType& U, MatrixType& V,
int& squarings);
212 template <
typename MatrixType>
213 struct matrix_exp_computeUV<MatrixType, float>
215 template <
typename ArgType>
216 static void run(
const ArgType&
arg, MatrixType& U, MatrixType& V,
int& squarings)
220 const float l1norm =
arg.cwiseAbs().colwise().sum().maxCoeff();
222 if (l1norm < 4.258730016922831e-001f) {
223 matrix_exp_pade3(
arg, U, V);
224 }
else if (l1norm < 1.880152677804762e+000f) {
225 matrix_exp_pade5(
arg, U, V);
227 const float maxnorm = 3.925724783138660f;
228 frexp(l1norm / maxnorm, &squarings);
229 if (squarings < 0) squarings = 0;
230 MatrixType A =
arg.unaryExpr(MatrixExponentialScalingOp<float>(squarings));
231 matrix_exp_pade7(A, U, V);
236 template <
typename MatrixType>
237 struct matrix_exp_computeUV<MatrixType, double>
239 typedef typename NumTraits<typename traits<MatrixType>::Scalar>::Real RealScalar;
240 template <
typename ArgType>
241 static void run(
const ArgType&
arg, MatrixType& U, MatrixType& V,
int& squarings)
245 const RealScalar l1norm =
arg.cwiseAbs().colwise().sum().maxCoeff();
247 if (l1norm < 1.495585217958292e-002) {
248 matrix_exp_pade3(
arg, U, V);
249 }
else if (l1norm < 2.539398330063230e-001) {
250 matrix_exp_pade5(
arg, U, V);
251 }
else if (l1norm < 9.504178996162932e-001) {
252 matrix_exp_pade7(
arg, U, V);
253 }
else if (l1norm < 2.097847961257068e+000) {
254 matrix_exp_pade9(
arg, U, V);
256 const RealScalar maxnorm = 5.371920351148152;
257 frexp(l1norm / maxnorm, &squarings);
258 if (squarings < 0) squarings = 0;
259 MatrixType A =
arg.unaryExpr(MatrixExponentialScalingOp<RealScalar>(squarings));
260 matrix_exp_pade13(A, U, V);
265 template <
typename MatrixType>
266 struct matrix_exp_computeUV<MatrixType, long double>
268 template <
typename ArgType>
269 static void run(
const ArgType&
arg, MatrixType& U, MatrixType& V,
int& squarings)
271 #if LDBL_MANT_DIG == 53
272 matrix_exp_computeUV<MatrixType, double>::run(
arg, U, V, squarings);
278 const long double l1norm =
arg.cwiseAbs().colwise().sum().maxCoeff();
281 #if LDBL_MANT_DIG <= 64
283 if (l1norm < 4.1968497232266989671e-003L) {
284 matrix_exp_pade3(
arg, U, V);
285 }
else if (l1norm < 1.1848116734693823091e-001L) {
286 matrix_exp_pade5(
arg, U, V);
287 }
else if (l1norm < 5.5170388480686700274e-001L) {
288 matrix_exp_pade7(
arg, U, V);
289 }
else if (l1norm < 1.3759868875587845383e+000L) {
290 matrix_exp_pade9(
arg, U, V);
292 const long double maxnorm = 4.0246098906697353063L;
293 frexp(l1norm / maxnorm, &squarings);
294 if (squarings < 0) squarings = 0;
295 MatrixType A =
arg.unaryExpr(MatrixExponentialScalingOp<long double>(squarings));
296 matrix_exp_pade13(A, U, V);
299 #elif LDBL_MANT_DIG <= 106
301 if (l1norm < 3.2787892205607026992947488108213e-005L) {
302 matrix_exp_pade3(
arg, U, V);
303 }
else if (l1norm < 6.4467025060072760084130906076332e-003L) {
304 matrix_exp_pade5(
arg, U, V);
305 }
else if (l1norm < 6.8988028496595374751374122881143e-002L) {
306 matrix_exp_pade7(
arg, U, V);
307 }
else if (l1norm < 2.7339737518502231741495857201670e-001L) {
308 matrix_exp_pade9(
arg, U, V);
309 }
else if (l1norm < 1.3203382096514474905666448850278e+000L) {
310 matrix_exp_pade13(
arg, U, V);
312 const long double maxnorm = 3.2579440895405400856599663723517L;
313 frexp(l1norm / maxnorm, &squarings);
314 if (squarings < 0) squarings = 0;
315 MatrixType A =
arg.unaryExpr(MatrixExponentialScalingOp<long double>(squarings));
316 matrix_exp_pade17(A, U, V);
319 #elif LDBL_MANT_DIG <= 113
321 if (l1norm < 1.639394610288918690547467954466970e-005L) {
322 matrix_exp_pade3(
arg, U, V);
323 }
else if (l1norm < 4.253237712165275566025884344433009e-003L) {
324 matrix_exp_pade5(
arg, U, V);
325 }
else if (l1norm < 5.125804063165764409885122032933142e-002L) {
326 matrix_exp_pade7(
arg, U, V);
327 }
else if (l1norm < 2.170000765161155195453205651889853e-001L) {
328 matrix_exp_pade9(
arg, U, V);
329 }
else if (l1norm < 1.125358383453143065081397882891878e+000L) {
330 matrix_exp_pade13(
arg, U, V);
332 const long double maxnorm = 2.884233277829519311757165057717815L;
333 frexp(l1norm / maxnorm, &squarings);
334 if (squarings < 0) squarings = 0;
335 MatrixType A =
arg.unaryExpr(MatrixExponentialScalingOp<long double>(squarings));
336 matrix_exp_pade17(A, U, V);
342 eigen_assert(
false &&
"Bug in MatrixExponential");
349 template<
typename T>
struct is_exp_known_type : false_type {};
350 template<>
struct is_exp_known_type<float> : true_type {};
351 template<>
struct is_exp_known_type<double> : true_type {};
352 #if LDBL_MANT_DIG <= 113
353 template<>
struct is_exp_known_type<long double> : true_type {};
356 template <
typename ArgType,
typename ResultType>
357 void matrix_exp_compute(
const ArgType&
arg, ResultType &result, true_type)
359 typedef typename ArgType::PlainObject MatrixType;
362 matrix_exp_computeUV<MatrixType>::run(
arg, U, V, squarings);
363 MatrixType numer = U + V;
364 MatrixType denom = -U + V;
365 result = denom.partialPivLu().solve(numer);
366 for (
int i=0; i<squarings; i++)
376 template <
typename ArgType,
typename ResultType>
377 void matrix_exp_compute(
const ArgType&
arg, ResultType &result, false_type)
379 typedef typename ArgType::PlainObject MatrixType;
380 typedef typename traits<MatrixType>::Scalar Scalar;
381 typedef typename NumTraits<Scalar>::Real RealScalar;
382 typedef typename std::complex<RealScalar> ComplexScalar;
383 result =
arg.matrixFunction(internal::stem_function_exp<ComplexScalar>);
399 :
public ReturnByValue<MatrixExponentialReturnValue<Derived> >
412 template <
typename ResultType>
413 inline void evalTo(ResultType& result)
const
415 const typename internal::nested_eval<Derived, 10>::type tmp(m_src);
416 internal::matrix_exp_compute(tmp, result, internal::is_exp_known_type<typename Derived::RealScalar>());
419 Index rows()
const {
return m_src.rows(); }
420 Index cols()
const {
return m_src.cols(); }
423 const typename internal::ref_selector<Derived>::type m_src;
427 template<
typename Derived>
428 struct traits<MatrixExponentialReturnValue<Derived> >
430 typedef typename Derived::PlainObject ReturnType;
434 template <
typename Derived>
437 eigen_assert(rows() == cols());
438 return MatrixExponentialReturnValue<Derived>(derived());
const MatrixExponentialReturnValue< Derived > exp() const
Definition: MatrixExponential.h:435
Namespace containing all symbols from the Eigen library.
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
const Eigen::CwiseUnaryOp< Eigen::internal::scalar_arg_op< typename Derived::Scalar >, const Derived > arg(const Eigen::ArrayBase< Derived > &x)
Proxy for the matrix exponential of some matrix (expression).
Definition: MatrixExponential.h:400
void evalTo(ResultType &result) const
Compute the matrix exponential.
Definition: MatrixExponential.h:413
MatrixExponentialReturnValue(const Derived &src)
Constructor.
Definition: MatrixExponential.h:406