Groups.jl/src/FreeGroup.jl

144 lines
3.8 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
immutable FreeSymbol <: GSymbol
str::String
pow::Int
end
typealias FreeGroupElem GWord{FreeSymbol}
2017-05-09 17:46:52 +02:00
type FreeGroup <: FPGroup
gens::Vector{FreeSymbol}
2017-05-11 18:16:21 +02:00
# order::Vector{T}
# fastmult_table::Array{Int,2}
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, generators
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
parent_type(::Type{FreeGroupElem}) = FreeGroup
2017-05-11 18:17:42 +02:00
elem_type(::FreeGroup) = FreeGroupElem
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
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)()
id = FreeGroupElem(FreeSymbol("", 0))
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::GWord)
eltype(w.symbols) == FreeSymbol || throw("Can not coerce $w to FreeGroup $G.")
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)
generators(G::FreeGroup) = [G(FreeGroupElem(g)) for g in G.gens]
2017-05-11 18:28:00 +02:00
###############################################################################
#
# String I/O
#
###############################################################################
function show(io::IO, G::FreeGroup)
print(io, "Finitely presented group on $(length(G.gens)) generators:\n")
print(io, "gens:\t", join([g.str for g in G.gens], ", "))
2017-05-09 17:46:52 +02:00
end
2017-05-11 18:28:00 +02:00
###############################################################################
#
# Comparison
#
###############################################################################
function (==)(s::FreeSymbol, t::FreeSymbol)
2017-05-11 18:22:37 +02:00
isone(s) && isone(t) && return true
s.str == t.str || return false
s.pow == t.pow || return false
return true
end
2017-05-11 18:28:00 +02:00
###############################################################################
#
# Inversion
#
###############################################################################
2017-05-11 18:22:37 +02:00
inv(s::FreeSymbol) = change_pow(s, -s.pow)
2017-05-11 18:22:37 +02:00
2017-05-11 18:28:00 +02:00
###############################################################################
#
# Misc
#
###############################################################################
2017-05-11 18:23:01 +02:00
# function add_rel!{T<:FreeSymbol}(G::FreeGroup, w::GWord{T})
# if !(w in G.rels)
# w = G(w)
# push!(G.rels, w)
# end
# return G
# end
#
# function quotientgroup(G::FreeGroup, rels::Vector{FreeGroupElem})
# for r in rels
# parent(r) == G || throw("Can not form quotient group: $r is not an element of $G")
# end
# H = deepcopy(G)
# for rel in rels
# add_rel!(H, rel)
# end
# return H
# end