sl@0: // (C) Copyright John Maddock 2005. sl@0: // Use, modification and distribution are subject to the sl@0: // Boost Software License, Version 1.0. (See accompanying file sl@0: // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) sl@0: sl@0: #ifndef BOOST_MATH_COMPLEX_ATANH_INCLUDED sl@0: #define BOOST_MATH_COMPLEX_ATANH_INCLUDED sl@0: sl@0: #ifndef BOOST_MATH_COMPLEX_DETAILS_INCLUDED sl@0: # include sl@0: #endif sl@0: #ifndef BOOST_MATH_LOG1P_INCLUDED sl@0: # include sl@0: #endif sl@0: #include sl@0: sl@0: #ifdef BOOST_NO_STDC_NAMESPACE sl@0: namespace std{ using ::sqrt; using ::fabs; using ::acos; using ::asin; using ::atan; using ::atan2; } sl@0: #endif sl@0: sl@0: namespace boost{ namespace math{ sl@0: sl@0: template sl@0: std::complex atanh(const std::complex& z) sl@0: { sl@0: // sl@0: // References: sl@0: // sl@0: // Eric W. Weisstein. "Inverse Hyperbolic Tangent." sl@0: // From MathWorld--A Wolfram Web Resource. sl@0: // http://mathworld.wolfram.com/InverseHyperbolicTangent.html sl@0: // sl@0: // Also: The Wolfram Functions Site, sl@0: // http://functions.wolfram.com/ElementaryFunctions/ArcTanh/ sl@0: // sl@0: // Also "Abramowitz and Stegun. Handbook of Mathematical Functions." sl@0: // at : http://jove.prohosting.com/~skripty/toc.htm sl@0: // sl@0: sl@0: static const T half_pi = static_cast(1.57079632679489661923132169163975144L); sl@0: static const T pi = static_cast(3.141592653589793238462643383279502884197L); sl@0: static const T one = static_cast(1.0L); sl@0: static const T two = static_cast(2.0L); sl@0: static const T four = static_cast(4.0L); sl@0: static const T zero = static_cast(0); sl@0: static const T a_crossover = static_cast(0.3L); sl@0: sl@0: T x = std::fabs(z.real()); sl@0: T y = std::fabs(z.imag()); sl@0: sl@0: T real, imag; // our results sl@0: sl@0: T safe_upper = detail::safe_max(two); sl@0: T safe_lower = detail::safe_min(static_cast(2)); sl@0: sl@0: // sl@0: // Begin by handling the special cases specified in C99: sl@0: // sl@0: if(detail::test_is_nan(x)) sl@0: { sl@0: if(detail::test_is_nan(y)) sl@0: return std::complex(x, x); sl@0: else if(std::numeric_limits::has_infinity && (y == std::numeric_limits::infinity())) sl@0: return std::complex(0, ((z.imag() < 0) ? -half_pi : half_pi)); sl@0: else sl@0: return std::complex(x, x); sl@0: } sl@0: else if(detail::test_is_nan(y)) sl@0: { sl@0: if(x == 0) sl@0: return std::complex(x, y); sl@0: if(std::numeric_limits::has_infinity && (x == std::numeric_limits::infinity())) sl@0: return std::complex(0, y); sl@0: else sl@0: return std::complex(y, y); sl@0: } sl@0: else if((x > safe_lower) && (x < safe_upper) && (y > safe_lower) && (y < safe_upper)) sl@0: { sl@0: sl@0: T xx = x*x; sl@0: T yy = y*y; sl@0: T x2 = x * two; sl@0: sl@0: /// sl@0: // The real part is given by: sl@0: // sl@0: // real(atanh(z)) == log((1 + x^2 + y^2 + 2x) / (1 + x^2 + y^2 - 2x)) sl@0: // sl@0: // However, when x is either large (x > 1/E) or very small sl@0: // (x < E) then this effectively simplifies sl@0: // to log(1), leading to wildly inaccurate results. sl@0: // By dividing the above (top and bottom) by (1 + x^2 + y^2) we get: sl@0: // sl@0: // real(atanh(z)) == log((1 + (2x / (1 + x^2 + y^2))) / (1 - (-2x / (1 + x^2 + y^2)))) sl@0: // sl@0: // which is much more sensitive to the value of x, when x is not near 1 sl@0: // (remember we can compute log(1+x) for small x very accurately). sl@0: // sl@0: // The cross-over from one method to the other has to be determined sl@0: // experimentally, the value used below appears correct to within a sl@0: // factor of 2 (and there are larger errors from other parts sl@0: // of the input domain anyway). sl@0: // sl@0: T alpha = two*x / (one + xx + yy); sl@0: if(alpha < a_crossover) sl@0: { sl@0: real = boost::math::log1p(alpha) - boost::math::log1p(-alpha); sl@0: } sl@0: else sl@0: { sl@0: T xm1 = x - one; sl@0: real = boost::math::log1p(x2 + xx + yy) - std::log(xm1*xm1 + yy); sl@0: } sl@0: real /= four; sl@0: if(z.real() < 0) sl@0: real = -real; sl@0: sl@0: imag = std::atan2((y * two), (one - xx - yy)); sl@0: imag /= two; sl@0: if(z.imag() < 0) sl@0: imag = -imag; sl@0: } sl@0: else sl@0: { sl@0: // sl@0: // This section handles exception cases that would normally cause sl@0: // underflow or overflow in the main formulas. sl@0: // sl@0: // Begin by working out the real part, we need to approximate sl@0: // alpha = 2x / (1 + x^2 + y^2) sl@0: // without either overflow or underflow in the squared terms. sl@0: // sl@0: T alpha = 0; sl@0: if(x >= safe_upper) sl@0: { sl@0: // this is really a test for infinity, sl@0: // but we may not have the necessary numeric_limits support: sl@0: if((x > (std::numeric_limits::max)()) || (y > (std::numeric_limits::max)())) sl@0: { sl@0: alpha = 0; sl@0: } sl@0: else if(y >= safe_upper) sl@0: { sl@0: // Big x and y: divide alpha through by x*y: sl@0: alpha = (two/y) / (x/y + y/x); sl@0: } sl@0: else if(y > one) sl@0: { sl@0: // Big x: divide through by x: sl@0: alpha = two / (x + y*y/x); sl@0: } sl@0: else sl@0: { sl@0: // Big x small y, as above but neglect y^2/x: sl@0: alpha = two/x; sl@0: } sl@0: } sl@0: else if(y >= safe_upper) sl@0: { sl@0: if(x > one) sl@0: { sl@0: // Big y, medium x, divide through by y: sl@0: alpha = (two*x/y) / (y + x*x/y); sl@0: } sl@0: else sl@0: { sl@0: // Small x and y, whatever alpha is, it's too small to calculate: sl@0: alpha = 0; sl@0: } sl@0: } sl@0: else sl@0: { sl@0: // one or both of x and y are small, calculate divisor carefully: sl@0: T div = one; sl@0: if(x > safe_lower) sl@0: div += x*x; sl@0: if(y > safe_lower) sl@0: div += y*y; sl@0: alpha = two*x/div; sl@0: } sl@0: if(alpha < a_crossover) sl@0: { sl@0: real = boost::math::log1p(alpha) - boost::math::log1p(-alpha); sl@0: } sl@0: else sl@0: { sl@0: // We can only get here as a result of small y and medium sized x, sl@0: // we can simply neglect the y^2 terms: sl@0: BOOST_ASSERT(x >= safe_lower); sl@0: BOOST_ASSERT(x <= safe_upper); sl@0: //BOOST_ASSERT(y <= safe_lower); sl@0: T xm1 = x - one; sl@0: real = std::log(1 + two*x + x*x) - std::log(xm1*xm1); sl@0: } sl@0: sl@0: real /= four; sl@0: if(z.real() < 0) sl@0: real = -real; sl@0: sl@0: // sl@0: // Now handle imaginary part, this is much easier, sl@0: // if x or y are large, then the formula: sl@0: // atan2(2y, 1 - x^2 - y^2) sl@0: // evaluates to +-(PI - theta) where theta is negligible compared to PI. sl@0: // sl@0: if((x >= safe_upper) || (y >= safe_upper)) sl@0: { sl@0: imag = pi; sl@0: } sl@0: else if(x <= safe_lower) sl@0: { sl@0: // sl@0: // If both x and y are small then atan(2y), sl@0: // otherwise just x^2 is negligible in the divisor: sl@0: // sl@0: if(y <= safe_lower) sl@0: imag = std::atan2(two*y, one); sl@0: else sl@0: { sl@0: if((y == zero) && (x == zero)) sl@0: imag = 0; sl@0: else sl@0: imag = std::atan2(two*y, one - y*y); sl@0: } sl@0: } sl@0: else sl@0: { sl@0: // sl@0: // y^2 is negligible: sl@0: // sl@0: if((y == zero) && (x == one)) sl@0: imag = 0; sl@0: else sl@0: imag = std::atan2(two*y, 1 - x*x); sl@0: } sl@0: imag /= two; sl@0: if(z.imag() < 0) sl@0: imag = -imag; sl@0: } sl@0: return std::complex(real, imag); sl@0: } sl@0: sl@0: } } // namespaces sl@0: sl@0: #endif // BOOST_MATH_COMPLEX_ATANH_INCLUDED