gegenbauer_test.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. * Copyright Nick Thompson, 2019
  3. * Use, modification and distribution are subject to the
  4. * Boost Software License, Version 1.0. (See accompanying file
  5. * LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. */
  7. #include "math_unit_test.hpp"
  8. #include <numeric>
  9. #include <utility>
  10. #include <random>
  11. #include <cmath>
  12. #include <boost/core/demangle.hpp>
  13. #include <boost/math/special_functions/gegenbauer.hpp>
  14. #ifdef BOOST_HAS_FLOAT128
  15. #include <boost/multiprecision/float128.hpp>
  16. using boost::multiprecision::float128;
  17. #endif
  18. using std::abs;
  19. using boost::math::gegenbauer;
  20. using boost::math::gegenbauer_derivative;
  21. template<class Real>
  22. void test_parity()
  23. {
  24. std::mt19937 gen(323723);
  25. std::uniform_real_distribution<Real> xdis(-1, +1);
  26. std::uniform_real_distribution<Real> lambdadis(-0.5, 1);
  27. for(unsigned n = 0; n < 50; ++n) {
  28. unsigned calls = 50;
  29. unsigned i = 0;
  30. while(i++ < calls) {
  31. Real x = xdis(gen);
  32. Real lambda = lambdadis(gen);
  33. if (n & 1) {
  34. CHECK_ULP_CLOSE(gegenbauer(n, lambda, -x), -gegenbauer(n, lambda, x), 0);
  35. }
  36. else {
  37. CHECK_ULP_CLOSE(gegenbauer(n, lambda, -x), gegenbauer(n, lambda, x), 0);
  38. }
  39. }
  40. }
  41. }
  42. template<class Real>
  43. void test_quadratic()
  44. {
  45. Real lambda = 1/Real(4);
  46. auto c2 = [&](Real x) { return -lambda + 2*lambda*(1+lambda)*x*x; };
  47. Real x = -1;
  48. Real h = 1/Real(256);
  49. while (x < 1) {
  50. Real expected = c2(x);
  51. Real computed = gegenbauer(2, lambda, x);
  52. CHECK_ULP_CLOSE(expected, computed, 0);
  53. x += h;
  54. }
  55. }
  56. template<class Real>
  57. void test_cubic()
  58. {
  59. Real lambda = 1/Real(4);
  60. auto c3 = [&](Real x) { return lambda*(1+lambda)*x*(-2 + 4*(2+lambda)*x*x/3); };
  61. Real x = -1;
  62. Real h = 1/Real(256);
  63. while (x < 1) {
  64. Real expected = c3(x);
  65. Real computed = gegenbauer(3, lambda, x);
  66. CHECK_ULP_CLOSE(expected, computed, 4);
  67. x += h;
  68. }
  69. }
  70. template<class Real>
  71. void test_derivative()
  72. {
  73. Real lambda = 0.5;
  74. auto c3_prime = [&](Real x) { return 2*lambda*(lambda+1)*(-1 + 2*(lambda+2)*x*x); };
  75. auto c3_double_prime = [&](Real x) { return 8*lambda*(lambda+1)*(lambda+2)*x; };
  76. Real x = -1;
  77. Real h = 1/Real(256);
  78. while (x < 1) {
  79. Real expected = c3_prime(x);
  80. Real computed = gegenbauer_derivative(3, lambda, x, 1);
  81. CHECK_ULP_CLOSE(expected, computed, 1);
  82. expected = c3_double_prime(x);
  83. computed = gegenbauer_derivative(3, lambda, x, 2);
  84. CHECK_ULP_CLOSE(expected, computed, 1);
  85. x += h;
  86. }
  87. }
  88. int main()
  89. {
  90. test_parity<float>();
  91. test_parity<double>();
  92. test_parity<long double>();
  93. test_quadratic<float>();
  94. test_quadratic<double>();
  95. test_quadratic<long double>();
  96. test_cubic<double>();
  97. test_cubic<long double>();
  98. test_derivative<float>();
  99. test_derivative<double>();
  100. test_derivative<long double>();
  101. #ifdef BOOST_HAS_FLOAT128
  102. test_quadratic<boost::multiprecision::float128>();
  103. test_cubic<boost::multiprecision::float128>();
  104. #endif
  105. return boost::math::test::report_errors();
  106. }