2 * Copyright Nick Thompson, 2019
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)
7 #define BOOST_TEST_MODULE lanczos_smoothing_test
11 #include <boost/range.hpp>
12 #include <boost/numeric/ublas/vector.hpp>
13 #include <boost/math/constants/constants.hpp>
14 #include <boost/test/included/unit_test.hpp>
15 #include <boost/test/tools/floating_point_comparison.hpp>
16 #include <boost/math/differentiation/lanczos_smoothing.hpp>
17 #include <boost/multiprecision/cpp_bin_float.hpp>
18 #include <boost/math/special_functions/next.hpp> // for float_distance
19 #include <boost/math/tools/condition_numbers.hpp>
25 using boost::math::constants::two_pi;
26 using boost::multiprecision::cpp_bin_float_50;
27 using boost::multiprecision::cpp_bin_float_100;
28 using boost::math::differentiation::discrete_lanczos_derivative;
29 using boost::math::differentiation::detail::discrete_legendre;
30 using boost::math::differentiation::detail::interior_velocity_filter;
31 using boost::math::differentiation::detail::boundary_velocity_filter;
32 using boost::math::tools::summation_condition_number;
37 std::cout << "Testing Discrete Legendre Polynomial norms on type " << typeid(Real).name() << "\n";
38 Real tol = std::numeric_limits<Real>::epsilon();
39 auto dlp = discrete_legendre<Real>(1, Real(0));
40 BOOST_CHECK_CLOSE_FRACTION(dlp.norm_sq(0), 3, tol);
41 BOOST_CHECK_CLOSE_FRACTION(dlp.norm_sq(1), 2, tol);
42 dlp = discrete_legendre<Real>(2, Real(0));
43 BOOST_CHECK_CLOSE_FRACTION(dlp.norm_sq(0), Real(5)/Real(2), tol);
44 BOOST_CHECK_CLOSE_FRACTION(dlp.norm_sq(1), Real(5)/Real(4), tol);
45 BOOST_CHECK_CLOSE_FRACTION(dlp.norm_sq(2), Real(3*3*7)/Real(pow(2,6)), 2*tol);
46 dlp = discrete_legendre<Real>(200, Real(0));
47 for(size_t r = 0; r < 10; ++r)
49 Real calc = dlp.norm_sq(r);
50 Real expected = Real(2)/Real(2*r+1);
51 // As long as r << n, ||q_r||^2 -> 2/(2r+1) as n->infty
52 BOOST_CHECK_CLOSE_FRACTION(calc, expected, 0.05);
58 void test_dlp_evaluation()
60 std::cout << "Testing evaluation of Discrete Legendre polynomials on type " << typeid(Real).name() << "\n";
61 Real tol = std::numeric_limits<Real>::epsilon();
64 auto dlp = discrete_legendre<Real>(n, x);
71 Real expected = 0.5*(3*x*x - Real(N*N - 1)/Real(4*n*n));
72 BOOST_CHECK_CLOSE_FRACTION(q2, expected, tol);
74 expected = (x/3)*(5*expected - (Real(N*N - 4))/(2*n*n));
75 BOOST_CHECK_CLOSE_FRACTION(q3, expected, 2*tol);
77 // q_r(x) is even for even r, and odd for odd r:
78 for (size_t n = 8; n < 22; ++n)
80 dlp = discrete_legendre<Real>(n, x);
81 for(size_t r = 2; r <= n; ++r)
86 Real q2 = -dlp(-x, r);
87 BOOST_CHECK_CLOSE_FRACTION(q1, q2, tol);
93 BOOST_CHECK_CLOSE_FRACTION(q1, q2, tol);
97 for (int j = -(int)n; j <= (int) n; ++j)
99 Real y = Real(j)/Real(n);
100 Real term = dlp(y, r);
104 Real l2_sq_expected = dlp.norm_sq(r);
105 BOOST_CHECK_CLOSE_FRACTION(l2_sq, l2_sq_expected, 20*tol);
113 std::cout << "Testing Discrete Legendre polynomial 'next' function on type " << typeid(Real).name() << "\n";
114 Real tol = std::numeric_limits<Real>::epsilon();
116 for(size_t n = 2; n < 20; ++n)
118 for(Real x = -1; x <= 1; x += 0.1)
120 auto dlp = discrete_legendre<Real>(n, x);
121 for (size_t k = 2; k < n; ++k)
123 BOOST_CHECK_CLOSE(dlp.next(), dlp(x, k), tol);
126 dlp = discrete_legendre<Real>(n, x);
127 for (size_t k = 2; k < n; ++k)
129 BOOST_CHECK_CLOSE(dlp.next_prime(), dlp.prime(x, k), tol);
137 void test_dlp_derivatives()
139 std::cout << "Testing Discrete Legendre polynomial derivatives on type " << typeid(Real).name() << "\n";
140 Real tol = 10*std::numeric_limits<Real>::epsilon();
143 auto dlp = discrete_legendre<Real>(n, x);
144 Real q0p = dlp.prime(x, 0);
145 BOOST_TEST(q0p == 0);
146 Real q1p = dlp.prime(x, 1);
147 BOOST_TEST(q1p == 1);
148 Real q2p = dlp.prime(x, 2);
150 BOOST_CHECK_CLOSE_FRACTION(q2p, expected, tol);
154 void test_dlp_second_derivative()
156 std::cout << "Testing Discrete Legendre polynomial derivatives on type " << typeid(Real).name() << "\n";
158 Real x = Real(1)/Real(3);
159 auto dlp = discrete_legendre<Real>(n, x);
160 Real q2pp = dlp.next_dbl_prime();
161 BOOST_TEST(q2pp == 3);
166 void test_interior_velocity_filter()
168 using boost::math::constants::half;
169 std::cout << "Testing interior filter on type " << typeid(Real).name() << "\n";
170 Real tol = std::numeric_limits<Real>::epsilon();
171 for(int n = 1; n < 10; ++n)
173 for (int p = 1; p < n; p += 2)
175 auto f = interior_velocity_filter<Real>(n,p);
176 // Since we only store half the filter coefficients,
177 // we need to reindex the moment sums:
178 auto cond = summation_condition_number<Real>(0);
179 for (size_t j = 0; j < f.size(); ++j)
183 BOOST_CHECK_CLOSE_FRACTION(cond.sum(), half<Real>(), 2*cond()*tol);
185 for (int l = 3; l <= p; l += 2)
187 cond = summation_condition_number<Real>(0);
188 for (size_t j = 0; j < f.size() - 1; ++j)
190 cond += pow(Real(j), l)*f[j];
192 Real expected = -pow(Real(f.size() - 1), l)*f[f.size()-1];
193 BOOST_CHECK_CLOSE_FRACTION(expected, cond.sum(), 7*cond()*tol);
195 //std::cout << "(n,p) = (" << n << "," << p << ") = {";
198 // std::cout << x << ", ";
200 //std::cout << "}\n";
206 void test_interior_lanczos()
208 std::cout << "Testing interior Lanczos on type " << typeid(Real).name() << "\n";
209 Real tol = std::numeric_limits<Real>::epsilon();
210 std::vector<Real> v(500);
211 std::fill(v.begin(), v.end(), 7);
213 for (size_t n = 1; n < 10; ++n)
215 for (size_t p = 2; p < 2*n; p += 2)
217 auto dld = discrete_lanczos_derivative(Real(0.1), n, p);
218 for (size_t m = n; m < v.size() - n; ++m)
220 Real dvdt = dld(v, m);
221 BOOST_CHECK_SMALL(dvdt, tol);
224 for (size_t m = n; m < v.size() - n; ++m)
226 BOOST_CHECK_SMALL(dvdt[m], tol);
232 for(size_t i = 0; i < v.size(); ++i)
237 for (size_t n = 1; n < 10; ++n)
239 for (size_t p = 2; p < 2*n; p += 2)
241 auto dld = discrete_lanczos_derivative(Real(1), n, p);
242 for (size_t m = n; m < v.size() - n; ++m)
244 Real dvdt = dld(v, m);
245 BOOST_CHECK_CLOSE_FRACTION(dvdt, 7, 2000*tol);
248 for (size_t m = n; m < v.size() - n; ++m)
250 BOOST_CHECK_CLOSE_FRACTION(dvdt[m], 7, 2000*tol);
255 //std::random_device rd{};
257 //std::cout << "Seed = " << seed << "\n";
258 std::mt19937 gen(4172378669);
259 std::normal_distribution<> dis{0, 0.01};
260 for (size_t i = 0; i < v.size(); ++i)
262 v[i] = 7*i+8 + dis(gen);
265 for (size_t n = 1; n < 10; ++n)
267 for (size_t p = 2; p < 2*n; p += 2)
269 auto dld = discrete_lanczos_derivative(Real(1), n, p);
270 for (size_t m = n; m < v.size() - n; ++m)
272 BOOST_CHECK_CLOSE_FRACTION(dld(v, m), Real(7), Real(0.0042));
278 for (size_t i = 0; i < v.size(); ++i)
280 v[i] = 15*i*i + 7*i+8 + dis(gen);
283 for (size_t n = 1; n < 10; ++n)
285 for (size_t p = 2; p < 2*n; p += 2)
287 auto dld = discrete_lanczos_derivative(Real(1), n, p);
288 for (size_t m = n; m < v.size() - n; ++m)
290 BOOST_CHECK_CLOSE_FRACTION(dld(v,m), Real(30*m + 7), Real(0.00008));
295 std::normal_distribution<> dis1{0, 0.0001};
296 Real omega = Real(1)/Real(16);
297 for (size_t i = 0; i < v.size(); ++i)
299 v[i] = sin(i*omega) + dis1(gen);
302 for (size_t n = 10; n < 20; ++n)
304 for (size_t p = 3; p < 100 && p < n/2; p += 2)
306 auto dld = discrete_lanczos_derivative(Real(1), n, p);
308 for (size_t m = n; m < v.size() - n && m < n + 10; ++m)
310 BOOST_CHECK_CLOSE_FRACTION(dld(v,m), omega*cos(omega*m), Real(0.03));
317 void test_boundary_velocity_filters()
319 std::cout << "Testing boundary filters on type " << typeid(Real).name() << "\n";
320 Real tol = std::numeric_limits<Real>::epsilon();
321 for(int n = 1; n < 5; ++n)
323 for (int p = 1; p < 2*n+1; ++p)
325 for (int s = -n; s <= n; ++s)
327 auto f = boundary_velocity_filter<Real>(n, p, s);
329 auto cond = summation_condition_number<Real>(0);
330 for (size_t i = 0; i < f.size() - 1; ++i)
335 BOOST_CHECK_CLOSE_FRACTION(cond.sum(), -f[f.size()-1], 6*cond()*tol);
337 cond = summation_condition_number<Real>(0);
338 for (size_t k = 0; k < f.size(); ++k)
340 Real j = Real(k) - Real(n);
341 // note the shifted index here:
344 BOOST_CHECK_CLOSE_FRACTION(cond.sum(), 1, 6*cond()*tol);
347 for (int l = 2; l <= p; ++l)
349 cond = summation_condition_number<Real>(0);
350 for (size_t k = 0; k < f.size() - 1; ++k)
352 Real j = Real(k) - Real(n);
353 // The condition number of this sum is infinite!
354 // No need to get to worked up about the tolerance.
355 cond += pow(j-s, l)*f[k];
358 Real expected = -pow(Real(f.size()-1) - Real(n) - Real(s), l)*f[f.size()-1];
361 BOOST_CHECK_SMALL(cond.sum(), cond()*tol);
365 BOOST_CHECK_CLOSE_FRACTION(expected, cond.sum(), 200*cond()*tol);
369 //std::cout << "(n,p,s) = ("<< n << ", " << p << "," << s << ") = {";
372 // std::cout << x << ", ";
374 //std::cout << "}\n";*/
381 void test_boundary_lanczos()
383 std::cout << "Testing Lanczos boundary on type " << typeid(Real).name() << "\n";
384 Real tol = std::numeric_limits<Real>::epsilon();
385 std::vector<Real> v(500, 7);
387 for (size_t n = 1; n < 10; ++n)
389 for (size_t p = 2; p < 2*n; ++p)
391 auto lsd = discrete_lanczos_derivative(Real(0.0125), n, p);
392 for (size_t m = 0; m < n; ++m)
394 Real dvdt = lsd(v,m);
395 BOOST_CHECK_SMALL(dvdt, 4*sqrt(tol));
397 for (size_t m = v.size() - n; m < v.size(); ++m)
399 Real dvdt = lsd(v,m);
400 BOOST_CHECK_SMALL(dvdt, 4*sqrt(tol));
405 for(size_t i = 0; i < v.size(); ++i)
410 for (size_t n = 3; n < 10; ++n)
412 for (size_t p = 2; p < 2*n; ++p)
414 auto lsd = discrete_lanczos_derivative(Real(1), n, p);
415 for (size_t m = 0; m < n; ++m)
417 Real dvdt = lsd(v,m);
418 BOOST_CHECK_CLOSE_FRACTION(dvdt, 7, sqrt(tol));
421 for (size_t m = v.size() - n; m < v.size(); ++m)
423 Real dvdt = lsd(v,m);
424 BOOST_CHECK_CLOSE_FRACTION(dvdt, 7, 4*sqrt(tol));
429 for (size_t i = 0; i < v.size(); ++i)
431 v[i] = 15*i*i + 7*i+8;
434 for (size_t n = 1; n < 10; ++n)
436 for (size_t p = 2; p < 2*n; ++p)
438 auto lsd = discrete_lanczos_derivative(Real(1), n, p);
439 for (size_t m = 0; m < v.size(); ++m)
441 BOOST_CHECK_CLOSE_FRACTION(lsd(v,m), 30*m+7, 30*sqrt(tol));
446 // Demonstrate that the boundary filters are also denoising:
447 //std::random_device rd{};
449 //std::cout << "seed = " << seed << "\n";
450 std::mt19937 gen(311354333);
451 std::normal_distribution<> dis{0, 0.01};
452 for (size_t i = 0; i < v.size(); ++i)
457 for (size_t n = 1; n < 10; ++n)
459 for (size_t p = 2; p < n; ++p)
461 auto lsd = discrete_lanczos_derivative(Real(1), n, p);
462 for (size_t m = 0; m < v.size(); ++m)
464 BOOST_CHECK_CLOSE_FRACTION(lsd(v,m), 30*m+7, 0.005);
467 for (size_t m = 0; m < v.size(); ++m)
469 BOOST_CHECK_CLOSE_FRACTION(dvdt[m], 30*m+7, 0.005);
476 void test_acceleration_filters()
478 Real eps = std::numeric_limits<Real>::epsilon();
479 for (size_t n = 1; n < 5; ++n)
481 for(size_t p = 3; p <= 2*n; ++p)
483 for(int64_t s = -int64_t(n); s <= 0; ++s)
485 auto g = boost::math::differentiation::detail::acceleration_filter<long double>(n,p,s);
487 std::vector<Real> f(g.size());
488 for (size_t i = 0; i < g.size(); ++i)
490 f[i] = static_cast<Real>(g[i]);
493 auto cond = summation_condition_number<Real>(0);
495 for (size_t i = 0; i < f.size() - 1; ++i)
499 BOOST_CHECK_CLOSE_FRACTION(cond.sum(), -f[f.size()-1], 10*cond()*eps);
502 cond = summation_condition_number<Real>(0);
503 for (size_t k = 0; k < f.size() -1; ++k)
505 Real j = Real(k) - Real(n);
508 Real expected = -(Real(f.size()-1)- Real(n) - s)*f[f.size()-1];
509 BOOST_CHECK_CLOSE_FRACTION(cond.sum(), expected, 10*cond()*eps);
511 cond = summation_condition_number<Real>(0);
512 for (size_t k = 0; k < f.size(); ++k)
514 Real j = Real(k) - Real(n);
515 cond += (j-s)*(j-s)*f[k];
517 BOOST_CHECK_CLOSE_FRACTION(cond.sum(), 2, 100*cond()*eps);
518 // See unlabelled equation in McDevitt, 2012, just after equation 26:
519 // It appears that there is an off-by-one error in that equation, since p + 1 moments don't vanish, only p.
520 // This test is itself suspect; the condition number of the moment sum is infinite.
521 // So the *slightest* error in the filter gets amplified by the test; in terms of the
522 // behavior of the actual filter, it's not a big deal.
523 for (size_t l = 3; l <= p; ++l)
525 cond = summation_condition_number<Real>(0);
526 for (size_t k = 0; k < f.size() - 1; ++k)
528 Real j = Real(k) - Real(n);
529 cond += pow((j-s), l)*f[k];
531 Real expected = -pow(Real(f.size()- 1 - n -s), l)*f[f.size()-1];
532 BOOST_CHECK_CLOSE_FRACTION(cond.sum(), expected, 1000*cond()*eps);
540 void test_lanczos_acceleration()
542 Real eps = std::numeric_limits<Real>::epsilon();
543 std::vector<Real> v(100, 7);
544 auto lanczos = discrete_lanczos_derivative<Real, 2>(Real(1), 4, 3);
545 for (size_t i = 0; i < v.size(); ++i)
547 BOOST_CHECK_SMALL(lanczos(v, i), eps);
550 for(size_t i = 0; i < v.size(); ++i)
554 for (size_t i = 0; i < v.size(); ++i)
556 BOOST_CHECK_SMALL(lanczos(v,i), 200*eps);
559 for(size_t i = 0; i < v.size(); ++i)
561 v[i] = 7*i*i + 9*i + 6;
563 for (size_t i = 0; i < v.size(); ++i)
565 BOOST_CHECK_CLOSE_FRACTION(lanczos(v, i), 14, 1500*eps);
568 // Now add noise, and kick up the smoothing of the Lanzcos derivative (increase n):
569 //std::random_device rd{};
571 //std::cout << "seed = " << seed << "\n";
572 size_t seed = 2507134629;
573 std::mt19937 gen(seed);
575 std::normal_distribution<Real> dis{0, std_dev};
576 for (size_t i = 0; i < v.size(); ++i)
580 lanczos = discrete_lanczos_derivative<Real, 2>(Real(1), 18, 3);
582 for (size_t i = 0; i < v.size(); ++i)
584 BOOST_CHECK_CLOSE_FRACTION(w[i], 14, std_dev/200);
589 void test_rescaling()
591 std::cout << "Test rescaling on type " << typeid(Real).name() << "\n";
592 Real tol = std::numeric_limits<Real>::epsilon();
593 std::vector<Real> v(500);
594 for(size_t i = 0; i < v.size(); ++i)
596 v[i] = 7*i*i + 9*i + 6;
598 std::vector<Real> dvdt1(500);
599 std::vector<Real> dvdt2(500);
600 auto lanczos1 = discrete_lanczos_derivative(Real(1));
601 auto lanczos2 = discrete_lanczos_derivative(Real(2));
606 for(size_t i = 0; i < v.size(); ++i)
608 BOOST_CHECK_CLOSE_FRACTION(dvdt1[i], 2*dvdt2[i], tol);
611 auto lanczos3 = discrete_lanczos_derivative<Real, 2>(Real(1));
612 auto lanczos4 = discrete_lanczos_derivative<Real, 2>(Real(2));
615 std::vector<Real> dv2dt21(500);
616 std::vector<Real> dv2dt22(500);
618 for(size_t i = 0; i < v.size(); ++i)
620 BOOST_CHECK_CLOSE_FRACTION(dv2dt21[i], 4*dv2dt22[i], tol);
625 void test_data_representations()
627 std::cout << "Test rescaling on type " << typeid(Real).name() << "\n";
628 Real tol = 150*std::numeric_limits<Real>::epsilon();
629 std::array<Real, 500> v;
630 for(size_t i = 0; i < v.size(); ++i)
634 std::array<Real, 500> dvdt;
635 auto lanczos = discrete_lanczos_derivative(Real(1));
639 for(size_t i = 0; i < v.size(); ++i)
641 BOOST_CHECK_CLOSE_FRACTION(dvdt[i], 9, tol);
644 boost::numeric::ublas::vector<Real> w(500);
645 boost::numeric::ublas::vector<Real> dwdt(500);
646 for(size_t i = 0; i < w.size(); ++i)
653 for(size_t i = 0; i < v.size(); ++i)
655 BOOST_CHECK_CLOSE_FRACTION(dwdt[i], 9, tol);
658 auto v1 = boost::make_iterator_range(v.begin(), v.end());
659 auto v2 = boost::make_iterator_range(dvdt.begin(), dvdt.end());
662 for(size_t i = 0; i < v2.size(); ++i)
664 BOOST_CHECK_CLOSE_FRACTION(v2[i], 9, tol);
667 auto lanczos2 = discrete_lanczos_derivative<Real, 2>(Real(1));
671 for(size_t i = 0; i < v2.size(); ++i)
673 BOOST_CHECK_SMALL(v2[i], 10*tol);
678 BOOST_AUTO_TEST_CASE(lanczos_smoothing_test)
680 test_dlp_second_derivative<double>();
681 test_dlp_norms<double>();
682 test_dlp_evaluation<double>();
683 test_dlp_derivatives<double>();
684 test_dlp_next<double>();
687 //test_dlp_norms<cpp_bin_float_50>();
688 test_boundary_velocity_filters<double>();
689 test_boundary_velocity_filters<long double>();
691 //test_boundary_velocity_filters<cpp_bin_float_50>();
692 test_boundary_lanczos<double>();
693 test_boundary_lanczos<long double>();
695 //test_boundary_lanczos<cpp_bin_float_50>();
697 test_interior_velocity_filter<double>();
698 test_interior_velocity_filter<long double>();
699 test_interior_lanczos<double>();
701 test_acceleration_filters<double>();
703 test_lanczos_acceleration<float>();
704 test_lanczos_acceleration<double>();
706 test_rescaling<double>();
707 test_data_representations<double>();