Stan  1.3
probability, sampling & optimization
 All Classes Namespaces Files Functions Variables Typedefs 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 
10 namespace stan {
11  namespace math {
12 
27  template <typename T_prob,
28  typename T_result,
29  class Policy>
30  bool check_simplex(const char* function,
31  const Eigen::Matrix<T_prob,Eigen::Dynamic,1>& theta,
32  const char* name,
33  T_result* result,
34  const Policy&) {
37  if (theta.size() == 0) {
38  std::string message(name);
39  message += " is not a valid simplex. %1% elements in the vector.";
40  T_result tmp = raise_domain_error<size_t, size_t>(function,
41  message.c_str(),
42  0,
43  Policy());
44  if (result != 0)
45  *result = tmp;
46  return false;
47  }
48  if (fabs(1.0 - theta.sum()) > CONSTRAINT_TOLERANCE) {
49  std::stringstream msg;
50  T_prob sum = theta.sum();
51  msg << "in function check_simplex(%1%), ";
52  msg << name << " is not a valid simplex.";
53  msg << " The sum of the elements should be 1, but is " << sum;
54  T_result tmp = raise_domain_error<T_result,T_prob>(function,
55  msg.str().c_str(),
56  sum,
57  Policy());
58  if (result != 0)
59  *result = tmp;
60  return false;
61  }
62  for (size_t n = 0; n < theta.size(); n++) {
63  if (!(theta[n] >= 0)) {
64  std::ostringstream stream;
65  stream << name << " is not a valid simplex."
66  << " The element at " << n
67  << " is %1%, but should be greater than or equal to 0";
68  T_result tmp
69  = raise_domain_error<T_result,T_prob>(function,
70  stream.str().c_str(),
71  theta[n],
72  Policy());
73  if (result != 0)
74  *result = tmp;
75  return false;
76  }
77  }
78  return true;
79  }
80 
81  template <typename T_y,
82  typename T_result> // = typename T_prob_vector::value_type,
83  inline bool check_simplex(const char* function,
84  const Eigen::Matrix<T_y,Eigen::Dynamic,1>& theta,
85  const char* name,
86  T_result* result) {
87  return check_simplex(function,theta,name,result,default_policy());
88  }
89  template <typename T>
90  inline bool check_simplex(const char* function,
91  const Eigen::Matrix<T,Eigen::Dynamic,1>& theta,
92  const char* name,
93  T* result = 0) {
94  return check_simplex(function,theta,name,result,default_policy());
95  }
96 
97  }
98 }
99 #endif

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