Stan Math Library  2.14.0
reverse mode automatic differentiation
check_positive.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_PRIM_SCAL_ERR_CHECK_POSITIVE_HPP
2 #define STAN_MATH_PRIM_SCAL_ERR_CHECK_POSITIVE_HPP
3 
10 #include <boost/type_traits/is_unsigned.hpp>
11 
12 namespace stan {
13  namespace math {
14 
15  namespace {
16 
17  template <typename T_y, bool is_vec>
18  struct positive {
19  static void check(const char* function,
20  const char* name,
21  const T_y& y) {
22  // have to use not is_unsigned. is_signed will be false
23  // floating point types that have no unsigned versions.
24  if (!boost::is_unsigned<T_y>::value && !(y > 0))
25  domain_error(function, name, y,
26  "is ", ", but must be > 0!");
27  }
28  };
29 
30  template <typename T_y>
31  struct positive<T_y, true> {
32  static void check(const char* function,
33  const char* name,
34  const T_y& y) {
35  using stan::length;
36  for (size_t n = 0; n < length(y); n++) {
37  if (!boost::is_unsigned<typename value_type<T_y>::type>::value
38  && !(stan::get(y, n) > 0))
39  domain_error_vec(function, name, y, n,
40  "is ", ", but must be > 0!");
41  }
42  }
43  };
44 
45  }
46 
62  template <typename T_y>
63  inline void check_positive(const char* function,
64  const char* name,
65  const T_y& y) {
66  positive<T_y, is_vector_like<T_y>::value>::check(function, name, y);
67  }
68 
69  }
70 }
71 #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.
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.
void check_positive(const char *function, const char *name, const T_y &y)
Check if y is positive.

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