From a33b8717549959146a91fb5edf7a490c98f2dc0e Mon Sep 17 00:00:00 2001 From: Marek Kaluba Date: Mon, 12 Feb 2024 12:17:48 +0100 Subject: [PATCH] simplify the constructors of the FreeGroup --- src/types.jl | 27 +++++++++++++-------------- test/AutFn.jl | 2 +- test/fp_groups.jl | 2 +- test/free_groups.jl | 2 +- 4 files changed, 16 insertions(+), 17 deletions(-) diff --git a/src/types.jl b/src/types.jl index 1ec1d45..b33c2b8 100644 --- a/src/types.jl +++ b/src/types.jl @@ -177,9 +177,16 @@ struct FreeGroup{T,O} <: AbstractFPGroup end end -FreeGroup(gens, A::Alphabet) = FreeGroup(gens, KnuthBendix.LenLex(A)) +function FreeGroup(n::Integer) + symbols = + collect(Iterators.flatten((Symbol(:f, i), Symbol(:F, i)) for i in 1:n)) + inverses = collect(Iterators.flatten((2i, 2i - 1) for i in 1:n)) + return FreeGroup(Alphabet(symbols, inverses)) +end -function FreeGroup(A::Alphabet) +FreeGroup(A::Alphabet) = FreeGroup(KnuthBendix.LenLex(A)) + +function __group_gens(A::Alphabet) @boundscheck @assert all(KnuthBendix.hasinverse(l, A) for l in A) gens = Vector{eltype(A)}() invs = Vector{eltype(A)}() @@ -188,20 +195,12 @@ function FreeGroup(A::Alphabet) push!(gens, l) push!(invs, inv(l, A)) end - - return FreeGroup(gens, A) + return gens end -function FreeGroup(n::Integer) - symbols = Symbol[] - inverses = Int[] - sizehint!(symbols, 2n) - sizehint!(inverses, 2n) - for i in 1:n - push!(symbols, Symbol(:f, i), Symbol(:F, i)) - push!(inverses, 2i, 2i - 1) - end - return FreeGroup(symbols[1:2:2n], Alphabet(symbols, inverses)) +function FreeGroup(O::KnuthBendix.WordOrdering) + grp_gens = __group_gens(alphabet(O)) + return FreeGroup(grp_gens, O) end function Base.show(io::IO, F::FreeGroup) diff --git a/test/AutFn.jl b/test/AutFn.jl index c3f035b..85b1837 100644 --- a/test/AutFn.jl +++ b/test/AutFn.jl @@ -38,7 +38,7 @@ [2, 1, 4, 3, 6, 5, 8, 7, 10, 9] ) - F4 = FreeGroup([:a, :b, :c, :d], A4) + F4 = FreeGroup(A4) a, b, c, d = gens(F4) D = ntuple(i -> gens(F4, i), 4) diff --git a/test/fp_groups.jl b/test/fp_groups.jl index 87e6ef7..47511a6 100644 --- a/test/fp_groups.jl +++ b/test/fp_groups.jl @@ -4,7 +4,7 @@ @test FreeGroup(A) isa FreeGroup @test sprint(show, FreeGroup(A)) == "free group on 3 generators" - F = FreeGroup([:a, :b, :c], A) + F = FreeGroup([:a, :b, :c], Groups.KnuthBendix.LenLex(A)) @test sprint(show, F) == "free group on 3 generators" a, b, c = gens(F) diff --git a/test/free_groups.jl b/test/free_groups.jl index f35d85e..9604a1d 100644 --- a/test/free_groups.jl +++ b/test/free_groups.jl @@ -1,7 +1,7 @@ @testset "FreeGroup" begin A3 = Alphabet([:a, :b, :c, :A, :B, :C], [4,5,6,1,2,3]) - F3 = FreeGroup([:a, :b, :c], A3) + F3 = FreeGroup(A3) @test F3 isa FreeGroup @test gens(F3) isa Vector