Skip to content

Commit 837a7af

Browse files
committed
add a minimal matrix rep
1 parent 114824d commit 837a7af

File tree

2 files changed

+176
-0
lines changed

2 files changed

+176
-0
lines changed

lib/matobjminimal.gd

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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. FListMatrices e.g. are positional objects and so on.
58+
DeclareRepresentation( "IsMinimalExampleMatrixRep",
59+
IsMatrixObj and IsMatrixOrMatrixObj and IsPositionalObjectRep
60+
and IsNoImmediateMethodsObject
61+
and HasNumberRows and HasNumberColumns
62+
and HasBaseDomain and HasOneOfBaseDomain and HasZeroOfBaseDomain,
63+
[] );
64+
65+
# If we implement our object as a positional object we often have to access its
66+
# properties in the code. To make that more readable we declare global
67+
# variables. If you do this too make sure you use variables that are unique and
68+
# unlikely to be used someplace else, even though that might mean using longer
69+
# names. Here we prefixed the names with the name of the representation. See
70+
# also Reference Manual Chapter 79 for more information about Objects.
71+
72+
# Some constants for matrix access:
73+
# Position in the positional object of the base domain
74+
BindGlobal( "MINREP_BDPOS", 1 );
75+
# Position in the positional object of the number of rows
76+
BindGlobal( "MINREP_NRPOS", 2 );
77+
# Position in the positional object of the number of columns
78+
BindGlobal( "MINREP_NCPOS", 3 );
79+
# Position in the positional object of the list of entries
80+
BindGlobal( "MINREP_ELSPOS", 4 );

lib/matobjminimal.gi

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
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+
list[(rowindex-1)*nrcols + colindex] := list_in[rowindex][colindex];
33+
od;
34+
od;
35+
else
36+
if Length(list_in) mod nrcols <> 0 then
37+
Error( "NewMatrix: Length of list must be a multiple of ncols." );
38+
fi;
39+
list := list_in;
40+
fi;
41+
42+
obj := [basedomain,Length(list)/nrcols,nrcols,list];
43+
filter2 := IsMinimalExampleMatrixRep and IsMutable;
44+
if HasCanEasilyCompareElements(Representative(basedomain)) and
45+
CanEasilyCompareElements(Representative(basedomain)) then
46+
filter2 := filter2 and CanEasilyCompareElements;
47+
fi;
48+
Objectify( NewType(CollectionsFamily(FamilyObj(basedomain)),
49+
filter2), obj);
50+
return obj;
51+
end );
52+
53+
InstallMethod( BaseDomain, "for a minimal example matrix rep",
54+
[ IsMinimalExampleMatrixRep ],
55+
function( m )
56+
return m![MINREP_BDPOS];
57+
end );
58+
59+
InstallMethod( NumberRows, "for a minimal example matrix rep",
60+
[ IsMinimalExampleMatrixRep ],
61+
function( m )
62+
return m![MINREP_NRPOS];
63+
end );
64+
65+
InstallMethod( NumberColumns, "for a minimal example matrix rep",
66+
[ IsMinimalExampleMatrixRep ],
67+
function( m )
68+
return m![MINREP_NCPOS];
69+
end );
70+
71+
InstallMethod( MatElm, "for an minimal example matrix rep and two positions",
72+
[ IsMinimalExampleMatrixRep, IsPosInt, IsPosInt ],
73+
function( mat, row, col )
74+
return mat![MINREP_ELSPOS][(row-1)*mat![MINREP_NCPOS]+col];
75+
end );
76+
77+
InstallMethod( SetMatElm, "for an minimal example matrix rep, two positions, and an object",
78+
[ IsMinimalExampleMatrixRep and IsMutable, IsPosInt, IsPosInt, IsObject ],
79+
function( mat, row, col, obj )
80+
mat![MINREP_ELSPOS][(row-1)*mat![MINREP_NCPOS]+col] := obj;
81+
end );
82+
83+
InstallMethod( \<, "for two minimal example matrices",
84+
[ IsMinimalExampleMatrixRep, IsMinimalExampleMatrixRep ],
85+
function( a, b )
86+
return LT_LIST_LIST_DEFAULT(a![MINREP_ELSPOS],b![MINREP_ELSPOS]);
87+
end );
88+
89+
InstallMethod( ConstructingFilter, "for a minimal example matrix rep",
90+
[ IsMinimalExampleMatrixRep ],
91+
function( mat )
92+
return IsMinimalExampleMatrixRep;
93+
end );
94+
95+
InstallMethod( CompatibleVectorFilter, [ IsMinimalExampleMatrixRep ],
96+
M -> IsPlistVectorRep );

0 commit comments

Comments
 (0)