tan.go 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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. package cmplx
  5. import (
  6. "math"
  7. "math/bits"
  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. // Complex circular tangent
  31. //
  32. // DESCRIPTION:
  33. //
  34. // If
  35. // z = x + iy,
  36. //
  37. // then
  38. //
  39. // sin 2x + i sinh 2y
  40. // w = --------------------.
  41. // cos 2x + cosh 2y
  42. //
  43. // On the real axis the denominator is zero at odd multiples
  44. // of PI/2. The denominator is evaluated by its Taylor
  45. // series near these points.
  46. //
  47. // ctan(z) = -i ctanh(iz).
  48. //
  49. // ACCURACY:
  50. //
  51. // Relative error:
  52. // arithmetic domain # trials peak rms
  53. // DEC -10,+10 5200 7.1e-17 1.6e-17
  54. // IEEE -10,+10 30000 7.2e-16 1.2e-16
  55. // Also tested by ctan * ccot = 1 and catan(ctan(z)) = z.
  56. // Tan returns the tangent of x.
  57. func Tan(x complex128) complex128 {
  58. switch re, im := real(x), imag(x); {
  59. case math.IsInf(im, 0):
  60. switch {
  61. case math.IsInf(re, 0) || math.IsNaN(re):
  62. return complex(math.Copysign(0, re), math.Copysign(1, im))
  63. }
  64. return complex(math.Copysign(0, math.Sin(2*re)), math.Copysign(1, im))
  65. case re == 0 && math.IsNaN(im):
  66. return x
  67. }
  68. d := math.Cos(2*real(x)) + math.Cosh(2*imag(x))
  69. if math.Abs(d) < 0.25 {
  70. d = tanSeries(x)
  71. }
  72. if d == 0 {
  73. return Inf()
  74. }
  75. return complex(math.Sin(2*real(x))/d, math.Sinh(2*imag(x))/d)
  76. }
  77. // Complex hyperbolic tangent
  78. //
  79. // DESCRIPTION:
  80. //
  81. // tanh z = (sinh 2x + i sin 2y) / (cosh 2x + cos 2y) .
  82. //
  83. // ACCURACY:
  84. //
  85. // Relative error:
  86. // arithmetic domain # trials peak rms
  87. // IEEE -10,+10 30000 1.7e-14 2.4e-16
  88. // Tanh returns the hyperbolic tangent of x.
  89. func Tanh(x complex128) complex128 {
  90. switch re, im := real(x), imag(x); {
  91. case math.IsInf(re, 0):
  92. switch {
  93. case math.IsInf(im, 0) || math.IsNaN(im):
  94. return complex(math.Copysign(1, re), math.Copysign(0, im))
  95. }
  96. return complex(math.Copysign(1, re), math.Copysign(0, math.Sin(2*im)))
  97. case im == 0 && math.IsNaN(re):
  98. return x
  99. }
  100. d := math.Cosh(2*real(x)) + math.Cos(2*imag(x))
  101. if d == 0 {
  102. return Inf()
  103. }
  104. return complex(math.Sinh(2*real(x))/d, math.Sin(2*imag(x))/d)
  105. }
  106. // reducePi reduces the input argument x to the range (-Pi/2, Pi/2].
  107. // x must be greater than or equal to 0. For small arguments it
  108. // uses Cody-Waite reduction in 3 float64 parts based on:
  109. // "Elementary Function Evaluation: Algorithms and Implementation"
  110. // Jean-Michel Muller, 1997.
  111. // For very large arguments it uses Payne-Hanek range reduction based on:
  112. // "ARGUMENT REDUCTION FOR HUGE ARGUMENTS: Good to the Last Bit"
  113. // K. C. Ng et al, March 24, 1992.
  114. func reducePi(x float64) float64 {
  115. // reduceThreshold is the maximum value of x where the reduction using
  116. // Cody-Waite reduction still gives accurate results. This threshold
  117. // is set by t*PIn being representable as a float64 without error
  118. // where t is given by t = floor(x * (1 / Pi)) and PIn are the leading partial
  119. // terms of Pi. Since the leading terms, PI1 and PI2 below, have 30 and 32
  120. // trailing zero bits respectively, t should have less than 30 significant bits.
  121. // t < 1<<30 -> floor(x*(1/Pi)+0.5) < 1<<30 -> x < (1<<30-1) * Pi - 0.5
  122. // So, conservatively we can take x < 1<<30.
  123. const reduceThreshold float64 = 1 << 30
  124. if math.Abs(x) < reduceThreshold {
  125. // Use Cody-Waite reduction in three parts.
  126. const (
  127. // PI1, PI2 and PI3 comprise an extended precision value of PI
  128. // such that PI ~= PI1 + PI2 + PI3. The parts are chosen so
  129. // that PI1 and PI2 have an approximately equal number of trailing
  130. // zero bits. This ensures that t*PI1 and t*PI2 are exact for
  131. // large integer values of t. The full precision PI3 ensures the
  132. // approximation of PI is accurate to 102 bits to handle cancellation
  133. // during subtraction.
  134. PI1 = 3.141592502593994 // 0x400921fb40000000
  135. PI2 = 1.5099578831723193e-07 // 0x3e84442d00000000
  136. PI3 = 1.0780605716316238e-14 // 0x3d08469898cc5170
  137. )
  138. t := x / math.Pi
  139. t += 0.5
  140. t = float64(int64(t)) // int64(t) = the multiple
  141. return ((x - t*PI1) - t*PI2) - t*PI3
  142. }
  143. // Must apply Payne-Hanek range reduction
  144. const (
  145. mask = 0x7FF
  146. shift = 64 - 11 - 1
  147. bias = 1023
  148. fracMask = 1<<shift - 1
  149. )
  150. // Extract out the integer and exponent such that,
  151. // x = ix * 2 ** exp.
  152. ix := math.Float64bits(x)
  153. exp := int(ix>>shift&mask) - bias - shift
  154. ix &= fracMask
  155. ix |= 1 << shift
  156. // mPi is the binary digits of 1/Pi as a uint64 array,
  157. // that is, 1/Pi = Sum mPi[i]*2^(-64*i).
  158. // 19 64-bit digits give 1216 bits of precision
  159. // to handle the largest possible float64 exponent.
  160. var mPi = [...]uint64{
  161. 0x0000000000000000,
  162. 0x517cc1b727220a94,
  163. 0xfe13abe8fa9a6ee0,
  164. 0x6db14acc9e21c820,
  165. 0xff28b1d5ef5de2b0,
  166. 0xdb92371d2126e970,
  167. 0x0324977504e8c90e,
  168. 0x7f0ef58e5894d39f,
  169. 0x74411afa975da242,
  170. 0x74ce38135a2fbf20,
  171. 0x9cc8eb1cc1a99cfa,
  172. 0x4e422fc5defc941d,
  173. 0x8ffc4bffef02cc07,
  174. 0xf79788c5ad05368f,
  175. 0xb69b3f6793e584db,
  176. 0xa7a31fb34f2ff516,
  177. 0xba93dd63f5f2f8bd,
  178. 0x9e839cfbc5294975,
  179. 0x35fdafd88fc6ae84,
  180. 0x2b0198237e3db5d5,
  181. }
  182. // Use the exponent to extract the 3 appropriate uint64 digits from mPi,
  183. // B ~ (z0, z1, z2), such that the product leading digit has the exponent -64.
  184. // Note, exp >= 50 since x >= reduceThreshold and exp < 971 for maximum float64.
  185. digit, bitshift := uint(exp+64)/64, uint(exp+64)%64
  186. z0 := (mPi[digit] << bitshift) | (mPi[digit+1] >> (64 - bitshift))
  187. z1 := (mPi[digit+1] << bitshift) | (mPi[digit+2] >> (64 - bitshift))
  188. z2 := (mPi[digit+2] << bitshift) | (mPi[digit+3] >> (64 - bitshift))
  189. // Multiply mantissa by the digits and extract the upper two digits (hi, lo).
  190. z2hi, _ := bits.Mul64(z2, ix)
  191. z1hi, z1lo := bits.Mul64(z1, ix)
  192. z0lo := z0 * ix
  193. lo, c := bits.Add64(z1lo, z2hi, 0)
  194. hi, _ := bits.Add64(z0lo, z1hi, c)
  195. // Find the magnitude of the fraction.
  196. lz := uint(bits.LeadingZeros64(hi))
  197. e := uint64(bias - (lz + 1))
  198. // Clear implicit mantissa bit and shift into place.
  199. hi = (hi << (lz + 1)) | (lo >> (64 - (lz + 1)))
  200. hi >>= 64 - shift
  201. // Include the exponent and convert to a float.
  202. hi |= e << shift
  203. x = math.Float64frombits(hi)
  204. // map to (-Pi/2, Pi/2]
  205. if x > 0.5 {
  206. x--
  207. }
  208. return math.Pi * x
  209. }
  210. // Taylor series expansion for cosh(2y) - cos(2x)
  211. func tanSeries(z complex128) float64 {
  212. const MACHEP = 1.0 / (1 << 53)
  213. x := math.Abs(2 * real(z))
  214. y := math.Abs(2 * imag(z))
  215. x = reducePi(x)
  216. x = x * x
  217. y = y * y
  218. x2 := 1.0
  219. y2 := 1.0
  220. f := 1.0
  221. rn := 0.0
  222. d := 0.0
  223. for {
  224. rn++
  225. f *= rn
  226. rn++
  227. f *= rn
  228. x2 *= x
  229. y2 *= y
  230. t := y2 + x2
  231. t /= f
  232. d += t
  233. rn++
  234. f *= rn
  235. rn++
  236. f *= rn
  237. x2 *= x
  238. y2 *= y
  239. t = y2 - x2
  240. t /= f
  241. d += t
  242. if !(math.Abs(t/d) > MACHEP) {
  243. // Caution: Use ! and > instead of <= for correct behavior if t/d is NaN.
  244. // See issue 17577.
  245. break
  246. }
  247. }
  248. return d
  249. }
  250. // Complex circular cotangent
  251. //
  252. // DESCRIPTION:
  253. //
  254. // If
  255. // z = x + iy,
  256. //
  257. // then
  258. //
  259. // sin 2x - i sinh 2y
  260. // w = --------------------.
  261. // cosh 2y - cos 2x
  262. //
  263. // On the real axis, the denominator has zeros at even
  264. // multiples of PI/2. Near these points it is evaluated
  265. // by a Taylor series.
  266. //
  267. // ACCURACY:
  268. //
  269. // Relative error:
  270. // arithmetic domain # trials peak rms
  271. // DEC -10,+10 3000 6.5e-17 1.6e-17
  272. // IEEE -10,+10 30000 9.2e-16 1.2e-16
  273. // Also tested by ctan * ccot = 1 + i0.
  274. // Cot returns the cotangent of x.
  275. func Cot(x complex128) complex128 {
  276. d := math.Cosh(2*imag(x)) - math.Cos(2*real(x))
  277. if math.Abs(d) < 0.25 {
  278. d = tanSeries(x)
  279. }
  280. if d == 0 {
  281. return Inf()
  282. }
  283. return complex(math.Sin(2*real(x))/d, -math.Sinh(2*imag(x))/d)
  284. }