barycentric_rational_detail.hpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /*
  2. * Copyright Nick Thompson, 2017
  3. * Use, modification and distribution are subject to the
  4. * Boost Software License, Version 1.0. (See accompanying file
  5. * LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. */
  7. #ifndef BOOST_MATH_INTERPOLATORS_BARYCENTRIC_RATIONAL_DETAIL_HPP
  8. #define BOOST_MATH_INTERPOLATORS_BARYCENTRIC_RATIONAL_DETAIL_HPP
  9. #include <vector>
  10. #include <utility> // for std::move
  11. #include <algorithm> // for std::is_sorted
  12. #include <string>
  13. #include <boost/math/special_functions/fpclassify.hpp>
  14. #include <boost/math/tools/assert.hpp>
  15. namespace boost{ namespace math{ namespace interpolators { namespace detail{
  16. template<class Real>
  17. class barycentric_rational_imp
  18. {
  19. public:
  20. template <class InputIterator1, class InputIterator2>
  21. barycentric_rational_imp(InputIterator1 start_x, InputIterator1 end_x, InputIterator2 start_y, size_t approximation_order = 3);
  22. barycentric_rational_imp(std::vector<Real>&& x, std::vector<Real>&& y, size_t approximation_order = 3);
  23. Real operator()(Real x) const;
  24. Real prime(Real x) const;
  25. // The barycentric weights are not really that interesting; except to the unit tests!
  26. Real weight(size_t i) const { return m_w[i]; }
  27. std::vector<Real>&& return_x()
  28. {
  29. return std::move(m_x);
  30. }
  31. std::vector<Real>&& return_y()
  32. {
  33. return std::move(m_y);
  34. }
  35. private:
  36. void calculate_weights(size_t approximation_order);
  37. std::vector<Real> m_x;
  38. std::vector<Real> m_y;
  39. std::vector<Real> m_w;
  40. };
  41. template <class Real>
  42. template <class InputIterator1, class InputIterator2>
  43. barycentric_rational_imp<Real>::barycentric_rational_imp(InputIterator1 start_x, InputIterator1 end_x, InputIterator2 start_y, size_t approximation_order)
  44. {
  45. std::ptrdiff_t n = std::distance(start_x, end_x);
  46. if (approximation_order >= (std::size_t)n)
  47. {
  48. throw std::domain_error("Approximation order must be < data length.");
  49. }
  50. // Big sad memcpy.
  51. m_x.resize(n);
  52. m_y.resize(n);
  53. for(unsigned i = 0; start_x != end_x; ++start_x, ++start_y, ++i)
  54. {
  55. // But if we're going to do a memcpy, we can do some error checking which is inexpensive relative to the copy:
  56. if(boost::math::isnan(*start_x))
  57. {
  58. std::string msg = std::string("x[") + std::to_string(i) + "] is a NAN";
  59. throw std::domain_error(msg);
  60. }
  61. if(boost::math::isnan(*start_y))
  62. {
  63. std::string msg = std::string("y[") + std::to_string(i) + "] is a NAN";
  64. throw std::domain_error(msg);
  65. }
  66. m_x[i] = *start_x;
  67. m_y[i] = *start_y;
  68. }
  69. calculate_weights(approximation_order);
  70. }
  71. template <class Real>
  72. barycentric_rational_imp<Real>::barycentric_rational_imp(std::vector<Real>&& x, std::vector<Real>&& y,size_t approximation_order) : m_x(std::move(x)), m_y(std::move(y))
  73. {
  74. BOOST_MATH_ASSERT_MSG(m_x.size() == m_y.size(), "There must be the same number of abscissas and ordinates.");
  75. BOOST_MATH_ASSERT_MSG(approximation_order < m_x.size(), "Approximation order must be < data length.");
  76. BOOST_MATH_ASSERT_MSG(std::is_sorted(m_x.begin(), m_x.end()), "The abscissas must be listed in increasing order x[0] < x[1] < ... < x[n-1].");
  77. calculate_weights(approximation_order);
  78. }
  79. template<class Real>
  80. void barycentric_rational_imp<Real>::calculate_weights(size_t approximation_order)
  81. {
  82. using std::abs;
  83. int64_t n = m_x.size();
  84. m_w.resize(n, 0);
  85. for(int64_t k = 0; k < n; ++k)
  86. {
  87. int64_t i_min = (std::max)(k - static_cast<int64_t>(approximation_order), static_cast<int64_t>(0));
  88. int64_t i_max = k;
  89. if (k >= n - (std::ptrdiff_t)approximation_order)
  90. {
  91. i_max = n - approximation_order - 1;
  92. }
  93. for(int64_t i = i_min; i <= i_max; ++i)
  94. {
  95. Real inv_product = 1;
  96. int64_t j_max = (std::min)(static_cast<int64_t>(i + approximation_order), static_cast<int64_t>(n - 1));
  97. for(int64_t j = i; j <= j_max; ++j)
  98. {
  99. if (j == k)
  100. {
  101. continue;
  102. }
  103. Real diff = m_x[k] - m_x[j];
  104. using std::numeric_limits;
  105. if (abs(diff) < (numeric_limits<Real>::min)())
  106. {
  107. std::string msg = std::string("Spacing between x[")
  108. + std::to_string(k) + std::string("] and x[")
  109. + std::to_string(i) + std::string("] is ")
  110. + std::string("smaller than the epsilon of ")
  111. + std::string(typeid(Real).name());
  112. throw std::logic_error(msg);
  113. }
  114. inv_product *= diff;
  115. }
  116. if (i % 2 == 0)
  117. {
  118. m_w[k] += 1/inv_product;
  119. }
  120. else
  121. {
  122. m_w[k] -= 1/inv_product;
  123. }
  124. }
  125. }
  126. }
  127. template<class Real>
  128. Real barycentric_rational_imp<Real>::operator()(Real x) const
  129. {
  130. Real numerator = 0;
  131. Real denominator = 0;
  132. for(size_t i = 0; i < m_x.size(); ++i)
  133. {
  134. // Presumably we should see if the accuracy is improved by using ULP distance of say, 5 here, instead of testing for floating point equality.
  135. // However, it has been shown that if x approx x_i, but x != x_i, then inaccuracy in the numerator cancels the inaccuracy in the denominator,
  136. // and the result is fairly accurate. See: http://epubs.siam.org/doi/pdf/10.1137/S0036144502417715
  137. if (x == m_x[i])
  138. {
  139. return m_y[i];
  140. }
  141. Real t = m_w[i]/(x - m_x[i]);
  142. numerator += t*m_y[i];
  143. denominator += t;
  144. }
  145. return numerator/denominator;
  146. }
  147. /*
  148. * A formula for computing the derivative of the barycentric representation is given in
  149. * "Some New Aspects of Rational Interpolation", by Claus Schneider and Wilhelm Werner,
  150. * Mathematics of Computation, v47, number 175, 1986.
  151. * http://www.ams.org/journals/mcom/1986-47-175/S0025-5718-1986-0842136-8/S0025-5718-1986-0842136-8.pdf
  152. * and reviewed in
  153. * Recent developments in barycentric rational interpolation
  154. * Jean-Paul Berrut, Richard Baltensperger and Hans D. Mittelmann
  155. *
  156. * Is it possible to complete this in one pass through the data?
  157. */
  158. template<class Real>
  159. Real barycentric_rational_imp<Real>::prime(Real x) const
  160. {
  161. Real rx = this->operator()(x);
  162. Real numerator = 0;
  163. Real denominator = 0;
  164. for(size_t i = 0; i < m_x.size(); ++i)
  165. {
  166. if (x == m_x[i])
  167. {
  168. Real sum = 0;
  169. for (size_t j = 0; j < m_x.size(); ++j)
  170. {
  171. if (j == i)
  172. {
  173. continue;
  174. }
  175. sum += m_w[j]*(m_y[i] - m_y[j])/(m_x[i] - m_x[j]);
  176. }
  177. return -sum/m_w[i];
  178. }
  179. Real t = m_w[i]/(x - m_x[i]);
  180. Real diff = (rx - m_y[i])/(x-m_x[i]);
  181. numerator += t*diff;
  182. denominator += t;
  183. }
  184. return numerator/denominator;
  185. }
  186. }}}}
  187. #endif