os/ossrv/ossrv_pub/boost_apis/boost/math/complex/atanh.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
//  Use, modification and distribution are subject to the
sl@0
     3
//  Boost Software License, Version 1.0. (See accompanying file
sl@0
     4
//  LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
sl@0
     5
sl@0
     6
#ifndef BOOST_MATH_COMPLEX_ATANH_INCLUDED
sl@0
     7
#define BOOST_MATH_COMPLEX_ATANH_INCLUDED
sl@0
     8
sl@0
     9
#ifndef BOOST_MATH_COMPLEX_DETAILS_INCLUDED
sl@0
    10
#  include <boost/math/complex/details.hpp>
sl@0
    11
#endif
sl@0
    12
#ifndef BOOST_MATH_LOG1P_INCLUDED
sl@0
    13
#  include <boost/math/special_functions/log1p.hpp>
sl@0
    14
#endif
sl@0
    15
#include <boost/assert.hpp>
sl@0
    16
sl@0
    17
#ifdef BOOST_NO_STDC_NAMESPACE
sl@0
    18
namespace std{ using ::sqrt; using ::fabs; using ::acos; using ::asin; using ::atan; using ::atan2; }
sl@0
    19
#endif
sl@0
    20
sl@0
    21
namespace boost{ namespace math{
sl@0
    22
sl@0
    23
template<class T> 
sl@0
    24
std::complex<T> atanh(const std::complex<T>& z)
sl@0
    25
{
sl@0
    26
   //
sl@0
    27
   // References:
sl@0
    28
   //
sl@0
    29
   // Eric W. Weisstein. "Inverse Hyperbolic Tangent." 
sl@0
    30
   // From MathWorld--A Wolfram Web Resource. 
sl@0
    31
   // http://mathworld.wolfram.com/InverseHyperbolicTangent.html
sl@0
    32
   //
sl@0
    33
   // Also: The Wolfram Functions Site,
sl@0
    34
   // http://functions.wolfram.com/ElementaryFunctions/ArcTanh/
sl@0
    35
   //
sl@0
    36
   // Also "Abramowitz and Stegun. Handbook of Mathematical Functions."
sl@0
    37
   // at : http://jove.prohosting.com/~skripty/toc.htm
sl@0
    38
   //
sl@0
    39
   
sl@0
    40
   static const T half_pi = static_cast<T>(1.57079632679489661923132169163975144L);
sl@0
    41
   static const T pi = static_cast<T>(3.141592653589793238462643383279502884197L);
sl@0
    42
   static const T one = static_cast<T>(1.0L);
sl@0
    43
   static const T two = static_cast<T>(2.0L);
sl@0
    44
   static const T four = static_cast<T>(4.0L);
sl@0
    45
   static const T zero = static_cast<T>(0);
sl@0
    46
   static const T a_crossover = static_cast<T>(0.3L);
sl@0
    47
sl@0
    48
   T x = std::fabs(z.real());
sl@0
    49
   T y = std::fabs(z.imag());
sl@0
    50
sl@0
    51
   T real, imag;  // our results
sl@0
    52
sl@0
    53
   T safe_upper = detail::safe_max(two);
sl@0
    54
   T safe_lower = detail::safe_min(static_cast<T>(2));
sl@0
    55
sl@0
    56
   //
sl@0
    57
   // Begin by handling the special cases specified in C99:
sl@0
    58
   //
sl@0
    59
   if(detail::test_is_nan(x))
sl@0
    60
   {
sl@0
    61
      if(detail::test_is_nan(y))
sl@0
    62
         return std::complex<T>(x, x);
sl@0
    63
      else if(std::numeric_limits<T>::has_infinity && (y == std::numeric_limits<T>::infinity()))
sl@0
    64
         return std::complex<T>(0, ((z.imag() < 0) ? -half_pi : half_pi));
sl@0
    65
      else
sl@0
    66
         return std::complex<T>(x, x);
sl@0
    67
   }
sl@0
    68
   else if(detail::test_is_nan(y))
sl@0
    69
   {
sl@0
    70
      if(x == 0)
sl@0
    71
         return std::complex<T>(x, y);
sl@0
    72
      if(std::numeric_limits<T>::has_infinity && (x == std::numeric_limits<T>::infinity()))
sl@0
    73
         return std::complex<T>(0, y);
sl@0
    74
      else
sl@0
    75
         return std::complex<T>(y, y);
sl@0
    76
   }
sl@0
    77
   else if((x > safe_lower) && (x < safe_upper) && (y > safe_lower) && (y < safe_upper))
sl@0
    78
   {
sl@0
    79
sl@0
    80
      T xx = x*x;
sl@0
    81
      T yy = y*y;
sl@0
    82
      T x2 = x * two;
sl@0
    83
sl@0
    84
      ///
sl@0
    85
      // The real part is given by:
sl@0
    86
      // 
sl@0
    87
      // real(atanh(z)) == log((1 + x^2 + y^2 + 2x) / (1 + x^2 + y^2 - 2x))
sl@0
    88
      // 
sl@0
    89
      // However, when x is either large (x > 1/E) or very small
sl@0
    90
      // (x < E) then this effectively simplifies
sl@0
    91
      // to log(1), leading to wildly inaccurate results.  
sl@0
    92
      // By dividing the above (top and bottom) by (1 + x^2 + y^2) we get:
sl@0
    93
      //
sl@0
    94
      // real(atanh(z)) == log((1 + (2x / (1 + x^2 + y^2))) / (1 - (-2x / (1 + x^2 + y^2))))
sl@0
    95
      //
sl@0
    96
      // which is much more sensitive to the value of x, when x is not near 1
sl@0
    97
      // (remember we can compute log(1+x) for small x very accurately).
sl@0
    98
      //
sl@0
    99
      // The cross-over from one method to the other has to be determined
sl@0
   100
      // experimentally, the value used below appears correct to within a 
sl@0
   101
      // factor of 2 (and there are larger errors from other parts
sl@0
   102
      // of the input domain anyway).
sl@0
   103
      //
sl@0
   104
      T alpha = two*x / (one + xx + yy);
sl@0
   105
      if(alpha < a_crossover)
sl@0
   106
      {
sl@0
   107
         real = boost::math::log1p(alpha) - boost::math::log1p(-alpha);
sl@0
   108
      }
sl@0
   109
      else
sl@0
   110
      {
sl@0
   111
         T xm1 = x - one;
sl@0
   112
         real = boost::math::log1p(x2 + xx + yy) - std::log(xm1*xm1 + yy);
sl@0
   113
      }
sl@0
   114
      real /= four;
sl@0
   115
      if(z.real() < 0)
sl@0
   116
         real = -real;
sl@0
   117
sl@0
   118
      imag = std::atan2((y * two), (one - xx - yy));
sl@0
   119
      imag /= two;
sl@0
   120
      if(z.imag() < 0)
sl@0
   121
         imag = -imag;
sl@0
   122
   }
sl@0
   123
   else
sl@0
   124
   {
sl@0
   125
      //
sl@0
   126
      // This section handles exception cases that would normally cause
sl@0
   127
      // underflow or overflow in the main formulas.
sl@0
   128
      //
sl@0
   129
      // Begin by working out the real part, we need to approximate
sl@0
   130
      //    alpha = 2x / (1 + x^2 + y^2)
sl@0
   131
      // without either overflow or underflow in the squared terms.
sl@0
   132
      //
sl@0
   133
      T alpha = 0;
sl@0
   134
      if(x >= safe_upper)
sl@0
   135
      {
sl@0
   136
         // this is really a test for infinity, 
sl@0
   137
         // but we may not have the necessary numeric_limits support:
sl@0
   138
         if((x > (std::numeric_limits<T>::max)()) || (y > (std::numeric_limits<T>::max)()))
sl@0
   139
         {
sl@0
   140
            alpha = 0;
sl@0
   141
         }
sl@0
   142
         else if(y >= safe_upper)
sl@0
   143
         {
sl@0
   144
            // Big x and y: divide alpha through by x*y:
sl@0
   145
            alpha = (two/y) / (x/y + y/x);
sl@0
   146
         }
sl@0
   147
         else if(y > one)
sl@0
   148
         {
sl@0
   149
            // Big x: divide through by x:
sl@0
   150
            alpha = two / (x + y*y/x);
sl@0
   151
         }
sl@0
   152
         else
sl@0
   153
         {
sl@0
   154
            // Big x small y, as above but neglect y^2/x:
sl@0
   155
            alpha = two/x;
sl@0
   156
         }
sl@0
   157
      }
sl@0
   158
      else if(y >= safe_upper)
sl@0
   159
      {
sl@0
   160
         if(x > one)
sl@0
   161
         {
sl@0
   162
            // Big y, medium x, divide through by y:
sl@0
   163
            alpha = (two*x/y) / (y + x*x/y);
sl@0
   164
         }
sl@0
   165
         else
sl@0
   166
         {
sl@0
   167
            // Small x and y, whatever alpha is, it's too small to calculate:
sl@0
   168
            alpha = 0;
sl@0
   169
         }
sl@0
   170
      }
sl@0
   171
      else
sl@0
   172
      {
sl@0
   173
         // one or both of x and y are small, calculate divisor carefully:
sl@0
   174
         T div = one;
sl@0
   175
         if(x > safe_lower)
sl@0
   176
            div += x*x;
sl@0
   177
         if(y > safe_lower)
sl@0
   178
            div += y*y;
sl@0
   179
         alpha = two*x/div;
sl@0
   180
      }
sl@0
   181
      if(alpha < a_crossover)
sl@0
   182
      {
sl@0
   183
         real = boost::math::log1p(alpha) - boost::math::log1p(-alpha);
sl@0
   184
      }
sl@0
   185
      else
sl@0
   186
      {
sl@0
   187
         // We can only get here as a result of small y and medium sized x,
sl@0
   188
         // we can simply neglect the y^2 terms:
sl@0
   189
         BOOST_ASSERT(x >= safe_lower);
sl@0
   190
         BOOST_ASSERT(x <= safe_upper);
sl@0
   191
         //BOOST_ASSERT(y <= safe_lower);
sl@0
   192
         T xm1 = x - one;
sl@0
   193
         real = std::log(1 + two*x + x*x) - std::log(xm1*xm1);
sl@0
   194
      }
sl@0
   195
      
sl@0
   196
      real /= four;
sl@0
   197
      if(z.real() < 0)
sl@0
   198
         real = -real;
sl@0
   199
sl@0
   200
      //
sl@0
   201
      // Now handle imaginary part, this is much easier,
sl@0
   202
      // if x or y are large, then the formula:
sl@0
   203
      //    atan2(2y, 1 - x^2 - y^2)
sl@0
   204
      // evaluates to +-(PI - theta) where theta is negligible compared to PI.
sl@0
   205
      //
sl@0
   206
      if((x >= safe_upper) || (y >= safe_upper))
sl@0
   207
      {
sl@0
   208
         imag = pi;
sl@0
   209
      }
sl@0
   210
      else if(x <= safe_lower)
sl@0
   211
      {
sl@0
   212
         //
sl@0
   213
         // If both x and y are small then atan(2y),
sl@0
   214
         // otherwise just x^2 is negligible in the divisor:
sl@0
   215
         //
sl@0
   216
         if(y <= safe_lower)
sl@0
   217
            imag = std::atan2(two*y, one);
sl@0
   218
         else
sl@0
   219
         {
sl@0
   220
            if((y == zero) && (x == zero))
sl@0
   221
               imag = 0;
sl@0
   222
            else
sl@0
   223
               imag = std::atan2(two*y, one - y*y);
sl@0
   224
         }
sl@0
   225
      }
sl@0
   226
      else
sl@0
   227
      {
sl@0
   228
         //
sl@0
   229
         // y^2 is negligible:
sl@0
   230
         //
sl@0
   231
         if((y == zero) && (x == one))
sl@0
   232
            imag = 0;
sl@0
   233
         else
sl@0
   234
            imag = std::atan2(two*y, 1 - x*x);
sl@0
   235
      }
sl@0
   236
      imag /= two;
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_ATANH_INCLUDED