/
Complex.pm
345 lines (272 loc) Β· 7.82 KB
/
Complex.pm
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
class Complex {
has $.re;
has $.im;
multi method new($re, $im) {
self.bless(*, :re($re), :im($im));
}
multi method ACCEPTS(Complex $topic) {
($topic.re ~~ $.re) && ($topic.im ~~ $.im);
}
multi method ACCEPTS($topic) {
($topic.Num ~~ $.re) && ($.im == 0);
}
multi method abs() {
($!re * $!re + $!im * $!im).sqrt
}
multi method Complex() { self }
our Bool multi method Bool() { ( $!re != 0 || $!im != 0 ) ?? Bool::True !! Bool::False }
multi method perl() {
"Complex.new($.re, $.im)";
}
multi method Str() {
"$.re + {$.im}i";
}
multi method exp() {
Complex.new($.re.Num.exp * $.im.Num.cos, $.re.Num.exp * $.im.Num.sin);
}
multi method sin($base = 'radians') {
$.re.sin($base) * $.im.cosh($base) + ($.re.cos($base) * $.im.sinh($base))i;
}
multi method asin($base = 'radians') {
# (-1i * log((self)i + sqrt(1 - self * self)))!from-radians($base);
}
multi method cos($base = 'radians') {
$.re.cos($base) * $.im.cosh($base) - ($.re.sin($base) * $.im.sinh($base))i;
}
multi method acos($base = 'radians') {
# (pi / 2)!from-radians($base) - self.asin($base);
}
multi method tan($base = 'radians') {
self.sin($base) / self.cos($base);
}
multi method atan($base = 'radians') {
# ((log(1 - (self)i) - log(1 + (self)i))i / 2)!from-radians($base);
}
multi method sec($base = 'radians') {
1 / self.cos($base);
}
multi method asec($base = 'radians') {
(1 / self).acos($base);
}
multi method cosec($base = 'radians') {
1 / self.sin($base);
}
multi method acosec($base = 'radians') {
(1 / self).asin($base);
}
multi method cotan($base = 'radians') {
self.cos($base) / self.sin($base);
}
multi method acotan($base = 'radians') {
(1 / self).atan($base);
}
multi method sinh($base = 'radians') {
-((1i * self).sin($base))i;
}
multi method asinh($base = 'radians') {
# (self + sqrt(1 + self * self)).log!from-radians($base);
}
multi method cosh($base = 'radians') {
(1i * self).cos($base);
}
multi method acosh($base = 'radians') {
# (self + sqrt(self * self - 1)).log!from-radians($base);
}
multi method tanh($base = 'radians') {
-((1i * self).tan($base))i;
}
multi method atanh($base = 'radians') {
# (((1 + self) / (1 - self)).log / 2)!from-radians($base);
}
multi method sech($base = 'radians') {
1 / self.cosh($base);
}
multi method asech($base = 'radians') {
(1 / self).acosh($base);
}
multi method cosech($base = 'radians') {
1 / self.sinh($base);
}
multi method acosech($base = 'radians') {
(1 / self).asinh($base);
}
multi method cotanh($base = 'radians') {
1 / self.tanh($base);
}
multi method acotanh($base = 'radians') {
(1 / self).atanh($base);
}
multi method log() {
Q:PIR {
.local pmc self
self = find_lex 'self'
$P0 = get_root_namespace ['parrot'; 'Complex' ]
$P0 = get_class $P0
$P0 = $P0.'new'()
$N0 = self.'re'()
$P0[0] = $N0
$N1 = self.'im'()
$P0[1] = $N1
$P0 = $P0.'ln'()
$N0 = $P0[0]
$P2 = box $N0
$N1 = $P0[1]
$P3 = box $N1
$P1 = get_hll_global 'Complex'
$P1 = $P1.'new'($P2, $P3)
%r = $P1
}
}
multi method log($base) {
$.log / $base.log;
}
multi method log10() {
$.log / 10.log;
}
multi method polar() {
$.abs, atan2($.im, $.re);
}
multi method roots($n is copy) {
# my ($mag, $angle) = @.polar;
my $mag = $.abs;
my $angle = atan2($.im, $.re);
if $n < 1
{
return NaN;
}
if $n == 1
{
return self;
}
# return NaN if $!re|$!im ~~ Inf|NaN|-Inf;
$n = $n.Int;
$mag **= 1/$n;
# (^$n).map: { $mag.unpolar( ($angle + $_ * 2 * pi) / $n) };
(0 ... ($n-1)).map: { $mag.unpolar( ($angle + $^x * 2 * 312689/99532) / $n) };
}
multi method sign() {
fail('Cannot take the sign() of a Complex number');
}
multi method sqrt() {
Q:PIR {
.local pmc self
self = find_lex 'self'
$P0 = get_root_namespace ['parrot'; 'Complex' ]
$P0 = get_class $P0
$P0 = $P0.'new'()
$N0 = self.'re'()
$P0[0] = $N0
$N1 = self.'im'()
$P0[1] = $N1
$P0 = $P0.'sqrt'()
$N0 = $P0[0]
$P2 = box $N0
$N1 = $P0[1]
$P3 = box $N1
$P1 = get_hll_global 'Complex'
$P1 = $P1.'new'($P2, $P3)
%r = $P1
}
}
multi method cosec($base = 'radians') {
# 1.0 / self!to-radians($base).sin;
}
multi method cosech($base = 'radians') {
# 1.0 / self!to-radians($base).sinh;
}
multi method acosec($base = 'radians') {
# (1.0 / self).asin!to-radians($base);
}
multi method cotan($base = 'radians') {
# 1.0 / self!to-radians($base).tan;
}
multi method cotanh($base = 'radians') {
# 1.0 / self!to-radians($base).tanh;
}
multi method acotan($base = 'radians') {
# (1.0 / self).atan!to-radians($base);
}
multi method acosech($base = 'radians') {
# (1.0 / self).asinh!to-radians($base);
}
multi method acotanh($base = 'radians') {
# (1.0 / self).atanh!to-radians($base);
}
multi method Num {
if $!im == 0 {
$!re;
} else {
fail "You can only coerce a Complex to Num if the imaginary part is zero"
}
}
}
multi sub infix:<+>(Complex $a, Complex $b) {
Complex.new($a.re + $b.re, $a.im + $b.im);
}
multi sub infix:<+>(Complex $a, $b) {
Complex.new($a.re + $b, $a.im);
}
multi sub infix:<+>($a, Complex $b) {
# Was $b + $a; but that trips a ng bug, and also means
# that Num + Complex is slower than Complex + Num, which
# seems daft.
Complex.new($a + $b.re, $b.im);
}
# Originally infix:<-> was implemented in terms of addition, but
# that adds an extra function call to each. This repeats ourselves,
# but should avoid odd performance anomalies.
multi sub infix:<->(Complex $a, Complex $b) {
Complex.new($a.re - $b.re, $a.im - $b.im);
}
multi sub infix:<->(Complex $a, $b) {
Complex.new($a.re - $b, $a.im);
}
multi sub infix:<->($a, Complex $b) {
Complex.new($a - $b.re, -$b.im);
}
multi sub infix:<*>(Complex $a, Complex $b) {
Complex.new($a.re * $b.re - $a.im * $b.im, $a.im * $b.re + $a.re * $b.im);
}
multi sub infix:<*>(Complex $a, $b) {
Complex.new($a.re * $b, $a.im * $b);
}
multi sub infix:<*>($a, Complex $b) {
Complex.new($a * $b.re, $a * $b.im);
}
multi sub infix:</>(Complex $a, Complex $b) {
my $d = $b.re * $b.re + $b.im * $b.im;
Complex.new(($a.re * $b.re + $a.im * $b.im) / $d,
($a.im * $b.re - $a.re * $b.im) / $d);
}
multi sub infix:</>(Complex $a, $b) {
Complex.new($a.re / $b, $a.im / $b);
}
multi sub infix:</>($a, Complex $b) {
Complex.new($a, 0) / $b;
}
multi sub postfix:<i>($x) {
Complex.new(0, +$x);
}
multi sub postfix:<i>(Complex $z) {
Complex.new(-$z.im, $z.re);
}
multi sub prefix:<->(Complex $a) {
Complex.new(-$a.re, -$a.im);
}
#multi sub infix:<**>(Complex $a, $b) is default {
# ($a.log * $b).exp;
#}
multi sub infix:<**>($a, Complex $b) {
($a.log * $b).exp;
}
multi sub log(Complex $x) {
$x.log()
}
multi sub log10(Complex $x) {
$x.log10;
}
multi sub sign(Complex $x) { $x.sign }
multi sub sqrt(Complex $x) {
$x.sqrt;
}
# vim: ft=perl6