| Stan Math Library 2.12.0 reverse mode automatic differentiation

- Main Page
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

check_nonnegative.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_PRIM_SCAL_ERR_CHECK_NONNEGATIVE_HPP
2 #define STAN_MATH_PRIM_SCAL_ERR_CHECK_NONNEGATIVE_HPP
3
4 #include <stan/math/prim/scal/err/domain_error.hpp>
5 #include <stan/math/prim/scal/err/domain_error_vec.hpp>
6 #include <stan/math/prim/scal/meta/length.hpp>
7 #include <stan/math/prim/scal/meta/value_type.hpp>
8 #include <stan/math/prim/scal/meta/is_vector_like.hpp>
9 #include <boost/type_traits/is_unsigned.hpp>
10
11 namespace stan {
12 namespace math {
13
14 namespace {
15 template <typename T_y, bool is_vec>
16 struct nonnegative {
17 static bool check(const char* function,
18 const char* name,
19 const T_y& y) {
20 // have to use not is_unsigned. is_signed will be false
21 // floating point types that have no unsigned versions.
22 if (!boost::is_unsigned<T_y>::value && !(y >= 0))
23 domain_error(function, name, y,
24 "is ", ", but must be >= 0!");
25 return true;
26 }
27 };
28
29 template <typename T_y>
30 struct nonnegative<T_y, true> {
31 static bool check(const char* function,
32 const char* name,
33 const T_y& y) {
34 using stan::length;
35
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 return true;
43 }
44 };
45 }
46
63 template <typename T_y>
64 inline bool check_nonnegative(const char* function,
65 const char* name,
66 const T_y& y) {
67 return nonnegative<T_y, is_vector_like<T_y>::value>
68 ::check(function, name, y);
69 }
70 }
71 }
72 #endif
stan
Definition: log_sum_exp.hpp:8
stan::length
size_t length(const std::vector< T > &x)
Definition: length.hpp:10
domain_error_vec.hpp
is_vector_like.hpp
stan::math::domain_error_vec
void domain_error_vec(const char *function, const char *name, const T &y, const size_t i, const char *msg1, const char *msg2)
Throw a domain error with a consistently formatted message.
Definition: domain_error_vec.hpp:37
stan::get
T get(const std::vector< T > &x, size_t n)
Definition: get.hpp:10
stan::math::domain_error
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.
Definition: domain_error.hpp:30
length.hpp
value_type.hpp
domain_error.hpp
stan::math::check_nonnegative
bool check_nonnegative(const char *function, const char *name, const T_y &y)
Return true if y is non-negative.
Definition: check_nonnegative.hpp:64

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