Stan Math Library  2.12.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  template <typename T_y,
41  typename T_eq>
42  struct equal<T_y, T_eq, true> {
43  static bool check(const char* function,
44  const char* name,
45  const T_y& y,
46  const T_eq& eq) {
47  using stan::length;
48  using stan::get;
49  VectorView<const T_eq> eq_vec(eq);
50  for (size_t n = 0; n < length(y); n++) {
51  if (!(get(y, n) == eq_vec[n])) {
52  std::stringstream msg;
53  msg << ", but must be equal to ";
54  msg << eq_vec[n];
55  std::string msg_str(msg.str());
56  domain_error_vec(function, name, y, n,
57  "is ", msg_str.c_str());
58  }
59  }
60  return true;
61  }
62  };
63  }
64 
88  template <typename T_y, typename T_eq>
89  inline bool check_equal(const char* function,
90  const char* name,
91  const T_y& y,
92  const T_eq& eq) {
93  return equal<T_y, T_eq, is_vector_like<T_y>::value>
94  ::check(function, name, y, eq);
95  }
96  }
97 }
98 #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:89
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–2016, Stan Development Team.