Automatic Differentiation
 
Loading...
Searching...
No Matches
check_less.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_PRIM_ERR_CHECK_LESS_HPP
2#define STAN_MATH_PRIM_ERR_CHECK_LESS_HPP
3
13#include <string>
14
15namespace stan {
16namespace math {
17
33template <typename T_y, typename T_high,
34 require_all_stan_scalar_t<T_y, T_high>* = nullptr, typename... Idxs>
35inline void check_less(const char* function, const char* name, const T_y& y,
36 const T_high& high, Idxs... idxs) {
37 if (!(y < high)) {
38 [](auto y, auto high, auto function, auto name,
39 auto... idxs) STAN_COLD_PATH {
41 function, internal::make_iter_name(name, idxs...).c_str(), y, "is ",
42 (", but must be less than " + std::to_string(value_of_rec(high)))
43 .c_str());
44 }(y, high, function, name, idxs...);
45 }
46}
47
66template <
67 typename T_y, typename T_high, require_stan_scalar_t<T_y>* = nullptr,
68 require_vector_t<T_high>* = nullptr,
70 typename... Idxs>
71inline void check_less(const char* function, const char* name, const T_y& y,
72 const T_high& high, Idxs... idxs) {
73 auto&& high_arr = as_array_or_scalar(value_of_rec(to_ref(high)));
74 for (Eigen::Index i = 0; i < high_arr.size(); ++i) {
75 if (unlikely(!(y < high_arr.coeff(i)))) {
76 [](auto y, auto&& high_arr, auto name, auto function, auto i,
77 auto... idxs) STAN_COLD_PATH {
79 function, internal::make_iter_name(name, idxs...).c_str(), y, "is ",
80 (", but must be less than " + std::to_string(high_arr.coeff(i)))
81 .c_str());
82 }(y, high_arr, name, function, i, idxs...);
83 }
84 }
85}
86
105template <typename T_y, typename T_high, require_stan_scalar_t<T_y>* = nullptr,
106 require_dense_dynamic_t<T_high>* = nullptr, typename... Idxs>
107inline void check_less(const char* function, const char* name, const T_y& y,
108 const T_high& high, Idxs... idxs) {
109 auto&& high_arr = value_of_rec(to_ref(high));
110 for (Eigen::Index j = 0; j < high_arr.cols(); ++j) {
111 for (Eigen::Index i = 0; i < high_arr.rows(); ++i) {
112 if (unlikely(!(y < high_arr.coeff(i, j)))) {
113 [](auto y, auto&& high_arr, auto name, auto function, auto i, auto j,
114 auto... idxs) STAN_COLD_PATH {
115 throw_domain_error(function,
116 internal::make_iter_name(name, idxs...).c_str(), y,
117 "is ",
118 (", but must be less than "
119 + std::to_string(high_arr.coeff(i, j)))
120 .c_str());
121 }(y, high_arr, name, function, i, j, idxs...);
122 }
123 }
124 }
125}
126
145template <typename T_y, typename T_high, require_vector_t<T_y>* = nullptr,
146 require_not_std_vector_vt<is_container_or_var_matrix, T_y>* = nullptr,
147 require_stan_scalar_t<T_high>* = nullptr, typename... Idxs>
148inline void check_less(const char* function, const char* name, const T_y& y,
149 const T_high& high, Idxs... idxs) {
150 auto&& y_arr = value_of_rec(as_array_or_scalar(to_ref(y)));
151 for (Eigen::Index i = 0; i < y_arr.size(); ++i) {
152 if (unlikely(!(y_arr.coeff(i) < high))) {
153 [](auto&& y_arr, auto high, auto name, auto function, auto i,
154 auto... idxs) STAN_COLD_PATH {
156 function, internal::make_iter_name(name, idxs...).c_str(), y_arr, i,
157 "is ",
158 (", but must be less than " + std::to_string(value_of_rec(high)))
159 .c_str());
160 }(y_arr, high, name, function, i, idxs...);
161 }
162 }
163}
164
183template <typename T_y, typename T_high,
184 require_dense_dynamic_t<T_y>* = nullptr,
185 require_stan_scalar_t<T_high>* = nullptr, typename... Idxs>
186inline void check_less(const char* function, const char* name, const T_y& y,
187 const T_high& high, Idxs... idxs) {
188 auto&& y_arr = value_of_rec(to_ref(y));
189 for (Eigen::Index j = 0; j < y_arr.cols(); ++j) {
190 for (Eigen::Index i = 0; i < y_arr.rows(); ++i) {
191 if (unlikely(!(y_arr.coeff(i, j) < high))) {
192 [](auto&& y_arr, auto high, auto name, auto function, auto i, auto j,
193 auto... idxs) STAN_COLD_PATH {
195 function, internal::make_iter_name(name, idxs...).c_str(), y_arr,
196 i, j, "is ",
197 (", but must be less than " + std::to_string(value_of_rec(high)))
198 .c_str());
199 }(y_arr, high, name, function, i, j, idxs...);
200 }
201 }
202 }
203}
204
225template <typename T_y, typename T_high,
226 require_all_vector_t<T_y, T_high>* = nullptr,
228 T_high>* = nullptr,
229 typename... Idxs>
230inline void check_less(const char* function, const char* name, const T_y& y,
231 const T_high& high, Idxs... idxs) {
232 auto&& y_arr = value_of_rec(to_ref(y));
233 auto&& high_arr = value_of_rec(to_ref(high));
234 for (Eigen::Index i = 0; i < y_arr.size(); ++i) {
235 if (unlikely(!(y_arr.coeff(i) < high_arr.coeff(i)))) {
236 [](auto&& y_arr, auto&& high_arr, auto name, auto function, auto i,
237 auto... idxs) STAN_COLD_PATH {
239 function, internal::make_iter_name(name, idxs...).c_str(), y_arr, i,
240 "is ",
241 (", but must be less than " + std::to_string(high_arr.coeff(i)))
242 .c_str());
243 }(y_arr, high_arr, name, function, i, idxs...);
244 }
245 }
246}
247
268template <typename T_y, typename T_high,
269 require_all_dense_dynamic_t<T_y, T_high>* = nullptr, typename... Idxs>
270inline void check_less(const char* function, const char* name, const T_y& y,
271 const T_high& high, Idxs... idxs) {
272 auto&& y_arr = value_of_rec(to_ref(y));
273 auto&& high_arr = value_of_rec(to_ref(high));
274 for (Eigen::Index j = 0; j < y_arr.cols(); ++j) {
275 for (Eigen::Index i = 0; i < y_arr.rows(); ++i) {
276 if (unlikely(!(y_arr.coeff(i, j) < high_arr.coeff(i, j)))) {
277 [](auto&& y_arr, auto&& high_arr, auto name, auto function, auto i,
278 auto j, auto... idxs) STAN_COLD_PATH {
280 function, internal::make_iter_name(name, idxs...).c_str(), y_arr,
281 i, j, "is ",
282 (", but must be less than "
283 + std::to_string(high_arr.coeff(i, j)))
284 .c_str());
285 }(y_arr, high_arr, name, function, i, j, idxs...);
286 }
287 }
288 }
289}
290
308template <typename T_y, typename T_high,
309 require_std_vector_vt<is_container_or_var_matrix, T_y>* = nullptr,
310 require_not_std_vector_t<T_high>* = nullptr, typename... Idxs>
311inline void check_less(const char* function, const char* name, const T_y& y,
312 const T_high& high, Idxs... idxs) {
313 for (size_t i = 0; i < y.size(); ++i) {
314 check_less(function, name, y[i], high, idxs..., i);
315 }
316}
317
335template <typename T_y, typename T_high,
336 require_not_std_vector_t<T_y>* = nullptr,
337 require_std_vector_vt<is_container_or_var_matrix, T_high>* = nullptr,
338 typename... Idxs>
339inline void check_less(const char* function, const char* name, const T_y& y,
340 const T_high& high, Idxs... idxs) {
341 for (size_t i = 0; i < high.size(); ++i) {
342 check_less(function, name, y, high[i], idxs..., i);
343 }
344}
345
364template <typename T_y, typename T_high,
366 T_high>* = nullptr,
367 require_all_std_vector_t<T_y, T_high>* = nullptr, typename... Idxs>
368inline void check_less(const char* function, const char* name, const T_y& y,
369 const T_high& high, Idxs... idxs) {
370 for (size_t i = 0; i < y.size(); ++i) {
371 check_less(function, name, y[i], high[i], idxs..., i);
372 }
373}
374
375} // namespace math
376} // namespace stan
377#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.
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.
void check_less(const char *function, const char *name, const T_y &y, const T_high &high, Idxs... idxs)
Throw an exception if y is not strictly less than high.
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