Skip to content

Commit

Permalink
Change Complex.roots to take an Int as spec'd, and make it a normal m…
Browse files Browse the repository at this point in the history
…ethod instead of a multi. Add Real.roots. Add Real.Complex and eliminate now unneeded Int.Complex and Num.Complex. Clean up Real.unpolar now that cheats are unneeded.
  • Loading branch information
colomon committed May 19, 2010
1 parent 77c58c2 commit 3a694dd
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 8 deletions.
3 changes: 1 addition & 2 deletions src/core/Complex.pm
Expand Up @@ -159,13 +159,12 @@ class Complex does Numeric is Cool {
$.abs, atan2($.im, $.re);
}

multi method roots(Complex $x: $n is copy) {
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;
$n = $n.Int;
$mag **= 1 / $n;
(^$n).map: { $mag.unpolar( ($angle + $_ * 2 * pi) / $n) };
}
Expand Down
2 changes: 0 additions & 2 deletions src/core/Int.pm
Expand Up @@ -36,8 +36,6 @@ augment class Int does Real {

# Next has been moved to Rat.pm for the moment.
# our Rat multi method Rat() { Rat.new(self, 1); }

our ::Complex multi method Complex() { Complex.new(self, 0); }
}

our multi sub infix:<+>(Int $a, Int $b) {
Expand Down
2 changes: 0 additions & 2 deletions src/core/Num.pm
Expand Up @@ -37,8 +37,6 @@ augment class Num does Real {
multi method Num() { self; }
multi method Complex() { self + 0i; }
method !modf($num) { my $q = $num.Int; $num - $q, $q; }
multi method Rat($epsilon = 1.0e-6) {
Expand Down
11 changes: 9 additions & 2 deletions src/core/Real.pm
Expand Up @@ -5,6 +5,10 @@ role Real does Numeric {
fail "Bridge must be defined for the Real type " ~ self.WHAT;
}

method Complex() {
Complex.new(self, 0);
}

method abs(Real $x:) {
$x < 0 ?? -$x !! $x;
}
Expand Down Expand Up @@ -39,9 +43,8 @@ role Real does Numeric {
floor($x / $scale + 0.5) * $scale;
}

# CHEAT: the .Bridges in unpolar should go away in the long run
method unpolar(Real $mag: Real $angle) {
Complex.new($mag * $angle.Bridge.cos(Radians),
Complex.new($mag * $angle.cos(Radians),
$mag * $angle.sin(Radians));
}

Expand All @@ -53,6 +56,10 @@ role Real does Numeric {
$x.Bridge.sqrt;
}

method roots(Real $x: Int $n) {
$x.Complex.roots($n);
}

method sin(Real $x: $base = Radians) {
$x.Bridge.sin($base);
}
Expand Down

0 comments on commit 3a694dd

Please sign in to comment.