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
|
/*
* 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)
*/
#include "math_unit_test.hpp"
#include <boost/type_index.hpp>
#include <boost/math/special_functions/chebyshev.hpp>
#include <boost/math/special_functions/chebyshev_transform.hpp>
#include <boost/math/special_functions/sinc.hpp>
#include <boost/math/tools/test_value.hpp>
#if !defined(TEST1) && !defined(TEST2) && !defined(TEST3) && !defined(TEST4)
# define TEST1
# define TEST2
# define TEST3
# define TEST4
#endif
using boost::math::chebyshev_t;
using boost::math::chebyshev_t_prime;
using boost::math::chebyshev_u;
using boost::math::chebyshev_transform;
template<class Real>
void test_sin_chebyshev_transform()
{
using boost::math::chebyshev_transform;
using boost::math::constants::half_pi;
using std::sin;
using std::cos;
using std::abs;
Real tol = std::numeric_limits<Real>::epsilon();
auto f = [](Real x)->Real { return sin(x); };
Real a = 0;
Real b = 1;
chebyshev_transform<Real> cheb(f, a, b, tol);
Real x = a;
while (x < b)
{
Real s = sin(x);
Real c = cos(x);
CHECK_ABSOLUTE_ERROR(s, cheb(x), tol);
CHECK_ABSOLUTE_ERROR(c, cheb.prime(x), 150*tol);
x += static_cast<Real>(1)/static_cast<Real>(1 << 7);
}
Real Q = cheb.integrate();
CHECK_ABSOLUTE_ERROR(1 - cos(static_cast<Real>(1)), Q, 100*tol);
}
template<class Real>
void test_sinc_chebyshev_transform()
{
using std::cos;
using std::sin;
using std::abs;
using boost::math::sinc_pi;
using boost::math::chebyshev_transform;
using boost::math::constants::half_pi;
Real tol = 100*std::numeric_limits<Real>::epsilon();
auto f = [](Real x) { return boost::math::sinc_pi(x); };
Real a = 0;
Real b = 1;
chebyshev_transform<Real> cheb(f, a, b, tol/50);
Real x = a;
while (x < b)
{
Real s = sinc_pi(x);
Real ds = (cos(x)-sinc_pi(x))/x;
if (x == 0) { ds = 0; }
CHECK_ABSOLUTE_ERROR(s, cheb(x), tol);
CHECK_ABSOLUTE_ERROR(ds, cheb.prime(x), 10*tol);
x += static_cast<Real>(1)/static_cast<Real>(1 << 7);
}
Real Q = cheb.integrate();
//NIntegrate[Sinc[x], {x, 0, 1}, WorkingPrecision -> 200, AccuracyGoal -> 150, PrecisionGoal -> 150, MaxRecursion -> 150]
Real Q_exp = BOOST_MATH_TEST_VALUE(Real, 0.94608307036718301494135331382317965781233795473811179047145477356668);
CHECK_ABSOLUTE_ERROR(Q_exp, Q, tol);
}
//Examples taken from "Approximation Theory and Approximation Practice", by Trefethen
template<class Real>
void test_atap_examples()
{
using std::sin;
using std::exp;
using std::sqrt;
using boost::math::constants::half;
using boost::math::sinc_pi;
using boost::math::chebyshev_transform;
using boost::math::constants::half_pi;
Real tol = 10*std::numeric_limits<Real>::epsilon();
auto f1 = [](Real x) { return ((0 < x) - (x < 0)) - x/2; };
auto f2 = [](Real x) { Real t = sin(6*x); Real s = sin(x + exp(2*x));
Real u = (0 < s) - (s < 0);
return t + u; };
//auto f3 = [](Real x) { return sin(6*x) + sin(60*exp(x)); };
//auto f4 = [](Real x) { return 1/(1+1000*(x+half<Real>())*(x+half<Real>())) + 1/sqrt(1+1000*(x-Real(1)/Real(2))*(x-Real(1)/Real(2)));};
Real a = -1;
Real b = 1;
chebyshev_transform<Real> cheb1(f1, a, b, tol);
chebyshev_transform<Real> cheb2(f2, a, b, tol);
//chebyshev_transform<Real> cheb3(f3, a, b, tol);
Real x = a;
while (x < b)
{
// f1 and f2 are not differentiable; standard convergence rate theorems don't apply.
// Basically, the max refinements are always hit; so the error is not related to the precision of the type.
Real acceptable_error = sqrt(tol);
Real acceptable_error_2 = 9e-4;
if (std::is_same<Real, long double>::value)
{
acceptable_error = 1.6e-5;
}
if (std::is_same<Real, double>::value)
{
acceptable_error *= 500;
}
CHECK_ABSOLUTE_ERROR(f1(x), cheb1(x), acceptable_error);
CHECK_ABSOLUTE_ERROR(f2(x), cheb2(x), acceptable_error_2);
x += static_cast<Real>(1)/static_cast<Real>(1 << 7);
}
}
//Validate that the Chebyshev polynomials are well approximated by the Chebyshev transform.
template<class Real>
void test_chebyshev_chebyshev_transform()
{
Real tol = 500*std::numeric_limits<Real>::epsilon();
// T_0 = 1:
auto t0 = [](Real) { return 1; };
chebyshev_transform<Real> cheb0(t0, -1, 1);
CHECK_ABSOLUTE_ERROR(2, cheb0.coefficients()[0], tol);
Real x = -1;
while (x < 1)
{
CHECK_ABSOLUTE_ERROR(1, cheb0(x), tol);
CHECK_ABSOLUTE_ERROR(Real(0), cheb0.prime(x), tol);
x += static_cast<Real>(1)/static_cast<Real>(1 << 7);
}
// T_1 = x:
auto t1 = [](Real x) { return x; };
chebyshev_transform<Real> cheb1(t1, -1, 1);
CHECK_ABSOLUTE_ERROR(Real(1), cheb1.coefficients()[1], tol);
x = -1;
while (x < 1)
{
CHECK_ABSOLUTE_ERROR(x, cheb1(x), tol);
CHECK_ABSOLUTE_ERROR(Real(1), cheb1.prime(x), tol);
x += static_cast<Real>(1)/static_cast<Real>(1 << 7);
}
auto t2 = [](Real x) { return 2*x*x-1; };
chebyshev_transform<Real> cheb2(t2, -1, 1);
CHECK_ABSOLUTE_ERROR(Real(1), cheb2.coefficients()[2], tol);
x = -1;
while (x < 1)
{
CHECK_ABSOLUTE_ERROR(t2(x), cheb2(x), tol);
CHECK_ABSOLUTE_ERROR(4*x, cheb2.prime(x), tol);
x += static_cast<Real>(1)/static_cast<Real>(1 << 7);
}
}
int main()
{
#ifdef TEST1
test_chebyshev_chebyshev_transform<float>();
test_sin_chebyshev_transform<float>();
test_atap_examples<float>();
test_sinc_chebyshev_transform<float>();
#endif
#ifdef TEST2
test_chebyshev_chebyshev_transform<double>();
test_sin_chebyshev_transform<double>();
test_atap_examples<double>();
test_sinc_chebyshev_transform<double>();
#endif
#ifdef TEST3
test_chebyshev_chebyshev_transform<long double>();
test_sin_chebyshev_transform<long double>();
test_atap_examples<long double>();
test_sinc_chebyshev_transform<long double>();
#endif
#ifdef TEST4
#ifdef BOOST_HAS_FLOAT128
test_chebyshev_chebyshev_transform<__float128>();
test_sin_chebyshev_transform<__float128>();
test_atap_examples<__float128>();
test_sinc_chebyshev_transform<__float128>();
#endif
#endif
return boost::math::test::report_errors();
}
|