diff --git a/.travis.yml b/.travis.yml index c189118..34dc7ce 100644 --- a/.travis.yml +++ b/.travis.yml @@ -6,12 +6,18 @@ os: julia: - release - nightly +matrix: + fast_finish: true + allow_failures: + julia: nightly + notifications: email: false # uncomment the following lines to override the default test script -#script: -# - if [[ -a .git/shallow ]]; then git fetch --unshallow; fi -# - julia -e 'Pkg.clone(pwd()); Pkg.build("Groups"); Pkg.test("Groups"; coverage=true)' +script: + - if [[ -a .git/shallow ]]; then git fetch --unshallow; fi + - julia -e 'Pkg.clone("https://github.com/scheinerman/Permutations.jl.git")' + - julia -e 'Pkg.clone(pwd()); Pkg.build("Groups"); Pkg.test("Groups"; coverage=true)' after_success: # push coverage results to Coveralls - julia -e 'cd(Pkg.dir("Groups")); Pkg.add("Coverage"); using Coverage; Coveralls.submit(Coveralls.process_folder())' diff --git a/REQUIRE b/REQUIRE index 9a19caf..fee6ae7 100644 --- a/REQUIRE +++ b/REQUIRE @@ -1,2 +1,3 @@ julia 0.5 Permutations +Combinatorics diff --git a/src/Groups.jl b/src/Groups.jl index 3b4a7b3..910cd4f 100644 --- a/src/Groups.jl +++ b/src/Groups.jl @@ -1,7 +1,8 @@ module Groups -import Base: length, ==, hash, show +import Base: length, ==, hash, show, convert import Base: one, inv, reduce, *, ^ +import Base: findfirst, findnext export GSymbol, GWord @@ -41,6 +42,7 @@ type GWord{T<:GSymbol} <: Word end GWord{T<:GSymbol}(s::T) = GWord{T}([s]) +convert{T<:GSymbol, W<:Word}(::Type{W}, s::T) = GWord{T}(s) IDWord{T<:GSymbol}(::Type{T}) = GWord(one(T)) IDWord{T<:GSymbol}(W::GWord{T}) = IDWord(T) @@ -94,7 +96,10 @@ end freegroup_reduce(W::GWord) = freegroup_reduce!(deepcopy(W)) -hash{T}(W::GWord{T}) = (W.modified && freegroup_reduce!(W); W.savedhash) +function hash{T}(W::GWord{T}, h::UInt) + W.modified && freegroup_reduce!(W) + return W.savedhash + h +end function (==){T}(W::GWord{T}, Z::GWord{T}) W.modified && freegroup_reduce!(W) # reduce clears the flag and recalculate the hash @@ -172,6 +177,72 @@ end (^)(x::GWord, n::Integer) = power_by_squaring(x,n) (^){T<:GSymbol}(x::T, n::Integer) = GWord(x)^n +is_subsymbol(s::GSymbol, t::GSymbol) = + s.gen == t.gen && (0 ≤ s.pow ≤ t.pow || 0 ≥ s.pow ≥ t.pow) + +function findfirst(W::GWord, Z::GWord) + n = length(Z.symbols) + + @assert n > 1 + for (idx,a) in enumerate(W.symbols) + if idx + n - 1 > length(W.symbols) + break + end + first = is_subsymbol(Z.symbols[1],a) + if first + middle = W.symbols[idx+1:idx+n-2] == Z.symbols[2:end-1] + last = is_subsymbol(Z.symbols[end], W.symbols[idx+n-1]) + if middle && last + return idx + end + end + end + return 0 +end + +function findnext(W::GWord, Z::GWord, i::Integer) + t = findfirst(GWord{eltype(W.symbols)}(W.symbols[i:end]), Z) + if t > 0 + return t+i-1 + else + return 0 + end +end + +function replace!(W::GWord, index, toreplace::GWord, replacement::GWord; asserts=true) + n = length(toreplace.symbols) + if asserts + @assert is_subsymbol(toreplace.symbols[1], W.symbols[index]) + @assert W.symbols[index+1:index+n-2] == toreplace.symbols[2:end-1] + @assert is_subsymbol(toreplace.symbols[end], W.symbols[index+n-1]) + end + + first = W.symbols[index]*inv(toreplace.symbols[1]) + last = W.symbols[index+n-1]*inv(toreplace.symbols[end]) + replacement = first*replacement*last + splice!(W.symbols, index:index+n-1, replacement.symbols) + Groups.freegroup_reduce!(W) + return W +end + +function replace(W::GWord, index, toreplace::GWord, replacement::GWord) + replace!(deepcopy(W), index, toreplace, replacement) +end + +function replace_all!{T}(W::GWord{T}, subst_dict::Dict{GWord{T}, GWord{T}}) + for toreplace in reverse!(sort!(collect(keys(subst_dict)),by=length)) + replacement = subst_dict[toreplace] + i = findfirst(W, toreplace) + while i ≠ 0 + replace!(W,i,toreplace, replacement) + i = findnext(W, toreplace, i) + end + end + return W +end + +replace_all(W::GWord, subst_dict::Dict{GWord, GWord}) = replace_all!(deepcopy(W), subst_dict) + include("free_groups.jl") include("automorphism_groups.jl") diff --git a/src/automorphism_groups.jl b/src/automorphism_groups.jl index 51d2a48..25ee5a1 100644 --- a/src/automorphism_groups.jl +++ b/src/automorphism_groups.jl @@ -7,19 +7,31 @@ immutable AutSymbol <: GSymbol gen::String pow::Int ex::Expr + fmap::Function + imap::Function +end + +function (f::AutSymbol){T}(v::Vector{GWord{T}}) + if f.pow > 0 + map = f.fmap + else + map = f.imap + end + for i in 1:abs(f.pow) + v::Vector{GWord{T}} = map(v) + end + return v end (==)(s::AutSymbol, t::AutSymbol) = s.gen == t.gen && s.pow == t.pow hash(s::AutSymbol, h::UInt) = hash(s.gen, hash(s.pow, hash(:AutSymbol, h))) -IdSymbol(::Type{AutSymbol}) = AutSymbol("(id)", 0, :(IdAutomorphism(N))) +IdSymbol(::Type{AutSymbol}) = AutSymbol("(id)", 0, :(Id(N)), v -> Vector{GWord}(v), v -> Vector{GWord}(v)) function change_pow(s::AutSymbol, n::Int) - if n == 0 return one(s) end - symbol = s.ex.args[1] if symbol == :ɛ return flip_AutSymbol(s.ex.args[2], pow=n) @@ -29,29 +41,58 @@ function change_pow(s::AutSymbol, n::Int) return rmul_AutSymbol(s.ex.args[2], s.ex.args[3], pow=n) elseif symbol == :λ return lmul_AutSymbol(s.ex.args[2], s.ex.args[3], pow=n) - elseif symbol == :IdAutomorphism + elseif symbol == :Id return s else warn("Changing an unknown type of symbol! $s") - return AutSymbol(s.gen, n, s.ex) + return AutSymbol(s.gen, n, s.ex, s.fmap, s.imap) end end inv(f::AutSymbol) = change_pow(f, -f.pow) +function ϱ(i,j) + # @assert i ≠ j + return v -> [(k!=i ? GWord(v[k]) : v[i]*v[j]) for k in eachindex(v)] +end + +function ϱ_inv(i,j) + # @assert i ≠ j + return v -> [(k!=i ? GWord(v[k]) : v[i]*v[j]^-1) for k in eachindex(v)] +end + + +function λ(i,j) + # @assert i ≠ j + return v -> ([(k!=i ? GWord(v[k]) : v[j]*v[i]) for k in eachindex(v)]) +end + +function λ_inv(i,j) + # @assert i ≠ j + return v -> ([(k!=i ? GWord(v[k]) : v[j]^-1*v[i]) for k in eachindex(v)]) +end + + +ɛ(i) = v -> [(k!=i ? GWord(v[k]) : v[k]^-1) for k in eachindex(v)] + +function σ(perm) + # @assert sort(perm) == collect(1:length(perm)) + return v -> [GWord(v[perm[k]]) for k in eachindex(v)] +end + function rmul_AutSymbol(i,j; pow::Int=1) gen = string('ϱ',Char(8320+i), Char(8320+j)...) - return AutSymbol(gen, pow, :(ϱ($i,$j))) + return AutSymbol(gen, pow, :(ϱ($i,$j)), ϱ(i,j), ϱ_inv(i,j)) end function lmul_AutSymbol(i,j; pow::Int=1) gen = string('λ',Char(8320+i), Char(8320+j)...) - return AutSymbol(gen, pow, :(λ($i,$j))) + return AutSymbol(gen, pow, :(λ($i,$j)), λ(i,j), λ_inv(i,j)) end function flip_AutSymbol(j; pow::Int=1) gen = string('ɛ', Char(8320 + j)) - return AutSymbol(gen, (2+ pow%2)%2, :(ɛ($j))) + return AutSymbol(gen, (2+ pow%2)%2, :(ɛ($j)), ɛ(j), ɛ(j)) end function symmetric_AutSymbol(perm::Vector{Int}; pow::Int=1) @@ -59,11 +100,12 @@ function symmetric_AutSymbol(perm::Vector{Int}; pow::Int=1) ord = order(perm) pow = pow % ord perm = perm^pow - if array(perm) == collect(1:length(perm)) + p = array(perm) + if p == collect(1:length(p)) return one(AutSymbol) else - gen = string('σ', [Char(8320 + i) for i in array(perm)]...) - return AutSymbol(gen, 1, :(σ($(array(perm))))) + gen = string('σ', [Char(8320 + i) for i in p]...) + return AutSymbol(gen, 1, :(σ($p)), σ(p), σ(array(inv(perm)))) end end @@ -77,6 +119,13 @@ end typealias AutWord GWord{AutSymbol} +function (F::AutWord)(v) + for f in F.symbols + v = f(v) + end + return v +end + convert(::Type{AutWord}, s::AutSymbol) = GWord(s) function simplify_perms!(W::AutWord) @@ -100,5 +149,6 @@ function simplify_perms!(W::AutWord) end end end + deleteat!(W.symbols, find(x -> x.pow == 0, W.symbols)) return reduced end diff --git a/test/runtests.jl b/test/runtests.jl index 7ed2238..d32f6d2 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -40,9 +40,15 @@ end @test isa(Groups.GWord(s), Groups.GWord) @test isa(Groups.GWord(s), FGWord) @test isa(FGWord(s), Groups.GWord) + @test isa(convert(FGWord, s), GWord) + @test isa(convert(FGWord, s), FGWord) + @test isa(Vector{FGWord}([s,t]), Vector{FGWord}) + @test Vector{GWord{FGSymbol}}([s,t]) == Vector{FGWord}([s,t]) @test isa(s*s, FGWord) @test s*s == s^2 @test t*s ≠ s*t + @test Vector{GWord}([s,t]) == [s^2*s^-1, t] + @test hash([t^1,s^1]) == hash([t^2*inv(t),s*inv(s)*s]) end @testset "eltary functions" begin @test length(FGWord(s)) == 1 @@ -56,6 +62,7 @@ end @test isa(one(w), FGWord) @test inv(s*t) == t^-1*s^-1 @test inv(w) == s*t^-1*s^-1 + end @testset "reductions" begin @@ -80,11 +87,33 @@ end @test (t*s*t^-1)^10 == t*s^10*t^-1 @test (t*s*t^-1)^-10 == t*s^-10*t^-1 end + + @testset "replacements" begin + @test Groups.is_subsymbol(s, Groups.change_pow(s,2)) == true + @test Groups.is_subsymbol(s, Groups.change_pow(s,-2)) == false + @test Groups.is_subsymbol(t, Groups.change_pow(s,-2)) == false + @test Groups.is_subsymbol(inv(t), Groups.change_pow(t,-2)) == true + c = s*t*s^-1*t^-1 + @test findfirst(c, s^-1*t^-1) == 3 + @test findnext(c*s^-1, s^-1*t^-1,3) == 3 + @test findnext(c*s^-1*t^-1, s^-1*t^-1,4) == 5 + @test findfirst(c*t, c) == 0 + w = s*t*s^-1 + subst = Dict{FGWord, FGWord}(w => s^1, s*t^-1 => t^4) + @test Groups.replace(c, 1, s*t, one(FGWord)) == s^-1*t^-1 + + @test Groups.replace(c, 1, w, subst[w]) == s*t^-1 + @test Groups.replace(s*c*t^-1, 1, w, subst[w]) == s^2*t^-2 + @test Groups.replace(t*c*t, 2, w, subst[w]) == t*s + @test Groups.replace_all!(s*c*s*c*s, subst) == s*t^4*s*t^4*s + end +end + @testset "Automorphisms" begin @testset "AutSymbol" begin @test_throws MethodError AutSymbol("a") @test_throws MethodError AutSymbol("a", 1) - f = AutSymbol("a", 1, :(a(0))) + f = AutSymbol("a", 1, :(a(0)), v -> v, v -> v) @test isa(f, GSymbol) @test isa(f, AutSymbol) @test isa(symmetric_AutSymbol([1,2,3,4]), AutSymbol) @@ -94,7 +123,7 @@ end end @testset "AutWords" begin - f = AutSymbol("a", 1, :(a(0))) + f = AutSymbol("a", 1, :(a(0)), v -> v, v -> v) @test isa(GWord(f), GWord) @test isa(GWord(f), AutWord) @test isa(AutWord(f), AutWord) @@ -120,4 +149,30 @@ end @test a*b == b*a @test a^3 * b^3 == one(a) end + @testset "specific Aut(𝔽₄) tests" begin + N = 4 + import Combinatorics.nthperm + SymmetricGroup(n) = [nthperm(collect(1:n), k) for k in 1:factorial(n)] + indexing = [[i,j] for i in 1:N for j in 1:N if i≠j] + + σs = [symmetric_AutSymbol(perm) for perm in SymmetricGroup(N)[2:end]]; + ϱs = [rmul_AutSymbol(i,j) for (i,j) in indexing] + λs = [lmul_AutSymbol(i,j) for (i,j) in indexing] + ɛs = [flip_AutSymbol(i) for i in 1:N]; + + S = vcat(ϱs, λs, σs, ɛs) + S = vcat(S, [inv(s) for s in S]) + @test isa(S, Vector{AutSymbol}) + @test length(S) == 102 + @test length(unique(S)) == 75 + S₁ = [GWord(s) for s in unique(S)] + @test isa(S₁, Vector{AutWord}) + p = prod(S₁) + @test length(p) == 75 + @test Groups.simplify_perms!(p) == false + @test length(p) == 53 + @test Groups.join_free_symbols!(p) == true + + + end end