]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/math/test/test_autodiff_6.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_6
)
11 /*********************************************************************************************************************
12 * special functions tests
13 *********************************************************************************************************************/
15 BOOST_AUTO_TEST_CASE_TEMPLATE(ellint_1_hpp
, T
, all_float_types
) {
16 using test_constants
= test_constants_t
<T
>;
17 static constexpr auto m
= test_constants::order
;
18 test_detail::RandomSample
<T
> k_sampler
{T
{-1}, T
{1}};
19 test_detail::RandomSample
<T
> phi_sampler
{-boost::math::constants::two_pi
<T
>(),
20 boost::math::constants::two_pi
<T
>()};
21 for (auto i
: boost::irange(test_constants::n_samples
)) {
23 auto k
= k_sampler
.next();
24 auto phi
= phi_sampler
.next();
25 BOOST_CHECK_CLOSE(boost::math::ellint_1(make_fvar
<T
, m
>(k
)).derivative(0u),
26 boost::math::ellint_1(k
),
27 2.5e3
* test_constants::pct_epsilon());
29 boost::math::ellint_1(make_fvar
<T
, m
>(k
), make_fvar
<T
, m
>(phi
))
31 boost::math::ellint_1(k
, phi
), 1e4
* test_constants::pct_epsilon());
35 BOOST_AUTO_TEST_CASE_TEMPLATE(ellint_2_hpp
, T
, all_float_types
) {
36 using test_constants
= test_constants_t
<T
>;
37 static constexpr auto m
= test_constants::order
;
38 test_detail::RandomSample
<T
> k_sampler
{-1, 1};
39 test_detail::RandomSample
<T
> phi_sampler
{-boost::math::constants::two_pi
<T
>(),
40 boost::math::constants::two_pi
<T
>()};
41 for (auto i
: boost::irange(test_constants::n_samples
)) {
43 auto k
= k_sampler
.next();
44 auto phi
= phi_sampler
.next();
45 BOOST_CHECK_CLOSE(boost::math::ellint_2(make_fvar
<T
, m
>(k
)).derivative(0u),
46 boost::math::ellint_2(k
),
47 2.5e3
* test_constants::pct_epsilon());
49 boost::math::ellint_2(make_fvar
<T
, m
>(k
), make_fvar
<T
, m
>(phi
))
51 boost::math::ellint_2(k
, phi
), 2.5e3
* test_constants::pct_epsilon());
55 BOOST_AUTO_TEST_CASE_TEMPLATE(ellint_3_hpp
, T
, all_float_types
) {
56 using boost::math::nextafter
;
57 using boost::multiprecision::nextafter
;
59 using boost::math::differentiation::detail::sin
;
60 using boost::multiprecision::min
;
64 using test_constants
= test_constants_t
<T
>;
65 static constexpr auto m
= test_constants::order
;
66 test_detail::RandomSample
<T
> k_sampler
{-1, 1};
67 test_detail::RandomSample
<T
> n_sampler
{-2000, 2000};
68 test_detail::RandomSample
<T
> phi_sampler
{-boost::math::constants::two_pi
<T
>(),
69 boost::math::constants::two_pi
<T
>()};
70 for (auto i
: boost::irange(test_constants::n_samples
)) {
72 auto k
= k_sampler
.next();
73 auto phi
= phi_sampler
.next();
74 auto n
= (min
)((min
)(n_sampler
.next(), T(1) / (sin(phi
) * sin(phi
))),
75 nextafter(T(1), T(0)));
76 BOOST_CHECK_CLOSE(boost::math::ellint_3(make_fvar
<T
, m
>(k
),
80 boost::math::ellint_3(k
, n
, phi
),
81 2.5e3
* test_constants::pct_epsilon());
85 BOOST_AUTO_TEST_CASE_TEMPLATE(ellint_d_hpp
, T
, all_float_types
) {
86 using test_constants
= test_constants_t
<T
>;
87 static constexpr auto m
= test_constants::order
;
88 test_detail::RandomSample
<T
> k_sampler
{-1, 1};
89 test_detail::RandomSample
<T
> phi_sampler
{-boost::math::constants::two_pi
<T
>(),
90 boost::math::constants::two_pi
<T
>()};
91 for (auto i
: boost::irange(test_constants::n_samples
)) {
93 auto k
= k_sampler
.next();
94 auto phi
= phi_sampler
.next();
95 BOOST_CHECK_CLOSE(boost::math::ellint_d(make_fvar
<T
, m
>(k
)).derivative(0u),
96 boost::math::ellint_d(k
),
97 2.5e3
* test_constants::pct_epsilon());
99 boost::math::ellint_d(make_fvar
<T
, m
>(k
), make_fvar
<T
, m
>(phi
))
101 boost::math::ellint_d(k
, phi
), 2.5e3
* test_constants::pct_epsilon());
105 BOOST_AUTO_TEST_CASE_TEMPLATE(ellint_rf_hpp
, T
, all_float_types
) {
107 using boost::math::tools::max
;
110 using boost::math::nextafter
;
111 using std::nextafter
;
113 using test_constants
= test_constants_t
<T
>;
114 static constexpr auto m
= test_constants::order
;
116 test_detail::RandomSample
<T
> x_sampler
{0, 2000};
117 test_detail::RandomSample
<T
> y_sampler
{0, 2000};
118 test_detail::RandomSample
<T
> z_sampler
{0, 2000};
119 for (auto i
: boost::irange(test_constants::n_samples
)) {
121 auto x
= nextafter(x_sampler
.next(), ((std::numeric_limits
<T
>::max
))());
122 auto y
= nextafter(y_sampler
.next(), ((std::numeric_limits
<T
>::max
))());
123 auto z
= nextafter(z_sampler
.next(), ((std::numeric_limits
<T
>::max
))());
126 boost::math::ellint_rf(make_fvar
<T
, m
>(x
), make_fvar
<T
, m
>(y
),
129 boost::math::ellint_rf(x
, y
, z
), 2.5e3
* test_constants::pct_epsilon());
133 BOOST_AUTO_TEST_CASE_TEMPLATE(ellint_rc_hpp
, T
, all_float_types
) {
134 using boost::math::fpclassify
;
135 using boost::math::nextafter
;
136 using boost::math::signbit
;
137 using boost::math::tools::max
;
138 using boost::multiprecision::fpclassify
;
139 using boost::multiprecision::signbit
;
141 using std::nextafter
;
143 using test_constants
= test_constants_t
<T
>;
144 static constexpr auto m
= test_constants::order
;
145 test_detail::RandomSample
<T
> x_sampler
{0, 2000};
146 test_detail::RandomSample
<T
> y_sampler
{0, 2000};
147 for (auto i
: boost::irange(test_constants::n_samples
)) {
149 auto x
= x_sampler
.next();
151 while (fpclassify(T(y
)) == FP_ZERO
) {
152 y
= (max
)(y_sampler
.next(),
153 nextafter(T(0), T(signbit(y
) ? -1 : 1) *
154 ((std::numeric_limits
<T
>::max
))()));
158 boost::math::ellint_rc(make_fvar
<T
, m
>(x
), make_fvar
<T
, m
>(y
))
160 boost::math::ellint_rc(x
, y
), 2.5e3
* test_constants::pct_epsilon());
164 BOOST_AUTO_TEST_CASE_TEMPLATE(ellint_rj_hpp
, T
, all_float_types
) {
165 using boost::math::fpclassify
;
166 using boost::math::nextafter
;
167 using boost::math::signbit
;
168 using boost::math::tools::max
;
169 using boost::multiprecision::fpclassify
;
170 using boost::multiprecision::signbit
;
173 using std::nextafter
;
175 using test_constants
= test_constants_t
<T
>;
176 static constexpr auto m
= test_constants::order
;
178 test_detail::RandomSample
<T
> x_sampler
{0, 2000};
179 test_detail::RandomSample
<T
> y_sampler
{0, 2000};
180 test_detail::RandomSample
<T
> z_sampler
{0, 2000};
181 test_detail::RandomSample
<T
> p_sampler
{-2000, 2000};
183 for (auto i
: boost::irange(test_constants::n_samples
)) {
185 auto x
= x_sampler
.next();
186 auto y
= (x
!= 0 ? 1 : 0) + y_sampler
.next();
187 auto z
= ((x
== 0 || y
== 0) ? 1 : 0) + z_sampler
.next();
190 while (fpclassify(T(p
)) == FP_ZERO
) {
191 p
= (max
)(p_sampler
.next(),
192 nextafter(T(0), T(signbit(p
) ? -1 : 1) *
193 ((std::numeric_limits
<T
>::max
))()));
196 boost::math::ellint_rj(make_fvar
<T
, m
>(x
), make_fvar
<T
, m
>(y
),
197 make_fvar
<T
, m
>(z
), make_fvar
<T
, m
>(p
))
199 boost::math::ellint_rj(x
, y
, z
, p
),
200 2.5e3
* test_constants::pct_epsilon());
204 BOOST_AUTO_TEST_CASE_TEMPLATE(ellint_rd_hpp
, T
, all_float_types
) {
205 using test_constants
= test_constants_t
<T
>;
206 static constexpr auto m
= test_constants::order
;
207 test_detail::RandomSample
<T
> x_sampler
{0, 2000};
208 test_detail::RandomSample
<T
> y_sampler
{0, 2000};
209 test_detail::RandomSample
<T
> z_sampler
{0, 2000};
210 for (auto i
: boost::irange(test_constants::n_samples
)) {
212 auto x
= x_sampler
.next();
213 auto y
= (x
== 0 ? 1 : 0) + y_sampler
.next();
214 auto z
= z_sampler
.next();
216 boost::math::ellint_rd(make_fvar
<T
, m
>(x
), make_fvar
<T
, m
>(y
),
219 boost::math::ellint_rd(x
, y
, z
), 2.5e3
* test_constants::pct_epsilon());
223 BOOST_AUTO_TEST_CASE_TEMPLATE(ellint_rg_hpp
, T
, all_float_types
) {
225 using boost::math::nextafter
;
226 using std::nextafter
;
228 using test_constants
= test_constants_t
<T
>;
229 static constexpr auto m
= test_constants::order
;
230 test_detail::RandomSample
<T
> x_sampler
{0, 2000};
231 test_detail::RandomSample
<T
> y_sampler
{0, 2000};
232 test_detail::RandomSample
<T
> z_sampler
{0, 2000};
234 for (auto i
: boost::irange(test_constants::n_samples
)) {
236 auto x
= nextafter(x_sampler
.next(), ((std::numeric_limits
<T
>::max
))());
237 auto y
= nextafter(y_sampler
.next(), ((std::numeric_limits
<T
>::max
))());
238 auto z
= z_sampler
.next();
240 boost::math::ellint_rg(make_fvar
<T
, m
>(x
), make_fvar
<T
, m
>(y
),
243 boost::math::ellint_rg(x
, y
, z
), 50 * test_constants::pct_epsilon());
247 BOOST_AUTO_TEST_CASE_TEMPLATE(erf_hpp
, T
, all_float_types
) {
248 using test_constants
= test_constants_t
<T
>;
249 static constexpr auto m
= test_constants::order
;
250 test_detail::RandomSample
<T
> x_sampler
{-2000, 2000};
251 for (auto i
: boost::irange(test_constants::n_samples
)) {
253 auto x
= x_sampler
.next();
255 BOOST_CHECK(isNearZero(erf(make_fvar
<T
, m
>(x
)).derivative(0u) -
256 boost::math::erf(x
)));
257 BOOST_CHECK(isNearZero(erfc(make_fvar
<T
, m
>(x
)).derivative(0u) -
258 boost::math::erfc(x
)));
262 BOOST_AUTO_TEST_CASE_TEMPLATE(expint_hpp
, T
, all_float_types
) {
263 using test_constants
= test_constants_t
<T
>;
264 static constexpr auto m
= test_constants::order
;
265 test_detail::RandomSample
<T
> x_sampler
{1, 83};
267 boost::irange(1u, static_cast<unsigned>(test_constants::n_samples
))) {
268 auto x
= x_sampler
.next();
269 BOOST_CHECK_CLOSE(boost::math::expint(n
, make_fvar
<T
, m
>(x
)).derivative(0u),
270 boost::math::expint(n
, x
),
271 200 * test_constants::pct_epsilon());
273 for (auto y
: {-1, 1}) {
275 boost::math::expint(make_fvar
<T
, m
>(x
* y
)).derivative(0u),
276 boost::math::expint(x
* y
), 200 * test_constants::pct_epsilon());
281 BOOST_AUTO_TEST_SUITE_END()