cardinal_cubic_b_spline_detail.hpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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. #ifndef BOOST_MATH_INTERPOLATORS_CARDINAL_CUBIC_B_SPLINE_DETAIL_HPP
  7. #define BOOST_MATH_INTERPOLATORS_CARDINAL_CUBIC_B_SPLINE_DETAIL_HPP
  8. #include <limits>
  9. #include <cmath>
  10. #include <vector>
  11. #include <memory>
  12. #include <boost/math/constants/constants.hpp>
  13. #include <boost/math/special_functions/fpclassify.hpp>
  14. namespace boost{ namespace math{ namespace interpolators{ namespace detail{
  15. template <class Real>
  16. class cardinal_cubic_b_spline_imp
  17. {
  18. public:
  19. // If you don't know the value of the derivative at the endpoints, leave them as nans and the routine will estimate them.
  20. // f[0] = f(a), f[length -1] = b, step_size = (b - a)/(length -1).
  21. template <class BidiIterator>
  22. cardinal_cubic_b_spline_imp(BidiIterator f, BidiIterator end_p, Real left_endpoint, Real step_size,
  23. Real left_endpoint_derivative = std::numeric_limits<Real>::quiet_NaN(),
  24. Real right_endpoint_derivative = std::numeric_limits<Real>::quiet_NaN());
  25. Real operator()(Real x) const;
  26. Real prime(Real x) const;
  27. Real double_prime(Real x) const;
  28. private:
  29. std::vector<Real> m_beta;
  30. Real m_h_inv;
  31. Real m_a;
  32. Real m_avg;
  33. };
  34. template <class Real>
  35. Real b3_spline(Real x)
  36. {
  37. using std::abs;
  38. Real absx = abs(x);
  39. if (absx < 1)
  40. {
  41. Real y = 2 - absx;
  42. Real z = 1 - absx;
  43. return boost::math::constants::sixth<Real>()*(y*y*y - 4*z*z*z);
  44. }
  45. if (absx < 2)
  46. {
  47. Real y = 2 - absx;
  48. return boost::math::constants::sixth<Real>()*y*y*y;
  49. }
  50. return (Real) 0;
  51. }
  52. template<class Real>
  53. Real b3_spline_prime(Real x)
  54. {
  55. if (x < 0)
  56. {
  57. return -b3_spline_prime(-x);
  58. }
  59. if (x < 1)
  60. {
  61. return x*(3*boost::math::constants::half<Real>()*x - 2);
  62. }
  63. if (x < 2)
  64. {
  65. return -boost::math::constants::half<Real>()*(2 - x)*(2 - x);
  66. }
  67. return (Real) 0;
  68. }
  69. template<class Real>
  70. Real b3_spline_double_prime(Real x)
  71. {
  72. if (x < 0)
  73. {
  74. return b3_spline_double_prime(-x);
  75. }
  76. if (x < 1)
  77. {
  78. return 3*x - 2;
  79. }
  80. if (x < 2)
  81. {
  82. return (2 - x);
  83. }
  84. return (Real) 0;
  85. }
  86. template <class Real>
  87. template <class BidiIterator>
  88. cardinal_cubic_b_spline_imp<Real>::cardinal_cubic_b_spline_imp(BidiIterator f, BidiIterator end_p, Real left_endpoint, Real step_size,
  89. Real left_endpoint_derivative, Real right_endpoint_derivative) : m_a(left_endpoint), m_avg(0)
  90. {
  91. using boost::math::constants::third;
  92. std::size_t length = end_p - f;
  93. if (length < 5)
  94. {
  95. if (boost::math::isnan(left_endpoint_derivative) || boost::math::isnan(right_endpoint_derivative))
  96. {
  97. throw std::logic_error("Interpolation using a cubic b spline with derivatives estimated at the endpoints requires at least 5 points.\n");
  98. }
  99. if (length < 3)
  100. {
  101. throw std::logic_error("Interpolation using a cubic b spline requires at least 3 points.\n");
  102. }
  103. }
  104. if (boost::math::isnan(left_endpoint))
  105. {
  106. throw std::logic_error("Left endpoint is NAN; this is disallowed.\n");
  107. }
  108. if (left_endpoint + length*step_size >= (std::numeric_limits<Real>::max)())
  109. {
  110. throw std::logic_error("Right endpoint overflows the maximum representable number of the specified precision.\n");
  111. }
  112. if (step_size <= 0)
  113. {
  114. throw std::logic_error("The step size must be strictly > 0.\n");
  115. }
  116. // Storing the inverse of the stepsize does provide a measurable speedup.
  117. // It's not huge, but nonetheless worthwhile.
  118. m_h_inv = 1/step_size;
  119. // Following Kress's notation, s'(a) = a1, s'(b) = b1
  120. Real a1 = left_endpoint_derivative;
  121. // See the finite-difference table on Wikipedia for reference on how
  122. // to construct high-order estimates for one-sided derivatives:
  123. // https://en.wikipedia.org/wiki/Finite_difference_coefficient#Forward_and_backward_finite_difference
  124. // Here, we estimate then to O(h^4), as that is the maximum accuracy we could obtain from this method.
  125. if (boost::math::isnan(a1))
  126. {
  127. // For simple functions (linear, quadratic, so on)
  128. // almost all the error comes from derivative estimation.
  129. // This does pairwise summation which gives us another digit of accuracy over naive summation.
  130. Real t0 = 4*(f[1] + third<Real>()*f[3]);
  131. Real t1 = -(25*third<Real>()*f[0] + f[4])/4 - 3*f[2];
  132. a1 = m_h_inv*(t0 + t1);
  133. }
  134. Real b1 = right_endpoint_derivative;
  135. if (boost::math::isnan(b1))
  136. {
  137. size_t n = length - 1;
  138. Real t0 = 4*(f[n-3] + third<Real>()*f[n - 1]);
  139. Real t1 = -(25*third<Real>()*f[n - 4] + f[n])/4 - 3*f[n - 2];
  140. b1 = m_h_inv*(t0 + t1);
  141. }
  142. // s(x) = \sum \alpha_i B_{3}( (x- x_i - a)/h )
  143. // Of course we must reindex from Kress's notation, since he uses negative indices which make C++ unhappy.
  144. m_beta.resize(length + 2, std::numeric_limits<Real>::quiet_NaN());
  145. // Since the splines have compact support, they decay to zero very fast outside the endpoints.
  146. // This is often very annoying; we'd like to evaluate the interpolant a little bit outside the
  147. // boundary [a,b] without massive error.
  148. // A simple way to deal with this is just to subtract the DC component off the signal, so we need the average.
  149. // This algorithm for computing the average is recommended in
  150. // http://www.heikohoffmann.de/htmlthesis/node134.html
  151. Real t = 1;
  152. for (size_t i = 0; i < length; ++i)
  153. {
  154. if (boost::math::isnan(f[i]))
  155. {
  156. std::string err = "This function you are trying to interpolate is a nan at index " + std::to_string(i) + "\n";
  157. throw std::logic_error(err);
  158. }
  159. m_avg += (f[i] - m_avg) / t;
  160. t += 1;
  161. }
  162. // Now we must solve an almost-tridiagonal system, which requires O(N) operations.
  163. // There are, in fact 5 diagonals, but they only differ from zero on the first and last row,
  164. // so we can patch up the tridiagonal row reduction algorithm to deal with two special rows.
  165. // See Kress, equations 8.41
  166. // The the "tridiagonal" matrix is:
  167. // 1 0 -1
  168. // 1 4 1
  169. // 1 4 1
  170. // 1 4 1
  171. // ....
  172. // 1 4 1
  173. // 1 0 -1
  174. // Numerical estimate indicate that as N->Infinity, cond(A) -> 6.9, so this matrix is good.
  175. std::vector<Real> rhs(length + 2, std::numeric_limits<Real>::quiet_NaN());
  176. std::vector<Real> super_diagonal(length + 2, std::numeric_limits<Real>::quiet_NaN());
  177. rhs[0] = -2*step_size*a1;
  178. rhs[rhs.size() - 1] = -2*step_size*b1;
  179. super_diagonal[0] = 0;
  180. for(size_t i = 1; i < rhs.size() - 1; ++i)
  181. {
  182. rhs[i] = 6*(f[i - 1] - m_avg);
  183. super_diagonal[i] = 1;
  184. }
  185. // One step of row reduction on the first row to patch up the 5-diagonal problem:
  186. // 1 0 -1 | r0
  187. // 1 4 1 | r1
  188. // mapsto:
  189. // 1 0 -1 | r0
  190. // 0 4 2 | r1 - r0
  191. // mapsto
  192. // 1 0 -1 | r0
  193. // 0 1 1/2| (r1 - r0)/4
  194. super_diagonal[1] = 0.5;
  195. rhs[1] = (rhs[1] - rhs[0])/4;
  196. // Now do a tridiagonal row reduction the standard way, until just before the last row:
  197. for (size_t i = 2; i < rhs.size() - 1; ++i)
  198. {
  199. Real diagonal = 4 - super_diagonal[i - 1];
  200. rhs[i] = (rhs[i] - rhs[i - 1])/diagonal;
  201. super_diagonal[i] /= diagonal;
  202. }
  203. // Now the last row, which is in the form
  204. // 1 sd[n-3] 0 | rhs[n-3]
  205. // 0 1 sd[n-2] | rhs[n-2]
  206. // 1 0 -1 | rhs[n-1]
  207. Real final_subdiag = -super_diagonal[rhs.size() - 3];
  208. rhs[rhs.size() - 1] = (rhs[rhs.size() - 1] - rhs[rhs.size() - 3])/final_subdiag;
  209. Real final_diag = -1/final_subdiag;
  210. // Now we're here:
  211. // 1 sd[n-3] 0 | rhs[n-3]
  212. // 0 1 sd[n-2] | rhs[n-2]
  213. // 0 1 final_diag | (rhs[n-1] - rhs[n-3])/diag
  214. final_diag = final_diag - super_diagonal[rhs.size() - 2];
  215. rhs[rhs.size() - 1] = rhs[rhs.size() - 1] - rhs[rhs.size() - 2];
  216. // Back substitutions:
  217. m_beta[rhs.size() - 1] = rhs[rhs.size() - 1]/final_diag;
  218. for(size_t i = rhs.size() - 2; i > 0; --i)
  219. {
  220. m_beta[i] = rhs[i] - super_diagonal[i]*m_beta[i + 1];
  221. }
  222. m_beta[0] = m_beta[2] + rhs[0];
  223. }
  224. template<class Real>
  225. Real cardinal_cubic_b_spline_imp<Real>::operator()(Real x) const
  226. {
  227. // See Kress, 8.40: Since B3 has compact support, we don't have to sum over all terms,
  228. // just the (at most 5) whose support overlaps the argument.
  229. Real z = m_avg;
  230. Real t = m_h_inv*(x - m_a) + 1;
  231. using std::max;
  232. using std::min;
  233. using std::ceil;
  234. using std::floor;
  235. size_t k_min = (size_t) (max)(static_cast<long>(0), boost::math::ltrunc(ceil(t - 2)));
  236. size_t k_max = (size_t) (max)((min)(static_cast<long>(m_beta.size() - 1), boost::math::ltrunc(floor(t + 2))), (long) 0);
  237. for (size_t k = k_min; k <= k_max; ++k)
  238. {
  239. z += m_beta[k]*b3_spline(t - k);
  240. }
  241. return z;
  242. }
  243. template<class Real>
  244. Real cardinal_cubic_b_spline_imp<Real>::prime(Real x) const
  245. {
  246. Real z = 0;
  247. Real t = m_h_inv*(x - m_a) + 1;
  248. using std::max;
  249. using std::min;
  250. using std::ceil;
  251. using std::floor;
  252. size_t k_min = (size_t) (max)(static_cast<long>(0), boost::math::ltrunc(ceil(t - 2)));
  253. size_t k_max = (size_t) (min)(static_cast<long>(m_beta.size() - 1), boost::math::ltrunc(floor(t + 2)));
  254. for (size_t k = k_min; k <= k_max; ++k)
  255. {
  256. z += m_beta[k]*b3_spline_prime(t - k);
  257. }
  258. return z*m_h_inv;
  259. }
  260. template<class Real>
  261. Real cardinal_cubic_b_spline_imp<Real>::double_prime(Real x) const
  262. {
  263. Real z = 0;
  264. Real t = m_h_inv*(x - m_a) + 1;
  265. using std::max;
  266. using std::min;
  267. using std::ceil;
  268. using std::floor;
  269. size_t k_min = (size_t) (max)(static_cast<long>(0), boost::math::ltrunc(ceil(t - 2)));
  270. size_t k_max = (size_t) (min)(static_cast<long>(m_beta.size() - 1), boost::math::ltrunc(floor(t + 2)));
  271. for (size_t k = k_min; k <= k_max; ++k)
  272. {
  273. z += m_beta[k]*b3_spline_double_prime(t - k);
  274. }
  275. return z*m_h_inv*m_h_inv;
  276. }
  277. }}}}
  278. #endif