Skip to content

Commit

Permalink
lib: use InstallMethodWithRandomSource in more places
Browse files Browse the repository at this point in the history
  • Loading branch information
fingolfin committed Feb 4, 2018
1 parent 36d4827 commit a88ea01
Show file tree
Hide file tree
Showing 18 changed files with 187 additions and 107 deletions.
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
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
18 changes: 11 additions & 7 deletions lib/monofree.gi
Original file line number Diff line number Diff line change
Expand Up @@ -89,14 +89,18 @@ InstallMethod( Enumerator,
##
#T use better method for the whole family, and for abelian monoids
##
InstallMethod( Random,
"for a free monoid",
[ IsMonoid and IsAssocWordWithOneCollection ],
function( M )
InstallMethodWithRandomSource( Random,
"for a random source and a free monoid",
[ IsRandomSource, IsMonoid and IsAssocWordWithOneCollection ],
function( rs, M )
local len, result, gens, i;

if IsTrivial( M ) then
return One( M );
fi;

# 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 @@ -107,9 +111,9 @@ InstallMethod( Random,

# Multiply 'len' random generators.
gens:= GeneratorsOfMagmaWithOne( M );
result:= Random( gens );
result:= Random( rs, gens );
for i in [ 2 .. len ] do
result:= result * Random( gens );
result:= result * Random( rs, gens );
od;

# Return the result.
Expand Down
11 changes: 6 additions & 5 deletions lib/vspchom.gi
Original file line number Diff line number Diff line change
Expand Up @@ -1869,19 +1869,20 @@ InstallMethod( Dimension,
##
#M Random( <M> ) . . . . . . . . . . . for full hom space of linear mappings
##
InstallMethod( Random,
"for full hom space of linear mappings",
[ IsFreeLeftModule and IsGeneralMappingCollection
InstallMethodWithRandomSource( Random,
"for a random source and full hom space of linear mappings",
[ IsRandomSource, IsFreeLeftModule and IsGeneralMappingCollection
and IsFullHomModule ],
function( M )
function( rs, M )
local BS, BR;

BR:= NiceFreeLeftModuleInfo( M );
BS:= BR.basissource;
BR:= BR.basisrange;

return LeftModuleHomomorphismByMatrix( BS,
RandomMat( Dimension( UnderlyingLeftModule( BS ) ),
RandomMat( rs,
Dimension( UnderlyingLeftModule( BS ) ),
Dimension( UnderlyingLeftModule( BR ) ),
LeftActingDomain( M ) ),
BR );
Expand Down
Loading

0 comments on commit a88ea01

Please sign in to comment.