test_autodiff_5.cpp 4.7 KB

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