atanh.hpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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 <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. // 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>(
  35. function,
  36. "atanh requires x >= -1, but got x = %1%.", x, pol);
  37. }
  38. else if(x > 1)
  39. {
  40. return policies::raise_domain_error<T>(
  41. function,
  42. "atanh requires x <= 1, but got x = %1%.", x, pol);
  43. }
  44. else if((boost::math::isnan)(x))
  45. {
  46. return policies::raise_domain_error<T>(
  47. function,
  48. "atanh requires -1 <= x <= 1, but got x = %1%.", x, pol);
  49. }
  50. else if(x < -1 + tools::epsilon<T>())
  51. {
  52. // -Infinity:
  53. return -policies::raise_overflow_error<T>(function, 0, pol);
  54. }
  55. else if(x > 1 - tools::epsilon<T>())
  56. {
  57. // Infinity:
  58. return policies::raise_overflow_error<T>(function, 0, pol);
  59. }
  60. else if(abs(x) >= tools::forth_root_epsilon<T>())
  61. {
  62. // http://functions.wolfram.com/ElementaryFunctions/ArcTanh/02/
  63. if(abs(x) < 0.5f)
  64. return (boost::math::log1p(x, pol) - boost::math::log1p(-x, pol)) / 2;
  65. return(log( (1 + x) / (1 - x) ) / 2);
  66. }
  67. else
  68. {
  69. // http://functions.wolfram.com/ElementaryFunctions/ArcTanh/06/01/03/01/
  70. // approximation by taylor series in x at 0 up to order 2
  71. T result = x;
  72. if (abs(x) >= tools::root_epsilon<T>())
  73. {
  74. T x3 = x*x*x;
  75. // approximation by taylor series in x at 0 up to order 4
  76. result += x3/static_cast<T>(3);
  77. }
  78. return(result);
  79. }
  80. }
  81. }
  82. template<typename T, typename Policy>
  83. inline typename tools::promote_args<T>::type atanh(T x, const Policy&)
  84. {
  85. typedef typename tools::promote_args<T>::type result_type;
  86. typedef typename policies::evaluation<result_type, Policy>::type value_type;
  87. typedef typename policies::normalise<
  88. Policy,
  89. policies::promote_float<false>,
  90. policies::promote_double<false>,
  91. policies::discrete_quantile<>,
  92. policies::assert_undefined<> >::type forwarding_policy;
  93. return policies::checked_narrowing_cast<result_type, forwarding_policy>(
  94. detail::atanh_imp(static_cast<value_type>(x), forwarding_policy()),
  95. "boost::math::atanh<%1%>(%1%)");
  96. }
  97. template<typename T>
  98. inline typename tools::promote_args<T>::type atanh(T x)
  99. {
  100. return boost::math::atanh(x, policies::policy<>());
  101. }
  102. }
  103. }
  104. #endif /* BOOST_ATANH_HPP */