Stan Math Library  2.9.0
reverse mode automatic differentiation
check_equal.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_PRIM_SCAL_ERR_CHECK_EQUAL_HPP
2 #define STAN_MATH_PRIM_SCAL_ERR_CHECK_EQUAL_HPP
3 
9 #include <string>
10 
11 namespace stan {
12  namespace math {
13 
14  namespace {
15  template <typename T_y,
16  typename T_eq,
17  bool is_vec>
18  struct equal {
19  static bool check(const char* function,
20  const char* name,
21  const T_y& y,
22  const T_eq& eq) {
23  using stan::length;
24  VectorView<const T_eq> eq_vec(eq);
25  for (size_t n = 0; n < length(eq); n++) {
26  if (!(y == eq_vec[n])) {
27  std::stringstream msg;
28  msg << ", but must be equal to ";
29  msg << eq_vec[n];
30  std::string msg_str(msg.str());
31 
32  domain_error(function, name, y,
33  "is ", msg_str.c_str());
34  }
35  }
36  return true;
37  }
38  };
39 
40  // throws if y or eq is nan
41  template <typename T_y,
42  typename T_eq>
43  struct equal<T_y, T_eq, true> {
44  static bool check(const char* function,
45  const char* name,
46  const T_y& y,
47  const T_eq& eq) {
48  using stan::length;
49  using stan::get;
50  VectorView<const T_eq> eq_vec(eq);
51  for (size_t n = 0; n < length(y); n++) {
52  if (!(get(y, n) == eq_vec[n])) {
53  std::stringstream msg;
54  msg << ", but must be equal to ";
55  msg << eq_vec[n];
56  std::string msg_str(msg.str());
57  domain_error_vec(function, name, y, n,
58  "is ", msg_str.c_str());
59  }
60  }
61  return true;
62  }
63  };
64  }
65 
89  template <typename T_y, typename T_eq>
90  inline bool check_equal(const char* function,
91  const char* name,
92  const T_y& y,
93  const T_eq& eq) {
94  return equal<T_y, T_eq, is_vector_like<T_y>::value>
95  ::check(function, name, y, eq);
96  }
97  }
98 }
99 #endif
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, const size_t i, const char *msg1, const char *msg2)
Throw a domain error with a consistently formatted message.
bool check_equal(const char *function, const char *name, const T_y &y, const T_eq &eq)
Return true if y is equal to eq.
Definition: check_equal.hpp:90
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)
Throw a domain error with a consistently formatted message.

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