1
0
mirror of https://github.com/kalmarek/Groups.jl.git synced 2024-11-19 14:35:28 +01:00
Groups.jl/src/Groups.jl

418 lines
11 KiB
Julia
Raw Normal View History

2017-11-08 09:06:50 +01:00
__precompile__()
module Groups
2017-05-11 17:34:07 +02:00
using Nemo
import Nemo: Group, GroupElem, Ring
import Nemo: parent, parent_type, elem_type
2017-07-05 16:26:14 +02:00
import Nemo: elements, order, gens
2017-05-11 17:34:07 +02:00
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-05-11 17:36:59 +02:00
doc"""
::GSymbol
2017-07-06 09:11:56 +02:00
> Abstract type which all group symbols of AbstractFPGroups should subtype. Each
2017-05-11 17:36:59 +02:00
> 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-09-13 11:22:21 +02:00
abstract type GSymbol end
2017-01-23 16:53:33 +01:00
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
abstract type GWord{T<:GSymbol} <:GroupElem end
mutable struct GroupWord{T} <: GWord{T}
symbols::Vector{T}
savedhash::UInt
modified::Bool
parent::Group
function GroupWord{T}(symbols::Vector{T}) where {T}
return new{T}(symbols, hash(symbols), true)
end
2017-01-23 16:53:33 +01:00
end
2017-09-13 11:22:21 +02:00
abstract type AbstractFPGroup <: Group end
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
parent{T<:GSymbol}(w::GWord{T}) = w.parent
2017-05-11 18:10:46 +02:00
###############################################################################
#
# ParentType / ObjectType constructors
#
###############################################################################
GroupWord(s::T) where {T<:GSymbol} = GroupWord{T}(T[s])
convert(::Type{GroupWord{T}}, s::T) where {T<:GSymbol} = GroupWord{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)
2017-09-13 11:22:21 +02:00
res = xor(W.savedhash, h)
2017-07-06 09:22:56 +02:00
return res
2017-05-11 18:00:46 +02:00
end
function deepcopy_internal(W::T, dict::ObjectIdDict) where {T<:GWord}
G = parent(W)
return G(T(deepcopy(W.symbols)))
2017-01-23 16:53:33 +01:00
end
isone(s::GSymbol) = s.pow == 0
2017-05-12 19:46:59 +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
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-07-06 09:05:24 +02:00
doc"""
gens(G::AbstractFPGroups)
> returns vector of generators of `G`, as its elements.
"""
2017-07-06 09:11:27 +02:00
gens(G::AbstractFPGroup) = [G(g) for g in G.gens]
2017-07-05 16:26:14 +02: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)
2017-05-19 10:35:29 +02:00
> 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
function show(io::IO, s::T) where {T<:GSymbol}
2017-05-12 19:48:33 +02:00
if isone(s)
print(io, "(id)")
elseif s.pow == 1
print(io, s.str)
else
print(io, (s.str)*"^$(s.pow)")
end
end
2017-05-11 18:10:46 +02:00
###############################################################################
#
# Comparison
#
###############################################################################
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
append!(W.symbols, x)
2017-01-23 16:53:33 +01:00
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)
2017-01-23 16:53:33 +01:00
if length(x) > 0
prepend!(W.symbols, x)
2017-01-23 16:53:33 +01:00
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)
(*)(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
function power_by_squaring(W::GWord, p::Integer)
2017-01-23 16:53:33 +01:00
if p < 0
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
return W
2017-01-23 16:53:33 +01:00
elseif p == 2
return W*W
2017-01-23 16:53:33 +01:00
end
W = deepcopy(W)
2017-01-23 16:53:33 +01:00
t = trailing_zeros(p) + 1
p >>= t
while (t -= 1) > 0
r_multiply!(W, W.symbols)
2017-01-23 16:53:33 +01:00
end
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
r_multiply!(W, W.symbols)
2017-01-23 16:53:33 +01:00
end
r_multiply!(Z, W.symbols)
2017-01-23 16:53:33 +01:00
end
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
function inv(W::T) where {T<:GWord}
2017-05-11 17:43:33 +02:00
if length(W) == 0
return W
else
G = parent(W)
w = T(reverse([inv(s) for s in W.symbols]))
2017-05-11 17:43:33 +02:00
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)
if n == 0
return 0
elseif n == 1
for (i,s) in enumerate(W.symbols)
if is_subsymbol(Z.symbols[1], s)
return i
end
end
return 0
else
for (idx,a) in enumerate(W.symbols)
if idx + n - 1 > length(W.symbols)
2017-01-25 11:30:46 +01:00
break
end
foundfirst = is_subsymbol(Z.symbols[1], a)
if foundfirst
middlematch = W.symbols[idx+1:idx+n-2] == Z.symbols[2:end-1]
lastmatch = is_subsymbol(Z.symbols[end], W.symbols[idx+n-1])
if middlematch && lastmatch
return idx
2017-01-25 11:30:46 +01:00
end
end
end
end
return 0
2017-01-25 11:30:46 +01:00
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)
n = length(toreplace.symbols)
if n == 0
return reduce!(W)
2017-01-25 11:32:12 +01:00
elseif n == 1
if check
@assert is_subsymbol(toreplace.symbols[1], W.symbols[index])
end
first = change_pow(W.symbols[index],
W.symbols[index].pow - toreplace.symbols[1].pow)
last = change_pow(W.symbols[index], 0)
2017-05-11 18:09:39 +02:00
else
if check
@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 = change_pow(W.symbols[index],
W.symbols[index].pow - toreplace.symbols[1].pow)
last = change_pow(W.symbols[index+n-1],
2017-05-11 18:09:39 +02:00
W.symbols[index+n-1].pow - toreplace.symbols[end].pow)
end
2017-05-11 18:09:39 +02:00
replacement = first * replacement * last
splice!(W.symbols, index:index+n-1, replacement.symbols)
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
function replace_all!(W::GWord{T},subst_dict::Dict{GWord{T},GWord{T}}) where {T}
modified = false
2017-07-05 16:22:50 +02: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
modified = true
2017-01-25 11:32:12 +01:00
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 modified
2017-01-25 11:32:12 +01:00
end
function replace_all(W::GWord{T},subst_dict::Dict{GWord{T},GWord{T}}) where {T}
2017-07-06 09:19:15 +02:00
W = deepcopy(W)
replace_all!(W, subst_dict)
return W
end
2017-01-26 12:50:54 +01:00
###############################################################################
#
# Misc
#
###############################################################################
function generate_balls(S::Vector{T}, Id::T=parent(first(S))(); radius=2, op=*) where T<:GWord
sizes = Int[]
B = [Id]
for i in 1:radius
BB = [op(i,j) for (i,j) in Base.product(B,S)]
B = unique([B; vec(BB)])
push!(sizes, length(B))
end
return B, sizes
end
2017-11-08 11:09:29 +01:00
function generate_balls{T<:RingElem}(S::Vector{T}, Id::T=one(parent(first(S))); radius=2, op=*)
sizes = Int[]
B = [Id]
for i in 1:radius
2017-11-08 11:09:29 +01:00
BB = [op(i,j) for (i,j) in Base.product(B,S)]
B = unique([B; vec(BB)])
push!(sizes, length(B))
end
return B, sizes
end
2017-05-11 18:10:46 +02:00
###############################################################################
#
# Includes
#
###############################################################################
2017-05-12 20:15:03 +02:00
include("FreeGroup.jl")
2017-07-06 09:12:35 +02:00
include("FPGroups.jl")
2017-05-12 20:15:03 +02:00
include("AutGroup.jl")
2017-01-23 16:53:33 +01:00
include("DirectProducts.jl")
include("WreathProducts.jl")
2017-01-23 16:53:33 +01:00
end # of module Groups