Automatic Differentiation
 
Loading...
Searching...
No Matches
check_corr_matrix.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_PRIM_ERR_CHECK_CORR_MATRIX_HPP
2#define STAN_MATH_PRIM_ERR_CHECK_CORR_MATRIX_HPP
3
11#include <sstream>
12#include <string>
13#include <cmath>
14
15namespace stan {
16namespace math {
17
33template <typename Mat, require_matrix_t<Mat>* = nullptr>
34inline void check_corr_matrix(const char* function, const char* name,
35 const Mat& y) {
36 auto&& y_ref = to_ref(value_of_rec(y));
37 check_square(function, name, y_ref);
38 using std::fabs;
39 if (y_ref.size() == 0) {
40 return;
41 }
42
43 for (Eigen::Index k = 0; k < y.rows(); ++k) {
44 if (!(fabs(y_ref.coeff(k, k) - 1.0) <= CONSTRAINT_TOLERANCE)) {
45 [&y_ref, name, k, function]() STAN_COLD_PATH {
46 std::ostringstream msg;
47 msg << "is not a valid correlation matrix. " << name << "("
48 << stan::error_index::value + k << ","
49 << stan::error_index::value + k << ") is ";
50 std::string msg_str(msg.str());
51 throw_domain_error(function, name, y_ref(k, k), msg_str.c_str(),
52 ", but should be near 1.0");
53 }();
54 }
55 }
56 check_pos_definite(function, name, y_ref);
57}
58
75template <typename StdVec, require_std_vector_t<StdVec>* = nullptr>
76void check_corr_matrix(const char* function, const char* name,
77 const StdVec& y) {
78 for (auto&& y_i : y) {
79 check_corr_matrix(function, name, y_i);
80 }
81}
82
83} // namespace math
84} // namespace stan
85#endif
#define STAN_COLD_PATH
double value_of_rec(const fvar< T > &v)
Return the value of the specified variable.
void check_square(const char *function, const char *name, const T_y &y)
Check if the specified matrix is square.
void check_corr_matrix(const char *function, const char *name, const Mat &y)
Throw an exception if the specified matrix is not a valid correlation matrix.
void check_pos_definite(const char *function, const char *name, const EigMat &y)
Check if the specified square, symmetric 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
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 ...
Definition fvar.hpp:9