acosh.hpp 3.6 KB

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