1
2
3
4
5 package cmplx
6
7 import "math"
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51 func Asin(x complex128) complex128 {
52 switch re, im := real(x), imag(x); {
53 case im == 0 && math.Abs(re) <= 1:
54 return complex(math.Asin(re), im)
55 case re == 0 && math.Abs(im) <= 1:
56 return complex(re, math.Asinh(im))
57 case math.IsNaN(im):
58 switch {
59 case re == 0:
60 return complex(re, math.NaN())
61 case math.IsInf(re, 0):
62 return complex(math.NaN(), re)
63 default:
64 return NaN()
65 }
66 case math.IsInf(im, 0):
67 switch {
68 case math.IsNaN(re):
69 return x
70 case math.IsInf(re, 0):
71 return complex(math.Copysign(math.Pi/4, re), im)
72 default:
73 return complex(math.Copysign(0, re), im)
74 }
75 case math.IsInf(re, 0):
76 return complex(math.Copysign(math.Pi/2, re), math.Copysign(re, im))
77 }
78 ct := complex(-imag(x), real(x))
79 xx := x * x
80 x1 := complex(1-real(xx), -imag(xx))
81 x2 := Sqrt(x1)
82 w := Log(ct + x2)
83 return complex(imag(w), -real(w))
84 }
85
86
87 func Asinh(x complex128) complex128 {
88 switch re, im := real(x), imag(x); {
89 case im == 0 && math.Abs(re) <= 1:
90 return complex(math.Asinh(re), im)
91 case re == 0 && math.Abs(im) <= 1:
92 return complex(re, math.Asin(im))
93 case math.IsInf(re, 0):
94 switch {
95 case math.IsInf(im, 0):
96 return complex(re, math.Copysign(math.Pi/4, im))
97 case math.IsNaN(im):
98 return x
99 default:
100 return complex(re, math.Copysign(0.0, im))
101 }
102 case math.IsNaN(re):
103 switch {
104 case im == 0:
105 return x
106 case math.IsInf(im, 0):
107 return complex(im, re)
108 default:
109 return NaN()
110 }
111 case math.IsInf(im, 0):
112 return complex(math.Copysign(im, re), math.Copysign(math.Pi/2, im))
113 }
114 xx := x * x
115 x1 := complex(1+real(xx), imag(xx))
116 return Log(x + Sqrt(x1))
117 }
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133 func Acos(x complex128) complex128 {
134 w := Asin(x)
135 return complex(math.Pi/2-real(w), -imag(w))
136 }
137
138
139 func Acosh(x complex128) complex128 {
140 if x == 0 {
141 return complex(0, math.Copysign(math.Pi/2, imag(x)))
142 }
143 w := Acos(x)
144 if imag(w) <= 0 {
145 return complex(-imag(w), real(w))
146 }
147 return complex(imag(w), -real(w))
148 }
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184 func Atan(x complex128) complex128 {
185 switch re, im := real(x), imag(x); {
186 case im == 0:
187 return complex(math.Atan(re), im)
188 case re == 0 && math.Abs(im) <= 1:
189 return complex(re, math.Atanh(im))
190 case math.IsInf(im, 0) || math.IsInf(re, 0):
191 if math.IsNaN(re) {
192 return complex(math.NaN(), math.Copysign(0, im))
193 }
194 return complex(math.Copysign(math.Pi/2, re), math.Copysign(0, im))
195 case math.IsNaN(re) || math.IsNaN(im):
196 return NaN()
197 }
198 x2 := real(x) * real(x)
199 a := 1 - x2 - imag(x)*imag(x)
200 if a == 0 {
201 return NaN()
202 }
203 t := 0.5 * math.Atan2(2*real(x), a)
204 w := reducePi(t)
205
206 t = imag(x) - 1
207 b := x2 + t*t
208 if b == 0 {
209 return NaN()
210 }
211 t = imag(x) + 1
212 c := (x2 + t*t) / b
213 return complex(w, 0.25*math.Log(c))
214 }
215
216
217 func Atanh(x complex128) complex128 {
218 z := complex(-imag(x), real(x))
219 z = Atan(z)
220 return complex(imag(z), -real(z))
221 }
222
View as plain text