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 internal {
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, const char* name, const T_y& y,
26  const T_low& low, const T_high& high) {
27  using stan::max_size;
28 
29  scalar_seq_view<T_low> low_vec(low);
30  scalar_seq_view<T_high> high_vec(high);
31  for (size_t n = 0; n < max_size(low, high); n++) {
32  if (!(low_vec[n] <= y && y <= high_vec[n])) {
33  std::stringstream msg;
34  msg << ", but must be in the interval ";
35  msg << "[" << low_vec[n] << ", " << high_vec[n] << "]";
36  std::string msg_str(msg.str());
37  domain_error(function, name, y, "is ", msg_str.c_str());
38  }
39  }
40  }
41 };
42 
43 template <typename T_y, typename T_low, typename T_high>
44 struct bounded<T_y, T_low, T_high, true> {
45  static void check(const char* function, const char* name, const T_y& y,
46  const T_low& low, const T_high& high) {
47  using stan::get;
48  using stan::length;
49 
50  scalar_seq_view<T_low> low_vec(low);
51  scalar_seq_view<T_high> high_vec(high);
52  for (size_t n = 0; n < length(y); n++) {
53  if (!(low_vec[n] <= get(y, n) && get(y, n) <= high_vec[n])) {
54  std::stringstream msg;
55  msg << ", but must be in the interval ";
56  msg << "[" << low_vec[n] << ", " << high_vec[n] << "]";
57  std::string msg_str(msg.str());
58  domain_error_vec(function, name, y, n, "is ", msg_str.c_str());
59  }
60  }
61  }
62 };
63 } // namespace internal
64 
65 /**
66  * Check if the value is between the low and high
67  * values, inclusively.
68  *
69  * @tparam T_y Type of value
70  * @tparam T_low Type of low value
71  * @tparam T_high Type of high value
72  *
73  * @param function Function name (for error messages)
74  * @param name Variable name (for error messages)
75  * @param y Value to check
76  * @param low Low bound
77  * @param high High bound
78  *
79  * @throw <code>std::domain_error</code> otherwise. This also throws
80  * if any of the arguments are NaN.
81  */
82 template <typename T_y, typename T_low, typename T_high>
83 inline void check_bounded(const char* function, const char* name, const T_y& y,
84  const T_low& low, const T_high& high) {
86  function, name, y, low, high);
87 }
88 
89 } // namespace math
90 } // namespace stan
91 #endif
const XML_Char * name
Definition: expat.h:151
void check_bounded(const char *function, const char *name, const T_y &y, const T_low &low, const T_high &high)
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)
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)
static void check(const char *function, const char *name, const T_y &y, const T_low &low, const T_high &high)
enum BeamMode string