Stan Math Library  2.20.0
reverse mode automatic differentiation
check_nonnegative.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_PRIM_SCAL_ERR_CHECK_NONNEGATIVE_HPP
2 #define STAN_MATH_PRIM_SCAL_ERR_CHECK_NONNEGATIVE_HPP
3 
7 #include <type_traits>
8 
9 namespace stan {
10 namespace math {
11 
12 namespace internal {
13 template <typename T_y, bool is_vec>
14 struct nonnegative {
15  static void check(const char* function, const char* name, const T_y& y) {
16  // have to use not is_unsigned. is_signed will be false
17  // floating point types that have no unsigned versions.
18  if (!std::is_unsigned<T_y>::value && !(y >= 0))
19  domain_error(function, name, y, "is ", ", but must be >= 0!");
20  }
21 };
22 
23 template <typename T_y>
24 struct nonnegative<T_y, true> {
25  static void check(const char* function, const char* name, const T_y& y) {
26  for (size_t n = 0; n < stan::length(y); n++) {
27  if (!std::is_unsigned<typename value_type<T_y>::type>::value
28  && !(stan::get(y, n) >= 0))
29  domain_error_vec(function, name, y, n, "is ", ", but must be >= 0!");
30  }
31  }
32 };
33 } // namespace internal
34 
45 template <typename T_y>
46 inline void check_nonnegative(const char* function, const char* name,
47  const T_y& y) {
49  y);
50 }
51 } // namespace math
52 } // namespace stan
53 #endif
static void check(const char *function, const char *name, const T_y &y)
size_t length(const std::vector< T > &x)
Returns the length of the provided std::vector.
Definition: length.hpp:16
void 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.
void check_nonnegative(const char *function, const char *name, const T_y &y)
Check if y is non-negative.
T get(const std::vector< T > &x, size_t n)
Returns the n-th element of the provided std::vector.
Definition: get.hpp:16
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.
static void check(const char *function, const char *name, const T_y &y)
Primary template class for metaprogram to compute the type of values stored in a container.
Definition: value_type.hpp:18

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