legendre.hpp 12 KB

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