atanh.hpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. // boost atanh.hpp header file
  2. // (C) Copyright 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_ATANH_HPP
  9. #define BOOST_ATANH_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/special_functions/fpclassify.hpp>
  19. // This is the inverse of the hyperbolic tangent function.
  20. namespace boost
  21. {
  22. namespace math
  23. {
  24. namespace detail
  25. {
  26. // This is the main fare
  27. template<typename T, typename Policy>
  28. inline T atanh_imp(const T x, const Policy& pol)
  29. {
  30. BOOST_MATH_STD_USING
  31. static const char* function = "boost::math::atanh<%1%>(%1%)";
  32. if(x < -1)
  33. {
  34. return policies::raise_domain_error<T>(function, "atanh requires x >= -1, but got x = %1%.", x, pol);
  35. }
  36. else if(x > 1)
  37. {
  38. return policies::raise_domain_error<T>(function, "atanh requires x <= 1, but got x = %1%.", x, pol);
  39. }
  40. else if((boost::math::isnan)(x))
  41. {
  42. return policies::raise_domain_error<T>(function, "atanh requires -1 <= x <= 1, but got x = %1%.", x, pol);
  43. }
  44. else if(x < -1 + tools::epsilon<T>())
  45. {
  46. // -Infinity:
  47. return -policies::raise_overflow_error<T>(function, nullptr, pol);
  48. }
  49. else if(x > 1 - tools::epsilon<T>())
  50. {
  51. // Infinity:
  52. return policies::raise_overflow_error<T>(function, nullptr, pol);
  53. }
  54. else if(abs(x) >= tools::forth_root_epsilon<T>())
  55. {
  56. // http://functions.wolfram.com/ElementaryFunctions/ArcTanh/02/
  57. if(abs(x) < 0.5f)
  58. return (boost::math::log1p(x, pol) - boost::math::log1p(-x, pol)) / 2;
  59. return(log( (1 + x) / (1 - x) ) / 2);
  60. }
  61. else
  62. {
  63. // http://functions.wolfram.com/ElementaryFunctions/ArcTanh/06/01/03/01/
  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>(3);
  71. }
  72. return(result);
  73. }
  74. }
  75. }
  76. template<typename T, typename Policy>
  77. inline typename tools::promote_args<T>::type atanh(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::atanh_imp(static_cast<value_type>(x), forwarding_policy()),
  89. "boost::math::atanh<%1%>(%1%)");
  90. }
  91. template<typename T>
  92. inline typename tools::promote_args<T>::type atanh(T x)
  93. {
  94. return boost::math::atanh(x, policies::policy<>());
  95. }
  96. }
  97. }
  98. #endif /* BOOST_ATANH_HPP */