Stan Math Library  2.20.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 
8 #include <type_traits>
9 #include <string>
10 
11 namespace stan {
12 namespace math {
13 
14 namespace {
15 
16 template <typename T_y, bool is_vec>
17 struct positive {
18  static void check(const char* function, const char* name, const T_y& y) {
19  // have to use not is_unsigned. is_signed will be false
20  // floating point types that have no unsigned versions.
21  if (!std::is_unsigned<T_y>::value && !(y > 0))
22  domain_error(function, name, y, "is ", ", but must be > 0!");
23  }
24 };
25 
26 template <typename T_y>
27 struct positive<T_y, true> {
28  static void check(const char* function, const char* name, const T_y& y) {
29  using stan::length;
30  for (size_t n = 0; n < length(y); n++) {
31  if (!std::is_unsigned<typename value_type<T_y>::type>::value
32  && !(stan::get(y, n) > 0))
33  domain_error_vec(function, name, y, n, "is ", ", but must be > 0!");
34  }
35  }
36 };
37 
38 } // namespace
39 
51 template <typename T_y>
52 inline void check_positive(const char* function, const char* name,
53  const T_y& y) {
54  positive<T_y, is_vector_like<T_y>::value>::check(function, name, y);
55 }
56 
66 inline void check_positive(const char* function, const char* name,
67  const char* expr, int size) {
68  if (size <= 0) {
69  std::stringstream msg;
70  msg << "; dimension size expression = " << expr;
71  std::string msg_str(msg.str());
72  invalid_argument(function, name, size, "must have a positive size, but is ",
73  msg_str.c_str());
74  }
75 }
76 
77 } // namespace math
78 } // namespace stan
79 #endif
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 invalid_argument(const char *function, const char *name, const T &y, const char *msg1, const char *msg2)
Throw an invalid_argument exception with a consistently formatted message.
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.
int size(const std::vector< T > &x)
Return the size of the specified standard vector.
Definition: size.hpp:17
void check_positive(const char *function, const char *name, const T_y &y)
Check if y is positive.

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