From 0b590c3d89543fcf4eb0f63b4e0ded7e15e75490 Mon Sep 17 00:00:00 2001 From: Max Horn Date: Thu, 8 Dec 2022 11:22:58 +0100 Subject: [PATCH] Add IsPositionalVectorRep, IsPositionalMatrixRep --- lib/matobj/positional.gd | 49 ++++++++++++++++++++++ lib/matobj/positional.gi | 88 ++++++++++++++++++++++++++++++++++++++++ lib/matobjplist.gd | 19 ++++----- lib/matobjplist.gi | 86 +-------------------------------------- lib/read3.g | 1 + lib/read5.g | 1 + 6 files changed, 147 insertions(+), 97 deletions(-) create mode 100644 lib/matobj/positional.gd create mode 100644 lib/matobj/positional.gi diff --git a/lib/matobj/positional.gd b/lib/matobj/positional.gd new file mode 100644 index 00000000000..51117f06260 --- /dev/null +++ b/lib/matobj/positional.gd @@ -0,0 +1,49 @@ +############################################################################# +## +## This file is part of GAP, a system for computational discrete algebra. +## +## SPDX-License-Identifier: GPL-2.0-or-later +## +## Copyright of GAP belongs to its developers, whose names are too numerous +## to list here. Please refer to the COPYRIGHT file for details. +## + +# TODO: document this +DeclareRepresentation( "IsPositionalVectorRep", + IsVectorObj and IsPositionalObjectRep + and IsNoImmediateMethodsObject + and HasBaseDomain and HasOneOfBaseDomain and HasZeroOfBaseDomain, + [] ); + +# TODO: document this +DeclareRepresentation( "IsPositionalMatrixRep", + IsMatrixObj and IsPositionalObjectRep + and IsNoImmediateMethodsObject + and HasNumberRows and HasNumberColumns + and HasBaseDomain and HasOneOfBaseDomain and HasZeroOfBaseDomain, + [] ); + + +# +# Some constants for matrix resp. vector access +# +# TODO: For now the order follows the order of the predecessors: +# BDPOS = 1, RLPOS = 3, ROWSPOS = 4; the goal is to +# eventually change this. But this needs us to carefully revisit +# all Objectify calls + +# Position of the base domain +BindConstant( "MAT_BD_POS", 1 ); +# Position of the number of rows +BindConstant( "MAT_NROWS_POS", 5 ); # FIXME: in many cases superfluous (can be computed from NCOLS and DATA) +# Position of the number of columns +BindConstant( "MAT_NCOLS_POS", 3 ); # +# Position of the data +BindConstant( "MAT_DATA_POS", 4 ); + +# Position of the base domain +BindConstant( "VEC_BD_POS", 1 ); +# Position of the data +BindConstant( "VEC_DATA_POS", 2 ); +# Position of the length +#BindConstant( "VEC_LENPOS", 3 ); # FIXME: not actually needed in general???? diff --git a/lib/matobj/positional.gi b/lib/matobj/positional.gi new file mode 100644 index 00000000000..3190d4ee73a --- /dev/null +++ b/lib/matobj/positional.gi @@ -0,0 +1,88 @@ +############################################################################# +## +## This file is part of GAP, a system for computational discrete algebra. +## +## SPDX-License-Identifier: GPL-2.0-or-later +## +## Copyright of GAP belongs to its developers, whose names are too numerous +## to list here. Please refer to the COPYRIGHT file for details. +## + +############################################################################ +# +# Operations for positional matrix objects +# +############################################################################ + +InstallMethod( BaseDomain, [ IsPositionalVectorRep ], + function( v ) + return v![VEC_BD_POS]; + end ); + +InstallMethod( Length, [ IsPositionalVectorRep ], + function( v ) + return Length(v![VEC_DATA_POS]); # FIXME: assumptions + end ); + + +InstallMethod( ShallowCopy, [ IsPositionalVectorRep ], + function( v ) + local i, res; + res := List([1..LEN_POSOBJ(v)], i -> v![i]); + res![VEC_DATA_POS] := ShallowCopy(v![VEC_DATA_POS]); + res := Objectify(TypeObj(v), res); + + # 'ShallowCopy' MUST return a mutable object if such an object exists at all! + if not IsMutable(v) then + SetFilterObj(res, IsMutable); + fi; + return res; + end ); + +# StructuralCopy works automatically + +InstallMethod( PostMakeImmutable, [ IsPositionalVectorRep ], + function( v ) + MakeImmutable( v![VEC_DATA_POS] ); + end ); + + +############################################################################ +# +# Operations for positional matrix objects +# +############################################################################ + +InstallMethod( BaseDomain, [ IsPositionalMatrixRep ], + function( m ) + return m![MAT_BD_POS]; + end ); + +InstallMethod( NumberRows, [ IsPositionalMatrixRep ], + function( m ) + return Length(m![MAT_DATA_POS]); # FIXME: this makes assumptions... + end ); + +InstallMethod( NumberColumns, [ IsPositionalMatrixRep ], + function( m ) + return m![MAT_NCOLS_POS]; + end ); + +InstallMethod( ShallowCopy, [ IsPositionalMatrixRep ], + function( m ) + local res; + res := List([1..LEN_POSOBJ(m)], i -> m![i]); + res![MAT_DATA_POS] := ShallowCopy(m![MAT_DATA_POS]); + res := Objectify(TypeObj(m), res); + + # 'ShallowCopy' MUST return a mutable object if such an object exists at all! + if not IsMutable(m) then + SetFilterObj(res, IsMutable); + fi; + return res; + end ); + +InstallMethod( PostMakeImmutable, [ IsPositionalMatrixRep ], + function( m ) + MakeImmutable( m![MAT_DATA_POS] ); + end ); diff --git a/lib/matobjplist.gd b/lib/matobjplist.gd index f4d54d78d23..fad2eccc456 100644 --- a/lib/matobjplist.gd +++ b/lib/matobjplist.gd @@ -45,9 +45,7 @@ ## <#/GAPDoc> ## DeclareRepresentation( "IsPlistVectorRep", - IsVectorObj and IsPositionalObjectRep - and IsNoImmediateMethodsObject - and HasBaseDomain and HasOneOfBaseDomain and HasZeroOfBaseDomain, + IsPositionalVectorRep, [] ); @@ -86,22 +84,19 @@ DeclareRepresentation( "IsPlistVectorRep", ## <#/GAPDoc> ## DeclareRepresentation( "IsPlistMatrixRep", - IsRowListMatrix and IsPositionalObjectRep - and IsNoImmediateMethodsObject - and HasNumberRows and HasNumberColumns - and HasBaseDomain and HasOneOfBaseDomain and HasZeroOfBaseDomain, + IsRowListMatrix and IsPositionalMatrixRep, [] ); # Some constants for matrix access: -BindGlobal( "BDPOS", 1 ); -BindGlobal( "EMPOS", 2 ); -BindGlobal( "RLPOS", 3 ); -BindGlobal( "ROWSPOS", 4 ); +BindGlobal( "BDPOS", 1 ); # base domain +BindGlobal( "EMPOS", 2 ); # empty vector as template for new vectors +BindGlobal( "RLPOS", 3 ); # row length = number of columns +BindGlobal( "ROWSPOS", 4 ); # list of row vectors # For vector access: #BindGlobal( "BDPOS", 1 ); # see above -BindGlobal( "ELSPOS", 2 ); +BindGlobal( "ELSPOS", 2 ); # list of elements # Two filters to speed up some methods: DeclareFilter( "IsIntVector" ); diff --git a/lib/matobjplist.gi b/lib/matobjplist.gi index d5f8b4a66b3..d16a4b9b6a1 100644 --- a/lib/matobjplist.gi +++ b/lib/matobjplist.gi @@ -192,21 +192,6 @@ InstallMethod( CompatibleVectorFilter, ["IsPlistMatrixRep"], ############################################################################ -############################################################################ -# The basic attributes: -############################################################################ - -InstallMethod( BaseDomain, "for a plist vector", [ IsPlistVectorRep ], - function( v ) - return v![BDPOS]; - end ); - -InstallMethod( Length, "for a plist vector", [ IsPlistVectorRep ], - function( v ) - return Length(v![ELSPOS]); - end ); - - ############################################################################ # Representation preserving constructors: ############################################################################ @@ -225,6 +210,7 @@ InstallMethod( ZeroVector, "for an integer and a plist matrix", [ IsInt, IsPlistMatrixRep ], function( l, m ) local v; + # TODO: minimize number of places invoking `Objectify` v := Objectify(TypeObj(m![EMPOS]), [m![BDPOS],ListWithIdenticalEntries(l,Zero(m![BDPOS]))]); if not IsMutable(v) then SetFilterObj(v,IsMutable); fi; @@ -323,26 +309,6 @@ InstallMethod( Unpack, "for a plist vector", end ); -############################################################################ -# Standard operations for all objects: -############################################################################ - -InstallMethod( ShallowCopy, "for a plist vector", [ IsPlistVectorRep ], - function( v ) - local res; - res := Objectify(TypeObj(v),[v![BDPOS],ShallowCopy(v![ELSPOS])]); - if not IsMutable(v) then SetFilterObj(res,IsMutable); fi; - return res; - end ); - -# StructuralCopy works automatically - -InstallMethod( PostMakeImmutable, "for a plist vector", [ IsPlistVectorRep ], - function( v ) - MakeImmutable( v![ELSPOS] ); - end ); - - ############################################################################ # Arithmetical operations: ############################################################################ @@ -562,36 +528,6 @@ InstallMethod( CopySubVector, "for two plist vectors and two lists", ############################################################################ ############################################################################ - -############################################################################ -# The basic attributes: -############################################################################ - -InstallMethod( BaseDomain, "for a plist matrix", - [ IsPlistMatrixRep ], - function( m ) - return m![BDPOS]; - end ); - -InstallMethod( NumberRows, "for a plist matrix", - [ IsPlistMatrixRep ], - function( m ) - return Length(m![ROWSPOS]); - end ); - -InstallMethod( NumberColumns, "for a plist matrix", - [ IsPlistMatrixRep ], - function( m ) - return m![RLPOS]; - end ); - -InstallMethod( DimensionsMat, "for a plist matrix", - [ IsPlistMatrixRep ], - function( m ) - return [Length(m![ROWSPOS]),m![RLPOS]]; - end ); - - ############################################################################ # Representation preserving constructors: ############################################################################ @@ -738,26 +674,6 @@ InstallMethod( Append, "for two plist matrices", Append(m![ROWSPOS],n![ROWSPOS]); end ); -InstallMethod( ShallowCopy, "for a plist matrix", - [ IsPlistMatrixRep ], - function( m ) - local res; - res := Objectify(TypeObj(m),[m![BDPOS],m![EMPOS],m![RLPOS], - ShallowCopy(m![ROWSPOS])]); - if not IsMutable(m) then - SetFilterObj(res,IsMutable); - fi; -#T 'ShallowCopy' MUST return a mutable object -#T if such an object exists at all! - return res; - end ); - -InstallMethod( PostMakeImmutable, "for a plist matrix", - [ IsPlistMatrixRep ], - function( m ) - MakeImmutable( m![ROWSPOS] ); - end ); - InstallMethod( ListOp, "for a plist matrix", [ IsPlistMatrixRep ], function( m ) diff --git a/lib/read3.g b/lib/read3.g index 76b4e05c0b1..b0f91d87481 100644 --- a/lib/read3.g +++ b/lib/read3.g @@ -105,6 +105,7 @@ ReadLib( "word.gd" ); ReadLib( "wordass.gd" ); ReadLib( "matobj2.gd" ); +ReadLib( "matobj/positional.gd" ); ReadLib( "matobjplist.gd" ); ReadLib( "matobjnz.gd" ); diff --git a/lib/read5.g b/lib/read5.g index 07963b6d659..072bdb1e663 100644 --- a/lib/read5.g +++ b/lib/read5.g @@ -112,6 +112,7 @@ ReadLib( "matrobjrowlist.gi" ); ReadLib( "vecmat.gi" ); ReadLib( "vec8bit.gi" ); ReadLib( "mat8bit.gi" ); +ReadLib( "matobj/positional.gi" ); ReadLib( "matobjplist.gi" ); ReadLib( "matobjnz.gi" ); ReadLib( "meataxe.gi" );