sin.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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 "math"
  6. // The original C code, the long comment, and the constants
  7. // below are from http://netlib.sandia.gov/cephes/c9x-complex/clog.c.
  8. // The go code is a simplified version of the original C.
  9. //
  10. // Cephes Math Library Release 2.8: June, 2000
  11. // Copyright 1984, 1987, 1989, 1992, 2000 by Stephen L. Moshier
  12. //
  13. // The readme file at http://netlib.sandia.gov/cephes/ says:
  14. // Some software in this archive may be from the book _Methods and
  15. // Programs for Mathematical Functions_ (Prentice-Hall or Simon & Schuster
  16. // International, 1989) or from the Cephes Mathematical Library, a
  17. // commercial product. In either event, it is copyrighted by the author.
  18. // What you see here may be used freely but it comes with no support or
  19. // guarantee.
  20. //
  21. // The two known misprints in the book are repaired here in the
  22. // source listings for the gamma function and the incomplete beta
  23. // integral.
  24. //
  25. // Stephen L. Moshier
  26. // moshier@na-net.ornl.gov
  27. // Complex circular sine
  28. //
  29. // DESCRIPTION:
  30. //
  31. // If
  32. // z = x + iy,
  33. //
  34. // then
  35. //
  36. // w = sin x cosh y + i cos x sinh y.
  37. //
  38. // csin(z) = -i csinh(iz).
  39. //
  40. // ACCURACY:
  41. //
  42. // Relative error:
  43. // arithmetic domain # trials peak rms
  44. // DEC -10,+10 8400 5.3e-17 1.3e-17
  45. // IEEE -10,+10 30000 3.8e-16 1.0e-16
  46. // Also tested by csin(casin(z)) = z.
  47. // Sin returns the sine of x.
  48. func Sin(x complex128) complex128 {
  49. s, c := math.Sincos(real(x))
  50. sh, ch := sinhcosh(imag(x))
  51. return complex(s*ch, c*sh)
  52. }
  53. // Complex hyperbolic sine
  54. //
  55. // DESCRIPTION:
  56. //
  57. // csinh z = (cexp(z) - cexp(-z))/2
  58. // = sinh x * cos y + i cosh x * sin y .
  59. //
  60. // ACCURACY:
  61. //
  62. // Relative error:
  63. // arithmetic domain # trials peak rms
  64. // IEEE -10,+10 30000 3.1e-16 8.2e-17
  65. // Sinh returns the hyperbolic sine of x.
  66. func Sinh(x complex128) complex128 {
  67. s, c := math.Sincos(imag(x))
  68. sh, ch := sinhcosh(real(x))
  69. return complex(c*sh, s*ch)
  70. }
  71. // Complex circular cosine
  72. //
  73. // DESCRIPTION:
  74. //
  75. // If
  76. // z = x + iy,
  77. //
  78. // then
  79. //
  80. // w = cos x cosh y - i sin x sinh y.
  81. //
  82. // ACCURACY:
  83. //
  84. // Relative error:
  85. // arithmetic domain # trials peak rms
  86. // DEC -10,+10 8400 4.5e-17 1.3e-17
  87. // IEEE -10,+10 30000 3.8e-16 1.0e-16
  88. // Cos returns the cosine of x.
  89. func Cos(x complex128) complex128 {
  90. s, c := math.Sincos(real(x))
  91. sh, ch := sinhcosh(imag(x))
  92. return complex(c*ch, -s*sh)
  93. }
  94. // Complex hyperbolic cosine
  95. //
  96. // DESCRIPTION:
  97. //
  98. // ccosh(z) = cosh x cos y + i sinh x sin y .
  99. //
  100. // ACCURACY:
  101. //
  102. // Relative error:
  103. // arithmetic domain # trials peak rms
  104. // IEEE -10,+10 30000 2.9e-16 8.1e-17
  105. // Cosh returns the hyperbolic cosine of x.
  106. func Cosh(x complex128) complex128 {
  107. s, c := math.Sincos(imag(x))
  108. sh, ch := sinhcosh(real(x))
  109. return complex(c*ch, s*sh)
  110. }
  111. // calculate sinh and cosh
  112. func sinhcosh(x float64) (sh, ch float64) {
  113. if math.Abs(x) <= 0.5 {
  114. return math.Sinh(x), math.Cosh(x)
  115. }
  116. e := math.Exp(x)
  117. ei := 0.5 / e
  118. e *= 0.5
  119. return e - ei, e + ei
  120. }