1[/ 2 Copyright 2017 Nick Thompson 3 4 Distributed under the Boost Software License, Version 1.0. 5 (See accompanying file LICENSE_1_0.txt or copy at 6 http://www.boost.org/LICENSE_1_0.txt). 7] 8 9[section:barycentric Barycentric Rational Interpolation] 10 11[heading Synopsis] 12 13`` 14#include <boost/math/interpolators/barycentric_rational.hpp> 15 16namespace boost{ namespace math{ 17 template<class Real> 18 class barycentric_rational 19 { 20 public: 21 template <class InputIterator1, class InputIterator2> 22 barycentric_rational(InputIterator1 start_x, InputIterator1 end_x, InputIterator2 start_y, size_t approximation_order = 3); 23 24 barycentric_rational(std::vector<Real>&& x, std::vector<Real>&& y, size_t order = 3); 25 26 barycentric_rational(const Real* const x, const Real* const y, size_t n, size_t approximation_order = 3); 27 28 Real operator()(Real x) const; 29 30 Real prime(Real x) const; 31 32 std::vector<Real>&& return_x(); 33 34 std::vector<Real>&& return_y(); 35 }; 36 37}} 38`` 39 40 41[heading Description] 42 43Barycentric rational interpolation is a high-accuracy interpolation method for non-uniformly spaced samples. 44It requires [bigo](/N/) time for construction, and [bigo](/N/) time for each evaluation. 45Linear time evaluation is not optimal; for instance the cubic B-spline can be evaluated in constant time. 46However, using the cubic B-spline requires uniformly-spaced samples, which are not always available. 47 48Use of the class requires a vector of independent variables `x[0]`, `x[1]`, .... `x[n-1]` where `x[i+1] > x[i]`, 49and a vector of dependent variables `y[0]`, `y[1]`, ... , `y[n-1]`. 50The call is trivial: 51 52 std::vector<double> x(500); 53 std::vector<double> y(500); 54 // populate x, y, then: 55 boost::math::barycentric_rational<double> interpolant(std::move(x), std::move(y)); 56 57This implicitly calls the constructor with approximation order 3, and hence the accuracy is [bigo](/h/[super 4]). 58In general, if you require an approximation order /d/, then the error is [bigo](/h/[super /d/+1]). 59A call to the constructor with an explicit approximation order is demonstrated below 60 61 boost::math::barycentric_rational<double> interpolant(std::move(x), std::move(y), 5); 62 63To evaluate the interpolant, simply use 64 65 double x = 2.3; 66 double y = interpolant(x); 67 68and to evaluate its derivative use 69 70 double y = interpolant.prime(x); 71 72If you no longer require the interpolant, then you can get your data back: 73 74 std::vector<double> xs = interpolant.return_x(); 75 std::vector<double> ys = interpolant.return_y(); 76 77Be aware that once you return your data, the interpolant is *dead*. 78 79[heading Caveats] 80 81Although this algorithm is robust, it can surprise you. 82The main way this occurs is if the sample spacing at the endpoints is much larger than the spacing in the center. 83This is to be expected; all interpolants perform better in the opposite regime, 84where samples are clustered at the endpoints and somewhat uniformly spaced throughout the center. 85 86A desirable property of any interpolator /f/ is that for all 87/x/[sub min] [le] /x/ [le] /x/[sub max], also /y/[sub min] [le] /f/(/x/) [le] /y/[sub max]. 88 89/This property does not hold for the barycentric rational interpolator./ 90However, unless you deliberately try to antagonize this interpolator (by, for instance, placing the final value far from all the rest), 91you will probably not fall victim to this problem. 92 93The reference used for implementation of this algorithm is 94[@https://web.archive.org/save/_embed/http://www.mn.uio.no/math/english/people/aca/michaelf/papers/rational.pdf Barycentric rational interpolation with no poles and a high rate of interpolation], and the evaluation of the derivative is given by [@http://www.ams.org/journals/mcom/1986-47-175/S0025-5718-1986-0842136-8/S0025-5718-1986-0842136-8.pdf Some New Aspects of Rational Interpolation]. 95 96[heading Examples] 97 98[import ../../example/barycentric_interpolation_example.cpp] 99[import ../../example/barycentric_interpolation_example_2.cpp] 100 101[barycentric_rational_example] 102 103[barycentric_rational_example2] 104 105[barycentric_rational_example2_out] 106 107[endsect] [/section:barycentric Barycentric Rational Interpolation] 108