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

Change more Random() methods to accept random sources (and some related tweaks) #2115

Merged
merged 2 commits into from
Feb 4, 2018
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
16 changes: 0 additions & 16 deletions hpcgap/lib/coll.gd
Original file line number Diff line number Diff line change
Expand Up @@ -1651,22 +1651,6 @@ DeclareOperation( "Random", [ IS_INT, IS_INT ] );
## See <Ref Oper="Reset"/> for a description of how to reset the
## random number generator to a previous state.
## <P/>
##
## <!-- all outdated? (FL)
## Many random methods in the library are eventually based on the function
## <Ref Func="RandomList"/>.
## As <Ref Func="RandomList"/> is restricted to lists of <M>2^{28}</M>
## elements, this may create problems for very large collections. Also note
## that the method used by <Ref Func="RandomList"/> is intended to provide
## a fast algorithm rather than to produce high quality randomness for
## statistical purposes.
## <P/>
## If you implement your own
## <Ref Func="Random" Label="for a list or collection"/> methods we recommend
## that they initialize their seed to a defined value when they are loaded
## to permit to reproduce calculations even if they involved random
## elements.
## -->
## <#/GAPDoc>
##

Expand Down
9 changes: 5 additions & 4 deletions hpcgap/lib/ffeconway.gi
Original file line number Diff line number Diff line change
Expand Up @@ -1562,9 +1562,10 @@ end);
#M Random -- use Rowspace
##

InstallMethod(Random,
[IsField and IsFFECollection and IsFinite],
function(f)
InstallMethodWithRandomSource( Random,
"for a random source and a large non-prime finite field",
[IsRandomSource, IsField and IsFFECollection and IsFinite],
function(rs, f)
local d, p, v, fam;
if Size(f) <= MAXSIZE_GF_INTERNAL then
TryNextMethod();
Expand All @@ -1574,7 +1575,7 @@ InstallMethod(Random,
fi;
d := DegreeOverPrimeField(f);
p := Characteristic(f);
v := Random(RowSpace(GF(p,1),d));
v := Random(rs, RowSpace(GF(p,1),d));
fam := FFEFamily(Characteristic(f));
return Objectify(fam!.ConwayFldEltDefaultType, [v,d,fail]);
end);
Expand Down
11 changes: 5 additions & 6 deletions hpcgap/lib/wordrep.gi
Original file line number Diff line number Diff line change
Expand Up @@ -1283,13 +1283,12 @@ InstallMethod( Position,
##
#M Random( <list> ) . . . . . . . . . . for an infinite list of generators
##
InstallMethod( Random,
"for an infinite list of generators",
true,
[ IsList and IsInfiniteListOfGeneratorsRep ], 0,
function( list )
InstallMethodWithRandomSource( Random,
"for a random source and an infinite list of generators",
[ IsRandomSource, IsList and IsInfiniteListOfGeneratorsRep ], 0,
function( rs, list )
local pos;
pos:= Random( Integers );
pos:= Random( rs, Integers );
if 0 <= pos then
return list[ 2 * pos + 1 ];
else
Expand Down
9 changes: 5 additions & 4 deletions lib/algfld.gi
Original file line number Diff line number Diff line change
Expand Up @@ -895,12 +895,13 @@ end);
##
#M Random
##
InstallMethod(Random,"Alg",true,
[IsAlgebraicExtension],0,
function(e)
InstallMethodWithRandomSource( Random,
"for a random source and an algebraic extension",
[IsRandomSource, IsAlgebraicExtension],
function(rs,e)
local fam,l;
fam:=e!.extFam;
l:=List([1..fam!.deg],i->Random(fam!.baseField));
l:=List([1..fam!.deg],i->Random(rs,fam!.baseField));
l:=ImmutableVector(fam!.rchar,l,true);
return AlgExtElm(fam,l);
end);
Expand Down
16 changes: 0 additions & 16 deletions lib/coll.gd
Original file line number Diff line number Diff line change
Expand Up @@ -1621,22 +1621,6 @@ DeclareOperation( "Random", [ IS_INT, IS_INT ] );
## See <Ref Oper="Reset"/> for a description of how to reset the
## random number generator to a previous state.
## <P/>
##
## <!-- all outdated? (FL)
## Many random methods in the library are eventually based on the function
## <Ref Func="RandomList"/>.
## As <Ref Func="RandomList"/> is restricted to lists of <M>2^{28}</M>
## elements, this may create problems for very large collections. Also note
## that the method used by <Ref Func="RandomList"/> is intended to provide
## a fast algorithm rather than to produce high quality randomness for
## statistical purposes.
## <P/>
## If you implement your own
## <Ref Func="Random" Label="for a list or collection"/> methods we recommend
## that they initialize their seed to a defined value when they are loaded
## to permit to reproduce calculations even if they involved random
## elements.
## -->
## <#/GAPDoc>
##

Expand Down
9 changes: 5 additions & 4 deletions lib/ffeconway.gi
Original file line number Diff line number Diff line change
Expand Up @@ -1532,9 +1532,10 @@ end);
#M Random -- use Rowspace
##

InstallMethod(Random,
[IsField and IsFFECollection and IsFinite],
function(f)
InstallMethodWithRandomSource( Random,
"for a random source and a large non-prime finite field",
[IsRandomSource, IsField and IsFFECollection and IsFinite],
function(rs, f)
local d, p, v, fam;
if Size(f) <= MAXSIZE_GF_INTERNAL then
TryNextMethod();
Expand All @@ -1544,7 +1545,7 @@ InstallMethod(Random,
fi;
d := DegreeOverPrimeField(f);
p := Characteristic(f);
v := Random(RowSpace(GF(p,1),d));
v := Random(rs, RowSpace(GF(p,1),d));
fam := FFEFamily(Characteristic(f));
return Objectify(fam!.ConwayFldEltDefaultType, [v,d,fail]);
end);
Expand Down
4 changes: 2 additions & 2 deletions lib/fieldfin.gi
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,12 @@ InstallMethod( GeneratorsOfLeftModule,
## primitive root, for efficiency reasons.
## All other cases are handled by the vector space methods.
##
InstallMethod( Random,
InstallMethodWithRandomSource( Random,
"for a random source and a finite prime field",
[ IsRandomSource, IsField and IsPrimeField and IsFinite ],
{ rs, F } -> Random(rs,1,Size(F)) * One( F ) );

InstallMethod( Random,
InstallMethodWithRandomSource( Random,
"for a random source and a finite field with known primitive root",
[ IsRandomSource, IsField and IsFinite and HasPrimitiveRoot ],
function ( rs, F )
Expand Down
36 changes: 14 additions & 22 deletions lib/grpffmat.gi
Original file line number Diff line number Diff line change
Expand Up @@ -470,19 +470,15 @@ end);
##
#M Random( <G> ) . . . . . . . . . . . . . . . . . . . . . . for natural GL
##
InstallMethod( Random,
"for natural GL",
true,
[ IsFFEMatrixGroup and IsFinite and IsNaturalGL ],
0,
function(G)
InstallMethodWithRandomSource( Random,
"for a random source and natural GL",
[ IsRandomSource, IsFFEMatrixGroup and IsFinite and IsNaturalGL ],
function(rs, G)
local m;
m := RandomInvertibleMat( DimensionOfMatrixGroup( G ),
m := RandomInvertibleMat( rs, DimensionOfMatrixGroup( G ),
FieldOfMatrixGroup( G ) );
MakeImmutable(m);
ConvertToMatrixRep(m, FieldOfMatrixGroup(G));
return m;
end );
return ImmutableMatrix(FieldOfMatrixGroup(G), m, true);
end);


#############################################################################
Expand All @@ -493,20 +489,16 @@ InstallMethod( Random,
## entry in the upper left corner to arbitrary nonzero values in the field
## $F$ form a set of coset representatives of $SL(n,F)$ in $GL(n,F)$.
##
InstallMethod( Random,
"for natural SL",
true,
[ IsFFEMatrixGroup and IsFinite and IsNaturalSL ],
0,
function( G )
InstallMethodWithRandomSource( Random,
"for a random source and natural SL",
[ IsRandomSource, IsFFEMatrixGroup and IsFinite and IsNaturalSL ],
function(rs, G)
local m;
m:= RandomInvertibleMat( DimensionOfMatrixGroup( G ),
m:= RandomInvertibleMat( rs, DimensionOfMatrixGroup( G ),
FieldOfMatrixGroup( G ) );
MultRowVector(m[1], DeterminantMat(m)^-1);
MakeImmutable(m);
ConvertToMatrixRep(m, FieldOfMatrixGroup(G));
return m;
end );
return ImmutableMatrix(FieldOfMatrixGroup(G), m, true);
end);

#############################################################################
##
Expand Down
8 changes: 5 additions & 3 deletions lib/grpfp.gi
Original file line number Diff line number Diff line change
Expand Up @@ -291,15 +291,17 @@ end );
##
#M Random <gp> )
##
InstallMethod( Random,"fp group", [ IsSubgroupFpGroup and IsFinite],0,
function( gp )
InstallMethodWithRandomSource( Random,
"for a random source and an fp group",
[ IsRandomSource, IsSubgroupFpGroup and IsFinite],
function( rs, gp )
local fam,hom;
fam:=ElementsFamily(FamilyObj(gp));
hom:=FPFaithHom(fam);
if hom=fail then
TryNextMethod();
fi;
return PreImagesRepresentative(hom,Random(Image(hom,gp)));
return PreImagesRepresentative(hom,Random(rs, Image(hom,gp)));
end );

#############################################################################
Expand Down
14 changes: 7 additions & 7 deletions lib/grpfree.gi
Original file line number Diff line number Diff line change
Expand Up @@ -297,14 +297,14 @@ InstallMethod( IsWholeFamily,
##
#T isn't this a generic group method? (without guarantee about distribution)
##
InstallMethod( Random,
"for a free group",
[ IsAssocWordWithInverseCollection and IsGroup ],
function( M )
InstallMethodWithRandomSource( Random,
"for a random source and a free group",
[ IsRandomSource, IsAssocWordWithInverseCollection and IsGroup ],
function( rs, M )
local len, result, gens, i;

# Get a random length for the word.
len:= Random( Integers );
len:= Random( rs, Integers );
if 0 < len then
len:= 2 * len;
elif len < 0 then
Expand All @@ -315,9 +315,9 @@ InstallMethod( Random,

# Multiply 'len' random generator powers.
gens:= GeneratorsOfGroup( M );
result:= Random( gens ) ^ Random( Integers );
result:= Random( rs, gens ) ^ Random( rs, Integers );
for i in [ 2 .. len ] do
result:= result * Random( gens ) ^ Random( Integers );
result:= result * Random( rs, gens ) ^ Random( rs, Integers );
od;

# Return the result.
Expand Down
9 changes: 4 additions & 5 deletions lib/grpnice.gi
Original file line number Diff line number Diff line change
Expand Up @@ -782,11 +782,10 @@ SubgroupMethodByNiceMonomorphism( RadicalGroup,
#M Random( <G> )
##
InstallMethod( Random,
"handled by nice monomorphism",
true,
[ IsGroup and IsHandledByNiceMonomorphism ], 0,
G -> PreImagesRepresentative( NiceMonomorphism( G ),
Random( NiceObject( G ) ) ) );
"for a random source and a group handled by nice monomorphism",
[ IsRandomSource, IsGroup and IsHandledByNiceMonomorphism ], 0,
{rs, G} -> PreImagesRepresentative( NiceMonomorphism( G ),
Random( rs, NiceObject( G ) ) ) );


#############################################################################
Expand Down
13 changes: 5 additions & 8 deletions lib/grppc.gi
Original file line number Diff line number Diff line change
Expand Up @@ -1283,20 +1283,17 @@ end );
##
#M Random( <pcgrp> )
##
InstallMethod( Random,
"pcgs computable groups",
true,
[ IsGroup and CanEasilyComputePcgs and IsFinite ],
0,

function(grp)
InstallMethodWithRandomSource( Random,
"for a random source and a pcgs computable groups",
[ IsRandomSource, IsGroup and CanEasilyComputePcgs and IsFinite ],
function(rs, grp)
local p;

p := Pcgs(grp);
if Length( p ) = 0 then
return One( grp );
else
return Product( p, x -> x^Random(1,RelativeOrderOfPcElement(p,x)) );
return Product( p, x -> x^Random(rs, 1, RelativeOrderOfPcElement(p,x)) );
fi;
end );

Expand Down
18 changes: 9 additions & 9 deletions lib/mgmfree.gi
Original file line number Diff line number Diff line change
Expand Up @@ -233,14 +233,14 @@ InstallMethod( Size,
##
#T use better method for the whole family
##
InstallMethod( Random,
"for a free magma",
[ IsMagma and IsNonassocWordCollection ],
function( M )
InstallMethodWithRandomSource( Random,
"for a random source and a free magma",
[ IsRandomSource, IsMagma and IsNonassocWordCollection ],
function( rs, M )
local len, result, gens, i;

# Get a random length for the word.
len:= Random( Integers );
len:= Random( rs, Integers );
if 0 <= len then
len:= 2 * len;
else
Expand All @@ -249,12 +249,12 @@ InstallMethod( Random,

# Multiply $'len' + 1$ random generators.
gens:= GeneratorsOfMagma( M );
result:= Random( gens );
result:= Random( rs, gens );
for i in [ 1 .. len ] do
if Random( [ 0, 1 ] ) = 0 then
result:= result * Random( gens );
if Random( rs, 0, 1 ) = 0 then
result:= result * Random( rs, gens );
else
result:= Random( gens ) * result;
result:= Random( rs, gens ) * result;
fi;
od;

Expand Down
10 changes: 5 additions & 5 deletions lib/modulmat.gi
Original file line number Diff line number Diff line change
Expand Up @@ -147,13 +147,13 @@ InstallMethod( Dimension,
##
#M Random( <M> )
##
InstallMethod( Random,
"for full matrix module",
[ IsFreeLeftModule and IsFullMatrixModule ],
function( M )
InstallMethodWithRandomSource( Random,
"for a random source and a full matrix module",
[ IsRandomSource, IsFreeLeftModule and IsFullMatrixModule ],
function( rs, M )
local random;
random:= DimensionOfVectors( M );
random:= RandomMat( random[1], random[2],
random:= RandomMat( rs, random[1], random[2],
LeftActingDomain( M ) );
if IsLieObjectCollection( M ) then
random:= LieObject( random );
Expand Down
10 changes: 5 additions & 5 deletions lib/modulrow.gi
Original file line number Diff line number Diff line change
Expand Up @@ -111,13 +111,13 @@ InstallMethod( Dimension,
##
#M Random( <M> )
##1
InstallMethod( Random,
"for full row module",
[ IsFreeLeftModule and IsFullRowModule ],
function( M )
InstallMethodWithRandomSource( Random,
"for a random source and a full row module",
[ IsRandomSource, IsFreeLeftModule and IsFullRowModule ],
function( rs, M )
local R,v;
R:= LeftActingDomain( M );
v := List( [ 1 .. DimensionOfVectors( M ) ], x -> Random( R ) );
v := List( [ 1 .. DimensionOfVectors( M ) ], x -> Random( rs, R ) );
if IsField(R) then
if IsHPCGAP then
if Size(R) <= 256 then
Expand Down
Loading