slightly more performant version of power_by_squaring
This commit is contained in:
parent
0882b740d3
commit
9e435188e1
|
@ -158,21 +158,22 @@ function power_by_squaring{T}(x::GWord{T}, p::Integer)
|
||||||
elseif p == 2
|
elseif p == 2
|
||||||
return x*x
|
return x*x
|
||||||
end
|
end
|
||||||
|
x = deepcopy(x)
|
||||||
t = trailing_zeros(p) + 1
|
t = trailing_zeros(p) + 1
|
||||||
p >>= t
|
p >>= t
|
||||||
while (t -= 1) > 0
|
while (t -= 1) > 0
|
||||||
x *= x
|
r_multiply!(x, x.symbols)
|
||||||
end
|
end
|
||||||
y = x
|
y = deepcopy(x)
|
||||||
while p > 0
|
while p > 0
|
||||||
t = trailing_zeros(p) + 1
|
t = trailing_zeros(p) + 1
|
||||||
p >>= t
|
p >>= t
|
||||||
while (t -= 1) >= 0
|
while (t -= 1) >= 0
|
||||||
x *= x
|
r_multiply!(x, x.symbols)
|
||||||
end
|
end
|
||||||
y *= x
|
r_multiply!(y, x.symbols)
|
||||||
end
|
end
|
||||||
return freegroup_reduce!(y)
|
return Groups.freegroup_reduce!(y)
|
||||||
end
|
end
|
||||||
|
|
||||||
(^)(x::GWord, n::Integer) = power_by_squaring(x,n)
|
(^)(x::GWord, n::Integer) = power_by_squaring(x,n)
|
||||||
|
|
Loading…
Reference in New Issue