2 * Copyright Nick Thompson, 2020
3 * Use, modification and distribution are subject to the
4 * Boost Software License, Version 1.0. (See accompanying file
5 * LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7 #ifndef BOOST_MATH_INTERPOLATORS_DETAIL_QUINTIC_HERMITE_DETAIL_HPP
8 #define BOOST_MATH_INTERPOLATORS_DETAIL_QUINTIC_HERMITE_DETAIL_HPP
16 namespace interpolators {
19 template<class RandomAccessContainer>
20 class quintic_hermite_detail {
22 using Real = typename RandomAccessContainer::value_type;
23 quintic_hermite_detail(RandomAccessContainer && x, RandomAccessContainer && y, RandomAccessContainer && dydx, RandomAccessContainer && d2ydx2) : x_{std::move(x)}, y_{std::move(y)}, dydx_{std::move(dydx)}, d2ydx2_{std::move(d2ydx2)}
25 if (x_.size() != y_.size())
27 throw std::domain_error("Number of abscissas must = number of ordinates.");
29 if (x_.size() != dydx_.size())
31 throw std::domain_error("Numbers of derivatives must = number of abscissas.");
33 if (x_.size() != d2ydx2_.size())
35 throw std::domain_error("Number of second derivatives must equal number of abscissas.");
39 throw std::domain_error("At least 2 abscissas are required.");
42 for (decltype(x_.size()) i = 1; i < x_.size(); ++i)
47 throw std::domain_error("Abscissas must be sorted in strictly increasing order x0 < x1 < ... < x_{n-1}");
53 void push_back(Real x, Real y, Real dydx, Real d2ydx2)
59 throw std::domain_error("Calling push_back must preserve the monotonicity of the x's");
63 dydx_.push_back(dydx);
64 d2ydx2_.push_back(d2ydx2);
67 inline Real operator()(Real x) const
69 if (x < x_[0] || x > x_.back())
71 std::ostringstream oss;
72 oss.precision(std::numeric_limits<Real>::digits10+3);
73 oss << "Requested abscissa x = " << x << ", which is outside of allowed range ["
74 << x_[0] << ", " << x_.back() << "]";
75 throw std::domain_error(oss.str());
77 // We need t := (x-x_k)/(x_{k+1}-x_k) \in [0,1) for this to work.
78 // Sadly this neccessitates this loathesome check, otherwise we get t = 1 at x = xf.
84 auto it = std::upper_bound(x_.begin(), x_.end(), x);
85 auto i = std::distance(x_.begin(), it) -1;
93 Real a1 = d2ydx2_[i+1];
100 // See the 'Basis functions' section of:
101 // https://www.rose-hulman.edu/~finn/CCLI/Notes/day09.pdf
102 // Also: https://github.com/MrHexxx/QuinticHermiteSpline/blob/master/HermiteSpline.cs
103 Real y = (1- t3*(10 + t*(-15 + 6*t)))*y0;
104 y += t*(1+ t2*(-6 + t*(8 -3*t)))*v0*dx;
105 y += t2*(1 + t*(-3 + t*(3-t)))*a0*dx*dx/2;
106 y += t3*((1 + t*(-2 + t))*a1*dx*dx/2 + (-4 + t*(7 - 3*t))*v1*dx + (10 + t*(-15 + 6*t))*y1);
110 inline Real prime(Real x) const
112 if (x < x_[0] || x > x_.back())
114 std::ostringstream oss;
115 oss.precision(std::numeric_limits<Real>::digits10+3);
116 oss << "Requested abscissa x = " << x << ", which is outside of allowed range ["
117 << x_[0] << ", " << x_.back() << "]";
118 throw std::domain_error(oss.str());
125 auto it = std::upper_bound(x_.begin(), x_.end(), x);
126 auto i = std::distance(x_.begin(), it) -1;
134 Real v1 = dydx_[i+1];
135 Real a0 = d2ydx2_[i];
136 Real a1 = d2ydx2_[i+1];
140 Real dydx = 30*t2*(1 - 2*t + t*t)*(y1-y0)/dx;
141 dydx += (1-18*t*t + 32*t*t*t - 15*t*t*t*t)*v0 - t*t*(12 - 28*t + 15*t*t)*v1;
142 dydx += (t*dx/2)*((2 - 9*t + 12*t*t - 5*t*t*t)*a0 + t*(3 - 8*t + 5*t*t)*a1);
146 inline Real double_prime(Real x) const
148 if (x < x_[0] || x > x_.back())
150 std::ostringstream oss;
151 oss.precision(std::numeric_limits<Real>::digits10+3);
152 oss << "Requested abscissa x = " << x << ", which is outside of allowed range ["
153 << x_[0] << ", " << x_.back() << "]";
154 throw std::domain_error(oss.str());
158 return d2ydx2_.back();
161 auto it = std::upper_bound(x_.begin(), x_.end(), x);
162 auto i = std::distance(x_.begin(), it) -1;
170 Real v1 = dydx_[i+1];
171 Real a0 = d2ydx2_[i];
172 Real a1 = d2ydx2_[i+1];
175 Real d2ydx2 = 60*t*(1 + t*(-3 + 2*t))*(y1-y0)/(dx*dx);
176 d2ydx2 += 12*t*(-3 + t*(8 - 5*t))*v0/dx;
177 d2ydx2 -= 12*t*(2 + t*(-7 + 5*t))*v1/dx;
178 d2ydx2 += (1 + t*(-9 + t*(18 - 10*t)))*a0;
179 d2ydx2 += t*(3 + t*(-12 + 10*t))*a1;
184 friend std::ostream& operator<<(std::ostream & os, const quintic_hermite_detail & m)
186 os << "(x,y,y') = {";
187 for (size_t i = 0; i < m.x_.size() - 1; ++i) {
188 os << "(" << m.x_[i] << ", " << m.y_[i] << ", " << m.dydx_[i] << ", " << m.d2ydx2_[i] << "), ";
190 auto n = m.x_.size()-1;
191 os << "(" << m.x_[n] << ", " << m.y_[n] << ", " << m.dydx_[n] << ", " << m.d2ydx2_[n] << ")}";
195 int64_t bytes() const
197 return 4*x_.size()*sizeof(x_);
200 std::pair<Real, Real> domain() const
202 return {x_.front(), x_.back()};
206 RandomAccessContainer x_;
207 RandomAccessContainer y_;
208 RandomAccessContainer dydx_;
209 RandomAccessContainer d2ydx2_;
213 template<class RandomAccessContainer>
214 class cardinal_quintic_hermite_detail {
216 using Real = typename RandomAccessContainer::value_type;
217 cardinal_quintic_hermite_detail(RandomAccessContainer && y, RandomAccessContainer && dydx, RandomAccessContainer && d2ydx2, Real x0, Real dx)
218 : y_{std::move(y)}, dy_{std::move(dydx)}, d2y_{std::move(d2ydx2)}, x0_{x0}, inv_dx_{1/dx}
220 if (y_.size() != dy_.size())
222 throw std::domain_error("Numbers of derivatives must = number of abscissas.");
224 if (y_.size() != d2y_.size())
226 throw std::domain_error("Number of second derivatives must equal number of abscissas.");
230 throw std::domain_error("At least 2 abscissas are required.");
234 throw std::domain_error("dx > 0 is required.");
237 for (auto & dy : dy_)
242 for (auto & d2y : d2y_)
249 inline Real operator()(Real x) const
251 const Real xf = x0_ + (y_.size()-1)/inv_dx_;
252 if (x < x0_ || x > xf)
254 std::ostringstream oss;
255 oss.precision(std::numeric_limits<Real>::digits10+3);
256 oss << "Requested abscissa x = " << x << ", which is outside of allowed range ["
257 << x0_ << ", " << xf << "]";
258 throw std::domain_error(oss.str());
264 return this->unchecked_evaluation(x);
267 inline Real unchecked_evaluation(Real x) const
270 Real s = (x-x0_)*inv_dx_;
272 auto i = static_cast<decltype(y_.size())>(ii);
283 Real d2y1 = d2y_[i+1];
285 // See the 'Basis functions' section of:
286 // https://www.rose-hulman.edu/~finn/CCLI/Notes/day09.pdf
287 // Also: https://github.com/MrHexxx/QuinticHermiteSpline/blob/master/HermiteSpline.cs
288 Real y = (1- t*t*t*(10 + t*(-15 + 6*t)))*y0;
289 y += t*(1+ t*t*(-6 + t*(8 -3*t)))*dy0;
290 y += t*t*(1 + t*(-3 + t*(3-t)))*d2y0;
291 y += t*t*t*((1 + t*(-2 + t))*d2y1 + (-4 + t*(7 -3*t))*dy1 + (10 + t*(-15 + 6*t))*y1);
295 inline Real prime(Real x) const
297 const Real xf = x0_ + (y_.size()-1)/inv_dx_;
298 if (x < x0_ || x > xf)
300 std::ostringstream oss;
301 oss.precision(std::numeric_limits<Real>::digits10+3);
302 oss << "Requested abscissa x = " << x << ", which is outside of allowed range ["
303 << x0_ << ", " << xf << "]";
304 throw std::domain_error(oss.str());
308 return dy_.back()*inv_dx_;
311 return this->unchecked_prime(x);
314 inline Real unchecked_prime(Real x) const
317 Real s = (x-x0_)*inv_dx_;
319 auto i = static_cast<decltype(y_.size())>(ii);
323 return dy_[i]*inv_dx_;
330 Real d2y1 = d2y_[i+1];
332 Real dydx = 30*t*t*(1 - 2*t + t*t)*(y1-y0);
333 dydx += (1-18*t*t + 32*t*t*t - 15*t*t*t*t)*dy0 - t*t*(12 - 28*t + 15*t*t)*dy1;
334 dydx += t*((2 - 9*t + 12*t*t - 5*t*t*t)*d2y0 + t*(3 - 8*t + 5*t*t)*d2y1);
339 inline Real double_prime(Real x) const
341 const Real xf = x0_ + (y_.size()-1)/inv_dx_;
342 if (x < x0_ || x > xf) {
343 std::ostringstream oss;
344 oss.precision(std::numeric_limits<Real>::digits10+3);
345 oss << "Requested abscissa x = " << x << ", which is outside of allowed range ["
346 << x0_ << ", " << xf << "]";
347 throw std::domain_error(oss.str());
351 return d2y_.back()*2*inv_dx_*inv_dx_;
354 return this->unchecked_double_prime(x);
357 inline Real unchecked_double_prime(Real x) const
360 Real s = (x-x0_)*inv_dx_;
362 auto i = static_cast<decltype(y_.size())>(ii);
366 return d2y_[i]*2*inv_dx_*inv_dx_;
374 Real d2y1 = d2y_[i+1];
376 Real d2ydx2 = 60*t*(1 - 3*t + 2*t*t)*(y1 - y0)*inv_dx_*inv_dx_;
377 d2ydx2 += (12*t)*((-3 + 8*t - 5*t*t)*dy0 - (2 - 7*t + 5*t*t)*dy1);
378 d2ydx2 += (1 - 9*t + 18*t*t - 10*t*t*t)*d2y0*(2*inv_dx_*inv_dx_) + t*(3 - 12*t + 10*t*t)*d2y1*(2*inv_dx_*inv_dx_);
382 int64_t bytes() const
384 return 3*y_.size()*sizeof(Real) + 2*sizeof(Real);
387 std::pair<Real, Real> domain() const
389 Real xf = x0_ + (y_.size()-1)/inv_dx_;
394 RandomAccessContainer y_;
395 RandomAccessContainer dy_;
396 RandomAccessContainer d2y_;
402 template<class RandomAccessContainer>
403 class cardinal_quintic_hermite_detail_aos {
405 using Point = typename RandomAccessContainer::value_type;
406 using Real = typename Point::value_type;
407 cardinal_quintic_hermite_detail_aos(RandomAccessContainer && data, Real x0, Real dx)
408 : data_{std::move(data)} , x0_{x0}, inv_dx_{1/dx}
410 if (data_.size() < 2)
412 throw std::domain_error("At least two points are required to interpolate using cardinal_quintic_hermite_aos");
415 if (data_[0].size() != 3)
417 throw std::domain_error("Each datum passed to the cardinal_quintic_hermite_aos must have three elements: {y, y', y''}");
421 throw std::domain_error("dx > 0 is required.");
424 for (auto & datum : data_)
427 datum[2] *= (dx*dx/2);
432 inline Real operator()(Real x) const
434 const Real xf = x0_ + (data_.size()-1)/inv_dx_;
435 if (x < x0_ || x > xf)
437 std::ostringstream oss;
438 oss.precision(std::numeric_limits<Real>::digits10+3);
439 oss << "Requested abscissa x = " << x << ", which is outside of allowed range ["
440 << x0_ << ", " << xf << "]";
441 throw std::domain_error(oss.str());
445 return data_.back()[0];
447 return this->unchecked_evaluation(x);
450 inline Real unchecked_evaluation(Real x) const
453 Real s = (x-x0_)*inv_dx_;
455 auto i = static_cast<decltype(data_.size())>(ii);
462 Real y0 = data_[i][0];
463 Real dy0 = data_[i][1];
464 Real d2y0 = data_[i][2];
465 Real y1 = data_[i+1][0];
466 Real dy1 = data_[i+1][1];
467 Real d2y1 = data_[i+1][2];
469 Real y = (1 - t*t*t*(10 + t*(-15 + 6*t)))*y0;
470 y += t*(1 + t*t*(-6 + t*(8 - 3*t)))*dy0;
471 y += t*t*(1 + t*(-3 + t*(3 - t)))*d2y0;
472 y += t*t*t*((1 + t*(-2 + t))*d2y1 + (-4 + t*(7 - 3*t))*dy1 + (10 + t*(-15 + 6*t))*y1);
476 inline Real prime(Real x) const
478 const Real xf = x0_ + (data_.size()-1)/inv_dx_;
479 if (x < x0_ || x > xf)
481 std::ostringstream oss;
482 oss.precision(std::numeric_limits<Real>::digits10+3);
483 oss << "Requested abscissa x = " << x << ", which is outside of allowed range ["
484 << x0_ << ", " << xf << "]";
485 throw std::domain_error(oss.str());
489 return data_.back()[1]*inv_dx_;
492 return this->unchecked_prime(x);
495 inline Real unchecked_prime(Real x) const
498 Real s = (x-x0_)*inv_dx_;
500 auto i = static_cast<decltype(data_.size())>(ii);
504 return data_[i][1]*inv_dx_;
508 Real y0 = data_[i][0];
509 Real y1 = data_[i+1][0];
510 Real v0 = data_[i][1];
511 Real v1 = data_[i+1][1];
512 Real a0 = data_[i][2];
513 Real a1 = data_[i+1][2];
515 Real dy = 30*t*t*(1 - 2*t + t*t)*(y1-y0);
516 dy += (1-18*t*t + 32*t*t*t - 15*t*t*t*t)*v0 - t*t*(12 - 28*t + 15*t*t)*v1;
517 dy += t*((2 - 9*t + 12*t*t - 5*t*t*t)*a0 + t*(3 - 8*t + 5*t*t)*a1);
521 inline Real double_prime(Real x) const
523 const Real xf = x0_ + (data_.size()-1)/inv_dx_;
524 if (x < x0_ || x > xf)
526 std::ostringstream oss;
527 oss.precision(std::numeric_limits<Real>::digits10+3);
528 oss << "Requested abscissa x = " << x << ", which is outside of allowed range ["
529 << x0_ << ", " << xf << "]";
530 throw std::domain_error(oss.str());
534 return data_.back()[2]*2*inv_dx_*inv_dx_;
537 return this->unchecked_double_prime(x);
540 inline Real unchecked_double_prime(Real x) const
543 Real s = (x-x0_)*inv_dx_;
545 auto i = static_cast<decltype(data_.size())>(ii);
548 return data_[i][2]*2*inv_dx_*inv_dx_;
550 Real y0 = data_[i][0];
551 Real dy0 = data_[i][1];
552 Real d2y0 = data_[i][2];
553 Real y1 = data_[i+1][0];
554 Real dy1 = data_[i+1][1];
555 Real d2y1 = data_[i+1][2];
557 Real d2ydx2 = 60*t*(1 - 3*t + 2*t*t)*(y1 - y0)*inv_dx_*inv_dx_;
558 d2ydx2 += (12*t)*((-3 + 8*t - 5*t*t)*dy0 - (2 - 7*t + 5*t*t)*dy1);
559 d2ydx2 += (1 - 9*t + 18*t*t - 10*t*t*t)*d2y0*(2*inv_dx_*inv_dx_) + t*(3 - 12*t + 10*t*t)*d2y1*(2*inv_dx_*inv_dx_);
563 int64_t bytes() const
565 return data_.size()*data_[0].size()*sizeof(Real) + 2*sizeof(Real);
568 std::pair<Real, Real> domain() const
570 Real xf = x0_ + (data_.size()-1)/inv_dx_;
575 RandomAccessContainer data_;