test_digamma.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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_light.hpp>
  6. #include "test_digamma.hpp"
  7. //
  8. // DESCRIPTION:
  9. // ~~~~~~~~~~~~
  10. //
  11. // This file tests the digamma function. There are two sets of tests, spot
  12. // tests which compare our results with selected values computed
  13. // using the online special function calculator at
  14. // functions.wolfram.com, while the bulk of the accuracy tests
  15. // use values generated with NTL::RR at 1000-bit precision
  16. // and our generic versions of these functions.
  17. //
  18. // Note that when this file is first run on a new platform many of
  19. // these tests will fail: the default accuracy is 1 epsilon which
  20. // is too tight for most platforms. In this situation you will
  21. // need to cast a human eye over the error rates reported and make
  22. // a judgement as to whether they are acceptable. Either way please
  23. // report the results to the Boost mailing list. Acceptable rates of
  24. // error are marked up below as a series of regular expressions that
  25. // identify the compiler/stdlib/platform/data-type/test-data/test-function
  26. // along with the maximum expected peek and RMS mean errors for that
  27. // test.
  28. //
  29. void expected_results()
  30. {
  31. //
  32. // Define the max and mean errors expected for
  33. // various compilers and platforms.
  34. //
  35. add_expected_result(
  36. ".*", // compiler
  37. ".*", // stdlib
  38. ".*", // platform
  39. ".*", // test type(s)
  40. ".*Negative.*", // test data group
  41. ".*", 300, 40); // test function
  42. add_expected_result(
  43. ".*", // compiler
  44. ".*", // stdlib
  45. ".*", // platform
  46. "real_concept", // test type(s)
  47. ".*Near the Positive Root.*", // test data group
  48. ".*", 25000, 3000); // test function
  49. add_expected_result(
  50. ".*", // compiler
  51. ".*", // stdlib
  52. ".*", // platform
  53. "real_concept", // test type(s)
  54. ".*Half.*", // test data group
  55. ".*", 15, 10); // test function
  56. add_expected_result(
  57. ".*", // compiler
  58. ".*", // stdlib
  59. ".*", // platform
  60. ".*", // test type(s)
  61. ".*", // test data group
  62. ".*", 3, 3); // test function
  63. //
  64. // Finish off by printing out the compiler/stdlib/platform names,
  65. // we do this to make it easier to mark up expected error rates.
  66. //
  67. std::cout << "Tests run with " << BOOST_COMPILER << ", "
  68. << BOOST_STDLIB << ", " << BOOST_PLATFORM << std::endl;
  69. }
  70. template <class T>
  71. void test_spots(T, const char* t)
  72. {
  73. std::cout << "Testing basic sanity checks for type " << t << std::endl;
  74. //
  75. // Basic sanity checks, tolerance is 3 epsilon expressed as a percentage:
  76. //
  77. T tolerance = boost::math::tools::epsilon<T>() * 300;
  78. //
  79. // Special tolerance (200eps) for when we're very near the root,
  80. // and T has more than 64-bits in it's mantissa:
  81. //
  82. BOOST_CHECK_CLOSE(::boost::math::digamma(static_cast<T>(0.125)), static_cast<T>(-8.3884926632958548678027429230863430000514460424495L), tolerance);
  83. BOOST_CHECK_CLOSE(::boost::math::digamma(static_cast<T>(0.5)), static_cast<T>(-1.9635100260214234794409763329987555671931596046604L), tolerance);
  84. BOOST_CHECK_CLOSE(::boost::math::digamma(static_cast<T>(1)), static_cast<T>(-0.57721566490153286060651209008240243104215933593992L), tolerance);
  85. BOOST_CHECK_CLOSE(::boost::math::digamma(static_cast<T>(1.5)), static_cast<T>(0.036489973978576520559023667001244432806840395339566L), tolerance * 40);
  86. BOOST_CHECK_CLOSE(::boost::math::digamma(static_cast<T>(1.5) - static_cast<T>(1)/32), static_cast<T>(0.00686541147073577672813890866512415766586241385896200579891429L), tolerance * 100);
  87. BOOST_CHECK_CLOSE(::boost::math::digamma(static_cast<T>(2)), static_cast<T>(0.42278433509846713939348790991759756895784066406008L), tolerance);
  88. BOOST_CHECK_CLOSE(::boost::math::digamma(static_cast<T>(8)), static_cast<T>(2.0156414779556099965363450527747404261006978069172L), tolerance);
  89. BOOST_CHECK_CLOSE(::boost::math::digamma(static_cast<T>(12)), static_cast<T>(2.4426616799758120167383652547949424463027180089374L), tolerance);
  90. BOOST_CHECK_CLOSE(::boost::math::digamma(static_cast<T>(22)), static_cast<T>(3.0681430398611966699248760264450329818421699570581L), tolerance);
  91. BOOST_CHECK_CLOSE(::boost::math::digamma(static_cast<T>(50)), static_cast<T>(3.9019896734278921969539597028823666609284424880275L), tolerance);
  92. BOOST_CHECK_CLOSE(::boost::math::digamma(static_cast<T>(500)), static_cast<T>(6.2136077650889917423827750552855712637776544784569L), tolerance);
  93. //
  94. // negative values:
  95. //
  96. BOOST_CHECK_CLOSE(::boost::math::digamma(static_cast<T>(-0.125)), static_cast<T>(7.1959829284523046176757814502538535827603450463013L), tolerance);
  97. BOOST_CHECK_CLOSE(::boost::math::digamma(static_cast<T>(-10.125)), static_cast<T>(9.9480538258660761287008034071425343357982429855241L), tolerance);
  98. BOOST_CHECK_CLOSE(::boost::math::digamma(static_cast<T>(-10.875)), static_cast<T>(-5.1527360383841562620205965901515879492020193154231L), tolerance);
  99. BOOST_CHECK_CLOSE(::boost::math::digamma(static_cast<T>(-1.5)), static_cast<T>(0.70315664064524318722569033366791109947350706200623L), tolerance);
  100. }
  101. BOOST_AUTO_TEST_CASE( test_main )
  102. {
  103. BOOST_MATH_CONTROL_FP;
  104. test_spots(0.0F, "float");
  105. test_spots(0.0, "double");
  106. #ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS
  107. test_spots(0.0L, "long double");
  108. test_spots(boost::math::concepts::real_concept(0.1), "real_concept");
  109. #endif
  110. expected_results();
  111. test_digamma(0.1F, "float");
  112. test_digamma(0.1, "double");
  113. #ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS
  114. test_digamma(0.1L, "long double");
  115. #ifndef BOOST_MATH_NO_REAL_CONCEPT_TESTS
  116. test_digamma(boost::math::concepts::real_concept(0.1), "real_concept");
  117. #endif
  118. #else
  119. std::cout << "<note>The long double tests have been disabled on this platform "
  120. "either because the long double overloads of the usual math functions are "
  121. "not available at all, or because they are too inaccurate for these tests "
  122. "to pass.</note>" << std::endl;
  123. #endif
  124. }