Automatic Differentiation
 
Loading...
Searching...
No Matches
is_corr_matrix.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_PRIM_ERR_IS_CORR_MATRIX_HPP
2#define STAN_MATH_PRIM_ERR_IS_CORR_MATRIX_HPP
3
12#include <cmath>
13
14namespace stan {
15namespace math {
16
29template <typename EigMat, require_eigen_matrix_dynamic_t<EigMat>* = nullptr>
30inline bool is_corr_matrix(const EigMat& y) {
31 using std::fabs;
32 const auto& y_ref = to_ref(y);
33 if (!is_size_match(y_ref.rows(), y_ref.cols())) {
34 return false;
35 }
36 if (!is_positive(y_ref.rows())) {
37 return false;
38 }
39 if (!is_pos_definite(y_ref)) {
40 return false;
41 }
42 if (is_symmetric(y_ref)) {
43 for (Eigen::Index k = 0; k < y_ref.rows(); ++k) {
44 if (!(fabs(y_ref(k, k) - 1.0) <= CONSTRAINT_TOLERANCE)) {
45 return false;
46 }
47 }
48 }
49 return true;
50}
51
52} // namespace math
53} // namespace stan
54#endif
bool is_size_match(T_size1 i, T_size2 j)
Return true if the provided sizes match.
bool is_positive(const T_y &y)
Return true if y is positive.
bool is_corr_matrix(const EigMat &y)
Return true if the matrix is square and not 0x0, if the matrix is symmetric, diagonals are near 1,...
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...
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.
fvar< T > fabs(const fvar< T > &x)
Definition fabs.hpp:15
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...