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>
26void check_sorted(const char* function, const char* name, const EigVec& y) {
27 const auto& y_ref = to_ref(y);
28 for (Eigen::Index n = 1; n < y_ref.size(); n++) {
29 if (!(y_ref[n] >= y_ref[n - 1])) {
30 std::ostringstream msg1;
31 msg1 << "is not a valid sorted vector."
32 << " The element at " << stan::error_index::value + n << " is ";
33 std::string msg1_str(msg1.str());
34 std::ostringstream msg2;
35 msg2 << ", but should be greater than or equal to the previous element, "
36 << y_ref[n - 1];
37 std::string msg2_str(msg2.str());
38 throw_domain_error(function, name, y_ref[n], msg1_str.c_str(),
39 msg2_str.c_str());
40 }
41 }
42}
43
55template <typename T_y>
56void check_sorted(const char* function, const char* name,
57 const std::vector<T_y>& y) {
58 for (size_t n = 1; n < y.size(); n++) {
59 if (!(y[n] >= y[n - 1])) {
60 std::ostringstream msg1;
61 msg1 << "is not a valid sorted vector."
62 << " The element at " << stan::error_index::value + n << " is ";
63 std::string msg1_str(msg1.str());
64 std::ostringstream msg2;
65 msg2 << ", but should be greater than or equal to the previous element, "
66 << y[n - 1];
67 std::string msg2_str(msg2.str());
68 throw_domain_error(function, name, y[n], msg1_str.c_str(),
69 msg2_str.c_str());
70 }
71 }
72}
73
74} // namespace math
75} // namespace stan
76#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.
ref_type_t< T && > to_ref(T &&a)
This evaluates expensive Eigen expressions.
Definition to_ref.hpp:17
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).
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...