Home | History | Annotate | Download | only in 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 (a] na-net.ornl.gov
     30 
     31 // Complex circular sine
     32 //
     33 // DESCRIPTION:
     34 //
     35 // If
     36 //     z = x + iy,
     37 //
     38 // then
     39 //
     40 //     w = sin x  cosh y  +  i cos x sinh y.
     41 //
     42 // csin(z) = -i csinh(iz).
     43 //
     44 // ACCURACY:
     45 //
     46 //                      Relative error:
     47 // arithmetic   domain     # trials      peak         rms
     48 //    DEC       -10,+10      8400       5.3e-17     1.3e-17
     49 //    IEEE      -10,+10     30000       3.8e-16     1.0e-16
     50 // Also tested by csin(casin(z)) = z.
     51 
     52 // Sin returns the sine of x.
     53 func Sin(x complex128) complex128 {
     54 	s, c := math.Sincos(real(x))
     55 	sh, ch := sinhcosh(imag(x))
     56 	return complex(s*ch, c*sh)
     57 }
     58 
     59 // Complex hyperbolic sine
     60 //
     61 // DESCRIPTION:
     62 //
     63 // csinh z = (cexp(z) - cexp(-z))/2
     64 //         = sinh x * cos y  +  i cosh x * sin y .
     65 //
     66 // ACCURACY:
     67 //
     68 //                      Relative error:
     69 // arithmetic   domain     # trials      peak         rms
     70 //    IEEE      -10,+10     30000       3.1e-16     8.2e-17
     71 
     72 // Sinh returns the hyperbolic sine of x.
     73 func Sinh(x complex128) complex128 {
     74 	s, c := math.Sincos(imag(x))
     75 	sh, ch := sinhcosh(real(x))
     76 	return complex(c*sh, s*ch)
     77 }
     78 
     79 // Complex circular cosine
     80 //
     81 // DESCRIPTION:
     82 //
     83 // If
     84 //     z = x + iy,
     85 //
     86 // then
     87 //
     88 //     w = cos x  cosh y  -  i sin x sinh y.
     89 //
     90 // ACCURACY:
     91 //
     92 //                      Relative error:
     93 // arithmetic   domain     # trials      peak         rms
     94 //    DEC       -10,+10      8400       4.5e-17     1.3e-17
     95 //    IEEE      -10,+10     30000       3.8e-16     1.0e-16
     96 
     97 // Cos returns the cosine of x.
     98 func Cos(x complex128) complex128 {
     99 	s, c := math.Sincos(real(x))
    100 	sh, ch := sinhcosh(imag(x))
    101 	return complex(c*ch, -s*sh)
    102 }
    103 
    104 // Complex hyperbolic cosine
    105 //
    106 // DESCRIPTION:
    107 //
    108 // ccosh(z) = cosh x  cos y + i sinh x sin y .
    109 //
    110 // ACCURACY:
    111 //
    112 //                      Relative error:
    113 // arithmetic   domain     # trials      peak         rms
    114 //    IEEE      -10,+10     30000       2.9e-16     8.1e-17
    115 
    116 // Cosh returns the hyperbolic cosine of x.
    117 func Cosh(x complex128) complex128 {
    118 	s, c := math.Sincos(imag(x))
    119 	sh, ch := sinhcosh(real(x))
    120 	return complex(c*ch, s*sh)
    121 }
    122 
    123 // calculate sinh and cosh
    124 func sinhcosh(x float64) (sh, ch float64) {
    125 	if math.Abs(x) <= 0.5 {
    126 		return math.Sinh(x), math.Cosh(x)
    127 	}
    128 	e := math.Exp(x)
    129 	ei := 0.5 / e
    130 	e *= 0.5
    131 	return e - ei, e + ei
    132 }
    133