Skip to content

Instantly share code, notes, and snippets.

@grondilu
Created July 30, 2013 13:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save grondilu/6112731 to your computer and use it in GitHub Desktop.
Save grondilu/6112731 to your computer and use it in GitHub Desktop.
ethiopian exponentiation in Perl 6
From f3c3e42801cc58889a7994d043891d04d16f5ba2 Mon Sep 17 00:00:00 2001
From: Lucien Grondin <grondilu@yahoo.fr>
Date: Tue, 30 Jul 2013 14:55:22 +0200
Subject: [PATCH] first attempt of ethiopian exponentiation
---
src/core/Complex.pm | 6 ++++++
1 file changed, 6 insertions(+)
diff --git a/src/core/Complex.pm b/src/core/Complex.pm
index d5e0ada..a775bbf 100644
--- a/src/core/Complex.pm
+++ b/src/core/Complex.pm
@@ -400,6 +400,12 @@ multi sub infix:</>(Real \a, Complex:D \b) returns Complex:D {
Complex.new(a, 0) / b;
}
+multi sub infix:<**>(Complex:D \z, Int:D \n where \n > 0) {
+ my @n = n, * div 2 ...^ 0;
+ my @z := z, { $^z * $z } ... *;
+ [*] map *.value, grep *.key % 2, (@n Z=> @z);
+}
+multi sub infix:<**>(Complex:D \z, Int:D \n where n < 0) { 1/ z**(-n) }
multi sub infix:<**>(Complex:D \a, Complex:D \b) returns Complex:D {
(a.re == 0e0 && a.im == 0e0) ?? Complex.new(0e0, 0e0) !! (b * a.log).exp
}
--
1.8.1.4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment