Stan  2.5.0
probability, sampling & optimization
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
check_positive.hpp
Go to the documentation of this file.
1 #ifndef STAN__MATH__ERROR_HANDLING_CHECK_POSITIVE_HPP
2 #define STAN__MATH__ERROR_HANDLING_CHECK_POSITIVE_HPP
3 
4 #include <boost/type_traits/is_unsigned.hpp>
8 #include <stan/meta/traits.hpp>
9 
10 namespace stan {
11 
12  namespace math {
13 
14  namespace {
15 
16  template <typename T_y, typename T_result, bool is_vec>
17  struct positive {
18  static bool check(const char* function,
19  const T_y& y,
20  const char* name,
21  T_result* result) {
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  return dom_err(function,y,name,
26  " is %1%, but must be > 0!","",
27  result);
28  return true;
29  }
30  };
31 
32  template <typename T_y, typename T_result>
33  struct positive<T_y, T_result, true> {
34  static bool check(const char* function,
35  const T_y& y,
36  const char* name,
37  T_result* result) {
38  using stan::length;
39  for (size_t n = 0; n < length(y); n++) {
40  if (!boost::is_unsigned<typename value_type<T_y>::type>::value
41  && !(stan::get(y,n) > 0))
42  return dom_err_vec(n,function,y,name,
43  " is %1%, but must be > 0!","",
44  result);
45  }
46  return true;
47  }
48  };
49 
50  }
51 
52  // throws if any element in y is nan
53  template <typename T_y, typename T_result>
54  inline bool check_positive(const char* function,
55  const T_y& y,
56  const char* name,
57  T_result* result) {
58  return positive<T_y,T_result,is_vector_like<T_y>::value>
59  ::check(function, y, name, result);
60  }
61 
62  }
63 }
64 #endif
bool dom_err(const char *function, const T &y, const char *name, const char *error_msg, const T_msg error_msg2, T_result *result)
Definition: dom_err.hpp:36
size_t length(const T &)
Definition: traits.hpp:159
bool dom_err_vec(const size_t i, const char *function, const T &y, const char *name, const char *error_msg, const T_msg error_msg2, T_result *result)
Definition: dom_err_vec.hpp:33
bool check_positive(const char *function, const T_y &y, const char *name, T_result *result)
T get(const T &x, size_t n)
Definition: traits.hpp:143

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