Stan  1.3
probability, sampling & optimization
 All Classes Namespaces Files Functions Variables Typedefs 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 
7 
8 namespace stan {
9  namespace math {
10 
11  namespace {
12  template <typename T_y,
13  typename T_low,
14  typename T_high,
15  typename T_result,
16  class Policy,
17  bool is_vec>
18  struct bounded {
19  static bool check(const char* function,
20  const T_y& y,
21  const T_low& low,
22  const T_high& high,
23  const char* name,
24  T_result* result,
25  const Policy&) {
26  using stan::length;
27  using stan::max_size;
28  VectorView<const T_low> low_vec(low);
29  VectorView<const T_high> high_vec(high);
30  for (size_t n = 0; n < max_size(low, high); n++) {
31  if (!(low_vec[n] <= y && y <= high_vec[n]))
32  return dom_err(function,y,name," is %1%, but must be between ",
33  std::pair<typename scalar_type<T_low>::type,
34  typename scalar_type<T_high>::type>(low_vec[n],
35  high_vec[n]),
36  result,Policy());
37  }
38  return true;
39  }
40  };
41 
42  template <typename T_y,
43  typename T_low,
44  typename T_high,
45  typename T_result,
46  class Policy>
47  struct bounded<T_y, T_low, T_high, T_result, Policy, true> {
48  static bool check(const char* function,
49  const T_y& y,
50  const T_low& low,
51  const T_high& high,
52  const char* name,
53  T_result* result,
54  const Policy&) {
55  using stan::length;
56  using stan::get;
57  VectorView<const T_low> low_vec(low);
58  VectorView<const 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  return dom_err_vec(n,function,y,name,
62  " is %1%, but must be between ",
63  std::pair<typename scalar_type<T_low>::type,
64  typename scalar_type<T_high>::type>(low_vec[n],
65  high_vec[n]),
66  result,Policy());
67  }
68  return true;
69  }
70  };
71  }
72  template <typename T_y, typename T_low, typename T_high, typename T_result, class Policy>
73  inline bool check_bounded(const char* function,
74  const T_y& y,
75  const T_low& low,
76  const T_high& high,
77  const char* name,
78  T_result* result,
79  const Policy&) {
80  return bounded<T_y,T_low,T_high,T_result,Policy,
82  ::check(function,y,low,high,name,result,Policy());
83  }
84  template <typename T_y, typename T_low, typename T_high, typename T_result>
85  inline bool check_bounded(const char* function,
86  const T_y& y,
87  const T_low& low,
88  const T_high& high,
89  const char* name,
90  T_result* result) {
91  return check_bounded(function,y,low,high,name,result,default_policy());
92  }
93  template <typename T_y, typename T_low, typename T_high>
94  inline bool check_bounded(const char* function,
95  const T_y& y,
96  const T_low& low,
97  const T_high& high,
98  const char* name) {
99  return check_bounded<T_y,T_low,T_high,typename scalar_type<T_y>::type *>
100  (function,y,low,high,name,0,default_policy());
101  }
102 
103  }
104 }
105 #endif

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