test_beta.hpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. #ifdef _MSC_VER
  7. # pragma warning (disable : 4996) // POSIX name for this item is deprecated
  8. # pragma warning (disable : 4224) // nonstandard extension used : formal parameter 'arg' was previously defined as a type
  9. # pragma warning (disable : 4180) // qualifier applied to function type has no meaning; ignored
  10. #endif
  11. #include <boost/math/concepts/real_concept.hpp>
  12. #define BOOST_TEST_MAIN
  13. #include <boost/test/unit_test.hpp>
  14. #include <boost/test/tools/floating_point_comparison.hpp>
  15. #include <boost/math/special_functions/math_fwd.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 "handle_test_result.hpp"
  23. #include "table_type.hpp"
  24. #undef small // VC++ #defines small char !!!!!!
  25. #ifndef SC_
  26. #define SC_(x) static_cast<typename table_type<T>::type>(BOOST_JOIN(x, L))
  27. #endif
  28. template <class Real, class T>
  29. void do_test_beta(const T& data, const char* type_name, const char* test_name)
  30. {
  31. #if !(defined(ERROR_REPORTING_MODE) && !defined(BETA_FUNCTION_TO_TEST))
  32. typedef Real value_type;
  33. typedef value_type (*pg)(value_type, value_type);
  34. #ifdef BETA_FUNCTION_TO_TEST
  35. pg funcp = BETA_FUNCTION_TO_TEST;
  36. #elif defined(BOOST_MATH_NO_DEDUCED_FUNCTION_POINTERS)
  37. pg funcp = boost::math::beta<value_type, value_type>;
  38. #else
  39. pg funcp = boost::math::beta;
  40. #endif
  41. boost::math::tools::test_result<value_type> result;
  42. std::cout << "Testing " << test_name << " with type " << type_name
  43. << "\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
  44. //
  45. // test beta against data:
  46. //
  47. result = boost::math::tools::test_hetero<Real>(
  48. data,
  49. bind_func<Real>(funcp, 0, 1),
  50. extract_result<Real>(2));
  51. handle_test_result(result, data[result.worst()], result.worst(), type_name, "beta", test_name);
  52. std::cout << std::endl;
  53. #endif
  54. }
  55. template <class T>
  56. void test_beta(T, const char* name)
  57. {
  58. //
  59. // The actual test data is rather verbose, so it's in a separate file
  60. //
  61. // The contents are as follows, each row of data contains
  62. // three items, input value a, input value b and beta(a, b):
  63. //
  64. # include "beta_small_data.ipp"
  65. do_test_beta<T>(beta_small_data, name, "Beta Function: Small Values");
  66. # include "beta_med_data.ipp"
  67. do_test_beta<T>(beta_med_data, name, "Beta Function: Medium Values");
  68. # include "beta_exp_data.ipp"
  69. do_test_beta<T>(beta_exp_data, name, "Beta Function: Divergent Values");
  70. }
  71. template <class T>
  72. void test_spots(T)
  73. {
  74. //
  75. // Basic sanity checks, tolerance is 20 epsilon expressed as a percentage:
  76. //
  77. T tolerance = boost::math::tools::epsilon<T>() * 20 * 100;
  78. T small = boost::math::tools::epsilon<T>() / 1024;
  79. BOOST_CHECK_CLOSE(::boost::math::beta(static_cast<T>(1), static_cast<T>(1)), static_cast<T>(1), tolerance);
  80. BOOST_CHECK_CLOSE(::boost::math::beta(static_cast<T>(1), static_cast<T>(4)), static_cast<T>(0.25), tolerance);
  81. BOOST_CHECK_CLOSE(::boost::math::beta(static_cast<T>(4), static_cast<T>(1)), static_cast<T>(0.25), tolerance);
  82. BOOST_CHECK_CLOSE(::boost::math::beta(small, static_cast<T>(4)), 1/small, tolerance);
  83. BOOST_CHECK_CLOSE(::boost::math::beta(static_cast<T>(4), small), 1/small, tolerance);
  84. BOOST_CHECK_CLOSE(::boost::math::beta(static_cast<T>(4), static_cast<T>(20)), static_cast<T>(0.00002823263692828910220214568040654997176736L), tolerance);
  85. BOOST_CHECK_CLOSE(::boost::math::beta(static_cast<T>(0.0125L), static_cast<T>(0.000023L)), static_cast<T>(43558.24045647538375006349016083320744662L), tolerance * 2);
  86. }