annotate DEPENDENCIES/generic/include/boost/math/special_functions/legendre.hpp @ 125:34e428693f5d vext

Vext -> Repoint
author Chris Cannam
date Thu, 14 Jun 2018 11:15:39 +0100
parents c530137014c0
children
rev   line source
Chris@16 1
Chris@16 2 // (C) Copyright John Maddock 2006.
Chris@16 3 // Use, modification and distribution are subject to the
Chris@16 4 // Boost Software License, Version 1.0. (See accompanying file
Chris@16 5 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
Chris@16 6
Chris@16 7 #ifndef BOOST_MATH_SPECIAL_LEGENDRE_HPP
Chris@16 8 #define BOOST_MATH_SPECIAL_LEGENDRE_HPP
Chris@16 9
Chris@16 10 #ifdef _MSC_VER
Chris@16 11 #pragma once
Chris@16 12 #endif
Chris@16 13
Chris@16 14 #include <boost/math/special_functions/math_fwd.hpp>
Chris@16 15 #include <boost/math/special_functions/factorials.hpp>
Chris@16 16 #include <boost/math/tools/config.hpp>
Chris@16 17
Chris@16 18 namespace boost{
Chris@16 19 namespace math{
Chris@16 20
Chris@16 21 // Recurrance relation for legendre P and Q polynomials:
Chris@16 22 template <class T1, class T2, class T3>
Chris@16 23 inline typename tools::promote_args<T1, T2, T3>::type
Chris@16 24 legendre_next(unsigned l, T1 x, T2 Pl, T3 Plm1)
Chris@16 25 {
Chris@16 26 typedef typename tools::promote_args<T1, T2, T3>::type result_type;
Chris@16 27 return ((2 * l + 1) * result_type(x) * result_type(Pl) - l * result_type(Plm1)) / (l + 1);
Chris@16 28 }
Chris@16 29
Chris@16 30 namespace detail{
Chris@16 31
Chris@16 32 // Implement Legendre P and Q polynomials via recurrance:
Chris@16 33 template <class T, class Policy>
Chris@16 34 T legendre_imp(unsigned l, T x, const Policy& pol, bool second = false)
Chris@16 35 {
Chris@16 36 static const char* function = "boost::math::legrendre_p<%1%>(unsigned, %1%)";
Chris@16 37 // Error handling:
Chris@16 38 if((x < -1) || (x > 1))
Chris@16 39 return policies::raise_domain_error<T>(
Chris@16 40 function,
Chris@16 41 "The Legendre Polynomial is defined for"
Chris@16 42 " -1 <= x <= 1, but got x = %1%.", x, pol);
Chris@16 43
Chris@16 44 T p0, p1;
Chris@16 45 if(second)
Chris@16 46 {
Chris@16 47 // A solution of the second kind (Q):
Chris@16 48 p0 = (boost::math::log1p(x, pol) - boost::math::log1p(-x, pol)) / 2;
Chris@16 49 p1 = x * p0 - 1;
Chris@16 50 }
Chris@16 51 else
Chris@16 52 {
Chris@16 53 // A solution of the first kind (P):
Chris@16 54 p0 = 1;
Chris@16 55 p1 = x;
Chris@16 56 }
Chris@16 57 if(l == 0)
Chris@16 58 return p0;
Chris@16 59
Chris@16 60 unsigned n = 1;
Chris@16 61
Chris@16 62 while(n < l)
Chris@16 63 {
Chris@16 64 std::swap(p0, p1);
Chris@16 65 p1 = boost::math::legendre_next(n, x, p0, p1);
Chris@16 66 ++n;
Chris@16 67 }
Chris@16 68 return p1;
Chris@16 69 }
Chris@16 70
Chris@16 71 } // namespace detail
Chris@16 72
Chris@16 73 template <class T, class Policy>
Chris@101 74 inline typename boost::enable_if_c<policies::is_policy<Policy>::value, typename tools::promote_args<T>::type>::type
Chris@16 75 legendre_p(int l, T x, const Policy& pol)
Chris@16 76 {
Chris@16 77 typedef typename tools::promote_args<T>::type result_type;
Chris@16 78 typedef typename policies::evaluation<result_type, Policy>::type value_type;
Chris@16 79 static const char* function = "boost::math::legendre_p<%1%>(unsigned, %1%)";
Chris@16 80 if(l < 0)
Chris@16 81 return policies::checked_narrowing_cast<result_type, Policy>(detail::legendre_imp(-l-1, static_cast<value_type>(x), pol, false), function);
Chris@16 82 return policies::checked_narrowing_cast<result_type, Policy>(detail::legendre_imp(l, static_cast<value_type>(x), pol, false), function);
Chris@16 83 }
Chris@16 84
Chris@16 85 template <class T>
Chris@16 86 inline typename tools::promote_args<T>::type
Chris@16 87 legendre_p(int l, T x)
Chris@16 88 {
Chris@16 89 return boost::math::legendre_p(l, x, policies::policy<>());
Chris@16 90 }
Chris@16 91
Chris@16 92 template <class T, class Policy>
Chris@101 93 inline typename boost::enable_if_c<policies::is_policy<Policy>::value, typename tools::promote_args<T>::type>::type
Chris@16 94 legendre_q(unsigned l, T x, const Policy& pol)
Chris@16 95 {
Chris@16 96 typedef typename tools::promote_args<T>::type result_type;
Chris@16 97 typedef typename policies::evaluation<result_type, Policy>::type value_type;
Chris@16 98 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%)");
Chris@16 99 }
Chris@16 100
Chris@16 101 template <class T>
Chris@16 102 inline typename tools::promote_args<T>::type
Chris@16 103 legendre_q(unsigned l, T x)
Chris@16 104 {
Chris@16 105 return boost::math::legendre_q(l, x, policies::policy<>());
Chris@16 106 }
Chris@16 107
Chris@16 108 // Recurrence for associated polynomials:
Chris@16 109 template <class T1, class T2, class T3>
Chris@16 110 inline typename tools::promote_args<T1, T2, T3>::type
Chris@16 111 legendre_next(unsigned l, unsigned m, T1 x, T2 Pl, T3 Plm1)
Chris@16 112 {
Chris@16 113 typedef typename tools::promote_args<T1, T2, T3>::type result_type;
Chris@16 114 return ((2 * l + 1) * result_type(x) * result_type(Pl) - (l + m) * result_type(Plm1)) / (l + 1 - m);
Chris@16 115 }
Chris@16 116
Chris@16 117 namespace detail{
Chris@16 118 // Legendre P associated polynomial:
Chris@16 119 template <class T, class Policy>
Chris@16 120 T legendre_p_imp(int l, int m, T x, T sin_theta_power, const Policy& pol)
Chris@16 121 {
Chris@16 122 // Error handling:
Chris@16 123 if((x < -1) || (x > 1))
Chris@16 124 return policies::raise_domain_error<T>(
Chris@16 125 "boost::math::legendre_p<%1%>(int, int, %1%)",
Chris@16 126 "The associated Legendre Polynomial is defined for"
Chris@16 127 " -1 <= x <= 1, but got x = %1%.", x, pol);
Chris@16 128 // Handle negative arguments first:
Chris@16 129 if(l < 0)
Chris@16 130 return legendre_p_imp(-l-1, m, x, sin_theta_power, pol);
Chris@16 131 if(m < 0)
Chris@16 132 {
Chris@16 133 int sign = (m&1) ? -1 : 1;
Chris@16 134 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);
Chris@16 135 }
Chris@16 136 // Special cases:
Chris@16 137 if(m > l)
Chris@16 138 return 0;
Chris@16 139 if(m == 0)
Chris@16 140 return boost::math::legendre_p(l, x, pol);
Chris@16 141
Chris@16 142 T p0 = boost::math::double_factorial<T>(2 * m - 1, pol) * sin_theta_power;
Chris@16 143
Chris@16 144 if(m&1)
Chris@16 145 p0 *= -1;
Chris@16 146 if(m == l)
Chris@16 147 return p0;
Chris@16 148
Chris@16 149 T p1 = x * (2 * m + 1) * p0;
Chris@16 150
Chris@16 151 int n = m + 1;
Chris@16 152
Chris@16 153 while(n < l)
Chris@16 154 {
Chris@16 155 std::swap(p0, p1);
Chris@16 156 p1 = boost::math::legendre_next(n, m, x, p0, p1);
Chris@16 157 ++n;
Chris@16 158 }
Chris@16 159 return p1;
Chris@16 160 }
Chris@16 161
Chris@16 162 template <class T, class Policy>
Chris@16 163 inline T legendre_p_imp(int l, int m, T x, const Policy& pol)
Chris@16 164 {
Chris@16 165 BOOST_MATH_STD_USING
Chris@16 166 // TODO: we really could use that mythical "pow1p" function here:
Chris@16 167 return legendre_p_imp(l, m, x, static_cast<T>(pow(1 - x*x, T(abs(m))/2)), pol);
Chris@16 168 }
Chris@16 169
Chris@16 170 }
Chris@16 171
Chris@16 172 template <class T, class Policy>
Chris@16 173 inline typename tools::promote_args<T>::type
Chris@16 174 legendre_p(int l, int m, T x, const Policy& pol)
Chris@16 175 {
Chris@16 176 typedef typename tools::promote_args<T>::type result_type;
Chris@16 177 typedef typename policies::evaluation<result_type, Policy>::type value_type;
Chris@16 178 return policies::checked_narrowing_cast<result_type, Policy>(detail::legendre_p_imp(l, m, static_cast<value_type>(x), pol), "bost::math::legendre_p<%1%>(int, int, %1%)");
Chris@16 179 }
Chris@16 180
Chris@16 181 template <class T>
Chris@16 182 inline typename tools::promote_args<T>::type
Chris@16 183 legendre_p(int l, int m, T x)
Chris@16 184 {
Chris@16 185 return boost::math::legendre_p(l, m, x, policies::policy<>());
Chris@16 186 }
Chris@16 187
Chris@16 188 } // namespace math
Chris@16 189 } // namespace boost
Chris@16 190
Chris@16 191 #endif // BOOST_MATH_SPECIAL_LEGENDRE_HPP
Chris@16 192
Chris@16 193
Chris@16 194