sinhc.hpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. // boost sinhc.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_SINHC_HPP
  8. #define BOOST_SINHC_HPP
  9. #ifdef _MSC_VER
  10. #pragma once
  11. #endif
  12. #include <boost/math/tools/precision.hpp>
  13. #include <boost/math/special_functions/math_fwd.hpp>
  14. #include <limits>
  15. #include <string>
  16. #include <stdexcept>
  17. #include <cmath>
  18. // These are the the "Hyperbolic Sinus Cardinal" functions.
  19. namespace boost
  20. {
  21. namespace math
  22. {
  23. namespace detail
  24. {
  25. // This is the "Hyperbolic Sinus Cardinal" of index Pi.
  26. template<typename T>
  27. inline T sinhc_pi_imp(const T x)
  28. {
  29. using ::std::abs;
  30. using ::std::sinh;
  31. using ::std::sqrt;
  32. static T const taylor_0_bound = tools::epsilon<T>();
  33. static T const taylor_2_bound = sqrt(taylor_0_bound);
  34. static T const taylor_n_bound = sqrt(taylor_2_bound);
  35. if (abs(x) >= taylor_n_bound)
  36. {
  37. return(sinh(x)/x);
  38. }
  39. else
  40. {
  41. // approximation by taylor series in x at 0 up to order 0
  42. T result = static_cast<T>(1);
  43. if (abs(x) >= taylor_0_bound)
  44. {
  45. T x2 = x*x;
  46. // approximation by taylor series in x at 0 up to order 2
  47. result += x2/static_cast<T>(6);
  48. if (abs(x) >= taylor_2_bound)
  49. {
  50. // approximation by taylor series in x at 0 up to order 4
  51. result += (x2*x2)/static_cast<T>(120);
  52. }
  53. }
  54. return(result);
  55. }
  56. }
  57. } // namespace detail
  58. template <class T>
  59. inline typename tools::promote_args<T>::type sinhc_pi(T x)
  60. {
  61. typedef typename tools::promote_args<T>::type result_type;
  62. return detail::sinhc_pi_imp(static_cast<result_type>(x));
  63. }
  64. template <class T, class Policy>
  65. inline typename tools::promote_args<T>::type sinhc_pi(T x, const Policy&)
  66. {
  67. return boost::math::sinhc_pi(x);
  68. }
  69. template<typename T, template<typename> class U>
  70. inline U<T> sinhc_pi(const U<T> x)
  71. {
  72. using std::abs;
  73. using std::sinh;
  74. using std::sqrt;
  75. using ::std::numeric_limits;
  76. static T const taylor_0_bound = tools::epsilon<T>();
  77. static T const taylor_2_bound = sqrt(taylor_0_bound);
  78. static T const taylor_n_bound = sqrt(taylor_2_bound);
  79. if (abs(x) >= taylor_n_bound)
  80. {
  81. return(sinh(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. if (abs(x) >= taylor_0_bound)
  92. {
  93. U<T> x2 = x*x;
  94. // approximation by taylor series in x at 0 up to order 2
  95. result += x2/static_cast<T>(6);
  96. if (abs(x) >= taylor_2_bound)
  97. {
  98. // approximation by taylor series in x at 0 up to order 4
  99. result += (x2*x2)/static_cast<T>(120);
  100. }
  101. }
  102. return(result);
  103. }
  104. }
  105. }
  106. }
  107. #endif /* BOOST_SINHC_HPP */