1 #ifndef STAN_MATH_PRIM_FUN_MDIVIDE_LEFT_LDLT_HPP
2 #define STAN_MATH_PRIM_FUN_MDIVIDE_LEFT_LDLT_HPP
3 
4 #include <stan/math/prim/meta.hpp>
5 #include <stan/math/prim/err.hpp>
6 #include <stan/math/prim/fun/Eigen.hpp>
7 #include <stan/math/prim/fun/LDLT_factor.hpp>
8 #include <type_traits>
9 
10 namespace stan {
11 namespace math {
12 
13 /**
14  * Returns the solution of the system Ax=b given an LDLT_factor of A
15  *
16  * @tparam T type of matrix in the LDLT_factor
17  * @tparam EigMat type of the right hand side
18  *
19  * @param A LDLT_factor
20  * @param b Right hand side matrix or vector.
21  * @return x = A^-1 b, solution of the linear system.
22  * @throws std::domain_error if rows of b don't match the size of A.
23  */
24 template <typename T, typename EigMat, require_eigen_t<EigMat>* = nullptr,
25           require_all_not_st_var<T, EigMat>* = nullptr,
26           require_any_not_t<std::is_arithmetic<value_type_t<T>>,
27                             is_fvar<value_type_t<EigMat>>>* = nullptr>
28 inline Eigen::Matrix<return_type_t<T, EigMat>, Eigen::Dynamic,
29                      EigMat::ColsAtCompileTime>
mdivide_left_ldlt(LDLT_factor<T> & A,const EigMat & b)30 mdivide_left_ldlt(LDLT_factor<T>& A, const EigMat& b) {
31   check_multiplicable("mdivide_left_ldlt", "A", A.matrix(), "b", b);
32 
33   if (A.matrix().cols() == 0) {
34     return {0, b.cols()};
35   }
36 
37   return A.ldlt().solve(
38       Eigen::Matrix<return_type_t<T, EigMat>, EigMat::RowsAtCompileTime,
39                     EigMat::ColsAtCompileTime>(b));
40 }
41 
42 }  // namespace math
43 }  // namespace stan
44 
45 #endif
46