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-05-11 18:10:46 +02:00
|
|
|
###############################################################################
|
|
|
|
#
|
|
|
|
# ParentType / ObjectType definition
|
|
|
|
#
|
|
|
|
###############################################################################
|
2017-01-23 16:45:24 +01:00
|
|
|
|
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
|
|
|
|
|
2017-05-11 17:59:37 +02:00
|
|
|
doc"""
|
|
|
|
W::GWord{T<:GSymbol} <:GroupElem
|
|
|
|
> Basic representation of element of a finitely presented group. `W.symbols`
|
|
|
|
> fieldname contains particular group symbols which multiplied constitute a
|
|
|
|
> group element, i.e. a word in generators.
|
|
|
|
> As reduction (inside group) of such word may be time consuming we provide
|
|
|
|
> `savedhash` and `modified` fields as well:
|
|
|
|
> hash (used e.g. in the `unique` function) is calculated by reducing the word,
|
|
|
|
> setting `modified` flag to `false` and computing the hash which is stored in
|
|
|
|
> `savedhash` field.
|
|
|
|
> whenever word `W` is changed `W.modified` is set to `false`;
|
|
|
|
> Future comparisons don't perform reduction (and use `savedhash`) as long as
|
|
|
|
> `modified` flag remains `false`.
|
2017-01-23 16:53:33 +01:00
|
|
|
|
2017-05-11 17:59:37 +02:00
|
|
|
"""
|
2017-01-23 16:53:33 +01:00
|
|
|
|
2017-05-11 17:59:37 +02:00
|
|
|
type GWord{T<:GSymbol} <: GroupElem
|
2017-01-23 16:53:33 +01:00
|
|
|
symbols::Vector{T}
|
|
|
|
savedhash::UInt
|
|
|
|
modified::Bool
|
2017-05-11 17:59:37 +02:00
|
|
|
parent::Group
|
2017-01-23 16:53:33 +01:00
|
|
|
|
|
|
|
function GWord(symbols::Vector{T})
|
|
|
|
return new(symbols, hash(symbols), true)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-05-11 18:08:18 +02:00
|
|
|
export GSymbol, GWord
|
2017-01-23 16:53:33 +01:00
|
|
|
|
2017-05-11 18:10:46 +02:00
|
|
|
###############################################################################
|
|
|
|
#
|
|
|
|
# Type and parent object methods
|
|
|
|
#
|
|
|
|
###############################################################################
|
2017-01-23 16:53:33 +01:00
|
|
|
|
2017-05-11 18:01:55 +02:00
|
|
|
parent{T<:GSymbol}(w::GWord{T}) = w.parent
|
|
|
|
|
2017-05-11 18:10:46 +02:00
|
|
|
###############################################################################
|
|
|
|
#
|
|
|
|
# ParentType / ObjectType constructors
|
|
|
|
#
|
|
|
|
###############################################################################
|
|
|
|
|
2017-05-11 18:01:55 +02:00
|
|
|
GWord{T<:GSymbol}(s::T) = GWord{T}(T[s])
|
|
|
|
convert{T<:GSymbol}(::Type{GWord{T}}, s::T) = GWord{T}(T[s])
|
2017-01-23 16:53:33 +01:00
|
|
|
|
2017-05-11 18:10:46 +02:00
|
|
|
###############################################################################
|
|
|
|
#
|
|
|
|
# Basic manipulation
|
|
|
|
#
|
|
|
|
###############################################################################
|
2017-01-23 16:53:33 +01:00
|
|
|
|
2017-05-11 18:00:46 +02:00
|
|
|
function hash(W::GWord, h::UInt)
|
|
|
|
W.modified && reduce!(W)
|
|
|
|
return W.savedhash $ h
|
|
|
|
end
|
2017-05-11 18:03:06 +02:00
|
|
|
|
|
|
|
function deepcopy_internal{T<:GSymbol}(W::GWord{T}, dict::ObjectIdDict)
|
|
|
|
G = parent(W)
|
|
|
|
return G(GWord{T}(deepcopy(W.symbols)))
|
2017-01-23 16:53:33 +01:00
|
|
|
end
|
2017-05-11 18:03:06 +02:00
|
|
|
|
2017-05-12 19:46:59 +02:00
|
|
|
isone{T<:GSymbol}(s::T) = s.pow == 0
|
|
|
|
|
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
|
2017-05-11 18:09:09 +02:00
|
|
|
if W.symbols[i].str == W.symbols[i+1].str
|
2017-01-23 16:53:33 +01:00
|
|
|
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-05-11 18:03:46 +02:00
|
|
|
function reduce!(W::GWord)
|
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
|
2017-05-11 18:03:46 +02:00
|
|
|
W.savedhash = hash(W.symbols, hash(typeof(W)))
|
2017-01-23 16:53:33 +01:00
|
|
|
return W
|
|
|
|
end
|
|
|
|
|
2017-05-11 18:03:46 +02:00
|
|
|
doc"""
|
|
|
|
reduce(W::GWord)
|
|
|
|
> performs reduction/simplification of a group element (word in generators).
|
|
|
|
> The default reduction is the free group reduction, i.e. consists of
|
|
|
|
> multiplying adjacent symbols with the same `str` identifier and deleting the
|
|
|
|
> identity elements from `W.symbols`.
|
|
|
|
> More specific procedures should be dispatched on `GWord`s type parameter.
|
2017-01-23 16:53:33 +01:00
|
|
|
|
2017-05-11 18:03:46 +02:00
|
|
|
"""
|
|
|
|
reduce(W::GWord) = reduce!(deepcopy(W))
|
2017-01-23 16:53:33 +01:00
|
|
|
|
2017-05-11 18:10:46 +02:00
|
|
|
###############################################################################
|
|
|
|
#
|
|
|
|
# String I/O
|
|
|
|
#
|
|
|
|
###############################################################################
|
2017-01-23 16:53:33 +01:00
|
|
|
|
2017-05-11 18:10:46 +02:00
|
|
|
doc"""
|
|
|
|
show(io::IO, W::GWord)
|
|
|
|
> The actual string produced by show depends on the eltype of W.symbols.
|
2017-01-23 16:53:33 +01:00
|
|
|
|
2017-05-11 18:10:46 +02:00
|
|
|
"""
|
2017-01-23 16:53:33 +01:00
|
|
|
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
|
|
|
|
|
2017-05-11 18:10:46 +02:00
|
|
|
###############################################################################
|
|
|
|
#
|
|
|
|
# Comparison
|
|
|
|
#
|
|
|
|
###############################################################################
|
|
|
|
|
2017-05-11 18:05:03 +02:00
|
|
|
function (==)(W::GWord, Z::GWord)
|
|
|
|
parent(W) == parent(Z) || return false
|
|
|
|
W.modified && reduce!(W) # reduce clears the flag and calculates savedhash
|
|
|
|
Z.modified && reduce!(Z)
|
|
|
|
return W.savedhash == Z.savedhash && W.symbols == Z.symbols
|
|
|
|
end
|
|
|
|
|
2017-05-11 18:10:46 +02:00
|
|
|
###############################################################################
|
|
|
|
#
|
|
|
|
# Binary operators
|
|
|
|
#
|
|
|
|
###############################################################################
|
|
|
|
|
2017-01-23 16:53:33 +01:00
|
|
|
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
|
|
|
|
|
2017-05-12 19:46:08 +02:00
|
|
|
function l_multiply!(W::GWord, x; reduced=true)
|
2017-01-23 16:53:33 +01:00
|
|
|
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
|
|
|
|
|
2017-05-12 19:46:08 +02:00
|
|
|
r_multiply(W::GWord, x; reduced=true) =
|
2017-01-23 16:53:33 +01:00
|
|
|
r_multiply!(deepcopy(W),x, reduced=reduced)
|
2017-05-12 19:46:08 +02:00
|
|
|
l_multiply(W::GWord, x; reduced=true) =
|
2017-01-23 16:53:33 +01:00
|
|
|
l_multiply!(deepcopy(W),x, reduced=reduced)
|
|
|
|
|
2017-05-11 18:07:57 +02:00
|
|
|
(*)(W::GWord, Z::GWord) = r_multiply(W, Z.symbols)
|
|
|
|
(*)(W::GWord, s::GSymbol) = r_multiply(W, [s])
|
|
|
|
(*)(s::GSymbol, W::GWord) = l_multiply(W, [s])
|
2017-01-23 16:53:33 +01:00
|
|
|
|
2017-05-11 18:07:57 +02:00
|
|
|
function power_by_squaring(W::GWord, p::Integer)
|
2017-01-23 16:53:33 +01:00
|
|
|
if p < 0
|
2017-05-11 18:07:57 +02:00
|
|
|
return power_by_squaring(inv(W), -p)
|
2017-01-23 16:53:33 +01:00
|
|
|
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-05-11 18:07:57 +02:00
|
|
|
return W
|
2017-01-23 16:53:33 +01:00
|
|
|
elseif p == 2
|
2017-05-11 18:07:57 +02:00
|
|
|
return W*W
|
2017-01-23 16:53:33 +01:00
|
|
|
end
|
2017-05-11 18:07:57 +02:00
|
|
|
W = deepcopy(W)
|
2017-01-23 16:53:33 +01:00
|
|
|
t = trailing_zeros(p) + 1
|
|
|
|
p >>= t
|
|
|
|
while (t -= 1) > 0
|
2017-05-11 18:07:57 +02:00
|
|
|
r_multiply!(W, W.symbols)
|
2017-01-23 16:53:33 +01:00
|
|
|
end
|
2017-05-11 18:07:57 +02:00
|
|
|
Z = deepcopy(W)
|
2017-01-23 16:53:33 +01:00
|
|
|
while p > 0
|
|
|
|
t = trailing_zeros(p) + 1
|
|
|
|
p >>= t
|
|
|
|
while (t -= 1) >= 0
|
2017-05-11 18:07:57 +02:00
|
|
|
r_multiply!(W, W.symbols)
|
2017-01-23 16:53:33 +01:00
|
|
|
end
|
2017-05-11 18:07:57 +02:00
|
|
|
r_multiply!(Z, W.symbols)
|
2017-01-23 16:53:33 +01:00
|
|
|
end
|
2017-05-11 18:07:57 +02:00
|
|
|
return Z
|
2017-01-23 16:53:33 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
(^)(x::GWord, n::Integer) = power_by_squaring(x,n)
|
2017-05-11 17:58:19 +02:00
|
|
|
|
2017-05-11 18:10:46 +02:00
|
|
|
###############################################################################
|
|
|
|
#
|
|
|
|
# Inversion
|
|
|
|
#
|
2017-05-11 17:58:19 +02:00
|
|
|
###############################################################################
|
2017-01-23 16:53:33 +01:00
|
|
|
|
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-05-11 18:10:46 +02:00
|
|
|
###############################################################################
|
|
|
|
#
|
|
|
|
# Replacement of symbols / sub-words
|
|
|
|
#
|
|
|
|
###############################################################################
|
|
|
|
|
2017-01-25 10:57:48 +01:00
|
|
|
is_subsymbol(s::GSymbol, t::GSymbol) =
|
2017-05-11 18:09:09 +02:00
|
|
|
s.str == t.str && (0 ≤ s.pow ≤ t.pow || 0 ≥ s.pow ≥ t.pow)
|
2017-01-25 10:57:48 +01:00
|
|
|
|
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
|
|
|
|
2017-05-11 18:09:55 +02:00
|
|
|
function replace!(W::GWord, index, toreplace::GWord, replacement::GWord; check=true)
|
2017-01-25 11:32:12 +01:00
|
|
|
n = length(toreplace.symbols)
|
2017-05-11 18:09:55 +02:00
|
|
|
if check
|
2017-01-25 11:32:12 +01:00
|
|
|
@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
|
|
|
|
|
2017-05-11 18:09:39 +02:00
|
|
|
first = change_pow(W.symbols[index],
|
|
|
|
W.symbols[index].pow - toreplace.symbols[1].pow)
|
|
|
|
|
|
|
|
last = change_pow(W.symbols[index+n-1],
|
|
|
|
W.symbols[index+n-1].pow - toreplace.symbols[end].pow)
|
|
|
|
|
|
|
|
replacement = first * replacement * last
|
2017-01-25 11:32:12 +01:00
|
|
|
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-05-11 18:10:46 +02:00
|
|
|
###############################################################################
|
|
|
|
#
|
|
|
|
# Includes
|
|
|
|
#
|
|
|
|
###############################################################################
|
|
|
|
|
2017-05-11 18:30:03 +02:00
|
|
|
include("FPGroups.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
|