1 #ifndef STAN_MATH_PRIM_MAT_ERR_CHECK_LDLT_FACTOR_HPP
2 #define STAN_MATH_PRIM_MAT_ERR_CHECK_LDLT_FACTOR_HPP
3 
4 #include <stan/math/prim/mat/fun/Eigen.hpp>
5 #include <stan/math/prim/scal/err/domain_error.hpp>
6 #include <stan/math/prim/mat/fun/LDLT_factor.hpp>
7 #include <sstream>
8 #include <string>
9 
10 namespace stan {
11 namespace math {
12 
13 /**
14  * Raise domain error if the specified LDLT factor is invalid.  An
15  * <code>LDLT_factor</code> is invalid if it was constructed from
16  * a matrix that is not positive definite.  The check is that the
17  * <code>success()</code> method returns <code>true</code>.
18  *
19  * @tparam T type of scalar
20  * @tparam R rows of the matrix
21  * @tparam C columns of the matrix
22  * @param[in] function function name for error messages
23  * @param[in] name variable name for error messages
24  * @param[in] A LDLT factor to check for validity
25  * @throws <code>std::domain_error</code> if the LDLT factor is
26  *   invalid.
27  */
28 template <typename T, int R, int C>
check_ldlt_factor(const char * function,const char * name,LDLT_factor<T,R,C> & A)29 inline void check_ldlt_factor(const char* function, const char* name,
30                               LDLT_factor<T, R, C>& A) {
31   if (!A.success()) {
32     std::ostringstream msg;
33     msg << "is not positive definite.  last conditional variance is ";
34     std::string msg_str(msg.str());
35     T too_small = A.vectorD().tail(1)(0);
36     domain_error(function, name, too_small, msg_str.c_str(), ".");
37   }
38 }
39 
40 }  // namespace math
41 }  // namespace stan
42 #endif
43