check_not_nan.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_PRIM_SCAL_ERR_CHECK_NOT_NAN_HPP
2 #define STAN_MATH_PRIM_SCAL_ERR_CHECK_NOT_NAN_HPP
3 
10 
11 namespace stan {
12 namespace math {
13 
14 namespace {
15 template <typename T_y, bool is_vec>
16 struct not_nan {
17  static void check(const char* function, const char* name, const T_y& y) {
18  if (is_nan(value_of_rec(y)))
19  domain_error(function, name, y, "is ", ", but must not be nan!");
20  }
21 };
22 
23 template <typename T_y>
24 struct not_nan<T_y, true> {
25  static void check(const char* function, const char* name, const T_y& y) {
26  for (size_t n = 0; n < stan::length(y); n++) {
27  if (is_nan(value_of_rec(stan::get(y, n))))
28  domain_error_vec(function, name, y, n, "is ", ", but must not be nan!");
29  }
30  }
31 };
32 } // namespace
33 
34 /**
35  * Check if <code>y</code> is not
36  * <code>NaN</code>.
37  *
38  * This function is vectorized and will check each element of
39  * <code>y</code>. If any element is <code>NaN</code>, this
40  * function will throw an exception.
41  *
42  * @tparam T_y Type of y
43  *
44  * @param function Function name (for error messages)
45  * @param name Variable name (for error messages)
46  * @param y Variable to check
47  *
48  * @throw <code>domain_error</code> if any element of y is NaN.
49  */
50 template <typename T_y>
51 inline void check_not_nan(const char* function, const char* name,
52  const T_y& y) {
53  not_nan<T_y, is_vector_like<T_y>::value>::check(function, name, y);
54 }
55 
56 } // namespace math
57 } // namespace stan
58 #endif
const XML_Char * name
Definition: expat.h:151
double value_of_rec(const fvar< T > &v)
size_t length(const std::vector< T > &x)
Definition: length.hpp:10
void domain_error_vec(const char *function, const char *name, const T &y, size_t i, const char *msg1, const char *msg2)
const XML_Char int const XML_Char * value
Definition: expat.h:331
void check_not_nan(const char *function, const char *name, const T_y &y)
T get(const std::vector< T > &x, size_t n)
Definition: get.hpp:10
void domain_error(const char *function, const char *name, const T &y, const char *msg1, const char *msg2)
int is_nan(const fvar< T > &x)
Definition: is_nan.hpp:19