gegenbauer.hpp 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. // (C) Copyright Nick Thompson 2019.
  2. // Use, modification and distribution are subject to the
  3. // Boost Software License, Version 1.0. (See accompanying file
  4. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. #ifndef BOOST_MATH_SPECIAL_GEGENBAUER_HPP
  6. #define BOOST_MATH_SPECIAL_GEGENBAUER_HPP
  7. #include <stdexcept>
  8. #include <type_traits>
  9. namespace boost { namespace math {
  10. template<typename Real>
  11. Real gegenbauer(unsigned n, Real lambda, Real x)
  12. {
  13. static_assert(!std::is_integral<Real>::value, "Gegenbauer polynomials required floating point arguments.");
  14. if (lambda <= -1/Real(2)) {
  15. throw std::domain_error("lambda > -1/2 is required.");
  16. }
  17. // The only reason to do this is because of some instability that could be present for x < 0 that is not present for x > 0.
  18. // I haven't observed this, but then again, I haven't managed to test an exhaustive number of parameters.
  19. // In any case, the routine is distinctly faster without this test:
  20. //if (x < 0) {
  21. // if (n&1) {
  22. // return -gegenbauer(n, lambda, -x);
  23. // }
  24. // return gegenbauer(n, lambda, -x);
  25. //}
  26. if (n == 0) {
  27. return Real(1);
  28. }
  29. Real y0 = 1;
  30. Real y1 = 2*lambda*x;
  31. Real yk = y1;
  32. Real k = 2;
  33. Real k_max = n*(1+std::numeric_limits<Real>::epsilon());
  34. Real gamma = 2*(lambda - 1);
  35. while(k < k_max)
  36. {
  37. yk = ( (2 + gamma/k)*x*y1 - (1+gamma/k)*y0);
  38. y0 = y1;
  39. y1 = yk;
  40. k += 1;
  41. }
  42. return yk;
  43. }
  44. template<typename Real>
  45. Real gegenbauer_derivative(unsigned n, Real lambda, Real x, unsigned k)
  46. {
  47. if (k > n) {
  48. return Real(0);
  49. }
  50. Real gegen = gegenbauer<Real>(n-k, lambda + k, x);
  51. Real scale = 1;
  52. for (unsigned j = 0; j < k; ++j) {
  53. scale *= 2*lambda;
  54. lambda += 1;
  55. }
  56. return scale*gegen;
  57. }
  58. template<typename Real>
  59. Real gegenbauer_prime(unsigned n, Real lambda, Real x) {
  60. return gegenbauer_derivative<Real>(n, lambda, x, 1);
  61. }
  62. }}
  63. #endif