Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-09-18 08:48:19

0001 // Copyright John Maddock 2006, 2007.
0002 // Copyright Paul A. Bristow 2006, 2007, 2012.
0003 // Copyright Matt Borland 2024
0004 
0005 // Use, modification and distribution are subject to the
0006 // Boost Software License, Version 1.0.
0007 // (See accompanying file LICENSE_1_0.txt
0008 // or copy at http://www.boost.org/LICENSE_1_0.txt)
0009 
0010 #ifndef BOOST_MATH_DISTRIBUTIONS_COMMON_ERROR_HANDLING_HPP
0011 #define BOOST_MATH_DISTRIBUTIONS_COMMON_ERROR_HANDLING_HPP
0012 
0013 #include <boost/math/tools/config.hpp>
0014 #include <boost/math/tools/numeric_limits.hpp>
0015 #include <boost/math/policies/error_handling.hpp>
0016 #include <boost/math/special_functions/fpclassify.hpp>
0017 // using boost::math::isfinite;
0018 // using boost::math::isnan;
0019 
0020 #ifdef _MSC_VER
0021 # pragma warning(push)
0022 # pragma warning(disable: 4702) // unreachable code (return after domain_error throw).
0023 #endif
0024 
0025 namespace boost{ namespace math{ namespace detail
0026 {
0027 
0028 template <class RealType, class Policy>
0029 BOOST_MATH_GPU_ENABLED inline bool check_probability(const char* function, RealType const& prob, RealType* result, const Policy& pol)
0030 {
0031    if((prob < 0) || (prob > 1) || !(boost::math::isfinite)(prob))
0032    {
0033       *result = policies::raise_domain_error<RealType>(
0034          function,
0035          "Probability argument is %1%, but must be >= 0 and <= 1 !", prob, pol);
0036       return false;
0037    }
0038    return true;
0039 }
0040 
0041 template <class RealType, class Policy>
0042 BOOST_MATH_GPU_ENABLED inline bool check_df(const char* function, RealType const& df, RealType* result, const Policy& pol)
0043 { //  df > 0 but NOT +infinity allowed.
0044    if((df <= 0) || !(boost::math::isfinite)(df))
0045    {
0046       *result = policies::raise_domain_error<RealType>(
0047          function,
0048          "Degrees of freedom argument is %1%, but must be > 0 !", df, pol);
0049       return false;
0050    }
0051    return true;
0052 }
0053 
0054 template <class RealType, class Policy>
0055 BOOST_MATH_GPU_ENABLED inline bool check_df_gt0_to_inf(const char* function, RealType const& df, RealType* result, const Policy& pol)
0056 {  // df > 0 or +infinity are allowed.
0057    if( (df <= 0) || (boost::math::isnan)(df) )
0058    { // is bad df <= 0 or NaN or -infinity.
0059       *result = policies::raise_domain_error<RealType>(
0060          function,
0061          "Degrees of freedom argument is %1%, but must be > 0 !", df, pol);
0062       return false;
0063    }
0064    return true;
0065 } // check_df_gt0_to_inf
0066 
0067 
0068 template <class RealType, class Policy>
0069 BOOST_MATH_GPU_ENABLED inline bool check_scale(
0070       const char* function,
0071       RealType scale,
0072       RealType* result,
0073       const Policy& pol)
0074 {
0075    if((scale <= 0) || !(boost::math::isfinite)(scale))
0076    { // Assume scale == 0 is NOT valid for any distribution.
0077       *result = policies::raise_domain_error<RealType>(
0078          function,
0079          "Scale parameter is %1%, but must be > 0 !", scale, pol);
0080       return false;
0081    }
0082    return true;
0083 }
0084 
0085 template <class RealType, class Policy>
0086 BOOST_MATH_GPU_ENABLED inline bool check_location(
0087       const char* function,
0088       RealType location,
0089       RealType* result,
0090       const Policy& pol)
0091 {
0092    if(!(boost::math::isfinite)(location))
0093    {
0094       *result = policies::raise_domain_error<RealType>(
0095          function,
0096          "Location parameter is %1%, but must be finite!", location, pol);
0097       return false;
0098    }
0099    return true;
0100 }
0101 
0102 template <class RealType, class Policy>
0103 BOOST_MATH_GPU_ENABLED inline bool check_x(
0104       const char* function,
0105       RealType x,
0106       RealType* result,
0107       const Policy& pol)
0108 {
0109    // Note that this test catches both infinity and NaN.
0110    // Some distributions permit x to be infinite, so these must be tested 1st and return,
0111    // leaving this test to catch any NaNs.
0112    // See Normal, Logistic, Laplace and Cauchy for example.
0113    if(!(boost::math::isfinite)(x))
0114    {
0115       *result = policies::raise_domain_error<RealType>(
0116          function,
0117          "Random variate x is %1%, but must be finite!", x, pol);
0118       return false;
0119    }
0120    return true;
0121 } // bool check_x
0122 
0123 template <class RealType, class Policy>
0124 BOOST_MATH_GPU_ENABLED inline bool check_x_not_NaN(
0125   const char* function,
0126   RealType x,
0127   RealType* result,
0128   const Policy& pol)
0129 {
0130   // Note that this test catches only NaN.
0131   // Some distributions permit x to be infinite, leaving this test to catch any NaNs.
0132   // See Normal, Logistic, Laplace and Cauchy for example.
0133   if ((boost::math::isnan)(x))
0134   {
0135     *result = policies::raise_domain_error<RealType>(
0136       function,
0137       "Random variate x is %1%, but must be finite or + or - infinity!", x, pol);
0138     return false;
0139   }
0140   return true;
0141 } // bool check_x_not_NaN
0142 
0143 template <class RealType, class Policy>
0144 BOOST_MATH_GPU_ENABLED inline bool check_x_gt0(
0145       const char* function,
0146       RealType x,
0147       RealType* result,
0148       const Policy& pol)
0149 {
0150    if(x <= 0)
0151    {
0152       *result = policies::raise_domain_error<RealType>(
0153          function,
0154          "Random variate x is %1%, but must be > 0!", x, pol);
0155       return false;
0156    }
0157 
0158    return true;
0159    // Note that this test catches both infinity and NaN.
0160    // Some special cases permit x to be infinite, so these must be tested 1st,
0161    // leaving this test to catch any NaNs.  See Normal and cauchy for example.
0162 } // bool check_x_gt0
0163 
0164 template <class RealType, class Policy>
0165 BOOST_MATH_GPU_ENABLED inline bool check_positive_x(
0166       const char* function,
0167       RealType x,
0168       RealType* result,
0169       const Policy& pol)
0170 {
0171    if(!(boost::math::isfinite)(x) || (x < 0))
0172    {
0173       *result = policies::raise_domain_error<RealType>(
0174          function,
0175          "Random variate x is %1%, but must be finite and >= 0!", x, pol);
0176       return false;
0177    }
0178    return true;
0179    // Note that this test catches both infinity and NaN.
0180    // Some special cases permit x to be infinite, so these must be tested 1st,
0181    // leaving this test to catch any NaNs.  see Normal and cauchy for example.
0182 }
0183 
0184 template <class RealType, class Policy>
0185 BOOST_MATH_GPU_ENABLED inline bool check_non_centrality(
0186       const char* function,
0187       RealType ncp,
0188       RealType* result,
0189       const Policy& pol)
0190 {
0191    BOOST_MATH_STATIC const RealType upper_limit = static_cast<RealType>((boost::math::numeric_limits<long long>::max)()) - boost::math::policies::get_max_root_iterations<Policy>();
0192 
0193    if((ncp < 0) || !(boost::math::isfinite)(ncp) || ncp > upper_limit)
0194    {
0195       *result = policies::raise_domain_error<RealType>(
0196          function,
0197          "Non centrality parameter is %1%, but must be > 0, and a countable value such that x+1 != x", ncp, pol);
0198       return false;
0199    }
0200    return true;
0201 }
0202 
0203 template <class RealType, class Policy>
0204 BOOST_MATH_GPU_ENABLED inline bool check_finite(
0205       const char* function,
0206       RealType x,
0207       RealType* result,
0208       const Policy& pol)
0209 {
0210    if(!(boost::math::isfinite)(x))
0211    { // Assume scale == 0 is NOT valid for any distribution.
0212       *result = policies::raise_domain_error<RealType>(
0213          function,
0214          "Parameter is %1%, but must be finite !", x, pol);
0215       return false;
0216    }
0217    return true;
0218 }
0219 
0220 } // namespace detail
0221 } // namespace math
0222 } // namespace boost
0223 
0224 #ifdef _MSC_VER
0225 #  pragma warning(pop)
0226 #endif
0227 
0228 #endif // BOOST_MATH_DISTRIBUTIONS_COMMON_ERROR_HANDLING_HPP