Imported Upstream version 1.57.0
[platform/upstream/boost.git] / libs / math / test / test_digamma.hpp
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
7 #include <boost/math/concepts/real_concept.hpp>
8 #define BOOST_TEST_MAIN
9 #include <boost/test/unit_test.hpp>
10 #include <boost/test/floating_point_comparison.hpp>
11 #include <boost/math/special_functions/math_fwd.hpp>
12 #include <boost/array.hpp>
13 #include "functor.hpp"
14
15 #include "handle_test_result.hpp"
16 #include "table_type.hpp"
17
18 #ifndef SC_
19 #define SC_(x) static_cast<typename table_type<T>::type>(BOOST_JOIN(x, L))
20 #endif
21
22 template <class Real, class T>
23 void do_test_digamma(const T& data, const char* type_name, const char* test_name)
24 {
25    typedef Real                   value_type;
26
27    typedef value_type (*pg)(value_type);
28 #if defined(BOOST_MATH_NO_DEDUCED_FUNCTION_POINTERS)
29    pg funcp = boost::math::digamma<value_type>;
30 #else
31    pg funcp = boost::math::digamma;
32 #endif
33
34    boost::math::tools::test_result<value_type> result;
35
36    std::cout << "Testing " << test_name << " with type " << type_name
37       << "\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
38
39    //
40    // test digamma against data:
41    //
42    result = boost::math::tools::test_hetero<Real>(
43       data, 
44       bind_func<Real>(funcp, 0), 
45       extract_result<Real>(1));
46    handle_test_result(result, data[result.worst()], result.worst(), type_name, "boost::math::digamma", test_name);
47    std::cout << std::endl;
48 }
49
50 template <class T>
51 void test_digamma(T, const char* name)
52 {
53    //
54    // The actual test data is rather verbose, so it's in a separate file
55    //
56    // The contents are as follows, each row of data contains
57    // three items, input value a, input value b and erf(a, b):
58    // 
59 #  include "digamma_data.ipp"
60
61    do_test_digamma<T>(digamma_data, name, "Digamma Function: Large Values");
62
63 #  include "digamma_root_data.ipp"
64
65    do_test_digamma<T>(digamma_root_data, name, "Digamma Function: Near the Positive Root");
66
67 #  include "digamma_small_data.ipp"
68
69    do_test_digamma<T>(digamma_small_data, name, "Digamma Function: Near Zero");
70
71 #  include "digamma_neg_data.ipp"
72
73    do_test_digamma<T>(digamma_neg_data, name, "Digamma Function: Negative Values");
74
75     static const boost::array<boost::array<T, 2>, 5> digamma_bugs = {{
76        // Test cases from Rocco Romeo:
77         {{ static_cast<T>(std::ldexp(1.0, -100)), SC_(-1.26765060022822940149670320537657721566490153286060651209008e30) }},
78         {{ static_cast<T>(-std::ldexp(1.0, -100)), SC_(1.26765060022822940149670320537542278433509846713939348790992e30) }},
79         {{ static_cast<T>(1), SC_(-0.577215664901532860606512090082402431042159335939923598805767) }},
80         {{ static_cast<T>(-1) + static_cast<T>(std::ldexp(1.0, -20)), SC_(-1.04857557721314249602848739817764518743062133735858753112190e6) }},
81         {{ static_cast<T>(-1) - static_cast<T>(std::ldexp(1.0, -20)), SC_(1.04857642278181269259522681939281063878220298942888100442172e6) }},
82     }};
83    do_test_digamma<T>(digamma_bugs, name, "Digamma Function: Values near 0");
84
85    BOOST_CHECK_THROW(boost::math::digamma(T(0)), std::domain_error);
86    BOOST_CHECK_THROW(boost::math::digamma(T(-1)), std::domain_error);
87    BOOST_CHECK_THROW(boost::math::digamma(T(-2)), std::domain_error);
88 }
89