Warning, file /include/boost/math/interpolators/detail/cubic_b_spline_detail.hpp was not indexed
or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).
0001
0002
0003
0004
0005
0006
0007 #ifndef CUBIC_B_SPLINE_DETAIL_HPP
0008 #define CUBIC_B_SPLINE_DETAIL_HPP
0009
0010 #include <limits>
0011 #include <cmath>
0012 #include <vector>
0013 #include <memory>
0014 #include <boost/math/constants/constants.hpp>
0015 #include <boost/math/special_functions/fpclassify.hpp>
0016
0017 namespace boost{ namespace math{ namespace detail{
0018
0019
0020 template <class Real>
0021 class cubic_b_spline_imp
0022 {
0023 public:
0024
0025
0026 template <class BidiIterator>
0027 cubic_b_spline_imp(BidiIterator f, BidiIterator end_p, Real left_endpoint, Real step_size,
0028 Real left_endpoint_derivative = std::numeric_limits<Real>::quiet_NaN(),
0029 Real right_endpoint_derivative = std::numeric_limits<Real>::quiet_NaN());
0030
0031 Real operator()(Real x) const;
0032
0033 Real prime(Real x) const;
0034
0035 Real double_prime(Real x) const;
0036
0037 private:
0038 std::vector<Real> m_beta;
0039 Real m_h_inv;
0040 Real m_a;
0041 Real m_avg;
0042 };
0043
0044
0045
0046 template <class Real>
0047 Real b3_spline(Real x)
0048 {
0049 using std::abs;
0050 Real absx = abs(x);
0051 if (absx < 1)
0052 {
0053 Real y = 2 - absx;
0054 Real z = 1 - absx;
0055 return boost::math::constants::sixth<Real>()*(y*y*y - 4*z*z*z);
0056 }
0057 if (absx < 2)
0058 {
0059 Real y = 2 - absx;
0060 return boost::math::constants::sixth<Real>()*y*y*y;
0061 }
0062 return static_cast<Real>(0);
0063 }
0064
0065 template<class Real>
0066 Real b3_spline_prime(Real x)
0067 {
0068 if (x < 0)
0069 {
0070 return -b3_spline_prime(-x);
0071 }
0072
0073 if (x < 1)
0074 {
0075 return x*(3*boost::math::constants::half<Real>()*x - 2);
0076 }
0077 if (x < 2)
0078 {
0079 return -boost::math::constants::half<Real>()*(2 - x)*(2 - x);
0080 }
0081 return static_cast<Real>(0);
0082 }
0083
0084 template<class Real>
0085 Real b3_spline_double_prime(Real x)
0086 {
0087 if (x < 0)
0088 {
0089 return b3_spline_double_prime(-x);
0090 }
0091
0092 if (x < 1)
0093 {
0094 return 3*x - 2;
0095 }
0096 if (x < 2)
0097 {
0098 return (2 - x);
0099 }
0100 return static_cast<Real>(0);
0101 }
0102
0103
0104 template <class Real>
0105 template <class BidiIterator>
0106 cubic_b_spline_imp<Real>::cubic_b_spline_imp(BidiIterator f, BidiIterator end_p, Real left_endpoint, Real step_size,
0107 Real left_endpoint_derivative, Real right_endpoint_derivative) : m_a(left_endpoint), m_avg(0)
0108 {
0109 using boost::math::constants::third;
0110
0111 std::size_t length = end_p - f;
0112
0113 if (length < 5)
0114 {
0115 if (boost::math::isnan(left_endpoint_derivative) || boost::math::isnan(right_endpoint_derivative))
0116 {
0117 throw std::logic_error("Interpolation using a cubic b spline with derivatives estimated at the endpoints requires at least 5 points.\n");
0118 }
0119 if (length < 3)
0120 {
0121 throw std::logic_error("Interpolation using a cubic b spline requires at least 3 points.\n");
0122 }
0123 }
0124
0125 if (boost::math::isnan(left_endpoint))
0126 {
0127 throw std::logic_error("Left endpoint is NAN; this is disallowed.\n");
0128 }
0129 if (left_endpoint + length*step_size >= (std::numeric_limits<Real>::max)())
0130 {
0131 throw std::logic_error("Right endpoint overflows the maximum representable number of the specified precision.\n");
0132 }
0133 if (step_size <= 0)
0134 {
0135 throw std::logic_error("The step size must be strictly > 0.\n");
0136 }
0137
0138
0139 m_h_inv = 1/step_size;
0140
0141
0142 Real a1 = left_endpoint_derivative;
0143
0144
0145
0146
0147 if (boost::math::isnan(a1))
0148 {
0149
0150
0151
0152 Real t0 = 4*(f[1] + third<Real>()*f[3]);
0153 Real t1 = -(25*third<Real>()*f[0] + f[4])/4 - 3*f[2];
0154 a1 = m_h_inv*(t0 + t1);
0155 }
0156
0157 Real b1 = right_endpoint_derivative;
0158 if (boost::math::isnan(b1))
0159 {
0160 size_t n = length - 1;
0161 Real t0 = 4*(f[n-3] + third<Real>()*f[n - 1]);
0162 Real t1 = -(25*third<Real>()*f[n - 4] + f[n])/4 - 3*f[n - 2];
0163
0164 b1 = m_h_inv*(t0 + t1);
0165 }
0166
0167
0168
0169 m_beta.resize(length + 2, std::numeric_limits<Real>::quiet_NaN());
0170
0171
0172
0173
0174
0175
0176
0177 Real t = 1;
0178 for (size_t i = 0; i < length; ++i)
0179 {
0180 if (boost::math::isnan(f[i]))
0181 {
0182 std::string err = "This function you are trying to interpolate is a nan at index " + std::to_string(i) + "\n";
0183 throw std::logic_error(err);
0184 }
0185 m_avg += (f[i] - m_avg) / t;
0186 t += 1;
0187 }
0188
0189
0190
0191
0192
0193
0194
0195
0196
0197
0198
0199
0200
0201
0202
0203 std::vector<Real> rhs(length + 2, std::numeric_limits<Real>::quiet_NaN());
0204 std::vector<Real> super_diagonal(length + 2, std::numeric_limits<Real>::quiet_NaN());
0205
0206 rhs[0] = -2*step_size*a1;
0207 rhs[rhs.size() - 1] = -2*step_size*b1;
0208
0209 super_diagonal[0] = 0;
0210
0211 for(size_t i = 1; i < rhs.size() - 1; ++i)
0212 {
0213 rhs[i] = 6*(f[i - 1] - m_avg);
0214 super_diagonal[i] = 1;
0215 }
0216
0217
0218
0219
0220
0221
0222
0223
0224
0225
0226
0227 super_diagonal[1] = 0.5;
0228 rhs[1] = (rhs[1] - rhs[0])/4;
0229
0230
0231 for (size_t i = 2; i < rhs.size() - 1; ++i)
0232 {
0233 Real diagonal = 4 - super_diagonal[i - 1];
0234 rhs[i] = (rhs[i] - rhs[i - 1])/diagonal;
0235 super_diagonal[i] /= diagonal;
0236 }
0237
0238
0239
0240
0241
0242 Real final_subdiag = -super_diagonal[rhs.size() - 3];
0243 rhs[rhs.size() - 1] = (rhs[rhs.size() - 1] - rhs[rhs.size() - 3])/final_subdiag;
0244 Real final_diag = -1/final_subdiag;
0245
0246
0247
0248
0249
0250 final_diag = final_diag - super_diagonal[rhs.size() - 2];
0251 rhs[rhs.size() - 1] = rhs[rhs.size() - 1] - rhs[rhs.size() - 2];
0252
0253
0254
0255 m_beta[rhs.size() - 1] = rhs[rhs.size() - 1]/final_diag;
0256 for(size_t i = rhs.size() - 2; i > 0; --i)
0257 {
0258 m_beta[i] = rhs[i] - super_diagonal[i]*m_beta[i + 1];
0259 }
0260 m_beta[0] = m_beta[2] + rhs[0];
0261 }
0262
0263 template<class Real>
0264 Real cubic_b_spline_imp<Real>::operator()(Real x) const
0265 {
0266
0267
0268 Real z = m_avg;
0269 Real t = m_h_inv*(x - m_a) + 1;
0270
0271 using std::max;
0272 using std::min;
0273 using std::ceil;
0274 using std::floor;
0275
0276 size_t k_min = static_cast<size_t>((max)(static_cast<long>(0), boost::math::ltrunc(ceil(t - 2))));
0277 size_t k_max = static_cast<size_t>((max)((min)(static_cast<long>(m_beta.size() - 1), boost::math::ltrunc(floor(t + 2))), 0l));
0278 for (size_t k = k_min; k <= k_max; ++k)
0279 {
0280 z += m_beta[k]*b3_spline(t - k);
0281 }
0282
0283 return z;
0284 }
0285
0286 template<class Real>
0287 Real cubic_b_spline_imp<Real>::prime(Real x) const
0288 {
0289 Real z = 0;
0290 Real t = m_h_inv*(x - m_a) + 1;
0291
0292 using std::max;
0293 using std::min;
0294 using std::ceil;
0295 using std::floor;
0296
0297 size_t k_min = static_cast<size_t>((max)(static_cast<long>(0), boost::math::ltrunc(ceil(t - 2))));
0298 size_t k_max = static_cast<size_t>((min)(static_cast<long>(m_beta.size() - 1), boost::math::ltrunc(floor(t + 2))));
0299
0300 for (size_t k = k_min; k <= k_max; ++k)
0301 {
0302 z += m_beta[k]*b3_spline_prime(t - k);
0303 }
0304 return z*m_h_inv;
0305 }
0306
0307 template<class Real>
0308 Real cubic_b_spline_imp<Real>::double_prime(Real x) const
0309 {
0310 Real z = 0;
0311 Real t = m_h_inv*(x - m_a) + 1;
0312
0313 using std::max;
0314 using std::min;
0315 using std::ceil;
0316 using std::floor;
0317
0318 size_t k_min = static_cast<size_t>((max)(static_cast<long>(0), boost::math::ltrunc(ceil(t - 2))));
0319 size_t k_max = static_cast<size_t>((min)(static_cast<long>(m_beta.size() - 1), boost::math::ltrunc(floor(t + 2))));
0320
0321 for (size_t k = k_min; k <= k_max; ++k)
0322 {
0323 z += m_beta[k]*b3_spline_double_prime(t - k);
0324 }
0325 return z*m_h_inv*m_h_inv;
0326 }
0327
0328
0329 }}}
0330 #endif