Stan Math Library  2.15.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 
9 #include <boost/type_traits/is_unsigned.hpp>
10 
11 namespace stan {
12  namespace math {
13 
14  namespace {
15  template <typename T_y, bool is_vec>
16  struct nonnegative {
17  static void check(const char* function,
18  const char* name,
19  const T_y& y) {
20  // have to use not is_unsigned. is_signed will be false
21  // floating point types that have no unsigned versions.
22  if (!boost::is_unsigned<T_y>::value && !(y >= 0))
23  domain_error(function, name, y,
24  "is ", ", but must be >= 0!");
25  }
26  };
27 
28  template <typename T_y>
29  struct nonnegative<T_y, true> {
30  static void check(const char* function,
31  const char* name,
32  const T_y& y) {
33  using stan::length;
34 
35  for (size_t n = 0; n < length(y); n++) {
36  if (!boost::is_unsigned<typename value_type<T_y>::type>::value
37  && !(stan::get(y, n) >= 0))
38  domain_error_vec(function, name, y, n,
39  "is ", ", but must be >= 0!");
40  }
41  }
42  };
43  }
44 
60  template <typename T_y>
61  inline void check_nonnegative(const char* function,
62  const char* name,
63  const T_y& y) {
64  nonnegative<T_y, is_vector_like<T_y>::value>::check(function, name, y);
65  }
66  }
67 }
68 #endif
size_t length(const std::vector< T > &x)
Definition: length.hpp:10
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)
Definition: get.hpp:10
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.

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