Skip to content

Commit

Permalink
Merge pull request #31 from kalmarek/mk/update_to_PG_0.6
Browse files Browse the repository at this point in the history
Update to PermutationGroups-0.6
  • Loading branch information
Marek Kaluba authored Feb 13, 2024
2 parents 866e431 + c13226a commit 1fbd7b8
Show file tree
Hide file tree
Showing 12 changed files with 255 additions and 148 deletions.
6 changes: 3 additions & 3 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "Groups"
uuid = "5d8bd718-bd84-11e8-3b40-ad14f4a32557"
authors = ["Marek Kaluba <kalmar@amu.edu.pl>"]
version = "0.7.8"
version = "0.8"

[deps]
GroupsCore = "d5909c97-4eac-4ecc-a3dc-fdd0858a4120"
Expand All @@ -14,10 +14,10 @@ Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
StaticArrays = "90137ffa-7385-5640-81b9-e52037218182"

[compat]
GroupsCore = "0.4"
GroupsCore = "0.5"
KnuthBendix = "0.4"
OrderedCollections = "1"
PermutationGroups = "0.4"
PermutationGroups = "0.6"
StaticArrays = "1"
julia = "1.6"

Expand Down
1 change: 1 addition & 0 deletions src/Groups.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ include(joinpath("constructions", "constructions.jl"))
import .Constructions

include("types.jl")
include("rand.jl")
include("hashing.jl")
include("normalform.jl")
include("autgroups.jl")
Expand Down
55 changes: 30 additions & 25 deletions src/autgroups.jl
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ function Base.:(==)(
hash(g) != hash(h) && return false
end

length(word(g)) > 8 && normalform!(g)
length(word(h)) > 8 && normalform!(h)
normalform!(g)
normalform!(h)

word(g) == word(h) && return true

Expand Down Expand Up @@ -138,43 +138,47 @@ end

# forward evaluate by substitution

struct LettersMap{T,A}
indices_map::Dict{Int,T}
struct LettersMap{W<:AbstractWord,A}
indices_map::Dict{Int,W}
A::A
end

function LettersMap(a::FPGroupElement{<:AutomorphismGroup})
dom = domain(a)
@assert all(isone length word, dom)
A = alphabet(first(dom))
first_letters = first.(word.(dom))
img = evaluate!(dom, a)

# (dom[i] → img[i] is a map from domain to images)
# we need a map from alphabet indices → (gens, gens⁻¹) → images
# here we do it for elements of the domain
# (trusting it's a set of generators that define a)
@assert length(dom) == length(img)

indices_map =
Dict(A[A[fl]] => word(im) for (fl, im) in zip(first_letters, img))
# inverses of generators are dealt lazily in getindex
if all(isone length word, dom)
A = alphabet(first(dom))
first_letters = first.(word.(dom))
img = evaluate!(dom, a)

# (dom[i] → img[i] is a map from domain to images)
# we need a map from alphabet indices → (gens, gens⁻¹) → images
# here we do it for elements of the domain
# (trusting it's a set of generators that define a)
@assert length(dom) == length(img)

indices_map =
Dict(Int(fl) => word(im) for (fl, im) in zip(first_letters, img))
# inverses of generators are dealt lazily in getindex
else
throw("LettersMap is not implemented for non-generators in domain")
end

return LettersMap(indices_map, A)
end

function Base.getindex(lm::LettersMap, i::Integer)
function Base.getindex(lm::LettersMap{W}, i::Integer) where {W}
# here i is an index of an alphabet
@boundscheck 1 i length(lm.A)

if !haskey(lm.indices_map, i)
img = if haskey(lm.indices_map, inv(i, lm.A))
inv(lm.indices_map[inv(i, lm.A)], lm.A)
I = inv(i, lm.A)
if haskey(lm.indices_map, I)
img = inv(lm.indices_map[I], lm.A)
lm.indices_map[i] = img
else
@warn "LetterMap: neither $i nor its inverse has assigned value"
one(valtype(lm.indices_map))
lm.indices_map[i] = W([i])
lm.indices_map[I] = W([I])
end
lm.indices_map[i] = img
end
return lm.indices_map[i]
end
Expand All @@ -185,9 +189,10 @@ function (a::FPGroupElement{<:AutomorphismGroup})(g::FPGroupElement)
return parent(g)(img_w)
end

evaluate(w::AbstractWord, lm::LettersMap) = evaluate!(one(w), w, lm)
evaluate(w::AbstractWord, lm::LettersMap) = evaluate!(similar(w), w, lm)

function evaluate!(res::AbstractWord, w::AbstractWord, lm::LettersMap)
resize!(res, 0)
for i in w
append!(res, lm[i])
end
Expand Down
64 changes: 38 additions & 26 deletions src/constructions/direct_power.jl
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,6 @@ end

GroupsCore.ngens(G::DirectPower) = _nfold(G) * ngens(G.group)

function GroupsCore.gens(G::DirectPower, i::Integer)
k = ngens(G.group)
ci = CartesianIndices((k, _nfold(G)))
@boundscheck checkbounds(ci, i)
r, c = Tuple(ci[i])
tup = ntuple(j -> j == c ? gens(G.group, r) : one(G.group), _nfold(G))
return DirectPowerElement(tup, G)
end

function GroupsCore.gens(G::DirectPower)
N = _nfold(G)
S = gens(G.group)
Expand All @@ -78,14 +69,6 @@ end

Base.isfinite(G::DirectPower) = isfinite(G.group)

function Base.rand(
rng::Random.AbstractRNG,
rs::Random.SamplerTrivial{<:DirectPower},
)
G = rs[]
return DirectPowerElement(rand(rng, G.group, _nfold(G)), G)
end

GroupsCore.parent(g::DirectPowerElement) = g.parent

function Base.:(==)(g::DirectPowerElement, h::DirectPowerElement)
Expand All @@ -94,20 +77,46 @@ end

Base.hash(g::DirectPowerElement, h::UInt) = hash(g.elts, hash(parent(g), h))

function Base.deepcopy_internal(g::DirectPowerElement, stackdict::IdDict)
return DirectPowerElement(
Base.deepcopy_internal(g.elts, stackdict),
parent(g),
)
end

Base.inv(g::DirectPowerElement) = DirectPowerElement(inv.(g.elts), parent(g))

function Base.:(*)(g::DirectPowerElement, h::DirectPowerElement)
@assert parent(g) === parent(h)
return DirectPowerElement(g.elts .* h.elts, parent(g))
end

# to make sure that parents are never copied i.e.
# g and deepcopy(g) share their parent
Base.deepcopy_internal(G::DirectPower, ::IdDict) = G

################## Implementing Group Interface Done!

function GroupsCore.gens(G::DirectPower, i::Integer)
k = ngens(G.group)
ci = CartesianIndices((k, _nfold(G)))
@boundscheck checkbounds(ci, i)
r, c = Tuple(ci[i])
tup = ntuple(j -> j == c ? gens(G.group, r) : one(G.group), _nfold(G))
return DirectPowerElement(tup, G)
end

# Overloading rand: the PRA of GroupsCore is known for not performing
# well on direct sums
function Random.Sampler(
RNG::Type{<:Random.AbstractRNG},
G::DirectPower,
repetition::Random.Repetition = Val(Inf),
)
return Random.SamplerTrivial(G)
end

function Base.rand(
rng::Random.AbstractRNG,
rs::Random.SamplerTrivial{<:DirectPower},
)
G = rs[]
return DirectPowerElement(rand(rng, G.group, _nfold(G)), G)
end

function GroupsCore.order(::Type{I}, g::DirectPowerElement) where {I<:Integer}
return convert(I, reduce(lcm, (order(I, h) for h in g.elts); init = one(I)))
end
Expand All @@ -117,10 +126,13 @@ Base.isone(g::DirectPowerElement) = all(isone, g.elts)
function Base.show(io::IO, G::DirectPower)
n = _nfold(G)
nn = n == 1 ? "1-st" : n == 2 ? "2-nd" : n == 3 ? "3-rd" : "$n-th"
return print(io, "Direct $(nn) power of $(G.group)")
return print(io, "Direct $(nn) power of ", G.group)
end

function Base.show(io::IO, g::DirectPowerElement)
return print(io, "( ", join(g.elts, ", "), " )")
print(io, "( ")
join(io, g.elts, ", ")
return print(" )")
end

# convienience:
Expand Down
46 changes: 29 additions & 17 deletions src/constructions/direct_product.jl
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,6 @@ end

Base.isfinite(G::DirectProduct) = isfinite(G.first) && isfinite(G.last)

function Base.rand(
rng::Random.AbstractRNG,
rs::Random.SamplerTrivial{<:DirectProduct},
)
G = rs[]
return DirectProductElement((rand(rng, G.first), rand(rng, G.last)), G)
end

GroupsCore.parent(g::DirectProductElement) = g.parent

function Base.:(==)(g::DirectProductElement, h::DirectProductElement)
Expand All @@ -88,13 +80,6 @@ end

Base.hash(g::DirectProductElement, h::UInt) = hash(g.elts, hash(parent(g), h))

function Base.deepcopy_internal(g::DirectProductElement, stackdict::IdDict)
return DirectProductElement(
Base.deepcopy_internal(g.elts, stackdict),
parent(g),
)
end

function Base.inv(g::DirectProductElement)
return DirectProductElement(inv.(g.elts), parent(g))
end
Expand All @@ -104,17 +89,44 @@ function Base.:(*)(g::DirectProductElement, h::DirectProductElement)
return DirectProductElement(g.elts .* h.elts, parent(g))
end

# to make sure that parents are never copied i.e.
# g and deepcopy(g) share their parent
Base.deepcopy_internal(G::DirectProduct, ::IdDict) = G

################## Implementing Group Interface Done!

# Overloading rand: the PRA of GroupsCore is known for not performing
# well on direct sums
function Random.Sampler(
RNG::Type{<:Random.AbstractRNG},
G::DirectProduct,
repetition::Random.Repetition = Val(Inf),
)
return Random.SamplerTrivial(G)
end

function Base.rand(
rng::Random.AbstractRNG,
rs::Random.SamplerTrivial{<:DirectProduct},
)
G = rs[]
return DirectProductElement((rand(rng, G.first), rand(rng, G.last)), G)
end

function GroupsCore.order(::Type{I}, g::DirectProductElement) where {I<:Integer}
return convert(I, lcm(order(I, first(g.elts)), order(I, last(g.elts))))
end

Base.isone(g::DirectProductElement) = all(isone, g.elts)

function Base.show(io::IO, G::DirectProduct)
return print(io, "Direct product of $(G.first) and $(G.last)")
return print(io, "Direct product of ", G.first, " and ", G.last)
end

function Base.show(io::IO, g::DirectProductElement)
return print(io, "( $(join(g.elts, ",")) )")
print(io, "( ")
join(io, g.elts, ", ")
return print(io, " )")
end

# convienience:
Expand Down
Loading

2 comments on commit 1fbd7b8

@kalmarek
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/100847

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.8.0 -m "<description of version>" 1fbd7b875bd8890674550a265c785fbdd4851960
git push origin v0.8.0

Please sign in to comment.