• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //           Copyright Matthew Pulver 2018 - 2019.
2 // Distributed under the Boost Software License, Version 1.0.
3 //      (See accompanying file LICENSE_1_0.txt or copy at
4 //           https://www.boost.org/LICENSE_1_0.txt)
5 
6 #include "test_autodiff.hpp"
7 #include <boost/math/special_functions.hpp>
8 
9 BOOST_AUTO_TEST_SUITE(test_autodiff_5)
10 
BOOST_AUTO_TEST_CASE_TEMPLATE(binomial_hpp,T,all_float_types)11 BOOST_AUTO_TEST_CASE_TEMPLATE(binomial_hpp, T, all_float_types) {
12   using boost::multiprecision::min;
13   using std::fabs;
14   using std::min;
15 
16   using test_constants = test_constants_t<T>;
17   static constexpr auto m = test_constants::order;
18   test_detail::RandomSample<unsigned> n_sampler{0u, 30u};
19   test_detail::RandomSample<unsigned> r_sampler{0u, 30u};
20 
21   for (auto i : boost::irange(test_constants::n_samples)) {
22     std::ignore = i;
23     auto n = n_sampler.next();
24     auto r = n == 0 ? 0 : (min)(r_sampler.next(), n - 1);
25 
26     // This is a hard function to test for type float due to a specialization of
27     // boost::math::binomial_coefficient
28     auto autodiff_v =
29         std::is_same<T, float>::value
30             ? make_fvar<T, m>(boost::math::binomial_coefficient<T>(n, r))
31             : boost::math::binomial_coefficient<T>(n, r);
32     auto anchor_v = boost::math::binomial_coefficient<T>(n, r);
33     BOOST_CHECK_EQUAL(autodiff_v.derivative(0u), anchor_v);
34   }
35 }
36 
BOOST_AUTO_TEST_CASE_TEMPLATE(cbrt_hpp,T,all_float_types)37 BOOST_AUTO_TEST_CASE_TEMPLATE(cbrt_hpp, T, all_float_types) {
38   using test_constants = test_constants_t<T>;
39   static constexpr auto m = test_constants::order;
40   test_detail::RandomSample<T> x_sampler{-2000, 2000};
41   for (auto i : boost::irange(test_constants::n_samples)) {
42     std::ignore = i;
43     auto x = x_sampler.next();
44     BOOST_CHECK_CLOSE(boost::math::cbrt(make_fvar<T, m>(x)).derivative(0u),
45                       boost::math::cbrt(x), 50 * test_constants::pct_epsilon());
46   }
47 }
48 
BOOST_AUTO_TEST_CASE_TEMPLATE(chebyshev_hpp,T,all_float_types)49 BOOST_AUTO_TEST_CASE_TEMPLATE(chebyshev_hpp, T, all_float_types) {
50   using test_constants = test_constants_t<T>;
51   static constexpr auto m = test_constants::order;
52   {
53     test_detail::RandomSample<unsigned> n_sampler{0u, 10u};
54     test_detail::RandomSample<T> x_sampler{-2, 2};
55     for (auto i : boost::irange(test_constants::n_samples)) {
56       std::ignore = i;
57       auto n = n_sampler.next();
58       auto x = x_sampler.next();
59       BOOST_CHECK_CLOSE(
60           boost::math::chebyshev_t(n, make_fvar<T, m>(x)).derivative(0u),
61           boost::math::chebyshev_t(n, x), 40 * test_constants::pct_epsilon());
62 
63       BOOST_CHECK_CLOSE(
64           boost::math::chebyshev_u(n, make_fvar<T, m>(x)).derivative(0u),
65           boost::math::chebyshev_u(n, x), 40 * test_constants::pct_epsilon());
66 
67       BOOST_CHECK_CLOSE(
68           boost::math::chebyshev_t_prime(n, make_fvar<T, m>(x)).derivative(0u),
69           boost::math::chebyshev_t_prime(n, x),
70           40 * test_constants::pct_epsilon());
71 
72       /*/usr/include/boost/math/special_functions/chebyshev.hpp:164:40: error:
73        cannot convert
74        boost::math::differentiation::autodiff_v1::detail::fvar<double, 3> to
75        double in return
76        BOOST_CHECK_EQUAL(boost::math::chebyshev_clenshaw_recurrence(c.data(),c.size(),make_fvar<T,m>(0.20))
77        ,
78        boost::math::chebyshev_clenshaw_recurrence(c.data(),c.size(),static_cast<T>(0.20)));*/
79       /*try {
80         std::array<T, 4> c0{{14.2, -13.7, 82.3, 96}};
81         BOOST_CHECK_CLOSE(boost::math::chebyshev_clenshaw_recurrence(c0.data(),
82       c0.size(), make_fvar<T,m>(x)),
83                                      boost::math::chebyshev_clenshaw_recurrence(c0.data(),
84       c0.size(), x), 10*test_constants::pct_epsilon()); } catch (...) {
85         std::rethrow_exception(std::exception_ptr(std::current_exception()));
86       }*/
87     }
88   }
89 }
90 
BOOST_AUTO_TEST_CASE_TEMPLATE(cospi_hpp,T,all_float_types)91 BOOST_AUTO_TEST_CASE_TEMPLATE(cospi_hpp, T, all_float_types) {
92   using test_constants = test_constants_t<T>;
93   static constexpr auto m = test_constants::order;
94   test_detail::RandomSample<T> x_sampler{-2000, 2000};
95   for (auto i : boost::irange(test_constants::n_samples)) {
96     std::ignore = i;
97     auto x = x_sampler.next();
98     BOOST_CHECK_CLOSE(boost::math::cos_pi(make_fvar<T, m>(x)).derivative(0u),
99                       boost::math::cos_pi(x), test_constants::pct_epsilon());
100   }
101 }
102 
BOOST_AUTO_TEST_CASE_TEMPLATE(digamma_hpp,T,all_float_types)103 BOOST_AUTO_TEST_CASE_TEMPLATE(digamma_hpp, T, all_float_types) {
104 
105   using boost::math::nextafter;
106   using std::nextafter;
107 
108   using test_constants = test_constants_t<T>;
109   static constexpr auto m = test_constants::order;
110   test_detail::RandomSample<T> x_sampler{-1, 2000};
111   for (auto i : boost::irange(test_constants::n_samples)) {
112     std::ignore = i;
113     auto x = nextafter(x_sampler.next(), ((std::numeric_limits<T>::max))());
114     auto autodiff_v = boost::math::digamma(make_fvar<T, m>(x));
115     auto anchor_v = boost::math::digamma(x);
116     BOOST_CHECK_CLOSE(autodiff_v.derivative(0u), anchor_v,
117                       1e4 * test_constants::pct_epsilon());
118   }
119 }
120 
121 BOOST_AUTO_TEST_SUITE_END()
122