Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
speed up 3 more Complex operations
  • Loading branch information
moritz committed Jul 1, 2011
1 parent f1fc5ed commit 437675e
Showing 1 changed file with 31 additions and 3 deletions.
34 changes: 31 additions & 3 deletions src/core/Complex.pm
Expand Up @@ -53,7 +53,11 @@ my class Complex is Numeric {
}

method abs(Complex $x:) {
($x.re * $x.re + $x.im * $x.im).sqrt
nqp::p6box_n(nqp::add_n(
nqp::mul_n($!re, $!re),
nqp::mul_n($!im, $!im),
)
).sqrt;
}

method polar() {
Expand Down Expand Up @@ -122,7 +126,20 @@ multi sub infix:<+>(Real \$a, Complex \$b) {
}

multi sub infix:<->(Complex \$a, Complex \$b) {
Complex.new($a.re - $b.re, $a.im - $b.im);
my $new := nqp::create(Complex);
nqp::bindattr_n( $new, Complex, '$!re',
nqp::sub_n(
nqp::getattr_n(pir::perl6_decontainerize__PP($a), Complex, '$!re'),
nqp::unbox_n($b.Num)
)
);
nqp::bindattr_n($new, Complex, '$!im',
nqp::sub_n(
nqp::getattr_n(pir::perl6_decontainerize__PP($a), Complex, '$!im'),
nqp::getattr_n(pir::perl6_decontainerize__PP($b), Complex, '$!im'),
)
);
$new
}

multi sub infix:<->(Complex \$a, Real \$b) {
Expand All @@ -134,7 +151,18 @@ multi sub infix:<->(Real \$a, Complex \$b) {
}

multi sub infix:<*>(Complex \$a, Complex \$b) {
Complex.new($a.re * $b.re - $a.im * $b.im, $a.im * $b.re + $a.re * $b.im);
my num $a_re = nqp::getattr_n(pir::perl6_decontainerize__PP($a), Complex, '$!re');
my num $a_im = nqp::getattr_n(pir::perl6_decontainerize__PP($a), Complex, '$!im');
my num $b_re = nqp::getattr_n(pir::perl6_decontainerize__PP($b), Complex, '$!re');
my num $b_im = nqp::getattr_n(pir::perl6_decontainerize__PP($b), Complex, '$!im');
my $new := nqp::create(Complex);
nqp::bindattr_n($new, Complex, '$!re',
nqp::sub_n(nqp::mul_n($a_re, $b_re), nqp::mul_n($a_im, $b_im)),
);
nqp::bindattr_n($new, Complex, '$!im',
nqp::add_n(nqp::mul_n($a_re, $b_im), nqp::mul_n($a_im, $b_re)),
);
$new;
}

multi sub infix:<*>(Complex \$a, Real \$b) {
Expand Down

0 comments on commit 437675e

Please sign in to comment.