Automatic Differentiation
 
Loading...
Searching...
No Matches
is_pos_definite.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_PRIM_ERR_IS_POS_DEFINITE_HPP
2#define STAN_MATH_PRIM_ERR_IS_POS_DEFINITE_HPP
3
12
13namespace stan {
14namespace math {
15
26template <typename EigMat, require_eigen_matrix_dynamic_t<EigMat>* = nullptr>
27inline bool is_pos_definite(const EigMat& y) {
28 const auto& y_ref = to_ref(y);
29 if (!is_symmetric(y_ref)) {
30 return false;
31 }
32 if (!is_positive(y_ref.rows())) {
33 return false;
34 }
35 if (y_ref.rows() == 1 && !(y_ref(0, 0) > CONSTRAINT_TOLERANCE)) {
36 return false;
37 }
38 Eigen::LDLT<Eigen::MatrixXd> cholesky = value_of_rec(y_ref).ldlt();
39 if (cholesky.info() != Eigen::Success || !cholesky.isPositive()
40 || (cholesky.vectorD().array() <= 0.0).any()) {
41 return false;
42 }
43 return is_not_nan(y_ref);
44}
45
55template <typename Derived>
56inline bool is_pos_definite(const Eigen::LDLT<Derived>& cholesky) {
57 return cholesky.info() == Eigen::Success && cholesky.isPositive()
58 && (cholesky.vectorD().array() > 0.0).all();
59}
60
70template <typename Derived>
71inline bool is_pos_definite(const Eigen::LLT<Derived>& cholesky) {
72 return cholesky.info() == Eigen::Success
73 && (cholesky.matrixLLT().diagonal().array() > 0.0).all();
74}
75
76} // namespace math
77} // namespace stan
78#endif
double value_of_rec(const fvar< T > &v)
Return the value of the specified variable.
bool is_positive(const T_y &y)
Return true if y is positive.
constexpr bool all(T x)
Return true if all values in the input are true.
Definition all.hpp:21
bool is_pos_definite(const EigMat &y)
Return true if the matrix is square or if the matrix has non-zero size, or if the matrix is symmetric...
bool is_not_nan(const T_y &y)
Return true if y is not NaN.
ref_type_t< T && > to_ref(T &&a)
This evaluates expensive Eigen expressions.
Definition to_ref.hpp:17
bool is_symmetric(const EigMat &y)
Return true if the matrix is square, and no element not on the main diagonal is NaN.
const double CONSTRAINT_TOLERANCE
The tolerance for checking arithmetic bounds in rank and in simplexes.
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...