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>
30inline void check_ordered(const char* function, const char* name,
31 const T_y& y) {
32 const auto& y_ref = to_ref(value_of_rec(y));
33 for (Eigen::Index n = 1; n < y_ref.size(); n++) {
34 if (!(y_ref[n] > y_ref[n - 1])) {
35 [&]() STAN_COLD_PATH {
36 std::ostringstream msg1;
37 msg1 << "is not a valid ordered vector."
38 << " The element at " << stan::error_index::value + n << " is ";
39 std::string msg1_str(msg1.str());
40 std::ostringstream msg2;
41 msg2 << ", but should be greater than the previous element, "
42 << y_ref[n - 1];
43 std::string msg2_str(msg2.str());
44 throw_domain_error(function, name, y_ref[n], msg1_str.c_str(),
45 msg2_str.c_str());
46 }();
47 }
48 }
49}
50
61template <typename T_y, require_std_vector_vt<is_stan_scalar, T_y>* = nullptr>
62inline void check_ordered(const char* function, const char* name,
63 const T_y& y) {
64 for (size_t n = 1; n < y.size(); n++) {
65 if (!(y[n] > y[n - 1])) {
66 [&]() STAN_COLD_PATH {
67 std::ostringstream msg1;
68 msg1 << "is not a valid ordered vector."
69 << " The element at " << stan::error_index::value + n << " is ";
70 std::string msg1_str(msg1.str());
71 std::ostringstream msg2;
72 msg2 << ", but should be greater than the previous element, "
73 << y[n - 1];
74 std::string msg2_str(msg2.str());
75 throw_domain_error(function, name, y[n], msg1_str.c_str(),
76 msg2_str.c_str());
77 }();
78 }
79 }
80}
81
92template <typename T_y, require_std_vector_t<T_y>* = nullptr,
93 require_not_vt_stan_scalar<T_y>* = nullptr>
94inline void check_ordered(const char* function, const char* name,
95 const T_y& y) {
96 for (size_t i = 0; i < y.size(); ++i) {
97 check_ordered(function, internal::make_iter_name(name, i).c_str(), y[i]);
98 }
99}
100
101} // namespace math
102} // namespace stan
103#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:18
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 ...