sqrt.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. // Copyright ©2017 The Gonum Authors. All rights reserved.
  5. // Use of this source code is governed by a BSD-style
  6. // license that can be found in the LICENSE file.
  7. package cmplx64
  8. import math "gonum.org/v1/gonum/internal/math32"
  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 square root
  31. //
  32. // DESCRIPTION:
  33. //
  34. // If z = x + iy, r = |z|, then
  35. //
  36. // 1/2
  37. // Re w = [ (r + x)/2 ] ,
  38. //
  39. // 1/2
  40. // Im w = [ (r - x)/2 ] .
  41. //
  42. // Cancelation error in r-x or r+x is avoided by using the
  43. // identity 2 Re w Im w = y.
  44. //
  45. // Note that -w is also a square root of z. The root chosen
  46. // is always in the right half plane and Im w has the same sign as y.
  47. //
  48. // ACCURACY:
  49. //
  50. // Relative error:
  51. // arithmetic domain # trials peak rms
  52. // DEC -10,+10 25000 3.2e-17 9.6e-18
  53. // IEEE -10,+10 1,000,000 2.9e-16 6.1e-17
  54. // Sqrt returns the square root of x.
  55. // The result r is chosen so that real(r) ≥ 0 and imag(r) has the same sign as imag(x).
  56. func Sqrt(x complex64) complex64 {
  57. if imag(x) == 0 {
  58. if real(x) == 0 {
  59. return complex(0, 0)
  60. }
  61. if real(x) < 0 {
  62. return complex(0, math.Sqrt(-real(x)))
  63. }
  64. return complex(math.Sqrt(real(x)), 0)
  65. }
  66. if real(x) == 0 {
  67. if imag(x) < 0 {
  68. r := math.Sqrt(-0.5 * imag(x))
  69. return complex(r, -r)
  70. }
  71. r := math.Sqrt(0.5 * imag(x))
  72. return complex(r, r)
  73. }
  74. a := real(x)
  75. b := imag(x)
  76. var scale float32
  77. // Rescale to avoid internal overflow or underflow.
  78. if math.Abs(a) > 4 || math.Abs(b) > 4 {
  79. a *= 0.25
  80. b *= 0.25
  81. scale = 2
  82. } else {
  83. a *= 1.8014398509481984e16 // 2**54
  84. b *= 1.8014398509481984e16
  85. scale = 7.450580596923828125e-9 // 2**-27
  86. }
  87. r := math.Hypot(a, b)
  88. var t float32
  89. if a > 0 {
  90. t = math.Sqrt(0.5*r + 0.5*a)
  91. r = scale * math.Abs((0.5*b)/t)
  92. t *= scale
  93. } else {
  94. r = math.Sqrt(0.5*r - 0.5*a)
  95. t = scale * math.Abs((0.5*b)/r)
  96. r *= scale
  97. }
  98. if b < 0 {
  99. return complex(t, -r)
  100. }
  101. return complex(t, r)
  102. }