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