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