Automatic Differentiation
 
Loading...
Searching...
No Matches
check_pos_semidefinite.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_PRIM_ERR_CHECK_POS_SEMIDEFINITE_HPP
2#define STAN_MATH_PRIM_ERR_CHECK_POS_SEMIDEFINITE_HPP
3
12#include <sstream>
13
14namespace stan {
15namespace math {
16
29template <typename EigMat, require_matrix_t<EigMat>* = nullptr>
30inline void check_pos_semidefinite(const char* function, const char* name,
31 const EigMat& y) {
32 const auto& y_ref = to_ref(y);
33 check_symmetric(function, name, y_ref);
34 check_positive(function, name, "rows", y_ref.rows());
35 check_not_nan(function, name, y_ref);
36
37 if (y_ref.rows() == 1 && !(y_ref(0, 0) >= 0.0)) {
38 throw_domain_error(function, name, "is not positive semi-definite.", "");
39 }
40
41 using Eigen::Dynamic;
42 using Eigen::LDLT;
43 using Eigen::Matrix;
44 LDLT<Matrix<double, Dynamic, Dynamic> > cholesky = value_of_rec(y_ref).ldlt();
45 if (cholesky.info() != Eigen::Success
46 || (cholesky.vectorD().array() < 0.0).any()) {
47 throw_domain_error(function, name, "is not positive semi-definite.", "");
48 }
49}
50
61template <typename Derived>
62inline void check_pos_semidefinite(const char* function, const char* name,
63 const Eigen::LDLT<Derived>& cholesky) {
64 if (cholesky.info() != Eigen::Success
65 || (cholesky.vectorD().array() < 0.0).any()) {
66 throw_domain_error(function, name, "is not positive semi-definite.", "");
67 }
68}
69
70} // namespace math
71} // namespace stan
72#endif
void check_symmetric(const char *function, const char *name, const matrix_cl< T > &y)
Check if the matrix_cl is symmetric.
double value_of_rec(const fvar< T > &v)
Return the value of the specified variable.
void check_pos_semidefinite(const char *function, const char *name, const EigMat &y)
Check if the specified matrix is positive definite.
void throw_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.
ref_type_t< T && > to_ref(T &&a)
This evaluates expensive Eigen expressions.
Definition to_ref.hpp:17
void check_not_nan(const char *function, const char *name, const T_y &y)
Check if y is not NaN.
void check_positive(const char *function, const char *name, const T_y &y)
Check if y is positive.
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...
Definition fvar.hpp:9