|
| 1 | +/* |
| 2 | + * Copyright Nick Thompson, 2023 |
| 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 | + |
| 8 | +#include "math_unit_test.hpp" |
| 9 | +#include <numeric> |
| 10 | +#include <utility> |
| 11 | +#include <iomanip> |
| 12 | +#include <iostream> |
| 13 | +#include <random> |
| 14 | +#include <boost/math/tools/condition_numbers.hpp> |
| 15 | +#include <boost/math/constants/constants.hpp> |
| 16 | +#include <boost/math/quadrature/trapezoidal.hpp> |
| 17 | +#include <boost/math/special_functions/daubechies_scaling.hpp> |
| 18 | +#include <boost/math/special_functions/fourier_transform_daubechies_scaling.hpp> |
| 19 | + |
| 20 | +#ifdef BOOST_HAS_FLOAT128 |
| 21 | +#include <boost/multiprecision/float128.hpp> |
| 22 | +using boost::multiprecision::float128; |
| 23 | +#endif |
| 24 | + |
| 25 | +using boost::math::fourier_transform_daubechies_scaling; |
| 26 | +using boost::math::tools::summation_condition_number; |
| 27 | +using boost::math::constants::two_pi; |
| 28 | +using boost::math::constants::one_div_root_two_pi; |
| 29 | +using boost::math::quadrature::trapezoidal; |
| 30 | +// 𝓕[φ](-ω) = 𝓕[φ](ω)* |
| 31 | +template<typename Real, int p> |
| 32 | +void test_evaluation_symmetry() { |
| 33 | + auto phi = fourier_transform_daubechies_scaling<Real, p>(0.0); |
| 34 | + CHECK_ULP_CLOSE(one_div_root_two_pi<Real>(), phi.real(), 3); |
| 35 | + CHECK_ULP_CLOSE(static_cast<Real>(0), phi.imag(), 3); |
| 36 | + |
| 37 | + Real domega = Real(1)/128; |
| 38 | + for (Real omega = domega; omega < 10; omega += domega) { |
| 39 | + auto phi1 = fourier_transform_daubechies_scaling<Real, p>(-omega); |
| 40 | + auto phi2 = fourier_transform_daubechies_scaling<Real, p>(omega); |
| 41 | + CHECK_ULP_CLOSE(phi1.real(), phi2.real(), 3); |
| 42 | + CHECK_ULP_CLOSE(phi1.imag(), -phi2.imag(), 3); |
| 43 | + } |
| 44 | + |
| 45 | + for (Real omega = 10; omega < std::numeric_limits<double>::max(); omega *= 10) { |
| 46 | + auto phi1 = fourier_transform_daubechies_scaling<Real, p>(-omega); |
| 47 | + auto phi2 = fourier_transform_daubechies_scaling<Real, p>(omega); |
| 48 | + CHECK_ULP_CLOSE(phi1.real(), phi2.real(), 3); |
| 49 | + CHECK_ULP_CLOSE(phi1.imag(), -phi2.imag(), 3); |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +template<int p> |
| 54 | +void test_quadrature() { |
| 55 | + auto phi = boost::math::daubechies_scaling<double, p>(); |
| 56 | + auto [tmin, tmax] = phi.support(); |
| 57 | + double domega = 1/128.0; |
| 58 | + for (double omega = domega; omega < 10; omega += domega) { |
| 59 | + // I suspect the quadrature is less accurate than special function evaluation, so this is just a sanity check: |
| 60 | + auto f = [&](double t) { |
| 61 | + return phi(t)*std::exp(std::complex<double>(0, -omega*t))*one_div_root_two_pi<double>(); |
| 62 | + }; |
| 63 | + auto expected = trapezoidal(f, tmin, tmax, 2*std::numeric_limits<double>::epsilon()); |
| 64 | + auto computed = fourier_transform_daubechies_scaling<float, p>(static_cast<float>(omega)); |
| 65 | + CHECK_MOLLIFIED_CLOSE(static_cast<float>(expected.real()), computed.real(), 1e-9); |
| 66 | + CHECK_MOLLIFIED_CLOSE(static_cast<float>(expected.imag()), computed.imag(), 1e-9); |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +// Tests Daubechies "Ten Lectures on Wavelets", equation 5.1.19: |
| 71 | +template<typename Real, int p> |
| 72 | +void test_ten_lectures_eq_5_1_19() { |
| 73 | + Real domega = Real(1)/Real(16); |
| 74 | + for (Real omega = 0; omega < 1; omega += domega) { |
| 75 | + Real term = std::norm(fourier_transform_daubechies_scaling<Real, p>(omega)); |
| 76 | + auto sum = summation_condition_number<Real>(term); |
| 77 | + int64_t l = 1; |
| 78 | + while (l < 50 && term > 2*std::numeric_limits<Real>::epsilon()) { |
| 79 | + Real tpl = std::norm(fourier_transform_daubechies_scaling<Real, p>(omega + two_pi<Real>()*l)); |
| 80 | + Real tml = std::norm(fourier_transform_daubechies_scaling<Real, p>(omega - two_pi<Real>()*l)); |
| 81 | + |
| 82 | + sum += tpl; |
| 83 | + sum += tml; |
| 84 | + Real term = tpl + tml; |
| 85 | + ++l; |
| 86 | + } |
| 87 | + CHECK_ULP_CLOSE(1/two_pi<Real>(), sum.sum(), 13); |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +int main() |
| 92 | +{ |
| 93 | + test_evaluation_symmetry<float, 2>(); |
| 94 | + test_evaluation_symmetry<float, 6>(); |
| 95 | + test_evaluation_symmetry<float, 8>(); |
| 96 | + test_evaluation_symmetry<float, 16>(); |
| 97 | + |
| 98 | + test_quadrature<17>(); |
| 99 | + test_quadrature<18>(); |
| 100 | + |
| 101 | + test_ten_lectures_eq_5_1_19<float, 2>(); |
| 102 | + test_ten_lectures_eq_5_1_19<float, 3>(); |
| 103 | + test_ten_lectures_eq_5_1_19<float, 4>(); |
| 104 | + test_ten_lectures_eq_5_1_19<float, 5>(); |
| 105 | + test_ten_lectures_eq_5_1_19<float, 6>(); |
| 106 | + test_ten_lectures_eq_5_1_19<float, 7>(); |
| 107 | + test_ten_lectures_eq_5_1_19<float, 8>(); |
| 108 | + test_ten_lectures_eq_5_1_19<float, 9>(); |
| 109 | + test_ten_lectures_eq_5_1_19<float, 10>(); |
| 110 | + |
| 111 | + return boost::math::test::report_errors(); |
| 112 | +} |
0 commit comments