acosh.hpp 3.6 KB

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