Skip to content

Commit 83a5f76

Browse files
committed
add a minimal matrix rep
1 parent 114824d commit 83a5f76

File tree

4 files changed

+186
-0
lines changed

4 files changed

+186
-0
lines changed

lib/matobjminimal.gd

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
#############################################################################
2+
##
3+
## This file is part of GAP, a system for computational discrete algebra.
4+
##
5+
## SPDX-License-Identifier: GPL-2.0-or-later
6+
##
7+
## Copyright of GAP belongs to its developers, whose names are too numerous
8+
## to list here. Please refer to the COPYRIGHT file for details.
9+
##
10+
11+
############################################################################
12+
#
13+
# This is a minimal implementation for nxm matrices
14+
# stored as a flat n*m element list. In this
15+
# file the representation, types and global
16+
# functions are declared.
17+
# This implementation should not be used for actual computations. It serves
18+
# as an example for a minimal MatrixObj implementation and can be used to test
19+
# various things.
20+
21+
22+
#############################################################################
23+
##
24+
## <#GAPDoc Label="IsMinimalExampleMatrixRep">
25+
## <ManSection>
26+
## <Filt Name="IsMinimalExampleMatrixRep" Arg='obj' Type="representation"/>
27+
##
28+
## <Description>
29+
## An object <A>obj</A> in <Ref Filt="IsMinimalExampleMatrixRep"/> describes
30+
## a matrix object that stores the matrix entries as a flat list. It is
31+
## internally represented as a positional object
32+
## (see <Ref Filt="IsPositionalObjectRep"/> that stores 4 entries:
33+
## <Enum>
34+
## <Item>
35+
## its base domain
36+
## (see <Ref Attr="BaseDomain" Label="for a matrix object"/>),
37+
## </Item>
38+
## <Item>
39+
## the number of rows (see <Ref Attr="NumberRows" Label="for a matrix object"/>),
40+
## </Item>
41+
## <Item>
42+
## the number of columns
43+
## (see <Ref Attr="NumberColumns" Label="for a matrix object"/>), and
44+
## </Item>
45+
## <Item>
46+
## a plain list (see <Ref Filt="IsPlistRep"/> of its entries.
47+
## </Item>
48+
## </Enum>
49+
## It implements the MatrixObj specification in a minimal way, that is it
50+
## implements exactly the required methods. This is intended for testing and
51+
## development and should not be used for actual calculations!
52+
## </Description>
53+
## </ManSection>
54+
## <#/GAPDoc>
55+
##
56+
# Here we declare the new representation and tell GAP which properties it
57+
# implies. This minimal example is implemented as a positional object
58+
# like IsFlistMatrixRep and IsPlistMatrixRep.
59+
DeclareRepresentation( "IsMinimalExampleMatrixRep",
60+
IsMatrixObj and IsMatrixOrMatrixObj and IsPositionalObjectRep
61+
and IsNoImmediateMethodsObject
62+
and HasNumberRows and HasNumberColumns
63+
and HasBaseDomain and HasOneOfBaseDomain and HasZeroOfBaseDomain,
64+
[] );
65+
66+
# If we implement our object as a positional object we often have to access its
67+
# properties in the code. To make that more readable we declare global
68+
# variables. If you do this too make sure you use variables that are unique and
69+
# unlikely to be used someplace else, even though that might mean using longer
70+
# names. Here we prefixed the names with the name of the representation. See
71+
# also Reference Manual Chapter 79 for more information about Objects.
72+
73+
# Some constants for matrix access:
74+
# Position in the positional object of the base domain
75+
BindGlobal( "MINREP_BDPOS", 1 );
76+
# Position in the positional object of the number of rows
77+
BindGlobal( "MINREP_NRPOS", 2 );
78+
# Position in the positional object of the number of columns
79+
BindGlobal( "MINREP_NCPOS", 3 );
80+
# Position in the positional object of the list of entries
81+
BindGlobal( "MINREP_ELSPOS", 4 );

lib/matobjminimal.gi

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
#############################################################################
2+
##
3+
## This file is part of GAP, a system for computational discrete algebra.
4+
##
5+
## SPDX-License-Identifier: GPL-2.0-or-later
6+
##
7+
## Copyright of GAP belongs to its developers, whose names are too numerous
8+
## to list here. Please refer to the COPYRIGHT file for details.
9+
##
10+
11+
############################################################################
12+
#
13+
# This file is a minimal implementation for new style vectors and matrices.
14+
# It stores matrices as a dense flat list.
15+
# This implementation implements only the methods required by the specification
16+
# for matrix objects. This is intended for testing.
17+
18+
InstallMethod( NewMatrix,
19+
"for IsMinimalExampleMatrixRep, a ring, an int, and a list",
20+
[ IsMinimalExampleMatrixRep, IsRing, IsInt, IsList ],
21+
function( filter, basedomain, nrcols, list_in )
22+
local obj, filter2, list, rowindex, colindex;
23+
24+
# If applicable then replace a nested list 'list_in' by a flat list 'list'.
25+
if Length(list_in) > 0 and (IsVectorObj(list_in[1]) or IsList(list_in[1])) then
26+
list := [];
27+
for rowindex in [1..Length(list_in)] do
28+
if Length(list_in[rowindex]) <> nrcols then
29+
Error( "NewMatrix: Each row must have nrcols entries." );
30+
fi;
31+
for colindex in [1..nrcols] do
32+
Assert(2, list_in[rowindex][colindex] in basedomain);
33+
list[(rowindex-1)*nrcols + colindex] := list_in[rowindex][colindex];
34+
od;
35+
od;
36+
else
37+
if Length(list_in) mod nrcols <> 0 then
38+
Error( "NewMatrix: Length of list must be a multiple of ncols." );
39+
fi;
40+
list := list_in;
41+
fi;
42+
43+
obj := [basedomain,Length(list)/nrcols,nrcols,list];
44+
filter2 := IsMinimalExampleMatrixRep and IsMutable;
45+
if HasCanEasilyCompareElements(Representative(basedomain)) and
46+
CanEasilyCompareElements(Representative(basedomain)) then
47+
filter2 := filter2 and CanEasilyCompareElements;
48+
fi;
49+
Objectify( NewType(CollectionsFamily(FamilyObj(basedomain)),
50+
filter2), obj);
51+
return obj;
52+
end );
53+
54+
InstallMethod( BaseDomain, "for a minimal example matrix rep",
55+
[ IsMinimalExampleMatrixRep ],
56+
function( m )
57+
return m![MINREP_BDPOS];
58+
end );
59+
60+
InstallMethod( NumberRows, "for a minimal example matrix rep",
61+
[ IsMinimalExampleMatrixRep ],
62+
function( m )
63+
return m![MINREP_NRPOS];
64+
end );
65+
66+
InstallMethod( NumberColumns, "for a minimal example matrix rep",
67+
[ IsMinimalExampleMatrixRep ],
68+
function( m )
69+
return m![MINREP_NCPOS];
70+
end );
71+
72+
InstallMethod( MatElm, "for an minimal example matrix rep and two positions",
73+
[ IsMinimalExampleMatrixRep, IsPosInt, IsPosInt ],
74+
function( mat, row, col )
75+
Assert(2, 1 <= row and row <= mat![MINREP_NRPOS]);
76+
Assert(2, 1 <= col and col <= mat![MINREP_NCPOS]);
77+
return mat![MINREP_ELSPOS][(row-1)*mat![MINREP_NCPOS]+col];
78+
end );
79+
80+
InstallMethod( SetMatElm, "for an minimal example matrix rep, two positions, and an object",
81+
[ IsMinimalExampleMatrixRep and IsMutable, IsPosInt, IsPosInt, IsObject ],
82+
function( mat, row, col, obj )
83+
Assert(2, 1 <= row and row <= mat![MINREP_NRPOS]);
84+
Assert(2, 1 <= col and col <= mat![MINREP_NCPOS]);
85+
Assert(2, obj in mat![MINREP_BDPOS]);
86+
mat![MINREP_ELSPOS][(row-1)*mat![MINREP_NCPOS]+col] := obj;
87+
end );
88+
89+
InstallMethod( \<, "for two minimal example matrices",
90+
[ IsMinimalExampleMatrixRep, IsMinimalExampleMatrixRep ],
91+
function( a, b )
92+
return LT_LIST_LIST_DEFAULT(a![MINREP_ELSPOS],b![MINREP_ELSPOS]);
93+
end );
94+
95+
InstallMethod( ConstructingFilter, "for a minimal example matrix rep",
96+
[ IsMinimalExampleMatrixRep ],
97+
function( mat )
98+
return IsMinimalExampleMatrixRep;
99+
end );
100+
101+
InstallMethod( CompatibleVectorFilter, [ IsMinimalExampleMatrixRep ],
102+
M -> IsPlistVectorRep );
103+

lib/read3.g

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ ReadLib( "wordass.gd" );
107107
ReadLib( "matobj2.gd" );
108108
ReadLib( "matobjplist.gd" );
109109
ReadLib( "matobjnz.gd" );
110+
ReadLib( "matobjminimal.gd" );
110111

111112
# files dealing with rewriting systems
112113
ReadLib( "rws.gd" );

lib/read5.g

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ ReadLib( "vec8bit.gi" );
114114
ReadLib( "mat8bit.gi" );
115115
ReadLib( "matobjplist.gi" );
116116
ReadLib( "matobjnz.gi" );
117+
ReadLib( "matobjminimal.gi" );
117118
ReadLib( "meataxe.gi" );
118119
ReadLib( "meatauto.gi" );
119120

0 commit comments

Comments
 (0)