chebyshev_transform_test.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. /*
  2. * Copyright Nick Thompson, 2017
  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. #define BOOST_TEST_MODULE chebyshev_transform_test
  8. #include <boost/cstdfloat.hpp>
  9. #include <boost/type_index.hpp>
  10. #include <boost/test/included/unit_test.hpp>
  11. #include <boost/test/tools/floating_point_comparison.hpp>
  12. #include <boost/math/special_functions/chebyshev.hpp>
  13. #include <boost/math/special_functions/chebyshev_transform.hpp>
  14. #include <boost/math/special_functions/sinc.hpp>
  15. #include <boost/multiprecision/cpp_bin_float.hpp>
  16. #include <boost/multiprecision/cpp_dec_float.hpp>
  17. #if !defined(TEST1) && !defined(TEST2) && !defined(TEST3) && !defined(TEST4)
  18. # define TEST1
  19. # define TEST2
  20. # define TEST3
  21. # define TEST4
  22. #endif
  23. using boost::multiprecision::cpp_bin_float_quad;
  24. using boost::multiprecision::cpp_bin_float_50;
  25. using boost::multiprecision::cpp_bin_float_100;
  26. using boost::math::chebyshev_t;
  27. using boost::math::chebyshev_t_prime;
  28. using boost::math::chebyshev_u;
  29. using boost::math::chebyshev_transform;
  30. template<class Real>
  31. void test_sin_chebyshev_transform()
  32. {
  33. using boost::math::chebyshev_transform;
  34. using boost::math::constants::half_pi;
  35. using std::sin;
  36. using std::cos;
  37. using std::abs;
  38. Real tol = 10*std::numeric_limits<Real>::epsilon();
  39. auto f = [](Real x) { return sin(x); };
  40. Real a = 0;
  41. Real b = 1;
  42. chebyshev_transform<Real> cheb(f, a, b, tol);
  43. Real x = a;
  44. while (x < b)
  45. {
  46. Real s = sin(x);
  47. Real c = cos(x);
  48. if (abs(s) < tol)
  49. {
  50. BOOST_CHECK_SMALL(cheb(x), 100*tol);
  51. BOOST_CHECK_CLOSE_FRACTION(c, cheb.prime(x), 100*tol);
  52. }
  53. else
  54. {
  55. BOOST_CHECK_CLOSE_FRACTION(s, cheb(x), 100*tol);
  56. if (abs(c) < tol)
  57. {
  58. BOOST_CHECK_SMALL(cheb.prime(x), 100*tol);
  59. }
  60. else
  61. {
  62. BOOST_CHECK_CLOSE_FRACTION(c, cheb.prime(x), 100*tol);
  63. }
  64. }
  65. x += static_cast<Real>(1)/static_cast<Real>(1 << 7);
  66. }
  67. Real Q = cheb.integrate();
  68. BOOST_CHECK_CLOSE_FRACTION(1 - cos(static_cast<Real>(1)), Q, 100*tol);
  69. }
  70. template<class Real>
  71. void test_sinc_chebyshev_transform()
  72. {
  73. using std::cos;
  74. using std::sin;
  75. using std::abs;
  76. using boost::math::sinc_pi;
  77. using boost::math::chebyshev_transform;
  78. using boost::math::constants::half_pi;
  79. Real tol = 500*std::numeric_limits<Real>::epsilon();
  80. auto f = [](Real x) { return boost::math::sinc_pi(x); };
  81. Real a = 0;
  82. Real b = 1;
  83. chebyshev_transform<Real> cheb(f, a, b, tol/50);
  84. Real x = a;
  85. while (x < b)
  86. {
  87. Real s = sinc_pi(x);
  88. Real ds = (cos(x)-sinc_pi(x))/x;
  89. if (x == 0) { ds = 0; }
  90. if (s < tol)
  91. {
  92. BOOST_CHECK_SMALL(cheb(x), tol);
  93. }
  94. else
  95. {
  96. BOOST_CHECK_CLOSE_FRACTION(s, cheb(x), tol);
  97. }
  98. if (abs(ds) < tol)
  99. {
  100. BOOST_CHECK_SMALL(cheb.prime(x), 5 * tol);
  101. }
  102. else
  103. {
  104. BOOST_CHECK_CLOSE_FRACTION(ds, cheb.prime(x), 300*tol);
  105. }
  106. x += static_cast<Real>(1)/static_cast<Real>(1 << 7);
  107. }
  108. Real Q = cheb.integrate();
  109. //NIntegrate[Sinc[x], {x, 0, 1}, WorkingPrecision -> 200, AccuracyGoal -> 150, PrecisionGoal -> 150, MaxRecursion -> 150]
  110. Real Q_exp = boost::lexical_cast<Real>("0.94608307036718301494135331382317965781233795473811179047145477356668");
  111. BOOST_CHECK_CLOSE_FRACTION(Q_exp, Q, tol);
  112. }
  113. //Examples taken from "Approximation Theory and Approximation Practice", by Trefethen
  114. template<class Real>
  115. void test_atap_examples()
  116. {
  117. using std::sin;
  118. using boost::math::constants::half;
  119. using boost::math::sinc_pi;
  120. using boost::math::chebyshev_transform;
  121. using boost::math::constants::half_pi;
  122. Real tol = 10*std::numeric_limits<Real>::epsilon();
  123. auto f1 = [](Real x) { return ((0 < x) - (x < 0)) - x/2; };
  124. auto f2 = [](Real x) { Real t = sin(6*x); Real s = sin(x + exp(2*x));
  125. Real u = (0 < s) - (s < 0);
  126. return t + u; };
  127. auto f3 = [](Real x) { return sin(6*x) + sin(60*exp(x)); };
  128. auto f4 = [](Real x) { return 1/(1+1000*(x+half<Real>())*(x+half<Real>())) + 1/sqrt(1+1000*(x-.5)*(x-0.5));};
  129. Real a = -1;
  130. Real b = 1;
  131. chebyshev_transform<Real> cheb1(f1, a, b);
  132. chebyshev_transform<Real> cheb2(f2, a, b, tol);
  133. //chebyshev_transform<Real> cheb3(f3, a, b, tol);
  134. Real x = a;
  135. while (x < b)
  136. {
  137. //Real s = f1(x);
  138. if (sizeof(Real) == sizeof(float))
  139. {
  140. BOOST_CHECK_CLOSE_FRACTION(f1(x), cheb1(x), 4e-3);
  141. }
  142. else
  143. {
  144. BOOST_CHECK_CLOSE_FRACTION(f1(x), cheb1(x), 1.3e-5);
  145. }
  146. BOOST_CHECK_CLOSE_FRACTION(f2(x), cheb2(x), 6e-3);
  147. //BOOST_CHECK_CLOSE_FRACTION(f3(x), cheb3(x), 100*tol);
  148. x += static_cast<Real>(1)/static_cast<Real>(1 << 7);
  149. }
  150. }
  151. //Validate that the Chebyshev polynomials are well approximated by the Chebyshev transform.
  152. template<class Real>
  153. void test_chebyshev_chebyshev_transform()
  154. {
  155. Real tol = 500*std::numeric_limits<Real>::epsilon();
  156. // T_0 = 1:
  157. auto t0 = [](Real) { return 1; };
  158. chebyshev_transform<Real> cheb0(t0, -1, 1);
  159. BOOST_CHECK_CLOSE_FRACTION(cheb0.coefficients()[0], 2, tol);
  160. Real x = -1;
  161. while (x < 1)
  162. {
  163. BOOST_CHECK_CLOSE_FRACTION(cheb0(x), 1, tol);
  164. BOOST_CHECK_SMALL(cheb0.prime(x), tol);
  165. x += static_cast<Real>(1)/static_cast<Real>(1 << 7);
  166. }
  167. // T_1 = x:
  168. auto t1 = [](Real x) { return x; };
  169. chebyshev_transform<Real> cheb1(t1, -1, 1);
  170. BOOST_CHECK_CLOSE_FRACTION(cheb1.coefficients()[1], 1, tol);
  171. x = -1;
  172. while (x < 1)
  173. {
  174. if (x == 0)
  175. {
  176. BOOST_CHECK_SMALL(cheb1(x), tol);
  177. }
  178. else
  179. {
  180. BOOST_CHECK_CLOSE_FRACTION(cheb1(x), x, tol);
  181. }
  182. BOOST_CHECK_CLOSE_FRACTION(cheb1.prime(x), 1, tol);
  183. x += static_cast<Real>(1)/static_cast<Real>(1 << 7);
  184. }
  185. auto t2 = [](Real x) { return 2*x*x-1; };
  186. chebyshev_transform<Real> cheb2(t2, -1, 1);
  187. BOOST_CHECK_CLOSE_FRACTION(cheb2.coefficients()[2], 1, tol);
  188. x = -1;
  189. while (x < 1)
  190. {
  191. BOOST_CHECK_CLOSE_FRACTION(cheb2(x), t2(x), tol);
  192. if (x != 0)
  193. {
  194. BOOST_CHECK_CLOSE_FRACTION(cheb2.prime(x), 4*x, tol);
  195. }
  196. else
  197. {
  198. BOOST_CHECK_SMALL(cheb2.prime(x), tol);
  199. }
  200. x += static_cast<Real>(1)/static_cast<Real>(1 << 7);
  201. }
  202. }
  203. BOOST_AUTO_TEST_CASE(chebyshev_transform_test)
  204. {
  205. #ifdef TEST1
  206. test_chebyshev_chebyshev_transform<float>();
  207. test_sin_chebyshev_transform<float>();
  208. test_atap_examples<float>();
  209. test_sinc_chebyshev_transform<float>();
  210. #endif
  211. #ifdef TEST2
  212. test_chebyshev_chebyshev_transform<double>();
  213. test_sin_chebyshev_transform<double>();
  214. test_atap_examples<double>();
  215. test_sinc_chebyshev_transform<double>();
  216. #endif
  217. #ifdef TEST3
  218. test_chebyshev_chebyshev_transform<long double>();
  219. test_sin_chebyshev_transform<long double>();
  220. test_atap_examples<long double>();
  221. test_sinc_chebyshev_transform<long double>();
  222. #endif
  223. #ifdef TEST4
  224. #ifdef BOOST_HAS_FLOAT128
  225. test_chebyshev_chebyshev_transform<__float128>();
  226. test_sin_chebyshev_transform<__float128>();
  227. test_atap_examples<__float128>();
  228. test_sinc_chebyshev_transform<__float128>();
  229. #endif
  230. #endif
  231. }