Stan Math Library  2.20.0
reverse mode automatic differentiation
check_bounded.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_PRIM_SCAL_ERR_CHECK_BOUNDED_HPP
2 #define STAN_MATH_PRIM_SCAL_ERR_CHECK_BOUNDED_HPP
3 
7 #include <string>
8 
9 namespace stan {
10 namespace math {
11 
12 namespace internal {
13 
14 // implemented using structs because there is no partial specialization
15 // for templated functions
16 // default implementation works for scalar T_y. T_low and T_high can
17 // be either scalar or vector
18 // throws if y, low, or high is nan
19 template <typename T_y, typename T_low, typename T_high, bool y_is_vec>
20 struct bounded {
21  static void check(const char* function, const char* name, const T_y& y,
22  const T_low& low, const T_high& high) {
23  scalar_seq_view<T_low> low_vec(low);
24  scalar_seq_view<T_high> high_vec(high);
25  for (size_t n = 0; n < stan::max_size(low, high); n++) {
26  if (!(low_vec[n] <= y && y <= high_vec[n])) {
27  std::stringstream msg;
28  msg << ", but must be in the interval ";
29  msg << "[" << low_vec[n] << ", " << high_vec[n] << "]";
30  std::string msg_str(msg.str());
31  domain_error(function, name, y, "is ", msg_str.c_str());
32  }
33  }
34  }
35 };
36 
37 template <typename T_y, typename T_low, typename T_high>
38 struct bounded<T_y, T_low, T_high, true> {
39  static void check(const char* function, const char* name, const T_y& y,
40  const T_low& low, const T_high& high) {
41  scalar_seq_view<T_low> low_vec(low);
42  scalar_seq_view<T_high> high_vec(high);
43  for (size_t n = 0; n < stan::length(y); n++) {
44  if (!(low_vec[n] <= stan::get(y, n) && stan::get(y, n) <= high_vec[n])) {
45  std::stringstream msg;
46  msg << ", but must be in the interval ";
47  msg << "[" << low_vec[n] << ", " << high_vec[n] << "]";
48  std::string msg_str(msg.str());
49  domain_error_vec(function, name, y, n, "is ", msg_str.c_str());
50  }
51  }
52  }
53 };
54 } // namespace internal
55 
69 template <typename T_y, typename T_low, typename T_high>
70 inline void check_bounded(const char* function, const char* name, const T_y& y,
71  const T_low& low, const T_high& high) {
73  function, name, y, low, high);
74 }
75 
76 } // namespace math
77 } // namespace stan
78 #endif
void check_bounded(const char *function, const char *name, const T_y &y, const T_low &low, const T_high &high)
Check if the value is between the low and high values, inclusively.
scalar_seq_view provides a uniform sequence-like wrapper around either a scalar or a sequence of scal...
size_t length(const std::vector< T > &x)
Returns the length of the provided std::vector.
Definition: length.hpp:16
void domain_error_vec(const char *function, const char *name, const T &y, size_t i, const char *msg1, const char *msg2)
Throw a domain error with a consistently formatted message.
static void check(const char *function, const char *name, const T_y &y, const T_low &low, const T_high &high)
static void check(const char *function, const char *name, const T_y &y, const T_low &low, const T_high &high)
T get(const std::vector< T > &x, size_t n)
Returns the n-th element of the provided std::vector.
Definition: get.hpp:16
size_t max_size(const T1 &x1, const T2 &x2)
Definition: max_size.hpp:9
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.