check_less.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_PRIM_SCAL_ERR_CHECK_LESS_HPP
2 #define STAN_MATH_PRIM_SCAL_ERR_CHECK_LESS_HPP
3 
9 #include <functional>
10 #include <string>
11 
12 namespace stan {
13 namespace math {
14 
15 namespace {
16 template <typename T_y, typename T_high, bool is_vec>
17 struct less {
18  static void check(const char* function, const char* name, const T_y& y,
19  const T_high& high) {
20  using stan::length;
21  scalar_seq_view<T_high> high_vec(high);
22  for (size_t n = 0; n < length(high); n++) {
23  if (!(y < high_vec[n])) {
24  std::stringstream msg;
25  msg << ", but must be less than ";
26  msg << high_vec[n];
27  std::string msg_str(msg.str());
28  domain_error(function, name, y, "is ", msg_str.c_str());
29  }
30  }
31  }
32 };
33 
34 template <typename T_y, typename T_high>
35 struct less<T_y, T_high, true> {
36  static void check(const char* function, const char* name, const T_y& y,
37  const T_high& high) {
38  using stan::length;
39  scalar_seq_view<T_high> high_vec(high);
40  for (size_t n = 0; n < length(y); n++) {
41  if (!(stan::get(y, n) < high_vec[n])) {
42  std::stringstream msg;
43  msg << ", but must be less than ";
44  msg << high_vec[n];
45  std::string msg_str(msg.str());
46  domain_error_vec(function, name, y, n, "is ", msg_str.c_str());
47  }
48  }
49  }
50 };
51 } // namespace
52 
53 /**
54  * Check if <code>y</code> is strictly less
55  * than <code>high</code>.
56  *
57  * This function is vectorized and will check each element of
58  * <code>y</code> against each element of <code>high</code>.
59  *
60  * @tparam T_y Type of y
61  * @tparam T_high Type of upper bound
62  *
63  * @param function Function name (for error messages)
64  * @param name Variable name (for error messages)
65  * @param y Variable to check
66  * @param high Upper bound
67  *
68  * @throw <code>domain_error</code> if y is not less than low
69  * or if any element of y or high is NaN.
70  */
71 template <typename T_y, typename T_high>
72 inline void check_less(const char* function, const char* name, const T_y& y,
73  const T_high& high) {
74  less<T_y, T_high, is_vector_like<T_y>::value>::check(function, name, y, high);
75 }
76 } // namespace math
77 } // namespace stan
78 #endif
const XML_Char * name
Definition: expat.h:151
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)
const XML_Char int const XML_Char * value
Definition: expat.h:331
T get(const std::vector< T > &x, size_t n)
Definition: get.hpp:10
void domain_error(const char *function, const char *name, const T &y, const char *msg1, const char *msg2)
void check_less(const char *function, const char *name, const T_y &y, const T_high &high)
Definition: check_less.hpp:72
enum BeamMode string