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;
93 template <
typename T_y,
98 static bool check(
const char*
function,
104 return dom_err(
function,y,name,
105 " is %1%, but must not be nan!",
"",
111 template <
typename T_y,
114 struct not_nan<T_y, T_result, Policy, true> {
115 static bool check(
const char*
function,
123 return dom_err_vec(n,
function,y,name,
124 " is %1%, but must not be nan!",
"",
143 template <
typename T_y,
151 return not_nan<T_y,T_result,Policy,
153 ::check(
function, y, name, result, Policy());
167 template <
typename T_y,
172 T_result* result = 0) {
176 template <
typename T>
180 return check_not_nan<T, typename scalar_type<T>::type *>
186 template <
typename T_y,
191 static bool check(
const char*
function,
197 return dom_err(
function,y,name,
198 " is %1%, but must be finite!",
"",
204 template <
typename T_y,
207 struct finite<T_y, T_result, Policy, true> {
208 static bool check(
const char*
function,
214 for (
size_t n = 0; n <
length(y); n++) {
216 return dom_err_vec(n,
function,y,name,
217 " is %1%, but must be finite!",
"",
227 template <
typename T_y,
typename T_result,
class Policy>
233 return finite<T_y,T_result,Policy,
235 ::check(
function, y, name, result, Policy());
238 template <
typename T_y,
typename T_result>
246 template <
typename T>
250 return check_finite<T,typename scalar_type<T>::type *>
255 template <
typename T_y,
261 static bool check(
const char*
function,
269 for (
size_t n = 0; n <
length(low); n++) {
270 if (!(y > low_vec[n]))
271 return dom_err(
function,y,name,
272 " is %1%, but must be greater than ",
273 low_vec[n],result,Policy());
279 template <
typename T_y,
283 struct greater<T_y, T_low, T_result, Policy, true> {
284 static bool check(
const char*
function,
291 VectorView<const T_low> low_vec(low);
292 for (
size_t n = 0; n <
length(y); n++) {
294 return dom_err_vec(n,
function,y,name,
295 " is %1%, but must be greater than ",
296 low_vec[n],result,Policy());
303 template <
typename T_y,
typename T_low,
typename T_result,
class Policy>
310 return greater<T_y,T_low,T_result,Policy,
313 template <
typename T_y,
typename T_low,
typename T_result>
321 template <
typename T_y,
typename T_low>
326 return check_greater<T_y,T_low,typename scalar_type<T_y>::type *>
331 template <
typename T_y,
336 struct greater_or_equal {
337 static bool check(
const char*
function,
345 for (
size_t n = 0; n <
length(low); n++) {
346 if (!(y >= low_vec[n]))
347 return dom_err(
function,y,name,
348 " is %1%, but must be greater than ",
349 low_vec[n],result,Policy());
355 template <
typename T_y,
359 struct greater_or_equal<T_y, T_low, T_result, Policy, true> {
360 static bool check(
const char*
function,
368 VectorView<const T_low> low_vec(low);
369 for (
size_t n = 0; n <
length(y); n++) {
370 if (!(
get(y,n) >= low_vec[n]))
371 return dom_err_vec(n,
function,y,name,
372 " is %1%, but must be greater than or equal to ",
373 low_vec[n],result,Policy());
379 template <
typename T_y,
typename T_low,
typename T_result,
class Policy>
386 return greater_or_equal<T_y,T_low,T_result,Policy,is_vector_like<T_y>::value>::check(
function,y,low,name,result,Policy());
388 template <
typename T_y,
typename T_low,
typename T_result>
397 template <
typename T_y,
typename T_low>
402 return check_greater_or_equal<T_y,T_low,typename scalar_type<T_y>::type *>
407 template <
typename T_y,
413 static bool check(
const char*
function,
421 for (
size_t n = 0; n <
length(high); n++) {
422 if (!(y < high_vec[n]))
423 return dom_err(
function,y,name,
424 " is %1%, but must be less than ",
425 high_vec[n],result,Policy());
431 template <
typename T_y,
435 struct less<T_y, T_high, T_result, Policy, true> {
436 static bool check(
const char*
function,
443 VectorView<const T_high> high_vec(high);
444 for (
size_t n = 0; n <
length(y); n++) {
446 return dom_err_vec(n,
function,y,name,
447 " is %1%, but must be less than ",
448 high_vec[n],result,Policy());
454 template <
typename T_y,
typename T_high,
typename T_result,
class Policy>
461 return less<T_y,T_high,T_result,Policy,is_vector_like<T_y>::value>
462 ::check(
function,y,high,name,result,Policy());
464 template <
typename T_y,
typename T_high,
typename T_result>
472 template <
typename T_y,
typename T_high>
477 return check_less<T_y,T_high,typename scalar_type<T_y>::type *>
482 template <
typename T_y,
487 struct less_or_equal {
488 static bool check(
const char*
function,
496 for (
size_t n = 0; n <
length(high); n++) {
497 if (!(y <= high_vec[n]))
498 return dom_err(
function,y,name,
499 " is %1%, but must be less than or equal to ",
500 high_vec[n],result,Policy());
506 template <
typename T_y,
510 struct less_or_equal<T_y, T_high, T_result, Policy, true> {
511 static bool check(
const char*
function,
518 VectorView<const T_high> high_vec(high);
519 for (
size_t n = 0; n <
length(y); n++) {
521 return dom_err_vec(n,
function,y,name,
522 " is %1%, but must be less than or equal to ",
523 high_vec[n],result,Policy());
529 template <
typename T_y,
typename T_high,
typename T_result,
class Policy>
536 return less_or_equal<T_y,T_high,T_result,Policy,
538 ::check(
function,y,high,name,result,Policy());
540 template <
typename T_y,
typename T_high,
typename T_result>
548 template <
typename T_y,
typename T_high>
553 return check_less_or_equal<T_y,T_high,typename scalar_type<T_y>::type *>
558 template <
typename T_y,
565 static bool check(
const char*
function,
576 for (
size_t n = 0; n <
max_size(low, high); n++) {
577 if (!(low_vec[n] <= y && y <= high_vec[n]))
578 return dom_err(
function,y,name,
" is %1%, but must be between ",
588 template <
typename T_y,
593 struct bounded<T_y, T_low, T_high, T_result, Policy, true> {
594 static bool check(
const char*
function,
603 VectorView<const T_low> low_vec(low);
604 VectorView<const T_high> high_vec(high);
605 for (
size_t n = 0; n <
length(y); n++) {
606 if (!(low_vec[n] <=
get(y,n) &&
get(y,n) <= high_vec[n]))
607 return dom_err_vec(n,
function,y,name,
608 " is %1%, but must be between ",
609 std::pair<
typename scalar_type<T_low>::type,
610 typename scalar_type<T_high>::type>(low_vec[n],
618 template <
typename T_y,
typename T_low,
typename T_high,
typename T_result,
class Policy>
626 return bounded<T_y,T_low,T_high,T_result,Policy,
628 ::check(
function,y,low,high,name,result,Policy());
630 template <
typename T_y,
typename T_low,
typename T_high,
typename T_result>
639 template <
typename T_y,
typename T_low,
typename T_high>
645 return check_bounded<T_y,T_low,T_high,typename scalar_type<T_y>::type *>
650 template <
typename T_y,
655 static bool check(
const char*
function,
662 if (!boost::is_unsigned<T_y>::value && !(y >= 0))
663 return dom_err(
function,y,name,
664 " is %1%, but must be >= 0!",
"",
670 template <
typename T_y,
673 struct nonnegative<T_y, T_result, Policy, true> {
674 static bool check(
const char*
function,
680 for (
size_t n = 0; n <
length(y); n++) {
681 if (!boost::is_unsigned<typename T_y::value_type>::value
683 return dom_err_vec(n,
function,y,name,
684 " is %1%, but must be >= 0!",
"",
691 template <
typename T_y,
typename T_result,
class Policy>
697 return nonnegative<T_y,T_result,Policy,
699 ::check(
function, y, name, result, Policy());
701 template <
typename T_y,
typename T_result>
708 template <
typename T_y>
712 return check_nonnegative<T_y,typename scalar_type<T_y>::type *>
719 template <
typename T_y,
724 static bool check(
const char*
function,
731 if (!boost::is_unsigned<T_y>::value && !(y > 0))
732 return dom_err(
function,y,name,
733 " is %1%, but must be > 0!",
"",
739 template <
typename T_y,
742 struct positive<T_y, T_result, Policy, true> {
743 static bool check(
const char*
function,
749 for (
size_t n = 0; n <
length(y); n++) {
750 if (!boost::is_unsigned<typename T_y::value_type>::value
752 return dom_err_vec(n,
function,y,name,
753 " is %1%, but must be > 0!",
"",
760 template <
typename T_y,
typename T_result,
class Policy>
766 return positive<T_y,T_result,Policy,
768 ::check(
function, y, name, result, Policy());
770 template <
typename T_x,
typename T_result>
777 template <
typename T>
781 return check_positive<T,typename scalar_type<T>::type *>
785 template <
typename T,
typename T_result,
class Policy>
787 const char*
function,
798 function,x_size,name,
799 " (max size) is %1%, but must be consistent, 1 or max=",max_size,
803 template <
typename T1,
typename T2,
typename T_result,
817 template <
typename T1,
typename T2,
typename T_result>
828 template <
typename T1,
typename T2,
typename T3,
typename T_result,
845 template <
typename T1,
typename T2,
typename T3,
typename T_result>
857 template <
typename T1,
typename T2,
typename T3,
typename T4,
879 template <
typename T1,
typename T2,
typename T3,
typename T4,
typename T_result>
891 name1,name2,name3,name4,