Stan  2.5.0
probability, sampling & optimization
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
check_simplex.hpp
Go to the documentation of this file.
1 #ifndef __STAN__MATH__ERROR_HANDLING__MATRIX__CHECK_SIMPLEX_HPP__
2 #define __STAN__MATH__ERROR_HANDLING__MATRIX__CHECK_SIMPLEX_HPP__
3 
4 #include <sstream>
9 #include <stan/meta/traits.hpp>
10 
11 
12 namespace stan {
13 
14  namespace math {
15 
31  template <typename T_prob, typename T_result>
32  bool check_simplex(const char* function,
33  const Eigen::Matrix<T_prob,Eigen::Dynamic,1>& theta,
34  const char* name,
35  T_result* result) {
36  using Eigen::Dynamic;
37  using Eigen::Matrix;
38  typedef typename index_type<Matrix<T_prob,Dynamic,1> >::type size_t;
39 
40  if (theta.size() == 0) {
41  std::stringstream msg;
42  msg << " is not a valid simplex. "
43  << "length(" << name << ") = %1%";
44  std::string tmp(msg.str());
45  return dom_err(function,0,name,
46  tmp.c_str(),"",
47  result);
48  }
49  if (!(fabs(1.0 - theta.sum()) <= CONSTRAINT_TOLERANCE)) {
50  std::stringstream msg;
51  T_prob sum = theta.sum();
52  msg << " is not a valid simplex.";
53  msg.precision(10);
54  msg << " sum(" << name << ") = " << sum
55  << ", but should be %1%";
56  std::string tmp(msg.str());
57  return dom_err(function,1.0,name,
58  tmp.c_str(),"",
59  result);
60  }
61  for (size_t n = 0; n < theta.size(); n++) {
62  if (!(theta[n] >= 0)) {
63  std::ostringstream stream;
64  stream << " is not a valid simplex. "
65  << name << "[" << n + stan::error_index::value << "]"
66  << " = %1%, but should be greater than or equal to 0";
67  std::string tmp(stream.str());
68  return dom_err(function,theta[n],name,
69  tmp.c_str(),"",
70  result);
71  }
72  }
73  return true;
74  }
75 
76  }
77 }
78 #endif
fvar< T > fabs(const fvar< T > &x)
Definition: fabs.hpp:18
bool dom_err(const char *function, const T &y, const char *name, const char *error_msg, const T_msg error_msg2, T_result *result)
Definition: dom_err.hpp:36
bool check_simplex(const char *function, const Eigen::Matrix< T_prob, Eigen::Dynamic, 1 > &theta, const char *name, T_result *result)
Return true if the specified vector is simplex.
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:21
double sum(std::vector< double > &x)
Definition: sum.hpp:10

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