Skip to content

Commit

Permalink
Crude but mostly working implementation of Complex.roots. Straightfor…
Browse files Browse the repository at this point in the history
…ward implementation of Num.Complex.
  • Loading branch information
colomon committed Jan 12, 2010
1 parent f2d5c76 commit 393c457
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
25 changes: 18 additions & 7 deletions src/core/Complex.pm
Original file line number Diff line number Diff line change
Expand Up @@ -162,13 +162,24 @@ class Complex {
}
multi method roots($n is copy) {
# my ($mag, $angle) = @.polar;
# return NaN if $n < 1;
# return self if $n == 1;
# return NaN if $!re|$!im ~~ Inf|NaN|-Inf;
# $n = $n.Int;
# $mag **= 1/$n;
# (^$n).map: { $mag.unpolar( ($angle + $_ * 2 * pi) / $n) };
# 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() {
Expand Down
2 changes: 2 additions & 0 deletions src/core/Num.pm
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ augment class Num {
multi method Num() { self; }
multi method Complex() { self + 0i; }
multi method abs() {
pir::abs__Nn(self);
}
Expand Down

0 comments on commit 393c457

Please sign in to comment.