makima.hpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. // Copyright Nick Thompson, 2020
  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. // See: https://blogs.mathworks.com/cleve/2019/04/29/makima-piecewise-cubic-interpolation/
  7. // And: https://doi.org/10.1145/321607.321609
  8. #ifndef BOOST_MATH_INTERPOLATORS_MAKIMA_HPP
  9. #define BOOST_MATH_INTERPOLATORS_MAKIMA_HPP
  10. #include <memory>
  11. #include <cmath>
  12. #include <boost/math/interpolators/detail/cubic_hermite_detail.hpp>
  13. namespace boost::math::interpolators {
  14. template<class RandomAccessContainer>
  15. class makima {
  16. public:
  17. using Real = typename RandomAccessContainer::value_type;
  18. makima(RandomAccessContainer && x, RandomAccessContainer && y,
  19. Real left_endpoint_derivative = std::numeric_limits<Real>::quiet_NaN(),
  20. Real right_endpoint_derivative = std::numeric_limits<Real>::quiet_NaN())
  21. {
  22. using std::isnan;
  23. using std::abs;
  24. if (x.size() < 4)
  25. {
  26. throw std::domain_error("Must be at least four data points.");
  27. }
  28. RandomAccessContainer s(x.size(), std::numeric_limits<Real>::quiet_NaN());
  29. Real m2 = (y[3]-y[2])/(x[3]-x[2]);
  30. Real m1 = (y[2]-y[1])/(x[2]-x[1]);
  31. Real m0 = (y[1]-y[0])/(x[1]-x[0]);
  32. // Quadratic extrapolation: m_{-1} = 2m_0 - m_1:
  33. Real mm1 = 2*m0 - m1;
  34. // Quadratic extrapolation: m_{-2} = 2*m_{-1}-m_0:
  35. Real mm2 = 2*mm1 - m0;
  36. Real w1 = abs(m1-m0) + abs(m1+m0)/2;
  37. Real w2 = abs(mm1-mm2) + abs(mm1+mm2)/2;
  38. if (isnan(left_endpoint_derivative))
  39. {
  40. s[0] = (w1*mm1 + w2*m0)/(w1+w2);
  41. if (isnan(s[0]))
  42. {
  43. s[0] = 0;
  44. }
  45. }
  46. else
  47. {
  48. s[0] = left_endpoint_derivative;
  49. }
  50. w1 = abs(m2-m1) + abs(m2+m1)/2;
  51. w2 = abs(m0-mm1) + abs(m0+mm1)/2;
  52. s[1] = (w1*m0 + w2*m1)/(w1+w2);
  53. if (isnan(s[1])) {
  54. s[1] = 0;
  55. }
  56. for (decltype(s.size()) i = 2; i < s.size()-2; ++i) {
  57. Real mim2 = (y[i-1]-y[i-2])/(x[i-1]-x[i-2]);
  58. Real mim1 = (y[i ]-y[i-1])/(x[i ]-x[i-1]);
  59. Real mi = (y[i+1]-y[i ])/(x[i+1]-x[i ]);
  60. Real mip1 = (y[i+2]-y[i+1])/(x[i+2]-x[i+1]);
  61. w1 = abs(mip1-mi) + abs(mip1+mi)/2;
  62. w2 = abs(mim1-mim2) + abs(mim1+mim2)/2;
  63. s[i] = (w1*mim1 + w2*mi)/(w1+w2);
  64. if (isnan(s[i])) {
  65. s[i] = 0;
  66. }
  67. }
  68. // Quadratic extrapolation at the other end:
  69. decltype(s.size()) n = s.size();
  70. Real mnm4 = (y[n-3]-y[n-4])/(x[n-3]-x[n-4]);
  71. Real mnm3 = (y[n-2]-y[n-3])/(x[n-2]-x[n-3]);
  72. Real mnm2 = (y[n-1]-y[n-2])/(x[n-1]-x[n-2]);
  73. Real mnm1 = 2*mnm2 - mnm3;
  74. Real mn = 2*mnm1 - mnm2;
  75. w1 = abs(mnm1 - mnm2) + abs(mnm1+mnm2)/2;
  76. w2 = abs(mnm3 - mnm4) + abs(mnm3+mnm4)/2;
  77. s[n-2] = (w1*mnm3 + w2*mnm2)/(w1 + w2);
  78. if (isnan(s[n-2])) {
  79. s[n-2] = 0;
  80. }
  81. w1 = abs(mn - mnm1) + abs(mn+mnm1)/2;
  82. w2 = abs(mnm2 - mnm3) + abs(mnm2+mnm3)/2;
  83. if (isnan(right_endpoint_derivative))
  84. {
  85. s[n-1] = (w1*mnm2 + w2*mnm1)/(w1+w2);
  86. if (isnan(s[n-1])) {
  87. s[n-1] = 0;
  88. }
  89. }
  90. else
  91. {
  92. s[n-1] = right_endpoint_derivative;
  93. }
  94. impl_ = std::make_shared<detail::cubic_hermite_detail<RandomAccessContainer>>(std::move(x), std::move(y), std::move(s));
  95. }
  96. Real operator()(Real x) const {
  97. return impl_->operator()(x);
  98. }
  99. Real prime(Real x) const {
  100. return impl_->prime(x);
  101. }
  102. friend std::ostream& operator<<(std::ostream & os, const makima & m)
  103. {
  104. os << *m.impl_;
  105. return os;
  106. }
  107. void push_back(Real x, Real y) {
  108. using std::abs;
  109. using std::isnan;
  110. if (x <= impl_->x_.back()) {
  111. throw std::domain_error("Calling push_back must preserve the monotonicity of the x's");
  112. }
  113. impl_->x_.push_back(x);
  114. impl_->y_.push_back(y);
  115. impl_->dydx_.push_back(std::numeric_limits<Real>::quiet_NaN());
  116. // dydx_[n-2] was computed by extrapolation. Now dydx_[n-2] -> dydx_[n-3], and it can be computed by the same formula.
  117. decltype(impl_->size()) n = impl_->size();
  118. auto i = n - 3;
  119. Real mim2 = (impl_->y_[i-1]-impl_->y_[i-2])/(impl_->x_[i-1]-impl_->x_[i-2]);
  120. Real mim1 = (impl_->y_[i ]-impl_->y_[i-1])/(impl_->x_[i ]-impl_->x_[i-1]);
  121. Real mi = (impl_->y_[i+1]-impl_->y_[i ])/(impl_->x_[i+1]-impl_->x_[i ]);
  122. Real mip1 = (impl_->y_[i+2]-impl_->y_[i+1])/(impl_->x_[i+2]-impl_->x_[i+1]);
  123. Real w1 = abs(mip1-mi) + abs(mip1+mi)/2;
  124. Real w2 = abs(mim1-mim2) + abs(mim1+mim2)/2;
  125. impl_->dydx_[i] = (w1*mim1 + w2*mi)/(w1+w2);
  126. if (isnan(impl_->dydx_[i])) {
  127. impl_->dydx_[i] = 0;
  128. }
  129. Real mnm4 = (impl_->y_[n-3]-impl_->y_[n-4])/(impl_->x_[n-3]-impl_->x_[n-4]);
  130. Real mnm3 = (impl_->y_[n-2]-impl_->y_[n-3])/(impl_->x_[n-2]-impl_->x_[n-3]);
  131. Real mnm2 = (impl_->y_[n-1]-impl_->y_[n-2])/(impl_->x_[n-1]-impl_->x_[n-2]);
  132. Real mnm1 = 2*mnm2 - mnm3;
  133. Real mn = 2*mnm1 - mnm2;
  134. w1 = abs(mnm1 - mnm2) + abs(mnm1+mnm2)/2;
  135. w2 = abs(mnm3 - mnm4) + abs(mnm3+mnm4)/2;
  136. impl_->dydx_[n-2] = (w1*mnm3 + w2*mnm2)/(w1 + w2);
  137. if (isnan(impl_->dydx_[n-2])) {
  138. impl_->dydx_[n-2] = 0;
  139. }
  140. w1 = abs(mn - mnm1) + abs(mn+mnm1)/2;
  141. w2 = abs(mnm2 - mnm3) + abs(mnm2+mnm3)/2;
  142. impl_->dydx_[n-1] = (w1*mnm2 + w2*mnm1)/(w1+w2);
  143. if (isnan(impl_->dydx_[n-1])) {
  144. impl_->dydx_[n-1] = 0;
  145. }
  146. }
  147. private:
  148. std::shared_ptr<detail::cubic_hermite_detail<RandomAccessContainer>> impl_;
  149. };
  150. }
  151. #endif