From 9e435188e1a97581f9f4fa4ea1e7022f82b05412 Mon Sep 17 00:00:00 2001 From: kalmar Date: Tue, 31 Jan 2017 14:53:41 +0100 Subject: [PATCH] slightly more performant version of power_by_squaring --- src/Groups.jl | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/Groups.jl b/src/Groups.jl index 706c8f1..8361ea7 100644 --- a/src/Groups.jl +++ b/src/Groups.jl @@ -158,21 +158,22 @@ function power_by_squaring{T}(x::GWord{T}, p::Integer) elseif p == 2 return x*x end + x = deepcopy(x) t = trailing_zeros(p) + 1 p >>= t while (t -= 1) > 0 - x *= x + r_multiply!(x, x.symbols) end - y = x + y = deepcopy(x) while p > 0 t = trailing_zeros(p) + 1 p >>= t while (t -= 1) >= 0 - x *= x + r_multiply!(x, x.symbols) end - y *= x + r_multiply!(y, x.symbols) end - return freegroup_reduce!(y) + return Groups.freegroup_reduce!(y) end (^)(x::GWord, n::Integer) = power_by_squaring(x,n)