cardinal_cubic_b_spline.hpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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_CARDINAL_CUBIC_B_SPLINE_HPP
  20. #define BOOST_MATH_INTERPOLATORS_CARINDAL_CUBIC_B_SPLINE_HPP
  21. #include <boost/math/interpolators/detail/cardinal_cubic_b_spline_detail.hpp>
  22. namespace boost{ namespace math{ namespace interpolators {
  23. template <class Real>
  24. class cardinal_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. cardinal_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. cardinal_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. cardinal_cubic_b_spline() = default;
  37. Real operator()(Real x) const;
  38. Real prime(Real x) const;
  39. Real double_prime(Real x) const;
  40. private:
  41. std::shared_ptr<detail::cardinal_cubic_b_spline_imp<Real>> m_imp;
  42. };
  43. template<class Real>
  44. cardinal_cubic_b_spline<Real>::cardinal_cubic_b_spline(const Real* const f, size_t length, Real left_endpoint, Real step_size,
  45. Real left_endpoint_derivative, Real right_endpoint_derivative) : m_imp(std::make_shared<detail::cardinal_cubic_b_spline_imp<Real>>(f, f + length, left_endpoint, step_size, left_endpoint_derivative, right_endpoint_derivative))
  46. {
  47. }
  48. template <class Real>
  49. template <class BidiIterator>
  50. cardinal_cubic_b_spline<Real>::cardinal_cubic_b_spline(BidiIterator f, BidiIterator end_p, Real left_endpoint, Real step_size,
  51. Real left_endpoint_derivative, Real right_endpoint_derivative) : m_imp(std::make_shared<detail::cardinal_cubic_b_spline_imp<Real>>(f, end_p, left_endpoint, step_size, left_endpoint_derivative, right_endpoint_derivative))
  52. {
  53. }
  54. template<class Real>
  55. Real cardinal_cubic_b_spline<Real>::operator()(Real x) const
  56. {
  57. return m_imp->operator()(x);
  58. }
  59. template<class Real>
  60. Real cardinal_cubic_b_spline<Real>::prime(Real x) const
  61. {
  62. return m_imp->prime(x);
  63. }
  64. template<class Real>
  65. Real cardinal_cubic_b_spline<Real>::double_prime(Real x) const
  66. {
  67. return m_imp->double_prime(x);
  68. }
  69. }}}
  70. #endif