sinc.hpp 3.5 KB

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