/Src/Dependencies/Boost/libs/math/test/test_igamma_inva.cpp

http://hadesmem.googlecode.com/ · C++ · 312 lines · 197 code · 33 blank · 82 comment · 30 complexity · 5ada4a6cac6a1c3294d3c988af474d2e MD5 · raw file

  1. // (C) Copyright John Maddock 2006.
  2. // Use, modification and distribution are subject to the
  3. // Boost Software License, Version 1.0. (See accompanying file
  4. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. #include <pch.hpp>
  6. #define BOOST_MATH_OVERFLOW_ERROR_POLICY ignore_error
  7. #include <boost/math/concepts/real_concept.hpp>
  8. #include <boost/math/special_functions/gamma.hpp>
  9. #include <boost/test/test_exec_monitor.hpp>
  10. #include <boost/test/results_collector.hpp>
  11. #include <boost/test/unit_test.hpp>
  12. #include <boost/test/floating_point_comparison.hpp>
  13. #include <boost/math/tools/stats.hpp>
  14. #include <boost/math/tools/test.hpp>
  15. #include <boost/math/constants/constants.hpp>
  16. #include <boost/type_traits/is_floating_point.hpp>
  17. #include <boost/array.hpp>
  18. #include "functor.hpp"
  19. #include "handle_test_result.hpp"
  20. #if !defined(TEST_FLOAT) && !defined(TEST_DOUBLE) && !defined(TEST_LDOUBLE) && !defined(TEST_REAL_CONCEPT)
  21. # define TEST_FLOAT
  22. # define TEST_DOUBLE
  23. # define TEST_LDOUBLE
  24. # define TEST_REAL_CONCEPT
  25. #endif
  26. //
  27. // DESCRIPTION:
  28. // ~~~~~~~~~~~~
  29. //
  30. // This file tests the incomplete gamma function inverses
  31. // gamma_p_inva and gamma_q_inva. There are two sets of tests:
  32. // 2) TODO: Accuracy tests use values generated with NTL::RR at
  33. // 1000-bit precision and our generic versions of these functions.
  34. // 3) Round trip sanity checks, use the test data for the forward
  35. // functions, and verify that we can get (approximately) back
  36. // where we started.
  37. //
  38. // Note that when this file is first run on a new platform many of
  39. // these tests will fail: the default accuracy is 1 epsilon which
  40. // is too tight for most platforms. In this situation you will
  41. // need to cast a human eye over the error rates reported and make
  42. // a judgement as to whether they are acceptable. Either way please
  43. // report the results to the Boost mailing list. Acceptable rates of
  44. // error are marked up below as a series of regular expressions that
  45. // identify the compiler/stdlib/platform/data-type/test-data/test-function
  46. // along with the maximum expected peek and RMS mean errors for that
  47. // test.
  48. //
  49. void expected_results()
  50. {
  51. //
  52. // Define the max and mean errors expected for
  53. // various compilers and platforms.
  54. //
  55. const char* largest_type;
  56. #ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS
  57. if(boost::math::policies::digits<double, boost::math::policies::policy<> >() == boost::math::policies::digits<long double, boost::math::policies::policy<> >())
  58. {
  59. largest_type = "(long\\s+)?double";
  60. }
  61. else
  62. {
  63. largest_type = "long double";
  64. }
  65. #else
  66. largest_type = "(long\\s+)?double";
  67. #endif
  68. //
  69. // Linux:
  70. //
  71. add_expected_result(
  72. "[^|]*", // compiler
  73. "[^|]*", // stdlib
  74. "linux", // platform
  75. largest_type, // test type(s)
  76. "[^|]*", // test data group
  77. "[^|]*", 800, 200); // test function
  78. //
  79. // Catch all cases come last:
  80. //
  81. add_expected_result(
  82. "[^|]*", // compiler
  83. "[^|]*", // stdlib
  84. "[^|]*", // platform
  85. "real_concept", // test type(s)
  86. "[^|]*", // test data group
  87. "[^|]*", 3000, 1000); // test function
  88. add_expected_result(
  89. "[^|]*", // compiler
  90. "[^|]*", // stdlib
  91. "[^|]*", // platform
  92. largest_type, // test type(s)
  93. "[^|]*", // test data group
  94. "[^|]*", 300, 100); // test function
  95. // this one has to come last in case double *is* the widest
  96. // float type:
  97. add_expected_result(
  98. "[^|]*", // compiler
  99. "[^|]*", // stdlib
  100. "[^|]*", // platform
  101. "float|double", // test type(s)
  102. "[^|]*", // test data group
  103. "[^|]*", 10, 5); // test function
  104. //
  105. // Finish off by printing out the compiler/stdlib/platform names,
  106. // we do this to make it easier to mark up expected error rates.
  107. //
  108. std::cout << "Tests run with " << BOOST_COMPILER << ", "
  109. << BOOST_STDLIB << ", " << BOOST_PLATFORM << std::endl;
  110. }
  111. #define BOOST_CHECK_CLOSE_EX(a, b, prec, i) \
  112. {\
  113. unsigned int failures = boost::unit_test::results_collector.results( boost::unit_test::framework::current_test_case().p_id ).p_assertions_failed;\
  114. BOOST_CHECK_CLOSE(a, b, prec); \
  115. if(failures != boost::unit_test::results_collector.results( boost::unit_test::framework::current_test_case().p_id ).p_assertions_failed)\
  116. {\
  117. std::cerr << "Failure was at row " << i << std::endl;\
  118. std::cerr << std::setprecision(35); \
  119. std::cerr << "{ " << data[i][0] << " , " << data[i][1] << " , " << data[i][2];\
  120. std::cerr << " , " << data[i][3] << " , " << data[i][4] << " , " << data[i][5] << " } " << std::endl;\
  121. }\
  122. }
  123. template <class T>
  124. void do_test_gamma_2(const T& data, const char* type_name, const char* test_name)
  125. {
  126. //
  127. // test gamma_p_inva(T, T) against data:
  128. //
  129. using namespace std;
  130. typedef typename T::value_type row_type;
  131. typedef typename row_type::value_type value_type;
  132. std::cout << test_name << " with type " << type_name << std::endl;
  133. //
  134. // These sanity checks test for a round trip accuracy of one half
  135. // of the bits in T, unless T is type float, in which case we check
  136. // for just one decimal digit. The problem here is the sensitivity
  137. // of the functions, not their accuracy. This test data was generated
  138. // for the forward functions, which means that when it is used as
  139. // the input to the inverses then it is necessarily inexact. This rounding
  140. // of the input is what makes the data unsuitable for use as an accuracy check,
  141. // and also demonstrates that you can't in general round-trip these functions.
  142. // It is however a useful sanity check.
  143. //
  144. value_type precision = static_cast<value_type>(ldexp(1.0, 1-boost::math::policies::digits<value_type, boost::math::policies::policy<> >()/2)) * 100;
  145. if(boost::math::policies::digits<value_type, boost::math::policies::policy<> >() < 50)
  146. precision = 1; // 1% or two decimal digits, all we can hope for when the input is truncated to float
  147. for(unsigned i = 0; i < data.size(); ++i)
  148. {
  149. //
  150. // These inverse tests are thrown off if the output of the
  151. // incomplete gamma is too close to 1: basically there is insuffient
  152. // information left in the value we're using as input to the inverse
  153. // to be able to get back to the original value.
  154. //
  155. if(data[i][5] == 0)
  156. BOOST_CHECK_EQUAL(boost::math::gamma_p_inva(data[i][1], data[i][5]), boost::math::tools::max_value<value_type>());
  157. else if((1 - data[i][5] > 0.001) && (fabs(data[i][5]) > 2 * boost::math::tools::min_value<value_type>()))
  158. {
  159. value_type inv = boost::math::gamma_p_inva(data[i][1], data[i][5]);
  160. BOOST_CHECK_CLOSE_EX(data[i][0], inv, precision, i);
  161. }
  162. else if(1 == data[i][5])
  163. BOOST_CHECK_EQUAL(boost::math::gamma_p_inva(data[i][1], data[i][5]), boost::math::tools::min_value<value_type>());
  164. else if(data[i][5] > 2 * boost::math::tools::min_value<value_type>())
  165. {
  166. // not enough bits in our input to get back to x, but we should be in
  167. // the same ball park:
  168. value_type inv = boost::math::gamma_p_inva(data[i][1], data[i][5]);
  169. BOOST_CHECK_CLOSE_EX(data[i][0], inv, 100, i);
  170. }
  171. if(data[i][3] == 0)
  172. BOOST_CHECK_EQUAL(boost::math::gamma_q_inva(data[i][1], data[i][3]), boost::math::tools::min_value<value_type>());
  173. else if((1 - data[i][3] > 0.001)
  174. && (fabs(data[i][3]) > 2 * boost::math::tools::min_value<value_type>())
  175. && (fabs(data[i][3]) > 2 * boost::math::tools::min_value<double>()))
  176. {
  177. value_type inv = boost::math::gamma_q_inva(data[i][1], data[i][3]);
  178. BOOST_CHECK_CLOSE_EX(data[i][0], inv, precision, i);
  179. }
  180. else if(1 == data[i][3])
  181. BOOST_CHECK_EQUAL(boost::math::gamma_q_inva(data[i][1], data[i][3]), boost::math::tools::max_value<value_type>());
  182. else if(data[i][3] > 2 * boost::math::tools::min_value<value_type>())
  183. {
  184. // not enough bits in our input to get back to x, but we should be in
  185. // the same ball park:
  186. value_type inv = boost::math::gamma_q_inva(data[i][1], data[i][3]);
  187. BOOST_CHECK_CLOSE_EX(data[i][0], inv, 100, i);
  188. }
  189. }
  190. std::cout << std::endl;
  191. }
  192. template <class T>
  193. void do_test_gamma_inva(const T& data, const char* type_name, const char* test_name)
  194. {
  195. typedef typename T::value_type row_type;
  196. typedef typename row_type::value_type value_type;
  197. typedef value_type (*pg)(value_type, value_type);
  198. #if defined(BOOST_MATH_NO_DEDUCED_FUNCTION_POINTERS)
  199. pg funcp = boost::math::gamma_p_inva<value_type, value_type>;
  200. #else
  201. pg funcp = boost::math::gamma_p_inva;
  202. #endif
  203. boost::math::tools::test_result<value_type> result;
  204. std::cout << "Testing " << test_name << " with type " << type_name
  205. << "\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
  206. //
  207. // test gamma_p_inva(T, T) against data:
  208. //
  209. result = boost::math::tools::test(
  210. data,
  211. bind_func(funcp, 0, 1),
  212. extract_result(2));
  213. handle_test_result(result, data[result.worst()], result.worst(), type_name, "boost::math::gamma_p_inva", test_name);
  214. //
  215. // test gamma_q_inva(T, T) against data:
  216. //
  217. #if defined(BOOST_MATH_NO_DEDUCED_FUNCTION_POINTERS)
  218. funcp = boost::math::gamma_q_inva<value_type, value_type>;
  219. #else
  220. funcp = boost::math::gamma_q_inva;
  221. #endif
  222. result = boost::math::tools::test(
  223. data,
  224. bind_func(funcp, 0, 1),
  225. extract_result(3));
  226. handle_test_result(result, data[result.worst()], result.worst(), type_name, "boost::math::gamma_q_inva", test_name);
  227. }
  228. template <class T>
  229. void test_gamma(T, const char* name)
  230. {
  231. //
  232. // The actual test data is rather verbose, so it's in a separate file
  233. //
  234. // First the data for the incomplete gamma function, each
  235. // row has the following 6 entries:
  236. // Parameter a, parameter z,
  237. // Expected tgamma(a, z), Expected gamma_q(a, z)
  238. // Expected tgamma_lower(a, z), Expected gamma_p(a, z)
  239. //
  240. # include "igamma_med_data.ipp"
  241. do_test_gamma_2(igamma_med_data, name, "Running round trip sanity checks on incomplete gamma medium sized values");
  242. # include "igamma_small_data.ipp"
  243. do_test_gamma_2(igamma_small_data, name, "Running round trip sanity checks on incomplete gamma small values");
  244. # include "igamma_big_data.ipp"
  245. do_test_gamma_2(igamma_big_data, name, "Running round trip sanity checks on incomplete gamma large values");
  246. # include "igamma_inva_data.ipp"
  247. do_test_gamma_inva(igamma_inva_data, name, "Incomplete gamma inverses.");
  248. }
  249. int test_main(int, char* [])
  250. {
  251. expected_results();
  252. BOOST_MATH_CONTROL_FP;
  253. #ifndef BOOST_MATH_BUGGY_LARGE_FLOAT_CONSTANTS
  254. #ifdef TEST_FLOAT
  255. test_gamma(0.1F, "float");
  256. #endif
  257. #endif
  258. #ifdef TEST_DOUBLE
  259. test_gamma(0.1, "double");
  260. #endif
  261. #ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS
  262. #ifdef TEST_LDOUBLE
  263. test_gamma(0.1L, "long double");
  264. #endif
  265. #ifndef BOOST_MATH_NO_REAL_CONCEPT_TESTS
  266. #if !BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x582))
  267. #ifdef TEST_REAL_CONCEPT
  268. test_gamma(boost::math::concepts::real_concept(0.1), "real_concept");
  269. #endif
  270. #endif
  271. #endif
  272. #else
  273. std::cout << "<note>The long double tests have been disabled on this platform "
  274. "either because the long double overloads of the usual math functions are "
  275. "not available at all, or because they are too inaccurate for these tests "
  276. "to pass.</note>" << std::cout;
  277. #endif
  278. return 0;
  279. }