Black Lives Matter. Support the Equal Justice Initiative.

Source file src/math/cmplx/pow.go

Documentation: math/cmplx

     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 power function
    32  //
    33  // DESCRIPTION:
    34  //
    35  // Raises complex A to the complex Zth power.
    36  // Definition is per AMS55 # 4.2.8,
    37  // analytically equivalent to cpow(a,z) = cexp(z clog(a)).
    38  //
    39  // ACCURACY:
    40  //
    41  //                      Relative error:
    42  // arithmetic   domain     # trials      peak         rms
    43  //    IEEE      -10,+10     30000       9.4e-15     1.5e-15
    44  
    45  // Pow returns x**y, the base-x exponential of y.
    46  // For generalized compatibility with math.Pow:
    47  //	Pow(0, ±0) returns 1+0i
    48  //	Pow(0, c) for real(c)<0 returns Inf+0i if imag(c) is zero, otherwise Inf+Inf i.
    49  func Pow(x, y complex128) complex128 {
    50  	if x == 0 { // Guaranteed also true for x == -0.
    51  		if IsNaN(y) {
    52  			return NaN()
    53  		}
    54  		r, i := real(y), imag(y)
    55  		switch {
    56  		case r == 0:
    57  			return 1
    58  		case r < 0:
    59  			if i == 0 {
    60  				return complex(math.Inf(1), 0)
    61  			}
    62  			return Inf()
    63  		case r > 0:
    64  			return 0
    65  		}
    66  		panic("not reached")
    67  	}
    68  	modulus := Abs(x)
    69  	if modulus == 0 {
    70  		return complex(0, 0)
    71  	}
    72  	r := math.Pow(modulus, real(y))
    73  	arg := Phase(x)
    74  	theta := real(y) * arg
    75  	if imag(y) != 0 {
    76  		r *= math.Exp(-imag(y) * arg)
    77  		theta += imag(y) * math.Log(modulus)
    78  	}
    79  	s, c := math.Sincos(theta)
    80  	return complex(r*c, r*s)
    81  }
    82  

View as plain text