Stan Math Library  2.20.0
reverse mode automatic differentiation
check_pos_definite.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_PRIM_MAT_ERR_CHECK_POS_DEFINITE_HPP
2 #define STAN_MATH_PRIM_MAT_ERR_CHECK_POS_DEFINITE_HPP
3 
12 
13 namespace stan {
14 namespace math {
15 
27 template <typename T_y>
28 inline void check_pos_definite(const char* function, const char* name,
29  const Eigen::Matrix<T_y, -1, -1>& y) {
30  check_symmetric(function, name, y);
31  check_positive(function, name, "rows", y.rows());
32  if (y.rows() == 1 && !(y(0, 0) > CONSTRAINT_TOLERANCE))
33  domain_error(function, name, "is not positive definite.", "");
34 
35  Eigen::LDLT<Eigen::MatrixXd> cholesky = value_of_rec(y).ldlt();
36  if (cholesky.info() != Eigen::Success || !cholesky.isPositive()
37  || (cholesky.vectorD().array() <= 0.0).any())
38  domain_error(function, name, "is not positive definite.", "");
39  check_not_nan(function, name, y);
40 }
41 
52 template <typename Derived>
53 inline void check_pos_definite(const char* function, const char* name,
54  const Eigen::LDLT<Derived>& cholesky) {
55  if (cholesky.info() != Eigen::Success || !cholesky.isPositive()
56  || !(cholesky.vectorD().array() > 0.0).all())
57  domain_error(function, "LDLT decomposition of", " failed", name);
58 }
59 
71 template <typename Derived>
72 inline void check_pos_definite(const char* function, const char* name,
73  const Eigen::LLT<Derived>& cholesky) {
74  if (cholesky.info() != Eigen::Success
75  || !(cholesky.matrixLLT().diagonal().array() > 0.0).all())
76  domain_error(function, "Matrix", " is not positive definite", name);
77 }
78 
79 } // namespace math
80 } // namespace stan
81 #endif
double value_of_rec(const fvar< T > &v)
Return the value of the specified variable.
const double CONSTRAINT_TOLERANCE
The tolerance for checking arithmetic bounds In rank and in simplexes.
void check_not_nan(const char *function, const char *name, const T_y &y)
Check if y is not NaN.
void domain_error(const char *function, const char *name, const T &y, const char *msg1, const char *msg2)
Throw a domain error with a consistently formatted message.
void check_positive(const char *function, const char *name, const T_y &y)
Check if y is positive.
void check_pos_definite(const char *function, const char *name, const Eigen::Matrix< T_y, -1, -1 > &y)
Check if the specified square, symmetric matrix is positive definite.
void check_symmetric(const char *function, const char *name, const matrix_cl &y)
Check if the matrix_cl is symmetric.

     [ Stan Home Page ] © 2011–2018, Stan Development Team.