test_igamma_inva.hpp 8.1 KB

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