Groups.jl/src/FreeGroup.jl

110 lines
2.9 KiB
Julia
Raw Normal View History

2017-05-11 18:28:00 +02:00
###############################################################################
#
# FreeSymbol/FreeGroupElem/FreeGroup definition
2017-05-11 18:28:00 +02:00
#
###############################################################################
2017-05-09 17:46:52 +02:00
2017-09-13 11:22:21 +02:00
struct FreeSymbol <: GSymbol
str::String
pow::Int
end
FreeGroupElem = GroupWord{FreeSymbol}
2017-05-09 17:46:52 +02:00
2017-09-13 11:22:21 +02:00
mutable struct FreeGroup <: AbstractFPGroup
gens::Vector{FreeSymbol}
function FreeGroup{T<:GSymbol}(gens::Vector{T})
G = new(gens)
2017-05-11 18:16:21 +02:00
G.gens = gens
return G
end
2017-05-09 17:46:52 +02:00
end
export FreeGroupElem, FreeGroup
2017-05-09 17:46:52 +02:00
2017-05-11 18:28:00 +02:00
###############################################################################
#
# Type and parent object methods
#
###############################################################################
2017-05-11 18:28:29 +02:00
elem_type(::Type{FreeGroup}) = FreeGroupElem
2017-05-11 18:17:42 +02:00
parent_type(::Type{FreeGroupElem}) = FreeGroup
2017-05-11 18:17:42 +02:00
2017-05-11 18:28:00 +02:00
###############################################################################
#
# FreeSymbol constructors
2017-05-11 18:28:00 +02:00
#
###############################################################################
2017-05-09 17:46:52 +02:00
FreeSymbol(s::String) = FreeSymbol(s,1)
2017-05-11 18:16:51 +02:00
2017-05-12 21:19:08 +02:00
FreeGroup(n::Int, symbol::String="f") = FreeGroup(["$symbol$i" for i in 1:n])
FreeGroup(a::Vector{String}) = FreeGroup([FreeSymbol(i) for i in a])
2017-05-11 18:16:51 +02:00
2017-05-11 18:28:00 +02:00
###############################################################################
#
# Parent object call overloads
#
###############################################################################
function (G::FreeGroup)()
2018-03-28 12:21:50 +02:00
id = FreeGroupElem(FreeSymbol[])
2017-05-11 18:18:06 +02:00
id.parent = G
return id
2017-05-09 17:46:52 +02:00
end
function (G::FreeGroup)(w::GroupWord{FreeSymbol})
2017-05-11 18:18:06 +02:00
if length(w) > 0
for s in w.symbols
i = findfirst(g -> g.str == s.str, G.gens)
i == 0 && throw("Symbol $s does not belong to $G.")
s.pow % G.gens[i].pow == 0 || throw("Symbol $s doesn't belong to $G.")
end
end
w.parent = G
return w
end
(G::FreeGroup)(s::FreeSymbol) = G(FreeGroupElem(s))
2017-05-11 18:18:06 +02:00
2017-05-11 18:28:00 +02:00
###############################################################################
#
# Basic manipulation
#
###############################################################################
2017-05-09 17:46:52 +02:00
hash(s::FreeSymbol, h::UInt) = hash(s.str, hash(s.pow, hash(FreeSymbol, h)))
change_pow(s::FreeSymbol, n::Int) = FreeSymbol(s.str, n)
length(s::FreeSymbol) = abs(s.pow)
2017-05-11 18:28:00 +02:00
###############################################################################
#
# String I/O
#
###############################################################################
function show(io::IO, G::FreeGroup)
2017-05-15 17:28:58 +02:00
print(io, "Free group on $(length(G.gens)) generators: ")
join(io, G.gens, ", ")
2017-05-09 17:46:52 +02:00
end
2017-05-11 18:28:00 +02:00
###############################################################################
#
# Comparison
#
###############################################################################
###############################################################################
#
# Inversion
#
###############################################################################
2017-05-11 18:22:37 +02:00
inv(s::FreeSymbol) = change_pow(s, -s.pow)