Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow creating random permutations using a random source #1165

Merged
merged 1 commit into from
Feb 25, 2017
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
42 changes: 20 additions & 22 deletions lib/gpprmsya.gi
Original file line number Diff line number Diff line change
Expand Up @@ -748,12 +748,12 @@ InstallMethod( Size,
[ IsNaturalSymmetricGroup ], 0,
sym -> Factorial( NrMovedPoints(sym) ) );

BindGlobal("FLOYDS_ALGORITHM", function(deg, even)
BindGlobal("FLOYDS_ALGORITHM", function(rs, deg, even)
local rnd, sgn, i, k, tmp;
rnd := [1..deg];
sgn := 1;
for i in [1..deg-1] do
k := Random( [ i .. deg] );
k := Random( rs, [ i .. deg] );
if k <> i then
tmp := rnd[i];
rnd[i] := rnd[k];
Expand All @@ -771,16 +771,15 @@ end);



InstallMethod( Random,
"symmetric group: floyd's algorithm",
InstallMethodWithRandomSource( Random,
"for a random source and a natural symmetric group: floyd's algorithm",
true,
[ IsNaturalSymmetricGroup ],
10, # override perm. gp. method
function ( G )
[ IsRandomSource, IsNaturalSymmetricGroup ],
10, # override perm group method
function ( rs, G )
local rnd, # random permutation, result
deg,
mov;

deg,
mov;

# test for internal rep
if HasGeneratorsOfGroup(G) and
Expand All @@ -791,34 +790,33 @@ function ( G )
# use Floyd\'s algorithm
mov:=MovedPoints(G);
deg:=Length(mov);
rnd := FLOYDS_ALGORITHM(deg,false);
rnd:=FLOYDS_ALGORITHM(rs,deg,false);

# return the permutation
return PermList( rnd )^MappingPermListList([1..deg],mov);
end);


InstallMethod( Random,
"alternating group: floyd's algorithm",
InstallMethodWithRandomSource( Random,
"for a random source and a natural alternating group: floyd's algorithm",
true,
[ IsNaturalAlternatingGroup ],
10, # override perm gp. method
function ( G )
[ IsRandomSource, IsNaturalAlternatingGroup ],
10, # override perm group method
function ( rs, G )
local rnd, # random permutation, result
sgn, # sign of the permutation so far
tmp, # temporary variable for swapping
deg,
mov,
i, k; # loop variables
deg,
mov;

# test for internal rep
if HasGeneratorsOfGroup(G) and
not ForAll(GeneratorsOfGroup(G),IsInternalRep) then
TryNextMethod();
fi;

# use Floyd\'s algorithm
mov:=MovedPoints(G);
deg:=Length(mov);
rnd := FLOYDS_ALGORITHM(deg, true);
rnd:=FLOYDS_ALGORITHM(rs,deg,true);

# return the permutation
return PermList( rnd )^MappingPermListList([1..deg],mov);
Expand Down
18 changes: 18 additions & 0 deletions tst/testinstall/random.tst
Original file line number Diff line number Diff line change
@@ -1,18 +1,36 @@
gap> START_TEST("random.tst");
gap> Read( Filename( DirectoriesLibrary( "tst" ), "testrandom.g" ) );

#
gap> randomTest(Integers, Random);
gap> randomTest(Rationals, Random);

#
gap> randomTest(SymmetricGroup(2), Random);
gap> randomTest(SymmetricGroup(3), Random);
gap> randomTest(SymmetricGroup(4), Random);
gap> randomTest(AlternatingGroup(3), Random);
gap> randomTest(AlternatingGroup(4), Random);
gap> randomTest(AlternatingGroup(5), Random);

#
gap> randomTest(Group((1,2),(3,4)), Random);
gap> randomTest(PrimitiveGroup(5,2), Random);
gap> randomTest(PrimitiveGroup(5,3), Random);
gap> randomTest(Group((1,2),(3,4))*(1,2,3), Random);
gap> randomTest(PrimitiveGroup(5,2)*(1,2,6), Random);
gap> randomTest(PrimitiveGroup(5,3)*(1,4,6), Random);

#
gap> randomTest(DoubleCoset(Group((1,2),(3,4)), (1,2,3,4,5,6), Group((1,2,3)) ), Random);
gap> randomTest(DoubleCoset(Group(()), (1,2), Group((1,2,3)) ), Random);
gap> randomTest(DoubleCoset(Group((1,2),(3,4)), (), Group((1,2,3)) ), Random);

#
gap> randomTest([1..10], Random);
gap> randomTest([1,-6,"cheese", Group(())], Random);

#
gap> randomTest(PadicExtensionNumberFamily(3, 5, [1,1,1], [1,1]), Random, function(x,y) return IsPadicExtensionNumber(x); end);
gap> randomTest(PurePadicNumberFamily(2,20), Random, function(x,y) return IsPurePadicNumber(x); end);
gap> STOP_TEST("random.tst", 1);