Stan Math Library  2.15.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 
9 #include <string>
10 
11 namespace stan {
12  namespace math {
13 
14  namespace detail {
15 
16  // implemented using structs because there is no partial specialization
17  // for templated functions
18  //
19  // default implementation works for scalar T_y. T_low and T_high can
20  // be either scalar or vector
21  //
22  // throws if y, low, or high is nan
23  template <typename T_y, typename T_low, typename T_high, bool y_is_vec>
24  struct bounded {
25  static void check(const char* function,
26  const char* name,
27  const T_y& y,
28  const T_low& low,
29  const T_high& high) {
30  using stan::max_size;
31 
32  scalar_seq_view<T_low> low_vec(low);
33  scalar_seq_view<T_high> high_vec(high);
34  for (size_t n = 0; n < max_size(low, high); n++) {
35  if (!(low_vec[n] <= y && y <= high_vec[n])) {
36  std::stringstream msg;
37  msg << ", but must be in the interval ";
38  msg << "[" << low_vec[n] << ", " << high_vec[n] << "]";
39  std::string msg_str(msg.str());
40  domain_error(function, name, y,
41  "is ", msg_str.c_str());
42  }
43  }
44  }
45  };
46 
47  template <typename T_y, typename T_low, typename T_high>
48  struct bounded<T_y, T_low, T_high, true> {
49  static void check(const char* function,
50  const char* name,
51  const T_y& y,
52  const T_low& low,
53  const T_high& high) {
54  using stan::length;
55  using stan::get;
56 
57  scalar_seq_view<T_low> low_vec(low);
58  scalar_seq_view<T_high> high_vec(high);
59  for (size_t n = 0; n < length(y); n++) {
60  if (!(low_vec[n] <= get(y, n) && get(y, n) <= high_vec[n])) {
61  std::stringstream msg;
62  msg << ", but must be in the interval ";
63  msg << "[" << low_vec[n] << ", " << high_vec[n] << "]";
64  std::string msg_str(msg.str());
65  domain_error_vec(function, name, y, n,
66  "is ", msg_str.c_str());
67  }
68  }
69  }
70  };
71  }
72 
90  template <typename T_y, typename T_low, typename T_high>
91  inline void check_bounded(const char* function,
92  const char* name,
93  const T_y& y,
94  const T_low& low,
95  const T_high& high) {
97  ::check(function, name, y, low, high);
98  }
99 
100  }
101 }
102 #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)
Definition: length.hpp:10
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)
Definition: get.hpp:10
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–2016, Stan Development Team.