test_ibeta_inv_ab.hpp 8.7 KB

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