File indexing completed on 2025-09-15 08:40:13
0001
0002
0003
0004
0005
0006
0007 #ifndef BOOST_MATH_SPECIAL_GEGENBAUER_HPP
0008 #define BOOST_MATH_SPECIAL_GEGENBAUER_HPP
0009
0010 #include <boost/math/tools/config.hpp>
0011 #include <boost/math/tools/type_traits.hpp>
0012 #include <boost/math/tools/numeric_limits.hpp>
0013
0014 #ifndef BOOST_MATH_NO_EXCEPTIONS
0015 #include <stdexcept>
0016 #endif
0017
0018 namespace boost { namespace math {
0019
0020 template<typename Real>
0021 BOOST_MATH_GPU_ENABLED Real gegenbauer(unsigned n, Real lambda, Real x)
0022 {
0023 static_assert(!boost::math::is_integral<Real>::value, "Gegenbauer polynomials required floating point arguments.");
0024 if (lambda <= -1/Real(2)) {
0025 #ifndef BOOST_MATH_NO_EXCEPTIONS
0026 throw std::domain_error("lambda > -1/2 is required.");
0027 #else
0028 return boost::math::numeric_limits<Real>::quiet_NaN();
0029 #endif
0030 }
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041 if (n == 0) {
0042 return Real(1);
0043 }
0044 Real y0 = 1;
0045 Real y1 = 2*lambda*x;
0046
0047 Real yk = y1;
0048 Real k = 2;
0049 Real k_max = n*(1+boost::math::numeric_limits<Real>::epsilon());
0050 Real gamma = 2*(lambda - 1);
0051 while(k < k_max)
0052 {
0053 yk = ( (2 + gamma/k)*x*y1 - (1+gamma/k)*y0);
0054 y0 = y1;
0055 y1 = yk;
0056 k += 1;
0057 }
0058 return yk;
0059 }
0060
0061
0062 template<typename Real>
0063 BOOST_MATH_GPU_ENABLED Real gegenbauer_derivative(unsigned n, Real lambda, Real x, unsigned k)
0064 {
0065 if (k > n) {
0066 return Real(0);
0067 }
0068 Real gegen = gegenbauer<Real>(n-k, lambda + k, x);
0069 Real scale = 1;
0070 for (unsigned j = 0; j < k; ++j) {
0071 scale *= 2*lambda;
0072 lambda += 1;
0073 }
0074 return scale*gegen;
0075 }
0076
0077 template<typename Real>
0078 BOOST_MATH_GPU_ENABLED Real gegenbauer_prime(unsigned n, Real lambda, Real x) {
0079 return gegenbauer_derivative<Real>(n, lambda, x, 1);
0080 }
0081
0082
0083 }}
0084 #endif