legendre_stieltjes.hpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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. if ((n & 1) == 1)
  37. {
  38. q = 1;
  39. r = (n-1)/2 + 2;
  40. }
  41. else
  42. {
  43. q = 0;
  44. r = n/2 + 1;
  45. }
  46. m_a.resize(r + 1);
  47. // We'll keep the ones-based indexing at the cost of storing a superfluous element
  48. // so that we can follow Patterson's notation exactly.
  49. m_a[r] = static_cast<Real>(1);
  50. // Make sure using the zero index is a bug:
  51. m_a[0] = std::numeric_limits<Real>::quiet_NaN();
  52. for (std::ptrdiff_t k = 1; k < r; ++k)
  53. {
  54. Real ratio = 1;
  55. m_a[r - k] = 0;
  56. for (std::ptrdiff_t i = r + 1 - k; i <= r; ++i)
  57. {
  58. // See Patterson, equation 12
  59. 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);
  60. 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));
  61. ratio *= static_cast<Real>(num)/static_cast<Real>(den);
  62. m_a[r - k] -= ratio*m_a[i];
  63. }
  64. }
  65. }
  66. Real norm_sq() const
  67. {
  68. Real t = 0;
  69. bool odd = ((m_m & 1) == 1);
  70. for (size_t i = 1; i < m_a.size(); ++i)
  71. {
  72. if(odd)
  73. {
  74. t += 2*m_a[i]*m_a[i]/static_cast<Real>(4*i-1);
  75. }
  76. else
  77. {
  78. t += 2*m_a[i]*m_a[i]/static_cast<Real>(4*i-3);
  79. }
  80. }
  81. return t;
  82. }
  83. Real operator()(Real x) const
  84. {
  85. // Trivial implementation:
  86. // Em += m_a[i]*legendre_p(2*i - 1, x); m odd
  87. // Em += m_a[i]*legendre_p(2*i - 2, x); m even
  88. size_t r = m_a.size() - 1;
  89. Real p0 = 1;
  90. Real p1 = x;
  91. Real Em;
  92. bool odd = ((m_m & 1) == 1);
  93. if (odd)
  94. {
  95. Em = m_a[1]*p1;
  96. }
  97. else
  98. {
  99. Em = m_a[1]*p0;
  100. }
  101. unsigned n = 1;
  102. for (size_t i = 2; i <= r; ++i)
  103. {
  104. std::swap(p0, p1);
  105. p1 = boost::math::legendre_next(n, x, p0, p1);
  106. ++n;
  107. if (!odd)
  108. {
  109. Em += m_a[i]*p1;
  110. }
  111. std::swap(p0, p1);
  112. p1 = boost::math::legendre_next(n, x, p0, p1);
  113. ++n;
  114. if(odd)
  115. {
  116. Em += m_a[i]*p1;
  117. }
  118. }
  119. return Em;
  120. }
  121. Real prime(Real x) const
  122. {
  123. Real Em_prime = 0;
  124. for (size_t i = 1; i < m_a.size(); ++i)
  125. {
  126. if(m_m & 1)
  127. {
  128. Em_prime += m_a[i]*detail::legendre_p_prime_imp(static_cast<unsigned>(2*i - 1), x, policies::policy<>());
  129. }
  130. else
  131. {
  132. Em_prime += m_a[i]*detail::legendre_p_prime_imp(static_cast<unsigned>(2*i - 2), x, policies::policy<>());
  133. }
  134. }
  135. return Em_prime;
  136. }
  137. std::vector<Real> zeros() const
  138. {
  139. using boost::math::constants::half;
  140. std::vector<Real> stieltjes_zeros;
  141. std::vector<Real> legendre_zeros = legendre_p_zeros<Real>(m_m - 1);
  142. size_t k;
  143. if (m_m & 1)
  144. {
  145. stieltjes_zeros.resize(legendre_zeros.size() + 1, std::numeric_limits<Real>::quiet_NaN());
  146. stieltjes_zeros[0] = 0;
  147. k = 1;
  148. }
  149. else
  150. {
  151. stieltjes_zeros.resize(legendre_zeros.size(), std::numeric_limits<Real>::quiet_NaN());
  152. k = 0;
  153. }
  154. while (k < stieltjes_zeros.size())
  155. {
  156. Real lower_bound;
  157. Real upper_bound;
  158. if (m_m & 1)
  159. {
  160. lower_bound = legendre_zeros[k - 1];
  161. if (k == legendre_zeros.size())
  162. {
  163. upper_bound = 1;
  164. }
  165. else
  166. {
  167. upper_bound = legendre_zeros[k];
  168. }
  169. }
  170. else
  171. {
  172. lower_bound = legendre_zeros[k];
  173. if (k == legendre_zeros.size() - 1)
  174. {
  175. upper_bound = 1;
  176. }
  177. else
  178. {
  179. upper_bound = legendre_zeros[k+1];
  180. }
  181. }
  182. // The root bracketing is not very tight; to keep weird stuff from happening
  183. // in the Newton's method, let's tighten up the tolerance using a few bisections.
  184. boost::math::tools::eps_tolerance<Real> tol(6);
  185. auto g = [&](Real t) { return this->operator()(t); };
  186. auto p = boost::math::tools::bisect(g, lower_bound, upper_bound, tol);
  187. Real x_nk_guess = p.first + (p.second - p.first)*half<Real>();
  188. std::uintmax_t number_of_iterations = 500;
  189. auto f = [&] (Real x) { Real Pn = this->operator()(x);
  190. Real Pn_prime = this->prime(x);
  191. return std::pair<Real, Real>(Pn, Pn_prime); };
  192. const Real x_nk = boost::math::tools::newton_raphson_iterate(f, x_nk_guess,
  193. p.first, p.second,
  194. tools::digits<Real>(),
  195. number_of_iterations);
  196. BOOST_MATH_ASSERT(p.first < x_nk);
  197. BOOST_MATH_ASSERT(x_nk < p.second);
  198. stieltjes_zeros[k] = x_nk;
  199. ++k;
  200. }
  201. return stieltjes_zeros;
  202. }
  203. private:
  204. // Coefficients of Legendre expansion
  205. std::vector<Real> m_a;
  206. int m_m;
  207. };
  208. }}
  209. #endif