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