Automatic Differentiation
 
Loading...
Searching...
No Matches
check_bounded.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_PRIM_ERR_CHECK_BOUNDED_HPP
2#define STAN_MATH_PRIM_ERR_CHECK_BOUNDED_HPP
3
12#include <string>
13
14namespace stan {
15namespace math {
16
17namespace internal {
18
19// implemented using structs because there is no partial specialization
20// for templated functions
21// default implementation works for scalar T_y. T_low and T_high can
22// be either scalar or vector
23// throws if y, low, or high is nan
24template <typename T_y, typename T_low, typename T_high, bool y_is_vec>
25struct bounded {
26 static void check(const char* function, const char* name, const T_y& y,
27 const T_low& low, const T_high& high) {
28 scalar_seq_view<T_low> low_vec(low);
29 scalar_seq_view<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 std::stringstream msg;
33 msg << ", but must be in the interval ";
34 msg << "[" << low_vec[n] << ", " << high_vec[n] << "]";
35 std::string msg_str(msg.str());
36 throw_domain_error(function, name, y, "is ", msg_str.c_str());
37 }
38 }
39 }
40};
41
42template <typename T_y, typename T_low, typename T_high>
43struct bounded<T_y, T_low, T_high, true> {
44 static void check(const char* function, const char* name, const T_y& y,
45 const T_low& low, const T_high& high) {
46 scalar_seq_view<T_low> low_vec(low);
47 scalar_seq_view<T_high> high_vec(high);
48 for (size_t n = 0; n < stan::math::size(y); n++) {
49 if (!(low_vec[n] <= stan::get(y, n) && stan::get(y, n) <= high_vec[n])) {
50 std::stringstream msg;
51 msg << ", but must be in the interval ";
52 msg << "[" << low_vec[n] << ", " << high_vec[n] << "]";
53 std::string msg_str(msg.str());
54 throw_domain_error_vec(function, name, y, n, "is ", msg_str.c_str());
55 }
56 }
57 }
58};
59} // namespace internal
60
74template <typename T_y, typename T_low, typename T_high>
75inline void check_bounded(const char* function, const char* name, const T_y& y,
76 const T_low& low, const T_high& high) {
77 if (size_zero(y, low, high)) {
78 return;
79 }
81 function, name, y, low, high);
82}
83
84} // namespace math
85} // namespace stan
86#endif
scalar_seq_view provides a uniform sequence-like wrapper around either a scalar or a sequence of scal...
T get(const T &x, size_t n)
Returns the provided element.
Definition get.hpp:23
int64_t size(const T &m)
Returns the size (number of the elements) of a matrix_cl or var_value<matrix_cl<T>>.
Definition size.hpp:19
bool size_zero(const T &x)
Returns 1 if input is of length 0, returns 0 otherwise.
Definition size_zero.hpp:19
void check_bounded(const char *function, const char *name, const T_y &y, const T_low &low, const T_high &high)
Check if the value is between the low and high values, inclusively.
void throw_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.
void throw_domain_error_vec(const char *function, const char *name, const T &y, size_t i, const char *msg1, const char *msg2)
Throw a domain error with a consistently formatted message.
int64_t max_size(const T1 &x1, const Ts &... xs)
Calculate the size of the largest input.
Definition max_size.hpp:20
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...
static void check(const char *function, const char *name, const T_y &y, const T_low &low, const T_high &high)
static void check(const char *function, const char *name, const T_y &y, const T_low &low, const T_high &high)