Stan  2.5.0
probability, sampling & optimization
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
check_bounded.hpp
Go to the documentation of this file.
1 #ifndef STAN__MATH__ERROR_HANDLING__CHECK_BOUNDED_HPP
2 #define STAN__MATH__ERROR_HANDLING__CHECK_BOUNDED_HPP
3 
6 #include <stan/meta/traits.hpp>
7 
8 namespace stan {
9  namespace math {
10 
11  namespace detail {
12 
13  // implemented using structs because there is no partial specialization
14  // for templated functions
15  //
16  // default implementation works for scalar T_y. T_low and T_high can
17  // be either scalar or vector
18  //
19  // throws if y, low, or high is nan
20  template <typename T_y, typename T_low, typename T_high, typename T_result,
21  bool y_is_vec>
22  struct bounded {
23  static bool check(const char* function,
24  const T_y& y,
25  const T_low& low,
26  const T_high& high,
27  const char* name,
28  T_result* result) {
29  using stan::length;
30  using stan::max_size;
31  typedef std::pair<typename scalar_type<T_low>::type,
32  typename scalar_type<T_high>::type> pair_type;
33 
34  VectorView<const T_low> low_vec(low);
35  VectorView<const T_high> high_vec(high);
36  for (size_t n = 0; n < max_size(low, high); n++) {
37  if (!(low_vec[n] <= y && y <= high_vec[n]))
38  return dom_err(function,y,name,
39  " is %1%, but must be between ",
40  pair_type(low_vec[n], high_vec[n]),
41  result);
42  }
43  return true;
44  }
45  };
46 
47  template <typename T_y,
48  typename T_low,
49  typename T_high,
50  typename T_result>
51  struct bounded<T_y, T_low, T_high, T_result, true> {
52  static bool check(const char* function,
53  const T_y& y,
54  const T_low& low,
55  const T_high& high,
56  const char* name,
57  T_result* result) {
58  using stan::length;
59  using stan::get;
60  typedef std::pair<typename scalar_type<T_low>::type,
61  typename scalar_type<T_high>::type> pair_type;
62 
63  VectorView<const T_low> low_vec(low);
64  VectorView<const T_high> high_vec(high);
65  for (size_t n = 0; n < length(y); n++) {
66  if (!(low_vec[n] <= get(y,n) && get(y,n) <= high_vec[n]))
67  return dom_err_vec(n,function,y,name,
68  " is %1%, but must be between ",
69  pair_type(low_vec[n], high_vec[n]),
70  result);
71  }
72  return true;
73  }
74  };
75  }
76 
77  // public check_bounded function
78  template <typename T_y, typename T_low, typename T_high, typename T_result>
79  inline bool check_bounded(const char* function,
80  const T_y& y,
81  const T_low& low,
82  const T_high& high,
83  const char* name,
84  T_result* result) {
85  return detail::bounded<T_y,T_low,T_high,T_result,
87  ::check(function,y,low,high,name,result);
88  }
89 
90  }
91 }
92 #endif
bool check_bounded(const char *function, const T_y &y, const T_low &low, const T_high &high, const char *name, T_result *result)
bool dom_err(const char *function, const T &y, const char *name, const char *error_msg, const T_msg error_msg2, T_result *result)
Definition: dom_err.hpp:36
size_t length(const T &)
Definition: traits.hpp:159
scalar_type_helper< is_vector< T >::value, T >::type type
Definition: traits.hpp:139
bool dom_err_vec(const size_t i, const char *function, const T &y, const char *name, const char *error_msg, const T_msg error_msg2, T_result *result)
Definition: dom_err_vec.hpp:33
size_t max_size(const T1 &x1, const T2 &x2)
Definition: traits.hpp:191
static bool check(const char *function, const T_y &y, const T_low &low, const T_high &high, const char *name, T_result *result)
T get(const T &x, size_t n)
Definition: traits.hpp:143
static bool check(const char *function, const T_y &y, const T_low &low, const T_high &high, const char *name, T_result *result)
VectorView is a template metaprogram that takes its argument and allows it to be used like a vector...
Definition: traits.hpp:275

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