asinh.hpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. // boost asinh.hpp header file
  2. // (C) Copyright Eric Ford & Hubert Holin 2001.
  3. // (C) Copyright John Maddock 2008.
  4. // Distributed under the Boost Software License, Version 1.0. (See
  5. // accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt)
  7. // See http://www.boost.org for updates, documentation, and revision history.
  8. #ifndef BOOST_ASINH_HPP
  9. #define BOOST_ASINH_HPP
  10. #ifdef _MSC_VER
  11. #pragma once
  12. #endif
  13. #include <boost/config/no_tr1/cmath.hpp>
  14. #include <boost/config.hpp>
  15. #include <boost/math/tools/precision.hpp>
  16. #include <boost/math/special_functions/math_fwd.hpp>
  17. #include <boost/math/special_functions/sqrt1pm1.hpp>
  18. #include <boost/math/special_functions/log1p.hpp>
  19. #include <boost/math/constants/constants.hpp>
  20. // This is the inverse of the hyperbolic sine function.
  21. namespace boost
  22. {
  23. namespace math
  24. {
  25. namespace detail{
  26. #if defined(__GNUC__) && (__GNUC__ < 3)
  27. // gcc 2.x ignores function scope using declarations,
  28. // put them in the scope of the enclosing namespace instead:
  29. using ::std::abs;
  30. using ::std::sqrt;
  31. using ::std::log;
  32. using ::std::numeric_limits;
  33. #endif
  34. template<typename T, class Policy>
  35. inline T asinh_imp(const T x, const Policy& pol)
  36. {
  37. BOOST_MATH_STD_USING
  38. if (x >= tools::forth_root_epsilon<T>())
  39. {
  40. if (x > 1 / tools::root_epsilon<T>())
  41. {
  42. // http://functions.wolfram.com/ElementaryFunctions/ArcSinh/06/01/06/01/0001/
  43. // approximation by laurent series in 1/x at 0+ order from -1 to 1
  44. return constants::ln_two<T>() + log(x) + 1/ (4 * x * x);
  45. }
  46. else if(x < 0.5f)
  47. {
  48. // As below, but rearranged to preserve digits:
  49. return boost::math::log1p(x + boost::math::sqrt1pm1(x * x, pol), pol);
  50. }
  51. else
  52. {
  53. // http://functions.wolfram.com/ElementaryFunctions/ArcSinh/02/
  54. return( log( x + sqrt(x*x+1) ) );
  55. }
  56. }
  57. else if (x <= -tools::forth_root_epsilon<T>())
  58. {
  59. return(-asinh(-x, pol));
  60. }
  61. else
  62. {
  63. // http://functions.wolfram.com/ElementaryFunctions/ArcSinh/06/01/03/01/0001/
  64. // approximation by taylor series in x at 0 up to order 2
  65. T result = x;
  66. if (abs(x) >= tools::root_epsilon<T>())
  67. {
  68. T x3 = x*x*x;
  69. // approximation by taylor series in x at 0 up to order 4
  70. result -= x3/static_cast<T>(6);
  71. }
  72. return(result);
  73. }
  74. }
  75. }
  76. template<typename T>
  77. inline typename tools::promote_args<T>::type asinh(T x)
  78. {
  79. return boost::math::asinh(x, policies::policy<>());
  80. }
  81. template<typename T, typename Policy>
  82. inline typename tools::promote_args<T>::type asinh(T x, const Policy&)
  83. {
  84. typedef typename tools::promote_args<T>::type result_type;
  85. typedef typename policies::evaluation<result_type, Policy>::type value_type;
  86. typedef typename policies::normalise<
  87. Policy,
  88. policies::promote_float<false>,
  89. policies::promote_double<false>,
  90. policies::discrete_quantile<>,
  91. policies::assert_undefined<> >::type forwarding_policy;
  92. return policies::checked_narrowing_cast<result_type, forwarding_policy>(
  93. detail::asinh_imp(static_cast<value_type>(x), forwarding_policy()),
  94. "boost::math::asinh<%1%>(%1%)");
  95. }
  96. }
  97. }
  98. #endif /* BOOST_ASINH_HPP */