1 #ifndef __STAN__MATH__ERROR_HANDLING_HPP__
2 #define __STAN__MATH__ERROR_HANDLING_HPP__
10 #include <boost/math/policies/policy.hpp>
11 #include <boost/type_traits/is_unsigned.hpp>
35 template <
typename T1,
typename T2>
37 std::pair<T1,T2> xs) {
38 o <<
'(' << xs.first <<
", " << xs.second <<
')';
42 template <
typename T_y,
46 inline bool dom_err(
const char*
function,
49 const char* error_msg,
54 std::ostringstream msg_o;
55 msg_o << name << error_msg << error_msg2;
56 T_result tmp = raise_domain_error<T_result,T_y>(
function,
65 template <
typename T_y,
69 inline bool dom_err_vec(
size_t i,
73 const char* error_msg,
78 std::ostringstream msg_o;
79 msg_o << name <<
"[" << i <<
"] " << error_msg << error_msg2;
80 T_result tmp = raise_domain_error<T_result,typename T_y::value_type>(
function,
91 template <
typename T_y,
96 static bool check(
const char*
function,
102 return dom_err(
function,y,name,
103 " is %1%, but must not be nan!",
"",
109 template <
typename T_y,
112 struct not_nan<T_y, T_result, Policy, true> {
113 static bool check(
const char*
function,
121 return dom_err_vec(n,
function,y,name,
122 " is %1%, but must not be nan!",
"",
141 template <
typename T_y,
149 return not_nan<T_y,T_result,Policy,is_vector<T_y>::value>::check(
function, y, name, result, Policy());
163 template <
typename T_y,
168 T_result* result = 0) {
172 template <
typename T>
176 return check_not_nan<T, typename scalar_type<T>::type *>
182 template <
typename T_y,
187 static bool check(
const char*
function,
193 return dom_err(
function,y,name,
194 " is %1%, but must be finite!",
"",
200 template <
typename T_y,
203 struct finite<T_y, T_result, Policy, true> {
204 static bool check(
const char*
function,
210 for (
size_t n = 0; n <
length(y); n++) {
212 return dom_err_vec(n,
function,y,name,
213 " is %1%, but must be finite!",
"",
223 template <
typename T_y,
typename T_result,
class Policy>
229 return finite<T_y,T_result,Policy,is_vector<T_y>::value>::check(
function, y, name, result, Policy());
232 template <
typename T_y,
typename T_result>
240 template <
typename T>
244 return check_finite<T,typename scalar_type<T>::type *>
249 template <
typename T_y,
255 static bool check(
const char*
function,
263 for (
size_t n = 0; n <
length(low); n++) {
264 if (!(y > low_vec[n]))
265 return dom_err(
function,y,name,
266 " is %1%, but must be greater than ",
267 low_vec[n],result,Policy());
273 template <
typename T_y,
277 struct greater<T_y, T_low, T_result, Policy, true> {
278 static bool check(
const char*
function,
285 VectorView<const T_low> low_vec(low);
286 for (
size_t n = 0; n <
length(y); n++) {
287 if (!(y[n] > low_vec[n])) {
288 return dom_err_vec(n,
function,y,name,
289 " is %1%, but must be greater than ",
290 low_vec[n],result,Policy());
297 template <
typename T_y,
typename T_low,
typename T_result,
class Policy>
304 return greater<T_y,T_low,T_result,Policy,
307 template <
typename T_y,
typename T_low,
typename T_result>
315 template <
typename T_y,
typename T_low>
320 return check_greater<T_y,T_low,typename scalar_type<T_y>::type *>
325 template <
typename T_y,
330 struct greater_or_equal {
331 static bool check(
const char*
function,
339 for (
size_t n = 0; n <
length(low); n++) {
340 if (!(y >= low_vec[n]))
341 return dom_err(
function,y,name,
342 " is %1%, but must be greater than ",
343 low_vec[n],result,Policy());
349 template <
typename T_y,
353 struct greater_or_equal<T_y, T_low, T_result, Policy, true> {
354 static bool check(
const char*
function,
361 VectorView<const T_low> low_vec(low);
362 for (
size_t n = 0; n <
length(y); n++) {
363 if (!(y[n] >= low_vec[n]))
364 return dom_err_vec(n,
function,y,name,
365 " is %1%, but must be greater than or equal to ",
366 low_vec[n],result,Policy());
372 template <
typename T_y,
typename T_low,
typename T_result,
class Policy>
379 return greater_or_equal<T_y,T_low,T_result,Policy,is_vector<T_y>::value>::check(
function,y,low,name,result,Policy());
381 template <
typename T_y,
typename T_low,
typename T_result>
390 template <
typename T_y,
typename T_low>
395 return check_greater_or_equal<T_y,T_low,typename scalar_type<T_y>::type *>
400 template <
typename T_y,
406 static bool check(
const char*
function,
414 for (
size_t n = 0; n <
length(high); n++) {
415 if (!(y < high_vec[n]))
416 return dom_err(
function,y,name,
417 " is %1%, but must be less than ",
418 high_vec[n],result,Policy());
424 template <
typename T_y,
428 struct less<T_y, T_high, T_result, Policy, true> {
429 static bool check(
const char*
function,
436 VectorView<const T_high> high_vec(high);
437 for (
size_t n = 0; n <
length(y); n++) {
438 if (!(y[n] < high_vec[n]))
439 return dom_err_vec(n,
function,y,name,
440 " is %1%, but must be less than ",
441 high_vec[n],result,Policy());
447 template <
typename T_y,
typename T_high,
typename T_result,
class Policy>
454 return less<T_y,T_high,T_result,Policy,is_vector<T_y>::value>::check(
function,y,high,name,result,Policy());
456 template <
typename T_y,
typename T_high,
typename T_result>
464 template <
typename T_y,
typename T_high>
469 return check_less<T_y,T_high,typename scalar_type<T_y>::type *>
474 template <
typename T_y,
479 struct less_or_equal {
480 static bool check(
const char*
function,
488 for (
size_t n = 0; n <
length(high); n++) {
489 if (!(y <= high_vec[n]))
490 return dom_err(
function,y,name,
491 " is %1%, but must be less than or equal to ",
492 high_vec[n],result,Policy());
498 template <
typename T_y,
502 struct less_or_equal<T_y, T_high, T_result, Policy, true> {
503 static bool check(
const char*
function,
510 VectorView<const T_high> high_vec(high);
511 for (
size_t n = 0; n <
length(y); n++) {
512 if (!(y[n] <= high_vec[n]))
513 return dom_err_vec(n,
function,y,name,
514 " is %1%, but must be less than or equal to ",
515 high_vec[n],result,Policy());
521 template <
typename T_y,
typename T_high,
typename T_result,
class Policy>
528 return less_or_equal<T_y,T_high,T_result,Policy,is_vector<T_y>::value>::check(
function,y,high,name,result,Policy());
530 template <
typename T_y,
typename T_high,
typename T_result>
538 template <
typename T_y,
typename T_high>
543 return check_less_or_equal<T_y,T_high,typename scalar_type<T_y>::type *>
548 template <
typename T_y,
555 static bool check(
const char*
function,
566 for (
size_t n = 0; n <
max_size(low, high); n++) {
567 if (!(low_vec[n] <= y && y <= high_vec[n]))
568 return dom_err(
function,y,name,
" is %1%, but must be between ",
576 template <
typename T_y,
581 struct bounded<T_y, T_low, T_high, T_result, Policy, true> {
582 static bool check(
const char*
function,
590 VectorView<const T_low> low_vec(low);
591 VectorView<const T_high> high_vec(high);
592 for (
size_t n = 0; n <
length(y); n++) {
593 if (!(low_vec[n] <= y[n] && y[n] <= high_vec[n]))
594 return dom_err_vec(n,
function,y,name,
595 " is %1%, but must be between ",
603 template <
typename T_y,
typename T_low,
typename T_high,
typename T_result,
class Policy>
611 return bounded<T_y,T_low,T_high,T_result,Policy,is_vector<T_y>::value>::check(
function,y,low,high,name,result,Policy());
613 template <
typename T_y,
typename T_low,
typename T_high,
typename T_result>
622 template <
typename T_y,
typename T_low,
typename T_high>
628 return check_bounded<T_y,T_low,T_high,typename scalar_type<T_y>::type *>
633 template <
typename T_y,
638 static bool check(
const char*
function,
645 if (!boost::is_unsigned<T_y>::value && !(y >= 0))
646 return dom_err(
function,y,name,
647 " is %1%, but must be >= 0!",
"",
653 template <
typename T_y,
656 struct nonnegative<T_y, T_result, Policy, true> {
657 static bool check(
const char*
function,
663 for (
size_t n = 0; n <
length(y); n++) {
664 if (!boost::is_unsigned<typename T_y::value_type>::value && !(y[n] >= 0))
665 return dom_err_vec(n,
function,y,name,
666 " is %1%, but must be >= 0!",
"",
673 template <
typename T_y,
typename T_result,
class Policy>
679 return nonnegative<T_y,T_result,Policy,is_vector<T_y>::value>::check(
function, y, name, result, Policy());
681 template <
typename T_y,
typename T_result>
688 template <
typename T_y>
692 return check_nonnegative<T_y,typename scalar_type<T_y>::type *>
699 template <
typename T_y,
704 static bool check(
const char*
function,
711 if (!boost::is_unsigned<T_y>::value && !(y > 0))
712 return dom_err(
function,y,name,
713 " is %1%, but must be > 0!",
"",
719 template <
typename T_y,
722 struct positive<T_y, T_result, Policy, true> {
723 static bool check(
const char*
function,
729 for (
size_t n = 0; n <
length(y); n++) {
730 if (!boost::is_unsigned<typename T_y::value_type>::value && !(y[n] > 0))
731 return dom_err_vec(n,
function,y,name,
732 " is %1%, but must be > 0!",
"",
739 template <
typename T_y,
typename T_result,
class Policy>
745 return positive<T_y,T_result,Policy,is_vector<T_y>::value>::check(
function, y, name, result, Policy());
747 template <
typename T_x,
typename T_result>
754 template <
typename T>
758 return check_positive<T,typename scalar_type<T>::type *>
762 template <
typename T,
typename T_result,
class Policy>
764 const char*
function,
775 function,x_size,name,
776 " (max size) is %1%, but must be consistent, 1 or max=",
max_size,
780 template <
typename T1,
typename T2,
typename T_result,
794 template <
typename T1,
typename T2,
typename T_result>
805 template <
typename T1,
typename T2,
typename T3,
typename T_result,
822 template <
typename T1,
typename T2,
typename T3,
typename T_result>
834 template <
typename T1,
typename T2,
typename T3,
typename T4,
856 template <
typename T1,
typename T2,
typename T3,
typename T4,
typename T_result>
868 name1,name2,name3,name4,
bool isfinite(const stan::agrad::var v)
Checks if the given number has finite value.
bool isnan(const stan::agrad::var v)
Checks if the given number is NaN.
std::ostream & operator<<(std::ostream &o, const expr_type &et)
T_result raise_domain_error(const char *function, const char *message, const T_val &val, const Policy &)
bool check_greater_or_equal(const char *function, const T_y &y, const T_low &low, const char *name, T_result *result, const Policy &)
int max(const std::vector< int > &x)
Returns the maximum coefficient in the specified column vector.
const double E
The base of the natural logarithm, .
bool check_less(const char *function, const T_y &y, const T_high &high, const char *name, T_result *result, const Policy &)
bool check_not_nan(const char *function, const T_y &y, const char *name, T_result *result, const Policy &)
Checks if the variable y is nan.
bool check_greater(const char *function, const T_y &y, const T_low &low, const char *name, T_result *result, const Policy &)
bool check_consistent_sizes(const char *function, const T1 &x1, const T2 &x2, const char *name1, const char *name2, T_result *result, const Policy &)
bool check_consistent_size(size_t max_size, const char *function, const T &x, const char *name, T_result *result, const Policy &)
bool check_less_or_equal(const char *function, const T_y &y, const T_high &high, const char *name, T_result *result, const Policy &)
bool check_bounded(const char *function, const T_y &y, const T_low &low, const T_high &high, const char *name, T_result *result, const Policy &)
const double CONSTRAINT_TOLERANCE
The tolerance for checking arithmetic bounds In rank and in simplexes.
bool check_positive(const char *function, const T_y &y, const char *name, T_result *result, const Policy &)
bool check_finite(const char *function, const T_y &y, const char *name, T_result *result, const Policy &)
Checks if the variable y is finite.
bool check_nonnegative(const char *function, const T_y &y, const char *name, T_result *result, const Policy &)
boost::math::policies::policy default_policy
Default error-handling policy from Boost.
Probability, optimization and sampling library.
size_t length(const T &x)
size_t size_of(const T &x)
size_t max_size(const T1 &x1, const T2 &x2)
scalar_type_helper< is_vector< T >::value, T >::type type