Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Rework Complex.roots a bit, restoring its original elegance. (It had …
…been hacked to pieces to get it to work under ng.)
  • Loading branch information
colomon committed May 19, 2010
1 parent 5ec28d3 commit 77c58c2
Showing 1 changed file with 8 additions and 18 deletions.
26 changes: 8 additions & 18 deletions src/core/Complex.pm
Expand Up @@ -159,25 +159,15 @@ class Complex does Numeric is Cool {
$.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;
multi method roots(Complex $x: $n is copy) {
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) };
(0 ... ($n-1)).map: { $mag.unpolar( ($angle + $^x * 2 * 312689/99532) / $n) };
$mag **= 1 / $n;
(^$n).map: { $mag.unpolar( ($angle + $_ * 2 * pi) / $n) };
}

multi method sign() {
Expand Down

0 comments on commit 77c58c2

Please sign in to comment.