Automatic Differentiation
 
Loading...
Searching...
No Matches
matrix_exp.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_PRIM_FUN_MATRIX_EXP_HPP
2#define STAN_MATH_PRIM_FUN_MATRIX_EXP_HPP
3
9#include <cmath>
10
11namespace stan {
12namespace math {
13
24template <typename T, typename = require_eigen_t<T>>
25inline plain_type_t<T> matrix_exp(const T& A_in) {
26 using std::exp;
27 const auto& A = A_in.eval();
28 check_square("matrix_exp", "input matrix", A);
29 if (T::RowsAtCompileTime == 1 && T::ColsAtCompileTime == 1) {
31 res << exp(A(0));
32 return res;
33 }
34 if (A_in.size() == 0) {
35 return {};
36 }
37 return (A.cols() == 2
38 && square(value_of(A(0, 0)) - value_of(A(1, 1)))
39 + 4 * value_of(A(0, 1)) * value_of(A(1, 0))
40 > 0)
42 : matrix_exp_pade(A);
43}
44
45} // namespace math
46} // namespace stan
47
48#endif
void check_square(const char *function, const char *name, const T_y &y)
Check if the specified matrix is square.
T value_of(const fvar< T > &v)
Return the value of the specified variable.
Definition value_of.hpp:18
plain_type_t< T > matrix_exp(const T &A_in)
Return the matrix exponential of the input matrix.
Eigen::Matrix< value_type_t< EigMat >, Eigen::Dynamic, Eigen::Dynamic > matrix_exp_2x2(const EigMat &A)
Return the matrix exponential of a 2x2 matrix.
Eigen::Matrix< value_type_t< EigMat >, EigMat::RowsAtCompileTime, EigMat::ColsAtCompileTime > matrix_exp_pade(const EigMat &arg)
Computes the matrix exponential, using a Pade approximation, coupled with scaling and squaring.
fvar< T > square(const fvar< T > &x)
Definition square.hpp:12
fvar< T > exp(const fvar< T > &x)
Definition exp.hpp:15
typename plain_type< T >::type plain_type_t
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...