| 1 |
// boost sinc.hpp header file |
| 2 |
|
| 3 |
// (C) Copyright Hubert Holin 2001. |
| 4 |
// Distributed under the Boost Software License, Version 1.0. (See |
| 5 |
// accompanying file LICENSE_1_0.txt or copy at |
| 6 |
// http://www.boost.org/LICENSE_1_0.txt) |
| 7 |
|
| 8 |
// See http://www.boost.org for updates, documentation, and revision history. |
| 9 |
|
| 10 |
#ifndef BOOST_SINC_HPP |
| 11 |
#define BOOST_SINC_HPP |
| 12 |
|
| 13 |
|
| 14 |
#ifdef _MSC_VER |
| 15 |
#pragma once |
| 16 |
#endif |
| 17 |
|
| 18 |
#include <boost/math/tools/config.hpp> |
| 19 |
#include <boost/math/tools/precision.hpp> |
| 20 |
#include <boost/math/tools/promotion.hpp> |
| 21 |
#include <boost/math/policies/policy.hpp> |
| 22 |
#include <boost/math/special_functions/fpclassify.hpp> |
| 23 |
|
| 24 |
#ifndef BOOST_MATH_HAS_NVRTC |
| 25 |
#include <boost/math/special_functions/math_fwd.hpp> |
| 26 |
#endif |
| 27 |
|
| 28 |
// These are the the "Sinus Cardinal" functions. |
| 29 |
|
| 30 |
namespace boost |
| 31 |
{ |
| 32 |
namespace math |
| 33 |
{ |
| 34 |
namespace detail |
| 35 |
{ |
| 36 |
// This is the "Sinus Cardinal" of index Pi. |
| 37 |
|
| 38 |
template<typename T> |
| 39 |
BOOST_MATH_GPU_ENABLED inline T sinc_pi_imp(const T x) |
| 40 |
{ |
| 41 |
BOOST_MATH_STD_USING |
| 42 |
|
| 43 |
if ((boost::math::isinf)(x)) |
| 44 |
{ |
| 45 |
return 0; |
| 46 |
} |
| 47 |
else if (abs(x) >= T(3.3) * tools::forth_root_epsilon<T>()) |
| 48 |
{ |
| 49 |
return(sin(x)/x); |
| 50 |
} |
| 51 |
else |
| 52 |
{ |
| 53 |
// |x| < (eps*120)^(1/4) |
| 54 |
return 1 - x * x / 6; |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
} // namespace detail |
| 59 |
|
| 60 |
template <class T> |
| 61 |
BOOST_MATH_GPU_ENABLED inline typename tools::promote_args<T>::type sinc_pi(T x) |
| 62 |
{ |
| 63 |
typedef typename tools::promote_args<T>::type result_type; |
| 64 |
return detail::sinc_pi_imp(static_cast<result_type>(x)); |
| 65 |
} |
| 66 |
|
| 67 |
template <class T, class Policy> |
| 68 |
BOOST_MATH_GPU_ENABLED inline typename tools::promote_args<T>::type sinc_pi(T x, const Policy&) |
| 69 |
{ |
| 70 |
typedef typename tools::promote_args<T>::type result_type; |
| 71 |
return detail::sinc_pi_imp(static_cast<result_type>(x)); |
| 72 |
} |
| 73 |
|
| 74 |
template<typename T, template<typename> class U> |
| 75 |
BOOST_MATH_GPU_ENABLED inline U<T> sinc_pi(const U<T> x) |
| 76 |
{ |
| 77 |
BOOST_MATH_STD_USING |
| 78 |
|
| 79 |
T const taylor_0_bound = tools::epsilon<T>(); |
| 80 |
T const taylor_2_bound = tools::root_epsilon<T>(); |
| 81 |
T const taylor_n_bound = tools::forth_root_epsilon<T>(); |
| 82 |
|
| 83 |
if (abs(x) >= taylor_n_bound) |
| 84 |
{ |
| 85 |
return(sin(x)/x); |
| 86 |
} |
| 87 |
else |
| 88 |
{ |
| 89 |
// approximation by taylor series in x at 0 up to order 0 |
| 90 |
#ifdef __MWERKS__ |
| 91 |
U<T> result = static_cast<U<T> >(1); |
| 92 |
#else |
| 93 |
U<T> result = U<T>(1); |
| 94 |
#endif |
| 95 |
|
| 96 |
if (abs(x) >= taylor_0_bound) |
| 97 |
{ |
| 98 |
U<T> x2 = x*x; |
| 99 |
|
| 100 |
// approximation by taylor series in x at 0 up to order 2 |
| 101 |
result -= x2/static_cast<T>(6); |
| 102 |
|
| 103 |
if (abs(x) >= taylor_2_bound) |
| 104 |
{ |
| 105 |
// approximation by taylor series in x at 0 up to order 4 |
| 106 |
result += (x2*x2)/static_cast<T>(120); |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
return(result); |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
template<typename T, template<typename> class U, class Policy> |
| 115 |
BOOST_MATH_GPU_ENABLED inline U<T> sinc_pi(const U<T> x, const Policy&) |
| 116 |
{ |
| 117 |
return sinc_pi(x); |
| 118 |
} |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
#endif /* BOOST_SINC_HPP */ |
| 123 |
|