Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/Groups/group_constructors.jl
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,16 @@ function symmetric_group(n::Int)
return PermGroup(GAP.Globals.SymmetricGroup(n)::GapObj)
end

# for functions like perm, cperm or macros like @perm provide a cached
# version of symmetric_group to reduce the overhead for these functions.
const SymmetricGroupID = AbstractAlgebra.CacheDictType{Int, PermGroup}()

function _symmetric_group_cached(n::Int)
Base.get!(SymmetricGroupID, n) do
symmetric_group(n)
end
end

"""
is_natural_symmetric_group(G::GAPGroup)

Expand Down
18 changes: 9 additions & 9 deletions src/Groups/perm.jl
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ Sym(6)
```
"""
function perm(L::AbstractVector{<:IntegerUnion})
return PermGroupElem(symmetric_group(length(L)), GAPWrap.PermList(GapObj(L;recursive=true)))
return PermGroupElem(_symmetric_group_cached(length(L)), GAPWrap.PermList(GapObj(L;recursive=true)))
end

"""
Expand Down Expand Up @@ -301,7 +301,7 @@ julia> x == y
true
```
"""
cperm() = one(symmetric_group(1))
cperm() = one(_symmetric_group_cached(1))

cperm(L::AbstractVector{T}, Ls::AbstractVector{T}...) where T <: IntegerUnion = _cperm((L,Ls...))

Expand All @@ -315,7 +315,7 @@ function _cperm(L)
# L is something like a Vector{Vector{Int}}, describing a sequence of cycles
# figure out the maximal entry occurring in there
deg = mapreduce(maximum, max, L; init=1)
return _cperm(symmetric_group(deg), L)
return _cperm(_symmetric_group_cached(deg), L)
end

function _cperm(g::PermGroup, L)
Expand All @@ -341,7 +341,7 @@ end
# fallback in case there are overlapping cycles -- we
# then resort to multiplication, which is slower but gets the job done
function _cperm_slow(g::PermGroup, L)
h = symmetric_group(degree(g))
h = _symmetric_group_cached(degree(g))
x = prod(y -> cperm(h, y), L)
@req x in g "the element does not embed in the group"
return PermGroupElem(g, GapObj(x))
Expand Down Expand Up @@ -906,23 +906,23 @@ end

function _perm_format(::Val{:single}, n_or_G, res)
return quote
let n = $(n_or_G), G = n isa Int ? symmetric_group(n) : n
let n = $(n_or_G), G = n isa Int ? _symmetric_group_cached(n) : n
cperm(G, $(res...))
end
end
end

function _perm_format(::Val{:vector}, n_or_G, res)
return quote
let n = $(n_or_G), G = n isa Int ? symmetric_group(n) : n
let n = $(n_or_G), G = n isa Int ? _symmetric_group_cached(n) : n
[cperm(G, p...) for p in [$(res...)]]
end
end
end

function _perm_format(::Val{:tuple}, n_or_G, res)
return quote
let n = $(n_or_G), G = n isa Int ? symmetric_group(n) : n
let n = $(n_or_G), G = n isa Int ? _symmetric_group_cached(n) : n
((cperm(G, p...) for p in [$(res...)])...,)
end
end
Expand All @@ -944,7 +944,7 @@ Permutation group of degree 5
```
"""
function permutation_group(n::IntegerUnion, perms::Vector{PermGroupElem})
return sub(symmetric_group(n), perms)[1]
return sub(_symmetric_group_cached(n), perms)[1]
end

@doc raw"""
Expand All @@ -970,7 +970,7 @@ macro permutation_group(n, gens...)
end

return quote
let g = symmetric_group($n)
let g = _symmetric_group_cached($n)
sub(g, [cperm(g, pi...) for pi in [$(ores...)]], check = false)[1]
end
end
Expand Down