acosh.hpp 3.6 KB

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