| 1 |
/* |
| 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) |
| 6 |
*/ |
| 7 |
#ifndef BOOST_MATH_INTERPOLATORS_DETAIL_SEPTIC_HERMITE_DETAIL_HPP |
| 8 |
#define BOOST_MATH_INTERPOLATORS_DETAIL_SEPTIC_HERMITE_DETAIL_HPP |
| 9 |
#include <algorithm> |
| 10 |
#include <stdexcept> |
| 11 |
#include <sstream> |
| 12 |
#include <limits> |
| 13 |
#include <cmath> |
| 14 |
|
| 15 |
namespace boost { |
| 16 |
namespace math { |
| 17 |
namespace interpolators { |
| 18 |
namespace detail { |
| 19 |
|
| 20 |
template<class RandomAccessContainer> |
| 21 |
class septic_hermite_detail { |
| 22 |
public: |
| 23 |
using Real = typename RandomAccessContainer::value_type; |
| 24 |
septic_hermite_detail(RandomAccessContainer && x, RandomAccessContainer && y, RandomAccessContainer && dydx, RandomAccessContainer && d2ydx2, RandomAccessContainer && d3ydx3) |
| 25 |
: x_{std::move(x)}, y_{std::move(y)}, dydx_{std::move(dydx)}, d2ydx2_{std::move(d2ydx2)}, d3ydx3_{std::move(d3ydx3)} |
| 26 |
{ |
| 27 |
if (x_.size() != y_.size()) |
| 28 |
{ |
| 29 |
throw std::domain_error("Number of abscissas must = number of ordinates."); |
| 30 |
} |
| 31 |
if (x_.size() != dydx_.size()) |
| 32 |
{ |
| 33 |
throw std::domain_error("Numbers of derivatives must = number of abscissas."); |
| 34 |
} |
| 35 |
if (x_.size() != d2ydx2_.size()) |
| 36 |
{ |
| 37 |
throw std::domain_error("Number of second derivatives must equal number of abscissas."); |
| 38 |
} |
| 39 |
if (x_.size() != d3ydx3_.size()) |
| 40 |
{ |
| 41 |
throw std::domain_error("Number of third derivatives must equal number of abscissas."); |
| 42 |
} |
| 43 |
|
| 44 |
if (x_.size() < 2) |
| 45 |
{ |
| 46 |
throw std::domain_error("At least 2 abscissas are required."); |
| 47 |
} |
| 48 |
Real x0 = x_[0]; |
| 49 |
for (decltype(x_.size()) i = 1; i < x_.size(); ++i) |
| 50 |
{ |
| 51 |
Real x1 = x_[i]; |
| 52 |
if (x1 <= x0) |
| 53 |
{ |
| 54 |
throw std::domain_error("Abscissas must be sorted in strictly increasing order x0 < x1 < ... < x_{n-1}"); |
| 55 |
} |
| 56 |
x0 = x1; |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
void push_back(Real x, Real y, Real dydx, Real d2ydx2, Real d3ydx3) |
| 61 |
{ |
| 62 |
using std::abs; |
| 63 |
using std::isnan; |
| 64 |
if (x <= x_.back()) { |
| 65 |
throw std::domain_error("Calling push_back must preserve the monotonicity of the x's"); |
| 66 |
} |
| 67 |
x_.push_back(x); |
| 68 |
y_.push_back(y); |
| 69 |
dydx_.push_back(dydx); |
| 70 |
d2ydx2_.push_back(d2ydx2); |
| 71 |
d3ydx3_.push_back(d3ydx3); |
| 72 |
} |
| 73 |
|
| 74 |
Real operator()(Real x) const |
| 75 |
{ |
| 76 |
if (x < x_[0] || x > x_.back()) |
| 77 |
{ |
| 78 |
std::ostringstream oss; |
| 79 |
oss.precision(std::numeric_limits<Real>::digits10+3); |
| 80 |
oss << "Requested abscissa x = " << x << ", which is outside of allowed range [" |
| 81 |
<< x_[0] << ", " << x_.back() << "]"; |
| 82 |
throw std::domain_error(oss.str()); |
| 83 |
} |
| 84 |
// t \in [0, 1) |
| 85 |
if (x == x_.back()) |
| 86 |
{ |
| 87 |
return y_.back(); |
| 88 |
} |
| 89 |
|
| 90 |
auto it = std::upper_bound(x_.begin(), x_.end(), x); |
| 91 |
auto i = std::distance(x_.begin(), it) -1; |
| 92 |
Real x0 = *(it-1); |
| 93 |
Real x1 = *it; |
| 94 |
Real dx = (x1-x0); |
| 95 |
Real t = (x-x0)/dx; |
| 96 |
|
| 97 |
// See: |
| 98 |
// http://seisweb.usask.ca/classes/GEOL481/2017/Labs/interpolation_utilities_matlab/shermite.m |
| 99 |
Real t2 = t*t; |
| 100 |
Real t3 = t2*t; |
| 101 |
Real t4 = t3*t; |
| 102 |
Real dx2 = dx*dx/2; |
| 103 |
Real dx3 = dx2*dx/3; |
| 104 |
|
| 105 |
Real s = t4*(-35 + t*(84 + t*(-70 + 20*t))); |
| 106 |
Real z4 = -s; |
| 107 |
Real z0 = s + 1; |
| 108 |
Real z1 = t*(1 + t3*(-20 + t*(45 + t*(-36 + 10*t)))); |
| 109 |
Real z2 = t2*(1 + t2*(-10 + t*(20 + t*(-15 + 4*t)))); |
| 110 |
Real z3 = t3*(1 + t*(-4 + t*(6 + t*(-4 + t)))); |
| 111 |
Real z5 = t4*(-15 + t*(39 + t*(-34 + 10*t))); |
| 112 |
Real z6 = t4*(5 + t*(-14 + t*(13 - 4*t))); |
| 113 |
Real z7 = t4*(-1 + t*(3 + t*(-3+t))); |
| 114 |
|
| 115 |
Real y0 = y_[i]; |
| 116 |
Real y1 = y_[i+1]; |
| 117 |
// Velocity: |
| 118 |
Real v0 = dydx_[i]; |
| 119 |
Real v1 = dydx_[i+1]; |
| 120 |
// Acceleration: |
| 121 |
Real a0 = d2ydx2_[i]; |
| 122 |
Real a1 = d2ydx2_[i+1]; |
| 123 |
// Jerk: |
| 124 |
Real j0 = d3ydx3_[i]; |
| 125 |
Real j1 = d3ydx3_[i+1]; |
| 126 |
|
| 127 |
return z0*y0 + z4*y1 + (z1*v0 + z5*v1)*dx + (z2*a0 + z6*a1)*dx2 + (z3*j0 + z7*j1)*dx3; |
| 128 |
} |
| 129 |
|
| 130 |
Real prime(Real x) const |
| 131 |
{ |
| 132 |
if (x < x_[0] || x > x_.back()) |
| 133 |
{ |
| 134 |
std::ostringstream oss; |
| 135 |
oss.precision(std::numeric_limits<Real>::digits10+3); |
| 136 |
oss << "Requested abscissa x = " << x << ", which is outside of allowed range [" |
| 137 |
<< x_[0] << ", " << x_.back() << "]"; |
| 138 |
throw std::domain_error(oss.str()); |
| 139 |
} |
| 140 |
if (x == x_.back()) |
| 141 |
{ |
| 142 |
return dydx_.back(); |
| 143 |
} |
| 144 |
|
| 145 |
auto it = std::upper_bound(x_.begin(), x_.end(), x); |
| 146 |
auto i = std::distance(x_.begin(), it) -1; |
| 147 |
Real x0 = *(it-1); |
| 148 |
Real x1 = *it; |
| 149 |
Real y0 = y_[i]; |
| 150 |
Real y1 = y_[i+1]; |
| 151 |
Real v0 = dydx_[i]; |
| 152 |
Real v1 = dydx_[i+1]; |
| 153 |
Real a0 = d2ydx2_[i]; |
| 154 |
Real a1 = d2ydx2_[i+1]; |
| 155 |
Real j0 = d3ydx3_[i]; |
| 156 |
Real j1 = d3ydx3_[i+1]; |
| 157 |
Real dx = x1 - x0; |
| 158 |
Real t = (x-x0)/dx; |
| 159 |
Real t2 = t*t; |
| 160 |
Real t3 = t2*t; |
| 161 |
Real z0 = 140*t3*(1 + t*(-3 + t*(3 - t))); |
| 162 |
Real z1 = 1 + t3*(-80 + t*(225 + t*(-216 + 70*t))); |
| 163 |
Real z2 = t3*(-60 + t*(195 + t*(-204 + 70*t))); |
| 164 |
Real z3 = 1 + t2*(-20 + t*(50 + t*(-45 + 14*t))); |
| 165 |
Real z4 = t2*(10 + t*(-35 + t*(39 - 14*t))); |
| 166 |
Real z5 = 3 + t*(-16 + t*(30 + t*(-24 + 7*t))); |
| 167 |
Real z6 = t*(-4 + t*(15 + t*(-18 + 7*t))); |
| 168 |
|
| 169 |
Real dydx = z0*(y1-y0)/dx; |
| 170 |
dydx += z1*v0 + z2*v1; |
| 171 |
dydx += (x-x0)*(z3*a0 + z4*a1); |
| 172 |
dydx += (x-x0)*(x-x0)*(z5*j0 + z6*j1)/6; |
| 173 |
return dydx; |
| 174 |
} |
| 175 |
|
| 176 |
inline Real double_prime(Real) const |
| 177 |
{ |
| 178 |
return std::numeric_limits<Real>::quiet_NaN(); |
| 179 |
} |
| 180 |
|
| 181 |
friend std::ostream& operator<<(std::ostream & os, const septic_hermite_detail & m) |
| 182 |
{ |
| 183 |
os << "(x,y,y') = {"; |
| 184 |
for (size_t i = 0; i < m.x_.size() - 1; ++i) { |
| 185 |
os << "(" << m.x_[i] << ", " << m.y_[i] << ", " << m.dydx_[i] << ", " << m.d2ydx2_[i] << ", " << m.d3ydx3_[i] << "), "; |
| 186 |
} |
| 187 |
auto n = m.x_.size()-1; |
| 188 |
os << "(" << m.x_[n] << ", " << m.y_[n] << ", " << m.dydx_[n] << ", " << m.d2ydx2_[n] << m.d3ydx3_[n] << ")}"; |
| 189 |
return os; |
| 190 |
} |
| 191 |
|
| 192 |
int64_t bytes() |
| 193 |
{ |
| 194 |
return 5*x_.size()*sizeof(Real) + 5*sizeof(x_); |
| 195 |
} |
| 196 |
|
| 197 |
std::pair<Real, Real> domain() const |
| 198 |
{ |
| 199 |
return {x_.front(), x_.back()}; |
| 200 |
} |
| 201 |
|
| 202 |
private: |
| 203 |
RandomAccessContainer x_; |
| 204 |
RandomAccessContainer y_; |
| 205 |
RandomAccessContainer dydx_; |
| 206 |
RandomAccessContainer d2ydx2_; |
| 207 |
RandomAccessContainer d3ydx3_; |
| 208 |
}; |
| 209 |
|
| 210 |
template<class RandomAccessContainer> |
| 211 |
class cardinal_septic_hermite_detail { |
| 212 |
public: |
| 213 |
using Real = typename RandomAccessContainer::value_type; |
| 214 |
cardinal_septic_hermite_detail(RandomAccessContainer && y, RandomAccessContainer && dydx, RandomAccessContainer && d2ydx2, RandomAccessContainer && d3ydx3, Real x0, Real dx) |
| 215 |
: y_{std::move(y)}, dy_{std::move(dydx)}, d2y_{std::move(d2ydx2)}, d3y_{std::move(d3ydx3)}, x0_{x0}, inv_dx_{1/dx} |
| 216 |
{ |
| 217 |
if (y_.size() != dy_.size()) |
| 218 |
{ |
| 219 |
throw std::domain_error("Numbers of derivatives must = number of ordinates."); |
| 220 |
} |
| 221 |
if (y_.size() != d2y_.size()) |
| 222 |
{ |
| 223 |
throw std::domain_error("Number of second derivatives must equal number of ordinates."); |
| 224 |
} |
| 225 |
if (y_.size() != d3y_.size()) |
| 226 |
{ |
| 227 |
throw std::domain_error("Number of third derivatives must equal number of ordinates."); |
| 228 |
} |
| 229 |
if (y_.size() < 2) |
| 230 |
{ |
| 231 |
throw std::domain_error("At least 2 abscissas are required."); |
| 232 |
} |
| 233 |
|
| 234 |
if (dx <= 0) |
| 235 |
{ |
| 236 |
throw std::domain_error("dx > 0 is required."); |
| 237 |
} |
| 238 |
|
| 239 |
for (auto & dy : dy_) |
| 240 |
{ |
| 241 |
dy *= dx; |
| 242 |
} |
| 243 |
for (auto & d2y : d2y_) |
| 244 |
{ |
| 245 |
d2y *= (dx*dx/2); |
| 246 |
} |
| 247 |
for (auto & d3y : d3y_) |
| 248 |
{ |
| 249 |
d3y *= (dx*dx*dx/6); |
| 250 |
} |
| 251 |
|
| 252 |
} |
| 253 |
|
| 254 |
inline Real operator()(Real x) const |
| 255 |
{ |
| 256 |
Real xf = x0_ + (y_.size()-1)/inv_dx_; |
| 257 |
if (x < x0_ || x > xf) |
| 258 |
{ |
| 259 |
std::ostringstream oss; |
| 260 |
oss.precision(std::numeric_limits<Real>::digits10+3); |
| 261 |
oss << "Requested abscissa x = " << x << ", which is outside of allowed range [" |
| 262 |
<< x0_ << ", " << xf << "]"; |
| 263 |
throw std::domain_error(oss.str()); |
| 264 |
} |
| 265 |
if (x == xf) |
| 266 |
{ |
| 267 |
return y_.back(); |
| 268 |
} |
| 269 |
return this->unchecked_evaluation(x); |
| 270 |
} |
| 271 |
|
| 272 |
inline Real unchecked_evaluation(Real x) const { |
| 273 |
using std::floor; |
| 274 |
Real s3 = (x-x0_)*inv_dx_; |
| 275 |
Real ii = floor(s3); |
| 276 |
auto i = static_cast<decltype(y_.size())>(ii); |
| 277 |
Real t = s3 - ii; |
| 278 |
if (t == 0) { |
| 279 |
return y_[i]; |
| 280 |
} |
| 281 |
// See: |
| 282 |
// http://seisweb.usask.ca/classes/GEOL481/2017/Labs/interpolation_utilities_matlab/shermite.m |
| 283 |
Real t2 = t*t; |
| 284 |
Real t3 = t2*t; |
| 285 |
Real t4 = t3*t; |
| 286 |
|
| 287 |
Real s = t4*(-35 + t*(84 + t*(-70 + 20*t))); |
| 288 |
Real z4 = -s; |
| 289 |
Real z0 = s + 1; |
| 290 |
Real z1 = t*(1 + t3*(-20 + t*(45 + t*(-36+10*t)))); |
| 291 |
Real z2 = t2*(1 + t2*(-10 + t*(20 + t*(-15+4*t)))); |
| 292 |
Real z3 = t3*(1 + t*(-4+t*(6+t*(-4+t)))); |
| 293 |
Real z5 = t4*(-15 + t*(39 + t*(-34 + 10*t))); |
| 294 |
Real z6 = t4*(5 + t*(-14 + t*(13-4*t))); |
| 295 |
Real z7 = t4*(-1 + t*(3+t*(-3+t))); |
| 296 |
|
| 297 |
Real y0 = y_[i]; |
| 298 |
Real y1 = y_[i+1]; |
| 299 |
Real dy0 = dy_[i]; |
| 300 |
Real dy1 = dy_[i+1]; |
| 301 |
Real a0 = d2y_[i]; |
| 302 |
Real a1 = d2y_[i+1]; |
| 303 |
Real j0 = d3y_[i]; |
| 304 |
Real j1 = d3y_[i+1]; |
| 305 |
|
| 306 |
return z0*y0 + z1*dy0 + z2*a0 + z3*j0 + z4*y1 + z5*dy1 + z6*a1 + z7*j1; |
| 307 |
} |
| 308 |
|
| 309 |
inline Real prime(Real x) const |
| 310 |
{ |
| 311 |
Real xf = x0_ + (y_.size()-1)/inv_dx_; |
| 312 |
if (x < x0_ || x > xf) |
| 313 |
{ |
| 314 |
std::ostringstream oss; |
| 315 |
oss.precision(std::numeric_limits<Real>::digits10+3); |
| 316 |
oss << "Requested abscissa x = " << x << ", which is outside of allowed range [" |
| 317 |
<< x0_ << ", " << xf << "]"; |
| 318 |
throw std::domain_error(oss.str()); |
| 319 |
} |
| 320 |
if (x == xf) |
| 321 |
{ |
| 322 |
return dy_.back()/inv_dx_; |
| 323 |
} |
| 324 |
|
| 325 |
return this->unchecked_prime(x); |
| 326 |
} |
| 327 |
|
| 328 |
inline Real unchecked_prime(Real x) const |
| 329 |
{ |
| 330 |
using std::floor; |
| 331 |
Real s3 = (x-x0_)*inv_dx_; |
| 332 |
Real ii = floor(s3); |
| 333 |
auto i = static_cast<decltype(y_.size())>(ii); |
| 334 |
Real t = s3 - ii; |
| 335 |
if (t==0) |
| 336 |
{ |
| 337 |
return dy_[i]/inv_dx_; |
| 338 |
} |
| 339 |
|
| 340 |
Real y0 = y_[i]; |
| 341 |
Real y1 = y_[i+1]; |
| 342 |
Real dy0 = dy_[i]; |
| 343 |
Real dy1 = dy_[i+1]; |
| 344 |
Real a0 = d2y_[i]; |
| 345 |
Real a1 = d2y_[i+1]; |
| 346 |
Real j0 = d3y_[i]; |
| 347 |
Real j1 = d3y_[i+1]; |
| 348 |
Real t2 = t*t; |
| 349 |
Real t3 = t2*t; |
| 350 |
Real z0 = 140*t3*(1 + t*(-3 + t*(3 - t))); |
| 351 |
Real z1 = 1 + t3*(-80 + t*(225 + t*(-216 + 70*t))); |
| 352 |
Real z2 = t3*(-60 + t*(195 + t*(-204 + 70*t))); |
| 353 |
Real z3 = 1 + t2*(-20 + t*(50 + t*(-45 + 14*t))); |
| 354 |
Real z4 = t2*(10 + t*(-35 + t*(39 - 14*t))); |
| 355 |
Real z5 = 3 + t*(-16 + t*(30 + t*(-24 + 7*t))); |
| 356 |
Real z6 = t*(-4 + t*(15 + t*(-18 + 7*t))); |
| 357 |
|
| 358 |
Real dydx = z0*(y1-y0)*inv_dx_; |
| 359 |
dydx += (z1*dy0 + z2*dy1)*inv_dx_; |
| 360 |
dydx += 2*t*(z3*a0 + z4*a1)*inv_dx_; |
| 361 |
dydx += t*t*(z5*j0 + z6*j1); |
| 362 |
return dydx; |
| 363 |
} |
| 364 |
|
| 365 |
inline Real double_prime(Real x) const |
| 366 |
{ |
| 367 |
Real xf = x0_ + (y_.size()-1)/inv_dx_; |
| 368 |
if (x < x0_ || x > xf) |
| 369 |
{ |
| 370 |
std::ostringstream oss; |
| 371 |
oss.precision(std::numeric_limits<Real>::digits10+3); |
| 372 |
oss << "Requested abscissa x = " << x << ", which is outside of allowed range [" |
| 373 |
<< x0_ << ", " << xf << "]"; |
| 374 |
throw std::domain_error(oss.str()); |
| 375 |
} |
| 376 |
if (x == xf) |
| 377 |
{ |
| 378 |
return d2y_.back()*2*inv_dx_*inv_dx_; |
| 379 |
} |
| 380 |
|
| 381 |
return this->unchecked_double_prime(x); |
| 382 |
} |
| 383 |
|
| 384 |
inline Real unchecked_double_prime(Real x) const |
| 385 |
{ |
| 386 |
using std::floor; |
| 387 |
Real s3 = (x-x0_)*inv_dx_; |
| 388 |
Real ii = floor(s3); |
| 389 |
auto i = static_cast<decltype(y_.size())>(ii); |
| 390 |
Real t = s3 - ii; |
| 391 |
if (t==0) |
| 392 |
{ |
| 393 |
return d2y_[i]*2*inv_dx_*inv_dx_; |
| 394 |
} |
| 395 |
|
| 396 |
Real y0 = y_[i]; |
| 397 |
Real y1 = y_[i+1]; |
| 398 |
Real dy0 = dy_[i]; |
| 399 |
Real dy1 = dy_[i+1]; |
| 400 |
Real a0 = d2y_[i]; |
| 401 |
Real a1 = d2y_[i+1]; |
| 402 |
Real j0 = d3y_[i]; |
| 403 |
Real j1 = d3y_[i+1]; |
| 404 |
Real t2 = t*t; |
| 405 |
|
| 406 |
Real z0 = 420*t2*(1 + t*(-4 + t*(5 - 2*t))); |
| 407 |
Real z1 = 60*t2*(-4 + t*(15 + t*(-18 + 7*t))); |
| 408 |
Real z2 = 60*t2*(-3 + t*(13 + t*(-17 + 7*t))); |
| 409 |
Real z3 = (1 + t2*(-60 + t*(200 + t*(-225 + 84*t)))); |
| 410 |
Real z4 = t2*(30 + t*(-140 + t*(195 - 84*t))); |
| 411 |
Real z5 = t*(1 + t*(-8 + t*(20 + t*(-20 + 7*t)))); |
| 412 |
Real z6 = t2*(-2 + t*(10 + t*(-15 + 7*t))); |
| 413 |
|
| 414 |
Real d2ydx2 = z0*(y1-y0)*inv_dx_*inv_dx_; |
| 415 |
d2ydx2 += (z1*dy0 + z2*dy1)*inv_dx_*inv_dx_; |
| 416 |
d2ydx2 += (z3*a0 + z4*a1)*2*inv_dx_*inv_dx_; |
| 417 |
d2ydx2 += 6*(z5*j0 + z6*j1)/(inv_dx_*inv_dx_); |
| 418 |
|
| 419 |
return d2ydx2; |
| 420 |
} |
| 421 |
|
| 422 |
int64_t bytes() const |
| 423 |
{ |
| 424 |
return 4*y_.size()*sizeof(Real) + 2*sizeof(Real) + 4*sizeof(y_); |
| 425 |
} |
| 426 |
|
| 427 |
std::pair<Real, Real> domain() const |
| 428 |
{ |
| 429 |
return {x0_, x0_ + (y_.size()-1)/inv_dx_}; |
| 430 |
} |
| 431 |
|
| 432 |
private: |
| 433 |
RandomAccessContainer y_; |
| 434 |
RandomAccessContainer dy_; |
| 435 |
RandomAccessContainer d2y_; |
| 436 |
RandomAccessContainer d3y_; |
| 437 |
Real x0_; |
| 438 |
Real inv_dx_; |
| 439 |
}; |
| 440 |
|
| 441 |
|
| 442 |
template<class RandomAccessContainer> |
| 443 |
class cardinal_septic_hermite_detail_aos { |
| 444 |
public: |
| 445 |
using Point = typename RandomAccessContainer::value_type; |
| 446 |
using Real = typename Point::value_type; |
| 447 |
cardinal_septic_hermite_detail_aos(RandomAccessContainer && dat, Real x0, Real dx) |
| 448 |
: data_{std::move(dat)}, x0_{x0}, inv_dx_{1/dx} |
| 449 |
{ |
| 450 |
if (data_.size() < 2) { |
| 451 |
throw std::domain_error("At least 2 abscissas are required."); |
| 452 |
} |
| 453 |
if (data_[0].size() != 4) { |
| 454 |
throw std::domain_error("There must be 4 data items per struct."); |
| 455 |
} |
| 456 |
|
| 457 |
for (auto & datum : data_) |
| 458 |
{ |
| 459 |
datum[1] *= dx; |
| 460 |
datum[2] *= (dx*dx/2); |
| 461 |
datum[3] *= (dx*dx*dx/6); |
| 462 |
} |
| 463 |
} |
| 464 |
|
| 465 |
inline Real operator()(Real x) const |
| 466 |
{ |
| 467 |
Real xf = x0_ + (data_.size()-1)/inv_dx_; |
| 468 |
if (x < x0_ || x > xf) |
| 469 |
{ |
| 470 |
std::ostringstream oss; |
| 471 |
oss.precision(std::numeric_limits<Real>::digits10+3); |
| 472 |
oss << "Requested abscissa x = " << x << ", which is outside of allowed range [" |
| 473 |
<< x0_ << ", " << xf << "]"; |
| 474 |
throw std::domain_error(oss.str()); |
| 475 |
} |
| 476 |
if (x == xf) |
| 477 |
{ |
| 478 |
return data_.back()[0]; |
| 479 |
} |
| 480 |
return this->unchecked_evaluation(x); |
| 481 |
} |
| 482 |
|
| 483 |
inline Real unchecked_evaluation(Real x) const |
| 484 |
{ |
| 485 |
using std::floor; |
| 486 |
Real s3 = (x-x0_)*inv_dx_; |
| 487 |
Real ii = floor(s3); |
| 488 |
auto i = static_cast<decltype(data_.size())>(ii); |
| 489 |
Real t = s3 - ii; |
| 490 |
if (t==0) |
| 491 |
{ |
| 492 |
return data_[i][0]; |
| 493 |
} |
| 494 |
Real t2 = t*t; |
| 495 |
Real t3 = t2*t; |
| 496 |
Real t4 = t3*t; |
| 497 |
|
| 498 |
Real s = t4*(-35 + t*(84 + t*(-70 + 20*t))); |
| 499 |
Real z4 = -s; |
| 500 |
Real z0 = s + 1; |
| 501 |
Real z1 = t*(1 + t3*(-20 + t*(45 + t*(-36+10*t)))); |
| 502 |
Real z2 = t2*(1 + t2*(-10 + t*(20 + t*(-15+4*t)))); |
| 503 |
Real z3 = t3*(1 + t*(-4+t*(6+t*(-4+t)))); |
| 504 |
Real z5 = t4*(-15 + t*(39 + t*(-34 + 10*t))); |
| 505 |
Real z6 = t4*(5 + t*(-14 + t*(13-4*t))); |
| 506 |
Real z7 = t4*(-1 + t*(3+t*(-3+t))); |
| 507 |
|
| 508 |
Real y0 = data_[i][0]; |
| 509 |
Real dy0 = data_[i][1]; |
| 510 |
Real a0 = data_[i][2]; |
| 511 |
Real j0 = data_[i][3]; |
| 512 |
Real y1 = data_[i+1][0]; |
| 513 |
Real dy1 = data_[i+1][1]; |
| 514 |
Real a1 = data_[i+1][2]; |
| 515 |
Real j1 = data_[i+1][3]; |
| 516 |
|
| 517 |
return z0*y0 + z1*dy0 + z2*a0 + z3*j0 + z4*y1 + z5*dy1 + z6*a1 + z7*j1; |
| 518 |
} |
| 519 |
|
| 520 |
inline Real prime(Real x) const |
| 521 |
{ |
| 522 |
Real xf = x0_ + (data_.size()-1)/inv_dx_; |
| 523 |
if (x < x0_ || x > xf) |
| 524 |
{ |
| 525 |
std::ostringstream oss; |
| 526 |
oss.precision(std::numeric_limits<Real>::digits10+3); |
| 527 |
oss << "Requested abscissa x = " << x << ", which is outside of allowed range [" |
| 528 |
<< x0_ << ", " << xf << "]"; |
| 529 |
throw std::domain_error(oss.str()); |
| 530 |
} |
| 531 |
if (x == xf) |
| 532 |
{ |
| 533 |
return data_.back()[1]*inv_dx_; |
| 534 |
} |
| 535 |
|
| 536 |
return this->unchecked_prime(x); |
| 537 |
} |
| 538 |
|
| 539 |
inline Real unchecked_prime(Real x) const |
| 540 |
{ |
| 541 |
using std::floor; |
| 542 |
Real s3 = (x-x0_)*inv_dx_; |
| 543 |
Real ii = floor(s3); |
| 544 |
auto i = static_cast<decltype(data_.size())>(ii); |
| 545 |
Real t = s3 - ii; |
| 546 |
if (t == 0) |
| 547 |
{ |
| 548 |
return data_[i][1]*inv_dx_; |
| 549 |
} |
| 550 |
|
| 551 |
Real y0 = data_[i][0]; |
| 552 |
Real y1 = data_[i+1][0]; |
| 553 |
Real dy0 = data_[i][1]; |
| 554 |
Real dy1 = data_[i+1][1]; |
| 555 |
Real a0 = data_[i][2]; |
| 556 |
Real a1 = data_[i+1][2]; |
| 557 |
Real j0 = data_[i][3]; |
| 558 |
Real j1 = data_[i+1][3]; |
| 559 |
Real t2 = t*t; |
| 560 |
Real t3 = t2*t; |
| 561 |
Real z0 = 140*t3*(1 + t*(-3 + t*(3 - t))); |
| 562 |
Real z1 = 1 + t3*(-80 + t*(225 + t*(-216 + 70*t))); |
| 563 |
Real z2 = t3*(-60 + t*(195 + t*(-204 + 70*t))); |
| 564 |
Real z3 = 1 + t2*(-20 + t*(50 + t*(-45 + 14*t))); |
| 565 |
Real z4 = t2*(10 + t*(-35 + t*(39 - 14*t))); |
| 566 |
Real z5 = 3 + t*(-16 + t*(30 + t*(-24 + 7*t))); |
| 567 |
Real z6 = t*(-4 + t*(15 + t*(-18 + 7*t))); |
| 568 |
|
| 569 |
Real dydx = z0*(y1-y0)*inv_dx_; |
| 570 |
dydx += (z1*dy0 + z2*dy1)*inv_dx_; |
| 571 |
dydx += 2*t*(z3*a0 + z4*a1)*inv_dx_; |
| 572 |
dydx += t*t*(z5*j0 + z6*j1); |
| 573 |
return dydx; |
| 574 |
} |
| 575 |
|
| 576 |
inline Real double_prime(Real x) const |
| 577 |
{ |
| 578 |
Real xf = x0_ + (data_.size()-1)/inv_dx_; |
| 579 |
if (x < x0_ || x > xf) |
| 580 |
{ |
| 581 |
std::ostringstream oss; |
| 582 |
oss.precision(std::numeric_limits<Real>::digits10+3); |
| 583 |
oss << "Requested abscissa x = " << x << ", which is outside of allowed range [" |
| 584 |
<< x0_ << ", " << xf << "]"; |
| 585 |
throw std::domain_error(oss.str()); |
| 586 |
} |
| 587 |
if (x == xf) |
| 588 |
{ |
| 589 |
return data_.back()[2]*2*inv_dx_*inv_dx_; |
| 590 |
} |
| 591 |
|
| 592 |
return this->unchecked_double_prime(x); |
| 593 |
} |
| 594 |
|
| 595 |
inline Real unchecked_double_prime(Real x) const |
| 596 |
{ |
| 597 |
using std::floor; |
| 598 |
Real s3 = (x-x0_)*inv_dx_; |
| 599 |
Real ii = floor(s3); |
| 600 |
auto i = static_cast<decltype(data_.size())>(ii); |
| 601 |
Real t = s3 - ii; |
| 602 |
if (t == 0) |
| 603 |
{ |
| 604 |
return data_[i][2]*2*inv_dx_*inv_dx_; |
| 605 |
} |
| 606 |
Real y0 = data_[i][0]; |
| 607 |
Real y1 = data_[i+1][0]; |
| 608 |
Real dy0 = data_[i][1]; |
| 609 |
Real dy1 = data_[i+1][1]; |
| 610 |
Real a0 = data_[i][2]; |
| 611 |
Real a1 = data_[i+1][2]; |
| 612 |
Real j0 = data_[i][3]; |
| 613 |
Real j1 = data_[i+1][3]; |
| 614 |
Real t2 = t*t; |
| 615 |
|
| 616 |
Real z0 = 420*t2*(1 + t*(-4 + t*(5 - 2*t))); |
| 617 |
Real z1 = 60*t2*(-4 + t*(15 + t*(-18 + 7*t))); |
| 618 |
Real z2 = 60*t2*(-3 + t*(13 + t*(-17 + 7*t))); |
| 619 |
Real z3 = (1 + t2*(-60 + t*(200 + t*(-225 + 84*t)))); |
| 620 |
Real z4 = t2*(30 + t*(-140 + t*(195 - 84*t))); |
| 621 |
Real z5 = t*(1 + t*(-8 + t*(20 + t*(-20 + 7*t)))); |
| 622 |
Real z6 = t2*(-2 + t*(10 + t*(-15 + 7*t))); |
| 623 |
|
| 624 |
Real d2ydx2 = z0*(y1-y0)*inv_dx_*inv_dx_; |
| 625 |
d2ydx2 += (z1*dy0 + z2*dy1)*inv_dx_*inv_dx_; |
| 626 |
d2ydx2 += (z3*a0 + z4*a1)*2*inv_dx_*inv_dx_; |
| 627 |
d2ydx2 += 6*(z5*j0 + z6*j1)/(inv_dx_*inv_dx_); |
| 628 |
|
| 629 |
return d2ydx2; |
| 630 |
} |
| 631 |
|
| 632 |
int64_t bytes() const |
| 633 |
{ |
| 634 |
return data_.size()*data_[0].size()*sizeof(Real) + 2*sizeof(Real) + sizeof(data_); |
| 635 |
} |
| 636 |
|
| 637 |
std::pair<Real, Real> domain() const |
| 638 |
{ |
| 639 |
return {x0_, x0_ + (data_.size() -1)/inv_dx_}; |
| 640 |
} |
| 641 |
|
| 642 |
private: |
| 643 |
RandomAccessContainer data_; |
| 644 |
Real x0_; |
| 645 |
Real inv_dx_; |
| 646 |
}; |
| 647 |
|
| 648 |
} |
| 649 |
} |
| 650 |
} |
| 651 |
} |
| 652 |
#endif |