1 #ifndef STAN_MATH_PRIM_MAT_ERR_CHECK_POS_SEMIDEFINITE_HPP
2 #define STAN_MATH_PRIM_MAT_ERR_CHECK_POS_SEMIDEFINITE_HPP
3
4 #include <stan/math/prim/scal/err/domain_error.hpp>
5 #include <stan/math/prim/mat/err/check_symmetric.hpp>
6 #include <stan/math/prim/mat/err/constraint_tolerance.hpp>
7 #include <stan/math/prim/scal/err/check_not_nan.hpp>
8 #include <stan/math/prim/scal/err/check_positive_size.hpp>
9 #include <stan/math/prim/mat/fun/Eigen.hpp>
10 #include <stan/math/prim/mat/meta/index_type.hpp>
11 #include <stan/math/prim/mat/fun/value_of_rec.hpp>
12 #include <sstream>
13
14 namespace stan {
15 namespace math {
16
17 /**
18 * Check if the specified matrix is positive definite
19 *
20 * @tparam T_y scalar type of the matrix
21 *
22 * @param function Function name (for error messages)
23 * @param name Variable name (for error messages)
24 * @param y Matrix to test
25 *
26 * @throw <code>std::invalid_argument</code> if the matrix is not square
27 * or if the matrix has 0 size.
28 * @throw <code>std::domain_error</code> if the matrix is not symmetric,
29 * or if it is not positive semi-definite,
30 * or if any element of the matrix is <code>NaN</code>.
31 */
32 template <typename T_y>
check_pos_semidefinite(const char * function,const char * name,const Eigen::Matrix<T_y,Eigen::Dynamic,Eigen::Dynamic> & y)33 inline void check_pos_semidefinite(
34 const char* function, const char* name,
35 const Eigen::Matrix<T_y, Eigen::Dynamic, Eigen::Dynamic>& y) {
36 check_symmetric(function, name, y);
37 check_positive_size(function, name, "rows", y.rows());
38
39 if (y.rows() == 1 && !(y(0, 0) >= 0.0))
40 domain_error(function, name, "is not positive semi-definite.", "");
41
42 using Eigen::Dynamic;
43 using Eigen::LDLT;
44 using Eigen::Matrix;
45 LDLT<Matrix<double, Dynamic, Dynamic> > cholesky = value_of_rec(y).ldlt();
46 if (cholesky.info() != Eigen::Success
47 || (cholesky.vectorD().array() < 0.0).any())
48 domain_error(function, name, "is not positive semi-definite.", "");
49 check_not_nan(function, name, y);
50 }
51
52 } // namespace math
53 } // namespace stan
54 #endif
55