os/ossrv/ossrv_pub/boost_apis/boost/math/complex/asin.hpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
sl@0
     1
//  (C) Copyright John Maddock 2005.
sl@0
     2
//  Distributed under the Boost Software License, Version 1.0. (See accompanying
sl@0
     3
//  file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
sl@0
     4
sl@0
     5
#ifndef BOOST_MATH_COMPLEX_ASIN_INCLUDED
sl@0
     6
#define BOOST_MATH_COMPLEX_ASIN_INCLUDED
sl@0
     7
sl@0
     8
#ifndef BOOST_MATH_COMPLEX_DETAILS_INCLUDED
sl@0
     9
#  include <boost/math/complex/details.hpp>
sl@0
    10
#endif
sl@0
    11
#ifndef BOOST_MATH_LOG1P_INCLUDED
sl@0
    12
#  include <boost/math/special_functions/log1p.hpp>
sl@0
    13
#endif
sl@0
    14
#include <boost/assert.hpp>
sl@0
    15
sl@0
    16
#ifdef BOOST_NO_STDC_NAMESPACE
sl@0
    17
namespace std{ using ::sqrt; using ::fabs; using ::acos; using ::asin; using ::atan; using ::atan2; }
sl@0
    18
#endif
sl@0
    19
sl@0
    20
namespace boost{ namespace math{
sl@0
    21
sl@0
    22
template<class T> 
sl@0
    23
inline std::complex<T> asin(const std::complex<T>& z)
sl@0
    24
{
sl@0
    25
   //
sl@0
    26
   // This implementation is a transcription of the pseudo-code in:
sl@0
    27
   //
sl@0
    28
   // "Implementing the complex Arcsine and Arccosine Functions using Exception Handling."
sl@0
    29
   // T E Hull, Thomas F Fairgrieve and Ping Tak Peter Tang.
sl@0
    30
   // ACM Transactions on Mathematical Software, Vol 23, No 3, Sept 1997.
sl@0
    31
   //
sl@0
    32
sl@0
    33
   //
sl@0
    34
   // These static constants should really be in a maths constants library:
sl@0
    35
   //
sl@0
    36
   static const T one = static_cast<T>(1);
sl@0
    37
   //static const T two = static_cast<T>(2);
sl@0
    38
   static const T half = static_cast<T>(0.5L);
sl@0
    39
   static const T a_crossover = static_cast<T>(1.5L);
sl@0
    40
   static const T b_crossover = static_cast<T>(0.6417L);
sl@0
    41
   //static const T pi = static_cast<T>(3.141592653589793238462643383279502884197L);
sl@0
    42
   static const T half_pi = static_cast<T>(1.57079632679489661923132169163975144L);
sl@0
    43
   static const T log_two = static_cast<T>(0.69314718055994530941723212145817657L);
sl@0
    44
   static const T quarter_pi = static_cast<T>(0.78539816339744830961566084581987572L);
sl@0
    45
   
sl@0
    46
   //
sl@0
    47
   // Get real and imaginary parts, discard the signs as we can 
sl@0
    48
   // figure out the sign of the result later:
sl@0
    49
   //
sl@0
    50
   T x = std::fabs(z.real());
sl@0
    51
   T y = std::fabs(z.imag());
sl@0
    52
   T real, imag;  // our results
sl@0
    53
sl@0
    54
   //
sl@0
    55
   // Begin by handling the special cases for infinities and nan's
sl@0
    56
   // specified in C99, most of this is handled by the regular logic
sl@0
    57
   // below, but handling it as a special case prevents overflow/underflow
sl@0
    58
   // arithmetic which may trip up some machines:
sl@0
    59
   //
sl@0
    60
   if(detail::test_is_nan(x))
sl@0
    61
   {
sl@0
    62
      if(detail::test_is_nan(y))
sl@0
    63
         return std::complex<T>(x, x);
sl@0
    64
      if(std::numeric_limits<T>::has_infinity && (y == std::numeric_limits<T>::infinity()))
sl@0
    65
      {
sl@0
    66
         real = x;
sl@0
    67
         imag = std::numeric_limits<T>::infinity();
sl@0
    68
      }
sl@0
    69
      else
sl@0
    70
         return std::complex<T>(x, x);
sl@0
    71
   }
sl@0
    72
   else if(detail::test_is_nan(y))
sl@0
    73
   {
sl@0
    74
      if(x == 0)
sl@0
    75
      {
sl@0
    76
         real = 0;
sl@0
    77
         imag = y;
sl@0
    78
      }
sl@0
    79
      else if(std::numeric_limits<T>::has_infinity && (x == std::numeric_limits<T>::infinity()))
sl@0
    80
      {
sl@0
    81
         real = y;
sl@0
    82
         imag = std::numeric_limits<T>::infinity();
sl@0
    83
      }
sl@0
    84
      else
sl@0
    85
         return std::complex<T>(y, y);
sl@0
    86
   }
sl@0
    87
   else if(std::numeric_limits<T>::has_infinity && (x == std::numeric_limits<T>::infinity()))
sl@0
    88
   {
sl@0
    89
      if(y == std::numeric_limits<T>::infinity())
sl@0
    90
      {
sl@0
    91
         real = quarter_pi;
sl@0
    92
         imag = std::numeric_limits<T>::infinity();
sl@0
    93
      }
sl@0
    94
      else
sl@0
    95
      {
sl@0
    96
         real = half_pi;
sl@0
    97
         imag = std::numeric_limits<T>::infinity();
sl@0
    98
      }
sl@0
    99
   }
sl@0
   100
   else if(std::numeric_limits<T>::has_infinity && (y == std::numeric_limits<T>::infinity()))
sl@0
   101
   {
sl@0
   102
      real = 0;
sl@0
   103
      imag = std::numeric_limits<T>::infinity();
sl@0
   104
   }
sl@0
   105
   else
sl@0
   106
   {
sl@0
   107
      //
sl@0
   108
      // special case for real numbers:
sl@0
   109
      //
sl@0
   110
      if((y == 0) && (x <= one))
sl@0
   111
         return std::complex<T>(std::asin(z.real()));
sl@0
   112
      //
sl@0
   113
      // Figure out if our input is within the "safe area" identified by Hull et al.
sl@0
   114
      // This would be more efficient with portable floating point exception handling;
sl@0
   115
      // fortunately the quantities M and u identified by Hull et al (figure 3), 
sl@0
   116
      // match with the max and min methods of numeric_limits<T>.
sl@0
   117
      //
sl@0
   118
      T safe_max = detail::safe_max(static_cast<T>(8));
sl@0
   119
      T safe_min = detail::safe_min(static_cast<T>(4));
sl@0
   120
sl@0
   121
      T xp1 = one + x;
sl@0
   122
      T xm1 = x - one;
sl@0
   123
sl@0
   124
      if((x < safe_max) && (x > safe_min) && (y < safe_max) && (y > safe_min))
sl@0
   125
      {
sl@0
   126
         T yy = y * y;
sl@0
   127
         T r = std::sqrt(xp1*xp1 + yy);
sl@0
   128
         T s = std::sqrt(xm1*xm1 + yy);
sl@0
   129
         T a = half * (r + s);
sl@0
   130
         T b = x / a;
sl@0
   131
sl@0
   132
         if(b <= b_crossover)
sl@0
   133
         {
sl@0
   134
            real = std::asin(b);
sl@0
   135
         }
sl@0
   136
         else
sl@0
   137
         {
sl@0
   138
            T apx = a + x;
sl@0
   139
            if(x <= one)
sl@0
   140
            {
sl@0
   141
               real = std::atan(x/std::sqrt(half * apx * (yy /(r + xp1) + (s-xm1))));
sl@0
   142
            }
sl@0
   143
            else
sl@0
   144
            {
sl@0
   145
               real = std::atan(x/(y * std::sqrt(half * (apx/(r + xp1) + apx/(s+xm1)))));
sl@0
   146
            }
sl@0
   147
         }
sl@0
   148
sl@0
   149
         if(a <= a_crossover)
sl@0
   150
         {
sl@0
   151
            T am1;
sl@0
   152
            if(x < one)
sl@0
   153
            {
sl@0
   154
               am1 = half * (yy/(r + xp1) + yy/(s - xm1));
sl@0
   155
            }
sl@0
   156
            else
sl@0
   157
            {
sl@0
   158
               am1 = half * (yy/(r + xp1) + (s + xm1));
sl@0
   159
            }
sl@0
   160
            imag = boost::math::log1p(am1 + std::sqrt(am1 * (a + one)));
sl@0
   161
         }
sl@0
   162
         else
sl@0
   163
         {
sl@0
   164
            imag = std::log(a + std::sqrt(a*a - one));
sl@0
   165
         }
sl@0
   166
      }
sl@0
   167
      else
sl@0
   168
      {
sl@0
   169
         //
sl@0
   170
         // This is the Hull et al exception handling code from Fig 3 of their paper:
sl@0
   171
         //
sl@0
   172
         if(y <= (std::numeric_limits<T>::epsilon() * std::fabs(xm1)))
sl@0
   173
         {
sl@0
   174
            if(x < one)
sl@0
   175
            {
sl@0
   176
               real = std::asin(x);
sl@0
   177
               imag = y / std::sqrt(xp1*xm1);
sl@0
   178
            }
sl@0
   179
            else
sl@0
   180
            {
sl@0
   181
               real = half_pi;
sl@0
   182
               if(((std::numeric_limits<T>::max)() / xp1) > xm1)
sl@0
   183
               {
sl@0
   184
                  // xp1 * xm1 won't overflow:
sl@0
   185
                  imag = boost::math::log1p(xm1 + std::sqrt(xp1*xm1));
sl@0
   186
               }
sl@0
   187
               else
sl@0
   188
               {
sl@0
   189
                  imag = log_two + std::log(x);
sl@0
   190
               }
sl@0
   191
            }
sl@0
   192
         }
sl@0
   193
         else if(y <= safe_min)
sl@0
   194
         {
sl@0
   195
            // There is an assumption in Hull et al's analysis that
sl@0
   196
            // if we get here then x == 1.  This is true for all "good"
sl@0
   197
            // machines where :
sl@0
   198
            // 
sl@0
   199
            // E^2 > 8*sqrt(u); with:
sl@0
   200
            //
sl@0
   201
            // E =  std::numeric_limits<T>::epsilon()
sl@0
   202
            // u = (std::numeric_limits<T>::min)()
sl@0
   203
            //
sl@0
   204
            // Hull et al provide alternative code for "bad" machines
sl@0
   205
            // but we have no way to test that here, so for now just assert
sl@0
   206
            // on the assumption:
sl@0
   207
            //
sl@0
   208
            BOOST_ASSERT(x == 1);
sl@0
   209
            real = half_pi - std::sqrt(y);
sl@0
   210
            imag = std::sqrt(y);
sl@0
   211
         }
sl@0
   212
         else if(std::numeric_limits<T>::epsilon() * y - one >= x)
sl@0
   213
         {
sl@0
   214
            real = x/y; // This can underflow!
sl@0
   215
            imag = log_two + std::log(y);
sl@0
   216
         }
sl@0
   217
         else if(x > one)
sl@0
   218
         {
sl@0
   219
            real = std::atan(x/y);
sl@0
   220
            T xoy = x/y;
sl@0
   221
            imag = log_two + std::log(y) + half * boost::math::log1p(xoy*xoy);
sl@0
   222
         }
sl@0
   223
         else
sl@0
   224
         {
sl@0
   225
            T a = std::sqrt(one + y*y);
sl@0
   226
            real = x/a; // This can underflow!
sl@0
   227
            imag = half * boost::math::log1p(static_cast<T>(2)*y*(y+a));
sl@0
   228
         }
sl@0
   229
      }
sl@0
   230
   }
sl@0
   231
sl@0
   232
   //
sl@0
   233
   // Finish off by working out the sign of the result:
sl@0
   234
   //
sl@0
   235
   if(z.real() < 0)
sl@0
   236
      real = -real;
sl@0
   237
   if(z.imag() < 0)
sl@0
   238
      imag = -imag;
sl@0
   239
sl@0
   240
   return std::complex<T>(real, imag);
sl@0
   241
}
sl@0
   242
sl@0
   243
} } // namespaces
sl@0
   244
sl@0
   245
#endif // BOOST_MATH_COMPLEX_ASIN_INCLUDED