Automatic Differentiation
 
Loading...
Searching...
No Matches
check_less_or_equal.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_PRIM_ERR_CHECK_LESS_OR_EQUAL_HPP
2#define STAN_MATH_PRIM_ERR_CHECK_LESS_OR_EQUAL_HPP
3
14#include <string>
15
16namespace stan {
17namespace math {
18
34template <typename T_y, typename T_high,
35 require_all_stan_scalar_t<T_y, T_high>* = nullptr, typename... Idxs>
36inline void check_less_or_equal(const char* function, const char* name,
37 const T_y& y, const T_high& high,
38 Idxs... idxs) {
39 if (unlikely(!(y <= high))) {
40 [](auto y, auto high, auto function, auto name,
41 auto... idxs) STAN_COLD_PATH {
43 function, internal::make_iter_name(name, idxs...).c_str(), y, "is ",
44 (", but must be less than or equal to "
45 + std::to_string(value_of_rec(high)))
46 .c_str());
47 }(y, high, function, name, idxs...);
48 }
49}
50
69template <
70 typename T_y, typename T_high, require_stan_scalar_t<T_y>* = nullptr,
71 require_vector_t<T_high>* = nullptr,
73 typename... Idxs>
74inline void check_less_or_equal(const char* function, const char* name,
75 const T_y& y, const T_high& high,
76 Idxs... idxs) {
77 auto&& high_arr = value_of_rec(as_array_or_scalar(to_ref(high)));
78 for (Eigen::Index i = 0; i < high_arr.size(); ++i) {
79 if (unlikely(!(y <= high_arr.coeff(i)))) {
80 [](auto y, auto&& high_arr, auto name, auto function, auto i,
81 auto... idxs) STAN_COLD_PATH {
83 function, internal::make_iter_name(name, idxs...).c_str(), y, "is ",
84 (", but must be less than or equal to "
85 + std::to_string(high_arr.coeff(i)))
86 .c_str());
87 }(y, high_arr, name, function, i, idxs...);
88 }
89 }
90}
91
110template <typename T_y, typename T_high, require_stan_scalar_t<T_y>* = nullptr,
111 require_dense_dynamic_t<T_high>* = nullptr, typename... Idxs>
112inline void check_less_or_equal(const char* function, const char* name,
113 const T_y& y, const T_high& high,
114 Idxs... idxs) {
115 auto&& high_arr = value_of_rec(to_ref(high));
116 for (Eigen::Index j = 0; j < high_arr.cols(); ++j) {
117 for (Eigen::Index i = 0; i < high_arr.rows(); ++i) {
118 if (unlikely(!(y <= high_arr.coeff(i, j)))) {
119 [](auto y, auto&& high_arr, auto name, auto function, auto i, auto j,
120 auto... idxs) STAN_COLD_PATH {
121 throw_domain_error(function,
122 internal::make_iter_name(name, idxs...).c_str(), y,
123 "is ",
124 (", but must be less than or equal to "
125 + std::to_string(high_arr.coeff(i, j)))
126 .c_str());
127 }(y, high_arr, name, function, i, j, idxs...);
128 }
129 }
130 }
131}
132
151template <typename T_y, typename T_high, require_vector_t<T_y>* = nullptr,
152 require_not_std_vector_vt<is_container_or_var_matrix, T_y>* = nullptr,
153 require_stan_scalar_t<T_high>* = nullptr, typename... Idxs>
154inline void check_less_or_equal(const char* function, const char* name,
155 const T_y& y, const T_high& high,
156 Idxs... idxs) {
157 auto&& y_arr = value_of_rec(as_array_or_scalar(to_ref(y)));
158 for (Eigen::Index i = 0; i < y_arr.size(); ++i) {
159 if (unlikely(!(y_arr.coeff(i) <= high))) {
160 [](auto&& y_arr, auto high, auto name, auto function, auto i,
161 auto... idxs) STAN_COLD_PATH {
162 throw_domain_error_vec(function,
163 internal::make_iter_name(name, idxs...).c_str(),
164 y_arr, i, "is ",
165 (", but must be less than or equal to "
166 + std::to_string(value_of_rec(high)))
167 .c_str());
168 }(y_arr, high, name, function, i, idxs...);
169 }
170 }
171}
172
191template <typename T_y, typename T_high,
192 require_dense_dynamic_t<T_y>* = nullptr,
193 require_stan_scalar_t<T_high>* = nullptr, typename... Idxs>
194inline void check_less_or_equal(const char* function, const char* name,
195 const T_y& y, const T_high& high,
196 Idxs... idxs) {
197 auto&& y_arr = value_of_rec(to_ref(y));
198 for (Eigen::Index j = 0; j < y_arr.cols(); ++j) {
199 for (Eigen::Index i = 0; i < y_arr.rows(); ++i) {
200 if (unlikely(!(y_arr.coeff(i, j) <= high))) {
201 [](auto&& y_arr, auto high, auto name, auto function, auto i, auto j,
202 auto... idxs) STAN_COLD_PATH {
204 function, internal::make_iter_name(name, idxs...).c_str(), y_arr,
205 i, j, "is ",
206 (", but must be less than or equal to "
207 + std::to_string(value_of_rec(high)))
208 .c_str());
209 }(y_arr, high, name, function, i, j, idxs...);
210 }
211 }
212 }
213}
214
235template <typename T_y, typename T_high,
236 require_all_vector_t<T_y, T_high>* = nullptr,
238 T_high>* = nullptr,
239 typename... Idxs>
240inline void check_less_or_equal(const char* function, const char* name,
241 const T_y& y, const T_high& high,
242 Idxs... idxs) {
243 auto&& y_arr = value_of_rec(as_array_or_scalar(to_ref(y)));
244 auto&& high_arr = value_of_rec(as_array_or_scalar(to_ref(high)));
245 for (Eigen::Index i = 0; i < y_arr.size(); ++i) {
246 if (unlikely(!(y_arr.coeff(i) <= high_arr.coeff(i)))) {
247 [](auto&& y_arr, auto&& high_arr, auto name, auto function, auto i,
248 auto... idxs) STAN_COLD_PATH {
249 throw_domain_error_vec(function,
250 internal::make_iter_name(name, idxs...).c_str(),
251 y_arr, i, "is ",
252 (", but must be less than or equal to "
253 + std::to_string(high_arr.coeff(i)))
254 .c_str());
255 }(y_arr, high_arr, name, function, i, idxs...);
256 }
257 }
258}
259
280template <typename T_y, typename T_high,
281 require_all_dense_dynamic_t<T_y, T_high>* = nullptr, typename... Idxs>
282inline void check_less_or_equal(const char* function, const char* name,
283 const T_y& y, const T_high& high,
284 Idxs... idxs) {
285 auto&& y_arr = value_of_rec(to_ref(y));
286 auto&& high_arr = value_of_rec(to_ref(high));
287 for (Eigen::Index j = 0; j < y_arr.cols(); ++j) {
288 for (Eigen::Index i = 0; i < y_arr.rows(); ++i) {
289 if (unlikely(!(y_arr.coeff(i, j) <= high_arr.coeff(i, j)))) {
290 [](auto&& y_arr, auto&& high_arr, auto name, auto function, auto i,
291 auto j, auto... idxs) STAN_COLD_PATH {
293 function, internal::make_iter_name(name, idxs...).c_str(), y_arr,
294 i, j, "is ",
295 (", but must be less than or equal to "
296 + std::to_string(high_arr.coeff(i, j)))
297 .c_str());
298 }(y_arr, high_arr, name, function, i, j, idxs...);
299 }
300 }
301 }
302}
303
321template <typename T_y, typename T_high,
322 require_std_vector_vt<is_container_or_var_matrix, T_y>* = nullptr,
323 require_not_std_vector_t<T_high>* = nullptr, typename... Idxs>
324inline void check_less_or_equal(const char* function, const char* name,
325 const T_y& y, const T_high& high,
326 Idxs... idxs) {
327 for (size_t i = 0; i < y.size(); ++i) {
328 check_less_or_equal(function, name, y[i], high, idxs..., i);
329 }
330}
331
349template <typename T_y, typename T_high,
350 require_not_std_vector_t<T_y>* = nullptr,
351 require_std_vector_vt<is_container_or_var_matrix, T_high>* = nullptr,
352 typename... Idxs>
353inline void check_less_or_equal(const char* function, const char* name,
354 const T_y& y, const T_high& high,
355 Idxs... idxs) {
356 for (size_t i = 0; i < high.size(); ++i) {
357 check_less_or_equal(function, name, y, high[i], idxs..., i);
358 }
359}
360
379template <typename T_y, typename T_high,
381 T_high>* = nullptr,
382 require_all_std_vector_t<T_y, T_high>* = nullptr, typename... Idxs>
383inline void check_less_or_equal(const char* function, const char* name,
384 const T_y& y, const T_high& high,
385 Idxs... idxs) {
386 for (size_t i = 0; i < y.size(); ++i) {
387 check_less_or_equal(function, name, y[i], high[i], idxs..., i);
388 }
389}
390
391} // namespace math
392} // namespace stan
393#endif
#define STAN_COLD_PATH
#define unlikely(x)
require_t< is_stan_scalar< std::decay_t< T > > > require_stan_scalar_t
Require type satisfies is_stan_scalar.
require_any_t< container_type_check_base< is_std_vector, value_type_t, TypeCheck, Check >... > require_any_std_vector_vt
Require any of the types satisfy is_std_vector.
require_not_t< container_type_check_base< is_std_vector, value_type_t, TypeCheck, Check... > > require_not_std_vector_vt
Require type does not satisfy is_std_vector or.
require_all_not_t< container_type_check_base< is_std_vector, value_type_t, TypeCheck, Check >... > require_all_not_std_vector_vt
Require none of the types satisfy is_std_vector.
require_t< is_vector< std::decay_t< T > > > require_vector_t
Require type satisfies is_vector.
auto make_iter_name(const char *name)
double value_of_rec(const fvar< T > &v)
Return the value of the specified variable.
void check_less_or_equal(const char *function, const char *name, const T_y &y, const T_high &high, Idxs... idxs)
Throw an exception if y is not less than high.
T as_array_or_scalar(T &&v)
Returns specified input value.
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 throw_domain_error_vec(const char *function, const char *name, const T &y, size_t i, 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 throw_domain_error_mat(const char *function, const char *name, const T &y, size_t i, size_t j, const char *msg1, const char *msg2)
Throw a domain error with a consistently formatted message for matrices.
bool_constant< math::disjunction< is_container< Container >, is_var_matrix< Container > >::value > is_container_or_var_matrix
Deduces whether type is eigen matrix, standard vector, or var<Matrix>.
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...
Definition fvar.hpp:9