cardinal_b_splines.qbk 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. [/
  2. Copyright 2019, Nick Thompson
  3. Distributed under the Boost Software License, Version 1.0.
  4. (See accompanying file LICENSE_1_0.txt or copy at
  5. http://www.boost.org/LICENSE_1_0.txt).
  6. ]
  7. [section:cardinal_b_splines Cardinal B-splines]
  8. [h4 Synopsis]
  9. ``
  10. #include <boost/math/special_functions/cardinal_b_spline.hpp>
  11. ``
  12. namespace boost{ namespace math{
  13. template<unsigned n, typename Real>
  14. auto cardinal_b_spline(Real x);
  15. template<unsigned n, typename Real>
  16. auto cardinal_b_spline_prime(Real x);
  17. template<unsigned n, typename Real>
  18. auto cardinal_b_spline_double_prime(Real x);
  19. template<unsigned n, typename Real>
  20. Real forward_cardinal_b_spline(Real x);
  21. }} // namespaces
  22. Cardinal /B/-splines are a family of compactly supported functions useful for the smooth interpolation of tables.
  23. The first /B/-spline /B/[sub 0] is simply a box function:
  24. It takes the value one inside the interval \[-1/2, 1/2\], and is zero elsewhere.
  25. /B/-splines of higher smoothness are constructed by iterative convolution, namely, /B/[sub 1] := /B/[sub 0] \u2217 /B/[sub 0],
  26. and /B/[sub /n/+1] := /B/[sub /n/ ] \u2217 /B/[sub 0].
  27. For example, /B/[sub 1](/x/) = 1 - |/x/| for /x/ in \[-1,1\], and zero elsewhere, so it is a hat function.
  28. A basic usage is as follows:
  29. using boost::math::cardinal_b_spline;
  30. using boost::math::cardinal_b_spline_prime;
  31. using boost::math::cardinal_b_spline_double_prime;
  32. double x = 0.5;
  33. // B₀(x), the box function:
  34. double y = cardinal_b_spline<0>(x);
  35. // B₁(x), the hat function:
  36. y = cardinal_b_spline<1>(x);
  37. // First derivative of B₃:
  38. yp = cardinal_b_spline_prime<3>(x);
  39. // Second derivative of B₃:
  40. ypp = cardinal_b_spline_double_prime<3>(x);
  41. [$../graphs/central_b_splines.svg]
  42. [$../graphs/central_b_spline_derivatives.svg]
  43. [$../graphs/central_b_spline_second_derivatives.svg]
  44. [h3 Caveats]
  45. Numerous notational conventions for /B/-splines exist.
  46. Whereas Boost.Math (following Kress) zero indexes the /B/-splines,
  47. other authors (such as Schoenberg and Massopust) use 1-based indexing.
  48. So (for example) Boost.Math's hat function /B/[sub 1] is what Schoenberg calls /M/[sub 2].
  49. Mathematica, like Boost, uses the zero-indexing convention for its `BSplineCurve`.
  50. Even the support of the splines is not agreed upon.
  51. Mathematica starts the support of the splines at zero and rescales the independent variable so that the support of every member is \[0, 1\].
  52. Massopust as well as Unser puts the support of the /B/-splines at \[0, /n/\], whereas Kress centers them at zero.
  53. Schoenberg distinguishes between the two cases, called the splines starting at zero forward splines, and the ones symmetric about zero /central/.
  54. The /B/-splines of Boost.Math are central, with support support \[-(/n/+1)\/2, (/n/+1)\/2\].
  55. If necessary, the forward splines can be evaluated by using `forward_cardinal_b_spline`, whose support is \[0, /n/+1\].
  56. [h3 Implementation]
  57. The implementation follows Maurice Cox' 1972 paper 'The Numerical Evaluation of B-splines', and uses the triangular array of Algorithm 6.1 of the reference rather than the rhombohedral array of Algorithm 6.2.
  58. Benchmarks revealed that the time to calculate the indexes of the rhombohedral array exceed the time to simply add zeroes together, except for /n/ > 18.
  59. Since few people use /B/ splines of degree 18, the triangular array is used.
  60. [h3 Performance]
  61. Double precision timing on a consumer x86 laptop is shown below:
  62. ``
  63. Run on (16 X 4300 MHz CPU s)
  64. CPU Caches:
  65. L1 Data 32K (x8)
  66. L1 Instruction 32K (x8)
  67. L2 Unified 1024K (x8)
  68. L3 Unified 11264K (x1)
  69. Load Average: 0.21, 0.33, 0.29
  70. -----------------------------------------
  71. Benchmark Time
  72. -----------------------------------------
  73. CardinalBSpline<1, double> 18.8 ns
  74. CardinalBSpline<2, double> 25.3 ns
  75. CardinalBSpline<3, double> 29.3 ns
  76. CardinalBSpline<4, double> 33.8 ns
  77. CardinalBSpline<5, double> 36.7 ns
  78. CardinalBSpline<6, double> 39.1 ns
  79. CardinalBSpline<7, double> 43.6 ns
  80. CardinalBSpline<8, double> 62.8 ns
  81. CardinalBSpline<9, double> 70.2 ns
  82. CardinalBSpline<10, double> 83.8 ns
  83. CardinalBSpline<11, double> 94.3 ns
  84. CardinalBSpline<12, double> 108 ns
  85. CardinalBSpline<13, double> 122 ns
  86. CardinalBSpline<14, double> 138 ns
  87. CardinalBSpline<15, double> 155 ns
  88. CardinalBSpline<16, double> 170 ns
  89. CardinalBSpline<17, double> 192 ns
  90. CardinalBSpline<18, double> 174 ns
  91. CardinalBSpline<19, double> 180 ns
  92. CardinalBSpline<20, double> 194 ns
  93. UniformReal<double> 11.5 ns
  94. ``
  95. A uniformly distributed random number within the support of the spline is generated for the argument, so subtracting 11.5 ns from these gives a good idea of the performance of the calls.
  96. [h3 Accuracy]
  97. Some representative ULP plots are shown below.
  98. The error grows linearly with /n/, as expected from Cox equation 10.5.
  99. [$../graphs/b_spline_ulp_3.svg]
  100. [$../graphs/b_spline_ulp_5.svg]
  101. [$../graphs/b_spline_ulp_9.svg]
  102. [h3 References]
  103. * I.J. Schoenberg, ['Cardinal Spline Interpolation], SIAM Volume 12, 1973
  104. * Rainer Kress, ['Numerical Analysis], Springer, 1998
  105. * Peter Massopust, ['On Some Generalizations of B-splines], arxiv preprint, 2019
  106. * Michael Unser and Thierry Blu, ['Fractional Splines and Wavelets], SIAM Review 2000, Volume 42, No. 1
  107. * Cox, Maurice G. ['The numerical evaluation of B-splines.], IMA Journal of Applied Mathematics 10.2 (1972): 134-149.
  108. [endsect]