exp.mx raw

   1  // Copyright 2010 The Go Authors. All rights reserved.
   2  // Use of this source code is governed by a BSD-style
   3  // license that can be found in the LICENSE file.
   4  
   5  package cmplx
   6  
   7  import "math"
   8  
   9  // The original C code, the long comment, and the constants
  10  // below are from http://netlib.sandia.gov/cephes/c9x-complex/clog.c.
  11  // The go code is a simplified version of the original C.
  12  //
  13  // Cephes Math Library Release 2.8:  June, 2000
  14  // Copyright 1984, 1987, 1989, 1992, 2000 by Stephen L. Moshier
  15  //
  16  // The readme file at http://netlib.sandia.gov/cephes/ says:
  17  //    Some software in this archive may be from the book _Methods and
  18  // Programs for Mathematical Functions_ (Prentice-Hall or Simon & Schuster
  19  // International, 1989) or from the Cephes Mathematical Library, a
  20  // commercial product. In either event, it is copyrighted by the author.
  21  // What you see here may be used freely but it comes with no support or
  22  // guarantee.
  23  //
  24  //   The two known misprints in the book are repaired here in the
  25  // source listings for the gamma function and the incomplete beta
  26  // integral.
  27  //
  28  //   Stephen L. Moshier
  29  //   moshier@na-net.ornl.gov
  30  
  31  // Complex exponential function
  32  //
  33  // DESCRIPTION:
  34  //
  35  // Returns the complex exponential of the complex argument z.
  36  //
  37  // If
  38  //     z = x + iy,
  39  //     r = exp(x),
  40  // then
  41  //     w = r cos y + i r sin y.
  42  //
  43  // ACCURACY:
  44  //
  45  //                      Relative error:
  46  // arithmetic   domain     # trials      peak         rms
  47  //    DEC       -10,+10      8700       3.7e-17     1.1e-17
  48  //    IEEE      -10,+10     30000       3.0e-16     8.7e-17
  49  
  50  // Exp returns e**x, the base-e exponential of x.
  51  func Exp(x complex128) complex128 {
  52  	switch re, im := real(x), imag(x); {
  53  	case math.IsInf(re, 0):
  54  		switch {
  55  		case re > 0 && im == 0:
  56  			return x
  57  		case math.IsInf(im, 0) || math.IsNaN(im):
  58  			if re < 0 {
  59  				return complex(0, math.Copysign(0, im))
  60  			} else {
  61  				return complex(math.Inf(1.0), math.NaN())
  62  			}
  63  		}
  64  	case math.IsNaN(re):
  65  		if im == 0 {
  66  			return complex(math.NaN(), im)
  67  		}
  68  	}
  69  	r := math.Exp(real(x))
  70  	s, c := math.Sincos(imag(x))
  71  	return complex(r*c, r*s)
  72  }
  73