Chris@16: // (C) Copyright John Maddock 2005. Chris@16: // Distributed under the Boost Software License, Version 1.0. (See accompanying Chris@16: // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) Chris@16: Chris@16: #ifndef BOOST_MATH_COMPLEX_ASIN_INCLUDED Chris@16: #define BOOST_MATH_COMPLEX_ASIN_INCLUDED Chris@16: Chris@16: #ifndef BOOST_MATH_COMPLEX_DETAILS_INCLUDED Chris@16: # include Chris@16: #endif Chris@16: #ifndef BOOST_MATH_LOG1P_INCLUDED Chris@16: # include Chris@16: #endif Chris@16: #include Chris@16: Chris@16: #ifdef BOOST_NO_STDC_NAMESPACE Chris@16: namespace std{ using ::sqrt; using ::fabs; using ::acos; using ::asin; using ::atan; using ::atan2; } Chris@16: #endif Chris@16: Chris@16: namespace boost{ namespace math{ Chris@16: Chris@16: template Chris@16: inline std::complex asin(const std::complex& z) Chris@16: { Chris@16: // Chris@16: // This implementation is a transcription of the pseudo-code in: Chris@16: // Chris@16: // "Implementing the complex Arcsine and Arccosine Functions using Exception Handling." Chris@16: // T E Hull, Thomas F Fairgrieve and Ping Tak Peter Tang. Chris@16: // ACM Transactions on Mathematical Software, Vol 23, No 3, Sept 1997. Chris@16: // Chris@16: Chris@16: // Chris@16: // These static constants should really be in a maths constants library, Chris@16: // note that we have tweaked the value of a_crossover as per https://svn.boost.org/trac/boost/ticket/7290: Chris@16: // Chris@16: static const T one = static_cast(1); Chris@16: //static const T two = static_cast(2); Chris@16: static const T half = static_cast(0.5L); Chris@16: static const T a_crossover = static_cast(10); Chris@16: static const T b_crossover = static_cast(0.6417L); Chris@16: static const T s_pi = boost::math::constants::pi(); Chris@16: static const T half_pi = s_pi / 2; Chris@16: static const T log_two = boost::math::constants::ln_two(); Chris@16: static const T quarter_pi = s_pi / 4; Chris@16: #ifdef BOOST_MSVC Chris@16: #pragma warning(push) Chris@16: #pragma warning(disable:4127) Chris@16: #endif Chris@16: // Chris@16: // Get real and imaginary parts, discard the signs as we can Chris@16: // figure out the sign of the result later: Chris@16: // Chris@16: T x = std::fabs(z.real()); Chris@16: T y = std::fabs(z.imag()); Chris@16: T real, imag; // our results Chris@16: Chris@16: // Chris@16: // Begin by handling the special cases for infinities and nan's Chris@16: // specified in C99, most of this is handled by the regular logic Chris@16: // below, but handling it as a special case prevents overflow/underflow Chris@16: // arithmetic which may trip up some machines: Chris@16: // Chris@16: if((boost::math::isnan)(x)) Chris@16: { Chris@16: if((boost::math::isnan)(y)) Chris@16: return std::complex(x, x); Chris@16: if((boost::math::isinf)(y)) Chris@16: { Chris@16: real = x; Chris@16: imag = std::numeric_limits::infinity(); Chris@16: } Chris@16: else Chris@16: return std::complex(x, x); Chris@16: } Chris@16: else if((boost::math::isnan)(y)) Chris@16: { Chris@16: if(x == 0) Chris@16: { Chris@16: real = 0; Chris@16: imag = y; Chris@16: } Chris@16: else if((boost::math::isinf)(x)) Chris@16: { Chris@16: real = y; Chris@16: imag = std::numeric_limits::infinity(); Chris@16: } Chris@16: else Chris@16: return std::complex(y, y); Chris@16: } Chris@16: else if((boost::math::isinf)(x)) Chris@16: { Chris@16: if((boost::math::isinf)(y)) Chris@16: { Chris@16: real = quarter_pi; Chris@16: imag = std::numeric_limits::infinity(); Chris@16: } Chris@16: else Chris@16: { Chris@16: real = half_pi; Chris@16: imag = std::numeric_limits::infinity(); Chris@16: } Chris@16: } Chris@16: else if((boost::math::isinf)(y)) Chris@16: { Chris@16: real = 0; Chris@16: imag = std::numeric_limits::infinity(); Chris@16: } Chris@16: else Chris@16: { Chris@16: // Chris@16: // special case for real numbers: Chris@16: // Chris@16: if((y == 0) && (x <= one)) Chris@16: return std::complex(std::asin(z.real()), z.imag()); Chris@16: // Chris@16: // Figure out if our input is within the "safe area" identified by Hull et al. Chris@16: // This would be more efficient with portable floating point exception handling; Chris@16: // fortunately the quantities M and u identified by Hull et al (figure 3), Chris@16: // match with the max and min methods of numeric_limits. Chris@16: // Chris@16: T safe_max = detail::safe_max(static_cast(8)); Chris@16: T safe_min = detail::safe_min(static_cast(4)); Chris@16: Chris@16: T xp1 = one + x; Chris@16: T xm1 = x - one; Chris@16: Chris@16: if((x < safe_max) && (x > safe_min) && (y < safe_max) && (y > safe_min)) Chris@16: { Chris@16: T yy = y * y; Chris@16: T r = std::sqrt(xp1*xp1 + yy); Chris@16: T s = std::sqrt(xm1*xm1 + yy); Chris@16: T a = half * (r + s); Chris@16: T b = x / a; Chris@16: Chris@16: if(b <= b_crossover) Chris@16: { Chris@16: real = std::asin(b); Chris@16: } Chris@16: else Chris@16: { Chris@16: T apx = a + x; Chris@16: if(x <= one) Chris@16: { Chris@16: real = std::atan(x/std::sqrt(half * apx * (yy /(r + xp1) + (s-xm1)))); Chris@16: } Chris@16: else Chris@16: { Chris@16: real = std::atan(x/(y * std::sqrt(half * (apx/(r + xp1) + apx/(s+xm1))))); Chris@16: } Chris@16: } Chris@16: Chris@16: if(a <= a_crossover) Chris@16: { Chris@16: T am1; Chris@16: if(x < one) Chris@16: { Chris@16: am1 = half * (yy/(r + xp1) + yy/(s - xm1)); Chris@16: } Chris@16: else Chris@16: { Chris@16: am1 = half * (yy/(r + xp1) + (s + xm1)); Chris@16: } Chris@16: imag = boost::math::log1p(am1 + std::sqrt(am1 * (a + one))); Chris@16: } Chris@16: else Chris@16: { Chris@16: imag = std::log(a + std::sqrt(a*a - one)); Chris@16: } Chris@16: } Chris@16: else Chris@16: { Chris@16: // Chris@16: // This is the Hull et al exception handling code from Fig 3 of their paper: Chris@16: // Chris@16: if(y <= (std::numeric_limits::epsilon() * std::fabs(xm1))) Chris@16: { Chris@16: if(x < one) Chris@16: { Chris@16: real = std::asin(x); Chris@16: imag = y / std::sqrt(-xp1*xm1); Chris@16: } Chris@16: else Chris@16: { Chris@16: real = half_pi; Chris@16: if(((std::numeric_limits::max)() / xp1) > xm1) Chris@16: { Chris@16: // xp1 * xm1 won't overflow: Chris@16: imag = boost::math::log1p(xm1 + std::sqrt(xp1*xm1)); Chris@16: } Chris@16: else Chris@16: { Chris@16: imag = log_two + std::log(x); Chris@16: } Chris@16: } Chris@16: } Chris@16: else if(y <= safe_min) Chris@16: { Chris@16: // There is an assumption in Hull et al's analysis that Chris@16: // if we get here then x == 1. This is true for all "good" Chris@16: // machines where : Chris@16: // Chris@16: // E^2 > 8*sqrt(u); with: Chris@16: // Chris@16: // E = std::numeric_limits::epsilon() Chris@16: // u = (std::numeric_limits::min)() Chris@16: // Chris@16: // Hull et al provide alternative code for "bad" machines Chris@16: // but we have no way to test that here, so for now just assert Chris@16: // on the assumption: Chris@16: // Chris@16: BOOST_ASSERT(x == 1); Chris@16: real = half_pi - std::sqrt(y); Chris@16: imag = std::sqrt(y); Chris@16: } Chris@16: else if(std::numeric_limits::epsilon() * y - one >= x) Chris@16: { Chris@16: real = x/y; // This can underflow! Chris@16: imag = log_two + std::log(y); Chris@16: } Chris@16: else if(x > one) Chris@16: { Chris@16: real = std::atan(x/y); Chris@16: T xoy = x/y; Chris@16: imag = log_two + std::log(y) + half * boost::math::log1p(xoy*xoy); Chris@16: } Chris@16: else Chris@16: { Chris@16: T a = std::sqrt(one + y*y); Chris@16: real = x/a; // This can underflow! Chris@16: imag = half * boost::math::log1p(static_cast(2)*y*(y+a)); Chris@16: } Chris@16: } Chris@16: } Chris@16: Chris@16: // Chris@16: // Finish off by working out the sign of the result: Chris@16: // Chris@16: if((boost::math::signbit)(z.real())) Chris@16: real = (boost::math::changesign)(real); Chris@16: if((boost::math::signbit)(z.imag())) Chris@16: imag = (boost::math::changesign)(imag); Chris@16: Chris@16: return std::complex(real, imag); Chris@16: #ifdef BOOST_MSVC Chris@16: #pragma warning(pop) Chris@16: #endif Chris@16: } Chris@16: Chris@16: } } // namespaces Chris@16: Chris@16: #endif // BOOST_MATH_COMPLEX_ASIN_INCLUDED