Stan  1.3
probability, sampling & optimization
 All Classes Namespaces Files Functions Variables Typedefs Enumerator Friends Macros Pages
check_nonnegative.hpp
Go to the documentation of this file.
1 #ifndef __STAN__MATH__ERROR_HANDLING__CHECK_NONNEGATIVE_HPP__
2 #define __STAN__MATH__ERROR_HANDLING__CHECK_NONNEGATIVE_HPP__
3 
7 
8 namespace stan {
9  namespace math {
10 
11  namespace {
12  template <typename T_y,
13  typename T_result,
14  class Policy,
15  bool is_vec>
16  struct nonnegative {
17  static bool check(const char* function,
18  const T_y& y,
19  const char* name,
20  T_result* result,
21  const Policy&) {
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,Policy());
28  return true;
29  }
30  };
31 
32  template <typename T_y,
33  typename T_result,
34  class Policy>
35  struct nonnegative<T_y, T_result, Policy, true> {
36  static bool check(const char* function,
37  const T_y& y,
38  const char* name,
39  T_result* result,
40  const Policy&) {
41  using stan::length;
42  for (size_t n = 0; n < length(y); n++) {
43  if (!boost::is_unsigned<typename T_y::value_type>::value
44  && !(stan::get(y,n) >= 0))
45  return dom_err_vec(n,function,y,name,
46  " is %1%, but must be >= 0!","",
47  result,Policy());
48  }
49  return true;
50  }
51  };
52  }
53  template <typename T_y, typename T_result, class Policy>
54  inline bool check_nonnegative(const char* function,
55  const T_y& y,
56  const char* name,
57  T_result* result,
58  const Policy&) {
59  return nonnegative<T_y,T_result,Policy,
61  ::check(function, y, name, result, Policy());
62  }
63  template <typename T_y, typename T_result>
64  inline bool check_nonnegative(const char* function,
65  const T_y& y,
66  const char* name,
67  T_result* result) {
68  return check_nonnegative(function,y,name,result,default_policy());
69  }
70  template <typename T_y>
71  inline bool check_nonnegative(const char* function,
72  const T_y& y,
73  const char* name) {
74  return check_nonnegative<T_y,typename scalar_type<T_y>::type *>
75  (function,y,name,0,default_policy());
76  }
77 
78  }
79 }
80 #endif

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