Automatic Differentiation
 
Loading...
Searching...
No Matches
check_ordered.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_PRIM_ERR_CHECK_ORDERED_HPP
2#define STAN_MATH_PRIM_ERR_CHECK_ORDERED_HPP
3
10#include <sstream>
11#include <string>
12#include <vector>
13
14namespace stan {
15namespace math {
16
28template <typename T_y, require_vector_t<T_y>* = nullptr,
29 require_not_std_vector_t<T_y>* = nullptr>
30void check_ordered(const char* function, const char* name, const T_y& y) {
31 const auto& y_ref = to_ref(value_of_rec(y));
32 for (Eigen::Index n = 1; n < y_ref.size(); n++) {
33 if (!(y_ref[n] > y_ref[n - 1])) {
34 [&]() STAN_COLD_PATH {
35 std::ostringstream msg1;
36 msg1 << "is not a valid ordered vector."
37 << " The element at " << stan::error_index::value + n << " is ";
38 std::string msg1_str(msg1.str());
39 std::ostringstream msg2;
40 msg2 << ", but should be greater than the previous element, "
41 << y_ref[n - 1];
42 std::string msg2_str(msg2.str());
43 throw_domain_error(function, name, y_ref[n], msg1_str.c_str(),
44 msg2_str.c_str());
45 }();
46 }
47 }
48}
49
60template <typename T_y, require_std_vector_vt<is_stan_scalar, T_y>* = nullptr>
61void check_ordered(const char* function, const char* name, const T_y& y) {
62 for (size_t n = 1; n < y.size(); n++) {
63 if (!(y[n] > y[n - 1])) {
64 [&]() STAN_COLD_PATH {
65 std::ostringstream msg1;
66 msg1 << "is not a valid ordered vector."
67 << " The element at " << stan::error_index::value + n << " is ";
68 std::string msg1_str(msg1.str());
69 std::ostringstream msg2;
70 msg2 << ", but should be greater than the previous element, "
71 << y[n - 1];
72 std::string msg2_str(msg2.str());
73 throw_domain_error(function, name, y[n], msg1_str.c_str(),
74 msg2_str.c_str());
75 }();
76 }
77 }
78}
79
90template <typename T_y, require_std_vector_t<T_y>* = nullptr,
91 require_not_vt_stan_scalar<T_y>* = nullptr>
92void check_ordered(const char* function, const char* name, const T_y& y) {
93 for (size_t i = 0; i < y.size(); ++i) {
94 check_ordered(function, internal::make_iter_name(name, i).c_str(), y[i]);
95 }
96}
97
98} // namespace math
99} // namespace stan
100#endif
#define STAN_COLD_PATH
auto make_iter_name(const char *name)
double value_of_rec(const fvar< T > &v)
Return the value of the specified variable.
void throw_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.
ref_type_t< T && > to_ref(T &&a)
This evaluates expensive Eigen expressions.
Definition to_ref.hpp:17
void check_ordered(const char *function, const char *name, const T_y &y)
Throw an exception if the specified vector is not sorted into strictly increasing order.
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...