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

WIP -- Accumulators (do not merge) #567

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
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
21 changes: 21 additions & 0 deletions lib/accum.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
DeclareCategory("IsAccumulator", IsCopyable);

DeclareOperation("ValueAccumulator", [IsAccumulator]);

DeclareConstructor("AccumulatorCons", [IsAccumulator, IsObject]);

DeclareOperation("RightAdd", [IsAccumulator and IsMutable, IsExtAElement] );
DeclareOperation("LeftAdd", [IsAccumulator and IsMutable, IsExtAElement] );
DeclareOperation("Subtract", [IsAccumulator and IsMutable, IsNearAdditiveElementWithInverse] );
DeclareOperation("Negate", [IsAccumulator and IsMutable] );
DeclareOperation("RightMultiply", [IsAccumulator and IsMutable, IsExtLElement] );
DeclareOperation("LeftMultiply", [IsAccumulator and IsMutable, IsExtRElement] );
DeclareOperation("RightDivide", [IsAccumulator and IsMutable, IsMultiplicativeElementWithInverse] );
DeclareOperation("LeftDivide", [IsAccumulator and IsMutable, IsMultiplicativeElementWithInverse] );
DeclareOperation("Invert", [IsAccumulator and IsMutable] );
DeclareOperation("Conjugate", [IsAccumulator and IsMutable, IsMultiplicativeElementWithInverse] );

DeclareCategory("IsPermutationAccumulator", IsAccumulator);
DeclareOperation("OnPointsAccumulator", [IsPosInt, IsPermutationAccumulator]);
DeclareOperation("OnTuplesAccumulator", [IsRowVector and IsCyclotomicCollection,
IsPermutationAccumulator]);
41 changes: 41 additions & 0 deletions lib/eagerpermacc.gi
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@

DeclareRepresentation("IsEagerPermutationAccumulatorRep",
IsDataObjectRep and IsPermutationAccumulator, 2);

BindGlobal("DefaultTypeEagerPermAccumulator",
NewType(AccumulatorsFamily, IsMutable and IsEagerPermutationAccumulatorRep));

InstallMethod(AccumulatorCons,[IsEagerPermutationAccumulatorRep, IsPerm and IsInternalRep],
function(t,p)
return NEW_PERMACC(p);
end);

InstallMethod(ValueAccumulator, [IsEagerPermutationAccumulatorRep],
VALUE_PERMACC);

InstallMethod(RightMultiply, [IsEagerPermutationAccumulatorRep and IsMutable, IsPerm and IsInternalRep],
RIGHT_MULTIPLY_PERMACC);

InstallMethod(LeftMultiply, [IsEagerPermutationAccumulatorRep and IsMutable, IsPerm and IsInternalRep],
LEFT_MULTIPLY_PERMACC);

InstallMethod(RightDivide, [IsEagerPermutationAccumulatorRep and IsMutable, IsPerm and IsInternalRep],
RIGHT_DIVIDE_PERMACC);

InstallMethod(LeftDivide, [IsEagerPermutationAccumulatorRep and IsMutable, IsPerm and IsInternalRep],
LEFT_DIVIDE_PERMACC);

InstallMethod(Invert, [IsEagerPermutationAccumulatorRep and IsMutable],
INVERT_PERMACC);

InstallMethod(ShallowCopy, [IsEagerPermutationAccumulatorRep],
SHALLOWCOPY_PERMACC);

InstallMethod(OnPointsAccumulator, [IsPosInt, IsEagerPermutationAccumulatorRep, ],
ONPOINTS_PERMACC);






15 changes: 15 additions & 0 deletions lib/genacc.gi
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
BindGlobal("AccumulatorsFamily", NewFamily(IsAccumulator));


InstallMethod(Conjugate, [IsAccumulator and IsMutable, IsMultiplicativeElementWithInverse],
function(acc, x)
acc := LeftDivide(acc, x);
if acc = fail then
return fail;
fi;
return RightMultiply(acc,x);
end);




36 changes: 24 additions & 12 deletions lib/gpprmsya.gi
Original file line number Diff line number Diff line change
Expand Up @@ -752,12 +752,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 @@ -778,9 +778,9 @@ end);
InstallMethod( Random,
"symmetric group: floyd's algorithm",
true,
[ IsNaturalSymmetricGroup ],
[ IsRandomSource, IsNaturalSymmetricGroup ],
10, # override perm. gp. method
function ( G )
function ( rs, G )
local rnd, # random permutation, result
deg,
mov;
Expand All @@ -795,7 +795,7 @@ 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);
Expand All @@ -804,15 +804,13 @@ end);
InstallMethod( Random,
"alternating group: floyd's algorithm",
true,
[ IsNaturalAlternatingGroup ],
[ IsRandomSource, IsNaturalAlternatingGroup ],
10, # override perm gp. method
function ( G )
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
mov;


# test for internal rep
if HasGeneratorsOfGroup(G) and
Expand All @@ -822,12 +820,26 @@ function ( G )
# 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);
end);

InstallMethod( Random,
"symmetric group: use default random source",
true,
[IsNaturalSymmetricGroup],
10,
G->Random(GlobalRandomSource, G));

InstallMethod( Random,
"alternating group: use default random source",
true,
[IsNaturalAlternatingGroup],
10,
G->Random(GlobalRandomSource, G));


#############################################################################
##
Expand Down
115 changes: 115 additions & 0 deletions lib/lazypermacc.gi
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
DeclareRepresentation("IsLazyPermutationAccumulatorRep",
IsPositionalObjectRep and IsPermutationAccumulator, 2);

BindGlobal("LazyPermutationAccumulatorDefaultType",
NewType(AccumulatorsFamily, IsMutable and IsLazyPermutationAccumulatorRep));


InstallMethod(AccumulatorCons,[IsLazyPermutationAccumulatorRep, IsPerm],
function(type, p)
return Objectify(LazyPermutationAccumulatorDefaultType, [[],[p],[],[true]]);
end);

InstallMethod(RightMultiply, [IsLazyPermutationAccumulatorRep and IsMutable, IsPerm],
function(acc,p)
Add(acc![2],p);
Add(acc![4],true);
return acc;
end);

InstallMethod(LeftMultiply, [IsLazyPermutationAccumulatorRep and IsMutable, IsPerm],
function(acc,p)
Add(acc![1],p);
Add(acc![3],true);
return acc;
end);

InstallMethod(RightDivide, [IsLazyPermutationAccumulatorRep and IsMutable, IsPerm],
function(acc,p)
Add(acc![2],p);
Add(acc![4],false);
return acc;

end);

InstallMethod(LeftDivide, [IsLazyPermutationAccumulatorRep and IsMutable, IsPerm],
function(acc,p)
Add(acc![1],p);
Add(acc![3],false);
return acc;
end);

InstallMethod(Invert, [IsLazyPermutationAccumulatorRep and IsMutable],
function(acc)
local x;
x := acc![1];
acc![1] := acc![2];
acc![2] := x;
x := acc![3];
acc![3] := List(acc![4], y-> not y);
acc![4] := List(x, y-> not y);
return acc;
end);


InstallMethod(ShallowCopy,[IsLazyPermutationAccumulatorRep],
function(acc)
return Objectify(LazyPermutationAccumulatorDefaultType, List([1..4],
i -> ShallowCopy(acc![i])));
end);

#
# Could be implemented better usign an EagerPermutationAccumulator
#

InstallMethod(ValueAccumulator, [IsLazyPermutationAccumulatorRep],
function(acc)
local v, l, i;
v := ();
l := Length(acc![1]);
for i in [l,l-1..1] do
if acc![3][i] then
v := v*acc![1][i];
else
v := v/acc![1][i];
fi;
od;
for i in [1..Length(acc![2])] do
if acc![4][i] then
v := v*acc![2][i];
else
v := v/acc![2][i];
fi;
od;
acc![2] := [v];
acc![1] := [];
acc![3] := [];
acc![4] := [true];
return v;
end);


InstallMethod(OnPointsAccumulator,[IsPosInt, IsLazyPermutationAccumulatorRep],
function(pt, acc)
local l, i;
l := Length(acc![1]);
for i in [l,l-1..1] do
if acc![3][i] then
pt := pt^acc![1][i];
else
pt := pt/acc![1][i];
fi;
od;
for i in [1..Length(acc![2])] do
if acc![4][i] then
pt := pt^acc![2][i];
else
pt := pt/acc![2][i];
fi;
od;
return pt;
end);



#T View/Print
12 changes: 12 additions & 0 deletions lib/permacc.gi
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
InstallMethod(RightAdd,[IsPermutationAccumulator and IsMutable, IsExtAElement], ReturnFail);
InstallMethod(LeftAdd,[IsPermutationAccumulator and IsMutable, IsExtAElement], ReturnFail);
InstallMethod(Subtract,[IsPermutationAccumulator and IsMutable,
IsNearAdditiveElementWithInverse], ReturnFail);
InstallMethod(Negate,[IsPermutationAccumulator and IsMutable], ReturnFail);

InstallMethod(OnTuplesAccumulator,
[IsSmallList and IsCyclotomicCollection and IsRowVector, IsPermutationAccumulator],
function(tup,acc)
return List(tup, x -> OnPointsAccumulator(acc,x));
end);

1 change: 1 addition & 0 deletions lib/random.gd
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ DeclareCategory( "IsRandomSource", IsComponentObjectRep );
## <#/GAPDoc>
##
DeclareOperation( "Random", [IsRandomSource, IsList] );
DeclareOperation( "Random", [IsRandomSource, IsCollection and IsFinite] );
DeclareOperation( "Random", [IsRandomSource, IsInt, IsInt] );

#############################################################################
Expand Down
1 change: 1 addition & 0 deletions lib/read1.g
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,4 @@ ReadLib( "session.g" );

ReadLib( "float.gd" );
ReadLib( "macfloat.g" );

5 changes: 5 additions & 0 deletions lib/read3.g
Original file line number Diff line number Diff line change
Expand Up @@ -251,3 +251,8 @@ ReadLib("function.gd");

# random sources
ReadLib("random.gi");

# accumulators

ReadLib("accum.gd");

8 changes: 8 additions & 0 deletions lib/read5.g
Original file line number Diff line number Diff line change
Expand Up @@ -258,3 +258,11 @@ ReadLib( "function.gi");
# floateans, now really install all handlers
ReadLib( "float.gi" );
ReadLib( "ieee754.g" );

# accumulators

ReadLib("genacc.gi");
ReadLib("permacc.gi");
ReadLib("lazypermacc.gi");
ReadLib("eagerpermacc.gi");

Loading