Skip to content

Commit

Permalink
Tweak Complex a bit, including requiring Real values be passed to new…
Browse files Browse the repository at this point in the history
…, moving sqrt, roots, and prefix:<->, and deleting sub log(Complex), sub sign(Complex), and Complex.sign.
  • Loading branch information
colomon committed Jun 12, 2010
1 parent 92b1f42 commit b502e87
Showing 1 changed file with 41 additions and 49 deletions.
90 changes: 41 additions & 49 deletions src/core/Complex.pm
Expand Up @@ -2,13 +2,14 @@ class Complex does Numeric is Cool {
has $.re;
has $.im;

multi method new($re, $im) {
multi method new(Real $re, Real $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);
}
Expand Down Expand Up @@ -71,6 +72,42 @@ class Complex does Numeric is Cool {
}
}
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
}
}

method roots(Complex $x: Int $n) {
return NaN if $n < 1;
return self if $n == 1;
return NaN if $x.re | $x.im ~~ Inf | NaN | -Inf;

my ($mag, $angle) = $x.polar;
$mag **= 1 / $n;
(^$n).map: { $mag.unpolar( ($angle + $_ * 2 * pi) / $n) };
}

multi method polar() {
$.abs, atan2($.im, $.re);
}

method sin(Complex $x: $base = Radians) {
$x.re.sin($base) * $x.im.cosh($base) + ($x.re.cos($base) * $x.im.sinh($base))i;
}
Expand Down Expand Up @@ -167,45 +204,10 @@ class Complex does Numeric is Cool {
(1 / $x).atanh($base);
}

multi method polar() {
$.abs, atan2($.im, $.re);
}

method roots(Complex $x: Int $n) {
return NaN if $n < 1;
return self if $n == 1;
return NaN if $x.re | $x.im ~~ Inf | NaN | -Inf;

my ($mag, $angle) = $x.polar;
$mag **= 1 / $n;
(^$n).map: { $mag.unpolar( ($angle + $_ * 2 * pi) / $n) };
}

multi method sign() {
fail('Cannot take the sign() of a Complex number');
}
}

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 sub prefix:<->(Complex $a) {
Complex.new(-$a.re, -$a.im);
}

multi sub infix:<+>(Complex $a, Complex $b) {
Expand Down Expand Up @@ -265,10 +267,6 @@ multi sub infix:</>(Real $a, Complex $b) {
Complex.new($a, 0) / $b;
}

multi sub prefix:<->(Complex $a) {
Complex.new(-$a.re, -$a.im);
}

multi sub infix:<**>(Complex $a, Complex $b) {
($a.log * $b).exp;
}
Expand All @@ -281,10 +279,4 @@ multi sub infix:<**>(Real $a, Complex $b) {
($a.log * $b).exp;
}

multi sub log(Complex $x) {
$x.log()
}

multi sub sign(Complex $x) { $x.sign }

# vim: ft=perl6

0 comments on commit b502e87

Please sign in to comment.