whittaker_shannon_detail.hpp 3.3 KB

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