1 #ifndef STAN_MATH_PRIM_SCAL_ERR_CHECK_POSITIVE_HPP
2 #define STAN_MATH_PRIM_SCAL_ERR_CHECK_POSITIVE_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/value_type.hpp>
7 #include <stan/math/prim/scal/meta/length.hpp>
8 #include <stan/math/prim/scal/meta/get.hpp>
9 #include <stan/math/prim/scal/meta/is_vector_like.hpp>
10 #include <type_traits>
11 
12 namespace stan {
13 namespace math {
14 
15 namespace internal {
16 
17 template <typename T_y, bool is_vec>
18 struct positive {
checkstan::math::internal::positive19   static void check(const char* function, const char* name, 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 (!std::is_unsigned<T_y>::value && !(y > 0))
23       domain_error(function, name, y, "is ", ", but must be > 0!");
24   }
25 };
26 
27 template <typename T_y>
28 struct positive<T_y, true> {
checkstan::math::internal::positive29   static void check(const char* function, const char* name, const T_y& y) {
30     using stan::length;
31     for (size_t n = 0; n < length(y); n++) {
32       if (!std::is_unsigned<typename value_type<T_y>::type>::value
33           && !(stan::get(y, n) > 0))
34         domain_error_vec(function, name, y, n, "is ", ", but must be > 0!");
35     }
36   }
37 };
38 
39 }  // namespace internal
40 
41 /**
42  * Check if <code>y</code> is positive.
43  *
44  * This function is vectorized and will check each element of
45  * <code>y</code>.
46  *
47  * @tparam T_y Type of y
48  *
49  * @param function Function name (for error messages)
50  * @param name Variable name (for error messages)
51  * @param y Variable to check
52  *
53  * @throw <code>domain_error</code> if y is negative or zero or
54  *   if any element of y is NaN.
55  */
56 template <typename T_y>
check_positive(const char * function,const char * name,const T_y & y)57 inline void check_positive(const char* function, const char* name,
58                            const T_y& y) {
59   internal::positive<T_y, is_vector_like<T_y>::value>::check(function, name, y);
60 }
61 
62 }  // namespace math
63 }  // namespace stan
64 #endif
65