Imported Upstream version 1.72.0
[platform/upstream/boost.git] / libs / math / test / test_igamma_inva.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 #include <boost/math/special_functions/math_fwd.hpp>
11 #define BOOST_TEST_MAIN
12 #include <boost/test/unit_test.hpp>
13 #include <boost/test/results_collector.hpp>
14 #include <boost/test/unit_test.hpp>
15 #include <boost/test/tools/floating_point_comparison.hpp>
16 #include <boost/math/tools/stats.hpp>
17 #include <boost/math/tools/test.hpp>
18 #include <boost/math/constants/constants.hpp>
19 #include <boost/type_traits/is_floating_point.hpp>
20 #include <boost/array.hpp>
21 #include "functor.hpp"
22 #include "table_type.hpp"
23 #include "handle_test_result.hpp"
24
25 #ifndef SC_
26 #define SC_(x) static_cast<typename table_type<T>::type>(BOOST_JOIN(x, L))
27 #endif
28
29 #define BOOST_CHECK_CLOSE_EX(a, b, prec, i) \
30    {\
31       unsigned int failures = boost::unit_test::results_collector.results( boost::unit_test::framework::current_test_case().p_id ).p_assertions_failed;\
32       BOOST_CHECK_CLOSE(a, b, prec); \
33       if(failures != boost::unit_test::results_collector.results( boost::unit_test::framework::current_test_case().p_id ).p_assertions_failed)\
34       {\
35          std::cerr << "Failure was at row " << i << std::endl;\
36          std::cerr << std::setprecision(35); \
37          std::cerr << "{ " << data[i][0] << " , " << data[i][1] << " , " << data[i][2];\
38          std::cerr << " , " << data[i][3] << " , " << data[i][4] << " , " << data[i][5] << " } " << std::endl;\
39       }\
40    }
41
42 template <class Real, class T>
43 void do_test_gamma_2(const T& data, const char* type_name, const char* test_name)
44 {
45    //
46    // test gamma_p_inva(T, T) against data:
47    //
48    using namespace std;
49    typedef Real                   value_type;
50
51    std::cout << test_name << " with type " << type_name << std::endl;
52
53    //
54    // These sanity checks test for a round trip accuracy of one half
55    // of the bits in T, unless T is type float, in which case we check
56    // for just one decimal digit.  The problem here is the sensitivity
57    // of the functions, not their accuracy.  This test data was generated
58    // for the forward functions, which means that when it is used as
59    // the input to the inverses then it is necessarily inexact.  This rounding
60    // of the input is what makes the data unsuitable for use as an accuracy check,
61    // and also demonstrates that you can't in general round-trip these functions.
62    // It is however a useful sanity check.
63    //
64    value_type precision = static_cast<value_type>(ldexp(1.0, 1-boost::math::policies::digits<value_type, boost::math::policies::policy<> >()/2)) * 100;
65    if(boost::math::policies::digits<value_type, boost::math::policies::policy<> >() < 50)
66       precision = 1;   // 1% or two decimal digits, all we can hope for when the input is truncated to float
67
68    for(unsigned i = 0; i < data.size(); ++i)
69    {
70       //
71       // These inverse tests are thrown off if the output of the
72       // incomplete gamma is too close to 1: basically there is insuffient
73       // information left in the value we're using as input to the inverse
74       // to be able to get back to the original value.
75       //
76       if(Real(data[i][5]) == 0)
77          BOOST_CHECK_EQUAL(boost::math::gamma_p_inva(Real(data[i][1]), Real(data[i][5])), std::numeric_limits<value_type>::has_infinity ? std::numeric_limits<value_type>::infinity() : boost::math::tools::max_value<value_type>());
78       else if((1 - Real(data[i][5]) > 0.001) && (fabs(Real(data[i][5])) > 2 * boost::math::tools::min_value<value_type>()))
79       {
80          value_type inv = boost::math::gamma_p_inva(Real(data[i][1]), Real(data[i][5]));
81          BOOST_CHECK_CLOSE_EX(Real(data[i][0]), inv, precision, i);
82       }
83       else if(1 == Real(data[i][5]))
84          BOOST_CHECK_EQUAL(boost::math::gamma_p_inva(Real(data[i][1]), Real(data[i][5])), boost::math::tools::min_value<value_type>());
85       else if(Real(data[i][5]) > 2 * boost::math::tools::min_value<value_type>())
86       {
87          // not enough bits in our input to get back to x, but we should be in
88          // the same ball park:
89          value_type inv = boost::math::gamma_p_inva(Real(data[i][1]), Real(data[i][5]));
90          BOOST_CHECK_CLOSE_EX(Real(data[i][0]), inv, 100, i);
91       }
92
93       if(Real(data[i][3]) == 0)
94          BOOST_CHECK_EQUAL(boost::math::gamma_q_inva(Real(data[i][1]), Real(data[i][3])), boost::math::tools::min_value<value_type>());
95       else if((1 - Real(data[i][3]) > 0.001) 
96          && (fabs(Real(data[i][3])) > 2 * boost::math::tools::min_value<value_type>()) 
97          && (fabs(Real(data[i][3])) > 2 * boost::math::tools::min_value<double>()))
98       {
99          value_type inv = boost::math::gamma_q_inva(Real(data[i][1]), Real(data[i][3]));
100          BOOST_CHECK_CLOSE_EX(Real(data[i][0]), inv, precision, i);
101       }
102       else if(1 == Real(data[i][3]))
103          BOOST_CHECK_EQUAL(boost::math::gamma_q_inva(Real(data[i][1]), Real(data[i][3])), std::numeric_limits<value_type>::has_infinity ? std::numeric_limits<value_type>::infinity() : boost::math::tools::max_value<value_type>());
104       else if(Real(data[i][3]) > 2 * boost::math::tools::min_value<value_type>()) 
105       {
106          // not enough bits in our input to get back to x, but we should be in
107          // the same ball park:
108          value_type inv = boost::math::gamma_q_inva(Real(data[i][1]), Real(data[i][3]));
109          BOOST_CHECK_CLOSE_EX(Real(data[i][0]), inv, 100, i);
110       }
111    }
112    std::cout << std::endl;
113 }
114
115 template <class Real, class T>
116 void do_test_gamma_inva(const T& data, const char* type_name, const char* test_name)
117 {
118 #if !(defined(ERROR_REPORTING_MODE) && !defined(GAMMAP_INVA_FUNCTION_TO_TEST))
119    typedef Real                   value_type;
120
121    typedef value_type (*pg)(value_type, value_type);
122 #ifdef GAMMAP_INVA_FUNCTION_TO_TEST
123    pg funcp = GAMMAP_INVA_FUNCTION_TO_TEST;
124 #elif defined(BOOST_MATH_NO_DEDUCED_FUNCTION_POINTERS)
125    pg funcp = boost::math::gamma_p_inva<value_type, value_type>;
126 #else
127    pg funcp = boost::math::gamma_p_inva;
128 #endif
129
130    boost::math::tools::test_result<value_type> result;
131
132    std::cout << "Testing " << test_name << " with type " << type_name
133       << "\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
134
135    //
136    // test gamma_p_inva(T, T) against data:
137    //
138    result = boost::math::tools::test_hetero<Real>(
139       data,
140       bind_func<Real>(funcp, 0, 1),
141       extract_result<Real>(2));
142    handle_test_result(result, data[result.worst()], result.worst(), type_name, "gamma_p_inva", test_name);
143    //
144    // test gamma_q_inva(T, T) against data:
145    //
146 #ifdef GAMMAQ_INVA_FUNCTION_TO_TEST
147    funcp = GAMMAQ_INVA_FUNCTION_TO_TEST;
148 #elif defined(BOOST_MATH_NO_DEDUCED_FUNCTION_POINTERS)
149    funcp = boost::math::gamma_q_inva<value_type, value_type>;
150 #else
151    funcp = boost::math::gamma_q_inva;
152 #endif
153    result = boost::math::tools::test_hetero<Real>(
154       data,
155       bind_func<Real>(funcp, 0, 1),
156       extract_result<Real>(3));
157    handle_test_result(result, data[result.worst()], result.worst(), type_name, "gamma_q_inva", test_name);
158 #endif
159 }
160
161 template <class T>
162 void test_gamma(T, const char* name)
163 {
164 #if !defined(TEST_UDT) && !defined(ERROR_REPORTING_MODE)
165    //
166    // The actual test data is rather verbose, so it's in a separate file
167    //
168    // First the data for the incomplete gamma function, each
169    // row has the following 6 entries:
170    // Parameter a, parameter z,
171    // Expected tgamma(a, z), Expected gamma_q(a, z)
172    // Expected tgamma_lower(a, z), Expected gamma_p(a, z)
173    //
174 #  include "igamma_med_data.ipp"
175
176    do_test_gamma_2<T>(igamma_med_data, name, "Running round trip sanity checks on incomplete gamma medium sized values");
177
178 #  include "igamma_small_data.ipp"
179
180    do_test_gamma_2<T>(igamma_small_data, name, "Running round trip sanity checks on incomplete gamma small values");
181
182 #  include "igamma_big_data.ipp"
183
184    do_test_gamma_2<T>(igamma_big_data, name, "Running round trip sanity checks on incomplete gamma large values");
185
186 #endif
187
188 #  include "igamma_inva_data.ipp"
189
190    do_test_gamma_inva<T>(igamma_inva_data, name, "Incomplete gamma inverses.");
191 }
192