1 #ifndef STAN_MATH_PRIM_FUN_SCALE_MATRIX_EXP_MULTIPLY_HPP
2 #define STAN_MATH_PRIM_FUN_SCALE_MATRIX_EXP_MULTIPLY_HPP
3 
4 #include <stan/math/prim/meta.hpp>
5 #include <stan/math/prim/err.hpp>
6 #include <stan/math/prim/fun/matrix_exp.hpp>
7 #include <stan/math/prim/fun/matrix_exp_action_handler.hpp>
8 #include <stan/math/prim/fun/multiply.hpp>
9 
10 namespace stan {
11 namespace math {
12 
13 /**
14  * Return product of exp(At) and B, where A is a NxN double matrix,
15  * B is a NxCb double matrix, and t is a double.
16  *
17  * Specialized for double values for efficiency.
18  *
19  * @tparam EigMat1 type of the first matrix
20  * @tparam EigMat2 type of the second matrix
21  *
22  * @param[in] A Matrix
23  * @param[in] B Matrix
24  * @param[in] t double
25  * @return exponential of At multiplied by B
26  */
27 template <typename EigMat1, typename EigMat2,
28           require_all_eigen_vt<std::is_arithmetic, EigMat1, EigMat2>* = nullptr>
29 inline Eigen::Matrix<double, Eigen::Dynamic, EigMat2::ColsAtCompileTime>
scale_matrix_exp_multiply(const double & t,const EigMat1 & A,const EigMat2 & B)30 scale_matrix_exp_multiply(const double& t, const EigMat1& A, const EigMat2& B) {
31   check_square("scale_matrix_exp_multiply", "input matrix", A);
32   check_multiplicable("scale_matrix_exp_multiply", "A", A, "B", B);
33   if (A.size() == 0) {
34     return {0, B.cols()};
35   }
36 
37   return matrix_exp_action_handler().action(A, B, t);
38 }
39 
40 /**
41  * Return product of exp(At) and B, where A is a NxN matrix,
42  * B is a NxCb matrix and t is a scalar.
43  *
44  * Generic implementation when arguments are not all double.
45  *
46  * @tparam Tt type of \c t
47  * @tparam EigMat1 type of the first matrix
48  * @tparam EigMat2 type of the second matrix
49  * @param[in] A Matrix
50  * @param[in] B Matrix
51  * @param[in] t double
52  * @return exponential of At multiplied by B
53  */
54 template <typename Tt, typename EigMat1, typename EigMat2,
55           require_all_eigen_t<EigMat1, EigMat2>* = nullptr,
56           require_any_autodiff_t<Tt, value_type_t<EigMat1>,
57                                  value_type_t<EigMat2>>* = nullptr>
58 inline Eigen::Matrix<return_type_t<Tt, EigMat1, EigMat2>, Eigen::Dynamic,
59                      EigMat2::ColsAtCompileTime>
scale_matrix_exp_multiply(const Tt & t,const EigMat1 & A,const EigMat2 & B)60 scale_matrix_exp_multiply(const Tt& t, const EigMat1& A, const EigMat2& B) {
61   check_square("scale_matrix_exp_multiply", "input matrix", A);
62   check_multiplicable("scale_matrix_exp_multiply", "A", A, "B", B);
63   if (A.size() == 0) {
64     return {0, B.cols()};
65   }
66 
67   return multiply(matrix_exp(multiply(A, t)), B);
68 }
69 
70 }  // namespace math
71 }  // namespace stan
72 #endif
73