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

ImmutableMatrix creates MatrixObj for Z/nZ #4797

Merged
merged 4 commits into from
Apr 22, 2022
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
21 changes: 14 additions & 7 deletions lib/dicthf.gi
Original file line number Diff line number Diff line change
Expand Up @@ -110,14 +110,21 @@ local f,n,bytelen,data,qq,i;
return function(v)
local x,sy,p;
sy := 0;
for x in v do
p := Position(f, x);
if IsZmodnZVectorRep(v) then
for x in v![ELSPOS] do
sy := n*sy + (x-1);
od;
else
for x in v do
p := Position(f, x);
# want to be quick: Assume no failures
# if p = fail then
# Error("NumberFFVector: Vector not over specified field");
# fi;
sy := n*sy + (p-1);
od;
# if p = fail then
# Error("NumberFFVector: Vector not over specified field");
# fi;
sy := n*sy + (p-1);
od;
fi;

return sy;
end;
fi;
Expand Down
19 changes: 16 additions & 3 deletions lib/grpffmat.gi
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,14 @@ end );
InstallMethod(FieldOfMatrixList,"finite field matrices",true,
[IsListOrCollection and IsFFECollCollColl],0,
function(l)
local deg, i, j, char;
local deg, i, j, char,B;
if Length(l)=0 then Error("list must be nonempty");fi;
if ForAll( l, HasBaseDomain ) then
B:= BaseDomain( l[1] );
if ForAll( l, x -> B = BaseDomain( x ) ) then
return B;
fi;
fi;
deg := 1;
for i in l do
for j in i do
Expand All @@ -58,8 +64,15 @@ end);
InstallMethod(DefaultScalarDomainOfMatrixList,"finite field matrices",true,
[IsListOrCollection and IsFFECollCollColl],0,
function(l)
local deg, i, j, char,m;
if Length(l)=0 then Error("list must be nonempty");fi;
local deg, i, j, char,m,B;
if Length(l)=0 then Error("list must be nonempty");fi;
if ForAll( l, HasBaseDomain ) then
B:= BaseDomain( l[1] );
if ForAll( l, x -> B = BaseDomain( x ) ) then
return B;
fi;
fi;

deg := 1;
for i in l do
# treat compact matrices quickly
Expand Down
83 changes: 83 additions & 0 deletions lib/grpmat.gi
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,7 @@ local field, dict, acts, start, j, zerov, zero, dim, base, partbas, heads,
else
Error("illegal action");
fi;
start:=List(start,x->ImmutableVector(field,x));

zerov:=Zero(start[1]);
zero:=zerov[1];
Expand Down Expand Up @@ -355,7 +356,14 @@ local field, dict, acts, start, j, zerov, zero, dim, base, partbas, heads,
if v<>zerov then
Add(base,orb[i]);
Add(partbas,ShallowCopy(orb[i]));
# filter for vector objects, not compressed FF vectors
if ForAny(partbas,x->IsVectorObj(x) and not IsDataObjectRep(x)) then
partbas:=Matrix(BaseDomain(partbas[1]),partbas);
fi;
TriangulizeMat(partbas);
if IsMatrixObj(partbas) then
partbas:=ShallowCopy(RowsOfMatrix(partbas));
fi;
heads:=List(partbas,PositionNonZero);
if Length(partbas)>=dim then
# full dimension reached
Expand Down Expand Up @@ -972,6 +980,81 @@ local G,fam,typ,f;
return G;
end );

# ditto for nonprime ZmodnZ
InstallMethod( GroupWithGenerators, "list of zmodnz matrices",
[ IsZmodnZObjNonprimeCollCollColl ],
#T ???
function( gens )
local G,typ,f;

if not IsFinite(gens) then TryNextMethod(); fi;
typ:=MakeGroupyType(FamilyObj(gens),
IsGroup and IsAttributeStoringRep
and HasGeneratorsOfMagmaWithInverses
and IsFinitelyGeneratedGroup and HasIsEmpty and IsFinite,
gens,false,true);

f:=DefaultScalarDomainOfMatrixList(gens);
gens:=List(Immutable(gens),i->ImmutableMatrix(f,i));

G:=rec();
ObjectifyWithAttributes(G,typ,GeneratorsOfMagmaWithInverses,AsList(gens));

if IsField(f) then SetDefaultFieldOfMatrixGroup(G,f);fi;

return G;
end );

InstallMethod( GroupWithGenerators,
"list of zmodnz matrices with identity", IsCollsElms,
[ IsZmodnZObjNonprimeCollCollColl,IsMultiplicativeElementWithInverse and
IsZmodnZObjNonprimeCollColl],
function( gens, id )
local G,typ,f;

if not IsFinite(gens) then TryNextMethod(); fi;
typ:=MakeGroupyType(FamilyObj(gens), IsGroup and IsAttributeStoringRep
and HasGeneratorsOfMagmaWithInverses and IsFinitelyGeneratedGroup
and HasIsEmpty and IsFinite and HasOne,
gens,id,true);

f:=DefaultScalarDomainOfMatrixList(gens);
gens:=List(Immutable(gens),i->ImmutableMatrix(f,i));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we make an immutable copy of gens before applying List to it? OK, you just copied the code (and that's fine), but I am wondering anyway, and asking in case you have an idea why this is done (if not, please simply ignore this question :-) ).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suspect this is the result of a global insertion of ImmutableMatrix in many places without considering in each place whether the local code could be improved subsequently.

id:=ImmutableMatrix(f,id);

G:=rec();
ObjectifyWithAttributes(G,typ,GeneratorsOfMagmaWithInverses,AsList(gens),
One,id);

if IsField(f) then SetDefaultFieldOfMatrixGroup(G,f);fi;

return G;
end );

InstallMethod( GroupWithGenerators,
"empty list of zmodnz matrices with identity", true,
[ IsList and IsEmpty,
IsMultiplicativeElementWithInverse and IsZmodnZObjNonprimeCollColl],
function( gens, id )
local G,fam,typ,f;

if not IsFinite(gens) then TryNextMethod(); fi;
typ:=MakeGroupyType(FamilyObj([id]), IsGroup and IsAttributeStoringRep
and HasGeneratorsOfMagmaWithInverses and HasOne and IsTrivial,
gens,id,true);

f:=DefaultScalarDomainOfMatrixList([id]);
id:=ImmutableMatrix(f,id);

G:=rec();
ObjectifyWithAttributes(G,typ,GeneratorsOfMagmaWithInverses,AsList(gens),
One,id);

if IsField(f) then SetDefaultFieldOfMatrixGroup(G,f);fi;

return G;
end );


#############################################################################
##
Expand Down
114 changes: 103 additions & 11 deletions lib/matobjnz.gi
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,20 @@ InstallMethod( NewZeroVector, "for IsZmodnZVectorRep, a ring, and an int",
end );

InstallMethod( ViewObj, "for a zmodnz vector", [ IsZmodnZVectorRep ],
function( v )
function( v )
local l;
if not IsMutable(v) then
Print("<immutable ");
else
Print("<");
fi;
Print("zmodnz vector over ",v![BDPOS]," of length ",Length(v![ELSPOS]),">");
Print("vector mod ",Size(v![BDPOS]));
l:=Length(v![ELSPOS]);
if 0<l and l<=8 then
Print(": ",v![ELSPOS],">");
else
Print(" of length ",Length(v![ELSPOS]),">");
fi;
end );

InstallMethod( PrintObj, "for a zmodnz vector", [ IsZmodnZVectorRep ],
Expand Down Expand Up @@ -395,6 +402,25 @@ local i,m;
fi;
end );

InstallOtherMethod( AddRowVector, "for plist, zmodnz vector, and a scalar",
[ IsPlistRep and IsMutable, IsZmodnZVectorRep, IsObject ],
function( a, b, s )
local i,m;
if not ForAll(a,IsModulusRep) then TryNextMethod();fi;
for i in [1..Length(a)] do
a[i]:=a[i]+b[i]*s;
od;
end);

InstallOtherMethod( AddRowVector, "for plist, plist vector, and a scalar",
[ IsPlistRep and IsMutable, IsPlistVectorRep, IsObject ],
function( a, b, s )
local i,m;
for i in [1..Length(a)] do
a[i]:=a[i]+b[i]*s;
od;
end);

InstallMethod( AddRowVector,
"for two zmodnz vectors, a scalar, and two positions",
[ IsZmodnZVectorRep and IsMutable, IsZmodnZVectorRep,
Expand Down Expand Up @@ -731,9 +757,16 @@ InstallMethod( Matrix, "for a list and a zmodnz matrix",

InstallMethod( ViewObj, "for a zmodnz matrix", [ IsZmodnZMatrixRep ],
function( m )
local l;
Print("<");
if not IsMutable(m) then Print("immutable "); fi;
Print(Length(m![ROWSPOS]),"x",m![RLPOS],"-matrix over ",m![BDPOS],">");
l:=[Length(m![ROWSPOS]),m![RLPOS]];
if Product(l)<=9 and Product(l)<>0 then
Print("matrix mod ",Size(m![BDPOS]),": ",
List(m![ROWSPOS],x->x![ELSPOS]),">");
else
Print(l[1],"x",l[2],"-matrix mod ",Size(m![BDPOS]),">");
fi;
end );

InstallMethod( PrintObj, "for a zmodnz matrix", [ IsZmodnZMatrixRep ],
Expand Down Expand Up @@ -1046,12 +1079,32 @@ InstallMethod( \*, "for two zmodnz matrices",IsIdenticalObj,
return Objectify( ty, [a![BDPOS],a![EMPOS],b![RLPOS],l] );
end );

InstallMethod(\*,"for zmodnz matrix and ordinary matrix",IsIdenticalObj,
[IsZmodnZMatrixRep,IsMatrix],
function(a,b)
return Matrix(BaseDomain(a),List(RowsOfMatrix(a),x->x*b));
end);


InstallMethod( \=, "for two zmodnz matrices",IsIdenticalObj,
[ IsZmodnZMatrixRep, IsZmodnZMatrixRep ],
function( a, b )
return EQ_LIST_LIST_DEFAULT(a![ROWSPOS],b![ROWSPOS]);
end );

InstallMethod( \=, "for zmodnz matrix and matrix",IsIdenticalObj,
[ IsZmodnZMatrixRep, IsMatrix ],
function( a, b )
return Unpack(a)=b;
end );

InstallMethod( \=, "for matrix and zmodnz matrix",IsIdenticalObj,
[ IsMatrix, IsZmodnZMatrixRep ],
function( a, b )
return a=Unpack(b);
end );


InstallMethod( \<, "for two zmodnz matrices",IsIdenticalObj,
[ IsZmodnZMatrixRep, IsZmodnZMatrixRep ],
function( a, b )
Expand Down Expand Up @@ -1302,9 +1355,7 @@ InstallMethod( TransposedMatImmutable, "for a zmodnz matrix",
return n;
end );

InstallMethod( \*, "for a zmodnz vector and a zmodnz matrix",
IsElmsColls, [ IsZmodnZVectorRep, IsZmodnZMatrixRep ],
function( v, m )
ZMZVECMAT:=function( v, m )
local i,res,s,r;
r:=BaseDomain(v);
# do arithmetic over Z first so that we reduce only once
Expand All @@ -1325,11 +1376,17 @@ InstallMethod( \*, "for a zmodnz vector and a zmodnz matrix",
MakeImmutable(res);
fi;
return res;
end );
end;

InstallOtherMethod( \*, "for a plist vector and a zmodnz matrix",
IsElmsColls, [ IsList, IsZmodnZMatrixRep ],
function( v, m )
InstallMethod( \*, "for a zmodnz vector and a zmodnz matrix",
IsElmsColls, [ IsZmodnZVectorRep, IsZmodnZMatrixRep ],
ZMZVECMAT);

InstallOtherMethod( \^, "for a zmodnz vector and a zmodnz matrix",
IsElmsColls, [ IsZmodnZVectorRep, IsZmodnZMatrixRep ],
ZMZVECMAT);

PLISTVECZMZMAT:=function( v, m )
local i,res,s,r;
r:=BaseDomain(m);
# do arithmetic over Z first so that we reduce only once
Expand All @@ -1350,7 +1407,42 @@ InstallOtherMethod( \*, "for a plist vector and a zmodnz matrix",
MakeImmutable(res);
fi;
return res;
end );
end;

InstallOtherMethod( \*, "for a plist vector and a zmodnz matrix",
IsElmsColls, [ IsList, IsZmodnZMatrixRep ],
PLISTVECZMZMAT);

InstallOtherMethod( \^, "for a plist vector and a zmodnz matrix",
IsElmsColls, [ IsList, IsZmodnZMatrixRep ],
PLISTVECZMZMAT);

ZMZVECTIMESPLISTMAT:=function( v, m )
local i,res,s,r;
r:=BaseDomain(v);
# do arithmetic over Z first so that we reduce only once
res:=ListWithIdenticalEntries(Length(m[1]),Zero(r));
for i in [1..Length(v)] do
s := v[i];
if not IsZero(s) then
AddRowVector(res,m[i],s);
fi;
od;
res:=Vector(r,res);

if not IsMutable(v) and not IsMutable(m) then
MakeImmutable(res);
fi;
return res;
end;

InstallOtherMethod( \*, "for a zmodnz vector and plist matrix",
IsElmsColls, [ IsZmodnZVectorRep, IsMatrix ],
ZMZVECTIMESPLISTMAT);

InstallOtherMethod( \^, "for a zmodnz vector and plist matrix",
IsElmsColls, [ IsZmodnZVectorRep, IsMatrix ],
ZMZVECTIMESPLISTMAT);

InstallMethod( CompatibleVector, "for a zmodnz matrix",
[ IsZmodnZMatrixRep ],
Expand Down
4 changes: 2 additions & 2 deletions lib/matrix.gd
Original file line number Diff line number Diff line change
Expand Up @@ -625,7 +625,7 @@ DeclareOperation( "TriangulizedNullspaceMatDestructive", [ IsMatrix and IsMutabl
## </ManSection>
## <#/GAPDoc>
##
DeclareOperation( "GeneralisedEigenvalues", [ IsRing, IsMatrix ] );
DeclareOperation( "GeneralisedEigenvalues", [ IsRing, IsMatrixOrMatrixObj ] );
DeclareSynonym( "GeneralizedEigenvalues", GeneralisedEigenvalues );

#############################################################################
Expand All @@ -644,7 +644,7 @@ DeclareSynonym( "GeneralizedEigenvalues", GeneralisedEigenvalues );
## </ManSection>
## <#/GAPDoc>
##
DeclareOperation( "GeneralisedEigenspaces", [ IsRing, IsMatrix ] );
DeclareOperation( "GeneralisedEigenspaces", [ IsRing, IsMatrixOrMatrixObj ] );
DeclareSynonym( "GeneralizedEigenspaces", GeneralisedEigenspaces );


Expand Down
Loading