legendre.hpp 11 KB

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