Imported Upstream version 1.72.0
[platform/upstream/boost.git] / libs / math / test / test_ibeta_inv_ab.hpp
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/tools/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>
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>
95 void test_inverses2(const T& data, const char* type_name, const char* test_name)
96 {
97 #if !(defined(ERROR_REPORTING_MODE) && !defined(IBETA_INVA_FUNCTION_TO_TEST))
98    //typedef typename T::value_type row_type;
99    typedef Real                   value_type;
100
101    typedef value_type (*pg)(value_type, value_type, value_type);
102 #ifdef IBETA_INVA_FUNCTION_TO_TEST
103    pg funcp = IBETA_INVA_FUNCTION_TO_TEST;
104 #elif defined(BOOST_MATH_NO_DEDUCED_FUNCTION_POINTERS)
105    pg funcp = boost::math::ibeta_inva<value_type, value_type, value_type>;
106 #else
107    pg funcp = boost::math::ibeta_inva;
108 #endif
109
110    boost::math::tools::test_result<value_type> result;
111
112    std::cout << "Testing " << test_name << " with type " << type_name
113       << "\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
114
115    //
116    // test ibeta_inva(T, T, T) against data:
117    //
118    result = boost::math::tools::test_hetero<Real>(
119       data,
120       bind_func<Real>(funcp, 0, 1, 2),
121       extract_result<Real>(3));
122    handle_test_result(result, data[result.worst()], result.worst(), type_name, "ibeta_inva", test_name);
123    //
124    // test ibetac_inva(T, T, T) against data:
125    //
126 #ifdef IBETAC_INVA_FUNCTION_TO_TEST
127    funcp = IBETAC_INVA_FUNCTION_TO_TEST;
128 #elif defined(BOOST_MATH_NO_DEDUCED_FUNCTION_POINTERS)
129    funcp = boost::math::ibetac_inva<value_type, value_type, value_type>;
130 #else
131    funcp = boost::math::ibetac_inva;
132 #endif
133    result = boost::math::tools::test_hetero<Real>(
134       data,
135       bind_func<Real>(funcp, 0, 1, 2),
136       extract_result<Real>(4));
137    handle_test_result(result, data[result.worst()], result.worst(), type_name, "ibetac_inva", test_name);
138    //
139    // test ibeta_invb(T, T, T) against data:
140    //
141 #ifdef IBETA_INVB_FUNCTION_TO_TEST
142    funcp = IBETA_INVB_FUNCTION_TO_TEST;
143 #elif defined(BOOST_MATH_NO_DEDUCED_FUNCTION_POINTERS)
144    funcp = boost::math::ibeta_invb<value_type, value_type, value_type>;
145 #else
146    funcp = boost::math::ibeta_invb;
147 #endif
148    result = boost::math::tools::test_hetero<Real>(
149       data,
150       bind_func<Real>(funcp, 0, 1, 2),
151       extract_result<Real>(5));
152    handle_test_result(result, data[result.worst()], result.worst(), type_name, "ibeta_invb", test_name);
153    //
154    // test ibetac_invb(T, T, T) against data:
155    //
156 #ifdef IBETAC_INVB_FUNCTION_TO_TEST
157    funcp = IBETAC_INVB_FUNCTION_TO_TEST;
158 #elif defined(BOOST_MATH_NO_DEDUCED_FUNCTION_POINTERS)
159    funcp = boost::math::ibetac_invb<value_type, value_type, value_type>;
160 #else
161    funcp = boost::math::ibetac_invb;
162 #endif
163    result = boost::math::tools::test_hetero<Real>(
164       data,
165       bind_func<Real>(funcp, 0, 1, 2),
166       extract_result<Real>(6));
167    handle_test_result(result, data[result.worst()], result.worst(), type_name, "ibetac_invb", test_name);
168 #endif
169 }
170
171 template <class T>
172 void test_beta(T, const char* name)
173 {
174 #if !defined(ERROR_REPORTING_MODE)
175    //
176    // The actual test data is rather verbose, so it's in a separate file
177    //
178    // The contents are as follows, each row of data contains
179    // five items, input value a, input value b, integration limits x, beta(a, b, x) and ibeta(a, b, x):
180    //
181    std::cout << "Running sanity checks for type " << name << std::endl;
182
183 #if !defined(TEST_DATA) || (TEST_DATA == 1)
184 #  include "ibeta_small_data.ipp"
185
186    test_inverses<T>(ibeta_small_data);
187 #endif
188
189 #if !defined(TEST_DATA) || (TEST_DATA == 2)
190 #  include "ibeta_data.ipp"
191
192    test_inverses<T>(ibeta_data);
193 #endif
194
195 #if !defined(TEST_DATA) || (TEST_DATA == 3)
196 #  include "ibeta_large_data.ipp"
197
198    test_inverses<T>(ibeta_large_data);
199 #endif
200 #endif
201
202 #if !defined(TEST_REAL_CONCEPT) || defined(FULL_TEST) || (TEST_DATA == 4)
203    if(boost::is_floating_point<T>::value){
204    //
205    // This accuracy test is normally only enabled for "real"
206    // floating point types and not for class real_concept.
207    // The reason is that these tests are exceptionally slow
208    // to complete when T doesn't have Lanczos support defined for it.
209    //
210 #  include "ibeta_inva_data.ipp"
211
212    test_inverses2<T>(ibeta_inva_data, name, "Inverse incomplete beta");
213    }
214 #endif
215 }
216