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

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