Stan Math Library  2.11.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 
10 #include <sstream>
11 #include <string>
12 
13 namespace stan {
14 
15  namespace math {
16 
40  template <typename T_prob>
41  bool check_simplex(const char* function,
42  const char* name,
43  const Eigen::Matrix<T_prob, Eigen::Dynamic, 1>& theta) {
44  using Eigen::Dynamic;
45  using Eigen::Matrix;
47 
48  typedef typename index_type<Matrix<T_prob, Dynamic, 1> >::type size_t;
49 
50  check_nonzero_size(function, name, theta);
51  if (!(fabs(1.0 - theta.sum()) <= CONSTRAINT_TOLERANCE)) {
52  std::stringstream msg;
53  T_prob sum = theta.sum();
54  msg << "is not a valid simplex.";
55  msg.precision(10);
56  msg << " sum(" << name << ") = " << sum
57  << ", but should be ";
58  std::string msg_str(msg.str());
59  domain_error(function, name, 1.0,
60  msg_str.c_str());
61  return false;
62  }
63  for (size_t n = 0; n < theta.size(); n++) {
64  if (!(theta[n] >= 0)) {
65  std::ostringstream msg;
66  msg << "is not a valid simplex. "
67  << name << "[" << n + stan::error_index::value << "]"
68  << " = ";
69  std::string msg_str(msg.str());
70  domain_error(function, name, theta[n],
71  msg_str.c_str(),
72  ", but should be greater than or equal to 0");
73  return false;
74  }
75  }
76  return true;
77  }
78  }
79 }
80 #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
fvar< T > fabs(const fvar< T > &x)
Definition: fabs.hpp:14
const double CONSTRAINT_TOLERANCE
The tolerance for checking arithmetic bounds In rank and in simplexes.
bool check_nonzero_size(const char *function, const char *name, const T_y &y)
Return true if the specified matrix/vector is of non-zero size.
Primary template class for the metaprogram to compute the index type of a container.
Definition: index_type.hpp:19
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.
bool check_simplex(const char *function, const char *name, const Eigen::Matrix< T_prob, Eigen::Dynamic, 1 > &theta)
Return true if the specified vector is simplex.

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