]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | // Copyright John Maddock 2006, 2007. |
2 | // Copyright Paul A. Bristow 2006, 2007, 2012. | |
3 | ||
4 | // Use, modification and distribution are subject to the | |
5 | // Boost Software License, Version 1.0. | |
6 | // (See accompanying file LICENSE_1_0.txt | |
7 | // or copy at http://www.boost.org/LICENSE_1_0.txt) | |
8 | ||
9 | #ifndef BOOST_MATH_DISTRIBUTIONS_COMMON_ERROR_HANDLING_HPP | |
10 | #define BOOST_MATH_DISTRIBUTIONS_COMMON_ERROR_HANDLING_HPP | |
11 | ||
12 | #include <boost/math/policies/error_handling.hpp> | |
13 | #include <boost/math/special_functions/fpclassify.hpp> | |
14 | // using boost::math::isfinite; | |
15 | // using boost::math::isnan; | |
16 | ||
17 | namespace boost{ namespace math{ namespace detail | |
18 | { | |
19 | ||
20 | template <class RealType, class Policy> | |
21 | inline bool check_probability(const char* function, RealType const& prob, RealType* result, const Policy& pol) | |
22 | { | |
23 | if((prob < 0) || (prob > 1) || !(boost::math::isfinite)(prob)) | |
24 | { | |
25 | *result = policies::raise_domain_error<RealType>( | |
26 | function, | |
27 | "Probability argument is %1%, but must be >= 0 and <= 1 !", prob, pol); | |
28 | return false; | |
29 | } | |
30 | return true; | |
31 | } | |
32 | ||
33 | template <class RealType, class Policy> | |
34 | inline bool check_df(const char* function, RealType const& df, RealType* result, const Policy& pol) | |
35 | { // df > 0 but NOT +infinity allowed. | |
36 | if((df <= 0) || !(boost::math::isfinite)(df)) | |
37 | { | |
38 | *result = policies::raise_domain_error<RealType>( | |
39 | function, | |
40 | "Degrees of freedom argument is %1%, but must be > 0 !", df, pol); | |
41 | return false; | |
42 | } | |
43 | return true; | |
44 | } | |
45 | ||
46 | template <class RealType, class Policy> | |
47 | inline bool check_df_gt0_to_inf(const char* function, RealType const& df, RealType* result, const Policy& pol) | |
48 | { // df > 0 or +infinity are allowed. | |
49 | if( (df <= 0) || (boost::math::isnan)(df) ) | |
50 | { // is bad df <= 0 or NaN or -infinity. | |
51 | *result = policies::raise_domain_error<RealType>( | |
52 | function, | |
53 | "Degrees of freedom argument is %1%, but must be > 0 !", df, pol); | |
54 | return false; | |
55 | } | |
56 | return true; | |
57 | } // check_df_gt0_to_inf | |
58 | ||
59 | ||
60 | template <class RealType, class Policy> | |
61 | inline bool check_scale( | |
62 | const char* function, | |
63 | RealType scale, | |
64 | RealType* result, | |
65 | const Policy& pol) | |
66 | { | |
67 | if((scale <= 0) || !(boost::math::isfinite)(scale)) | |
68 | { // Assume scale == 0 is NOT valid for any distribution. | |
69 | *result = policies::raise_domain_error<RealType>( | |
70 | function, | |
71 | "Scale parameter is %1%, but must be > 0 !", scale, pol); | |
72 | return false; | |
73 | } | |
74 | return true; | |
75 | } | |
76 | ||
77 | template <class RealType, class Policy> | |
78 | inline bool check_location( | |
79 | const char* function, | |
80 | RealType location, | |
81 | RealType* result, | |
82 | const Policy& pol) | |
83 | { | |
84 | if(!(boost::math::isfinite)(location)) | |
85 | { | |
86 | *result = policies::raise_domain_error<RealType>( | |
87 | function, | |
88 | "Location parameter is %1%, but must be finite!", location, pol); | |
89 | return false; | |
90 | } | |
91 | return true; | |
92 | } | |
93 | ||
94 | template <class RealType, class Policy> | |
95 | inline bool check_x( | |
96 | const char* function, | |
97 | RealType x, | |
98 | RealType* result, | |
99 | const Policy& pol) | |
100 | { | |
101 | // Note that this test catches both infinity and NaN. | |
102 | // Some distributions permit x to be infinite, so these must be tested 1st and return, | |
103 | // leaving this test to catch any NaNs. | |
104 | // See Normal, Logistic, Laplace and Cauchy for example. | |
105 | if(!(boost::math::isfinite)(x)) | |
106 | { | |
107 | *result = policies::raise_domain_error<RealType>( | |
108 | function, | |
109 | "Random variate x is %1%, but must be finite!", x, pol); | |
110 | return false; | |
111 | } | |
112 | return true; | |
113 | } // bool check_x | |
114 | ||
115 | template <class RealType, class Policy> | |
116 | inline bool check_x_gt0( | |
117 | const char* function, | |
118 | RealType x, | |
119 | RealType* result, | |
120 | const Policy& pol) | |
121 | { | |
122 | if(x <= 0) | |
123 | { | |
124 | *result = policies::raise_domain_error<RealType>( | |
125 | function, | |
126 | "Random variate x is %1%, but must be > 0!", x, pol); | |
127 | return false; | |
128 | } | |
129 | ||
130 | return true; | |
131 | // Note that this test catches both infinity and NaN. | |
132 | // Some special cases permit x to be infinite, so these must be tested 1st, | |
133 | // leaving this test to catch any NaNs. See Normal and cauchy for example. | |
134 | } // bool check_x_gt0 | |
135 | ||
136 | template <class RealType, class Policy> | |
137 | inline bool check_positive_x( | |
138 | const char* function, | |
139 | RealType x, | |
140 | RealType* result, | |
141 | const Policy& pol) | |
142 | { | |
143 | if(!(boost::math::isfinite)(x) || (x < 0)) | |
144 | { | |
145 | *result = policies::raise_domain_error<RealType>( | |
146 | function, | |
147 | "Random variate x is %1%, but must be finite and >= 0!", x, pol); | |
148 | return false; | |
149 | } | |
150 | return true; | |
151 | // Note that this test catches both infinity and NaN. | |
152 | // Some special cases permit x to be infinite, so these must be tested 1st, | |
153 | // leaving this test to catch any NaNs. see Normal and cauchy for example. | |
154 | } | |
155 | ||
156 | template <class RealType, class Policy> | |
157 | inline bool check_non_centrality( | |
158 | const char* function, | |
159 | RealType ncp, | |
160 | RealType* result, | |
161 | const Policy& pol) | |
162 | { | |
163 | if((ncp < 0) || !(boost::math::isfinite)(ncp)) | |
164 | { // Assume scale == 0 is NOT valid for any distribution. | |
165 | *result = policies::raise_domain_error<RealType>( | |
166 | function, | |
167 | "Non centrality parameter is %1%, but must be > 0 !", ncp, pol); | |
168 | return false; | |
169 | } | |
170 | return true; | |
171 | } | |
172 | ||
173 | template <class RealType, class Policy> | |
174 | inline bool check_finite( | |
175 | const char* function, | |
176 | RealType x, | |
177 | RealType* result, | |
178 | const Policy& pol) | |
179 | { | |
180 | if(!(boost::math::isfinite)(x)) | |
181 | { // Assume scale == 0 is NOT valid for any distribution. | |
182 | *result = policies::raise_domain_error<RealType>( | |
183 | function, | |
184 | "Parameter is %1%, but must be finite !", x, pol); | |
185 | return false; | |
186 | } | |
187 | return true; | |
188 | } | |
189 | ||
190 | } // namespace detail | |
191 | } // namespace math | |
192 | } // namespace boost | |
193 | ||
194 | #endif // BOOST_MATH_DISTRIBUTIONS_COMMON_ERROR_HANDLING_HPP |