Permalink
Browse files

speed up 3 more Complex operations

  • Loading branch information...
moritz committed Jul 1, 2011
1 parent f1fc5ed commit 437675e330460d0cf801d48a635c413d532e062f
Showing with 31 additions and 3 deletions.
  1. +31 −3 src/core/Complex.pm
View
@@ -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() {
@@ -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) {
@@ -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) {

0 comments on commit 437675e

Please sign in to comment.