1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430
|
// Copyright Nick Thompson, 2017
// Use, modification and distribution are subject to the
// Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt
// or copy at http://www.boost.org/LICENSE_1_0.txt)
#define BOOST_TEST_MODULE test_cubic_b_spline
#include <cmath>
#include <random>
#include <functional>
#include <boost/random/uniform_real_distribution.hpp>
#include <boost/type_index.hpp>
#include <boost/test/included/unit_test.hpp>
#include <boost/test/tools/floating_point_comparison.hpp>
#include <boost/math/interpolators/cardinal_cubic_b_spline.hpp>
#include <boost/math/interpolators/detail/cardinal_cubic_b_spline_detail.hpp>
#include <boost/math/constants/constants.hpp>
#include <boost/multiprecision/cpp_bin_float.hpp>
#if __has_include(<stdfloat>)
# include <stdfloat>
#endif
using std::sin;
using boost::multiprecision::cpp_bin_float_50;
using boost::math::constants::third;
using boost::math::constants::half;
template<class Real>
void test_b3_spline()
{
std::cout << "Testing evaluation of spline basis functions on type " << boost::typeindex::type_id<Real>().pretty_name() << "\n";
// Outside the support:
Real eps = std::numeric_limits<Real>::epsilon();
BOOST_CHECK_SMALL(boost::math::interpolators::detail::b3_spline<Real>(Real(2.5)), (Real) 0);
BOOST_CHECK_SMALL(boost::math::interpolators::detail::b3_spline<Real>(Real(-2.5)), (Real) 0);
BOOST_CHECK_SMALL(boost::math::interpolators::detail::b3_spline_prime<Real>(Real(2.5)), (Real) 0);
BOOST_CHECK_SMALL(boost::math::interpolators::detail::b3_spline_prime<Real>(Real(-2.5)), (Real) 0);
BOOST_CHECK_SMALL(boost::math::interpolators::detail::b3_spline_double_prime<Real>(Real(2.5)), (Real) 0);
BOOST_CHECK_SMALL(boost::math::interpolators::detail::b3_spline_double_prime<Real>(Real(-2.5)), (Real) 0);
// On the boundary of support:
BOOST_CHECK_SMALL(boost::math::interpolators::detail::b3_spline<Real>(2), (Real) 0);
BOOST_CHECK_SMALL(boost::math::interpolators::detail::b3_spline<Real>(-2), (Real) 0);
BOOST_CHECK_SMALL(boost::math::interpolators::detail::b3_spline_prime<Real>(2), (Real) 0);
BOOST_CHECK_SMALL(boost::math::interpolators::detail::b3_spline_prime<Real>(-2), (Real) 0);
// Special values:
BOOST_CHECK_CLOSE(boost::math::interpolators::detail::b3_spline<Real>(-1), third<Real>()*half<Real>(), eps);
BOOST_CHECK_CLOSE(boost::math::interpolators::detail::b3_spline<Real>( 1), third<Real>()*half<Real>(), eps);
BOOST_CHECK_CLOSE(boost::math::interpolators::detail::b3_spline<Real>(0), 2*third<Real>(), eps);
BOOST_CHECK_CLOSE(boost::math::interpolators::detail::b3_spline_prime<Real>(-1), half<Real>(), eps);
BOOST_CHECK_CLOSE(boost::math::interpolators::detail::b3_spline_prime<Real>( 1), -half<Real>(), eps);
BOOST_CHECK_SMALL(boost::math::interpolators::detail::b3_spline_prime<Real>(0), eps);
// Properties: B3 is an even function, B3' is an odd function.
for (size_t i = 1; i < 200; ++i)
{
Real arg = i*Real(0.01);
BOOST_CHECK_CLOSE(boost::math::interpolators::detail::b3_spline<Real>(arg), boost::math::interpolators::detail::b3_spline<Real>(arg), eps);
BOOST_CHECK_CLOSE(boost::math::interpolators::detail::b3_spline_prime<Real>(-arg), -boost::math::interpolators::detail::b3_spline_prime<Real>(arg), eps);
BOOST_CHECK_CLOSE(boost::math::interpolators::detail::b3_spline_double_prime<Real>(-arg), boost::math::interpolators::detail::b3_spline_double_prime<Real>(arg), eps);
}
}
/*
* This test ensures that the interpolant s(x_j) = f(x_j) at all grid points.
*/
template<class Real>
void test_interpolation_condition()
{
using std::sqrt;
std::cout << "Testing interpolation condition for cubic b splines on type " << boost::typeindex::type_id<Real>().pretty_name() << "\n";
std::random_device rd;
std::mt19937 gen(rd());
boost::random::uniform_real_distribution<Real> dis(1, 10);
std::vector<Real> v(5000);
for (size_t i = 0; i < v.size(); ++i)
{
v[i] = dis(gen);
}
Real step = static_cast<Real>(0.01);
Real a = Real(5);
boost::math::interpolators::cardinal_cubic_b_spline<Real> spline(v.data(), v.size(), a, step);
for (size_t i = 0; i < v.size(); ++i)
{
Real y = spline(i*step + a);
// This seems like a very large tolerance, but I don't know of any other interpolators
// that will be able to do much better on random data.
BOOST_CHECK_CLOSE(y, v[i], 10000*sqrt(std::numeric_limits<Real>::epsilon()));
}
}
template<class Real>
void test_constant_function()
{
std::cout << "Testing that constants are interpolated correctly by cubic b splines on type " << boost::typeindex::type_id<Real>().pretty_name() << "\n";
std::vector<Real> v(500);
const Real constant = Real(50.2);
for (size_t i = 0; i < v.size(); ++i)
{
v[i] = constant;
}
Real step = static_cast<Real>(0.02);
Real a = 5;
boost::math::interpolators::cardinal_cubic_b_spline<Real> spline(v.data(), v.size(), a, step);
for (size_t i = 0; i < v.size(); ++i)
{
// Do not test at interpolation point; we already know it works there:
Real y = spline(i*step + a + Real(0.001));
BOOST_CHECK_CLOSE(y, constant, 10*std::numeric_limits<Real>::epsilon());
Real y_prime = spline.prime(i*step + a + Real(0.002));
BOOST_CHECK_SMALL(y_prime, 5000*std::numeric_limits<Real>::epsilon());
Real y_double_prime = spline.double_prime(i*step + a + Real(0.002));
BOOST_CHECK_SMALL(y_double_prime, 5000*std::numeric_limits<Real>::epsilon());
}
// Test that correctly specified left and right-derivatives work properly:
spline = boost::math::interpolators::cardinal_cubic_b_spline<Real>(v.data(), v.size(), a, step, 0, 0);
for (size_t i = 0; i < v.size(); ++i)
{
Real y = spline(i*step + a + Real(0.002));
BOOST_CHECK_CLOSE(y, constant, std::numeric_limits<Real>::epsilon());
Real y_prime = spline.prime(i*step + a + Real(0.002));
BOOST_CHECK_SMALL(y_prime, std::numeric_limits<Real>::epsilon());
}
//
// Again with iterator constructor:
//
boost::math::interpolators::cardinal_cubic_b_spline<Real> spline2(v.begin(), v.end(), a, step);
for (size_t i = 0; i < v.size(); ++i)
{
// Do not test at interpolation point; we already know it works there:
Real y = spline2(i*step + a + Real(0.001));
BOOST_CHECK_CLOSE(y, constant, 10 * std::numeric_limits<Real>::epsilon());
Real y_prime = spline2.prime(i*step + a + Real(0.002));
BOOST_CHECK_SMALL(y_prime, 5000 * std::numeric_limits<Real>::epsilon());
}
// Test that correctly specified left and right-derivatives work properly:
spline2 = boost::math::interpolators::cardinal_cubic_b_spline<Real>(v.begin(), v.end(), a, step, 0, 0);
for (size_t i = 0; i < v.size(); ++i)
{
Real y = spline2(i*step + a + Real(0.002));
BOOST_CHECK_CLOSE(y, constant, std::numeric_limits<Real>::epsilon());
Real y_prime = spline2.prime(i*step + a + Real(0.002));
BOOST_CHECK_SMALL(y_prime, std::numeric_limits<Real>::epsilon());
}
}
template<class Real>
void test_affine_function()
{
using std::sqrt;
std::cout << "Testing that affine functions are interpolated correctly by cubic b splines on type " << boost::typeindex::type_id<Real>().pretty_name() << "\n";
std::vector<Real> v(500);
Real a = 10;
Real b = 8;
Real step = static_cast<Real>(0.005);
auto f = [a, b](Real x) { return a*x + b; };
for (size_t i = 0; i < v.size(); ++i)
{
v[i] = f(i*step);
}
boost::math::interpolators::cardinal_cubic_b_spline<Real> spline(v.data(), v.size(), 0, step);
for (size_t i = 0; i < v.size() - 1; ++i)
{
Real arg = static_cast<Real>(i*step + Real(0.0001));
Real y = spline(arg);
BOOST_CHECK_CLOSE(y, f(arg), sqrt(std::numeric_limits<Real>::epsilon()));
Real y_prime = spline.prime(arg);
BOOST_CHECK_CLOSE(y_prime, a, 100*sqrt(std::numeric_limits<Real>::epsilon()));
}
// Test that correctly specified left and right-derivatives work properly:
spline = boost::math::interpolators::cardinal_cubic_b_spline<Real>(v.data(), v.size(), 0, step, a, a);
for (size_t i = 0; i < v.size() - 1; ++i)
{
Real arg = static_cast<Real>(i*step + Real(0.0001));
Real y = spline(arg);
BOOST_CHECK_CLOSE(y, f(arg), sqrt(std::numeric_limits<Real>::epsilon()));
Real y_prime = spline.prime(arg);
BOOST_CHECK_CLOSE(y_prime, a, 100*sqrt(std::numeric_limits<Real>::epsilon()));
}
}
template<class Real>
void test_quadratic_function()
{
using std::sqrt;
std::cout << "Testing that quadratic functions are interpolated correctly by cubic b splines on type " << boost::typeindex::type_id<Real>().pretty_name() << "\n";
std::vector<Real> v(500);
Real a = static_cast<Real>(1.2);
Real b = static_cast<Real>(-3.4);
Real c = static_cast<Real>(-8.6);
Real step = static_cast<Real>(0.01);
auto f = [a, b, c](Real x) { return a*x*x + b*x + c; };
for (size_t i = 0; i < v.size(); ++i)
{
v[i] = f(i*step);
}
boost::math::interpolators::cardinal_cubic_b_spline<Real> spline(v.data(), v.size(), 0, step);
for (size_t i = 0; i < v.size() -1; ++i)
{
Real arg = static_cast<Real>(i*step + Real(0.001));
Real y = spline(arg);
BOOST_CHECK_CLOSE(y, f(arg), Real(0.1));
Real y_prime = spline.prime(arg);
BOOST_CHECK_CLOSE(y_prime, 2*a*arg + b, Real(2.0));
}
}
template<class Real>
void test_circ_conic_function()
{
using std::sqrt;
std::cout << "Testing that conic section of a circle is interpolated correctly by cubic b splines on type " << boost::typeindex::type_id<Real>().pretty_name() << '\n';
std::vector<Real> v(500);
Real cv = Real(0.1);
Real w = Real(2.0);
Real step = Real(2 * w / (v.size() - 1));
auto f = [cv](Real x) { return cv * x * x / (1 + sqrt(1 - cv * cv * x * x)); };
auto df = [cv](Real x) { return cv * x / sqrt(1 - cv * cv * x * x); };
for (size_t i = 0; i < v.size(); ++i)
{
v[i] = f(-w + i * step);
}
boost::math::interpolators::cardinal_cubic_b_spline<Real> spline(v.data(), v.size(), -w, step);
Real tol = 100 * sqrt(std::numeric_limits<Real>::epsilon());
if ((std::numeric_limits<Real>::digits > 100) || !std::numeric_limits<Real>::digits)
tol *= 500000;
// First check derivatives exactly at end points
BOOST_CHECK_CLOSE(spline.prime(-w), df(-w), tol);
BOOST_CHECK_CLOSE(spline.prime(w), df(w), tol);
for (size_t i = 0; i < v.size() - 1; ++i)
{
Real arg = -w + i * step + Real(0.001);
Real y = spline(arg);
BOOST_CHECK_CLOSE(y, f(arg), Real(2.0));
Real y_prime = spline.prime(arg);
BOOST_CHECK_CLOSE(y_prime, df(arg), Real(1.0));
}
}
template<class Real>
void test_trig_function()
{
std::cout << "Testing that sine functions are interpolated correctly by cubic b splines on type " << boost::typeindex::type_id<Real>().pretty_name() << "\n";
std::mt19937 gen;
std::vector<Real> v(500);
Real x0 = 1;
Real step = Real(0.125);
for (size_t i = 0; i < v.size(); ++i)
{
v[i] = sin(x0 + step * i);
}
boost::math::interpolators::cardinal_cubic_b_spline<Real> spline(v.data(), v.size(), x0, step);
boost::random::uniform_real_distribution<Real> abscissa(x0, x0 + 499 * step);
for (size_t i = 0; i < v.size(); ++i)
{
Real x = abscissa(gen);
Real y = spline(x);
BOOST_CHECK_CLOSE(y, Real(sin(x)), Real(1.0));
auto y_prime = spline.prime(x);
BOOST_CHECK_CLOSE(y_prime, Real(cos(x)), Real(2.0));
}
}
template<class Real>
void test_copy_move()
{
std::cout << "Testing that copy/move operation succeed on cubic b spline\n";
std::vector<Real> v(500);
Real x0 = 1;
Real step = static_cast<Real>(0.125);
constexpr Real tol = Real(0.01);
for (size_t i = 0; i < v.size(); ++i)
{
v[i] = sin(x0 + step * i);
}
boost::math::interpolators::cardinal_cubic_b_spline<Real> spline(v.data(), v.size(), x0, step);
// Default constructor should compile so that splines can be member variables:
boost::math::interpolators::cardinal_cubic_b_spline<Real> d;
d = boost::math::interpolators::cardinal_cubic_b_spline<Real>(v.data(), v.size(), x0, step);
BOOST_CHECK_CLOSE(d(x0), Real(sin(x0)), tol);
// Passing to lambda should compile:
auto f = [=](Real x) { return d(x); };
// Make sure this variable is used.
BOOST_CHECK_CLOSE(f(x0), Real(sin(x0)), tol);
// Move operations should compile.
auto s = std::move(spline);
// Copy operations should compile:
boost::math::interpolators::cardinal_cubic_b_spline<Real> c = d;
BOOST_CHECK_CLOSE(c(x0), Real(sin(x0)), tol);
// Test with std::bind:
auto h = std::bind(&boost::math::interpolators::cardinal_cubic_b_spline<Real>::operator(), &s, std::placeholders::_1);
BOOST_CHECK_CLOSE(h(x0), Real(sin(x0)), tol);
}
template<class Real>
void test_outside_interval()
{
std::cout << "Testing that the spline can be evaluated outside the interpolation interval\n";
std::vector<Real> v(400);
Real x0 = 1;
Real step = Real(0.125);
for (size_t i = 0; i < v.size(); ++i)
{
v[i] = sin(x0 + step * i);
}
boost::math::interpolators::cardinal_cubic_b_spline<Real> spline(v.data(), v.size(), x0, step);
// There's no test here; it simply does it's best to be an extrapolator.
//
std::ostream cnull(0);
cnull << spline(0);
cnull << spline(2000);
}
BOOST_AUTO_TEST_CASE(test_cubic_b_spline)
{
#ifdef __STDCPP_FLOAT32_T__
test_b3_spline<std::float32_t>();
test_interpolation_condition<std::float32_t>();
test_constant_function<std::float32_t>();
test_affine_function<std::float32_t>();
test_quadratic_function<std::float32_t>();
test_circ_conic_function<std::float32_t>();
test_trig_function<std::float32_t>();
#else
test_b3_spline<float>();
test_interpolation_condition<float>();
test_constant_function<float>();
test_affine_function<float>();
test_quadratic_function<float>();
test_circ_conic_function<float>();
test_trig_function<float>();
#endif
#ifdef __STDCPP_FLOAT64_T__
test_b3_spline<std::float64_t>();
test_interpolation_condition<std::float64_t>();
test_constant_function<std::float64_t>();
test_affine_function<std::float64_t>();
test_quadratic_function<std::float64_t>();
test_circ_conic_function<std::float64_t>();
test_trig_function<std::float64_t>();
test_copy_move<std::float64_t>();
test_outside_interval<std::float64_t>();
#else
test_b3_spline<double>();
test_interpolation_condition<double>();
test_constant_function<double>();
test_affine_function<double>();
test_quadratic_function<double>();
test_circ_conic_function<double>();
test_trig_function<double>();
test_copy_move<double>();
test_outside_interval<double>();
#endif
#ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS
test_b3_spline<long double>();
test_interpolation_condition<long double>();
test_constant_function<long double>();
test_affine_function<long double>();
test_quadratic_function<long double>();
test_circ_conic_function<long double>();
test_trig_function<long double>();
#endif
test_b3_spline<cpp_bin_float_50>();
test_interpolation_condition<cpp_bin_float_50>();
test_constant_function<cpp_bin_float_50>();
test_affine_function<cpp_bin_float_50>();
test_quadratic_function<cpp_bin_float_50>();
test_trig_function<cpp_bin_float_50>();
}
|