2017-01-23 16:45:24 +01:00
|
|
|
module Groups
|
|
|
|
|
2017-05-11 17:34:07 +02:00
|
|
|
using Nemo
|
|
|
|
import Nemo: Group, parent
|
|
|
|
|
2017-01-25 12:18:01 +01:00
|
|
|
import Base: length, ==, hash, show, convert
|
2017-05-11 17:57:18 +02:00
|
|
|
import Base: inv, reduce, *, ^
|
2017-01-26 12:48:32 +01:00
|
|
|
import Base: findfirst, findnext
|
2017-05-11 17:34:07 +02:00
|
|
|
import Base: deepcopy_internal
|
|
|
|
|
2017-01-23 16:45:24 +01:00
|
|
|
|
2017-01-23 16:53:33 +01:00
|
|
|
export GSymbol, GWord
|
|
|
|
|
2017-05-11 17:36:59 +02:00
|
|
|
doc"""
|
|
|
|
::GSymbol
|
|
|
|
> Abstract type which all group symbols of FPGroups should subtype. Each
|
|
|
|
> concrete subtype should implement fields:
|
|
|
|
> * `str` which is the string representation/identification of a symbol
|
|
|
|
> * `pow` which is the (multiplicative) exponent of a symbol.
|
|
|
|
|
|
|
|
"""
|
2017-01-23 16:53:33 +01:00
|
|
|
abstract GSymbol
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
symbols::Vector{T}
|
|
|
|
savedhash::UInt
|
|
|
|
modified::Bool
|
|
|
|
|
|
|
|
function GWord(symbols::Vector{T})
|
|
|
|
return new(symbols, hash(symbols), true)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
GWord{T<:GSymbol}(s::T) = GWord{T}([s])
|
2017-01-25 12:18:01 +01:00
|
|
|
convert{T<:GSymbol, W<:Word}(::Type{W}, s::T) = GWord{T}(s)
|
2017-01-23 16:53:33 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
end
|
2017-05-11 17:49:56 +02:00
|
|
|
length(W::GWord) = sum([length(s) for s in W.symbols])
|
2017-01-23 16:53:33 +01:00
|
|
|
|
2017-05-11 17:51:16 +02:00
|
|
|
function free_reduce!(W::GWord)
|
2017-01-23 16:53:33 +01:00
|
|
|
reduced = true
|
|
|
|
for i in 1:length(W.symbols) - 1
|
|
|
|
if W.symbols[i].gen == W.symbols[i+1].gen
|
|
|
|
reduced = false
|
|
|
|
p1 = W.symbols[i].pow
|
|
|
|
p2 = W.symbols[i+1].pow
|
|
|
|
W.symbols[i+1] = change_pow(W.symbols[i], p1 + p2)
|
2017-05-11 17:57:18 +02:00
|
|
|
W.symbols[i] = change_pow(W.symbols[i], 0)
|
2017-01-23 16:53:33 +01:00
|
|
|
end
|
|
|
|
end
|
2017-05-11 17:52:14 +02:00
|
|
|
deleteat!(W.symbols, find(x -> x.pow == 0, W.symbols))
|
2017-01-23 16:53:33 +01:00
|
|
|
return reduced
|
|
|
|
end
|
|
|
|
|
2017-01-31 16:56:49 +01:00
|
|
|
function reduce!{T}(W::GWord{T})
|
2017-01-23 16:53:33 +01:00
|
|
|
if length(W) < 2
|
|
|
|
deleteat!(W.symbols, find(x -> x.pow == 0, W.symbols))
|
|
|
|
else
|
|
|
|
reduced = false
|
|
|
|
while !reduced
|
2017-05-11 17:51:16 +02:00
|
|
|
reduced = free_reduce!(W)
|
2017-01-23 16:53:33 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
W.modified = false
|
|
|
|
W.savedhash = hash(W.symbols,hash(typeof(W)))
|
|
|
|
return W
|
|
|
|
end
|
|
|
|
|
2017-01-31 16:56:49 +01:00
|
|
|
reduce(W::GWord) = reduce!(deepcopy(W))
|
2017-01-23 16:53:33 +01:00
|
|
|
|
2017-01-25 15:40:50 +01:00
|
|
|
function hash{T}(W::GWord{T}, h::UInt)
|
2017-01-31 16:56:49 +01:00
|
|
|
W.modified && reduce!(W)
|
2017-01-25 15:57:25 +01:00
|
|
|
return W.savedhash + h
|
2017-01-25 15:40:50 +01:00
|
|
|
end
|
2017-01-23 16:53:33 +01:00
|
|
|
|
|
|
|
function (==){T}(W::GWord{T}, Z::GWord{T})
|
2017-01-31 16:56:49 +01:00
|
|
|
W.modified && reduce!(W) # reduce clears the flag and recalculate the hash
|
|
|
|
Z.modified && reduce!(Z)
|
2017-01-23 16:53:33 +01:00
|
|
|
return W.savedhash == Z.savedhash && W.symbols == Z.symbols
|
|
|
|
end
|
|
|
|
|
|
|
|
(==){T}(W::GWord{T}, s::T) = W == GWord(s)
|
|
|
|
(==){T}(s::T, W::GWord{T}) = W == GWord(s)
|
|
|
|
|
|
|
|
function show(io::IO, W::GWord)
|
|
|
|
if length(W) == 0
|
|
|
|
print(io, "(id)")
|
|
|
|
else
|
|
|
|
join(io, [string(s) for s in W.symbols], "*")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function r_multiply!(W::GWord, x; reduced::Bool=true)
|
|
|
|
if length(x) > 0
|
|
|
|
push!(W.symbols, x...)
|
|
|
|
end
|
|
|
|
if reduced
|
2017-01-31 16:56:49 +01:00
|
|
|
reduce!(W)
|
2017-01-23 16:53:33 +01:00
|
|
|
end
|
|
|
|
return W
|
|
|
|
end
|
|
|
|
|
|
|
|
function l_multiply!(W::GWord, x; reduced::Bool=true)
|
|
|
|
if length(x) > 0
|
|
|
|
unshift!(W.symbols, reverse(x)...)
|
|
|
|
end
|
|
|
|
if reduced
|
2017-01-31 16:56:49 +01:00
|
|
|
reduce!(W)
|
2017-01-23 16:53:33 +01:00
|
|
|
end
|
|
|
|
return W
|
|
|
|
end
|
|
|
|
|
|
|
|
r_multiply(W::GWord, x; reduced::Bool=true) =
|
|
|
|
r_multiply!(deepcopy(W),x, reduced=reduced)
|
|
|
|
l_multiply(W::GWord, x; reduced::Bool=true) =
|
|
|
|
l_multiply!(deepcopy(W),x, reduced=reduced)
|
|
|
|
|
|
|
|
(*){T}(W::GWord{T}, Z::GWord{T}) = r_multiply(W, Z.symbols)
|
|
|
|
(*)(W::GWord, s::GSymbol) = W*GWord(s)
|
|
|
|
(*)(s::GSymbol, W::GWord) = GWord(s)*W
|
|
|
|
|
|
|
|
function power_by_squaring{T}(x::GWord{T}, p::Integer)
|
|
|
|
if p < 0
|
|
|
|
return power_by_squaring(inv(x), -p)
|
|
|
|
elseif p == 0
|
2017-05-11 17:57:18 +02:00
|
|
|
return parent(W)()
|
2017-01-23 16:53:33 +01:00
|
|
|
elseif p == 1
|
2017-01-31 11:22:40 +01:00
|
|
|
return x
|
2017-01-23 16:53:33 +01:00
|
|
|
elseif p == 2
|
|
|
|
return x*x
|
|
|
|
end
|
2017-01-31 14:53:41 +01:00
|
|
|
x = deepcopy(x)
|
2017-01-23 16:53:33 +01:00
|
|
|
t = trailing_zeros(p) + 1
|
|
|
|
p >>= t
|
|
|
|
while (t -= 1) > 0
|
2017-01-31 14:53:41 +01:00
|
|
|
r_multiply!(x, x.symbols)
|
2017-01-23 16:53:33 +01:00
|
|
|
end
|
2017-01-31 14:53:41 +01:00
|
|
|
y = deepcopy(x)
|
2017-01-23 16:53:33 +01:00
|
|
|
while p > 0
|
|
|
|
t = trailing_zeros(p) + 1
|
|
|
|
p >>= t
|
|
|
|
while (t -= 1) >= 0
|
2017-01-31 14:53:41 +01:00
|
|
|
r_multiply!(x, x.symbols)
|
2017-01-23 16:53:33 +01:00
|
|
|
end
|
2017-01-31 14:53:41 +01:00
|
|
|
r_multiply!(y, x.symbols)
|
2017-01-23 16:53:33 +01:00
|
|
|
end
|
2017-01-31 16:54:03 +01:00
|
|
|
return y
|
2017-01-23 16:53:33 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
(^)(x::GWord, n::Integer) = power_by_squaring(x,n)
|
|
|
|
(^){T<:GSymbol}(x::T, n::Integer) = GWord(x)^n
|
|
|
|
|
2017-05-11 17:43:33 +02:00
|
|
|
function inv{T}(W::GWord{T})
|
|
|
|
if length(W) == 0
|
|
|
|
return W
|
|
|
|
else
|
|
|
|
G = parent(W)
|
|
|
|
w = GWord{T}(reverse([inv(s) for s in W.symbols]))
|
|
|
|
w.modified = true
|
|
|
|
return G(w)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-01-25 10:57:48 +01:00
|
|
|
is_subsymbol(s::GSymbol, t::GSymbol) =
|
|
|
|
s.gen == t.gen && (0 ≤ s.pow ≤ t.pow || 0 ≥ s.pow ≥ t.pow)
|
|
|
|
|
2017-01-26 12:48:32 +01:00
|
|
|
function findfirst(W::GWord, Z::GWord)
|
2017-01-25 11:30:46 +01:00
|
|
|
n = length(Z.symbols)
|
|
|
|
|
|
|
|
@assert n > 1
|
2017-01-26 12:48:32 +01:00
|
|
|
for (idx,a) in enumerate(W.symbols)
|
2017-01-25 11:30:46 +01:00
|
|
|
if idx + n - 1 > length(W.symbols)
|
|
|
|
break
|
|
|
|
end
|
|
|
|
first = is_subsymbol(Z.symbols[1],a)
|
|
|
|
if first
|
2017-01-26 12:48:32 +01:00
|
|
|
middle = W.symbols[idx+1:idx+n-2] == Z.symbols[2:end-1]
|
|
|
|
last = is_subsymbol(Z.symbols[end], W.symbols[idx+n-1])
|
2017-01-25 11:30:46 +01:00
|
|
|
if middle && last
|
2017-01-26 12:48:32 +01:00
|
|
|
return idx
|
2017-01-25 11:30:46 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return 0
|
|
|
|
end
|
|
|
|
|
2017-01-26 12:48:32 +01:00
|
|
|
function findnext(W::GWord, Z::GWord, i::Integer)
|
|
|
|
t = findfirst(GWord{eltype(W.symbols)}(W.symbols[i:end]), Z)
|
|
|
|
if t > 0
|
|
|
|
return t+i-1
|
|
|
|
else
|
|
|
|
return 0
|
|
|
|
end
|
|
|
|
end
|
2017-01-25 11:32:12 +01:00
|
|
|
|
|
|
|
function replace!(W::GWord, index, toreplace::GWord, replacement::GWord; asserts=true)
|
|
|
|
n = length(toreplace.symbols)
|
|
|
|
if asserts
|
|
|
|
@assert is_subsymbol(toreplace.symbols[1], W.symbols[index])
|
|
|
|
@assert W.symbols[index+1:index+n-2] == toreplace.symbols[2:end-1]
|
|
|
|
@assert is_subsymbol(toreplace.symbols[end], W.symbols[index+n-1])
|
|
|
|
end
|
|
|
|
|
|
|
|
first = W.symbols[index]*inv(toreplace.symbols[1])
|
|
|
|
last = W.symbols[index+n-1]*inv(toreplace.symbols[end])
|
|
|
|
replacement = first*replacement*last
|
|
|
|
splice!(W.symbols, index:index+n-1, replacement.symbols)
|
2017-01-31 16:56:49 +01:00
|
|
|
return reduce!(W)
|
2017-01-25 11:32:12 +01:00
|
|
|
end
|
|
|
|
|
2017-01-26 12:50:54 +01:00
|
|
|
function replace(W::GWord, index, toreplace::GWord, replacement::GWord)
|
|
|
|
replace!(deepcopy(W), index, toreplace, replacement)
|
|
|
|
end
|
|
|
|
|
2017-01-25 11:32:12 +01:00
|
|
|
function replace_all!{T}(W::GWord{T}, subst_dict::Dict{GWord{T}, GWord{T}})
|
2017-01-26 12:50:02 +01:00
|
|
|
for toreplace in reverse!(sort!(collect(keys(subst_dict)),by=length))
|
2017-01-25 11:32:12 +01:00
|
|
|
replacement = subst_dict[toreplace]
|
|
|
|
i = findfirst(W, toreplace)
|
|
|
|
while i ≠ 0
|
|
|
|
replace!(W,i,toreplace, replacement)
|
2017-01-26 12:51:48 +01:00
|
|
|
i = findnext(W, toreplace, i)
|
2017-01-25 11:32:12 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
return W
|
|
|
|
end
|
|
|
|
|
2017-01-26 12:50:54 +01:00
|
|
|
replace_all(W::GWord, subst_dict::Dict{GWord, GWord}) = replace_all!(deepcopy(W), subst_dict)
|
|
|
|
|
2017-01-23 16:53:33 +01:00
|
|
|
include("free_groups.jl")
|
2017-01-23 19:46:41 +01:00
|
|
|
include("automorphism_groups.jl")
|
2017-01-23 16:53:33 +01:00
|
|
|
|
|
|
|
end # of module Groups
|