1 // (C) Copyright John Maddock 2005.
2 // Use, modification and distribution are subject to the
3 // Boost Software License, Version 1.0. (See accompanying file
4 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 #ifndef BOOST_MATH_COMPLEX_ATANH_INCLUDED
7 #define BOOST_MATH_COMPLEX_ATANH_INCLUDED
9 #ifndef BOOST_MATH_COMPLEX_DETAILS_INCLUDED
10 # include <boost/math/complex/details.hpp>
12 #ifndef BOOST_MATH_LOG1P_INCLUDED
13 # include <boost/math/special_functions/log1p.hpp>
15 #include <boost/assert.hpp>
17 #ifdef BOOST_NO_STDC_NAMESPACE
18 namespace std{ using ::sqrt; using ::fabs; using ::acos; using ::asin; using ::atan; using ::atan2; }
21 namespace boost{ namespace math{
24 std::complex<T> atanh(const std::complex<T>& z)
29 // Eric W. Weisstein. "Inverse Hyperbolic Tangent."
30 // From MathWorld--A Wolfram Web Resource.
31 // http://mathworld.wolfram.com/InverseHyperbolicTangent.html
33 // Also: The Wolfram Functions Site,
34 // http://functions.wolfram.com/ElementaryFunctions/ArcTanh/
36 // Also "Abramowitz and Stegun. Handbook of Mathematical Functions."
37 // at : http://jove.prohosting.com/~skripty/toc.htm
40 static const T half_pi = static_cast<T>(1.57079632679489661923132169163975144L);
41 static const T pi = static_cast<T>(3.141592653589793238462643383279502884197L);
42 static const T one = static_cast<T>(1.0L);
43 static const T two = static_cast<T>(2.0L);
44 static const T four = static_cast<T>(4.0L);
45 static const T zero = static_cast<T>(0);
46 static const T a_crossover = static_cast<T>(0.3L);
48 T x = std::fabs(z.real());
49 T y = std::fabs(z.imag());
51 T real, imag; // our results
53 T safe_upper = detail::safe_max(two);
54 T safe_lower = detail::safe_min(static_cast<T>(2));
57 // Begin by handling the special cases specified in C99:
59 if(detail::test_is_nan(x))
61 if(detail::test_is_nan(y))
62 return std::complex<T>(x, x);
63 else if(std::numeric_limits<T>::has_infinity && (y == std::numeric_limits<T>::infinity()))
64 return std::complex<T>(0, ((z.imag() < 0) ? -half_pi : half_pi));
66 return std::complex<T>(x, x);
68 else if(detail::test_is_nan(y))
71 return std::complex<T>(x, y);
72 if(std::numeric_limits<T>::has_infinity && (x == std::numeric_limits<T>::infinity()))
73 return std::complex<T>(0, y);
75 return std::complex<T>(y, y);
77 else if((x > safe_lower) && (x < safe_upper) && (y > safe_lower) && (y < safe_upper))
85 // The real part is given by:
87 // real(atanh(z)) == log((1 + x^2 + y^2 + 2x) / (1 + x^2 + y^2 - 2x))
89 // However, when x is either large (x > 1/E) or very small
90 // (x < E) then this effectively simplifies
91 // to log(1), leading to wildly inaccurate results.
92 // By dividing the above (top and bottom) by (1 + x^2 + y^2) we get:
94 // real(atanh(z)) == log((1 + (2x / (1 + x^2 + y^2))) / (1 - (-2x / (1 + x^2 + y^2))))
96 // which is much more sensitive to the value of x, when x is not near 1
97 // (remember we can compute log(1+x) for small x very accurately).
99 // The cross-over from one method to the other has to be determined
100 // experimentally, the value used below appears correct to within a
101 // factor of 2 (and there are larger errors from other parts
102 // of the input domain anyway).
104 T alpha = two*x / (one + xx + yy);
105 if(alpha < a_crossover)
107 real = boost::math::log1p(alpha) - boost::math::log1p(-alpha);
112 real = boost::math::log1p(x2 + xx + yy) - std::log(xm1*xm1 + yy);
118 imag = std::atan2((y * two), (one - xx - yy));
126 // This section handles exception cases that would normally cause
127 // underflow or overflow in the main formulas.
129 // Begin by working out the real part, we need to approximate
130 // alpha = 2x / (1 + x^2 + y^2)
131 // without either overflow or underflow in the squared terms.
136 // this is really a test for infinity,
137 // but we may not have the necessary numeric_limits support:
138 if((x > (std::numeric_limits<T>::max)()) || (y > (std::numeric_limits<T>::max)()))
142 else if(y >= safe_upper)
144 // Big x and y: divide alpha through by x*y:
145 alpha = (two/y) / (x/y + y/x);
149 // Big x: divide through by x:
150 alpha = two / (x + y*y/x);
154 // Big x small y, as above but neglect y^2/x:
158 else if(y >= safe_upper)
162 // Big y, medium x, divide through by y:
163 alpha = (two*x/y) / (y + x*x/y);
167 // Small x and y, whatever alpha is, it's too small to calculate:
173 // one or both of x and y are small, calculate divisor carefully:
181 if(alpha < a_crossover)
183 real = boost::math::log1p(alpha) - boost::math::log1p(-alpha);
187 // We can only get here as a result of small y and medium sized x,
188 // we can simply neglect the y^2 terms:
189 BOOST_ASSERT(x >= safe_lower);
190 BOOST_ASSERT(x <= safe_upper);
191 //BOOST_ASSERT(y <= safe_lower);
193 real = std::log(1 + two*x + x*x) - std::log(xm1*xm1);
201 // Now handle imaginary part, this is much easier,
202 // if x or y are large, then the formula:
203 // atan2(2y, 1 - x^2 - y^2)
204 // evaluates to +-(PI - theta) where theta is negligible compared to PI.
206 if((x >= safe_upper) || (y >= safe_upper))
210 else if(x <= safe_lower)
213 // If both x and y are small then atan(2y),
214 // otherwise just x^2 is negligible in the divisor:
217 imag = std::atan2(two*y, one);
220 if((y == zero) && (x == zero))
223 imag = std::atan2(two*y, one - y*y);
229 // y^2 is negligible:
231 if((y == zero) && (x == one))
234 imag = std::atan2(two*y, 1 - x*x);
240 return std::complex<T>(real, imag);
245 #endif // BOOST_MATH_COMPLEX_ATANH_INCLUDED