Skip to content

Commit

Permalink
lib: allow creating random permutations using a random source
Browse files Browse the repository at this point in the history
Also add some tests for Random on natural symmetric and alternating groups
  • Loading branch information
fingolfin authored and ChrisJefferson committed Feb 25, 2017
1 parent 413dc3e commit ee504a1
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 22 deletions.
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);

0 comments on commit ee504a1

Please sign in to comment.