sinc.hpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 <limits>
  17. #include <string>
  18. #include <stdexcept>
  19. #include <cmath>
  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. inline T sinc_pi_imp(const T x)
  30. {
  31. BOOST_MATH_STD_USING
  32. if (abs(x) >= 3.3 * tools::forth_root_epsilon<T>())
  33. {
  34. return(sin(x)/x);
  35. }
  36. else
  37. {
  38. // |x| < (eps*120)^(1/4)
  39. return 1 - x * x / 6;
  40. }
  41. }
  42. } // namespace detail
  43. template <class T>
  44. inline typename tools::promote_args<T>::type sinc_pi(T x)
  45. {
  46. typedef typename tools::promote_args<T>::type result_type;
  47. return detail::sinc_pi_imp(static_cast<result_type>(x));
  48. }
  49. template <class T, class Policy>
  50. inline typename tools::promote_args<T>::type sinc_pi(T x, const Policy&)
  51. {
  52. typedef typename tools::promote_args<T>::type result_type;
  53. return detail::sinc_pi_imp(static_cast<result_type>(x));
  54. }
  55. template<typename T, template<typename> class U>
  56. inline U<T> sinc_pi(const U<T> x)
  57. {
  58. BOOST_MATH_STD_USING
  59. using ::std::numeric_limits;
  60. T const taylor_0_bound = tools::epsilon<T>();
  61. T const taylor_2_bound = tools::root_epsilon<T>();
  62. T const taylor_n_bound = tools::forth_root_epsilon<T>();
  63. if (abs(x) >= taylor_n_bound)
  64. {
  65. return(sin(x)/x);
  66. }
  67. else
  68. {
  69. // approximation by taylor series in x at 0 up to order 0
  70. #ifdef __MWERKS__
  71. U<T> result = static_cast<U<T> >(1);
  72. #else
  73. U<T> result = U<T>(1);
  74. #endif
  75. if (abs(x) >= taylor_0_bound)
  76. {
  77. U<T> x2 = x*x;
  78. // approximation by taylor series in x at 0 up to order 2
  79. result -= x2/static_cast<T>(6);
  80. if (abs(x) >= taylor_2_bound)
  81. {
  82. // approximation by taylor series in x at 0 up to order 4
  83. result += (x2*x2)/static_cast<T>(120);
  84. }
  85. }
  86. return(result);
  87. }
  88. }
  89. template<typename T, template<typename> class U, class Policy>
  90. inline U<T> sinc_pi(const U<T> x, const Policy&)
  91. {
  92. return sinc_pi(x);
  93. }
  94. }
  95. }
  96. #endif /* BOOST_SINC_HPP */