Stan Math Library  2.20.0
reverse mode automatic differentiation
matrix_exp.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_PRIM_MAT_FUN_MATRIX_EXP_HPP
2 #define STAN_MATH_PRIM_MAT_FUN_MATRIX_EXP_HPP
3 
6 
7 namespace stan {
8 namespace math {
9 
19 template <typename T>
20 inline Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> matrix_exp(
21  const Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic>& A) {
22  check_nonzero_size("matrix_exp", "input matrix", A);
23  check_square("matrix_exp", "input matrix", A);
24 
25  return (A.cols() == 2
26  && square(value_of(A(0, 0)) - value_of(A(1, 1)))
27  + 4 * value_of(A(0, 1)) * value_of(A(1, 0))
28  > 0)
29  ? matrix_exp_2x2(A)
30  : matrix_exp_pade(A);
31 }
32 
43 template <typename T, int N>
44 inline Eigen::Matrix<T, N, N> matrix_exp(const Eigen::Matrix<T, N, N>& A) {
45  check_nonzero_size("matrix_exp", "input matrix", A);
46 
47  return (N == 2
48  && square(value_of(A(0, 0)) - value_of(A(1, 1)))
49  + 4 * value_of(A(0, 1)) * value_of(A(1, 0))
50  > 0)
51  ? matrix_exp_2x2(A)
52  : matrix_exp_pade(A);
53 }
54 
63 template <typename T>
64 inline Eigen::Matrix<T, 1, 1> matrix_exp(const Eigen::Matrix<T, 1, 1>& A) {
65  Eigen::Matrix<T, 1, 1> res;
66  res << exp(A(0));
67  return res;
68 }
69 } // namespace math
70 } // namespace stan
71 #endif
void check_nonzero_size(const char *function, const char *name, const T_y &y)
Check if the specified matrix/vector is of non-zero size.
T value_of(const fvar< T > &v)
Return the value of the specified variable.
Definition: value_of.hpp:17
Mtype matrix_exp_2x2(const Mtype &A)
Return the matrix exponential of a 2x2 matrix.
void check_square(const char *function, const char *name, const matrix_cl &y)
Check if the matrix_cl is square.
fvar< T > square(const fvar< T > &x)
Definition: square.hpp:12
fvar< T > exp(const fvar< T > &x)
Definition: exp.hpp:11
Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic > matrix_exp(const Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic > &A)
Return the matrix exponential of the input matrix.
Definition: matrix_exp.hpp:20
MatrixType matrix_exp_pade(const MatrixType &arg)
Computes the matrix exponential, using a Pade approximation, coupled with scaling and squaring...

     [ Stan Home Page ] © 2011–2018, Stan Development Team.