]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/math/test/test_autodiff_5.cpp
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)
6 #include "test_autodiff.hpp"
7 #include <boost/math/special_functions.hpp>
9 BOOST_AUTO_TEST_SUITE(test_autodiff_5
)
11 BOOST_AUTO_TEST_CASE_TEMPLATE(binomial_hpp
, T
, all_float_types
) {
12 using boost::multiprecision::min
;
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};
21 for (auto i
: boost::irange(test_constants::n_samples
)) {
23 auto n
= n_sampler
.next();
24 auto r
= n
== 0 ? 0 : (min
)(r_sampler
.next(), n
- 1);
26 // This is a hard function to test for type float due to a specialization of
27 // boost::math::binomial_coefficient
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
);
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
)) {
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());
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
;
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
)) {
57 auto n
= n_sampler
.next();
58 auto x
= x_sampler
.next();
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());
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());
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());
72 /*/usr/include/boost/math/special_functions/chebyshev.hpp:164:40: error:
74 boost::math::differentiation::autodiff_v1::detail::fvar<double, 3> to
76 BOOST_CHECK_EQUAL(boost::math::chebyshev_clenshaw_recurrence(c.data(),c.size(),make_fvar<T,m>(0.20))
78 boost::math::chebyshev_clenshaw_recurrence(c.data(),c.size(),static_cast<T>(0.20)));*/
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()));
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
)) {
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());
103 BOOST_AUTO_TEST_CASE_TEMPLATE(digamma_hpp
, T
, all_float_types
) {
105 using boost::math::nextafter
;
106 using std::nextafter
;
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
)) {
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());
121 BOOST_AUTO_TEST_SUITE_END()