-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #50 from kalmarek/mk/rand_via_pra
implement rand via Product Replacement Algorithm
- Loading branch information
Showing
9 changed files
with
64 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
name = "GroupsCore" | ||
uuid = "d5909c97-4eac-4ecc-a3dc-fdd0858a4120" | ||
authors = ["Marek Kaluba <[email protected]> and contributors"] | ||
version = "0.4.2" | ||
authors = ["Marek Kaluba <[email protected]>"] | ||
version = "0.5.0" | ||
|
||
[deps] | ||
Markdown = "d6f4376e-aef5-505a-96c1-9c027394607a" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import Random | ||
|
||
""" | ||
PRASampler | ||
Product Replacement Algorithm sampler for arbitrary group generated by | ||
an explicit finite set of generators. | ||
""" | ||
mutable struct PRASampler{T} <: Random.Sampler{T} | ||
gentuple::Vector{T} | ||
right::T | ||
left::T | ||
end | ||
|
||
# constants taken from GAP | ||
function PRASampler(G, n::Integer=2ngens(G) + 10, scramble_time::Integer=10max(n, 10)) | ||
return PRASampler(Random.default_rng(), G, n, scramble_time) | ||
end | ||
|
||
function Random.Sampler( | ||
RNG::Type{<:Random.AbstractRNG}, | ||
G::Group, | ||
repetition::Random.Repetition=Val(Inf) | ||
) | ||
return PRASampler(RNG(), G) | ||
end | ||
|
||
function PRASampler( | ||
rng::Random.AbstractRNG, | ||
G::Group, | ||
n::Integer=2ngens(G) + 10, | ||
scramble_time::Integer=10max(n, 10) | ||
) | ||
if istrivial(G) | ||
return PRASampler(fill(one(G), n), one(G), one(G)) | ||
end | ||
@assert hasgens(G) | ||
l = max(n, 2ngens(G), 2) | ||
sampler = let S = gens(G) | ||
S = union!(inv.(S), S) | ||
append!(S, rand(rng, S, l - length(S))) | ||
PRASampler(S, one(G), one(G)) | ||
end | ||
for _ in 1:scramble_time | ||
_ = rand(rng, sampler) | ||
end | ||
return sampler | ||
end | ||
|
||
function Random.rand(rng::Random.AbstractRNG, pra::PRASampler) | ||
range = 1:length(pra.gentuple) | ||
|
||
pra.right = pra.right * rand(rng, pra.gentuple) | ||
|
||
i = rand(rng, range) | ||
@inbounds pra.gentuple[i] = pra.gentuple[i] * pra.right | ||
|
||
j = rand(rng, range) | ||
@inbounds pra.left = pra.left * pra.gentuple[j] | ||
|
||
return pra.left | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters