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/policies/policy.hpp> 21 #include <boost/math/special_functions/math_fwd.hpp> 22 #include <limits> 23 #include <string> 24 #include <stdexcept> 25 #include <cmath> 26 27 // These are the the "Sinus Cardinal" functions. 28 29 namespace boost 30 { 31 namespace math 32 { 33 namespace detail 34 { 35 // This is the "Sinus Cardinal" of index Pi. 36 37 template<typename T> sinc_pi_imp(const T x)38 inline T sinc_pi_imp(const T x) 39 { 40 BOOST_MATH_STD_USING 41 42 if (abs(x) >= 3.3 * tools::forth_root_epsilon<T>()) 43 { 44 return(sin(x)/x); 45 } 46 else 47 { 48 // |x| < (eps*120)^(1/4) 49 return 1 - x * x / 6; 50 } 51 } 52 53 } // namespace detail 54 55 template <class T> sinc_pi(T x)56 inline typename tools::promote_args<T>::type sinc_pi(T x) 57 { 58 typedef typename tools::promote_args<T>::type result_type; 59 return detail::sinc_pi_imp(static_cast<result_type>(x)); 60 } 61 62 template <class T, class Policy> sinc_pi(T x,const Policy &)63 inline typename tools::promote_args<T>::type sinc_pi(T x, const Policy&) 64 { 65 typedef typename tools::promote_args<T>::type result_type; 66 return detail::sinc_pi_imp(static_cast<result_type>(x)); 67 } 68 69 template<typename T, template<typename> class U> sinc_pi(const U<T> x)70 inline U<T> sinc_pi(const U<T> x) 71 { 72 BOOST_MATH_STD_USING 73 using ::std::numeric_limits; 74 75 T const taylor_0_bound = tools::epsilon<T>(); 76 T const taylor_2_bound = tools::root_epsilon<T>(); 77 T const taylor_n_bound = tools::forth_root_epsilon<T>(); 78 79 if (abs(x) >= taylor_n_bound) 80 { 81 return(sin(x)/x); 82 } 83 else 84 { 85 // approximation by taylor series in x at 0 up to order 0 86 #ifdef __MWERKS__ 87 U<T> result = static_cast<U<T> >(1); 88 #else 89 U<T> result = U<T>(1); 90 #endif 91 92 if (abs(x) >= taylor_0_bound) 93 { 94 U<T> x2 = x*x; 95 96 // approximation by taylor series in x at 0 up to order 2 97 result -= x2/static_cast<T>(6); 98 99 if (abs(x) >= taylor_2_bound) 100 { 101 // approximation by taylor series in x at 0 up to order 4 102 result += (x2*x2)/static_cast<T>(120); 103 } 104 } 105 106 return(result); 107 } 108 } 109 110 template<typename T, template<typename> class U, class Policy> sinc_pi(const U<T> x,const Policy &)111 inline U<T> sinc_pi(const U<T> x, const Policy&) 112 { 113 return sinc_pi(x); 114 } 115 } 116 } 117 118 #endif /* BOOST_SINC_HPP */ 119 120