Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:39:38

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