cardinal_trigonometric_test.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /*
  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)
  6. */
  7. #include "math_unit_test.hpp"
  8. #include <vector>
  9. #include <random>
  10. #include <boost/math/constants/constants.hpp>
  11. #include <boost/math/interpolators/cardinal_trigonometric.hpp>
  12. #ifdef BOOST_HAS_FLOAT128
  13. #include <boost/multiprecision/float128.hpp>
  14. #endif
  15. using std::sin;
  16. using boost::math::constants::two_pi;
  17. using boost::math::interpolators::cardinal_trigonometric;
  18. template<class Real>
  19. void test_constant()
  20. {
  21. Real t0 = 0;
  22. Real h = 1;
  23. for(size_t n = 1; n < 20; ++n)
  24. {
  25. Real c = 8;
  26. std::vector<Real> v(n, c);
  27. auto ct = cardinal_trigonometric<decltype(v)>(v, t0, h);
  28. CHECK_ULP_CLOSE(c, ct(0.3), 3);
  29. CHECK_ULP_CLOSE(c*h*n, ct.integrate(), 3);
  30. CHECK_ULP_CLOSE(c*c*h*n, ct.squared_l2(), 3);
  31. CHECK_MOLLIFIED_CLOSE(Real(0), ct.prime(0.8), 25*std::numeric_limits<Real>::epsilon());
  32. CHECK_MOLLIFIED_CLOSE(Real(0), ct.double_prime(0.8), 25*std::numeric_limits<Real>::epsilon());
  33. }
  34. }
  35. template<class Real>
  36. void test_interpolation_condition()
  37. {
  38. std::mt19937 gen(1234);
  39. std::uniform_real_distribution<Real> dis(1, 10);
  40. for(size_t n = 1; n < 20; ++n) {
  41. Real t0 = dis(gen);
  42. Real h = dis(gen);
  43. std::vector<Real> v(n);
  44. for (size_t i = 0; i < n; ++i) {
  45. v[i] = dis(gen);
  46. }
  47. auto ct = cardinal_trigonometric<decltype(v)>(v, t0, h);
  48. for (size_t i = 0; i < n; ++i) {
  49. Real arg = t0 + i*h;
  50. Real expected = v[i];
  51. Real computed = ct(arg);
  52. if(!CHECK_ULP_CLOSE(expected, computed, 5*n))
  53. {
  54. std::cerr << " Samples: " << n << "\n";
  55. }
  56. }
  57. }
  58. }
  59. #ifdef BOOST_HAS_FLOAT128
  60. void test_constant_q()
  61. {
  62. __float128 t0 = 0;
  63. __float128 h = 1;
  64. for(size_t n = 1; n < 20; ++n)
  65. {
  66. __float128 c = 8;
  67. std::vector<__float128> v(n, c);
  68. auto ct = cardinal_trigonometric<decltype(v)>(v, t0, h);
  69. CHECK_ULP_CLOSE(boost::multiprecision::float128(c), boost::multiprecision::float128(ct(0.3)), 3);
  70. CHECK_ULP_CLOSE(boost::multiprecision::float128(c*h*n), boost::multiprecision::float128(ct.integrate()), 3);
  71. }
  72. }
  73. #endif
  74. template<class Real>
  75. void test_sampled_sine()
  76. {
  77. using std::sin;
  78. using std::cos;
  79. for (unsigned n = 15; n < 50; ++n)
  80. {
  81. Real t0 = 0;
  82. Real T = 1;
  83. Real h = T/n;
  84. std::vector<Real> v(n);
  85. auto s = [&](Real t) { return sin(two_pi<Real>()*(t-t0)/T);};
  86. auto s_prime = [&](Real t) { return two_pi<Real>()*cos(two_pi<Real>()*(t-t0)/T)/T;};
  87. auto s_double_prime = [&](Real t) { return -two_pi<Real>()*two_pi<Real>()*sin(two_pi<Real>()*(t-t0)/T)/(T*T);};
  88. for(size_t j = 0; j < v.size(); ++j)
  89. {
  90. Real t = t0 + j*h;
  91. v[j] = s(t);
  92. }
  93. auto ct = cardinal_trigonometric<decltype(v)>(v, t0, h);
  94. CHECK_ULP_CLOSE(T, ct.period(), 3);
  95. std::mt19937 gen(1234);
  96. std::uniform_real_distribution<Real> dist(0, 500);
  97. unsigned j = 0;
  98. while (j++ < 50) {
  99. Real arg = dist(gen);
  100. Real expected = s(arg);
  101. Real computed = ct(arg);
  102. CHECK_MOLLIFIED_CLOSE(expected, computed, std::numeric_limits<Real>::epsilon()*4000);
  103. expected = s_prime(arg);
  104. computed = ct.prime(arg);
  105. CHECK_MOLLIFIED_CLOSE(expected, computed, 18000*std::numeric_limits<Real>::epsilon());
  106. expected = s_double_prime(arg);
  107. computed = ct.double_prime(arg);
  108. CHECK_MOLLIFIED_CLOSE(expected, computed, 100000*std::numeric_limits<Real>::epsilon());
  109. }
  110. CHECK_MOLLIFIED_CLOSE(Real(0), ct.integrate(), std::numeric_limits<Real>::epsilon());
  111. }
  112. }
  113. template<class Real>
  114. void test_bump()
  115. {
  116. using std::exp;
  117. using std::abs;
  118. using std::sqrt;
  119. using std::pow;
  120. auto bump = [](Real x)->Real { if (abs(x) >= 1) { return Real(0); } return exp(-Real(1)/(Real(1)-x*x)); };
  121. auto bump_prime = [](Real x)->Real {
  122. if (abs(x) >= 1) { return Real(0); }
  123. return -2*x*exp(-Real(1)/(Real(1)-x*x))/pow(1-x*x,2);
  124. };
  125. auto bump_double_prime = [](Real x)->Real {
  126. if (abs(x) >= 1) { return Real(0); }
  127. return (6*pow(x,4)-2)*exp(-Real(1)/(Real(1)-x*x))/pow(1-x*x,4);
  128. };
  129. Real t0 = -1;
  130. size_t n = 4096;
  131. Real h = Real(2)/Real(n);
  132. std::vector<Real> v(n);
  133. for(size_t i = 0; i < n; ++i)
  134. {
  135. Real t = t0 + i*h;
  136. v[i] = bump(t);
  137. }
  138. auto ct = cardinal_trigonometric<decltype(v)>(v, t0, h);
  139. std::mt19937 gen(323723);
  140. std::uniform_real_distribution<long double> dis(-0.9, 0.9);
  141. size_t i = 0;
  142. while (i++ < 1000)
  143. {
  144. Real t = static_cast<Real>(dis(gen));
  145. Real expected = bump(t);
  146. Real computed = ct(t);
  147. if(!CHECK_MOLLIFIED_CLOSE(expected, computed, 2*std::numeric_limits<Real>::epsilon())) {
  148. std::cerr << " Problem occured at abscissa " << t << "\n";
  149. }
  150. expected = bump_prime(t);
  151. computed = ct.prime(t);
  152. if(!CHECK_MOLLIFIED_CLOSE(expected, computed, 4000*std::numeric_limits<Real>::epsilon())) {
  153. std::cerr << " Problem occured at abscissa " << t << "\n";
  154. }
  155. expected = bump_double_prime(t);
  156. computed = ct.double_prime(t);
  157. if(!CHECK_MOLLIFIED_CLOSE(expected, computed, 4000*4000*std::numeric_limits<Real>::epsilon())) {
  158. std::cerr << " Problem occured at abscissa " << t << "\n";
  159. }
  160. }
  161. // Wolfram Alpha:
  162. // NIntegrate[Exp[-1/(1-x*x)],{x,-1,1}]
  163. CHECK_ULP_CLOSE(Real(0.443993816168079437823L), ct.integrate(), 3);
  164. // NIntegrate[Exp[-2/(1-x*x)],{x,-1,1}]
  165. CHECK_ULP_CLOSE(Real(0.1330861208449942715569473279553285713625791551628130055345002588895389L), ct.squared_l2(), 1);
  166. }
  167. int main()
  168. {
  169. #ifdef TEST1
  170. test_constant<float>();
  171. test_sampled_sine<float>();
  172. test_bump<float>();
  173. test_interpolation_condition<float>();
  174. #endif
  175. #ifdef TEST2
  176. test_constant<double>();
  177. test_sampled_sine<double>();
  178. test_bump<double>();
  179. test_interpolation_condition<double>();
  180. #endif
  181. #ifdef TEST3
  182. test_constant<long double>();
  183. test_sampled_sine<long double>();
  184. test_bump<long double>();
  185. test_interpolation_condition<long double>();
  186. #endif
  187. #ifdef TEST4
  188. #ifdef BOOST_HAS_FLOAT128
  189. test_constant_q();
  190. #endif
  191. #endif
  192. return boost::math::test::report_errors();
  193. }