Stan Math Library  2.20.0
reverse mode automatic differentiation
check_simplex.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_PRIM_MAT_ERR_CHECK_SIMPLEX_HPP
2 #define STAN_MATH_PRIM_MAT_ERR_CHECK_SIMPLEX_HPP
3 
9 #include <sstream>
10 #include <string>
11 
12 namespace stan {
13 namespace math {
14 
33 template <typename T_prob>
34 void check_simplex(const char* function, const char* name,
35  const Eigen::Matrix<T_prob, Eigen::Dynamic, 1>& theta) {
37  size_t;
38 
39  check_nonzero_size(function, name, theta);
40  if (!(fabs(1.0 - theta.sum()) <= CONSTRAINT_TOLERANCE)) {
41  std::stringstream msg;
42  T_prob sum = theta.sum();
43  msg << "is not a valid simplex.";
44  msg.precision(10);
45  msg << " sum(" << name << ") = " << sum << ", but should be ";
46  std::string msg_str(msg.str());
47  domain_error(function, name, 1.0, msg_str.c_str());
48  }
49  for (size_t n = 0; n < theta.size(); n++) {
50  if (!(theta[n] >= 0)) {
51  std::ostringstream msg;
52  msg << "is not a valid simplex. " << name << "["
53  << n + stan::error_index::value << "]"
54  << " = ";
55  std::string msg_str(msg.str());
56  domain_error(function, name, theta[n], msg_str.c_str(),
57  ", but should be greater than or equal to 0");
58  }
59  }
60 }
61 } // namespace math
62 } // namespace stan
63 #endif
fvar< T > sum(const std::vector< fvar< T > > &m)
Return the sum of the entries of the specified standard vector.
Definition: sum.hpp:20
void check_simplex(const char *function, const char *name, const Eigen::Matrix< T_prob, Eigen::Dynamic, 1 > &theta)
Check if the specified vector is simplex.
void check_nonzero_size(const char *function, const char *name, const T_y &y)
Check if the specified matrix/vector is of non-zero size.
fvar< T > fabs(const fvar< T > &x)
Definition: fabs.hpp:15
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.

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