5
multi method new($re, $im) {
6
self.bless(*, :$re, :$im);
9
multi method ACCEPTS(Complex $topic) {
10
($topic.re ~~ $.re) && ($topic.im ~~ $.im);
12
multi method ACCEPTS($topic) {
13
($topic.Num ~~ $.re) && ($.im == 0);
17
($!re * $!re + $!im * $!im).sqrt
20
multi method Complex() { self }
23
"Complex.new({$.re.perl}, {$.im.perl})";
31
Complex.new($.re.Num.exp * $.im.Num.cos, $.re.Num.exp * $.im.Num.sin);
34
multi method sin($base = 'radians') {
35
$.re.sin($base) * $.im.cosh($base) + ($.re.cos($base) * $.im.sinh($base))i;
38
multi method asin($base = 'radians') {
39
(-1i * log((self)i + sqrt(1 - self * self)))!from-radians($base);
42
multi method cos($base = 'radians') {
43
$.re.cos($base) * $.im.cosh($base) - ($.re.sin($base) * $.im.sinh($base))i;
46
multi method acos($base = 'radians') {
47
(pi / 2)!from-radians($base) - self.asin($base);
50
multi method tan($base = 'radians') {
51
self.sin($base) / self.cos($base);
54
multi method atan($base = 'radians') {
55
((log(1 - (self)i) - log(1 + (self)i))i / 2)!from-radians($base);
58
multi method sec($base = 'radians') {
62
multi method asec($base = 'radians') {
63
(1 / self).acos($base);
66
multi method cosec($base = 'radians') {
70
multi method acosec($base = 'radians') {
71
(1 / self).asin($base);
74
multi method cotan($base = 'radians') {
75
self.cos($base) / self.sin($base);
78
multi method acotan($base = 'radians') {
79
(1 / self).atan($base);
82
multi method sinh($base = 'radians') {
83
-((1i * self).sin($base))i;
86
multi method asinh($base = 'radians') {
87
(self + sqrt(1 + self * self)).log!from-radians($base);
90
multi method cosh($base = 'radians') {
91
(1i * self).cos($base);
94
multi method acosh($base = 'radians') {
95
(self + sqrt(self * self - 1)).log!from-radians($base);
98
multi method tanh($base = 'radians') {
99
-((1i * self).tan($base))i;
102
multi method atanh($base = 'radians') {
103
(((1 + self) / (1 - self)).log / 2)!from-radians($base);
106
multi method sech($base = 'radians') {
107
1 / self.cosh($base);
110
multi method asech($base = 'radians') {
111
(1 / self).acosh($base);
114
multi method cosech($base = 'radians') {
115
1 / self.sinh($base);
118
multi method acosech($base = 'radians') {
119
(1 / self).asinh($base);
122
multi method cotanh($base = 'radians') {
123
1 / self.tanh($base);
126
multi method acotanh($base = 'radians') {
127
(1 / self).atanh($base);
132
$P0 = get_root_namespace ['parrot'; 'Complex' ]
144
$P1 = get_hll_global 'Complex'
145
$P1 = $P1.'new'($P2, $P3)
150
multi method log($base) {
154
multi method log10() {
158
multi method polar() {
159
$.abs, atan2($.im, $.re);
162
multi method roots($n is copy) {
163
my ($mag, $angle) = @.polar;
164
return NaN if $n < 1;
165
return self if $n == 1;
166
return NaN if $!re|$!im ~~ Inf|NaN|-Inf;
169
(^$n).map: { $mag.unpolar( ($angle + $_ * 2 * pi) / $n) };
172
multi method sign() {
173
fail('Cannot take the sign() of a Complex number');
176
multi method sqrt() {
178
$P0 = get_root_namespace ['parrot'; 'Complex' ]
190
$P1 = get_hll_global 'Complex'
191
$P1 = $P1.'new'($P2, $P3)
196
multi method cosec($base = 'radians') {
197
1.0 / self!to-radians($base).sin;
200
multi method cosech($base = 'radians') {
201
1.0 / self!to-radians($base).sinh;
204
multi method acosec($base = 'radians') {
205
(1.0 / self).asin!to-radians($base);
208
multi method cotan($base = 'radians') {
209
1.0 / self!to-radians($base).tan;
212
multi method cotanh($base = 'radians') {
213
1.0 / self!to-radians($base).tanh;
216
multi method acotan($base = 'radians') {
217
(1.0 / self).atan!to-radians($base);
220
multi method acosech($base = 'radians') {
221
(1.0 / self).asinh!to-radians($base);
224
multi method acotanh($base = 'radians') {
225
(1.0 / self).atanh!to-radians($base);
232
fail "You can only coerce a Complex to Num if the imaginary part is zero"
237
multi sub abs(Complex $x) { $x.abs }
239
multi sub infix:<+>(Complex $a, Complex $b) {
240
Complex.new($a.re + $b.re, $a.im + $b.im);
243
multi sub infix:<+>(Complex $a, $b) is default {
244
Complex.new($a.re + $b, $a.im);
247
multi sub infix:<+>($a, Complex $b) {
251
multi sub infix:<->(Complex $a, $b) is default {
255
multi sub infix:<->($a, Complex $b) {
259
multi sub infix:<*>(Complex $a, Complex $b) {
260
Complex.new($a.re * $b.re - $a.im * $b.im, $a.im * $b.re + $a.re * $b.im);
261
# Complex.new($a.re * $a.re - $a.im * $b.im, $a.re * $b.im + $a.im * $b.re);
264
multi sub infix:<*>(Complex $a, $b) is default {
265
Complex.new($a.re * $b, $a.im * $b);
269
multi sub infix:<*>($a, Complex $b) {
270
Complex.new($a * $b.re, $a * $b.im);
273
multi sub infix:</>(Complex $a, Complex $b) {
274
my $d = $b.re * $b.re + $b.im * $b.im;
275
Complex.new(($a.re * $b.re + $a.im * $b.im) / $d,
276
($a.im * $b.re - $a.re * $b.im) / $d);
279
multi sub infix:</>(Complex $a, $b) is default {
283
multi sub infix:</>($a, Complex $b) {
284
Complex.new($a, 0) / $b;
287
multi sub postfix:<i>($x) {
291
multi sub postfix:<i>(Complex $z) {
292
Complex.new(-$z.im, $z.re);
295
multi sub prefix:<->(Complex $a) {
296
Complex.new(-$a.re, -$a.im);
299
multi sub infix:<**>(Complex $a, $b) is default {
303
multi sub infix:<**>($a, Complex $b) {
307
multi sub sign(Complex $x) { $x.sign }
309
multi sub sqrt(Complex $x) {
313
multi sub exp(Complex $x) {