sinc.hpp 3.3 KB

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