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)
6 #ifndef BOOST_MATH_SPECIAL_GEGENBAUER_HPP
7 #define BOOST_MATH_SPECIAL_GEGENBAUER_HPP
10 #include <type_traits>
12 namespace boost { namespace math {
14 template<typename Real>
15 Real gegenbauer(unsigned n, Real lambda, Real x)
17 static_assert(!std::is_integral<Real>::value, "Gegenbauer polynomials required floating point arguments.");
18 if (lambda <= -1/Real(2)) {
19 throw std::domain_error("lambda > -1/2 is required.");
21 // 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.
22 // I haven't observed this, but then again, I haven't managed to test an exhaustive number of parameters.
23 // In any case, the routine is distinctly faster without this test:
26 // return -gegenbauer(n, lambda, -x);
28 // return gegenbauer(n, lambda, -x);
39 Real k_max = n*(1+std::numeric_limits<Real>::epsilon());
40 Real gamma = 2*(lambda - 1);
43 yk = ( (2 + gamma/k)*x*y1 - (1+gamma/k)*y0);
52 template<typename Real>
53 Real gegenbauer_derivative(unsigned n, Real lambda, Real x, unsigned k)
58 Real gegen = gegenbauer<Real>(n-k, lambda + k, x);
60 for (unsigned j = 0; j < k; ++j) {
67 template<typename Real>
68 Real gegenbauer_prime(unsigned n, Real lambda, Real x) {
69 return gegenbauer_derivative<Real>(n, lambda, x, 1);