legendre.hpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  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 = static_cast<T>(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) == 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 = static_cast<T>(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 = static_cast<T>(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_MATH_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. std::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. if (number_of_iterations >= policies::get_max_root_iterations<Policy>())
  188. {
  189. policies::raise_evaluation_error<T>("legendre_p_zeros<%1%>", "Unable to locate solution in a reasonable time:" // LCOV_EXCL_LINE
  190. " either there is no answer or the answer is infinite. Current best guess is %1%", x_nk, Policy()); // LCOV_EXCL_LINE
  191. }
  192. BOOST_MATH_ASSERT(lower_bound < x_nk);
  193. BOOST_MATH_ASSERT(upper_bound > x_nk);
  194. zeros[k] = x_nk;
  195. ++k;
  196. }
  197. return zeros;
  198. }
  199. } // namespace detail
  200. template <class T, class Policy>
  201. inline typename std::enable_if<policies::is_policy<Policy>::value, typename tools::promote_args<T>::type>::type
  202. legendre_p(int l, T x, const Policy& pol)
  203. {
  204. typedef typename tools::promote_args<T>::type result_type;
  205. typedef typename policies::evaluation<result_type, Policy>::type value_type;
  206. static const char* function = "boost::math::legendre_p<%1%>(unsigned, %1%)";
  207. if(l < 0)
  208. return policies::checked_narrowing_cast<result_type, Policy>(detail::legendre_imp(-l-1, static_cast<value_type>(x), pol, false), function);
  209. return policies::checked_narrowing_cast<result_type, Policy>(detail::legendre_imp(l, static_cast<value_type>(x), pol, false), function);
  210. }
  211. template <class T, class Policy>
  212. inline typename std::enable_if<policies::is_policy<Policy>::value, typename tools::promote_args<T>::type>::type
  213. legendre_p_prime(int l, T x, const Policy& pol)
  214. {
  215. typedef typename tools::promote_args<T>::type result_type;
  216. typedef typename policies::evaluation<result_type, Policy>::type value_type;
  217. static const char* function = "boost::math::legendre_p_prime<%1%>(unsigned, %1%)";
  218. if(l < 0)
  219. return policies::checked_narrowing_cast<result_type, Policy>(detail::legendre_p_prime_imp(-l-1, static_cast<value_type>(x), pol), function);
  220. return policies::checked_narrowing_cast<result_type, Policy>(detail::legendre_p_prime_imp(l, static_cast<value_type>(x), pol), function);
  221. }
  222. template <class T>
  223. inline typename tools::promote_args<T>::type
  224. legendre_p(int l, T x)
  225. {
  226. return boost::math::legendre_p(l, x, policies::policy<>());
  227. }
  228. template <class T>
  229. inline typename tools::promote_args<T>::type
  230. legendre_p_prime(int l, T x)
  231. {
  232. return boost::math::legendre_p_prime(l, x, policies::policy<>());
  233. }
  234. template <class T, class Policy>
  235. inline std::vector<T> legendre_p_zeros(int l, const Policy& pol)
  236. {
  237. if(l < 0)
  238. return detail::legendre_p_zeros_imp<T>(-l-1, pol);
  239. return detail::legendre_p_zeros_imp<T>(l, pol);
  240. }
  241. template <class T>
  242. inline std::vector<T> legendre_p_zeros(int l)
  243. {
  244. return boost::math::legendre_p_zeros<T>(l, policies::policy<>());
  245. }
  246. template <class T, class Policy>
  247. inline typename std::enable_if<policies::is_policy<Policy>::value, typename tools::promote_args<T>::type>::type
  248. legendre_q(unsigned l, T x, const Policy& pol)
  249. {
  250. typedef typename tools::promote_args<T>::type result_type;
  251. typedef typename policies::evaluation<result_type, Policy>::type value_type;
  252. 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%)");
  253. }
  254. template <class T>
  255. inline typename tools::promote_args<T>::type
  256. legendre_q(unsigned l, T x)
  257. {
  258. return boost::math::legendre_q(l, x, policies::policy<>());
  259. }
  260. // Recurrence for associated polynomials:
  261. template <class T1, class T2, class T3>
  262. inline typename tools::promote_args<T1, T2, T3>::type
  263. legendre_next(unsigned l, unsigned m, T1 x, T2 Pl, T3 Plm1)
  264. {
  265. typedef typename tools::promote_args<T1, T2, T3>::type result_type;
  266. return ((2 * l + 1) * result_type(x) * result_type(Pl) - (l + m) * result_type(Plm1)) / (l + 1 - m);
  267. }
  268. namespace detail{
  269. // Legendre P associated polynomial:
  270. template <class T, class Policy>
  271. T legendre_p_imp(int l, int m, T x, T sin_theta_power, const Policy& pol)
  272. {
  273. BOOST_MATH_STD_USING
  274. // Error handling:
  275. if((x < -1) || (x > 1))
  276. return policies::raise_domain_error<T>(
  277. "boost::math::legendre_p<%1%>(int, int, %1%)",
  278. "The associated Legendre Polynomial is defined for"
  279. " -1 <= x <= 1, but got x = %1%.", x, pol);
  280. // Handle negative arguments first:
  281. if(l < 0)
  282. return legendre_p_imp(-l-1, m, x, sin_theta_power, pol);
  283. if ((l == 0) && (m == -1))
  284. {
  285. return sqrt((1 - x) / (1 + x));
  286. }
  287. if ((l == 1) && (m == 0))
  288. {
  289. return x;
  290. }
  291. if (-m == l)
  292. {
  293. return pow((1 - x * x) / 4, T(l) / 2) / boost::math::tgamma<T>(l + 1, pol);
  294. }
  295. if(m < 0)
  296. {
  297. int sign = (m&1) ? -1 : 1;
  298. 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);
  299. }
  300. // Special cases:
  301. if(m > l)
  302. return 0;
  303. if(m == 0)
  304. return boost::math::legendre_p(l, x, pol);
  305. T p0 = boost::math::double_factorial<T>(2 * m - 1, pol) * sin_theta_power;
  306. if(m&1)
  307. p0 *= -1;
  308. if(m == l)
  309. return p0;
  310. T p1 = x * (2 * m + 1) * p0;
  311. int n = m + 1;
  312. while(n < l)
  313. {
  314. std::swap(p0, p1);
  315. p1 = boost::math::legendre_next(n, m, x, p0, p1);
  316. ++n;
  317. }
  318. return p1;
  319. }
  320. template <class T, class Policy>
  321. inline T legendre_p_imp(int l, int m, T x, const Policy& pol)
  322. {
  323. BOOST_MATH_STD_USING
  324. // TODO: we really could use that mythical "pow1p" function here:
  325. return legendre_p_imp(l, m, x, static_cast<T>(pow(1 - x*x, T(abs(m))/2)), pol);
  326. }
  327. }
  328. template <class T, class Policy>
  329. inline typename tools::promote_args<T>::type
  330. legendre_p(int l, int m, T x, const Policy& pol)
  331. {
  332. typedef typename tools::promote_args<T>::type result_type;
  333. typedef typename policies::evaluation<result_type, Policy>::type value_type;
  334. 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%)");
  335. }
  336. template <class T>
  337. inline typename tools::promote_args<T>::type
  338. legendre_p(int l, int m, T x)
  339. {
  340. return boost::math::legendre_p(l, m, x, policies::policy<>());
  341. }
  342. } // namespace math
  343. } // namespace boost
  344. #endif // BOOST_MATH_SPECIAL_LEGENDRE_HPP