acosh.hpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. // boost asinh.hpp header file
  2. // (C) Copyright Eric Ford 2001 & Hubert Holin.
  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_ACOSH_HPP
  9. #define BOOST_ACOSH_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/policies/error_handling.hpp>
  17. #include <boost/math/special_functions/math_fwd.hpp>
  18. #include <boost/math/special_functions/log1p.hpp>
  19. #include <boost/math/constants/constants.hpp>
  20. // This is the inverse of the hyperbolic cosine function.
  21. namespace boost
  22. {
  23. namespace math
  24. {
  25. namespace detail
  26. {
  27. #if defined(__GNUC__) && (__GNUC__ < 3)
  28. // gcc 2.x ignores function scope using declarations,
  29. // put them in the scope of the enclosing namespace instead:
  30. using ::std::abs;
  31. using ::std::sqrt;
  32. using ::std::log;
  33. using ::std::numeric_limits;
  34. #endif
  35. template<typename T, typename Policy>
  36. inline T acosh_imp(const T x, const Policy& pol)
  37. {
  38. BOOST_MATH_STD_USING
  39. if(x < 1)
  40. {
  41. return policies::raise_domain_error<T>(
  42. "boost::math::acosh<%1%>(%1%)",
  43. "acosh requires x >= 1, but got x = %1%.", x, pol);
  44. }
  45. else if ((x - 1) >= tools::root_epsilon<T>())
  46. {
  47. if (x > 1 / tools::root_epsilon<T>())
  48. {
  49. // http://functions.wolfram.com/ElementaryFunctions/ArcCosh/06/01/06/01/0001/
  50. // approximation by laurent series in 1/x at 0+ order from -1 to 0
  51. return log(x) + constants::ln_two<T>();
  52. }
  53. else if(x < 1.5f)
  54. {
  55. // This is just a rearrangement of the standard form below
  56. // devised to minimse loss of precision when x ~ 1:
  57. T y = x - 1;
  58. return boost::math::log1p(y + sqrt(y * y + 2 * y), pol);
  59. }
  60. else
  61. {
  62. // http://functions.wolfram.com/ElementaryFunctions/ArcCosh/02/
  63. return( log( x + sqrt(x * x - 1) ) );
  64. }
  65. }
  66. else
  67. {
  68. // see http://functions.wolfram.com/ElementaryFunctions/ArcCosh/06/01/04/01/0001/
  69. T y = x - 1;
  70. // approximation by taylor series in y at 0 up to order 2
  71. T result = sqrt(2 * y) * (1 - y /12 + 3 * y * y / 160);
  72. return result;
  73. }
  74. }
  75. }
  76. template<typename T, typename Policy>
  77. inline typename tools::promote_args<T>::type acosh(T x, const Policy&)
  78. {
  79. typedef typename tools::promote_args<T>::type result_type;
  80. typedef typename policies::evaluation<result_type, Policy>::type value_type;
  81. typedef typename policies::normalise<
  82. Policy,
  83. policies::promote_float<false>,
  84. policies::promote_double<false>,
  85. policies::discrete_quantile<>,
  86. policies::assert_undefined<> >::type forwarding_policy;
  87. return policies::checked_narrowing_cast<result_type, forwarding_policy>(
  88. detail::acosh_imp(static_cast<value_type>(x), forwarding_policy()),
  89. "boost::math::acosh<%1%>(%1%)");
  90. }
  91. template<typename T>
  92. inline typename tools::promote_args<T>::type acosh(T x)
  93. {
  94. return boost::math::acosh(x, policies::policy<>());
  95. }
  96. }
  97. }
  98. #endif /* BOOST_ACOSH_HPP */