Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:40:17

0001 //  (C) Copyright John Maddock 2006.
0002 //  Use, modification and distribution are subject to the
0003 //  Boost Software License, Version 1.0. (See accompanying file
0004 //  LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
0005 
0006 #ifndef BOOST_MATH_SPECIAL_LEGENDRE_HPP
0007 #define BOOST_MATH_SPECIAL_LEGENDRE_HPP
0008 
0009 #ifdef _MSC_VER
0010 #pragma once
0011 #endif
0012 
0013 #include <utility>
0014 #include <vector>
0015 #include <type_traits>
0016 #include <boost/math/special_functions/math_fwd.hpp>
0017 #include <boost/math/special_functions/factorials.hpp>
0018 #include <boost/math/tools/roots.hpp>
0019 #include <boost/math/tools/config.hpp>
0020 #include <boost/math/tools/cxx03_warn.hpp>
0021 
0022 namespace boost{
0023 namespace math{
0024 
0025 // Recurrence relation for legendre P and Q polynomials:
0026 template <class T1, class T2, class T3>
0027 inline typename tools::promote_args<T1, T2, T3>::type
0028    legendre_next(unsigned l, T1 x, T2 Pl, T3 Plm1)
0029 {
0030    typedef typename tools::promote_args<T1, T2, T3>::type result_type;
0031    return ((2 * l + 1) * result_type(x) * result_type(Pl) - l * result_type(Plm1)) / (l + 1);
0032 }
0033 
0034 namespace detail{
0035 
0036 // Implement Legendre P and Q polynomials via recurrence:
0037 template <class T, class Policy>
0038 T legendre_imp(unsigned l, T x, const Policy& pol, bool second = false)
0039 {
0040    static const char* function = "boost::math::legrendre_p<%1%>(unsigned, %1%)";
0041    // Error handling:
0042    if((x < -1) || (x > 1))
0043       return policies::raise_domain_error<T>(
0044          function,
0045          "The Legendre Polynomial is defined for"
0046          " -1 <= x <= 1, but got x = %1%.", x, pol);
0047 
0048    T p0, p1;
0049    if(second)
0050    {
0051       // A solution of the second kind (Q):
0052       p0 = (boost::math::log1p(x, pol) - boost::math::log1p(-x, pol)) / 2;
0053       p1 = x * p0 - 1;
0054    }
0055    else
0056    {
0057       // A solution of the first kind (P):
0058       p0 = 1;
0059       p1 = x;
0060    }
0061    if(l == 0)
0062       return p0;
0063 
0064    unsigned n = 1;
0065 
0066    while(n < l)
0067    {
0068       std::swap(p0, p1);
0069       p1 = static_cast<T>(boost::math::legendre_next(n, x, p0, p1));
0070       ++n;
0071    }
0072    return p1;
0073 }
0074 
0075 template <class T, class Policy>
0076 T legendre_p_prime_imp(unsigned l, T x, const Policy& pol, T* Pn 
0077 #ifdef BOOST_NO_CXX11_NULLPTR
0078    = 0
0079 #else
0080    = nullptr
0081 #endif
0082 )
0083 {
0084    static const char* function = "boost::math::legrendre_p_prime<%1%>(unsigned, %1%)";
0085    // Error handling:
0086    if ((x < -1) || (x > 1))
0087       return policies::raise_domain_error<T>(
0088          function,
0089          "The Legendre Polynomial is defined for"
0090          " -1 <= x <= 1, but got x = %1%.", x, pol);
0091    
0092    if (l == 0)
0093     {
0094         if (Pn)
0095         {
0096            *Pn = 1;
0097         }
0098         return 0;
0099     }
0100     T p0 = 1;
0101     T p1 = x;
0102     T p_prime;
0103     bool odd = ((l & 1) == 1);
0104     // If the order is odd, we sum all the even polynomials:
0105     if (odd)
0106     {
0107         p_prime = p0;
0108     }
0109     else // Otherwise we sum the odd polynomials * (2n+1)
0110     {
0111         p_prime = 3*p1;
0112     }
0113 
0114     unsigned n = 1;
0115     while(n < l - 1)
0116     {
0117        std::swap(p0, p1);
0118        p1 = static_cast<T>(boost::math::legendre_next(n, x, p0, p1));
0119        ++n;
0120        if (odd)
0121        {
0122           p_prime += (2*n+1)*p1;
0123           odd = false;
0124        }
0125        else
0126        {
0127            odd = true;
0128        }
0129     }
0130     // This allows us to evaluate the derivative and the function for the same cost.
0131     if (Pn)
0132     {
0133         std::swap(p0, p1);
0134         *Pn = static_cast<T>(boost::math::legendre_next(n, x, p0, p1));
0135     }
0136     return p_prime;
0137 }
0138 
0139 template <class T, class Policy>
0140 struct legendre_p_zero_func
0141 {
0142    int n;
0143    const Policy& pol;
0144 
0145    legendre_p_zero_func(int n_, const Policy& p) : n(n_), pol(p) {}
0146 
0147    std::pair<T, T> operator()(T x) const
0148    { 
0149       T Pn;
0150       T Pn_prime = detail::legendre_p_prime_imp(n, x, pol, &Pn);
0151       return std::pair<T, T>(Pn, Pn_prime); 
0152    }
0153 };
0154 
0155 template <class T, class Policy>
0156 std::vector<T> legendre_p_zeros_imp(int n, const Policy& pol)
0157 {
0158     using std::cos;
0159     using std::sin;
0160     using std::ceil;
0161     using std::sqrt;
0162     using boost::math::constants::pi;
0163     using boost::math::constants::half;
0164     using boost::math::tools::newton_raphson_iterate;
0165 
0166     BOOST_MATH_ASSERT(n >= 0);
0167     std::vector<T> zeros;
0168     if (n == 0)
0169     {
0170         // There are no zeros of P_0(x) = 1.
0171         return zeros;
0172     }
0173     int k;
0174     if (n & 1)
0175     {
0176         zeros.resize((n-1)/2 + 1, std::numeric_limits<T>::quiet_NaN());
0177         zeros[0] = 0;
0178         k = 1;
0179     }
0180     else
0181     {
0182         zeros.resize(n/2, std::numeric_limits<T>::quiet_NaN());
0183         k = 0;
0184     }
0185     T half_n = ceil(n*half<T>());
0186 
0187     while (k < (int)zeros.size())
0188     {
0189         // Bracket the root: Szego:
0190         // Gabriel Szego, Inequalities for the Zeros of Legendre Polynomials and Related Functions, Transactions of the American Mathematical Society, Vol. 39, No. 1 (1936)
0191         T theta_nk =  ((half_n - half<T>()*half<T>() - static_cast<T>(k))*pi<T>())/(static_cast<T>(n)+half<T>());
0192         T lower_bound = cos( (half_n - static_cast<T>(k))*pi<T>()/static_cast<T>(n + 1));
0193         T cos_nk = cos(theta_nk);
0194         T upper_bound = cos_nk;
0195         // First guess follows from:
0196         //  F. G. Tricomi, Sugli zeri dei polinomi sferici ed ultrasferici, Ann. Mat. Pura Appl., 31 (1950), pp. 93-97;
0197         T inv_n_sq = 1/static_cast<T>(n*n);
0198         T sin_nk = sin(theta_nk);
0199         T x_nk_guess = (1 - inv_n_sq/static_cast<T>(8) + inv_n_sq /static_cast<T>(8*n) - (inv_n_sq*inv_n_sq/384)*(39  - 28 / (sin_nk*sin_nk) ) )*cos_nk;
0200 
0201         std::uintmax_t number_of_iterations = policies::get_max_root_iterations<Policy>();
0202 
0203         legendre_p_zero_func<T, Policy> f(n, pol);
0204 
0205         const T x_nk = newton_raphson_iterate(f, x_nk_guess,
0206                                               lower_bound, upper_bound,
0207                                               policies::digits<T, Policy>(),
0208                                               number_of_iterations);
0209 
0210         BOOST_MATH_ASSERT(lower_bound < x_nk);
0211         BOOST_MATH_ASSERT(upper_bound > x_nk);
0212         zeros[k] = x_nk;
0213         ++k;
0214     }
0215     return zeros;
0216 }
0217 
0218 } // namespace detail
0219 
0220 template <class T, class Policy>
0221 inline typename std::enable_if<policies::is_policy<Policy>::value, typename tools::promote_args<T>::type>::type
0222    legendre_p(int l, T x, const Policy& pol)
0223 {
0224    typedef typename tools::promote_args<T>::type result_type;
0225    typedef typename policies::evaluation<result_type, Policy>::type value_type;
0226    static const char* function = "boost::math::legendre_p<%1%>(unsigned, %1%)";
0227    if(l < 0)
0228       return policies::checked_narrowing_cast<result_type, Policy>(detail::legendre_imp(-l-1, static_cast<value_type>(x), pol, false), function);
0229    return policies::checked_narrowing_cast<result_type, Policy>(detail::legendre_imp(l, static_cast<value_type>(x), pol, false), function);
0230 }
0231 
0232 
0233 template <class T, class Policy>
0234 inline typename std::enable_if<policies::is_policy<Policy>::value, typename tools::promote_args<T>::type>::type
0235    legendre_p_prime(int l, T x, const Policy& pol)
0236 {
0237    typedef typename tools::promote_args<T>::type result_type;
0238    typedef typename policies::evaluation<result_type, Policy>::type value_type;
0239    static const char* function = "boost::math::legendre_p_prime<%1%>(unsigned, %1%)";
0240    if(l < 0)
0241       return policies::checked_narrowing_cast<result_type, Policy>(detail::legendre_p_prime_imp(-l-1, static_cast<value_type>(x), pol), function);
0242    return policies::checked_narrowing_cast<result_type, Policy>(detail::legendre_p_prime_imp(l, static_cast<value_type>(x), pol), function);
0243 }
0244 
0245 template <class T>
0246 inline typename tools::promote_args<T>::type
0247    legendre_p(int l, T x)
0248 {
0249    return boost::math::legendre_p(l, x, policies::policy<>());
0250 }
0251 
0252 template <class T>
0253 inline typename tools::promote_args<T>::type
0254    legendre_p_prime(int l, T x)
0255 {
0256    return boost::math::legendre_p_prime(l, x, policies::policy<>());
0257 }
0258 
0259 template <class T, class Policy>
0260 inline std::vector<T> legendre_p_zeros(int l, const Policy& pol)
0261 {
0262     if(l < 0)
0263         return detail::legendre_p_zeros_imp<T>(-l-1, pol);
0264 
0265     return detail::legendre_p_zeros_imp<T>(l, pol);
0266 }
0267 
0268 
0269 template <class T>
0270 inline std::vector<T> legendre_p_zeros(int l)
0271 {
0272    return boost::math::legendre_p_zeros<T>(l, policies::policy<>());
0273 }
0274 
0275 template <class T, class Policy>
0276 inline typename std::enable_if<policies::is_policy<Policy>::value, typename tools::promote_args<T>::type>::type
0277    legendre_q(unsigned l, T x, const Policy& pol)
0278 {
0279    typedef typename tools::promote_args<T>::type result_type;
0280    typedef typename policies::evaluation<result_type, Policy>::type value_type;
0281    return policies::checked_narrowing_cast<result_type, Policy>(detail::legendre_imp(l, static_cast<value_type>(x), pol, true), "boost::math::legendre_q<%1%>(unsigned, %1%)");
0282 }
0283 
0284 template <class T>
0285 inline typename tools::promote_args<T>::type
0286    legendre_q(unsigned l, T x)
0287 {
0288    return boost::math::legendre_q(l, x, policies::policy<>());
0289 }
0290 
0291 // Recurrence for associated polynomials:
0292 template <class T1, class T2, class T3>
0293 inline typename tools::promote_args<T1, T2, T3>::type
0294    legendre_next(unsigned l, unsigned m, T1 x, T2 Pl, T3 Plm1)
0295 {
0296    typedef typename tools::promote_args<T1, T2, T3>::type result_type;
0297    return ((2 * l + 1) * result_type(x) * result_type(Pl) - (l + m) * result_type(Plm1)) / (l + 1 - m);
0298 }
0299 
0300 namespace detail{
0301 // Legendre P associated polynomial:
0302 template <class T, class Policy>
0303 T legendre_p_imp(int l, int m, T x, T sin_theta_power, const Policy& pol)
0304 {
0305    BOOST_MATH_STD_USING
0306    // Error handling:
0307    if((x < -1) || (x > 1))
0308       return policies::raise_domain_error<T>(
0309       "boost::math::legendre_p<%1%>(int, int, %1%)",
0310          "The associated Legendre Polynomial is defined for"
0311          " -1 <= x <= 1, but got x = %1%.", x, pol);
0312    // Handle negative arguments first:
0313    if(l < 0)
0314       return legendre_p_imp(-l-1, m, x, sin_theta_power, pol);
0315    if ((l == 0) && (m == -1))
0316    {
0317       return sqrt((1 - x) / (1 + x));
0318    }
0319    if ((l == 1) && (m == 0))
0320    {
0321       return x;
0322    }
0323    if (-m == l)
0324    {
0325       return pow((1 - x * x) / 4, T(l) / 2) / boost::math::tgamma<T>(l + 1, pol);
0326    }
0327    if(m < 0)
0328    {
0329       int sign = (m&1) ? -1 : 1;
0330       return sign * boost::math::tgamma_ratio(static_cast<T>(l+m+1), static_cast<T>(l+1-m), pol) * legendre_p_imp(l, -m, x, sin_theta_power, pol);
0331    }
0332    // Special cases:
0333    if(m > l)
0334       return 0;
0335    if(m == 0)
0336       return boost::math::legendre_p(l, x, pol);
0337 
0338    T p0 = boost::math::double_factorial<T>(2 * m - 1, pol) * sin_theta_power;
0339 
0340    if(m&1)
0341       p0 *= -1;
0342    if(m == l)
0343       return p0;
0344 
0345    T p1 = x * (2 * m + 1) * p0;
0346 
0347    int n = m + 1;
0348 
0349    while(n < l)
0350    {
0351       std::swap(p0, p1);
0352       p1 = boost::math::legendre_next(n, m, x, p0, p1);
0353       ++n;
0354    }
0355    return p1;
0356 }
0357 
0358 template <class T, class Policy>
0359 inline T legendre_p_imp(int l, int m, T x, const Policy& pol)
0360 {
0361    BOOST_MATH_STD_USING
0362    // TODO: we really could use that mythical "pow1p" function here:
0363    return legendre_p_imp(l, m, x, static_cast<T>(pow(1 - x*x, T(abs(m))/2)), pol);
0364 }
0365 
0366 }
0367 
0368 template <class T, class Policy>
0369 inline typename tools::promote_args<T>::type
0370    legendre_p(int l, int m, T x, const Policy& pol)
0371 {
0372    typedef typename tools::promote_args<T>::type result_type;
0373    typedef typename policies::evaluation<result_type, Policy>::type value_type;
0374    return policies::checked_narrowing_cast<result_type, Policy>(detail::legendre_p_imp(l, m, static_cast<value_type>(x), pol), "boost::math::legendre_p<%1%>(int, int, %1%)");
0375 }
0376 
0377 template <class T>
0378 inline typename tools::promote_args<T>::type
0379    legendre_p(int l, int m, T x)
0380 {
0381    return boost::math::legendre_p(l, m, x, policies::policy<>());
0382 }
0383 
0384 } // namespace math
0385 } // namespace boost
0386 
0387 #endif // BOOST_MATH_SPECIAL_LEGENDRE_HPP