Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

speed up 3 more Complex operations

  • Loading branch information...
commit 437675e330460d0cf801d48a635c413d532e062f 1 parent f1fc5ed
Moritz Lenz authored July 01, 2011

Showing 1 changed file with 31 additions and 3 deletions. Show diff stats Hide diff stats

  1. 34  src/core/Complex.pm
34  src/core/Complex.pm
@@ -53,7 +53,11 @@ my class Complex is Numeric {
53 53
     }
54 54
 
55 55
     method abs(Complex $x:) {
56  
-        ($x.re * $x.re + $x.im * $x.im).sqrt
  56
+        nqp::p6box_n(nqp::add_n(
  57
+                nqp::mul_n($!re, $!re),
  58
+                nqp::mul_n($!im, $!im),
  59
+            )
  60
+        ).sqrt;
57 61
     }
58 62
 
59 63
     method polar() {
@@ -122,7 +126,20 @@ multi sub infix:<+>(Real \$a, Complex \$b) {
122 126
 }
123 127
 
124 128
 multi sub infix:<->(Complex \$a, Complex \$b) {
125  
-    Complex.new($a.re - $b.re, $a.im - $b.im);
  129
+    my $new := nqp::create(Complex);
  130
+    nqp::bindattr_n( $new, Complex, '$!re',
  131
+        nqp::sub_n(
  132
+            nqp::getattr_n(pir::perl6_decontainerize__PP($a), Complex, '$!re'),
  133
+            nqp::unbox_n($b.Num)
  134
+        )
  135
+    );
  136
+    nqp::bindattr_n($new, Complex, '$!im',
  137
+        nqp::sub_n(
  138
+            nqp::getattr_n(pir::perl6_decontainerize__PP($a), Complex, '$!im'),
  139
+            nqp::getattr_n(pir::perl6_decontainerize__PP($b), Complex, '$!im'),
  140
+        )
  141
+    );
  142
+    $new
126 143
 }
127 144
 
128 145
 multi sub infix:<->(Complex \$a, Real \$b) {
@@ -134,7 +151,18 @@ multi sub infix:<->(Real \$a, Complex \$b) {
134 151
 }
135 152
 
136 153
 multi sub infix:<*>(Complex \$a, Complex \$b) {
137  
-    Complex.new($a.re * $b.re - $a.im * $b.im, $a.im * $b.re + $a.re * $b.im);
  154
+    my num $a_re = nqp::getattr_n(pir::perl6_decontainerize__PP($a), Complex, '$!re');
  155
+    my num $a_im = nqp::getattr_n(pir::perl6_decontainerize__PP($a), Complex, '$!im');
  156
+    my num $b_re = nqp::getattr_n(pir::perl6_decontainerize__PP($b), Complex, '$!re');
  157
+    my num $b_im = nqp::getattr_n(pir::perl6_decontainerize__PP($b), Complex, '$!im');
  158
+    my $new := nqp::create(Complex);
  159
+    nqp::bindattr_n($new, Complex, '$!re',
  160
+        nqp::sub_n(nqp::mul_n($a_re, $b_re), nqp::mul_n($a_im, $b_im)),
  161
+    );
  162
+    nqp::bindattr_n($new, Complex, '$!im',
  163
+        nqp::add_n(nqp::mul_n($a_re, $b_im), nqp::mul_n($a_im, $b_re)),
  164
+    );
  165
+    $new;
138 166
 }
139 167
 
140 168
 multi sub infix:<*>(Complex \$a, Real \$b) {

0 notes on commit 437675e

Please sign in to comment.
Something went wrong with that request. Please try again.