sinhc.hpp 4.5 KB

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