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)
8 #ifndef BOOST_MATH_INTERPOLATORS_BARYCENTRIC_RATIONAL_DETAIL_HPP
9 #define BOOST_MATH_INTERPOLATORS_BARYCENTRIC_RATIONAL_DETAIL_HPP
12 #include <utility> // for std::move
13 #include <algorithm> // for std::is_sorted
15 #include <boost/math/special_functions/fpclassify.hpp>
16 #include <boost/math/tools/assert.hpp>
18 namespace boost{ namespace math{ namespace interpolators { namespace detail{
21 class barycentric_rational_imp
24 template <class InputIterator1, class InputIterator2>
25 barycentric_rational_imp(InputIterator1 start_x, InputIterator1 end_x, InputIterator2 start_y, size_t approximation_order = 3);
27 barycentric_rational_imp(std::vector<Real>&& x, std::vector<Real>&& y, size_t approximation_order = 3);
29 Real operator()(Real x) const;
31 Real prime(Real x) const;
33 // The barycentric weights are not really that interesting; except to the unit tests!
34 Real weight(size_t i) const { return m_w[i]; }
36 std::vector<Real>&& return_x()
38 return std::move(m_x);
41 std::vector<Real>&& return_y()
43 return std::move(m_y);
48 void calculate_weights(size_t approximation_order);
50 std::vector<Real> m_x;
51 std::vector<Real> m_y;
52 std::vector<Real> m_w;
56 template <class InputIterator1, class InputIterator2>
57 barycentric_rational_imp<Real>::barycentric_rational_imp(InputIterator1 start_x, InputIterator1 end_x, InputIterator2 start_y, size_t approximation_order)
59 std::ptrdiff_t n = std::distance(start_x, end_x);
61 if (approximation_order >= (std::size_t)n)
63 throw std::domain_error("Approximation order must be < data length.");
69 for(unsigned i = 0; start_x != end_x; ++start_x, ++start_y, ++i)
71 // But if we're going to do a memcpy, we can do some error checking which is inexpensive relative to the copy:
72 if(boost::math::isnan(*start_x))
74 std::string msg = std::string("x[") + std::to_string(i) + "] is a NAN";
75 throw std::domain_error(msg);
78 if(boost::math::isnan(*start_y))
80 std::string msg = std::string("y[") + std::to_string(i) + "] is a NAN";
81 throw std::domain_error(msg);
87 calculate_weights(approximation_order);
91 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))
93 BOOST_MATH_ASSERT_MSG(m_x.size() == m_y.size(), "There must be the same number of abscissas and ordinates.");
94 BOOST_MATH_ASSERT_MSG(approximation_order < m_x.size(), "Approximation order must be < data length.");
95 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].");
96 calculate_weights(approximation_order);
100 void barycentric_rational_imp<Real>::calculate_weights(size_t approximation_order)
103 int64_t n = m_x.size();
105 for(int64_t k = 0; k < n; ++k)
107 int64_t i_min = (std::max)(k - static_cast<int64_t>(approximation_order), static_cast<int64_t>(0));
109 if (k >= n - (std::ptrdiff_t)approximation_order)
111 i_max = n - approximation_order - 1;
114 for(int64_t i = i_min; i <= i_max; ++i)
116 Real inv_product = 1;
117 int64_t j_max = (std::min)(static_cast<int64_t>(i + approximation_order), static_cast<int64_t>(n - 1));
118 for(int64_t j = i; j <= j_max; ++j)
125 Real diff = m_x[k] - m_x[j];
126 using std::numeric_limits;
127 if (abs(diff) < (numeric_limits<Real>::min)())
129 std::string msg = std::string("Spacing between x[")
130 + std::to_string(k) + std::string("] and x[")
131 + std::to_string(i) + std::string("] is ")
132 + std::string("smaller than the epsilon of ")
133 + std::string(typeid(Real).name());
134 throw std::logic_error(msg);
140 m_w[k] += 1/inv_product;
144 m_w[k] -= 1/inv_product;
152 Real barycentric_rational_imp<Real>::operator()(Real x) const
155 Real denominator = 0;
156 for(size_t i = 0; i < m_x.size(); ++i)
158 // Presumably we should see if the accuracy is improved by using ULP distance of say, 5 here, instead of testing for floating point equality.
159 // 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,
160 // and the result is fairly accurate. See: http://epubs.siam.org/doi/pdf/10.1137/S0036144502417715
165 Real t = m_w[i]/(x - m_x[i]);
166 numerator += t*m_y[i];
169 return numerator/denominator;
173 * A formula for computing the derivative of the barycentric representation is given in
174 * "Some New Aspects of Rational Interpolation", by Claus Schneider and Wilhelm Werner,
175 * Mathematics of Computation, v47, number 175, 1986.
176 * http://www.ams.org/journals/mcom/1986-47-175/S0025-5718-1986-0842136-8/S0025-5718-1986-0842136-8.pdf
178 * Recent developments in barycentric rational interpolation
179 * Jean-Paul Berrut, Richard Baltensperger and Hans D. Mittelmann
181 * Is it possible to complete this in one pass through the data?
185 Real barycentric_rational_imp<Real>::prime(Real x) const
187 Real rx = this->operator()(x);
189 Real denominator = 0;
190 for(size_t i = 0; i < m_x.size(); ++i)
195 for (size_t j = 0; j < m_x.size(); ++j)
201 sum += m_w[j]*(m_y[i] - m_y[j])/(m_x[i] - m_x[j]);
205 Real t = m_w[i]/(x - m_x[i]);
206 Real diff = (rx - m_y[i])/(x-m_x[i]);
211 return numerator/denominator;