2017-01-19 10:14:37 +01:00
|
|
|
module Groups
|
2017-01-17 08:01:22 +01:00
|
|
|
|
2017-01-19 10:14:37 +01:00
|
|
|
import Base: length, ==, hash, show
|
|
|
|
import Base: one, inv, reduce, *, ^
|
2017-01-17 08:01:22 +01:00
|
|
|
|
2017-01-21 17:19:08 +01:00
|
|
|
export GSymbol, GWord
|
|
|
|
|
2017-01-23 11:23:36 +01:00
|
|
|
export IdSymbol, change_pow
|
2017-01-17 08:01:22 +01:00
|
|
|
|
2017-01-19 10:14:37 +01:00
|
|
|
abstract GSymbol
|
2017-01-17 08:01:22 +01:00
|
|
|
|
2017-01-17 15:13:54 +01:00
|
|
|
function show(io::IO, s::GSymbol)
|
2017-01-19 10:14:37 +01:00
|
|
|
if s.pow == 0 || s.pow == 1
|
|
|
|
print(io, s.gen)
|
2017-01-17 08:01:22 +01:00
|
|
|
else
|
|
|
|
print(io, (s.gen)*"^$(s.pow)")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-01-17 17:54:30 +01:00
|
|
|
length(s::GSymbol) = (s.pow == 0 ? 0 : 1)
|
2017-01-17 08:01:22 +01:00
|
|
|
|
2017-01-23 11:23:36 +01:00
|
|
|
IdSymbol(T::Type{GSymbol}) = throw(ArgumentError("Define IdSymbol(::Type{$T}) which is the identity element for Your type!"))
|
2017-01-21 17:19:08 +01:00
|
|
|
|
2017-01-23 11:23:36 +01:00
|
|
|
one{T<:GSymbol}(::Type{T}) = IdSymbol(T)
|
2017-01-18 17:49:50 +01:00
|
|
|
one(s::GSymbol) = one(typeof(s))
|
2017-01-17 17:54:30 +01:00
|
|
|
|
2017-01-19 10:14:37 +01:00
|
|
|
(*){T<:GSymbol}(s::T, t::T) = return GWord{T}([s])*t
|
2017-01-17 17:54:30 +01:00
|
|
|
|
2017-01-21 17:15:33 +01:00
|
|
|
change_pow(s::GSymbol, n::Int) = throw(ArgumentError("Define change_pow function for $(typeof(s))!"))
|
|
|
|
|
2017-01-17 17:54:30 +01:00
|
|
|
|
|
|
|
abstract Word
|
|
|
|
|
2017-01-18 17:51:58 +01:00
|
|
|
#=
|
|
|
|
@ScottPJones
|
|
|
|
|
|
|
|
If so, I'd recommend
|
|
|
|
1) making GWord a type, not an immutable
|
|
|
|
2) add fields
|
|
|
|
savedhash::UInt and
|
|
|
|
modified::Bool
|
|
|
|
3) make any function that modifies the contents of .symbols set the modified flag,
|
|
|
|
4) make the hash function
|
|
|
|
a) check that flag:
|
|
|
|
if false, return the savedhash field,
|
|
|
|
otherwise, call reduce!,
|
|
|
|
b) clear the modified flag, and
|
|
|
|
c) calculate a hash value simply by calling hash(symbols)
|
|
|
|
d) save that back into the savedhash field
|
|
|
|
|
|
|
|
5) for ==, I don't think you need to do all that checking for length or length == 0, that will already be handled by comparing the symbols vectors (possibly faster)
|
|
|
|
|
|
|
|
=#
|
|
|
|
|
2017-01-21 17:15:33 +01:00
|
|
|
type GWord{T<:GSymbol} <: Word
|
2017-01-17 17:54:30 +01:00
|
|
|
symbols::Vector{T}
|
2017-01-21 17:15:33 +01:00
|
|
|
savedhash::UInt
|
|
|
|
modified::Bool
|
|
|
|
|
|
|
|
function GWord(symbols::Vector{T})
|
|
|
|
return new(symbols, hash(symbols), false)
|
|
|
|
end
|
2017-01-17 17:54:30 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
GWord{T<:GSymbol}(s::T) = GWord{T}([s])
|
2017-01-18 17:49:50 +01:00
|
|
|
|
|
|
|
IDWord{T<:GSymbol}(::Type{T}) = GWord(one(T))
|
|
|
|
IDWord{T<:GSymbol}(W::GWord{T}) = IDWord(T)
|
2017-01-17 17:54:30 +01:00
|
|
|
|
|
|
|
function length(W::GWord)
|
|
|
|
return sum([abs(s.pow) for s in W.symbols])
|
|
|
|
end
|
2017-01-17 08:01:22 +01:00
|
|
|
|
2017-01-18 17:49:50 +01:00
|
|
|
one{T}(::Type{GWord{T}}) = IDWord(T)
|
2017-01-17 15:13:54 +01:00
|
|
|
one{T}(w::GWord{T}) = one(GWord{T})
|
2017-01-17 08:01:22 +01:00
|
|
|
|
2017-01-17 15:13:54 +01:00
|
|
|
function inv{T}(W::GWord{T})
|
|
|
|
if length(W) == 0
|
|
|
|
return W
|
|
|
|
else
|
2017-01-21 17:15:59 +01:00
|
|
|
return reduceGWord{T}(reverse([inv(s) for s in W.symbols]))
|
2017-01-17 15:13:54 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-01-18 17:50:29 +01:00
|
|
|
function free_group_reduction!(W::GWord)
|
|
|
|
reduced = true
|
|
|
|
for i in 1:length(W.symbols) - 1
|
|
|
|
if W.symbols[i].gen == W.symbols[i+1].gen
|
|
|
|
reduced = false
|
|
|
|
p1 = W.symbols[i].pow
|
|
|
|
p2 = W.symbols[i+1].pow
|
|
|
|
W.symbols[i+1] = change_pow(W.symbols[i], p1 + p2)
|
|
|
|
W.symbols[i] = one(W.symbols[i])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return reduced
|
|
|
|
end
|
|
|
|
|
|
|
|
function reduce!{T}(W::GWord{T}, reduce_func::Function=free_group_reduction!)
|
2017-01-17 15:13:54 +01:00
|
|
|
if length(W) < 2
|
|
|
|
deleteat!(W.symbols, find(x -> x.pow == 0, W.symbols))
|
2017-01-21 17:15:33 +01:00
|
|
|
else
|
2017-01-17 15:13:54 +01:00
|
|
|
|
2017-01-21 17:15:33 +01:00
|
|
|
reduced = false
|
|
|
|
while !reduced
|
|
|
|
reduced = reduce_func(W)
|
|
|
|
deleteat!(W.symbols, find(x -> x.pow == 0, W.symbols))
|
|
|
|
end
|
2017-01-17 08:01:22 +01:00
|
|
|
end
|
2017-01-21 17:15:33 +01:00
|
|
|
|
|
|
|
W.modified = false
|
|
|
|
W.savedhash = hash(W.symbols,hash(typeof(W)))
|
2017-01-17 08:01:22 +01:00
|
|
|
return W
|
|
|
|
end
|
|
|
|
|
2017-01-17 15:13:54 +01:00
|
|
|
reduce(W::GWord) = reduce!(deepcopy(W))
|
2017-01-17 08:01:22 +01:00
|
|
|
|
2017-01-21 17:15:33 +01:00
|
|
|
hash{T}(W::GWord{T}) = (W.modified && reduce!(W); W.savedhash)
|
|
|
|
|
2017-01-18 17:51:58 +01:00
|
|
|
function (==){T}(W::GWord{T}, Z::GWord{T})
|
2017-01-21 17:15:33 +01:00
|
|
|
W.modified && reduce!(W) # reduce could actually clear the flag and recalculate the hash
|
|
|
|
Z.modified && reduce!(Z)
|
|
|
|
return W.savedhash == Z.savedhash && W.symbols == Z.symbols
|
2017-01-18 17:51:58 +01:00
|
|
|
end
|
2017-01-17 08:01:22 +01:00
|
|
|
|
2017-01-17 15:13:54 +01:00
|
|
|
function show(io::IO, W::GWord)
|
2017-01-17 08:01:22 +01:00
|
|
|
if length(W) == 0
|
|
|
|
print(io, "(id)")
|
|
|
|
else
|
|
|
|
join(io, [string(s) for s in W.symbols], "*")
|
|
|
|
end
|
2017-01-17 17:54:30 +01:00
|
|
|
end
|
2017-01-17 08:01:22 +01:00
|
|
|
|
2017-01-17 15:13:54 +01:00
|
|
|
function r_multiply!(W::GWord, x; reduced::Bool=true)
|
2017-01-17 08:01:22 +01:00
|
|
|
if length(x) > 0
|
2017-01-19 10:14:37 +01:00
|
|
|
push!(W.symbols, x...)
|
2017-01-17 08:01:22 +01:00
|
|
|
end
|
|
|
|
if reduced
|
|
|
|
reduce!(W)
|
|
|
|
end
|
|
|
|
return W
|
|
|
|
end
|
|
|
|
|
2017-01-17 15:13:54 +01:00
|
|
|
function l_multiply!(W::GWord, x; reduced::Bool=true)
|
2017-01-17 08:01:22 +01:00
|
|
|
if length(x) > 0
|
2017-01-19 10:14:37 +01:00
|
|
|
unshift!(W.symbols, reverse(x)...)
|
2017-01-17 08:01:22 +01:00
|
|
|
end
|
|
|
|
if reduced
|
|
|
|
reduce!(W)
|
|
|
|
end
|
|
|
|
return W
|
|
|
|
end
|
|
|
|
|
2017-01-17 15:13:54 +01:00
|
|
|
r_multiply(W::GWord, x; reduced::Bool=true) =
|
|
|
|
r_multiply!(deepcopy(W),x, reduced=reduced)
|
|
|
|
l_multiply(W::GWord, x; reduced::Bool=true) =
|
|
|
|
l_multiply!(deepcopy(W),x, reduced=reduced)
|
2017-01-17 08:01:22 +01:00
|
|
|
|
2017-01-21 17:19:08 +01:00
|
|
|
(*){T}(W::GWord{T}, Z::GWord{T}) = r_multiply(W, Z.symbols)
|
2017-01-17 15:13:54 +01:00
|
|
|
(*)(W::GWord, s::GSymbol) = W*GWord(s)
|
|
|
|
(*)(s::GSymbol, W::GWord) = GWord(s)*W
|
2017-01-17 08:01:22 +01:00
|
|
|
|
2017-01-17 15:13:54 +01:00
|
|
|
function power_by_squaring{T}(x::GWord{T}, p::Integer)
|
2017-01-17 08:01:22 +01:00
|
|
|
if p < 0
|
|
|
|
return power_by_squaring(inv(x), -p)
|
|
|
|
elseif p == 0
|
|
|
|
return one(x)
|
|
|
|
elseif p == 1
|
|
|
|
return deepcopy(x)
|
|
|
|
elseif p == 2
|
|
|
|
return x*x
|
|
|
|
end
|
|
|
|
t = trailing_zeros(p) + 1
|
|
|
|
p >>= t
|
|
|
|
while (t -= 1) > 0
|
|
|
|
x *= x
|
|
|
|
end
|
|
|
|
y = x
|
|
|
|
while p > 0
|
|
|
|
t = trailing_zeros(p) + 1
|
|
|
|
p >>= t
|
|
|
|
while (t -= 1) >= 0
|
|
|
|
x *= x
|
|
|
|
end
|
|
|
|
y *= x
|
|
|
|
end
|
|
|
|
return reduce!(y)
|
|
|
|
end
|
|
|
|
|
2017-01-17 15:13:54 +01:00
|
|
|
(^)(x::GWord, n::Integer) = power_by_squaring(x,n)
|
2017-01-17 08:01:22 +01:00
|
|
|
|
2017-01-17 17:54:56 +01:00
|
|
|
|
2017-01-17 08:01:22 +01:00
|
|
|
end
|