Stan Math Library  2.20.0
reverse mode automatic differentiation
check_corr_matrix.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_PRIM_MAT_ERR_CHECK_CORR_MATRIX_HPP
2 #define STAN_MATH_PRIM_MAT_ERR_CHECK_CORR_MATRIX_HPP
3 
11 #include <sstream>
12 #include <string>
13 
14 namespace stan {
15 namespace math {
16 
34 template <typename T_y>
35 inline void check_corr_matrix(
36  const char* function, const char* name,
37  const Eigen::Matrix<T_y, Eigen::Dynamic, Eigen::Dynamic>& y) {
38  typedef typename index_type<
39  Eigen::Matrix<T_y, Eigen::Dynamic, Eigen::Dynamic> >::type size_t;
40 
41  check_size_match(function, "Rows of correlation matrix", y.rows(),
42  "columns of correlation matrix", y.cols());
43  check_positive(function, name, "rows", y.rows());
44  check_symmetric(function, "y", y);
45 
46  for (size_t k = 0; k < y.rows(); ++k) {
47  if (!(fabs(y(k, k) - 1.0) <= CONSTRAINT_TOLERANCE)) {
48  std::ostringstream msg;
49  msg << "is not a valid correlation matrix. " << name << "("
51  << ") is ";
52  std::string msg_str(msg.str());
53  domain_error(function, name, y(k, k), msg_str.c_str(),
54  ", but should be near 1.0");
55  }
56  }
57  check_pos_definite(function, "y", y);
58 }
59 
60 } // namespace math
61 } // namespace stan
62 #endif
fvar< T > fabs(const fvar< T > &x)
Definition: fabs.hpp:15
void check_size_match(const char *function, const char *name_i, T_size1 i, const char *name_j, T_size2 j)
Check if the provided sizes match.
void check_corr_matrix(const char *function, const char *name, const Eigen::Matrix< T_y, Eigen::Dynamic, Eigen::Dynamic > &y)
Check if the specified matrix is a valid correlation matrix.
const double CONSTRAINT_TOLERANCE
The tolerance for checking arithmetic bounds In rank and in simplexes.
Primary template class for the metaprogram to compute the index type of a container.
Definition: index_type.hpp:18
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.
void check_positive(const char *function, const char *name, const T_y &y)
Check if y is positive.
void check_pos_definite(const char *function, const char *name, const Eigen::Matrix< T_y, -1, -1 > &y)
Check if the specified square, symmetric matrix is positive definite.
void check_symmetric(const char *function, const char *name, const matrix_cl &y)
Check if the matrix_cl is symmetric.

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