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