Chris@16: Chris@16: // (C) Copyright John Maddock 2006. Chris@16: // Use, modification and distribution are subject to the Chris@16: // Boost Software License, Version 1.0. (See accompanying file Chris@16: // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) Chris@16: Chris@16: #ifndef BOOST_MATH_SPECIAL_LEGENDRE_HPP Chris@16: #define BOOST_MATH_SPECIAL_LEGENDRE_HPP Chris@16: Chris@16: #ifdef _MSC_VER Chris@16: #pragma once Chris@16: #endif Chris@16: Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: Chris@16: namespace boost{ Chris@16: namespace math{ Chris@16: Chris@16: // Recurrance relation for legendre P and Q polynomials: Chris@16: template Chris@16: inline typename tools::promote_args::type Chris@16: legendre_next(unsigned l, T1 x, T2 Pl, T3 Plm1) Chris@16: { Chris@16: typedef typename tools::promote_args::type result_type; Chris@16: return ((2 * l + 1) * result_type(x) * result_type(Pl) - l * result_type(Plm1)) / (l + 1); Chris@16: } Chris@16: Chris@16: namespace detail{ Chris@16: Chris@16: // Implement Legendre P and Q polynomials via recurrance: Chris@16: template Chris@16: T legendre_imp(unsigned l, T x, const Policy& pol, bool second = false) Chris@16: { Chris@16: static const char* function = "boost::math::legrendre_p<%1%>(unsigned, %1%)"; Chris@16: // Error handling: Chris@16: if((x < -1) || (x > 1)) Chris@16: return policies::raise_domain_error( Chris@16: function, Chris@16: "The Legendre Polynomial is defined for" Chris@16: " -1 <= x <= 1, but got x = %1%.", x, pol); Chris@16: Chris@16: T p0, p1; Chris@16: if(second) Chris@16: { Chris@16: // A solution of the second kind (Q): Chris@16: p0 = (boost::math::log1p(x, pol) - boost::math::log1p(-x, pol)) / 2; Chris@16: p1 = x * p0 - 1; Chris@16: } Chris@16: else Chris@16: { Chris@16: // A solution of the first kind (P): Chris@16: p0 = 1; Chris@16: p1 = x; Chris@16: } Chris@16: if(l == 0) Chris@16: return p0; Chris@16: Chris@16: unsigned n = 1; Chris@16: Chris@16: while(n < l) Chris@16: { Chris@16: std::swap(p0, p1); Chris@16: p1 = boost::math::legendre_next(n, x, p0, p1); Chris@16: ++n; Chris@16: } Chris@16: return p1; Chris@16: } Chris@16: Chris@16: } // namespace detail Chris@16: Chris@16: template Chris@101: inline typename boost::enable_if_c::value, typename tools::promote_args::type>::type Chris@16: legendre_p(int l, T x, const Policy& pol) Chris@16: { Chris@16: typedef typename tools::promote_args::type result_type; Chris@16: typedef typename policies::evaluation::type value_type; Chris@16: static const char* function = "boost::math::legendre_p<%1%>(unsigned, %1%)"; Chris@16: if(l < 0) Chris@16: return policies::checked_narrowing_cast(detail::legendre_imp(-l-1, static_cast(x), pol, false), function); Chris@16: return policies::checked_narrowing_cast(detail::legendre_imp(l, static_cast(x), pol, false), function); Chris@16: } Chris@16: Chris@16: template Chris@16: inline typename tools::promote_args::type Chris@16: legendre_p(int l, T x) Chris@16: { Chris@16: return boost::math::legendre_p(l, x, policies::policy<>()); Chris@16: } Chris@16: Chris@16: template Chris@101: inline typename boost::enable_if_c::value, typename tools::promote_args::type>::type Chris@16: legendre_q(unsigned l, T x, const Policy& pol) Chris@16: { Chris@16: typedef typename tools::promote_args::type result_type; Chris@16: typedef typename policies::evaluation::type value_type; Chris@16: return policies::checked_narrowing_cast(detail::legendre_imp(l, static_cast(x), pol, true), "boost::math::legendre_q<%1%>(unsigned, %1%)"); Chris@16: } Chris@16: Chris@16: template Chris@16: inline typename tools::promote_args::type Chris@16: legendre_q(unsigned l, T x) Chris@16: { Chris@16: return boost::math::legendre_q(l, x, policies::policy<>()); Chris@16: } Chris@16: Chris@16: // Recurrence for associated polynomials: Chris@16: template Chris@16: inline typename tools::promote_args::type Chris@16: legendre_next(unsigned l, unsigned m, T1 x, T2 Pl, T3 Plm1) Chris@16: { Chris@16: typedef typename tools::promote_args::type result_type; Chris@16: return ((2 * l + 1) * result_type(x) * result_type(Pl) - (l + m) * result_type(Plm1)) / (l + 1 - m); Chris@16: } Chris@16: Chris@16: namespace detail{ Chris@16: // Legendre P associated polynomial: Chris@16: template Chris@16: T legendre_p_imp(int l, int m, T x, T sin_theta_power, const Policy& pol) Chris@16: { Chris@16: // Error handling: Chris@16: if((x < -1) || (x > 1)) Chris@16: return policies::raise_domain_error( Chris@16: "boost::math::legendre_p<%1%>(int, int, %1%)", Chris@16: "The associated Legendre Polynomial is defined for" Chris@16: " -1 <= x <= 1, but got x = %1%.", x, pol); Chris@16: // Handle negative arguments first: Chris@16: if(l < 0) Chris@16: return legendre_p_imp(-l-1, m, x, sin_theta_power, pol); Chris@16: if(m < 0) Chris@16: { Chris@16: int sign = (m&1) ? -1 : 1; Chris@16: return sign * boost::math::tgamma_ratio(static_cast(l+m+1), static_cast(l+1-m), pol) * legendre_p_imp(l, -m, x, sin_theta_power, pol); Chris@16: } Chris@16: // Special cases: Chris@16: if(m > l) Chris@16: return 0; Chris@16: if(m == 0) Chris@16: return boost::math::legendre_p(l, x, pol); Chris@16: Chris@16: T p0 = boost::math::double_factorial(2 * m - 1, pol) * sin_theta_power; Chris@16: Chris@16: if(m&1) Chris@16: p0 *= -1; Chris@16: if(m == l) Chris@16: return p0; Chris@16: Chris@16: T p1 = x * (2 * m + 1) * p0; Chris@16: Chris@16: int n = m + 1; Chris@16: Chris@16: while(n < l) Chris@16: { Chris@16: std::swap(p0, p1); Chris@16: p1 = boost::math::legendre_next(n, m, x, p0, p1); Chris@16: ++n; Chris@16: } Chris@16: return p1; Chris@16: } Chris@16: Chris@16: template Chris@16: inline T legendre_p_imp(int l, int m, T x, const Policy& pol) Chris@16: { Chris@16: BOOST_MATH_STD_USING Chris@16: // TODO: we really could use that mythical "pow1p" function here: Chris@16: return legendre_p_imp(l, m, x, static_cast(pow(1 - x*x, T(abs(m))/2)), pol); Chris@16: } Chris@16: Chris@16: } Chris@16: Chris@16: template Chris@16: inline typename tools::promote_args::type Chris@16: legendre_p(int l, int m, T x, const Policy& pol) Chris@16: { Chris@16: typedef typename tools::promote_args::type result_type; Chris@16: typedef typename policies::evaluation::type value_type; Chris@16: return policies::checked_narrowing_cast(detail::legendre_p_imp(l, m, static_cast(x), pol), "bost::math::legendre_p<%1%>(int, int, %1%)"); Chris@16: } Chris@16: Chris@16: template Chris@16: inline typename tools::promote_args::type Chris@16: legendre_p(int l, int m, T x) Chris@16: { Chris@16: return boost::math::legendre_p(l, m, x, policies::policy<>()); Chris@16: } Chris@16: Chris@16: } // namespace math Chris@16: } // namespace boost Chris@16: Chris@16: #endif // BOOST_MATH_SPECIAL_LEGENDRE_HPP Chris@16: Chris@16: Chris@16: