Skip to content

Commit

Permalink
Speed up ElementsStabChain and AsList for perm groups
Browse files Browse the repository at this point in the history
The documentation for `ElementsStabChain` never claimed that the result
it returns is sorted. So we change it to not do that, by using `Append`
instead of `UniteSet`. Then we use that to provide a faster version of
`AsList`. As a side effect, this actually even speeds up `AsSet`, as
sorting only at the end is cheaper then sorting repeatedly during
creation of the list.

Some timings:

Before:

    gap> AsList(SymmetricGroup(10));; time;
    1507

    gap> AsSet(SymmetricGroup(10));; time;
    1492

After:

    gap> AsList(SymmetricGroup(10)); time;
    611

    gap> AsSet(SymmetricGroup(10)); time;
    1120

As another side effect, the output of `AsList(G)` and
`List(Iterator(G))` now seem to coincide if `G` is a permutation group.
  • Loading branch information
fingolfin committed Dec 5, 2022
1 parent ed4d7f8 commit f01b270
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 21 deletions.
2 changes: 1 addition & 1 deletion doc/ref/grpperm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ gap> Length(MovedPoints(p));
720
gap> q:=SmallerDegreePermutationRepresentation(p);;
gap> NrMovedPoints(Image(q));
12
6
]]></Example>

</Section>
Expand Down
6 changes: 3 additions & 3 deletions lib/coll.gd
Original file line number Diff line number Diff line change
Expand Up @@ -1732,12 +1732,12 @@ DeclareOperation( "Random", [ IS_INT, IS_INT ] );
## 3
## gap> elms:= AsList( Group( (1,2,3) ) );;
## gap> RandomList( elms ); RandomList( elms );
## (1,2,3)
## (1,3,2)
## (1,2,3)
## gap> rs:= RandomSource( IsMersenneTwister, 1 );
## <RandomSource in IsMersenneTwister>
## gap> RandomList( rs, elms );
## (1,2,3)
## (1,3,2)
## ]]></Example>
## </Description>
## </ManSection>
Expand Down Expand Up @@ -2286,7 +2286,7 @@ DeclareSynonym( "Set", SSortedList );
## false
## true
## gap> AsList( Group( (1,2,3), (1,2) ) );
## [ (), (2,3), (1,2), (1,2,3), (1,3,2), (1,3) ]
## [ (), (2,3), (1,3,2), (1,3), (1,2,3), (1,2) ]
## ]]></Example>
## </Description>
## </ManSection>
Expand Down
17 changes: 9 additions & 8 deletions lib/grpperm.gi
Original file line number Diff line number Diff line change
Expand Up @@ -2201,21 +2201,22 @@ end);
##
#M AsList( <G> ) elements of perm group
##
InstallMethod( AsList,"permgp: AsSSortedList", true,
[ IsPermGroup ], 0, AsSSortedList );
InstallMethod( AsList, "permgp: AsSSortedList", [ IsPermGroup ],
G -> ElementsStabChain( StabChainMutable(G)) );

#############################################################################
##
#M AsSSortedList( <G> ) elements of perm group
##
InstallMethod( AsSSortedList,"via stabchain",true, [ IsPermGroup ], 0,
function( G )
return ElementsStabChain( StabChainMutable(G));
end );
InstallMethod( AsSSortedList, "via stabchain", [ IsPermGroup ],
AsSSortedListNonstored );

InstallMethod( AsSSortedListNonstored,"via stabchain", true, [ IsPermGroup ], 0,
InstallMethod( AsSSortedListNonstored, "via stabchain", [ IsPermGroup ],
function( G )
return ElementsStabChain( StabChainMutable(G));
local list;
list := ElementsStabChain( StabChainMutable(G));
Sort(list);
return list;
end );

#############################################################################
Expand Down
2 changes: 1 addition & 1 deletion lib/oprt.gd
Original file line number Diff line number Diff line change
Expand Up @@ -1192,7 +1192,7 @@ DeclareGlobalFunction( "ImageElmActionHomomorphism" );
## <Example><![CDATA[
## gap> g:=Group((1,2,3),(1,2));;
## gap> Action(g,AsList(g),OnRight);
## Group([ (1,4,5)(2,3,6), (1,3)(2,4)(5,6) ])
## Group([ (1,5,3)(2,6,4), (1,6)(2,5)(3,4) ])
## ]]></Example>
## </Description>
## </ManSection>
Expand Down
2 changes: 1 addition & 1 deletion lib/stbc.gd
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,7 @@ DeclareGlobalFunction( "OrbitStabChain" );
##
## <Description>
## returns a list of all elements of the group described by the stabilizer
## chain <A>S</A>.
## chain <A>S</A>. The list is duplicate free but may be unsorted.
## </Description>
## </ManSection>
## <#/GAPDoc>
Expand Down
2 changes: 1 addition & 1 deletion lib/stbc.gi
Original file line number Diff line number Diff line change
Expand Up @@ -1756,7 +1756,7 @@ InstallGlobalFunction(ElementsStabChain,function ( S )
while S.orbit[1] ^ rep <> pnt do
rep := LeftQuotient( S.transversal[pnt/rep], rep );
od;
UniteSet( elms, stb * rep );
Append( elms, stb * rep );

od;

Expand Down
12 changes: 6 additions & 6 deletions tst/testinstall/grp/basic.tst
Original file line number Diff line number Diff line change
Expand Up @@ -107,13 +107,13 @@ Alt( [ 2, 4, 17 ] )

#
gap> AlternatingGroup( IsRegular, 3 );
Group([ (1,2,3) ])
Group([ (1,3,2) ])
gap> AlternatingGroup( IsRegular, 4 );
Group([ (1,5,7)(2,4,8)(3,6,9)(10,11,12), (1,2,3)(4,7,10)(5,9,11)(6,8,12) ])
Group([ (1,12,8)(2,10,9)(3,11,7)(4,5,6), (1,3,2)(4,12,9)(5,10,7)(6,11,8) ])
gap> AlternatingGroup( IsRegular, [2,4,6] );
Group([ (1,2,3) ])
Group([ (1,3,2) ])
gap> AlternatingGroup( IsRegular, [2,4,6,7] );
Group([ (1,5,7)(2,4,8)(3,6,9)(10,11,12), (1,2,3)(4,7,10)(5,9,11)(6,8,12) ])
Group([ (1,12,8)(2,10,9)(3,11,7)(4,5,6), (1,3,2)(4,12,9)(5,10,7)(6,11,8) ])

#
gap> AlternatingGroup(2,3);
Expand Down Expand Up @@ -548,9 +548,9 @@ Sym( [ 2, 4, 17 ] )

#
gap> SymmetricGroup( IsRegular, 3 );
Group([ (1,4,5)(2,3,6), (1,3)(2,4)(5,6) ])
Group([ (1,5,4)(2,6,3), (1,6)(2,5)(3,4) ])
gap> SymmetricGroup( IsRegular, [2,4,6] );
Group([ (1,4,5)(2,3,6), (1,3)(2,4)(5,6) ])
Group([ (1,5,4)(2,6,3), (1,6)(2,5)(3,4) ])

#
gap> SymmetricGroup(2,3);
Expand Down
10 changes: 10 additions & 0 deletions tst/testinstall/grpperm.tst
Original file line number Diff line number Diff line change
Expand Up @@ -94,4 +94,14 @@ gap> Index(sym,b);
3603600
gap> Length(ContainedConjugates(sym,a,b));
5

#
gap> AsSet(SymmetricGroup(3));
[ (), (2,3), (1,2), (1,2,3), (1,3,2), (1,3) ]
gap> AsList(SymmetricGroup(3));
[ (), (2,3), (1,3), (1,3,2), (1,2,3), (1,2) ]
gap> List(Iterator(SymmetricGroup(3)));
[ (), (2,3), (1,3), (1,3,2), (1,2,3), (1,2) ]
gap> AsList(SymmetricGroup(6)) = List(Iterator(SymmetricGroup(6)));
true
gap> STOP_TEST( "grpperm.tst", 1);

0 comments on commit f01b270

Please sign in to comment.