Permalink
Browse files

optimize and fix a few ops. Not correct yet, but compiles

  • Loading branch information...
1 parent 9867c66 commit a5bfdd063ed515ab4f2b9ec659143908416d2cf1 @moritz moritz committed Jul 2, 2011
Showing with 74 additions and 8 deletions.
  1. +74 −8 src/core/Complex.pm
View
@@ -143,11 +143,33 @@ multi sub infix:<->(Complex \$a, Complex \$b) {
}
multi sub infix:<->(Complex \$a, Real \$b) {
- Complex.new($a.re - $b, $a.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'),
+ $b.Num,
+ )
+ );
+ nqp::bindattr_n($new, Complex, '$!im',
+ nqp::sub_n(
+ nqp::getattr_n(pir::perl6_decontainerize__PP($a), Complex, '$!im'),
+ )
+ );
+ $new
}
multi sub infix:<->(Real \$a, Complex \$b) {
- Complex.new($a - $b.re, -$b.im);
+ my $new := nqp::create(Complex);
+ nqp::bindattr_n( $new, Complex, '$!re',
+ nqp::sub_n(
+ $a.Num,
+ nqp::getattr_n(pir::perl6_decontainerize__PP($b), Complex, '$!re'),
+ )
+ );
+ nqp::bindattr_n($new, Complex, '$!im',
+ nqp::getattr_n(pir::perl6_decontainerize__PP($b), Complex, '$!im'),
+ );
+ $new
}
multi sub infix:<*>(Complex \$a, Complex \$b) {
@@ -166,17 +188,61 @@ multi sub infix:<*>(Complex \$a, Complex \$b) {
}
multi sub infix:<*>(Complex \$a, Real \$b) {
- Complex.new($a.re * $b, $a.im * $b);
+ my $new := nqp::create(Complex);
+ my num $b_num = $b.Num;
+ nqp::bindattr_n($new, Complex, '$!re',
+ nqp::mul_n(
+ nqp::getattr_n(pir::perl6_decontainerize__PP($a), Complex, '$!re'),
+ $b_num,
+ )
+ );
+ nqp::bindattr_n($new, Complex, '$!im',
+ nqp::mul_n(
+ nqp::getattr_n(pir::perl6_decontainerize__PP($a), Complex, '$!im'),
+ $b_num,
+ )
+ );
+ $new
}
multi sub infix:<*>(Real \$a, Complex \$b) {
- Complex.new($a * $b.re, $a * $b.im);
+ my $new := nqp::create(Complex);
+ my num $a_num = $a.Num;
+ nqp::bindattr_n($new, Complex, '$!re',
+ nqp::mul_n(
+ $a_num,
+ nqp::getattr_n(pir::perl6_decontainerize__PP($b), Complex, '$!re'),
+ )
+ );
+ nqp::bindattr_n($new, Complex, '$!im',
+ nqp::mul_n(
+ $a_num,
+ nqp::getattr_n(pir::perl6_decontainerize__PP($b), Complex, '$!im'),
+ )
+ );
+ $new
}
multi sub infix:</>(Complex \$a, Complex \$b) {
- my $d = $b.re * $b.re + $b.im * $b.im;
- Complex.new(($a.re * $b.re + $a.im * $b.im) / $d,
- ($a.im * $b.re - $a.re * $b.im) / $d);
+ 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 num $d = nqp::add_n(nqp::mul_n($b_re, $b_re), nqp::mul_n($b_im, $b_im));
+ my $new := nqp::create(Complex);
+ nqp::bindattr_n($new, Complex, '$!re',
+ nqp::div_n(
+ nqp::add_n(nqp::mul_n($a_re, $b_re), nqp::mul_n($a_im, $b_im)),
+ $d,
+ )
+ );
+ nqp::bindattr_n($new, Complex, '$!im',
+ nqp::div_n(
+ nqp::sub_n(nqp::mul_n($a_im, $b_re), nqp::mul_n($a_re, $b_im)),
+ $d,
+ )
+ );
+ $new;
}
multi sub infix:</>(Complex \$a, Real \$b) {
@@ -189,7 +255,7 @@ multi sub infix:</>(Real \$a, Complex \$b) {
proto postfix:<i>(|$) { * }
multi postfix:<i>(Real \$a) { Complex.new(0e0, $a); }
-multi postfix:<i>(Complex \$a) { Complex.new(-$a.re, $a.im) }
+multi postfix:<i>(Complex \$a) { Complex.new(-$a.im, $a.re) }
multi postfix:<i>(Numeric \$a) { $a * Complex.new(0e0, 1e0) }
# vim: ft=perl6

0 comments on commit a5bfdd0

Please sign in to comment.