cubic_b_spline.hpp 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. // This implements the compactly supported cubic b spline algorithm described in
  7. // Kress, Rainer. "Numerical analysis, volume 181 of Graduate Texts in Mathematics." (1998).
  8. // Splines of compact support are faster to evaluate and are better conditioned than classical cubic splines.
  9. // Let f be the function we are trying to interpolate, and s be the interpolating spline.
  10. // The routine constructs the interpolant in O(N) time, and evaluating s at a point takes constant time.
  11. // The order of accuracy depends on the regularity of the f, however, assuming f is
  12. // four-times continuously differentiable, the error is of O(h^4).
  13. // In addition, we can differentiate the spline and obtain a good interpolant for f'.
  14. // The main restriction of this method is that the samples of f must be evenly spaced.
  15. // Look for barycentric rational interpolation for non-evenly sampled data.
  16. // Properties:
  17. // - s(x_j) = f(x_j)
  18. // - All cubic polynomials interpolated exactly
  19. #ifndef BOOST_MATH_INTERPOLATORS_CUBIC_B_SPLINE_HPP
  20. #define BOOST_MATH_INTERPOLATORS_CUBIC_B_SPLINE_HPP
  21. #include <boost/math/interpolators/detail/cubic_b_spline_detail.hpp>
  22. namespace boost{ namespace math{
  23. template <class Real>
  24. class cubic_b_spline
  25. {
  26. public:
  27. // If you don't know the value of the derivative at the endpoints, leave them as nans and the routine will estimate them.
  28. // f[0] = f(a), f[length -1] = b, step_size = (b - a)/(length -1).
  29. template <class BidiIterator>
  30. cubic_b_spline(const BidiIterator f, BidiIterator end_p, Real left_endpoint, Real step_size,
  31. Real left_endpoint_derivative = std::numeric_limits<Real>::quiet_NaN(),
  32. Real right_endpoint_derivative = std::numeric_limits<Real>::quiet_NaN());
  33. cubic_b_spline(const Real* const f, size_t length, Real left_endpoint, Real step_size,
  34. Real left_endpoint_derivative = std::numeric_limits<Real>::quiet_NaN(),
  35. Real right_endpoint_derivative = std::numeric_limits<Real>::quiet_NaN());
  36. cubic_b_spline() = default;
  37. Real operator()(Real x) const;
  38. Real prime(Real x) const;
  39. private:
  40. std::shared_ptr<detail::cubic_b_spline_imp<Real>> m_imp;
  41. };
  42. template<class Real>
  43. cubic_b_spline<Real>::cubic_b_spline(const Real* const f, size_t length, Real left_endpoint, Real step_size,
  44. Real left_endpoint_derivative, Real right_endpoint_derivative) : m_imp(std::make_shared<detail::cubic_b_spline_imp<Real>>(f, f + length, left_endpoint, step_size, left_endpoint_derivative, right_endpoint_derivative))
  45. {
  46. }
  47. template <class Real>
  48. template <class BidiIterator>
  49. cubic_b_spline<Real>::cubic_b_spline(BidiIterator f, BidiIterator end_p, Real left_endpoint, Real step_size,
  50. Real left_endpoint_derivative, Real right_endpoint_derivative) : m_imp(std::make_shared<detail::cubic_b_spline_imp<Real>>(f, end_p, left_endpoint, step_size, left_endpoint_derivative, right_endpoint_derivative))
  51. {
  52. }
  53. template<class Real>
  54. Real cubic_b_spline<Real>::operator()(Real x) const
  55. {
  56. return m_imp->operator()(x);
  57. }
  58. template<class Real>
  59. Real cubic_b_spline<Real>::prime(Real x) const
  60. {
  61. return m_imp->prime(x);
  62. }
  63. }}
  64. #endif