Groups.jl/src/Groups.jl

452 lines
12 KiB
Julia
Raw Normal View History

module Groups
2018-07-30 08:30:27 +02:00
using AbstractAlgebra
import AbstractAlgebra: Group, GroupElem, Ring
import AbstractAlgebra: parent, parent_type, elem_type
import AbstractAlgebra: order, gens, matrix_repr
2017-05-11 17:34:07 +02:00
import Base: length, ==, hash, show, convert, eltype, iterate
2018-09-21 18:08:44 +02:00
import Base: inv, reduce, *, ^, power_by_squaring
2017-01-26 12:48:32 +01:00
import Base: findfirst, findnext
2017-05-11 17:34:07 +02:00
import Base: deepcopy_internal
export elements
2018-09-21 18:08:44 +02:00
using LinearAlgebra
using Markdown
2017-05-11 18:10:46 +02:00
###############################################################################
#
# ParentType / ObjectType definition
#
###############################################################################
2018-09-21 18:08:44 +02:00
@doc doc"""
2017-05-11 17:36:59 +02:00
::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:
2019-01-03 03:37:37 +01:00
> * `id` which is the `Symbol` representation/identification of a symbol
2017-05-11 17:36:59 +02:00
> * `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
abstract type GWord{T<:GSymbol} <:GroupElem end
2018-09-21 18:08:44 +02:00
@doc doc"""
W::GroupWord{T} <: GWord{T<:GSymbol} <:GroupElem
2017-05-11 17:59:37 +02:00
> 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
"""
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
2018-03-25 23:55:16 +02:00
###############################################################################
#
# Includes
#
###############################################################################
include("FreeGroup.jl")
include("FPGroups.jl")
include("AutGroup.jl")
2019-01-02 10:30:25 +01:00
include("DirectPower.jl")
2018-03-25 23:55:16 +02:00
include("WreathProducts.jl")
2017-05-11 18:10:46 +02:00
###############################################################################
#
# Type and parent object methods
#
###############################################################################
2017-01-23 16:53:33 +01:00
2018-09-21 18:08:44 +02:00
parent(w::GWord{T}) where {T<:GSymbol} = w.parent
2017-05-11 18:10:46 +02:00
###############################################################################
#
# ParentType / ObjectType constructors
#
###############################################################################
GroupWord(s::T) where {T<:GSymbol} = GroupWord{T}(T[s])
2018-09-21 18:09:13 +02:00
GroupWord{T}(s::T) where {T<:GSymbol} = GroupWord{T}(T[s])
GroupWord(w::GroupWord{T}) where {T<:GSymbol} = w
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)
2018-03-29 19:24:15 +02:00
W.modified && reduce!(W)
2018-03-28 12:28:47 +02:00
return xor(W.savedhash, h)
2017-05-11 18:00:46 +02:00
end
# WARNING: Due to specialised (constant) hash function of GWords this one is actually necessary!
2018-09-21 18:08:44 +02:00
function deepcopy_internal(W::T, dict::IdDict) where {T<:GWord}
G = parent(W)
return G(T(deepcopy(W.symbols)))
2017-01-23 16:53:33 +01:00
end
length(W::GWord) = sum([length(s) for s in W.symbols])
2017-01-23 16:53:33 +01:00
2018-04-02 18:14:08 +02:00
function deleteids!(W::GWord)
to_delete = Int[]
for i in 1:length(W.symbols)
if W.symbols[i].pow == 0
push!(to_delete, i)
end
end
deleteat!(W.symbols, to_delete)
end
2018-04-02 18:19:55 +02:00
function freereduce!(W::GWord)
2017-01-23 16:53:33 +01:00
reduced = true
for i in 1:length(W.symbols) - 1
if W.symbols[i].pow == 0
continue
2019-01-03 03:37:37 +01:00
elseif W.symbols[i].id == W.symbols[i+1].id
2017-01-23 16:53:33 +01:00
reduced = false
p1 = W.symbols[i].pow
p2 = W.symbols[i+1].pow
2017-01-23 16:53:33 +01:00
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
2018-04-02 18:14:08 +02:00
deleteids!(W)
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
2018-04-02 18:14:08 +02:00
deleteids!(W)
2017-01-23 16:53:33 +01:00
else
reduced = false
while !reduced
2018-04-02 18:19:55 +02:00
reduced = freereduce!(W)
2017-01-23 16:53:33 +01:00
end
end
2018-03-29 19:24:15 +02:00
W.savedhash = hash(W.symbols, hash(typeof(W), hash(parent(W), zero(UInt))))
W.modified = false
2017-01-23 16:53:33 +01:00
return W
end
2018-09-21 18:08:44 +02:00
@doc doc"""
2017-05-11 18:03:46 +02:00
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
2019-01-03 03:37:37 +01:00
> multiplying adjacent symbols with the same `id` identifier and deleting the
2017-05-11 18:03:46 +02:00
> 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
2018-09-21 18:08:44 +02:00
@doc doc"""
2017-07-06 09:05:24 +02:00
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
2018-09-21 18:08:44 +02:00
@doc doc"""
2017-05-11 18:10:46 +02:00
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}
2018-03-29 19:37:32 +02:00
if s.pow == 1
2019-01-03 03:37:37 +01:00
print(io, string(s.id))
2017-05-12 19:48:33 +02:00
else
2019-01-03 03:37:37 +01:00
print(io, string((s.id))*"^$(s.pow)")
2017-05-12 19:48:33 +02:00
end
end
2017-05-11 18:10:46 +02:00
###############################################################################
#
# Comparison
#
###############################################################################
function (==)(W::GWord, Z::GWord)
parent(W) == parent(Z) || return false
2018-03-28 12:28:47 +02:00
2018-03-29 19:24:15 +02:00
W.modified && reduce!(W)
Z.modified && reduce!(Z)
2018-03-28 12:28:47 +02:00
if W.savedhash != Z.savedhash
return false
end
return W.symbols == Z.symbols
end
function (==)(s::GSymbol, t::GSymbol)
s.pow == t.pow || return false
s.pow == 0 && return true
2019-01-03 03:37:37 +01:00
s.id == t.id || return false
return true
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
#
###############################################################################
2018-03-27 21:31:23 +02:00
issubsymbol(s::GSymbol, t::GSymbol) =
2019-01-03 03:37:37 +01:00
s.id == t.id && (0 s.pow t.pow || 0 s.pow t.pow)
2017-01-25 10:57:48 +01:00
2018-03-27 21:48:02 +02:00
"""doc
Find the first linear index k>=i such that Z < W.symbols[k:k+length(Z)-1]
"""
function findnext(W::GWord, Z::GWord, i::Int)
2017-01-25 11:30:46 +01:00
n = length(Z.symbols)
if n == 0
return 0
elseif n == 1
2018-09-21 18:08:44 +02:00
for idx in i:lastindex(W.symbols)
2018-03-27 21:48:02 +02:00
if issubsymbol(Z.symbols[1], W.symbols[idx])
return idx
end
end
return 0
else
2018-09-21 18:08:44 +02:00
for idx in i:lastindex(W.symbols) - n + 1
2018-03-27 21:48:02 +02:00
foundfirst = issubsymbol(Z.symbols[1], W.symbols[idx])
lastmatch = issubsymbol(Z.symbols[end], W.symbols[idx+n-1])
if foundfirst && lastmatch
# middles match:
if view(Z.symbols, 2:n-1) == view(W.symbols, idx+1:idx+n-2)
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
2018-03-27 21:48:02 +02:00
findfirst(W::GWord, Z::GWord) = findnext(W, Z, 1)
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
2018-03-27 21:31:23 +02:00
@assert issubsymbol(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
2018-03-27 21:31:23 +02:00
@assert issubsymbol(toreplace.symbols[1], W.symbols[index])
@assert W.symbols[index+1:index+n-2] == toreplace.symbols[2:end-1]
2018-03-27 21:31:23 +02:00
@assert issubsymbol(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
2018-03-27 21:48:36 +02:00
function replace_all!(W::T,subst_dict::Dict{T,T}) where {T<:GWord}
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
2018-03-27 21:48:36 +02:00
function replace_all(W::T, subst_dict::Dict{T,T}) where {T<:GWord}
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
#
###############################################################################
2019-01-09 16:43:44 +01:00
function generate_balls(S::Vector{T}, Id::T=parent(first(S))(); radius=2, op=*) where T<:GroupElem
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
2018-09-21 18:08:44 +02:00
function generate_balls(S::Vector{T}, Id::T=one(parent(first(S))); radius=2, op=*) where {T<:RingElem}
2017-11-08 11:09:29 +01:00
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
2018-09-21 19:10:28 +02:00
########### iteration for GFField
length(F::AbstractAlgebra.GFField) = order(F)
function iterate(F::AbstractAlgebra.GFField, s=0)
if s >= order(F)
return nothing
else
return F(s), s+1
end
end
eltype(::Type{AbstractAlgebra.GFField{I}}) where I = AbstractAlgebra.gfelem{I}
2017-01-23 16:53:33 +01:00
end # of module Groups