Stan  1.3
probability, sampling & optimization
 All Classes Namespaces Files Functions Variables Typedefs 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>
5 #include <stan/meta/traits.hpp>
9 
10 namespace stan {
11  namespace math {
12 
13  namespace {
14  template <typename T_y,
15  typename T_result,
16  class Policy,
17  bool is_vec>
18  struct positive {
19  static bool check(const char* function,
20  const T_y& y,
21  const char* name,
22  T_result* result,
23  const Policy&) {
24  // have to use not is_unsigned. is_signed will be false
25  // floating point types that have no unsigned versions.
26  if (!boost::is_unsigned<T_y>::value && !(y > 0))
27  return dom_err(function,y,name,
28  " is %1%, but must be > 0!","",
29  result,Policy());
30  return true;
31  }
32  };
33 
34  template <typename T_y,
35  typename T_result,
36  class Policy>
37  struct positive<T_y, T_result, Policy, true> {
38  static bool check(const char* function,
39  const T_y& y,
40  const char* name,
41  T_result* result,
42  const Policy&) {
43  using stan::length;
44  for (size_t n = 0; n < length(y); n++) {
45  if (!boost::is_unsigned<typename T_y::value_type>::value
46  && !(stan::get(y,n) > 0))
47  return dom_err_vec(n,function,y,name,
48  " is %1%, but must be > 0!","",
49  result,Policy());
50  }
51  return true;
52  }
53  };
54  }
55  template <typename T_y, typename T_result, class Policy>
56  inline bool check_positive(const char* function,
57  const T_y& y,
58  const char* name,
59  T_result* result,
60  const Policy&) {
61  return positive<T_y,T_result,Policy,
63  ::check(function, y, name, result, Policy());
64  }
65  template <typename T_x, typename T_result>
66  inline bool check_positive(const char* function,
67  const T_x& x,
68  const char* name,
69  T_result* result) {
70  return check_positive(function,x,name,result,default_policy());
71  }
72  template <typename T>
73  inline bool check_positive(const char* function,
74  const T& x,
75  const char* name) {
76  return check_positive<T,typename scalar_type<T>::type *>
77  (function,x,name,0,default_policy());
78  }
79 
80  }
81 }
82 #endif

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