1 // Copyright John Maddock 2006.
2 // Copyright Paul A. Bristow 2007, 2009
3 // Use, modification and distribution are subject to the
4 // Boost Software License, Version 1.0. (See accompanying file
5 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6
7 #define BOOST_MATH_OVERFLOW_ERROR_POLICY ignore_error
8
9 #include <boost/math/concepts/real_concept.hpp>
10 #define BOOST_TEST_MAIN
11 #include <boost/test/unit_test.hpp>
12 #include <boost/test/floating_point_comparison.hpp>
13 #include <boost/math/special_functions/math_fwd.hpp>
14 #include <boost/math/tools/stats.hpp>
15 #include <boost/math/tools/test.hpp>
16 #include <boost/math/constants/constants.hpp>
17 #include <boost/type_traits/is_floating_point.hpp>
18 #include <boost/array.hpp>
19 #include "functor.hpp"
20
21 #ifdef TEST_GSL
22 #include <gsl/gsl_errno.h>
23 #include <gsl/gsl_message.h>
24 #endif
25
26 #include "handle_test_result.hpp"
27 #include "table_type.hpp"
28
29 #ifndef SC_
30 #define SC_(x) static_cast<typename table_type<T>::type>(BOOST_JOIN(x, L))
31 #endif
32
33 template <class Real, class T>
test_inverses(const T & data)34 void test_inverses(const T& data)
35 {
36 using namespace std;
37 typedef typename T::value_type row_type;
38 typedef Real value_type;
39
40 value_type precision = static_cast<value_type>(ldexp(1.0, 1-boost::math::policies::digits<value_type, boost::math::policies::policy<> >()/2)) * 100;
41 if(boost::math::policies::digits<value_type, boost::math::policies::policy<> >() < 50)
42 precision = 1; // 1% or two decimal digits, all we can hope for when the input is truncated
43
44 for(unsigned i = 0; i < data.size(); ++i)
45 {
46 //
47 // These inverse tests are thrown off if the output of the
48 // incomplete beta is too close to 1: basically there is insuffient
49 // information left in the value we're using as input to the inverse
50 // to be able to get back to the original value.
51 //
52 if(Real(data[i][5]) == 0)
53 {
54 BOOST_CHECK_EQUAL(boost::math::ibeta_inva(Real(data[i][1]), Real(data[i][2]), Real(data[i][5])), std::numeric_limits<value_type>::has_infinity ? std::numeric_limits<value_type>::infinity() : boost::math::tools::max_value<value_type>());
55 BOOST_CHECK_EQUAL(boost::math::ibeta_invb(Real(data[i][0]), Real(data[i][2]), Real(data[i][5])), boost::math::tools::min_value<value_type>());
56 }
57 else if((1 - Real(data[i][5]) > 0.001)
58 && (fabs(Real(data[i][5])) > 2 * boost::math::tools::min_value<value_type>())
59 && (fabs(Real(data[i][5])) > 2 * boost::math::tools::min_value<double>()))
60 {
61 value_type inv = boost::math::ibeta_inva(Real(data[i][1]), Real(data[i][2]), Real(data[i][5]));
62 BOOST_CHECK_CLOSE(Real(data[i][0]), inv, precision);
63 inv = boost::math::ibeta_invb(Real(data[i][0]), Real(data[i][2]), Real(data[i][5]));
64 BOOST_CHECK_CLOSE(Real(data[i][1]), inv, precision);
65 }
66 else if(1 == Real(data[i][5]))
67 {
68 BOOST_CHECK_EQUAL(boost::math::ibeta_inva(Real(data[i][1]), Real(data[i][2]), Real(data[i][5])), boost::math::tools::min_value<value_type>());
69 BOOST_CHECK_EQUAL(boost::math::ibeta_invb(Real(data[i][0]), Real(data[i][2]), Real(data[i][5])), std::numeric_limits<value_type>::has_infinity ? std::numeric_limits<value_type>::infinity() : boost::math::tools::max_value<value_type>());
70 }
71
72 if(Real(data[i][6]) == 0)
73 {
74 BOOST_CHECK_EQUAL(boost::math::ibetac_inva(Real(data[i][1]), Real(data[i][2]), Real(data[i][6])), boost::math::tools::min_value<value_type>());
75 BOOST_CHECK_EQUAL(boost::math::ibetac_invb(Real(data[i][0]), Real(data[i][2]), Real(data[i][6])), std::numeric_limits<value_type>::has_infinity ? std::numeric_limits<value_type>::infinity() : boost::math::tools::max_value<value_type>());
76 }
77 else if((1 - Real(data[i][6]) > 0.001)
78 && (fabs(Real(data[i][6])) > 2 * boost::math::tools::min_value<value_type>())
79 && (fabs(Real(data[i][6])) > 2 * boost::math::tools::min_value<double>()))
80 {
81 value_type inv = boost::math::ibetac_inva(Real(data[i][1]), Real(data[i][2]), Real(data[i][6]));
82 BOOST_CHECK_CLOSE(Real(data[i][0]), inv, precision);
83 inv = boost::math::ibetac_invb(Real(data[i][0]), Real(data[i][2]), Real(data[i][6]));
84 BOOST_CHECK_CLOSE(Real(data[i][1]), inv, precision);
85 }
86 else if(Real(data[i][6]) == 1)
87 {
88 BOOST_CHECK_EQUAL(boost::math::ibetac_inva(Real(data[i][1]), Real(data[i][2]), Real(data[i][6])), std::numeric_limits<value_type>::has_infinity ? std::numeric_limits<value_type>::infinity() : boost::math::tools::max_value<value_type>());
89 BOOST_CHECK_EQUAL(boost::math::ibetac_invb(Real(data[i][0]), Real(data[i][2]), Real(data[i][6])), boost::math::tools::min_value<value_type>());
90 }
91 }
92 }
93
94 template <class Real, class T>
test_inverses2(const T & data,const char * type_name,const char * test_name)95 void test_inverses2(const T& data, const char* type_name, const char* test_name)
96 {
97 typedef typename T::value_type row_type;
98 typedef Real value_type;
99
100 typedef value_type (*pg)(value_type, value_type, value_type);
101 #if defined(BOOST_MATH_NO_DEDUCED_FUNCTION_POINTERS)
102 pg funcp = boost::math::ibeta_inva<value_type, value_type, value_type>;
103 #else
104 pg funcp = boost::math::ibeta_inva;
105 #endif
106
107 boost::math::tools::test_result<value_type> result;
108
109 std::cout << "Testing " << test_name << " with type " << type_name
110 << "\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
111
112 //
113 // test ibeta_inva(T, T, T) against data:
114 //
115 result = boost::math::tools::test_hetero<Real>(
116 data,
117 bind_func<Real>(funcp, 0, 1, 2),
118 extract_result<Real>(3));
119 handle_test_result(result, data[result.worst()], result.worst(), type_name, "boost::math::ibeta_inva", test_name);
120 //
121 // test ibetac_inva(T, T, T) against data:
122 //
123 #if defined(BOOST_MATH_NO_DEDUCED_FUNCTION_POINTERS)
124 funcp = boost::math::ibetac_inva<value_type, value_type, value_type>;
125 #else
126 funcp = boost::math::ibetac_inva;
127 #endif
128 result = boost::math::tools::test_hetero<Real>(
129 data,
130 bind_func<Real>(funcp, 0, 1, 2),
131 extract_result<Real>(4));
132 handle_test_result(result, data[result.worst()], result.worst(), type_name, "boost::math::ibetac_inva", test_name);
133 //
134 // test ibeta_invb(T, T, T) against data:
135 //
136 #if defined(BOOST_MATH_NO_DEDUCED_FUNCTION_POINTERS)
137 funcp = boost::math::ibeta_invb<value_type, value_type, value_type>;
138 #else
139 funcp = boost::math::ibeta_invb;
140 #endif
141 result = boost::math::tools::test_hetero<Real>(
142 data,
143 bind_func<Real>(funcp, 0, 1, 2),
144 extract_result<Real>(5));
145 handle_test_result(result, data[result.worst()], result.worst(), type_name, "boost::math::ibeta_invb", test_name);
146 //
147 // test ibetac_invb(T, T, T) against data:
148 //
149 #if defined(BOOST_MATH_NO_DEDUCED_FUNCTION_POINTERS)
150 funcp = boost::math::ibetac_invb<value_type, value_type, value_type>;
151 #else
152 funcp = boost::math::ibetac_invb;
153 #endif
154 result = boost::math::tools::test_hetero<Real>(
155 data,
156 bind_func<Real>(funcp, 0, 1, 2),
157 extract_result<Real>(6));
158 handle_test_result(result, data[result.worst()], result.worst(), type_name, "boost::math::ibetac_invb", test_name);
159 }
160
161 template <class T>
test_beta(T,const char * name)162 void test_beta(T, const char* name)
163 {
164 //
165 // The actual test data is rather verbose, so it's in a separate file
166 //
167 // The contents are as follows, each row of data contains
168 // five items, input value a, input value b, integration limits x, beta(a, b, x) and ibeta(a, b, x):
169 //
170 std::cout << "Running sanity checks for type " << name << std::endl;
171
172 #if !defined(TEST_DATA) || (TEST_DATA == 1)
173 # include "ibeta_small_data.ipp"
174
175 test_inverses<T>(ibeta_small_data);
176 #endif
177
178 #if !defined(TEST_DATA) || (TEST_DATA == 2)
179 # include "ibeta_data.ipp"
180
181 test_inverses<T>(ibeta_data);
182 #endif
183
184 #if !defined(TEST_DATA) || (TEST_DATA == 3)
185 # include "ibeta_large_data.ipp"
186
187 test_inverses<T>(ibeta_large_data);
188 #endif
189
190 #if !defined(TEST_REAL_CONCEPT) || defined(FULL_TEST) || (TEST_DATA == 4)
191 #ifndef FULL_TEST
192 if(boost::is_floating_point<T>::value){
193 #endif
194 //
195 // This accuracy test is normally only enabled for "real"
196 // floating point types and not for class real_concept.
197 // The reason is that these tests are exceptionally slow
198 // to complete when T doesn't have Lanczos support defined for it.
199 //
200 # include "ibeta_inva_data.ipp"
201
202 test_inverses2<T>(ibeta_inva_data, name, "Inverse incomplete beta");
203 #ifndef FULL_TEST
204 }
205 #endif
206 #endif
207 }
208
209