-
Notifications
You must be signed in to change notification settings - Fork 0
/
complex.go
500 lines (421 loc) · 7.09 KB
/
complex.go
1
2
3
4
5
6
7
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
package ops
import (
"fmt"
"math/cmplx"
"github.com/blackchip-org/zc/v5/pkg/types"
"github.com/blackchip-org/zc/v5/pkg/zc"
)
/*
oper abs
func AbsComplex p0:Complex -- Float
title Distance from zero in complex plane
desc
The distance of *p0* from zero in the complex plane.
end
example
2+2i abs 5 round -- 2.82843
end
*/
func AbsComplex(c zc.Calc) {
a0 := zc.PopComplex(c)
r0 := cmplx.Abs(a0)
zc.PushFloat(c, r0)
}
/*
oper acos
func AcosComplex p0:Complex -- Complex
title Inverse cosine
desc
Inverse cosine of *p0*.
end
example
0.5+1i acos 5 round -- 1.22136-0.92613i
end
*/
func AcosComplex(c zc.Calc) {
a0 := zc.PopComplex(c)
r0 := cmplx.Acos(a0)
zc.PushComplex(c, r0)
}
/*
oper acosh
func AcoshComplex p0:Complex -- Complex
title Inverse hyperbolic cosine
desc
Inverse hyperbolic cosine of *p0*
end
example
2+2i acosh 5 round -- 1.73432+0.81655i
end
*/
func AcoshComplex(c zc.Calc) {
a0 := zc.PopComplex(c)
r0 := cmplx.Acosh(a0)
zc.PushComplex(c, r0)
}
/*
oper asin
func AsinComplex p0:Complex -- Complex
title Inverse sine
desc
Inverse sine of *p0*.
end
example
0.5+2i asin 5 round -- 0.22102+1.46572i
end
*/
func AsinComplex(c zc.Calc) {
a0 := zc.PopComplex(c)
r0 := cmplx.Asin(a0)
zc.PushComplex(c, r0)
}
/*
oper asinh
func AsinComplex p0:Complex -- Complex
title Inverse hyperbolic sine
desc
Inverse hyperbolic sine of *p0*.
end
example
2+2i asinh 5 round -- 0.75425+1.73432i
end
*/
func AsinhComplex(c zc.Calc) {
a0 := zc.PopComplex(c)
r0 := cmplx.Asinh(a0)
zc.PushComplex(c, r0)
}
/*
oper atan
func AtanComplex p0:Complex -- Complex
title Inverse tangent
desc
Inverse tangent of *p0*.
end
example
0.5+2i atan 5 round -- 1.42155+0.50037i
end
*/
func AtanComplex(c zc.Calc) {
a0 := zc.PopComplex(c)
r0 := cmplx.Atan(a0)
zc.PushComplex(c, r0)
}
/*
oper atanh
func AtanhComplex p0:Complex -- Complex
title Inverse hyperbolic tangent
desc
Inverse hyperbolic tangent of *p0*.
end
example
0.5+2i atanh 5 round --0.09642+1.12656i
end
*/
func AtanhComplex(c zc.Calc) {
a0 := zc.PopComplex(c)
r0 := cmplx.Atanh(a0)
zc.PushComplex(c, r0)
}
/*
oper complex
func Complex r:Float i:Float -- Complex
title Complex from real and imaginary
desc
A complex number from a real *r* and an imaginary *i* number.
end
example
6 -- 6
12 -- 6 | 12
complex -- 6+12i
end
*/
func Complex(c zc.Calc) {
i := zc.PopFloat(c)
r := zc.PopFloat(c)
r0 := complex(r, i)
zc.PushComplex(c, r0)
}
/*
oper conj
func Conj p0:Complex -- Complex
title Complex conjugate
desc
The complex conjugate of *p0*
end
example
5+3i conj -- 5-3i
end
*/
func Conj(c zc.Calc) {
a0 := zc.PopComplex(c)
r0 := cmplx.Conj(a0)
zc.PushComplex(c, r0)
}
/*
oper cos
func CosComplex p0:Complex -- Complex
title Cosine
desc
Cosine of *p0*.
end
example
2+2i cos 5 round -- -1.56563-3.29789i
end
*/
func CosComplex(c zc.Calc) {
a0 := zc.PopComplex(c)
r0 := cmplx.Cos(a0)
zc.PushComplex(c, r0)
}
/*
oper cosh
func CoshComplex p0:Complex -- Complex
title Hyperbolic cosine
desc
Hyperbolic cosine of *p0*.
end
example
2+2i cosh 5 round -- -1.56563+3.29789i
end
*/
func CoshComplex(c zc.Calc) {
a0 := zc.PopComplex(c)
r0 := cmplx.Cosh(a0)
zc.PushComplex(c, r0)
}
/*
oper cot
func CotComplex p0:Complex -- Complex
title Cotangent
desc
Cotangent of *p0*.
end
example
2+3i cot 5 round -- -0.00374-0.99676i
end
*/
func CotComplex(c zc.Calc) {
p0 := zc.PopComplex(c)
r0 := cmplx.Cot(p0)
zc.PushComplex(c, r0)
}
/*
oper exp
func ExpComplex p0:Complex -- Complex
title Natural exponential
desc
Natural exponential of *p0*.
end
example
2+2i exp 5 round -- -3.07493+6.71885i
end
*/
func ExpComplex(c zc.Calc) {
a0 := zc.PopComplex(c)
r0 := cmplx.Exp(a0)
zc.PushComplex(c, r0)
}
/*
oper imag
func Imag p0:Complex -- Float
title Imaginary number from complex
desc
The imaginary number part of complex number *p0*
end
example
3+4i imag -- 4
end
*/
func Imag(c zc.Calc) {
a0 := zc.PopComplex(c)
r0 := imag(a0)
zc.PushFloat(c, r0)
}
/*
oper log
func LogComplex p0:Complex -- Complex
title Natural logarithm
desc
Natural logarithm of *p0*.
end
example
8+2i log 5 round -- 2.10975+0.24498i
end
*/
func LogComplex(c zc.Calc) {
a0 := zc.PopComplex(c)
r0 := cmplx.Log(a0)
zc.PushComplex(c, r0)
}
/*
oper log10
func Log10Complex p0:Complex -- Complex
title Decimal logarithm
desc
Decimal logarithm of *p0*.
end
example
50+20i log10 5 round -- 1.7312+0.16525i
end
*/
func Log10Complex(c zc.Calc) {
a0 := zc.PopComplex(c)
r0 := cmplx.Log10(a0)
zc.PushComplex(c, r0)
}
/*
oper real
func Real p0:Complex -- Float
title Real number from complex
desc
The real number part of complex number *p0*
end
example
3+4i real -- 3
end
*/
func Real(c zc.Calc) {
a0 := zc.PopComplex(c)
r0 := real(a0)
zc.PushFloat(c, r0)
}
/*
oper round
func RoundComplex p0:Complex n:Int -- Complex
alias r
title Round to a given precision
desc
Rounds the real and imaginary numbers of *p0* to *n* digits using the half
up rounding mode.
end
example
1.047197551196598+5.948293i 3 round -- 1.047+5.948i
end
*/
func RoundComplex(c zc.Calc) {
n := zc.PopInt32(c)
a0 := zc.PopComplex(c)
r := types.NewDecimalFromFloat(real(a0))
i := types.NewDecimalFromFloat(imag(a0))
posSign := ""
if i.IsPositive() {
posSign = "+"
}
r0 := fmt.Sprintf("%v%v%vi", r.Round(n), posSign, i.Round(n))
zc.PushString(c, r0)
}
/*
oper phase
func PhaseComplex p0:Complex -- Float
title Phase (argument)
desc
The phase, or argument, of *p0* in the range of [-π, π]
end
example
1+1i phase 5 round -- 0.7854
end
*/
func PhaseComplex(c zc.Calc) {
a0 := zc.PopComplex(c)
r0 := cmplx.Phase(a0)
zc.PushFloat(c, r0)
}
/*
oper polar
func PolarComplex p0:Complex -- r:Float θ:Float
title Complex to polar
desc
The absolute value *r* and phase *θ* of *p0*.
end
example
2i polar pi div 1 round -- 2 | 0.5
end
*/
func PolarComplex(c zc.Calc) {
a0 := zc.PopComplex(c)
r, θ := cmplx.Polar(a0)
zc.PushFloat(c, r)
zc.PushFloat(c, θ)
}
/*
oper rect
func RectComplex r:Float θ:Float -- Complex
title Polar to complex
desc
The complex number with polar coordinates *r* and *θ*.
end
example
2 0.5 pi mul rect imag -- 2
end
*/
func RectComplex(c zc.Calc) {
θ := zc.PopFloat(c)
r := zc.PopFloat(c)
r0 := cmplx.Rect(r, θ)
zc.PushComplex(c, r0)
}
/*
oper sin
func SinComplex p0:Complex -- Complex
title Sine
desc
Sine of *p0*.
end
example
2+2i sin 5 round -- 3.42095-1.50931i
end
*/
func SinComplex(c zc.Calc) {
a0 := zc.PopComplex(c)
r0 := cmplx.Sin(a0)
zc.PushComplex(c, r0)
}
/*
oper sinh
func SinhComplex p0:Complex -- Complex
title Hyperbolic sine
desc
Hyperbolic sine of *p0*.
end
example
2+2i sinh 5 round -- -1.50931+3.42095i
end
*/
func SinhComplex(c zc.Calc) {
a0 := zc.PopComplex(c)
r0 := cmplx.Sinh(a0)
zc.PushComplex(c, r0)
}
/*
oper tan
func TanComplex p0:Complex -- Complex
title Tangent
desc
Tangent of *p0*.
end
example
2+2i tan 5 round -- -0.02839+1.02384i
end
*/
func TanComplex(c zc.Calc) {
a0 := zc.PopComplex(c)
r0 := cmplx.Tan(a0)
zc.PushComplex(c, r0)
}
/*
oper tanh
func TanhComplex p0:Complex -- Complex
title Hyperbolic tangent
desc
Hyperbolic tangent of *p0*.
end
example
2+2i tanh 5 round -- 1.02384-0.02839i
end
*/
func TanhComplex(c zc.Calc) {
a0 := zc.PopComplex(c)
r0 := cmplx.Tanh(a0)
zc.PushComplex(c, r0)
}