124 lines · plain
1// boost sinc.hpp header file2 3// (C) Copyright Hubert Holin 2001.4// Distributed under the Boost Software License, Version 1.0. (See5// accompanying file LICENSE_1_0.txt or copy at6// 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_HPP11#define BOOST_SINC_HPP12 13 14#ifdef _MSC_VER15#pragma once16#endif17 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_NVRTC25#include <boost/math/special_functions/math_fwd.hpp>26#endif27 28// These are the the "Sinus Cardinal" functions.29 30namespace boost31{32 namespace math33 {34 namespace detail35 {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_USING42 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 else52 {53 // |x| < (eps*120)^(1/4)54 return 1 - x * x / 6;55 }56 }57 58 } // namespace detail59 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_USING78 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 else88 {89 // approximation by taylor series in x at 0 up to order 090 #ifdef __MWERKS__91 U<T> result = static_cast<U<T> >(1);92 #else93 U<T> result = U<T>(1);94 #endif95 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 2101 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 4106 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 124