Stan Math Library: stan/math/prim/scal/err/check_equal.hpp Source File

| Stan Math Library 2.10.0 reverse mode automatic differentiation

- Main Page
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

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
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/VectorView.hpp>
7 #include <stan/math/prim/scal/meta/length.hpp>
8 #include <stan/math/prim/scal/meta/get.hpp>
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
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
VectorView.hpp
get.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:38
stan::math::check_equal
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
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:32
length.hpp
domain_error.hpp

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