Groups.jl/src/Groups.jl

510 lines
14 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
2020-03-25 03:33:43 +01:00
Base.one(G::Generic.PermGroup) = Generic.Perm(G.n)
Base.one(r::NCRingElem) = one(parent(r))
2019-11-14 09:21:11 +01:00
2017-05-11 18:10:46 +02:00
###############################################################################
#
# ParentType / ObjectType definition
#
2020-03-25 03:33:43 +01:00
abstract type AbstractFPGroup <: Group end
function Base.one(G::Gr) where Gr <: AbstractFPGroup
El = elem_type(G)
id = El(eltype(El)[])
id.parent = G
return id
end
elem_type(G::Gr) where Gr <:AbstractFPGroup = elem_type(Gr) # fallback 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
Base.iterate(s::GS, i=1) where GS<:GSymbol = i <= abs(s.pow) ? (GS(s.id, sign(s.pow)), i+1) : nothing
Base.length(s::GSymbol) = abs(s.pow)
Base.size(s::GSymbol) = (length(s), )
Base.eltype(s::GS) where GS<:GSymbol = GS
Base.isone(s::GSymbol) = iszero(s.pow)
change_pow(s::S, n::Integer) where S<:GSymbol = S(s.id, n)
Base.inv(s::GSymbol) = change_pow(s, -s.pow)
hash(s::S, h::UInt) where S<:GSymbol = hash(s.id, hash(s.pow, hash(S, h)))
abstract type GWord{T<:GSymbol} <: GroupElem end
2020-03-25 03:33:43 +01:00
# fallback definitions
Base.eltype(w::GW) where GW<:GWord = eltype(GW)
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}
2020-03-24 23:53:29 +01:00
symbols::Vector{T}
modified::Bool
savedhash::UInt
parent::Group
2020-03-24 23:53:29 +01:00
function GroupWord{T}(symbols::Vector{T}) where {T}
return new{T}(symbols, true, zero(UInt))
end
2017-01-23 16:53:33 +01:00
end
2020-03-24 23:53:29 +01:00
syllablelength(w::GWord) = length(w.symbols)
syllables(w::GWord) = w.symbols
ismodified(w::GWord) = w.modified
setmodified!(w::GWord) = (w.modified = true; w)
unsetmodified!(w::GWord) = (w.modified = false; w)
2020-03-25 03:33:43 +01:00
Base.one(w::GWord) = one(parent(w))
2020-03-24 23:53:29 +01:00
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
function hash_internal(W::GWord)
reduce!(W)
return hash(syllables(W), hash(typeof(W), hash(parent(W))))
end
2017-05-11 18:00:46 +02:00
function hash(W::GWord, h::UInt)
if ismodified(W)
W.savedhash = hash_internal(W)
unsetmodified!(W)
end
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)
2020-03-24 23:53:29 +01:00
return G(T(deepcopy(syllables(W))))
end
2020-03-24 23:55:42 +01:00
function freereduce!(::Type{Bool}, w::GWord)
2017-01-23 16:53:33 +01:00
reduced = true
2020-03-24 23:55:42 +01:00
for i in 1:syllablelength(w)-1
s, ns = syllables(w)[i], syllables(w)[i+1]
if isone(s)
continue
2020-03-24 23:55:42 +01:00
elseif s.id == ns.id
2017-01-23 16:53:33 +01:00
reduced = false
2020-03-24 23:55:42 +01:00
setmodified!(w)
p1 = s.pow
p2 = ns.pow
2020-03-24 23:55:42 +01:00
syllables(w)[i+1] = change_pow(s, p1 + p2)
syllables(w)[i] = change_pow(s, 0)
2017-01-23 16:53:33 +01:00
end
end
2020-03-24 23:53:29 +01:00
filter!(!isone, syllables(w))
2017-01-23 16:53:33 +01:00
return reduced
end
2020-03-24 23:55:42 +01:00
function freereduce!(w::GWord)
reduced = false
while !reduced
reduced = freereduce!(Bool, w)
2017-01-23 16:53:33 +01:00
end
2020-03-24 23:55:42 +01:00
return w
2017-01-23 16:53:33 +01:00
end
2020-03-24 23:56:30 +01:00
reduce!(w::GWord) = freereduce!(w)
2018-09-21 18:08:44 +02:00
@doc doc"""
2020-03-24 23:56:30 +01:00
reduce(w::GWord)
2017-05-11 18:03:46 +02:00
> performs reduction/simplification of a group element (word in generators).
2020-03-24 23:56:30 +01:00
> The default reduction is the free group reduction
2017-05-11 18:03:46 +02:00
> 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
"""
2020-03-24 23:56:30 +01: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
#
###############################################################################
2020-03-25 00:38:07 +01:00
function (==)(W::T, Z::T) where T <: GWord
parent(W) != parent(Z) && return false
hash(W) != hash(Z) && return false
return syllables(W) == syllables(Z)
end
function (==)(s::GSymbol, t::GSymbol)
2020-03-24 23:53:29 +01:00
isone(s) && isone(t) && return true
s.pow == t.pow && s.id == t.id && return true
return false
end
2017-05-11 18:10:46 +02:00
###############################################################################
#
# Binary operators
#
###############################################################################
function Base.append!(w::GWord{T}, v::AbstractVector{T}) where T
append!(syllables(w), v)
return w
2019-06-05 10:58:44 +02:00
end
function Base.prepend!(w::GWord{T}, v::AbstractVector{T}) where T
prepend!(syllables(w), v)
return w
end
Base.append!(w::T, v::T) where T <: GWord = append!(w, syllables(v))
Base.prepend!(w::T, v::T) where T <: GWord = prepend!(w, syllables(v))
for (mul, f) in ((:rmul!, :push!), (:lmul!, :pushfirst!))
@eval begin
function $mul(out::T, w::T, s::GSymbol) where T <:GWord
$f(syllables(out), s)
return freereduce!(out)
end
2017-01-23 16:53:33 +01:00
end
end
2020-03-25 03:36:36 +01:00
function rmul!(out::T, x::T, y::T) where T<: GWord
if out === x
out = deepcopy(out)
return freereduce!(append!(out, y))
elseif out === y
out = deepcopy(out)
return freereduce!(prepend!(out, x))
else
slenx = syllablelength(x)
sleny = syllablelength(y)
resize!(syllables(out), slenx+sleny)
syllables(out)[1:slenx] .= syllables(x)
syllables(out)[slenx+1:slenx+sleny] .= syllables(y)
return freereduce!(out)
2017-01-23 16:53:33 +01:00
end
end
2020-03-25 03:36:36 +01:00
lmul!(out::T, x::T, y::T) where T <: GWord = rmul!(out, y, x)
2017-01-23 16:53:33 +01:00
2020-03-25 03:36:36 +01:00
function AbstractAlgebra.mul!(out::T, x::T, y::T) where T <: GWord
return rmul!(out, x, y)
end
(*)(W::GW, Z::GW) where GW <: GWord = rmul!(deepcopy(W), W, Z)
(*)(W::GWord, s::GSymbol) = rmul!(deepcopy(W), W, s)
(*)(s::GSymbol, W::GWord) = lmul!(deepcopy(W), 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
2019-11-14 09:21:11 +01:00
return one(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
2020-03-25 03:36:36 +01:00
append!(W, W)
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
2020-03-25 03:36:36 +01:00
append!(W, W)
2017-01-23 16:53:33 +01:00
end
2020-03-25 03:36:36 +01:00
append!(Z, W)
2017-01-23 16:53:33 +01:00
end
2020-03-25 03:36:36 +01:00
return freereduce!(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
2020-03-24 23:53:29 +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)
2020-03-24 23:53:29 +01:00
w = T([inv(s) for s in Iterators.reverse(syllables(W))])
2017-05-11 17:43:33 +02:00
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
2020-03-25 03:38:33 +01:00
function issubsymbol(s::FreeSymbol, w::GWord, sindex::Integer)
@boundscheck 1 sindex syllablelength(w) || throw(BoundsError(w, sindex))
return issubsymbol(s, syllables(w)[sindex])
end
function issubword(z::GWord, w::GWord, sindex::Integer)
isempty(z) && return true
@boundscheck 1 sindex syllablelength(w) || throw(BoundsError(w, sindex))
n = syllablelength(z)
n == 1 && return issubsymbol(first(syllables(z)), syllables(w)[sindex])
lastindex = sindex + n - 1
lastindex > syllablelength(w) && return false
issubsymbol(first(z), syllables(w)[sindex]) || return false
issubsymbol(syllables(z)[end], syllables(w)[lastindex]) || return false
for (zidx, widx) in zip(2:n-1, sindex+1:lastindex-1)
syllables(z)[zidx] == syllables(w)[widx] || return false
end
return true
end
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-11-14 09:21:11 +01:00
function generate_balls(S::AbstractVector{T}, Id::T=one(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
function generate_balls(S::AbstractVector{T}, Id::T=one(parent(first(S)));
radius=2, op=*) where {T<:NCRingElem}
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