Stan Math Library  2.8.0
reverse mode automatic differentiation
 All Classes Namespaces Files Functions Variables Typedefs Enumerator Friends Macros Groups
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  using Eigen::Dynamic;
19 
36  template <typename T_y>
37  inline bool
38  check_pos_definite(const char* function,
39  const char* name,
40  const Eigen::Matrix<T_y, Dynamic, Dynamic>& y) {
41  check_symmetric(function, name, y);
42  check_positive_size(function, name, "rows", y.rows());
43 
44  if (y.rows() == 1 && !(y(0, 0) > CONSTRAINT_TOLERANCE))
45  domain_error(function, name, y, "is not positive definite: ");
46 
47  using Eigen::LDLT;
48  using Eigen::Matrix;
49  using Eigen::Dynamic;
50  LDLT< Matrix<double, Dynamic, Dynamic> > cholesky
51  = value_of_rec(y).ldlt();
52  if (cholesky.info() != Eigen::Success
53  || !cholesky.isPositive()
54  || (cholesky.vectorD().array() <= 0.0).any())
55  domain_error(function, name, y, "is not positive definite:\n");
56  check_not_nan(function, name, y);
57  return true;
58  }
59 
74  template <typename Derived>
75  inline bool
76  check_pos_definite(const char* function,
77  const char* name,
78  const Eigen::LDLT<Derived>& cholesky) {
79  if (cholesky.info() != Eigen::Success
80  || !cholesky.isPositive()
81  || !(cholesky.vectorD().array() > 0.0).all())
82  domain_error(function, "LDLT decomposition of", " failed", name);
83  return true;
84  }
85 
100  template <typename Derived>
101  inline bool
102  check_pos_definite(const char* function,
103  const char* name,
104  const Eigen::LLT<Derived>& cholesky) {
105  if (cholesky.info() != Eigen::Success
106  || !(cholesky.matrixLLT().diagonal().array() > 0.0).all())
107  domain_error(function, "Cholesky decomposition of", " failed", name);
108  return true;
109  }
110 
111  }
112 }
113 #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.
bool check_symmetric(const char *function, const char *name, const Eigen::Matrix< T_y, Dynamic, Dynamic > &y)
Return true if the specified matrix is symmetric.
const double CONSTRAINT_TOLERANCE
The tolerance for checking arithmetic bounds In rank and in simplexes.
bool check_pos_definite(const char *function, const char *name, const Eigen::Matrix< T_y, Dynamic, Dynamic > &y)
Return true if the specified square, symmetric matrix is positive definite.
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.

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