legendre.hpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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 <utility>
  11. #include <vector>
  12. #include <boost/math/special_functions/math_fwd.hpp>
  13. #include <boost/math/special_functions/factorials.hpp>
  14. #include <boost/math/tools/roots.hpp>
  15. #include <boost/math/tools/config.hpp>
  16. #include <boost/math/tools/cxx03_warn.hpp>
  17. namespace boost{
  18. namespace math{
  19. // Recurrence relation for legendre P and Q polynomials:
  20. template <class T1, class T2, class T3>
  21. inline typename tools::promote_args<T1, T2, T3>::type
  22. legendre_next(unsigned l, T1 x, T2 Pl, T3 Plm1)
  23. {
  24. typedef typename tools::promote_args<T1, T2, T3>::type result_type;
  25. return ((2 * l + 1) * result_type(x) * result_type(Pl) - l * result_type(Plm1)) / (l + 1);
  26. }
  27. namespace detail{
  28. // Implement Legendre P and Q polynomials via recurrence:
  29. template <class T, class Policy>
  30. T legendre_imp(unsigned l, T x, const Policy& pol, bool second = false)
  31. {
  32. static const char* function = "boost::math::legrendre_p<%1%>(unsigned, %1%)";
  33. // Error handling:
  34. if((x < -1) || (x > 1))
  35. return policies::raise_domain_error<T>(
  36. function,
  37. "The Legendre Polynomial is defined for"
  38. " -1 <= x <= 1, but got x = %1%.", x, pol);
  39. T p0, p1;
  40. if(second)
  41. {
  42. // A solution of the second kind (Q):
  43. p0 = (boost::math::log1p(x, pol) - boost::math::log1p(-x, pol)) / 2;
  44. p1 = x * p0 - 1;
  45. }
  46. else
  47. {
  48. // A solution of the first kind (P):
  49. p0 = 1;
  50. p1 = x;
  51. }
  52. if(l == 0)
  53. return p0;
  54. unsigned n = 1;
  55. while(n < l)
  56. {
  57. std::swap(p0, p1);
  58. p1 = boost::math::legendre_next(n, x, p0, p1);
  59. ++n;
  60. }
  61. return p1;
  62. }
  63. template <class T, class Policy>
  64. T legendre_p_prime_imp(unsigned l, T x, const Policy& pol, T* Pn
  65. #ifdef BOOST_NO_CXX11_NULLPTR
  66. = 0
  67. #else
  68. = nullptr
  69. #endif
  70. )
  71. {
  72. static const char* function = "boost::math::legrendre_p_prime<%1%>(unsigned, %1%)";
  73. // Error handling:
  74. if ((x < -1) || (x > 1))
  75. return policies::raise_domain_error<T>(
  76. function,
  77. "The Legendre Polynomial is defined for"
  78. " -1 <= x <= 1, but got x = %1%.", x, pol);
  79. if (l == 0)
  80. {
  81. if (Pn)
  82. {
  83. *Pn = 1;
  84. }
  85. return 0;
  86. }
  87. T p0 = 1;
  88. T p1 = x;
  89. T p_prime;
  90. bool odd = l & 1;
  91. // If the order is odd, we sum all the even polynomials:
  92. if (odd)
  93. {
  94. p_prime = p0;
  95. }
  96. else // Otherwise we sum the odd polynomials * (2n+1)
  97. {
  98. p_prime = 3*p1;
  99. }
  100. unsigned n = 1;
  101. while(n < l - 1)
  102. {
  103. std::swap(p0, p1);
  104. p1 = boost::math::legendre_next(n, x, p0, p1);
  105. ++n;
  106. if (odd)
  107. {
  108. p_prime += (2*n+1)*p1;
  109. odd = false;
  110. }
  111. else
  112. {
  113. odd = true;
  114. }
  115. }
  116. // This allows us to evaluate the derivative and the function for the same cost.
  117. if (Pn)
  118. {
  119. std::swap(p0, p1);
  120. *Pn = boost::math::legendre_next(n, x, p0, p1);
  121. }
  122. return p_prime;
  123. }
  124. template <class T, class Policy>
  125. struct legendre_p_zero_func
  126. {
  127. int n;
  128. const Policy& pol;
  129. legendre_p_zero_func(int n_, const Policy& p) : n(n_), pol(p) {}
  130. std::pair<T, T> operator()(T x) const
  131. {
  132. T Pn;
  133. T Pn_prime = detail::legendre_p_prime_imp(n, x, pol, &Pn);
  134. return std::pair<T, T>(Pn, Pn_prime);
  135. };
  136. };
  137. template <class T, class Policy>
  138. std::vector<T> legendre_p_zeros_imp(int n, const Policy& pol)
  139. {
  140. using std::cos;
  141. using std::sin;
  142. using std::ceil;
  143. using std::sqrt;
  144. using boost::math::constants::pi;
  145. using boost::math::constants::half;
  146. using boost::math::tools::newton_raphson_iterate;
  147. BOOST_ASSERT(n >= 0);
  148. std::vector<T> zeros;
  149. if (n == 0)
  150. {
  151. // There are no zeros of P_0(x) = 1.
  152. return zeros;
  153. }
  154. int k;
  155. if (n & 1)
  156. {
  157. zeros.resize((n-1)/2 + 1, std::numeric_limits<T>::quiet_NaN());
  158. zeros[0] = 0;
  159. k = 1;
  160. }
  161. else
  162. {
  163. zeros.resize(n/2, std::numeric_limits<T>::quiet_NaN());
  164. k = 0;
  165. }
  166. T half_n = ceil(n*half<T>());
  167. while (k < (int)zeros.size())
  168. {
  169. // Bracket the root: Szego:
  170. // Gabriel Szego, Inequalities for the Zeros of Legendre Polynomials and Related Functions, Transactions of the American Mathematical Society, Vol. 39, No. 1 (1936)
  171. T theta_nk = ((half_n - half<T>()*half<T>() - static_cast<T>(k))*pi<T>())/(static_cast<T>(n)+half<T>());
  172. T lower_bound = cos( (half_n - static_cast<T>(k))*pi<T>()/static_cast<T>(n + 1));
  173. T cos_nk = cos(theta_nk);
  174. T upper_bound = cos_nk;
  175. // First guess follows from:
  176. // F. G. Tricomi, Sugli zeri dei polinomi sferici ed ultrasferici, Ann. Mat. Pura Appl., 31 (1950), pp. 93-97;
  177. T inv_n_sq = 1/static_cast<T>(n*n);
  178. T sin_nk = sin(theta_nk);
  179. 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;
  180. boost::uintmax_t number_of_iterations = policies::get_max_root_iterations<Policy>();
  181. legendre_p_zero_func<T, Policy> f(n, pol);
  182. const T x_nk = newton_raphson_iterate(f, x_nk_guess,
  183. lower_bound, upper_bound,
  184. policies::digits<T, Policy>(),
  185. number_of_iterations);
  186. BOOST_ASSERT(lower_bound < x_nk);
  187. BOOST_ASSERT(upper_bound > x_nk);
  188. zeros[k] = x_nk;
  189. ++k;
  190. }
  191. return zeros;
  192. }
  193. } // namespace detail
  194. template <class T, class Policy>
  195. inline typename boost::enable_if_c<policies::is_policy<Policy>::value, typename tools::promote_args<T>::type>::type
  196. legendre_p(int l, T x, const Policy& pol)
  197. {
  198. typedef typename tools::promote_args<T>::type result_type;
  199. typedef typename policies::evaluation<result_type, Policy>::type value_type;
  200. static const char* function = "boost::math::legendre_p<%1%>(unsigned, %1%)";
  201. if(l < 0)
  202. return policies::checked_narrowing_cast<result_type, Policy>(detail::legendre_imp(-l-1, static_cast<value_type>(x), pol, false), function);
  203. return policies::checked_narrowing_cast<result_type, Policy>(detail::legendre_imp(l, static_cast<value_type>(x), pol, false), function);
  204. }
  205. template <class T, class Policy>
  206. inline typename boost::enable_if_c<policies::is_policy<Policy>::value, typename tools::promote_args<T>::type>::type
  207. legendre_p_prime(int l, T x, const Policy& pol)
  208. {
  209. typedef typename tools::promote_args<T>::type result_type;
  210. typedef typename policies::evaluation<result_type, Policy>::type value_type;
  211. static const char* function = "boost::math::legendre_p_prime<%1%>(unsigned, %1%)";
  212. if(l < 0)
  213. return policies::checked_narrowing_cast<result_type, Policy>(detail::legendre_p_prime_imp(-l-1, static_cast<value_type>(x), pol), function);
  214. return policies::checked_narrowing_cast<result_type, Policy>(detail::legendre_p_prime_imp(l, static_cast<value_type>(x), pol), function);
  215. }
  216. template <class T>
  217. inline typename tools::promote_args<T>::type
  218. legendre_p(int l, T x)
  219. {
  220. return boost::math::legendre_p(l, x, policies::policy<>());
  221. }
  222. template <class T>
  223. inline typename tools::promote_args<T>::type
  224. legendre_p_prime(int l, T x)
  225. {
  226. return boost::math::legendre_p_prime(l, x, policies::policy<>());
  227. }
  228. template <class T, class Policy>
  229. inline std::vector<T> legendre_p_zeros(int l, const Policy& pol)
  230. {
  231. if(l < 0)
  232. return detail::legendre_p_zeros_imp<T>(-l-1, pol);
  233. return detail::legendre_p_zeros_imp<T>(l, pol);
  234. }
  235. template <class T>
  236. inline std::vector<T> legendre_p_zeros(int l)
  237. {
  238. return boost::math::legendre_p_zeros<T>(l, policies::policy<>());
  239. }
  240. template <class T, class Policy>
  241. inline typename boost::enable_if_c<policies::is_policy<Policy>::value, typename tools::promote_args<T>::type>::type
  242. legendre_q(unsigned l, T x, const Policy& pol)
  243. {
  244. typedef typename tools::promote_args<T>::type result_type;
  245. typedef typename policies::evaluation<result_type, Policy>::type value_type;
  246. 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%)");
  247. }
  248. template <class T>
  249. inline typename tools::promote_args<T>::type
  250. legendre_q(unsigned l, T x)
  251. {
  252. return boost::math::legendre_q(l, x, policies::policy<>());
  253. }
  254. // Recurrence for associated polynomials:
  255. template <class T1, class T2, class T3>
  256. inline typename tools::promote_args<T1, T2, T3>::type
  257. legendre_next(unsigned l, unsigned m, T1 x, T2 Pl, T3 Plm1)
  258. {
  259. typedef typename tools::promote_args<T1, T2, T3>::type result_type;
  260. return ((2 * l + 1) * result_type(x) * result_type(Pl) - (l + m) * result_type(Plm1)) / (l + 1 - m);
  261. }
  262. namespace detail{
  263. // Legendre P associated polynomial:
  264. template <class T, class Policy>
  265. T legendre_p_imp(int l, int m, T x, T sin_theta_power, const Policy& pol)
  266. {
  267. // Error handling:
  268. if((x < -1) || (x > 1))
  269. return policies::raise_domain_error<T>(
  270. "boost::math::legendre_p<%1%>(int, int, %1%)",
  271. "The associated Legendre Polynomial is defined for"
  272. " -1 <= x <= 1, but got x = %1%.", x, pol);
  273. // Handle negative arguments first:
  274. if(l < 0)
  275. return legendre_p_imp(-l-1, m, x, sin_theta_power, pol);
  276. if(m < 0)
  277. {
  278. int sign = (m&1) ? -1 : 1;
  279. 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);
  280. }
  281. // Special cases:
  282. if(m > l)
  283. return 0;
  284. if(m == 0)
  285. return boost::math::legendre_p(l, x, pol);
  286. T p0 = boost::math::double_factorial<T>(2 * m - 1, pol) * sin_theta_power;
  287. if(m&1)
  288. p0 *= -1;
  289. if(m == l)
  290. return p0;
  291. T p1 = x * (2 * m + 1) * p0;
  292. int n = m + 1;
  293. while(n < l)
  294. {
  295. std::swap(p0, p1);
  296. p1 = boost::math::legendre_next(n, m, x, p0, p1);
  297. ++n;
  298. }
  299. return p1;
  300. }
  301. template <class T, class Policy>
  302. inline T legendre_p_imp(int l, int m, T x, const Policy& pol)
  303. {
  304. BOOST_MATH_STD_USING
  305. // TODO: we really could use that mythical "pow1p" function here:
  306. return legendre_p_imp(l, m, x, static_cast<T>(pow(1 - x*x, T(abs(m))/2)), pol);
  307. }
  308. }
  309. template <class T, class Policy>
  310. inline typename tools::promote_args<T>::type
  311. legendre_p(int l, int m, T x, const Policy& pol)
  312. {
  313. typedef typename tools::promote_args<T>::type result_type;
  314. typedef typename policies::evaluation<result_type, Policy>::type value_type;
  315. 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%)");
  316. }
  317. template <class T>
  318. inline typename tools::promote_args<T>::type
  319. legendre_p(int l, int m, T x)
  320. {
  321. return boost::math::legendre_p(l, m, x, policies::policy<>());
  322. }
  323. } // namespace math
  324. } // namespace boost
  325. #endif // BOOST_MATH_SPECIAL_LEGENDRE_HPP