From 32f926916239deab857a16256f52d1ffad197cac Mon Sep 17 00:00:00 2001 From: Max Horn Date: Thu, 19 Jan 2023 09:09:39 +0100 Subject: [PATCH] WIP: Add IsPositionalVectorRep, IsPositionalMatrixRep FIXME: actually the "generic" ShallowCopy method is wrong, as it e.g. doesn't reset IsZero etc -- if we want to keep this, we need something like a helper to produce a "basic" typeobj for the given basedomain. --- lib/matobj/positional.gd | 49 ++++++++++++++++++++ lib/matobj/positional.gi | 97 ++++++++++++++++++++++++++++++++++++++++ lib/matobjplist.gd | 21 +++------ lib/matobjplist.gi | 82 --------------------------------- lib/read3.g | 1 + lib/read5.g | 1 + 6 files changed, 155 insertions(+), 96 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 0000000000..0afd1b3d16 --- /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 0000000000..33e6d7c10f --- /dev/null +++ b/lib/matobj/positional.gi @@ -0,0 +1,97 @@ +############################################################################# +## +## 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); +# FIXME: actually the "generic" ShallowCopy method is wrong, as it +# e.g. doesn't reset IsZero etc -- if we want to keep this, we need +# something like a helper to produce a "basic" typeobj for the +# given basedomain. + + + # '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); +# FIXME: actually the "generic" ShallowCopy method is wrong, as it +# e.g. doesn't reset IsZero etc -- if we want to keep this, we need +# something like a helper to produce a "basic" typeobj for the +# given basedomain. + + # '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 0025cc8b80..6faba7be29 100644 --- a/lib/matobjplist.gd +++ b/lib/matobjplist.gd @@ -49,10 +49,7 @@ ## ## DeclareRepresentation( "IsPlistVectorRep", - IsVectorObj and IsPositionalObjectRep - and IsCopyable - and IsNoImmediateMethodsObject - and HasBaseDomain and HasOneOfBaseDomain and HasZeroOfBaseDomain, + IsPositionalVectorRep and IsCopyable, [] ); @@ -94,23 +91,19 @@ DeclareRepresentation( "IsPlistVectorRep", ## ## DeclareRepresentation( "IsPlistMatrixRep", - IsRowListMatrix and IsPositionalObjectRep - and IsCopyable - and IsNoImmediateMethodsObject - and HasNumberRows and HasNumberColumns - and HasBaseDomain and HasOneOfBaseDomain and HasZeroOfBaseDomain, + IsRowListMatrix and IsPositionalMatrixRep and IsCopyable, [] ); # 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 aaf444add0..c775061ee1 100644 --- a/lib/matobjplist.gi +++ b/lib/matobjplist.gi @@ -221,21 +221,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: ############################################################################ @@ -340,23 +325,6 @@ InstallMethod( Unpack, "for a plist vector", end ); -############################################################################ -# Standard operations for all objects: -############################################################################ - -InstallMethod( ShallowCopy, "for a plist vector", [ IsPlistVectorRep ], - function( v ) - return MakeIsPlistVectorRep(v![BDPOS], ShallowCopy(v![ELSPOS])); - end ); - -# StructuralCopy works automatically - -InstallMethod( PostMakeImmutable, "for a plist vector", [ IsPlistVectorRep ], - function( v ) - MakeImmutable( v![ELSPOS] ); - end ); - - ############################################################################ # Arithmetical operations: ############################################################################ @@ -545,36 +513,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: ############################################################################ @@ -721,26 +659,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 76b4e05c0b..b0f91d8748 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 07963b6d65..072bdb1e66 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" );