Automatic Differentiation
 
Loading...
Searching...
No Matches
check_sorted.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_PRIM_ERR_CHECK_SORTED_HPP
2#define STAN_MATH_PRIM_ERR_CHECK_SORTED_HPP
3
7#include <sstream>
8#include <string>
9#include <vector>
10
11namespace stan {
12namespace math {
13
25template <typename EigVec, require_eigen_vector_t<EigVec>* = nullptr>
26inline void check_sorted(const char* function, const char* name,
27 const EigVec& y) {
28 const auto& y_ref = to_ref(y);
29 for (Eigen::Index n = 1; n < y_ref.size(); n++) {
30 if (!(y_ref[n] >= y_ref[n - 1])) {
31 std::ostringstream msg1;
32 msg1 << "is not a valid sorted vector."
33 << " The element at " << stan::error_index::value + n << " is ";
34 std::string msg1_str(msg1.str());
35 std::ostringstream msg2;
36 msg2 << ", but should be greater than or equal to the previous element, "
37 << y_ref[n - 1];
38 std::string msg2_str(msg2.str());
39 throw_domain_error(function, name, y_ref[n], msg1_str.c_str(),
40 msg2_str.c_str());
41 }
42 }
43}
44
56template <typename T_y>
57inline void check_sorted(const char* function, const char* name,
58 const std::vector<T_y>& y) {
59 for (size_t n = 1; n < y.size(); n++) {
60 if (!(y[n] >= y[n - 1])) {
61 std::ostringstream msg1;
62 msg1 << "is not a valid sorted vector."
63 << " The element at " << stan::error_index::value + n << " is ";
64 std::string msg1_str(msg1.str());
65 std::ostringstream msg2;
66 msg2 << ", but should be greater than or equal to the previous element, "
67 << y[n - 1];
68 std::string msg2_str(msg2.str());
69 throw_domain_error(function, name, y[n], msg1_str.c_str(),
70 msg2_str.c_str());
71 }
72 }
73}
74
75} // namespace math
76} // namespace stan
77#endif
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.
void check_sorted(const char *function, const char *name, const EigVec &y)
Check if the specified vector is sorted into increasing order (repeated values are okay).
ref_type_t< T && > to_ref(T &&a)
This evaluates expensive Eigen expressions.
Definition to_ref.hpp:18
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...