1 #ifndef STAN_MATH_PRIM_FUN_MDIVIDE_RIGHT_SPD_HPP
2 #define STAN_MATH_PRIM_FUN_MDIVIDE_RIGHT_SPD_HPP
3 
4 #include <stan/math/prim/err.hpp>
5 #include <stan/math/prim/fun/Eigen.hpp>
6 #include <stan/math/prim/fun/mdivide_left_spd.hpp>
7 #include <stan/math/prim/fun/transpose.hpp>
8 #include <stan/math/prim/fun/to_ref.hpp>
9 
10 namespace stan {
11 namespace math {
12 
13 /**
14  * Returns the solution of the system xA=b where A is symmetric
15  * positive definite.
16  *
17  * @tparam EigMat1 type of the right-hand side matrix or vector
18  * @tparam EigMat2 type of the second matrix
19  *
20  * @param b right-hand side matrix or vector
21  * @param A matrix
22  * @return x = b A^-1, solution of the linear system.
23  * @throws std::domain_error if A is not square or the rows of b don't
24  * match the size of A.
25  */
26 template <typename EigMat1, typename EigMat2,
27           require_all_eigen_t<EigMat1, EigMat2>* = nullptr>
28 inline Eigen::Matrix<return_type_t<EigMat1, EigMat2>,
29                      EigMat1::RowsAtCompileTime, EigMat2::ColsAtCompileTime>
mdivide_right_spd(const EigMat1 & b,const EigMat2 & A)30 mdivide_right_spd(const EigMat1& b, const EigMat2& A) {
31   static const char* function = "mdivide_right_spd";
32   check_multiplicable(function, "b", b, "A", A);
33   const auto& A_ref = to_ref(A);
34   check_symmetric(function, "A", A_ref);
35   check_not_nan(function, "A", A_ref);
36   if (A.size() == 0) {
37     return {b.rows(), 0};
38   }
39 
40   return mdivide_left_spd(A_ref, b.transpose()).transpose();
41 }
42 
43 }  // namespace math
44 }  // namespace stan
45 
46 #endif
47