Automatic Differentiation
 
Loading...
Searching...
No Matches
check_greater_or_equal.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_PRIM_ERR_CHECK_GREATER_OR_EQUAL_HPP
2#define STAN_MATH_PRIM_ERR_CHECK_GREATER_OR_EQUAL_HPP
3
13#include <string>
14
15namespace stan {
16namespace math {
17
34template <typename T_y, typename T_low,
35 require_all_stan_scalar_t<T_y, T_low>* = nullptr, typename... Idxs>
36inline void check_greater_or_equal(const char* function, const char* name,
37 const T_y& y, const T_low& low,
38 Idxs... idxs) {
39 if (unlikely(!(y >= low))) {
40 [](auto y, auto low, 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 greater than or equal to "
45 + std::to_string(value_of_rec(low)))
46 .c_str());
47 }(y, low, function, name, idxs...);
48 }
49}
50
69template <
70 typename T_y, typename T_low, require_stan_scalar_t<T_y>* = nullptr,
71 require_vector_t<T_low>* = nullptr,
73 typename... Idxs>
74inline void check_greater_or_equal(const char* function, const char* name,
75 const T_y& y, const T_low& low,
76 Idxs... idxs) {
77 auto&& low_arr = value_of_rec(as_array_or_scalar(to_ref(low)));
78 for (Eigen::Index i = 0; i < low_arr.size(); ++i) {
79 if (unlikely(!(y >= low_arr.coeff(i)))) {
80 [](auto y, auto&& low_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 greater than or equal to "
85 + std::to_string(low_arr.coeff(i)))
86 .c_str());
87 }(y, low_arr, name, function, i, idxs...);
88 }
89 }
90}
91
110template <typename T_y, typename T_low, require_stan_scalar_t<T_y>* = nullptr,
111 require_dense_dynamic_t<T_low>* = nullptr, typename... Idxs>
112inline void check_greater_or_equal(const char* function, const char* name,
113 const T_y& y, const T_low& low,
114 Idxs... idxs) {
115 auto&& low_arr = value_of_rec(to_ref(low));
116 for (Eigen::Index j = 0; j < low_arr.cols(); ++j) {
117 for (Eigen::Index i = 0; i < low_arr.rows(); ++i) {
118 if (unlikely(!(y >= low_arr.coeff(i, j)))) {
119 [](auto y, auto&& low_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 greater than or equal to "
125 + std::to_string(low_arr.coeff(i, j)))
126 .c_str());
127 }(y, low_arr, name, function, i, j, idxs...);
128 }
129 }
130 }
131}
132
151template <typename T_y, typename T_low, 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_low>* = nullptr, typename... Idxs>
154inline void check_greater_or_equal(const char* function, const char* name,
155 const T_y& y, const T_low& low,
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) >= low))) {
160 [](auto&& y_arr, auto low, auto name, auto function, auto i, auto... idxs)
163 function, internal::make_iter_name(name, idxs...).c_str(),
164 y_arr, i, "is ",
165 (", but must be greater than or equal to "
166 + std::to_string(value_of_rec(low)))
167 .c_str());
168 }(y_arr, low, name, function, i, idxs...);
169 }
170 }
171}
172
191template <typename T_y, typename T_low, require_dense_dynamic_t<T_y>* = nullptr,
192 require_stan_scalar_t<T_low>* = nullptr, typename... Idxs>
193inline void check_greater_or_equal(const char* function, const char* name,
194 const T_y& y, const T_low& low,
195 Idxs... idxs) {
196 auto&& y_arr = value_of_rec(to_ref(y));
197 for (Eigen::Index j = 0; j < y_arr.cols(); ++j) {
198 for (Eigen::Index i = 0; i < y_arr.rows(); ++i) {
199 if (unlikely(!(y_arr.coeff(i, j) >= low))) {
200 [](auto&& y_arr, auto low, auto name, auto function, auto i, auto j,
201 auto... idxs) STAN_COLD_PATH {
203 function, internal::make_iter_name(name, idxs...).c_str(), y_arr,
204 i, j, "is ",
205 (", but must be greater than or equal to "
206 + std::to_string(value_of_rec(low)))
207 .c_str());
208 }(y_arr, low, name, function, i, j, idxs...);
209 }
210 }
211 }
212}
213
234template <typename T_y, typename T_low,
235 require_all_vector_t<T_y, T_low>* = nullptr,
237 T_low>* = nullptr,
238 typename... Idxs>
239inline void check_greater_or_equal(const char* function, const char* name,
240 const T_y& y, const T_low& low,
241 Idxs... idxs) {
242 auto&& y_arr = value_of_rec(as_array_or_scalar(to_ref(y)));
243 auto&& low_arr = value_of_rec(as_array_or_scalar(to_ref(low)));
244 for (Eigen::Index i = 0; i < low_arr.size(); ++i) {
245 if (unlikely(!(y_arr.coeff(i) >= low_arr.coeff(i)))) {
246 [](auto&& y_arr, auto&& low_arr, auto name, auto function, auto i,
247 auto... idxs) STAN_COLD_PATH {
248 throw_domain_error_vec(function,
249 internal::make_iter_name(name, idxs...).c_str(),
250 y_arr, i, "is ",
251 (", but must be greater than or equal to "
252 + std::to_string(low_arr.coeff(i)))
253 .c_str());
254 }(y_arr, low_arr, name, function, i, idxs...);
255 }
256 }
257}
258
279template <typename T_y, typename T_low,
280 require_all_dense_dynamic_t<T_y, T_low>* = nullptr, typename... Idxs>
281inline void check_greater_or_equal(const char* function, const char* name,
282 const T_y& y, const T_low& low,
283 Idxs... idxs) {
284 auto&& y_arr = value_of_rec(to_ref(y));
285 auto&& low_arr = value_of_rec(to_ref(low));
286 for (Eigen::Index j = 0; j < low_arr.cols(); ++j) {
287 for (Eigen::Index i = 0; i < low_arr.rows(); ++i) {
288 if (unlikely(!(y_arr.coeff(i, j) >= low_arr.coeff(i, j)))) {
289 [](auto&& y_arr, auto&& low_arr, auto name, auto function, auto i,
290 auto j, auto... idxs) STAN_COLD_PATH {
292 function, internal::make_iter_name(name, idxs...).c_str(), y_arr,
293 i, j, "is ",
294 (", but must be greater than or equal to "
295 + std::to_string(low_arr.coeff(i, j)))
296 .c_str());
297 }(y_arr, low_arr, name, function, i, j, idxs...);
298 }
299 }
300 }
301}
302
320template <typename T_y, typename T_low,
321 require_std_vector_vt<is_container_or_var_matrix, T_y>* = nullptr,
322 require_not_std_vector_t<T_low>* = nullptr, typename... Idxs>
323inline void check_greater_or_equal(const char* function, const char* name,
324 const T_y& y, const T_low& low,
325 Idxs... idxs) {
326 for (size_t i = 0; i < y.size(); ++i) {
327 check_greater_or_equal(function, name, y[i], low, idxs..., i);
328 }
329}
330
348template <typename T_y, typename T_low,
349 require_not_std_vector_t<T_y>* = nullptr,
350 require_std_vector_vt<is_container_or_var_matrix, T_low>* = nullptr,
351 typename... Idxs>
352inline void check_greater_or_equal(const char* function, const char* name,
353 const T_y& y, const T_low& low,
354 Idxs... idxs) {
355 for (size_t i = 0; i < low.size(); ++i) {
356 check_greater_or_equal(function, name, y, low[i], idxs..., i);
357 }
358}
359
376template <typename T_y, typename T_low,
378 T_low>* = nullptr,
379 require_all_std_vector_t<T_y, T_low>* = nullptr, typename... Idxs>
380inline void check_greater_or_equal(const char* function, const char* name,
381 const T_y& y, const T_low& low,
382 Idxs... idxs) {
383 for (size_t i = 0; i < y.size(); ++i) {
384 check_greater_or_equal(function, name, y[i], low[i], idxs..., i);
385 }
386}
387
388} // namespace math
389} // namespace stan
390#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.
void check_greater_or_equal(const char *function, const char *name, const T_y &y, const T_low &low, Idxs... idxs)
Throw an exception if y is not greater or equal than low.
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 ...