| Stan Math Library 2.6.3 probability, sampling & optimization

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

All Classes Namespaces Files Functions Variables Typedefs Enumerator Friends Macros
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
4 #include <stan/math/prim/scal/err/domain_error.hpp>
5 #include <stan/math/prim/scal/err/domain_error_vec.hpp>
6 #include <stan/math/prim/scal/meta/max_size.hpp>
7 #include <stan/math/prim/scal/meta/VectorView.hpp>
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,
23 bool y_is_vec>
24 struct bounded {
25 static bool check(const char* function,
26 const char* name,
27 const T_y& y,
28 const T_low& low,
29 const T_high& high) {
30 using stan::max_size;
31
32 VectorView<const T_low> low_vec(low);
33 VectorView<const T_high> high_vec(high);
34 for (size_t n = 0; n < max_size(low, high); n++) {
35 if (!(low_vec[n] <= y && y <= high_vec[n])) {
36 std::stringstream msg;
37 msg << ", but must be between ";
38 msg << "(" << low_vec[n] << ", " << high_vec[n] << ")";
39 std::string msg_str(msg.str());
40 domain_error(function, name, y,
41 "is ", msg_str.c_str());
42 }
43 }
44 return true;
45 }
46 };
47
48 template <typename T_y, typename T_low, typename T_high>
49 struct bounded<T_y, T_low, T_high, true> {
50 static bool check(const char* function,
51 const char* name,
52 const T_y& y,
53 const T_low& low,
54 const T_high& high) {
55 using stan::length;
56 using stan::get;
57
58 VectorView<const T_low> low_vec(low);
59 VectorView<const T_high> high_vec(high);
60 for (size_t n = 0; n < length(y); n++) {
61 if (!(low_vec[n] <= get(y, n) && get(y, n) <= high_vec[n])) {
62 std::stringstream msg;
63 msg << ", but must be between ";
64 msg << "(" << low_vec[n] << ", " << high_vec[n] << ")";
65 std::string msg_str(msg.str());
66 domain_error_vec(function, name, y, n,
67 "is ", msg_str.c_str());
68 }
69 }
70 return true;
71 }
72 };
73 }
74
94 template <typename T_y, typename T_low, typename T_high>
95 inline bool check_bounded(const char* function,
96 const char* name,
97 const T_y& y,
98 const T_low& low,
99 const T_high& high) {
100 return detail::bounded<T_y, T_low, T_high,
101 is_vector_like<T_y>::value>
102 ::check(function, name, y, low, high);
103 }
104
105 }
106 }
107 #endif
stan::is_vector_like
Definition: is_vector_like.hpp:12
stan::length
size_t length(const std::vector< T > &x)
Definition: length.hpp:10
stan::math::check_bounded
bool check_bounded(const char *function, const char *name, const T_y &y, const T_low &low, const T_high &high)
Return true if the value is between the low and high values, inclusively.
Definition: check_bounded.hpp:95
stan::math::detail::bounded< T_y, T_low, T_high, true >::check
static bool check(const char *function, const char *name, const T_y &y, const T_low &low, const T_high &high)
Definition: check_bounded.hpp:50
domain_error_vec.hpp
VectorView.hpp
stan::math::detail::bounded::check
static bool check(const char *function, const char *name, const T_y &y, const T_low &low, const T_high &high)
Definition: check_bounded.hpp:25
stan::math::domain_error_vec
void domain_error_vec(const char *function, const char *name, const T &y, const size_t i, const char *msg1, const char *msg2)
Throw a domain error with a consistently formatted message.
Definition: domain_error_vec.hpp:38
stan::get
T get(const std::vector< T > &x, size_t n)
Definition: get.hpp:10
stan::max_size
size_t max_size(const T1 &x1, const T2 &x2)
Definition: max_size.hpp:9
stan::math::domain_error
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.
Definition: domain_error.hpp:32
stan::math::detail::bounded
Definition: check_bounded.hpp:24
domain_error.hpp
stan::VectorView
VectorView is a template metaprogram that takes its argument and allows it to be used like a vector...
Definition: VectorView.hpp:41
max_size.hpp

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