• 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_8)
10 
BOOST_AUTO_TEST_CASE_TEMPLATE(hermite_hpp,T,all_float_types)11 BOOST_AUTO_TEST_CASE_TEMPLATE(hermite_hpp, T, all_float_types) {
12   using test_constants = test_constants_t<T>;
13   static constexpr auto m = test_constants::order;
14   test_detail::RandomSample<T> x_sampler{-200, 200};
15   for (auto i : boost::irange(14u)) {
16     auto x = x_sampler.next();
17     auto autodiff_v = boost::math::hermite(i, make_fvar<T, m>(x));
18     auto anchor_v = boost::math::hermite(i, x);
19     BOOST_CHECK(isNearZero(autodiff_v.derivative(0u) - anchor_v));
20   }
21 }
22 
BOOST_AUTO_TEST_CASE_TEMPLATE(heuman_lambda_hpp,T,all_float_types)23 BOOST_AUTO_TEST_CASE_TEMPLATE(heuman_lambda_hpp, T, all_float_types) {
24   using test_constants = test_constants_t<T>;
25   static constexpr auto m = test_constants::order;
26   test_detail::RandomSample<T> x_sampler{-1, 1};
27   test_detail::RandomSample<T> phi_sampler{-boost::math::constants::two_pi<T>(),
28                                            boost::math::constants::two_pi<T>()};
29   for (auto i : boost::irange(test_constants::n_samples)) {
30     std::ignore = i;
31     auto x = x_sampler.next();
32     auto phi = phi_sampler.next();
33     auto autodiff_v =
34         boost::math::heuman_lambda(make_fvar<T, m>(x), make_fvar<T, m>(phi));
35     auto anchor_v = boost::math::heuman_lambda(x, phi);
36     BOOST_CHECK(isNearZero(autodiff_v.derivative(0u) - anchor_v));
37   }
38 }
39 
BOOST_AUTO_TEST_CASE_TEMPLATE(hypot_hpp,T,all_float_types)40 BOOST_AUTO_TEST_CASE_TEMPLATE(hypot_hpp, T, all_float_types) {
41   using test_constants = test_constants_t<T>;
42   static constexpr auto m = test_constants::order;
43   test_detail::RandomSample<T> x_sampler{-2000, 2000};
44   test_detail::RandomSample<T> y_sampler{-2000, 2000};
45   for (auto i : boost::irange(test_constants::n_samples)) {
46     std::ignore = i;
47     auto x = x_sampler.next();
48     auto y = y_sampler.next();
49     auto autodiff_v =
50         boost::math::hypot(make_fvar<T, m>(x), make_fvar<T, m>(y));
51     auto anchor_v = boost::math::hypot(x, y);
52     BOOST_CHECK(isNearZero(autodiff_v.derivative(0u) - anchor_v));
53   }
54 }
55 
BOOST_AUTO_TEST_CASE_TEMPLATE(jacobi_elliptic_hpp,T,all_float_types)56 BOOST_AUTO_TEST_CASE_TEMPLATE(jacobi_elliptic_hpp, T, all_float_types) {
57   using test_constants = test_constants_t<T>;
58   static constexpr auto m = test_constants::order;
59   test_detail::RandomSample<T> k_sampler{0, 1};
60   test_detail::RandomSample<T> theta_sampler{-100, 100};
61   for (auto i : boost::irange(test_constants::n_samples)) {
62     std::ignore = i;
63     auto k = k_sampler.next();
64     auto theta = theta_sampler.next();
65     BOOST_CHECK(isNearZero(
66         boost::math::jacobi_cd(make_fvar<T, m>(k), make_fvar<T, m>(theta))
67             .derivative(0u) -
68         boost::math::jacobi_cd(k, theta)));
69     BOOST_CHECK(isNearZero(
70         boost::math::jacobi_cn(make_fvar<T, m>(k), make_fvar<T, m>(theta))
71             .derivative(0u) -
72         boost::math::jacobi_cn(k, theta)));
73     BOOST_CHECK(isNearZero(
74         boost::math::jacobi_cs(make_fvar<T, m>(k), make_fvar<T, m>(theta))
75             .derivative(0u) -
76         boost::math::jacobi_cs(k, theta)));
77     BOOST_CHECK(isNearZero(
78         boost::math::jacobi_dc(make_fvar<T, m>(k), make_fvar<T, m>(theta))
79             .derivative(0u) -
80         boost::math::jacobi_dc(k, theta)));
81     BOOST_CHECK(isNearZero(
82         boost::math::jacobi_dn(make_fvar<T, m>(k), make_fvar<T, m>(theta))
83             .derivative(0u) -
84         boost::math::jacobi_dn(k, theta)));
85     BOOST_CHECK(isNearZero(
86         boost::math::jacobi_ds(make_fvar<T, m>(k), make_fvar<T, m>(theta))
87             .derivative(0u) -
88         boost::math::jacobi_ds(k, theta)));
89     BOOST_CHECK(isNearZero(
90         boost::math::jacobi_nc(make_fvar<T, m>(k), make_fvar<T, m>(theta))
91             .derivative(0u) -
92         boost::math::jacobi_nc(k, theta)));
93     BOOST_CHECK(isNearZero(
94         boost::math::jacobi_nd(make_fvar<T, m>(k), make_fvar<T, m>(theta))
95             .derivative(0u) -
96         boost::math::jacobi_nd(k, theta)));
97     BOOST_CHECK(isNearZero(
98         boost::math::jacobi_ns(make_fvar<T, m>(k), make_fvar<T, m>(theta))
99             .derivative(0u) -
100         boost::math::jacobi_ns(k, theta)));
101     BOOST_CHECK(isNearZero(
102         boost::math::jacobi_sc(make_fvar<T, m>(k), make_fvar<T, m>(theta))
103             .derivative(0u) -
104         boost::math::jacobi_sc(k, theta)));
105     BOOST_CHECK(isNearZero(
106         boost::math::jacobi_sd(make_fvar<T, m>(k), make_fvar<T, m>(theta))
107             .derivative(0u) -
108         boost::math::jacobi_sd(k, theta)));
109     BOOST_CHECK(isNearZero(
110         boost::math::jacobi_sn(make_fvar<T, m>(k), make_fvar<T, m>(theta))
111             .derivative(0u) -
112         boost::math::jacobi_sn(k, theta)));
113   }
114 }
115 
BOOST_AUTO_TEST_CASE_TEMPLATE(jacobi_zeta_hpp,T,all_float_types)116 BOOST_AUTO_TEST_CASE_TEMPLATE(jacobi_zeta_hpp, T, all_float_types) {
117   using test_constants = test_constants_t<T>;
118   static constexpr auto m = test_constants::order;
119   test_detail::RandomSample<T> x_sampler{-1, 1};
120   test_detail::RandomSample<T> phi_sampler{-boost::math::constants::two_pi<T>(),
121                                            boost::math::constants::two_pi<T>()};
122   for (auto i : boost::irange(test_constants::n_samples)) {
123     std::ignore = i;
124     auto x = x_sampler.next();
125     auto phi = phi_sampler.next();
126     BOOST_CHECK(isNearZero(
127         boost::math::jacobi_zeta(make_fvar<T, m>(x), make_fvar<T, m>(phi))
128             .derivative(0u) -
129         boost::math::jacobi_zeta(x, phi)));
130   }
131 }
132 
BOOST_AUTO_TEST_CASE_TEMPLATE(laguerre_hpp,T,all_float_types)133 BOOST_AUTO_TEST_CASE_TEMPLATE(laguerre_hpp, T, all_float_types) {
134   using boost::multiprecision::min;
135   using std::min;
136 
137   using test_constants = test_constants_t<T>;
138   static constexpr auto m = test_constants::order;
139   test_detail::RandomSample<unsigned> n_sampler{1, 50};
140   test_detail::RandomSample<unsigned> r_sampler{0, 50};
141   test_detail::RandomSample<T> x_sampler{0, 50};
142 
143   for (auto i : boost::irange(test_constants::n_samples)) {
144     std::ignore = i;
145     auto n = n_sampler.next();
146     auto r = (min)(n - 1, r_sampler.next());
147     auto x = x_sampler.next();
148 
149     {
150       auto autodiff_v = boost::math::laguerre(n, make_fvar<T, m>(x));
151       auto anchor_v = boost::math::laguerre(n, x);
152       BOOST_CHECK(isNearZero(autodiff_v.derivative(0u) - anchor_v));
153     }
154     {
155       auto autodiff_v = boost::math::laguerre(n, r, make_fvar<T, m>(x));
156       auto anchor_v = boost::math::laguerre(n, r, x);
157       BOOST_CHECK(isNearZero(autodiff_v.derivative(0u) - anchor_v));
158     }
159   }
160 }
161 
BOOST_AUTO_TEST_CASE_TEMPLATE(lambert_w_hpp,T,all_float_types)162 BOOST_AUTO_TEST_CASE_TEMPLATE(lambert_w_hpp, T, all_float_types) {
163   using boost::math::nextafter;
164   using boost::math::tools::max;
165   using boost::multiprecision::fabs;
166   using boost::multiprecision::min;
167   using detail::fabs;
168   using std::fabs;
169   using std::max;
170   using std::min;
171   using std::nextafter;
172 
173   using promoted_t = promote<T, double>;
174   using test_constants = test_constants_t<T>;
175   static constexpr auto m = test_constants::order;
176   test_detail::RandomSample<T> x_sampler{static_cast<T>(-1 / std::exp(-1)),
177                                          ((std::numeric_limits<T>::max))()};
178   for (auto i : boost::irange(test_constants::n_samples)) {
179     std::ignore = i;
180     auto x = x_sampler.next();
181     {
182       auto x_ = (min<T>)(static_cast<T>(((max<promoted_t>))(
183                              -exp(promoted_t(-1)), promoted_t(x))),
184                          ((std::numeric_limits<T>::max))());
185       {
186         auto autodiff_v = boost::math::lambert_w0(make_fvar<T, m>(x_));
187         auto anchor_v = boost::math::lambert_w0(x_);
188         BOOST_CHECK(isNearZero(autodiff_v.derivative(0u) - anchor_v));
189       }
190       {
191         auto autodiff_v = boost::math::lambert_w0_prime(make_fvar<T, m>(x_));
192         auto anchor_v = boost::math::lambert_w0_prime(x_);
193         BOOST_CHECK(isNearZero(autodiff_v.derivative(0u) - anchor_v));
194       }
195     }
196 
197     {
198       auto x_ = nextafter(
199           static_cast<T>(nextafter(
200               ((max))(static_cast<promoted_t>(-exp(-1)), -fabs(promoted_t(x))),
201               ((std::numeric_limits<promoted_t>::max))())),
202           ((std::numeric_limits<T>::max))());
203       x_ = (max)(static_cast<T>(-0.36), x_);
204       BOOST_CHECK(isNearZero(
205           boost::math::lambert_wm1(make_fvar<T, m>(x_)).derivative(0u) -
206           boost::math::lambert_wm1(x_)));
207       BOOST_CHECK(isNearZero(
208           boost::math::lambert_wm1_prime(make_fvar<T, m>(x_)).derivative(0u) -
209           boost::math::lambert_wm1_prime(x_)));
210     }
211   }
212 }
213 
BOOST_AUTO_TEST_CASE_TEMPLATE(log1p_hpp,T,all_float_types)214 BOOST_AUTO_TEST_CASE_TEMPLATE(log1p_hpp, T, all_float_types) {
215   using boost::math::log1p;
216   using boost::multiprecision::log1p;
217 
218   using test_constants = test_constants_t<T>;
219   static constexpr auto m = test_constants::order;
220   test_detail::RandomSample<T> x_sampler{-1, 2000};
221   for (auto i : boost::irange(test_constants::n_samples)) {
222     std::ignore = i;
223     auto x = x_sampler.next();
224     BOOST_CHECK(
225         isNearZero(log1p(make_fvar<T, m>(x)).derivative(0u) - log1p(x)));
226   }
227 }
228 
BOOST_AUTO_TEST_CASE_TEMPLATE(next_hpp,T,all_float_types)229 BOOST_AUTO_TEST_CASE_TEMPLATE(next_hpp, T, all_float_types) {
230   using boost::math::float_advance;
231   using boost::math::float_distance;
232   using boost::math::float_next;
233   using boost::math::float_prior;
234   using boost::math::nextafter;
235   using boost::multiprecision::nextafter;
236 
237   using test_constants = test_constants_t<T>;
238   static constexpr auto m = test_constants::order;
239   for (auto i : boost::irange(test_constants::n_samples)) {
240     const auto j = static_cast<T>(i);
241     auto fvar_j = make_fvar<T, m>(j);
242     BOOST_CHECK(isNearZero(float_next(fvar_j).derivative(0u) - float_next(j)));
243     BOOST_CHECK(
244         isNearZero(float_prior(fvar_j).derivative(0u) - float_prior(j)));
245 
246     BOOST_CHECK(isNearZero(
247         nextafter(fvar_j, make_fvar<T, m>(static_cast<T>(1))).derivative(0u) -
248         nextafter(j, static_cast<T>(1))));
249     BOOST_CHECK(
250         isNearZero(nextafter(fvar_j, make_fvar<T, m>(static_cast<T>(i + 2))) -
251                    nextafter(j, static_cast<T>(i + 2))));
252     BOOST_CHECK(
253         isNearZero(nextafter(fvar_j, make_fvar<T, m>(static_cast<T>(i + 1)))
254                        .derivative(0u) -
255                    nextafter(j, static_cast<T>(i + 2))));
256     BOOST_CHECK(isNearZero(
257         nextafter(fvar_j, make_fvar<T, m>(static_cast<T>(-1))).derivative(0u) -
258         nextafter(j, static_cast<T>(-1))));
259     BOOST_CHECK(isNearZero(
260         nextafter(fvar_j, make_fvar<T, m>(static_cast<T>(-1 * (i + 2))))
261             .derivative(0u) -
262         nextafter(j, -1 * static_cast<T>(i + 2))));
263 
264     BOOST_CHECK(isNearZero(
265         nextafter(fvar_j, make_fvar<T, m>(static_cast<T>(-1 * (i + 1))))
266             .derivative(0u) -
267         nextafter(j, -1 * static_cast<T>(i + 2))));
268 
269     BOOST_CHECK(isNearZero(nextafter(fvar_j, fvar_j) - fvar_j));
270 
271     BOOST_CHECK(isNearZero(float_advance(fvar_j, 1).derivative(0u) -
272                            float_advance(j, 1)));
273     BOOST_CHECK(isNearZero(float_advance(fvar_j, i + 2).derivative(0u) -
274                            float_advance(j, i + 2)));
275     BOOST_CHECK(isNearZero(
276         float_advance(fvar_j, i + 1).derivative(0u) -
277         float_advance(float_advance(fvar_j, i + 2), -1).derivative(0u)));
278 
279     BOOST_CHECK(isNearZero(float_advance(fvar_j, -1).derivative(0u) -
280                            float_advance(j, -1)));
281 
282     BOOST_CHECK(isNearZero(
283         float_advance(fvar_j, -i - 1).derivative(0u) -
284         float_advance(float_advance(fvar_j, -i - 2), 1).derivative(0u)));
285 
286     BOOST_CHECK(isNearZero(float_advance(fvar_j, 0) - fvar_j));
287 
288     BOOST_CHECK(isNearZero(float_distance(fvar_j, j).derivative(0u) -
289                            static_cast<T>(0)));
290     BOOST_CHECK(
291         isNearZero(float_distance(float_next(fvar_j), fvar_j).derivative(0u) -
292                    ((make_fvar<T, m>(-1))).derivative(0u)));
293     BOOST_CHECK(
294         isNearZero(float_distance(float_prior(fvar_j), fvar_j).derivative(0u) -
295                    (make_fvar<T, m>(1)).derivative(0u)));
296   }
297 }
298 
BOOST_AUTO_TEST_CASE_TEMPLATE(owens_t_hpp,T,bin_float_types)299 BOOST_AUTO_TEST_CASE_TEMPLATE(owens_t_hpp, T, bin_float_types) {
300   BOOST_MATH_STD_USING;
301   using test_constants = test_constants_t<T>;
302   static constexpr auto m = test_constants::order;
303   test_detail::RandomSample<T> h_sampler{-2000, 2000};
304   test_detail::RandomSample<T> a_sampler{-2000, 2000};
305   for (auto i : boost::irange(test_constants::n_samples)) {
306     std::ignore = i;
307     auto h = h_sampler.next();
308     auto a = a_sampler.next();
309     auto autodiff_v =
310         boost::math::owens_t(make_fvar<T, m>(h), make_fvar<T, m>(a));
311     auto anchor_v = boost::math::owens_t(h, a);
312     BOOST_CHECK(isNearZero(autodiff_v.derivative(0u) - anchor_v));
313   }
314 }
315 
316 
BOOST_AUTO_TEST_CASE_TEMPLATE(pow_hpp,T,all_float_types)317 BOOST_AUTO_TEST_CASE_TEMPLATE(pow_hpp, T, all_float_types) {
318   using test_constants = test_constants_t<T>;
319   static constexpr auto m = test_constants::order;
320   for (auto i : boost::irange(10)) {
321     BOOST_CHECK_CLOSE(
322         boost::math::pow<0>(make_fvar<T, m>(static_cast<T>(i))).derivative(0u),
323         boost::math::pow<0>(static_cast<T>(i)), test_constants::pct_epsilon());
324     BOOST_CHECK_CLOSE(
325         boost::math::pow<1>(make_fvar<T, m>(static_cast<T>(i))).derivative(0u),
326         boost::math::pow<1>(static_cast<T>(i)), test_constants::pct_epsilon());
327     BOOST_CHECK_CLOSE(
328         boost::math::pow<2>(make_fvar<T, m>(static_cast<T>(i))).derivative(0u),
329         boost::math::pow<2>(static_cast<T>(i)), test_constants::pct_epsilon());
330     BOOST_CHECK_CLOSE(
331         boost::math::pow<3>(make_fvar<T, m>(static_cast<T>(i))).derivative(0u),
332         boost::math::pow<3>(static_cast<T>(i)), test_constants::pct_epsilon());
333     BOOST_CHECK_CLOSE(
334         boost::math::pow<4>(make_fvar<T, m>(static_cast<T>(i))).derivative(0u),
335         boost::math::pow<4>(static_cast<T>(i)), test_constants::pct_epsilon());
336     BOOST_CHECK_CLOSE(
337         boost::math::pow<5>(make_fvar<T, m>(static_cast<T>(i))).derivative(0u),
338         boost::math::pow<5>(static_cast<T>(i)), test_constants::pct_epsilon());
339     BOOST_CHECK_CLOSE(
340         boost::math::pow<6>(make_fvar<T, m>(static_cast<T>(i))).derivative(0u),
341         boost::math::pow<6>(static_cast<T>(i)), test_constants::pct_epsilon());
342     BOOST_CHECK_CLOSE(
343         boost::math::pow<7>(make_fvar<T, m>(static_cast<T>(i))).derivative(0u),
344         boost::math::pow<7>(static_cast<T>(i)), test_constants::pct_epsilon());
345     BOOST_CHECK_CLOSE(
346         boost::math::pow<8>(make_fvar<T, m>(static_cast<T>(i))).derivative(0u),
347         boost::math::pow<8>(static_cast<T>(i)), test_constants::pct_epsilon());
348     BOOST_CHECK_CLOSE(
349         boost::math::pow<9>(make_fvar<T, m>(static_cast<T>(i))).derivative(0u),
350         boost::math::pow<9>(static_cast<T>(i)), test_constants::pct_epsilon());
351   }
352 }
353 
BOOST_AUTO_TEST_CASE_TEMPLATE(polygamma_hpp,T,all_float_types)354 BOOST_AUTO_TEST_CASE_TEMPLATE(polygamma_hpp, T, all_float_types) {
355   using test_constants = test_constants_t<T>;
356   static constexpr auto m = test_constants::order;
357   test_detail::RandomSample<T> x_sampler{0, 2000};
358   for (auto i : boost::irange(test_constants::n_samples)) {
359     auto x = x_sampler.next();
360     try {
361       auto autodiff_v = boost::math::polygamma(i, make_fvar<T, m>(x));
362       auto anchor_v = boost::math::polygamma(i, x);
363       BOOST_CHECK(isNearZero(autodiff_v.derivative(0u) - anchor_v));
364     } catch (const std::overflow_error &) {
365       std::cout << "Overflow Error thrown with inputs i: " << i << " x: " << x
366                 << std::endl;
367       BOOST_CHECK_THROW(boost::math::polygamma(i, make_fvar<T, m>(x)),
368                         boost::wrapexcept<std::overflow_error>);
369       BOOST_CHECK_THROW(boost::math::polygamma(i, x),
370                         boost::wrapexcept<std::overflow_error>);
371     }
372   }
373 }
374 
BOOST_AUTO_TEST_CASE_TEMPLATE(powm1_hpp,T,all_float_types)375 BOOST_AUTO_TEST_CASE_TEMPLATE(powm1_hpp, T, all_float_types) {
376   using boost::math::tools::max;
377   using boost::multiprecision::max;
378   using std::max;
379 
380   using boost::multiprecision::log;
381   using detail::log;
382   using std::log;
383 
384   using boost::multiprecision::min;
385   using std::min;
386 
387   using boost::multiprecision::sqrt;
388   using detail::sqrt;
389   using std::sqrt;
390 
391   using boost::math::nextafter;
392   using boost::multiprecision::nextafter;
393 
394   using test_constants = test_constants_t<T>;
395   static constexpr auto m = test_constants::order;
396   test_detail::RandomSample<T> x_sampler{0, 2000};
397   for (auto i : boost::irange(test_constants::n_samples)) {
398     std::ignore = i;
399     auto x = ((max))(x_sampler.next(),
400                      boost::math::nextafter(static_cast<T>(0),
401                                             ((std::numeric_limits<T>::max))()));
402 
403     auto y =
404         ((min))(x_sampler.next(),
405                 log(sqrt(((std::numeric_limits<T>::max))()) + 1) / log(x + 1));
406     auto autodiff_v =
407         boost::math::powm1(make_fvar<T, m>(x), make_fvar<T, m>(y));
408     auto anchor_v = boost::math::powm1(x, y);
409     BOOST_CHECK(isNearZero(autodiff_v.derivative(0u) - anchor_v));
410   }
411 }
412 
BOOST_AUTO_TEST_CASE_TEMPLATE(sin_pi_hpp,T,all_float_types)413 BOOST_AUTO_TEST_CASE_TEMPLATE(sin_pi_hpp, T, all_float_types) {
414   using test_constants = test_constants_t<T>;
415   static constexpr auto m = test_constants::order;
416   test_detail::RandomSample<T> x_sampler{-2000, 2000};
417   for (auto i : boost::irange(test_constants::n_samples)) {
418     std::ignore = i;
419     auto x = x_sampler.next();
420     BOOST_CHECK(
421         isNearZero(boost::math::sin_pi(make_fvar<T, m>(x)).derivative(0u) -
422                    boost::math::sin_pi(x)));
423   }
424 }
425 
BOOST_AUTO_TEST_CASE_TEMPLATE(sinhc_hpp,T,all_float_types)426 BOOST_AUTO_TEST_CASE_TEMPLATE(sinhc_hpp, T, all_float_types) {
427   using test_constants = test_constants_t<T>;
428   static constexpr auto m = test_constants::order;
429   test_detail::RandomSample<T> x_sampler{ -80, 80 };
430   for (auto i : boost::irange(test_constants::n_samples)) {
431     std::ignore = i;
432     auto x = x_sampler.next();
433     if (x != 0) {
434       auto autodiff_v = boost::math::sinhc_pi(make_fvar<T, m>(x));
435       auto anchor_v = boost::math::sinhc_pi(x);
436       BOOST_CHECK_CLOSE(autodiff_v.derivative(0u), anchor_v,
437                         50 * test_constants::pct_epsilon());
438     }
439   }
440 }
441 
BOOST_AUTO_TEST_CASE_TEMPLATE(spherical_harmonic_hpp,T,all_float_types)442 BOOST_AUTO_TEST_CASE_TEMPLATE(spherical_harmonic_hpp, T, all_float_types) {
443   using test_constants = test_constants_t<T>;
444   static constexpr auto m = test_constants::order;
445   test_detail::RandomSample<T> theta_sampler{0,
446                                              boost::math::constants::pi<T>()};
447   test_detail::RandomSample<T> phi_sampler{0,
448                                            boost::math::constants::two_pi<T>()};
449   test_detail::RandomSample<int> r_sampler{0, test_constants::n_samples};
450   for (auto n : boost::irange(
451            1u, static_cast<unsigned>(test_constants::n_samples) + 1u)) {
452     auto theta = theta_sampler.next();
453     auto phi = phi_sampler.next();
454     auto r = (std::min)(static_cast<int>(n) - 1, r_sampler.next());
455     {
456       auto autodiff_v = boost::math::spherical_harmonic(
457           n, r, make_fvar<T, m>(theta), make_fvar<T, m>(phi));
458       auto anchor_v = boost::math::spherical_harmonic(n, r, theta, phi);
459       BOOST_CHECK(
460           isNearZero(autodiff_v.real().derivative(0u) - anchor_v.real()));
461       BOOST_CHECK(
462           isNearZero(autodiff_v.imag().derivative(0u) - anchor_v.imag()));
463     }
464 
465     {
466       auto autodiff_v = boost::math::spherical_harmonic_r(
467           n, r, make_fvar<T, m>(theta), make_fvar<T, m>(phi));
468       auto anchor_v = boost::math::spherical_harmonic_r(n, r, theta, phi);
469       BOOST_CHECK(isNearZero(autodiff_v.derivative(0u) - anchor_v));
470     }
471 
472     {
473       auto autodiff_v = boost::math::spherical_harmonic_i(
474           n, r, make_fvar<T, m>(theta), make_fvar<T, m>(phi));
475       auto anchor_v = boost::math::spherical_harmonic_i(n, r, theta, phi);
476       BOOST_CHECK(isNearZero(autodiff_v.derivative(0u) - anchor_v));
477     }
478   }
479 }
480 
BOOST_AUTO_TEST_CASE_TEMPLATE(sqrt1pm1_hpp,T,all_float_types)481 BOOST_AUTO_TEST_CASE_TEMPLATE(sqrt1pm1_hpp, T, all_float_types) {
482   using test_constants = test_constants_t<T>;
483   static constexpr auto m = test_constants::order;
484   test_detail::RandomSample<T> x_sampler{-1, 2000};
485   for (auto i : boost::irange(test_constants::n_samples)) {
486     std::ignore = i;
487     auto x = x_sampler.next();
488     BOOST_CHECK(
489         isNearZero(boost::math::sqrt1pm1(make_fvar<T, m>(x)).derivative(0u) -
490                    boost::math::sqrt1pm1(x)));
491   }
492 }
493 
BOOST_AUTO_TEST_CASE_TEMPLATE(trigamma_hpp,T,all_float_types)494 BOOST_AUTO_TEST_CASE_TEMPLATE(trigamma_hpp, T, all_float_types) {
495   using test_constants = test_constants_t<T>;
496   static constexpr auto m = test_constants::order;
497   test_detail::RandomSample<T> x_sampler{0, 2000};
498   for (auto i : boost::irange(test_constants::n_samples)) {
499     std::ignore = i;
500     auto x = x_sampler.next();
501     BOOST_CHECK(
502         isNearZero(boost::math::trigamma(make_fvar<T, m>(x)).derivative(0u) -
503                    boost::math::trigamma(x)));
504   }
505 }
506 
507 BOOST_AUTO_TEST_SUITE_END()
508