legendre.hpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. // (C) Copyright John Maddock 2006.
  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)
  5. #ifndef BOOST_MATH_SPECIAL_LEGENDRE_HPP
  6. #define BOOST_MATH_SPECIAL_LEGENDRE_HPP
  7. #ifdef _MSC_VER
  8. #pragma once
  9. #endif
  10. #include <boost/math/special_functions/math_fwd.hpp>
  11. #include <boost/math/special_functions/factorials.hpp>
  12. #include <boost/math/tools/config.hpp>
  13. namespace boost{
  14. namespace math{
  15. // Recurrance relation for legendre P and Q polynomials:
  16. template <class T1, class T2, class T3>
  17. inline typename tools::promote_args<T1, T2, T3>::type
  18. legendre_next(unsigned l, T1 x, T2 Pl, T3 Plm1)
  19. {
  20. typedef typename tools::promote_args<T1, T2, T3>::type result_type;
  21. return ((2 * l + 1) * result_type(x) * result_type(Pl) - l * result_type(Plm1)) / (l + 1);
  22. }
  23. namespace detail{
  24. // Implement Legendre P and Q polynomials via recurrance:
  25. template <class T, class Policy>
  26. T legendre_imp(unsigned l, T x, const Policy& pol, bool second = false)
  27. {
  28. static const char* function = "boost::math::legrendre_p<%1%>(unsigned, %1%)";
  29. // Error handling:
  30. if((x < -1) || (x > 1))
  31. return policies::raise_domain_error<T>(
  32. function,
  33. "The Legendre Polynomial is defined for"
  34. " -1 <= x <= 1, but got x = %1%.", x, pol);
  35. T p0, p1;
  36. if(second)
  37. {
  38. // A solution of the second kind (Q):
  39. p0 = (boost::math::log1p(x, pol) - boost::math::log1p(-x, pol)) / 2;
  40. p1 = x * p0 - 1;
  41. }
  42. else
  43. {
  44. // A solution of the first kind (P):
  45. p0 = 1;
  46. p1 = x;
  47. }
  48. if(l == 0)
  49. return p0;
  50. unsigned n = 1;
  51. while(n < l)
  52. {
  53. std::swap(p0, p1);
  54. p1 = boost::math::legendre_next(n, x, p0, p1);
  55. ++n;
  56. }
  57. return p1;
  58. }
  59. } // namespace detail
  60. template <class T, class Policy>
  61. inline typename tools::promote_args<T>::type
  62. legendre_p(int l, T x, const Policy& pol)
  63. {
  64. typedef typename tools::promote_args<T>::type result_type;
  65. typedef typename policies::evaluation<result_type, Policy>::type value_type;
  66. static const char* function = "boost::math::legendre_p<%1%>(unsigned, %1%)";
  67. if(l < 0)
  68. return policies::checked_narrowing_cast<result_type, Policy>(detail::legendre_imp(-l-1, static_cast<value_type>(x), pol, false), function);
  69. return policies::checked_narrowing_cast<result_type, Policy>(detail::legendre_imp(l, static_cast<value_type>(x), pol, false), function);
  70. }
  71. template <class T>
  72. inline typename tools::promote_args<T>::type
  73. legendre_p(int l, T x)
  74. {
  75. return boost::math::legendre_p(l, x, policies::policy<>());
  76. }
  77. template <class T, class Policy>
  78. inline typename tools::promote_args<T>::type
  79. legendre_q(unsigned l, T x, const Policy& pol)
  80. {
  81. typedef typename tools::promote_args<T>::type result_type;
  82. typedef typename policies::evaluation<result_type, Policy>::type value_type;
  83. 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%)");
  84. }
  85. template <class T>
  86. inline typename tools::promote_args<T>::type
  87. legendre_q(unsigned l, T x)
  88. {
  89. return boost::math::legendre_q(l, x, policies::policy<>());
  90. }
  91. // Recurrence for associated polynomials:
  92. template <class T1, class T2, class T3>
  93. inline typename tools::promote_args<T1, T2, T3>::type
  94. legendre_next(unsigned l, unsigned m, T1 x, T2 Pl, T3 Plm1)
  95. {
  96. typedef typename tools::promote_args<T1, T2, T3>::type result_type;
  97. return ((2 * l + 1) * result_type(x) * result_type(Pl) - (l + m) * result_type(Plm1)) / (l + 1 - m);
  98. }
  99. namespace detail{
  100. // Legendre P associated polynomial:
  101. template <class T, class Policy>
  102. T legendre_p_imp(int l, int m, T x, T sin_theta_power, const Policy& pol)
  103. {
  104. // Error handling:
  105. if((x < -1) || (x > 1))
  106. return policies::raise_domain_error<T>(
  107. "boost::math::legendre_p<%1%>(int, int, %1%)",
  108. "The associated Legendre Polynomial is defined for"
  109. " -1 <= x <= 1, but got x = %1%.", x, pol);
  110. // Handle negative arguments first:
  111. if(l < 0)
  112. return legendre_p_imp(-l-1, m, x, sin_theta_power, pol);
  113. if(m < 0)
  114. {
  115. int sign = (m&1) ? -1 : 1;
  116. 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);
  117. }
  118. // Special cases:
  119. if(m > l)
  120. return 0;
  121. if(m == 0)
  122. return boost::math::legendre_p(l, x, pol);
  123. T p0 = boost::math::double_factorial<T>(2 * m - 1, pol) * sin_theta_power;
  124. if(m&1)
  125. p0 *= -1;
  126. if(m == l)
  127. return p0;
  128. T p1 = x * (2 * m + 1) * p0;
  129. int n = m + 1;
  130. while(n < l)
  131. {
  132. std::swap(p0, p1);
  133. p1 = boost::math::legendre_next(n, m, x, p0, p1);
  134. ++n;
  135. }
  136. return p1;
  137. }
  138. template <class T, class Policy>
  139. inline T legendre_p_imp(int l, int m, T x, const Policy& pol)
  140. {
  141. BOOST_MATH_STD_USING
  142. // TODO: we really could use that mythical "pow1p" function here:
  143. return legendre_p_imp(l, m, x, static_cast<T>(pow(1 - x*x, T(abs(m))/2)), pol);
  144. }
  145. }
  146. template <class T, class Policy>
  147. inline typename tools::promote_args<T>::type
  148. legendre_p(int l, int m, T x, const Policy& pol)
  149. {
  150. typedef typename tools::promote_args<T>::type result_type;
  151. typedef typename policies::evaluation<result_type, Policy>::type value_type;
  152. 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%)");
  153. }
  154. template <class T>
  155. inline typename tools::promote_args<T>::type
  156. legendre_p(int l, int m, T x)
  157. {
  158. return boost::math::legendre_p(l, m, x, policies::policy<>());
  159. }
  160. } // namespace math
  161. } // namespace boost
  162. #endif // BOOST_MATH_SPECIAL_LEGENDRE_HPP