cardinal_cubic_b_spline.qbk 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. [/
  2. Copyright (c) 2017 Nick Thompson
  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. [section:cardinal_cubic_b Cardinal Cubic B-spline interpolation]
  8. [heading Synopsis]
  9. ``
  10. #include <boost/math/interpolators/cardinal_cubic_b_spline.hpp>
  11. ``
  12. namespace boost { namespace math { namespace interpolators {
  13. template <class Real>
  14. class cardinal_cubic_b_spline
  15. {
  16. public:
  17. template <class BidiIterator>
  18. cardinal_cubic_b_spline(BidiIterator a, BidiIterator b, Real left_endpoint, Real step_size,
  19. Real left_endpoint_derivative = std::numeric_limits<Real>::quiet_NaN(),
  20. Real right_endpoint_derivative = std::numeric_limits<Real>::quiet_NaN());
  21. cardinal_cubic_b_spline(const Real* const f, size_t length, Real left_endpoint, Real step_size,
  22. Real left_endpoint_derivative = std::numeric_limits<Real>::quiet_NaN(),
  23. Real right_endpoint_derivative = std::numeric_limits<Real>::quiet_NaN());
  24. Real operator()(Real x) const;
  25. Real prime(Real x) const;
  26. Real double_prime(Real x) const;
  27. };
  28. }}} // namespaces
  29. [heading Cardinal Cubic B-Spline Interpolation]
  30. The cardinal cubic /B/-spline class provided by Boost allows fast and accurate interpolation of a function which is known at equally spaced points.
  31. The cubic /B/-spline interpolation is numerically stable as it uses compactly supported basis functions constructed via iterative convolution.
  32. This is to be contrasted to one-sided power function cubic spline interpolation which is ill-conditioned as the global support of cubic polynomials causes small changes far from the evaluation point to exert a large influence on the calculated value.
  33. There are many use cases for interpolating a function at equally spaced points.
  34. One particularly important example is solving ODEs whose coefficients depend on data determined from experiment or numerical simulation.
  35. Since most ODE steppers are adaptive, they must be able to sample the coefficients at arbitrary points;
  36. not just at the points we know the values of our function.
  37. The first two arguments to the constructor are either:
  38. * A pair of bidirectional iterators into the data, or
  39. * A pointer to the data, and a length of the data array.
  40. These are then followed by:
  41. * The start of the functions domain,
  42. * The step size.
  43. Optionally, you may provide two additional arguments to the constructor, namely the derivative of the function at the left endpoint, and the derivative at the right endpoint.
  44. If you do not provide these arguments, they will be estimated using one-sided finite-difference formulas.
  45. An example of a valid call to the constructor is
  46. std::vector<double> f{0.01, -0.02, 0.3, 0.8, 1.9, -8.78, -22.6};
  47. double t0 = 0;
  48. double h = 0.01;
  49. boost::math::interpolators::cardinal_cubic_b_spline<double> spline(f.begin(), f.end(), t0, h);
  50. The endpoints are estimated using a one-sided finite-difference formula.
  51. If you know the derivative at the endpoint, you may pass it to the constructor via
  52. boost::math::interpolators::cardinal_cubic_b_spline<double> spline(f.begin(), f.end(), t0, h, a_prime, b_prime);
  53. To evaluate the interpolant at a point, we simply use
  54. double y = spline(x);
  55. and to evaluate the derivative of the interpolant we use
  56. double yp = spline.prime(x);
  57. Be aware that the accuracy guarantees on the derivative of the spline are an order lower than the guarantees on the original function,
  58. see [@http://www.springer.com/us/book/9780387984087 Numerical Analysis, Graduate Texts in Mathematics, 181, Rainer Kress] for details.
  59. The last interesting member is the second derivative, evaluated via
  60. double ypp = spline.double_prime(x);
  61. The basis functions of the spline are cubic polynomials, so the second derivative is simply linear interpolation.
  62. But the interpolation is not constrained by data (you don't pass in the second derivatives),
  63. and hence is less accurate than would be naively expected from a linear interpolator.
  64. The problem is especially pronounced at the boundaries, where the second derivative is totally unconstrained.
  65. Use the second derivative of the cubic B-spline interpolator only in desperation.
  66. The quintic /B/-spline interpolator is recommended for cases where second derivatives are needed.
  67. [heading Complexity and Performance]
  68. The call to the constructor requires [bigo](/n/) operations, where /n/ is the number of points to interpolate.
  69. Each call the the interpolant is [bigo](1) (constant time).
  70. On the author's Intel Xeon E3-1230, this takes 21ns as long as the vector is small enough to fit in cache.
  71. [heading Accuracy]
  72. Let /h/ be the stepsize. If /f/ is four-times continuously differentiable, then the interpolant is ['[bigo](h[super 4])] accurate and the derivative is ['[bigo](h[super 3])] accurate.
  73. [heading Testing]
  74. Since the interpolant obeys [role serif_italic s(x[sub j]) = f(x[sub j])] at all interpolation points,
  75. the tests generate random data and evaluate the interpolant at the interpolation points,
  76. validating that equality with the data holds.
  77. In addition, constant, linear, and quadratic functions are interpolated to ensure that the interpolant behaves as expected.
  78. [heading Example]
  79. [import ../../example/cardinal_cubic_b_spline_example.cpp]
  80. [cubic_b_spline_example]
  81. [cubic_b_spline_example_out]
  82. [endsect] [/section:cardinal_cubic_b]