gegenbauer.hpp 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. // (C) Copyright Nick Thompson 2019.
  2. // (C) Copyright Matt Borland 2024.
  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. #ifndef BOOST_MATH_SPECIAL_GEGENBAUER_HPP
  7. #define BOOST_MATH_SPECIAL_GEGENBAUER_HPP
  8. #include <boost/math/tools/config.hpp>
  9. #include <boost/math/tools/type_traits.hpp>
  10. #include <boost/math/tools/numeric_limits.hpp>
  11. #ifndef BOOST_MATH_NO_EXCEPTIONS
  12. #include <stdexcept>
  13. #endif
  14. namespace boost { namespace math {
  15. template<typename Real>
  16. BOOST_MATH_GPU_ENABLED Real gegenbauer(unsigned n, Real lambda, Real x)
  17. {
  18. static_assert(!boost::math::is_integral<Real>::value, "Gegenbauer polynomials required floating point arguments.");
  19. if (lambda <= -1/Real(2)) {
  20. #ifndef BOOST_MATH_NO_EXCEPTIONS
  21. throw std::domain_error("lambda > -1/2 is required.");
  22. #else
  23. return boost::math::numeric_limits<Real>::quiet_NaN();
  24. #endif
  25. }
  26. // 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.
  27. // I haven't observed this, but then again, I haven't managed to test an exhaustive number of parameters.
  28. // In any case, the routine is distinctly faster without this test:
  29. //if (x < 0) {
  30. // if (n&1) {
  31. // return -gegenbauer(n, lambda, -x);
  32. // }
  33. // return gegenbauer(n, lambda, -x);
  34. //}
  35. if (n == 0) {
  36. return Real(1);
  37. }
  38. Real y0 = 1;
  39. Real y1 = 2*lambda*x;
  40. Real yk = y1;
  41. Real k = 2;
  42. Real k_max = n*(1+boost::math::numeric_limits<Real>::epsilon());
  43. Real gamma = 2*(lambda - 1);
  44. while(k < k_max)
  45. {
  46. yk = ( (2 + gamma/k)*x*y1 - (1+gamma/k)*y0);
  47. y0 = y1;
  48. y1 = yk;
  49. k += 1;
  50. }
  51. return yk;
  52. }
  53. template<typename Real>
  54. BOOST_MATH_GPU_ENABLED Real gegenbauer_derivative(unsigned n, Real lambda, Real x, unsigned k)
  55. {
  56. if (k > n) {
  57. return Real(0);
  58. }
  59. Real gegen = gegenbauer<Real>(n-k, lambda + k, x);
  60. Real scale = 1;
  61. for (unsigned j = 0; j < k; ++j) {
  62. scale *= 2*lambda;
  63. lambda += 1;
  64. }
  65. return scale*gegen;
  66. }
  67. template<typename Real>
  68. BOOST_MATH_GPU_ENABLED Real gegenbauer_prime(unsigned n, Real lambda, Real x) {
  69. return gegenbauer_derivative<Real>(n, lambda, x, 1);
  70. }
  71. }}
  72. #endif