legendre.hpp 11 KB

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