1 #ifndef STAN_MATH_PRIM_FUN_MDIVIDE_LEFT_HPP
2 #define STAN_MATH_PRIM_FUN_MDIVIDE_LEFT_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
8 namespace stan {
9 namespace math {
10
11 /**
12 * Returns the solution of the system Ax=b.
13 *
14 * @tparam T1 type of the first matrix
15 * @tparam T2 type of the right-hand side matrix or vector
16 *
17 * @param A Matrix.
18 * @param b Right hand side matrix or vector.
19 * @return x = A^-1 b, solution of the linear system.
20 * @throws std::domain_error if A is not square or the rows of b don't
21 * match the size of A.
22 */
23 template <typename T1, typename T2,
24 require_all_eigen_vt<std::is_arithmetic, T1, T2>* = nullptr>
25 inline Eigen::Matrix<return_type_t<T1, T2>, T1::RowsAtCompileTime,
26 T2::ColsAtCompileTime>
mdivide_left(const T1 & A,const T2 & b)27 mdivide_left(const T1& A, const T2& b) {
28 check_square("mdivide_left", "A", A);
29 check_multiplicable("mdivide_left", "A", A, "b", b);
30 if (A.size() == 0) {
31 return {0, b.cols()};
32 }
33
34 return Eigen::Matrix<return_type_t<T1, T2>, T1::RowsAtCompileTime,
35 T1::ColsAtCompileTime>(A)
36 .lu()
37 .solve(Eigen::Matrix<return_type_t<T1, T2>, T2::RowsAtCompileTime,
38 T2::ColsAtCompileTime>(b));
39 }
40
41 } // namespace math
42 } // namespace stan
43
44 #endif
45