Stan Math Library  2.10.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 
15 namespace stan {
16 
17  namespace math {
18 
35  template <typename T_y>
36  inline bool
38  const char* function,
39  const char* name,
40  const Eigen::Matrix<T_y, Eigen::Dynamic, Eigen::Dynamic>& y
41  ) {
42  check_symmetric(function, name, y);
43  check_positive_size(function, name, "rows", y.rows());
44 
45  if (y.rows() == 1 && !(y(0, 0) > CONSTRAINT_TOLERANCE))
46  domain_error(function, name, y, "is not positive definite: ");
47 
48  using Eigen::LDLT;
49  using Eigen::Matrix;
50  using Eigen::Dynamic;
51  LDLT< Matrix<double, Dynamic, Dynamic> > cholesky
52  = value_of_rec(y).ldlt();
53  if (cholesky.info() != Eigen::Success
54  || !cholesky.isPositive()
55  || (cholesky.vectorD().array() <= 0.0).any())
56  domain_error(function, name, y, "is not positive definite:\n");
57  check_not_nan(function, name, y);
58  return true;
59  }
60 
75  template <typename Derived>
76  inline bool
77  check_pos_definite(const char* function,
78  const char* name,
79  const Eigen::LDLT<Derived>& cholesky) {
80  if (cholesky.info() != Eigen::Success
81  || !cholesky.isPositive()
82  || !(cholesky.vectorD().array() > 0.0).all())
83  domain_error(function, "LDLT decomposition of", " failed", name);
84  return true;
85  }
86 
101  template <typename Derived>
102  inline bool
103  check_pos_definite(const char* function,
104  const char* name,
105  const Eigen::LLT<Derived>& cholesky) {
106  if (cholesky.info() != Eigen::Success
107  || !(cholesky.matrixLLT().diagonal().array() > 0.0).all())
108  domain_error(function, "Cholesky decomposition of", " failed", name);
109  return true;
110  }
111 
112  }
113 }
114 #endif
bool check_not_nan(const char *function, const char *name, const T_y &y)
Return true if y is not NaN.
bool check_positive_size(const char *function, const char *name, const char *expr, const int size)
Return true if size is positive.
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 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.
bool check_pos_definite(const char *function, const char *name, const Eigen::Matrix< T_y, Eigen::Dynamic, Eigen::Dynamic > &y)
Return true if the specified square, symmetric matrix is positive definite.
bool check_symmetric(const char *function, const char *name, const Eigen::Matrix< T_y, Eigen::Dynamic, Eigen::Dynamic > &y)
Return true if the specified matrix is symmetric.

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