Stan Math Library  2.14.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 
13 #include <sstream>
14 #include <string>
15 
16 namespace stan {
17  namespace math {
18 
42  template <typename T_y>
43  inline void
44  check_corr_matrix(const char* function,
45  const char* name,
46  const Eigen::Matrix<T_y, Eigen::Dynamic, Eigen::Dynamic>& y) {
47  using Eigen::Matrix;
48 
49  typedef typename index_type<Matrix<
50  T_y, Eigen::Dynamic, Eigen::Dynamic> >::type size_t;
51 
52  check_size_match(function,
53  "Rows of correlation matrix", y.rows(),
54  "columns of correlation matrix", y.cols());
55  check_positive_size(function, name, "rows", y.rows());
56  check_symmetric(function, "y", y);
57 
58  for (size_t k = 0; k < y.rows(); ++k) {
59  if (!(fabs(y(k, k) - 1.0) <= CONSTRAINT_TOLERANCE)) {
60  std::ostringstream msg;
61  msg << "is not a valid correlation matrix. "
62  << name << "(" << stan::error_index::value + k
63  << "," << stan::error_index::value + k
64  << ") is ";
65  std::string msg_str(msg.str());
66  domain_error(function, name, y(k, k),
67  msg_str.c_str(),
68  ", but should be near 1.0");
69  }
70  }
71  check_pos_definite(function, "y", y);
72  }
73 
74  }
75 }
76 #endif
fvar< T > fabs(const fvar< T > &x)
Definition: fabs.hpp:15
void check_positive_size(const char *function, const char *name, const char *expr, int size)
Check if size is positive.
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 check_symmetric(const char *function, const char *name, const Eigen::Matrix< T_y, Eigen::Dynamic, Eigen::Dynamic > &y)
Check if the specified matrix is symmetric.
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_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.

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