quintic_hermite_detail.hpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  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_QUINTIC_HERMITE_DETAIL_HPP
  8. #define BOOST_MATH_INTERPOLATORS_DETAIL_QUINTIC_HERMITE_DETAIL_HPP
  9. #include <algorithm>
  10. #include <stdexcept>
  11. #include <sstream>
  12. #include <limits>
  13. #include <cmath>
  14. namespace boost {
  15. namespace math {
  16. namespace interpolators {
  17. namespace detail {
  18. template<class RandomAccessContainer>
  19. class quintic_hermite_detail {
  20. public:
  21. using Real = typename RandomAccessContainer::value_type;
  22. 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)}
  23. {
  24. if (x_.size() != y_.size())
  25. {
  26. throw std::domain_error("Number of abscissas must = number of ordinates.");
  27. }
  28. if (x_.size() != dydx_.size())
  29. {
  30. throw std::domain_error("Numbers of derivatives must = number of abscissas.");
  31. }
  32. if (x_.size() != d2ydx2_.size())
  33. {
  34. throw std::domain_error("Number of second derivatives must equal number of abscissas.");
  35. }
  36. if (x_.size() < 2)
  37. {
  38. throw std::domain_error("At least 2 abscissas are required.");
  39. }
  40. Real x0 = x_[0];
  41. for (decltype(x_.size()) i = 1; i < x_.size(); ++i)
  42. {
  43. Real x1 = x_[i];
  44. if (x1 <= x0)
  45. {
  46. throw std::domain_error("Abscissas must be sorted in strictly increasing order x0 < x1 < ... < x_{n-1}");
  47. }
  48. x0 = x1;
  49. }
  50. }
  51. void push_back(Real x, Real y, Real dydx, Real d2ydx2)
  52. {
  53. using std::abs;
  54. using std::isnan;
  55. if (x <= x_.back())
  56. {
  57. throw std::domain_error("Calling push_back must preserve the monotonicity of the x's");
  58. }
  59. x_.push_back(x);
  60. y_.push_back(y);
  61. dydx_.push_back(dydx);
  62. d2ydx2_.push_back(d2ydx2);
  63. }
  64. inline Real operator()(Real x) const
  65. {
  66. if (x < x_[0] || x > x_.back())
  67. {
  68. std::ostringstream oss;
  69. oss.precision(std::numeric_limits<Real>::digits10+3);
  70. oss << "Requested abscissa x = " << x << ", which is outside of allowed range ["
  71. << x_[0] << ", " << x_.back() << "]";
  72. throw std::domain_error(oss.str());
  73. }
  74. // We need t := (x-x_k)/(x_{k+1}-x_k) \in [0,1) for this to work.
  75. // Sadly this neccessitates this loathesome check, otherwise we get t = 1 at x = xf.
  76. if (x == x_.back())
  77. {
  78. return y_.back();
  79. }
  80. auto it = std::upper_bound(x_.begin(), x_.end(), x);
  81. auto i = std::distance(x_.begin(), it) -1;
  82. Real x0 = *(it-1);
  83. Real x1 = *it;
  84. Real y0 = y_[i];
  85. Real y1 = y_[i+1];
  86. Real v0 = dydx_[i];
  87. Real v1 = dydx_[i+1];
  88. Real a0 = d2ydx2_[i];
  89. Real a1 = d2ydx2_[i+1];
  90. Real dx = (x1-x0);
  91. Real t = (x-x0)/dx;
  92. Real t2 = t*t;
  93. Real t3 = t2*t;
  94. // See the 'Basis functions' section of:
  95. // https://www.rose-hulman.edu/~finn/CCLI/Notes/day09.pdf
  96. // Also: https://github.com/MrHexxx/QuinticHermiteSpline/blob/master/HermiteSpline.cs
  97. Real y = (1- t3*(10 + t*(-15 + 6*t)))*y0;
  98. y += t*(1+ t2*(-6 + t*(8 -3*t)))*v0*dx;
  99. y += t2*(1 + t*(-3 + t*(3-t)))*a0*dx*dx/2;
  100. y += t3*((1 + t*(-2 + t))*a1*dx*dx/2 + (-4 + t*(7 - 3*t))*v1*dx + (10 + t*(-15 + 6*t))*y1);
  101. return y;
  102. }
  103. inline Real prime(Real x) const
  104. {
  105. if (x < x_[0] || x > x_.back())
  106. {
  107. std::ostringstream oss;
  108. oss.precision(std::numeric_limits<Real>::digits10+3);
  109. oss << "Requested abscissa x = " << x << ", which is outside of allowed range ["
  110. << x_[0] << ", " << x_.back() << "]";
  111. throw std::domain_error(oss.str());
  112. }
  113. if (x == x_.back())
  114. {
  115. return dydx_.back();
  116. }
  117. auto it = std::upper_bound(x_.begin(), x_.end(), x);
  118. auto i = std::distance(x_.begin(), it) -1;
  119. Real x0 = *(it-1);
  120. Real x1 = *it;
  121. Real dx = x1 - x0;
  122. Real y0 = y_[i];
  123. Real y1 = y_[i+1];
  124. Real v0 = dydx_[i];
  125. Real v1 = dydx_[i+1];
  126. Real a0 = d2ydx2_[i];
  127. Real a1 = d2ydx2_[i+1];
  128. Real t= (x-x0)/dx;
  129. Real t2 = t*t;
  130. Real dydx = 30*t2*(1 - 2*t + t*t)*(y1-y0)/dx;
  131. dydx += (1-18*t*t + 32*t*t*t - 15*t*t*t*t)*v0 - t*t*(12 - 28*t + 15*t*t)*v1;
  132. dydx += (t*dx/2)*((2 - 9*t + 12*t*t - 5*t*t*t)*a0 + t*(3 - 8*t + 5*t*t)*a1);
  133. return dydx;
  134. }
  135. inline Real double_prime(Real x) const
  136. {
  137. if (x < x_[0] || x > x_.back())
  138. {
  139. std::ostringstream oss;
  140. oss.precision(std::numeric_limits<Real>::digits10+3);
  141. oss << "Requested abscissa x = " << x << ", which is outside of allowed range ["
  142. << x_[0] << ", " << x_.back() << "]";
  143. throw std::domain_error(oss.str());
  144. }
  145. if (x == x_.back())
  146. {
  147. return d2ydx2_.back();
  148. }
  149. auto it = std::upper_bound(x_.begin(), x_.end(), x);
  150. auto i = std::distance(x_.begin(), it) -1;
  151. Real x0 = *(it-1);
  152. Real x1 = *it;
  153. Real dx = x1 - x0;
  154. Real y0 = y_[i];
  155. Real y1 = y_[i+1];
  156. Real v0 = dydx_[i];
  157. Real v1 = dydx_[i+1];
  158. Real a0 = d2ydx2_[i];
  159. Real a1 = d2ydx2_[i+1];
  160. Real t = (x-x0)/dx;
  161. Real d2ydx2 = 60*t*(1 + t*(-3 + 2*t))*(y1-y0)/(dx*dx);
  162. d2ydx2 += 12*t*(-3 + t*(8 - 5*t))*v0/dx;
  163. d2ydx2 -= 12*t*(2 + t*(-7 + 5*t))*v1/dx;
  164. d2ydx2 += (1 + t*(-9 + t*(18 - 10*t)))*a0;
  165. d2ydx2 += t*(3 + t*(-12 + 10*t))*a1;
  166. return d2ydx2;
  167. }
  168. friend std::ostream& operator<<(std::ostream & os, const quintic_hermite_detail & m)
  169. {
  170. os << "(x,y,y') = {";
  171. for (size_t i = 0; i < m.x_.size() - 1; ++i) {
  172. os << "(" << m.x_[i] << ", " << m.y_[i] << ", " << m.dydx_[i] << ", " << m.d2ydx2_[i] << "), ";
  173. }
  174. auto n = m.x_.size()-1;
  175. os << "(" << m.x_[n] << ", " << m.y_[n] << ", " << m.dydx_[n] << ", " << m.d2ydx2_[n] << ")}";
  176. return os;
  177. }
  178. int64_t bytes() const
  179. {
  180. return 4*x_.size()*sizeof(x_);
  181. }
  182. std::pair<Real, Real> domain() const
  183. {
  184. return {x_.front(), x_.back()};
  185. }
  186. private:
  187. RandomAccessContainer x_;
  188. RandomAccessContainer y_;
  189. RandomAccessContainer dydx_;
  190. RandomAccessContainer d2ydx2_;
  191. };
  192. template<class RandomAccessContainer>
  193. class cardinal_quintic_hermite_detail {
  194. public:
  195. using Real = typename RandomAccessContainer::value_type;
  196. cardinal_quintic_hermite_detail(RandomAccessContainer && y, RandomAccessContainer && dydx, RandomAccessContainer && d2ydx2, Real x0, Real dx)
  197. : y_{std::move(y)}, dy_{std::move(dydx)}, d2y_{std::move(d2ydx2)}, x0_{x0}, inv_dx_{1/dx}
  198. {
  199. if (y_.size() != dy_.size())
  200. {
  201. throw std::domain_error("Numbers of derivatives must = number of abscissas.");
  202. }
  203. if (y_.size() != d2y_.size())
  204. {
  205. throw std::domain_error("Number of second derivatives must equal number of abscissas.");
  206. }
  207. if (y_.size() < 2)
  208. {
  209. throw std::domain_error("At least 2 abscissas are required.");
  210. }
  211. if (dx <= 0)
  212. {
  213. throw std::domain_error("dx > 0 is required.");
  214. }
  215. for (auto & dy : dy_)
  216. {
  217. dy *= dx;
  218. }
  219. for (auto & d2y : d2y_)
  220. {
  221. d2y *= (dx*dx)/2;
  222. }
  223. }
  224. inline Real operator()(Real x) const
  225. {
  226. const Real xf = x0_ + (y_.size()-1)/inv_dx_;
  227. if (x < x0_ || x > xf)
  228. {
  229. std::ostringstream oss;
  230. oss.precision(std::numeric_limits<Real>::digits10+3);
  231. oss << "Requested abscissa x = " << x << ", which is outside of allowed range ["
  232. << x0_ << ", " << xf << "]";
  233. throw std::domain_error(oss.str());
  234. }
  235. if (x == xf)
  236. {
  237. return y_.back();
  238. }
  239. return this->unchecked_evaluation(x);
  240. }
  241. inline Real unchecked_evaluation(Real x) const
  242. {
  243. using std::floor;
  244. Real s = (x-x0_)*inv_dx_;
  245. Real ii = floor(s);
  246. auto i = static_cast<decltype(y_.size())>(ii);
  247. Real t = s - ii;
  248. if (t == 0)
  249. {
  250. return y_[i];
  251. }
  252. Real y0 = y_[i];
  253. Real y1 = y_[i+1];
  254. Real dy0 = dy_[i];
  255. Real dy1 = dy_[i+1];
  256. Real d2y0 = d2y_[i];
  257. Real d2y1 = d2y_[i+1];
  258. // See the 'Basis functions' section of:
  259. // https://www.rose-hulman.edu/~finn/CCLI/Notes/day09.pdf
  260. // Also: https://github.com/MrHexxx/QuinticHermiteSpline/blob/master/HermiteSpline.cs
  261. Real y = (1- t*t*t*(10 + t*(-15 + 6*t)))*y0;
  262. y += t*(1+ t*t*(-6 + t*(8 -3*t)))*dy0;
  263. y += t*t*(1 + t*(-3 + t*(3-t)))*d2y0;
  264. y += t*t*t*((1 + t*(-2 + t))*d2y1 + (-4 + t*(7 -3*t))*dy1 + (10 + t*(-15 + 6*t))*y1);
  265. return y;
  266. }
  267. inline Real prime(Real x) const
  268. {
  269. const Real xf = x0_ + (y_.size()-1)/inv_dx_;
  270. if (x < x0_ || x > xf)
  271. {
  272. std::ostringstream oss;
  273. oss.precision(std::numeric_limits<Real>::digits10+3);
  274. oss << "Requested abscissa x = " << x << ", which is outside of allowed range ["
  275. << x0_ << ", " << xf << "]";
  276. throw std::domain_error(oss.str());
  277. }
  278. if (x == xf)
  279. {
  280. return dy_.back()*inv_dx_;
  281. }
  282. return this->unchecked_prime(x);
  283. }
  284. inline Real unchecked_prime(Real x) const
  285. {
  286. using std::floor;
  287. Real s = (x-x0_)*inv_dx_;
  288. Real ii = floor(s);
  289. auto i = static_cast<decltype(y_.size())>(ii);
  290. Real t = s - ii;
  291. if (t == 0)
  292. {
  293. return dy_[i]*inv_dx_;
  294. }
  295. Real y0 = y_[i];
  296. Real y1 = y_[i+1];
  297. Real dy0 = dy_[i];
  298. Real dy1 = dy_[i+1];
  299. Real d2y0 = d2y_[i];
  300. Real d2y1 = d2y_[i+1];
  301. Real dydx = 30*t*t*(1 - 2*t + t*t)*(y1-y0);
  302. dydx += (1-18*t*t + 32*t*t*t - 15*t*t*t*t)*dy0 - t*t*(12 - 28*t + 15*t*t)*dy1;
  303. dydx += t*((2 - 9*t + 12*t*t - 5*t*t*t)*d2y0 + t*(3 - 8*t + 5*t*t)*d2y1);
  304. dydx *= inv_dx_;
  305. return dydx;
  306. }
  307. inline Real double_prime(Real x) const
  308. {
  309. const Real xf = x0_ + (y_.size()-1)/inv_dx_;
  310. if (x < x0_ || x > xf) {
  311. std::ostringstream oss;
  312. oss.precision(std::numeric_limits<Real>::digits10+3);
  313. oss << "Requested abscissa x = " << x << ", which is outside of allowed range ["
  314. << x0_ << ", " << xf << "]";
  315. throw std::domain_error(oss.str());
  316. }
  317. if (x == xf)
  318. {
  319. return d2y_.back()*2*inv_dx_*inv_dx_;
  320. }
  321. return this->unchecked_double_prime(x);
  322. }
  323. inline Real unchecked_double_prime(Real x) const
  324. {
  325. using std::floor;
  326. Real s = (x-x0_)*inv_dx_;
  327. Real ii = floor(s);
  328. auto i = static_cast<decltype(y_.size())>(ii);
  329. Real t = s - ii;
  330. if (t==0)
  331. {
  332. return d2y_[i]*2*inv_dx_*inv_dx_;
  333. }
  334. Real y0 = y_[i];
  335. Real y1 = y_[i+1];
  336. Real dy0 = dy_[i];
  337. Real dy1 = dy_[i+1];
  338. Real d2y0 = d2y_[i];
  339. Real d2y1 = d2y_[i+1];
  340. Real d2ydx2 = 60*t*(1 - 3*t + 2*t*t)*(y1 - y0)*inv_dx_*inv_dx_;
  341. d2ydx2 += (12*t)*((-3 + 8*t - 5*t*t)*dy0 - (2 - 7*t + 5*t*t)*dy1);
  342. 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_);
  343. return d2ydx2;
  344. }
  345. int64_t bytes() const
  346. {
  347. return 3*y_.size()*sizeof(Real) + 2*sizeof(Real);
  348. }
  349. std::pair<Real, Real> domain() const
  350. {
  351. Real xf = x0_ + (y_.size()-1)/inv_dx_;
  352. return {x0_, xf};
  353. }
  354. private:
  355. RandomAccessContainer y_;
  356. RandomAccessContainer dy_;
  357. RandomAccessContainer d2y_;
  358. Real x0_;
  359. Real inv_dx_;
  360. };
  361. template<class RandomAccessContainer>
  362. class cardinal_quintic_hermite_detail_aos {
  363. public:
  364. using Point = typename RandomAccessContainer::value_type;
  365. using Real = typename Point::value_type;
  366. cardinal_quintic_hermite_detail_aos(RandomAccessContainer && data, Real x0, Real dx)
  367. : data_{std::move(data)} , x0_{x0}, inv_dx_{1/dx}
  368. {
  369. if (data_.size() < 2)
  370. {
  371. throw std::domain_error("At least two points are required to interpolate using cardinal_quintic_hermite_aos");
  372. }
  373. if (data_[0].size() != 3)
  374. {
  375. throw std::domain_error("Each datum passed to the cardinal_quintic_hermite_aos must have three elements: {y, y', y''}");
  376. }
  377. if (dx <= 0)
  378. {
  379. throw std::domain_error("dx > 0 is required.");
  380. }
  381. for (auto & datum : data_)
  382. {
  383. datum[1] *= dx;
  384. datum[2] *= (dx*dx/2);
  385. }
  386. }
  387. inline Real operator()(Real x) const
  388. {
  389. const Real xf = x0_ + (data_.size()-1)/inv_dx_;
  390. if (x < x0_ || x > xf)
  391. {
  392. std::ostringstream oss;
  393. oss.precision(std::numeric_limits<Real>::digits10+3);
  394. oss << "Requested abscissa x = " << x << ", which is outside of allowed range ["
  395. << x0_ << ", " << xf << "]";
  396. throw std::domain_error(oss.str());
  397. }
  398. if (x == xf)
  399. {
  400. return data_.back()[0];
  401. }
  402. return this->unchecked_evaluation(x);
  403. }
  404. inline Real unchecked_evaluation(Real x) const
  405. {
  406. using std::floor;
  407. Real s = (x-x0_)*inv_dx_;
  408. Real ii = floor(s);
  409. auto i = static_cast<decltype(data_.size())>(ii);
  410. Real t = s - ii;
  411. if (t == 0)
  412. {
  413. return data_[i][0];
  414. }
  415. Real y0 = data_[i][0];
  416. Real dy0 = data_[i][1];
  417. Real d2y0 = data_[i][2];
  418. Real y1 = data_[i+1][0];
  419. Real dy1 = data_[i+1][1];
  420. Real d2y1 = data_[i+1][2];
  421. Real y = (1 - t*t*t*(10 + t*(-15 + 6*t)))*y0;
  422. y += t*(1 + t*t*(-6 + t*(8 - 3*t)))*dy0;
  423. y += t*t*(1 + t*(-3 + t*(3 - t)))*d2y0;
  424. y += t*t*t*((1 + t*(-2 + t))*d2y1 + (-4 + t*(7 - 3*t))*dy1 + (10 + t*(-15 + 6*t))*y1);
  425. return y;
  426. }
  427. inline Real prime(Real x) const
  428. {
  429. const Real xf = x0_ + (data_.size()-1)/inv_dx_;
  430. if (x < x0_ || x > xf)
  431. {
  432. std::ostringstream oss;
  433. oss.precision(std::numeric_limits<Real>::digits10+3);
  434. oss << "Requested abscissa x = " << x << ", which is outside of allowed range ["
  435. << x0_ << ", " << xf << "]";
  436. throw std::domain_error(oss.str());
  437. }
  438. if (x == xf)
  439. {
  440. return data_.back()[1]*inv_dx_;
  441. }
  442. return this->unchecked_prime(x);
  443. }
  444. inline Real unchecked_prime(Real x) const
  445. {
  446. using std::floor;
  447. Real s = (x-x0_)*inv_dx_;
  448. Real ii = floor(s);
  449. auto i = static_cast<decltype(data_.size())>(ii);
  450. Real t = s - ii;
  451. if (t == 0)
  452. {
  453. return data_[i][1]*inv_dx_;
  454. }
  455. Real y0 = data_[i][0];
  456. Real y1 = data_[i+1][0];
  457. Real v0 = data_[i][1];
  458. Real v1 = data_[i+1][1];
  459. Real a0 = data_[i][2];
  460. Real a1 = data_[i+1][2];
  461. Real dy = 30*t*t*(1 - 2*t + t*t)*(y1-y0);
  462. dy += (1-18*t*t + 32*t*t*t - 15*t*t*t*t)*v0 - t*t*(12 - 28*t + 15*t*t)*v1;
  463. dy += t*((2 - 9*t + 12*t*t - 5*t*t*t)*a0 + t*(3 - 8*t + 5*t*t)*a1);
  464. return dy*inv_dx_;
  465. }
  466. inline Real double_prime(Real x) const
  467. {
  468. const Real xf = x0_ + (data_.size()-1)/inv_dx_;
  469. if (x < x0_ || x > xf)
  470. {
  471. std::ostringstream oss;
  472. oss.precision(std::numeric_limits<Real>::digits10+3);
  473. oss << "Requested abscissa x = " << x << ", which is outside of allowed range ["
  474. << x0_ << ", " << xf << "]";
  475. throw std::domain_error(oss.str());
  476. }
  477. if (x == xf)
  478. {
  479. return data_.back()[2]*2*inv_dx_*inv_dx_;
  480. }
  481. return this->unchecked_double_prime(x);
  482. }
  483. inline Real unchecked_double_prime(Real x) const
  484. {
  485. using std::floor;
  486. Real s = (x-x0_)*inv_dx_;
  487. Real ii = floor(s);
  488. auto i = static_cast<decltype(data_.size())>(ii);
  489. Real t = s - ii;
  490. if (t == 0) {
  491. return data_[i][2]*2*inv_dx_*inv_dx_;
  492. }
  493. Real y0 = data_[i][0];
  494. Real dy0 = data_[i][1];
  495. Real d2y0 = data_[i][2];
  496. Real y1 = data_[i+1][0];
  497. Real dy1 = data_[i+1][1];
  498. Real d2y1 = data_[i+1][2];
  499. Real d2ydx2 = 60*t*(1 - 3*t + 2*t*t)*(y1 - y0)*inv_dx_*inv_dx_;
  500. d2ydx2 += (12*t)*((-3 + 8*t - 5*t*t)*dy0 - (2 - 7*t + 5*t*t)*dy1);
  501. 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_);
  502. return d2ydx2;
  503. }
  504. int64_t bytes() const
  505. {
  506. return data_.size()*data_[0].size()*sizeof(Real) + 2*sizeof(Real);
  507. }
  508. std::pair<Real, Real> domain() const
  509. {
  510. Real xf = x0_ + (data_.size()-1)/inv_dx_;
  511. return {x0_, xf};
  512. }
  513. private:
  514. RandomAccessContainer data_;
  515. Real x0_;
  516. Real inv_dx_;
  517. };
  518. }
  519. }
  520. }
  521. }
  522. #endif