whittaker_shannon_detail.hpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. // Copyright Nick Thompson, 2019
  2. // Use, modification and distribution are subject to the
  3. // Boost Software License, Version 1.0.
  4. // (See accompanying file LICENSE_1_0.txt
  5. // or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. #ifndef BOOST_MATH_INTERPOLATORS_WHITAKKER_SHANNON_DETAIL_HPP
  7. #define BOOST_MATH_INTERPOLATORS_WHITAKKER_SHANNON_DETAIL_HPP
  8. #include <boost/assert.hpp>
  9. #include <boost/math/constants/constants.hpp>
  10. #include <boost/math/special_functions/sin_pi.hpp>
  11. #include <boost/math/special_functions/cos_pi.hpp>
  12. namespace boost { namespace math { namespace interpolators { namespace detail {
  13. template<class RandomAccessContainer>
  14. class whittaker_shannon_detail {
  15. public:
  16. using Real = typename RandomAccessContainer::value_type;
  17. whittaker_shannon_detail(RandomAccessContainer&& y, Real const & t0, Real const & h) : m_y{std::move(y)}, m_t0{t0}, m_h{h}
  18. {
  19. for (size_t i = 1; i < m_y.size(); i += 2)
  20. {
  21. m_y[i] = -m_y[i];
  22. }
  23. }
  24. inline Real operator()(Real t) const {
  25. using boost::math::constants::pi;
  26. using std::isfinite;
  27. using std::floor;
  28. Real y = 0;
  29. Real x = (t - m_t0)/m_h;
  30. Real z = x;
  31. auto it = m_y.begin();
  32. // For some reason, neither clang nor g++ will cache the address of m_y.end() in a register.
  33. // Hence make a copy of it:
  34. auto end = m_y.end();
  35. while(it != end)
  36. {
  37. y += *it++/z;
  38. z -= 1;
  39. }
  40. if (!isfinite(y))
  41. {
  42. BOOST_ASSERT_MSG(floor(x) == ceil(x), "Floor and ceiling should be equal.\n");
  43. size_t i = static_cast<size_t>(floor(x));
  44. if (i & 1)
  45. {
  46. return -m_y[i];
  47. }
  48. return m_y[i];
  49. }
  50. return y*boost::math::sin_pi(x)/pi<Real>();
  51. }
  52. Real prime(Real t) const {
  53. using boost::math::constants::pi;
  54. using std::isfinite;
  55. using std::floor;
  56. Real x = (t - m_t0)/m_h;
  57. if (ceil(x) == x) {
  58. Real s = 0;
  59. long j = static_cast<long>(x);
  60. long n = m_y.size();
  61. for (long i = 0; i < n; ++i)
  62. {
  63. if (j - i != 0)
  64. {
  65. s += m_y[i]/(j-i);
  66. }
  67. // else derivative of sinc at zero is zero.
  68. }
  69. if (j & 1) {
  70. s /= -m_h;
  71. } else {
  72. s /= m_h;
  73. }
  74. return s;
  75. }
  76. Real z = x;
  77. auto it = m_y.begin();
  78. Real cospix = boost::math::cos_pi(x);
  79. Real sinpix_div_pi = boost::math::sin_pi(x)/pi<Real>();
  80. Real s = 0;
  81. auto end = m_y.end();
  82. while(it != end)
  83. {
  84. s += (*it++)*(z*cospix - sinpix_div_pi)/(z*z);
  85. z -= 1;
  86. }
  87. return s/m_h;
  88. }
  89. Real operator[](size_t i) const {
  90. if (i & 1)
  91. {
  92. return -m_y[i];
  93. }
  94. return m_y[i];
  95. }
  96. RandomAccessContainer&& return_data() {
  97. for (size_t i = 1; i < m_y.size(); i += 2)
  98. {
  99. m_y[i] = -m_y[i];
  100. }
  101. return std::move(m_y);
  102. }
  103. private:
  104. RandomAccessContainer m_y;
  105. Real m_t0;
  106. Real m_h;
  107. };
  108. }}}}
  109. #endif