atanh.hpp 4.1 KB

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