legendre_stieltjes.hpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. // Copyright Nick Thompson 2017.
  2. // Use, modification and distribution are subject to the
  3. // Boost Software License, Version 1.0.
  4. // (See accompanying file LICENSE_1_0.txt
  5. // or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. #ifndef BOOST_MATH_SPECIAL_LEGENDRE_STIELTJES_HPP
  7. #define BOOST_MATH_SPECIAL_LEGENDRE_STIELTJES_HPP
  8. /*
  9. * Constructs the Legendre-Stieltjes polynomial of degree m.
  10. * The Legendre-Stieltjes polynomials are used to create extensions for Gaussian quadratures,
  11. * commonly called "Gauss-Konrod" quadratures.
  12. *
  13. * References:
  14. * Patterson, TNL. "The optimum addition of points to quadrature formulae." Mathematics of Computation 22.104 (1968): 847-856.
  15. */
  16. #include <iostream>
  17. #include <vector>
  18. #include <boost/math/tools/roots.hpp>
  19. #include <boost/math/special_functions/legendre.hpp>
  20. namespace boost{
  21. namespace math{
  22. template<class Real>
  23. class legendre_stieltjes
  24. {
  25. public:
  26. legendre_stieltjes(size_t m)
  27. {
  28. if (m == 0)
  29. {
  30. throw std::domain_error("The Legendre-Stieltjes polynomial is defined for order m > 0.\n");
  31. }
  32. m_m = static_cast<int>(m);
  33. std::ptrdiff_t n = m - 1;
  34. std::ptrdiff_t q;
  35. std::ptrdiff_t r;
  36. bool odd = n & 1;
  37. if (odd)
  38. {
  39. q = 1;
  40. r = (n-1)/2 + 2;
  41. }
  42. else
  43. {
  44. q = 0;
  45. r = n/2 + 1;
  46. }
  47. m_a.resize(r + 1);
  48. // We'll keep the ones-based indexing at the cost of storing a superfluous element
  49. // so that we can follow Patterson's notation exactly.
  50. m_a[r] = static_cast<Real>(1);
  51. // Make sure using the zero index is a bug:
  52. m_a[0] = std::numeric_limits<Real>::quiet_NaN();
  53. for (std::ptrdiff_t k = 1; k < r; ++k)
  54. {
  55. Real ratio = 1;
  56. m_a[r - k] = 0;
  57. for (std::ptrdiff_t i = r + 1 - k; i <= r; ++i)
  58. {
  59. // See Patterson, equation 12
  60. std::ptrdiff_t num = (n - q + 2*(i + k - 1))*(n + q + 2*(k - i + 1))*(n-1-q+2*(i-k))*(2*(k+i-1) -1 -q -n);
  61. std::ptrdiff_t den = (n - q + 2*(i - k))*(2*(k + i - 1) - q - n)*(n + 1 + q + 2*(k - i))*(n - 1 - q + 2*(i + k));
  62. ratio *= static_cast<Real>(num)/static_cast<Real>(den);
  63. m_a[r - k] -= ratio*m_a[i];
  64. }
  65. }
  66. }
  67. Real norm_sq() const
  68. {
  69. Real t = 0;
  70. bool odd = m_m & 1;
  71. for (size_t i = 1; i < m_a.size(); ++i)
  72. {
  73. if(odd)
  74. {
  75. t += 2*m_a[i]*m_a[i]/static_cast<Real>(4*i-1);
  76. }
  77. else
  78. {
  79. t += 2*m_a[i]*m_a[i]/static_cast<Real>(4*i-3);
  80. }
  81. }
  82. return t;
  83. }
  84. Real operator()(Real x) const
  85. {
  86. // Trivial implementation:
  87. // Em += m_a[i]*legendre_p(2*i - 1, x); m odd
  88. // Em += m_a[i]*legendre_p(2*i - 2, x); m even
  89. size_t r = m_a.size() - 1;
  90. Real p0 = 1;
  91. Real p1 = x;
  92. Real Em;
  93. bool odd = m_m & 1;
  94. if (odd)
  95. {
  96. Em = m_a[1]*p1;
  97. }
  98. else
  99. {
  100. Em = m_a[1]*p0;
  101. }
  102. unsigned n = 1;
  103. for (size_t i = 2; i <= r; ++i)
  104. {
  105. std::swap(p0, p1);
  106. p1 = boost::math::legendre_next(n, x, p0, p1);
  107. ++n;
  108. if (!odd)
  109. {
  110. Em += m_a[i]*p1;
  111. }
  112. std::swap(p0, p1);
  113. p1 = boost::math::legendre_next(n, x, p0, p1);
  114. ++n;
  115. if(odd)
  116. {
  117. Em += m_a[i]*p1;
  118. }
  119. }
  120. return Em;
  121. }
  122. Real prime(Real x) const
  123. {
  124. Real Em_prime = 0;
  125. for (size_t i = 1; i < m_a.size(); ++i)
  126. {
  127. if(m_m & 1)
  128. {
  129. Em_prime += m_a[i]*detail::legendre_p_prime_imp(static_cast<unsigned>(2*i - 1), x, policies::policy<>());
  130. }
  131. else
  132. {
  133. Em_prime += m_a[i]*detail::legendre_p_prime_imp(static_cast<unsigned>(2*i - 2), x, policies::policy<>());
  134. }
  135. }
  136. return Em_prime;
  137. }
  138. std::vector<Real> zeros() const
  139. {
  140. using boost::math::constants::half;
  141. std::vector<Real> stieltjes_zeros;
  142. std::vector<Real> legendre_zeros = legendre_p_zeros<Real>(m_m - 1);
  143. int k;
  144. if (m_m & 1)
  145. {
  146. stieltjes_zeros.resize(legendre_zeros.size() + 1, std::numeric_limits<Real>::quiet_NaN());
  147. stieltjes_zeros[0] = 0;
  148. k = 1;
  149. }
  150. else
  151. {
  152. stieltjes_zeros.resize(legendre_zeros.size(), std::numeric_limits<Real>::quiet_NaN());
  153. k = 0;
  154. }
  155. while (k < (int)stieltjes_zeros.size())
  156. {
  157. Real lower_bound;
  158. Real upper_bound;
  159. if (m_m & 1)
  160. {
  161. lower_bound = legendre_zeros[k - 1];
  162. if (k == (int)legendre_zeros.size())
  163. {
  164. upper_bound = 1;
  165. }
  166. else
  167. {
  168. upper_bound = legendre_zeros[k];
  169. }
  170. }
  171. else
  172. {
  173. lower_bound = legendre_zeros[k];
  174. if (k == (int)legendre_zeros.size() - 1)
  175. {
  176. upper_bound = 1;
  177. }
  178. else
  179. {
  180. upper_bound = legendre_zeros[k+1];
  181. }
  182. }
  183. // The root bracketing is not very tight; to keep weird stuff from happening
  184. // in the Newton's method, let's tighten up the tolerance using a few bisections.
  185. boost::math::tools::eps_tolerance<Real> tol(6);
  186. auto g = [&](Real t) { return this->operator()(t); };
  187. auto p = boost::math::tools::bisect(g, lower_bound, upper_bound, tol);
  188. Real x_nk_guess = p.first + (p.second - p.first)*half<Real>();
  189. boost::uintmax_t number_of_iterations = 500;
  190. auto f = [&] (Real x) { Real Pn = this->operator()(x);
  191. Real Pn_prime = this->prime(x);
  192. return std::pair<Real, Real>(Pn, Pn_prime); };
  193. const Real x_nk = boost::math::tools::newton_raphson_iterate(f, x_nk_guess,
  194. p.first, p.second,
  195. tools::digits<Real>(),
  196. number_of_iterations);
  197. BOOST_ASSERT(p.first < x_nk);
  198. BOOST_ASSERT(x_nk < p.second);
  199. stieltjes_zeros[k] = x_nk;
  200. ++k;
  201. }
  202. return stieltjes_zeros;
  203. }
  204. private:
  205. // Coefficients of Legendre expansion
  206. std::vector<Real> m_a;
  207. int m_m;
  208. };
  209. }}
  210. #endif