os/ossrv/ossrv_pub/boost_apis/boost/math/complex/acos.hpp
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
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_ACOS_INCLUDED
sl@0
     6
#define BOOST_MATH_COMPLEX_ACOS_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
std::complex<T> acos(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 s_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
sl@0
    53
   T real, imag; // these hold our result
sl@0
    54
sl@0
    55
   // 
sl@0
    56
   // Handle special cases specified by the C99 standard,
sl@0
    57
   // many of these special cases aren't really needed here,
sl@0
    58
   // but doing it this way prevents overflow/underflow arithmetic
sl@0
    59
   // in the main body of the logic, which may trip up some machines:
sl@0
    60
   //
sl@0
    61
   if(std::numeric_limits<T>::has_infinity && (x == std::numeric_limits<T>::infinity()))
sl@0
    62
   {
sl@0
    63
      if(y == std::numeric_limits<T>::infinity())
sl@0
    64
      {
sl@0
    65
         real = quarter_pi;
sl@0
    66
         imag = std::numeric_limits<T>::infinity();
sl@0
    67
      }
sl@0
    68
      else if(detail::test_is_nan(y))
sl@0
    69
      {
sl@0
    70
         return std::complex<T>(y, -std::numeric_limits<T>::infinity());
sl@0
    71
      }
sl@0
    72
      else
sl@0
    73
      {
sl@0
    74
         // y is not infinity or nan:
sl@0
    75
         real = 0;
sl@0
    76
         imag = std::numeric_limits<T>::infinity();
sl@0
    77
      }
sl@0
    78
   }
sl@0
    79
   else if(detail::test_is_nan(x))
sl@0
    80
   {
sl@0
    81
      if(y == std::numeric_limits<T>::infinity())
sl@0
    82
         return std::complex<T>(x, (z.imag() < 0) ? std::numeric_limits<T>::infinity() :  -std::numeric_limits<T>::infinity());
sl@0
    83
      return std::complex<T>(x, x);
sl@0
    84
   }
sl@0
    85
   else if(std::numeric_limits<T>::has_infinity && (y == std::numeric_limits<T>::infinity()))
sl@0
    86
   {
sl@0
    87
      real = half_pi;
sl@0
    88
      imag = std::numeric_limits<T>::infinity();
sl@0
    89
   }
sl@0
    90
   else if(detail::test_is_nan(y))
sl@0
    91
   {
sl@0
    92
      return std::complex<T>((x == 0) ? half_pi : y, y);
sl@0
    93
   }
sl@0
    94
   else
sl@0
    95
   {
sl@0
    96
      //
sl@0
    97
      // What follows is the regular Hull et al code,
sl@0
    98
      // begin with the special case for real numbers:
sl@0
    99
      //
sl@0
   100
      if((y == 0) && (x <= one))
sl@0
   101
         return std::complex<T>((x == 0) ? half_pi : std::acos(z.real()));
sl@0
   102
      //
sl@0
   103
      // Figure out if our input is within the "safe area" identified by Hull et al.
sl@0
   104
      // This would be more efficient with portable floating point exception handling;
sl@0
   105
      // fortunately the quantities M and u identified by Hull et al (figure 3), 
sl@0
   106
      // match with the max and min methods of numeric_limits<T>.
sl@0
   107
      //
sl@0
   108
      T safe_max = detail::safe_max(static_cast<T>(8));
sl@0
   109
      T safe_min = detail::safe_min(static_cast<T>(4));
sl@0
   110
sl@0
   111
      T xp1 = one + x;
sl@0
   112
      T xm1 = x - one;
sl@0
   113
sl@0
   114
      if((x < safe_max) && (x > safe_min) && (y < safe_max) && (y > safe_min))
sl@0
   115
      {
sl@0
   116
         T yy = y * y;
sl@0
   117
         T r = std::sqrt(xp1*xp1 + yy);
sl@0
   118
         T s = std::sqrt(xm1*xm1 + yy);
sl@0
   119
         T a = half * (r + s);
sl@0
   120
         T b = x / a;
sl@0
   121
sl@0
   122
         if(b <= b_crossover)
sl@0
   123
         {
sl@0
   124
            real = std::acos(b);
sl@0
   125
         }
sl@0
   126
         else
sl@0
   127
         {
sl@0
   128
            T apx = a + x;
sl@0
   129
            if(x <= one)
sl@0
   130
            {
sl@0
   131
               real = std::atan(std::sqrt(half * apx * (yy /(r + xp1) + (s-xm1)))/x);
sl@0
   132
            }
sl@0
   133
            else
sl@0
   134
            {
sl@0
   135
               real = std::atan((y * std::sqrt(half * (apx/(r + xp1) + apx/(s+xm1))))/x);
sl@0
   136
            }
sl@0
   137
         }
sl@0
   138
sl@0
   139
         if(a <= a_crossover)
sl@0
   140
         {
sl@0
   141
            T am1;
sl@0
   142
            if(x < one)
sl@0
   143
            {
sl@0
   144
               am1 = half * (yy/(r + xp1) + yy/(s - xm1));
sl@0
   145
            }
sl@0
   146
            else
sl@0
   147
            {
sl@0
   148
               am1 = half * (yy/(r + xp1) + (s + xm1));
sl@0
   149
            }
sl@0
   150
            imag = boost::math::log1p(am1 + std::sqrt(am1 * (a + one)));
sl@0
   151
         }
sl@0
   152
         else
sl@0
   153
         {
sl@0
   154
            imag = std::log(a + std::sqrt(a*a - one));
sl@0
   155
         }
sl@0
   156
      }
sl@0
   157
      else
sl@0
   158
      {
sl@0
   159
         //
sl@0
   160
         // This is the Hull et al exception handling code from Fig 6 of their paper:
sl@0
   161
         //
sl@0
   162
         if(y <= (std::numeric_limits<T>::epsilon() * std::fabs(xm1)))
sl@0
   163
         {
sl@0
   164
            if(x < one)
sl@0
   165
            {
sl@0
   166
               real = std::acos(x);
sl@0
   167
               imag = y / std::sqrt(xp1*(one-x));
sl@0
   168
            }
sl@0
   169
            else
sl@0
   170
            {
sl@0
   171
               real = 0;
sl@0
   172
               if(((std::numeric_limits<T>::max)() / xp1) > xm1)
sl@0
   173
               {
sl@0
   174
                  // xp1 * xm1 won't overflow:
sl@0
   175
                  imag = boost::math::log1p(xm1 + std::sqrt(xp1*xm1));
sl@0
   176
               }
sl@0
   177
               else
sl@0
   178
               {
sl@0
   179
                  imag = log_two + std::log(x);
sl@0
   180
               }
sl@0
   181
            }
sl@0
   182
         }
sl@0
   183
         else if(y <= safe_min)
sl@0
   184
         {
sl@0
   185
            // There is an assumption in Hull et al's analysis that
sl@0
   186
            // if we get here then x == 1.  This is true for all "good"
sl@0
   187
            // machines where :
sl@0
   188
            // 
sl@0
   189
            // E^2 > 8*sqrt(u); with:
sl@0
   190
            //
sl@0
   191
            // E =  std::numeric_limits<T>::epsilon()
sl@0
   192
            // u = (std::numeric_limits<T>::min)()
sl@0
   193
            //
sl@0
   194
            // Hull et al provide alternative code for "bad" machines
sl@0
   195
            // but we have no way to test that here, so for now just assert
sl@0
   196
            // on the assumption:
sl@0
   197
            //
sl@0
   198
            BOOST_ASSERT(x == 1);
sl@0
   199
            real = std::sqrt(y);
sl@0
   200
            imag = std::sqrt(y);
sl@0
   201
         }
sl@0
   202
         else if(std::numeric_limits<T>::epsilon() * y - one >= x)
sl@0
   203
         {
sl@0
   204
            real = half_pi;
sl@0
   205
            imag = log_two + std::log(y);
sl@0
   206
         }
sl@0
   207
         else if(x > one)
sl@0
   208
         {
sl@0
   209
            real = std::atan(y/x);
sl@0
   210
            T xoy = x/y;
sl@0
   211
            imag = log_two + std::log(y) + half * boost::math::log1p(xoy*xoy);
sl@0
   212
         }
sl@0
   213
         else
sl@0
   214
         {
sl@0
   215
            real = half_pi;
sl@0
   216
            T a = std::sqrt(one + y*y);
sl@0
   217
            imag = half * boost::math::log1p(static_cast<T>(2)*y*(y+a));
sl@0
   218
         }
sl@0
   219
      }
sl@0
   220
   }
sl@0
   221
sl@0
   222
   //
sl@0
   223
   // Finish off by working out the sign of the result:
sl@0
   224
   //
sl@0
   225
   if(z.real() < 0)
sl@0
   226
      real = s_pi - real;
sl@0
   227
   if(z.imag() > 0)
sl@0
   228
      imag = -imag;
sl@0
   229
sl@0
   230
   return std::complex<T>(real, imag);
sl@0
   231
}
sl@0
   232
sl@0
   233
} } // namespaces
sl@0
   234
sl@0
   235
#endif // BOOST_MATH_COMPLEX_ACOS_INCLUDED