Skip to content

Commit c1c19dd

Browse files
OldAnchovyToppingfingolfincodex
authored
Add AddMatrix, MultMatrix for in-place modification of matrices (#5980)
AI assistance: Codex was used to implement some of the code changes, expand the MatrixObj test coverage, and update the documentation. Co-authored-by: Max Horn <max@quendi.de> Co-authored-by: Codex <codex@openai.com>
1 parent cda63d1 commit c1c19dd

File tree

7 files changed

+361
-14
lines changed

7 files changed

+361
-14
lines changed

doc/ref/matobj.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,8 @@ All rows of a matrix must have the same length and the same base domain.
221221
<#Include Label="AddMatrixColumnsLeft">
222222
<#Include Label="SwapMatrixRows">
223223
<#Include Label="SwapMatrixColumns">
224+
<#Include Label="AddMatrix">
225+
<#Include Label="MultMatrix">
224226

225227
</Section>
226228

@@ -376,4 +378,3 @@ described in this chapter is supported.
376378
</Section>
377379

378380
</Chapter>
379-

hpcgap/lib/vec8bit.gi

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -463,7 +463,7 @@ end );
463463
#M <vec1> = <vec2>
464464
##
465465

466-
InstallMethod( \=, "for 2 8 bit vectors",
466+
InstallMethod( \=, "for two 8 bit vectors",
467467
IsIdenticalObj, [IsRowVector and Is8BitVectorRep,
468468
IsRowVector and Is8BitVectorRep],
469469
0,
@@ -476,7 +476,7 @@ InstallMethod( \=, "for 2 8 bit vectors",
476476
## Usual lexicographic ordering
477477
##
478478

479-
InstallMethod( \<, "for 2 8 bit vectors",
479+
InstallMethod( \<, "for two 8 bit vectors",
480480
IsIdenticalObj, [IsRowVector and Is8BitVectorRep,
481481
IsRowVector and Is8BitVectorRep],
482482
0,
@@ -488,7 +488,7 @@ InstallMethod( \<, "for 2 8 bit vectors",
488488
##
489489
## scalar product
490490
#'
491-
InstallMethod( \*, "for 2 8 bit vectors",
491+
InstallMethod( \*, "for two 8 bit vectors",
492492
IsIdenticalObj, [IsRingElementList and Is8BitVectorRep,
493493
IsRingElementList and Is8BitVectorRep],
494494
0,
@@ -525,7 +525,7 @@ end);
525525
## add <mult>*<vec2> to <vec1> in place
526526
##
527527

528-
InstallOtherMethod( AddRowVector, "for 2 8 bit vectors and a field element and from and to",
528+
InstallOtherMethod( AddRowVector, "for two 8 bit vectors and a field element and from and to",
529529
IsCollsCollsElmsXX, [ IsRowVector and Is8BitVectorRep,
530530
IsRowVector and Is8BitVectorRep,
531531
IsFFE and IsInternalRep, IsPosInt, IsPosInt ], 0,
@@ -538,7 +538,7 @@ InstallOtherMethod( AddRowVector, "for 2 8 bit vectors and a field element and f
538538
## add <mult>*<vec2> to <vec1> in place
539539
##
540540

541-
InstallOtherMethod( AddRowVector, "for 2 8 bit vectors and a field element",
541+
InstallOtherMethod( AddRowVector, "for two 8 bit vectors and a field element",
542542
IsCollsCollsElms, [ IsRowVector and Is8BitVectorRep,
543543
IsRowVector and Is8BitVectorRep,
544544
IsFFE and IsInternalRep ], 0,
@@ -551,7 +551,7 @@ InstallOtherMethod( AddRowVector, "for 2 8 bit vectors and a field element",
551551
## add <vec2> to <vec1> in place
552552
##
553553

554-
InstallOtherMethod( AddRowVector, "for 2 8 bit vectors",
554+
InstallOtherMethod( AddRowVector, "for two 8 bit vectors",
555555
IsIdenticalObj, [ IsRowVector and Is8BitVectorRep,
556556
IsRowVector and Is8BitVectorRep], 0,
557557
ADD_ROWVECTOR_VEC8BITS_2);

lib/matobj.gi

Lines changed: 148 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1897,11 +1897,158 @@ InstallMethod( SwapMatrixColumns, "for a mutable matrix object, and two column n
18971897

18981898
end );
18991899

1900+
#############################################################################
1901+
##
1902+
#M AddMatrix( <mat1>, <mat2> )
1903+
##
1904+
1905+
InstallMethod( AddMatrix, "for a mutable matrix object and a matrix object",
1906+
[ IsMatrixOrMatrixObj and IsMutable, IsMatrixOrMatrixObj ],
1907+
function( dstmat, srcmat )
1908+
local i, j;
1909+
if DimensionsMat(dstmat) <> DimensionsMat(srcmat) then
1910+
Error("AddMatrix: matrices must have the same dimensions");
1911+
fi;
1912+
for i in [1..NrRows(dstmat)] do
1913+
for j in [1..NrCols(dstmat)] do
1914+
dstmat[i,j] := dstmat[i,j] + srcmat[i,j];
1915+
od;
1916+
od;
1917+
end );
1918+
1919+
InstallEarlyMethod( AddMatrix,
1920+
function( dstmat, srcmat )
1921+
local i;
1922+
if IsPlistRep(dstmat) and IsPlistRep(srcmat) then
1923+
if DimensionsMat(dstmat) <> DimensionsMat(srcmat) then
1924+
Error("AddMatrix: matrices must have the same dimensions");
1925+
fi;
1926+
for i in [1..NrRows(dstmat)] do
1927+
AddRowVector(dstmat[i], srcmat[i]);
1928+
od;
1929+
else
1930+
TryNextMethod();
1931+
fi;
1932+
end );
1933+
1934+
InstallMethod( AddMatrix, "for a mutable 8bit matrix and an 8bit matrix",
1935+
[ Is8BitMatrixRep and IsMutable, Is8BitMatrixRep ],
1936+
function( dstmat, srcmat )
1937+
local i;
1938+
if DimensionsMat(dstmat) <> DimensionsMat(srcmat) then
1939+
Error("AddMatrix: matrices must have the same dimensions");
1940+
fi;
1941+
for i in [1..NrRows(dstmat)] do
1942+
ADD_COEFFS_VEC8BIT_2(dstmat[i], srcmat[i]);
1943+
od;
1944+
end );
1945+
1946+
#############################################################################
1947+
##
1948+
#M AddMatrix( <mat1>, <mat2>, <mult> )
1949+
##
1950+
1951+
InstallMethod( AddMatrix, "for a mutable matrix object, a matrix object, and a scalar",
1952+
[ IsMatrixOrMatrixObj and IsMutable, IsMatrixOrMatrixObj, IsScalar ],
1953+
function( dstmat, srcmat, scalar )
1954+
local i, j;
1955+
if DimensionsMat(dstmat) <> DimensionsMat(srcmat) then
1956+
Error("AddMatrix: matrices must have the same dimensions");
1957+
fi;
1958+
for i in [1..NrRows(dstmat)] do
1959+
for j in [1..NrCols(dstmat)] do
1960+
dstmat[i,j] := dstmat[i,j] + srcmat[i,j] * scalar;
1961+
od;
1962+
od;
1963+
end );
1964+
1965+
InstallEarlyMethod( AddMatrix,
1966+
function( dstmat, srcmat, scalar )
1967+
local i;
1968+
if IsPlistRep(dstmat) and IsPlistRep(srcmat) then
1969+
if DimensionsMat(dstmat) <> DimensionsMat(srcmat) then
1970+
Error("AddMatrix: matrices must have the same dimensions");
1971+
fi;
1972+
for i in [1..NrRows(dstmat)] do
1973+
AddRowVector(dstmat[i], srcmat[i], scalar);
1974+
od;
1975+
else
1976+
TryNextMethod();
1977+
fi;
1978+
end );
1979+
1980+
InstallMethod( AddMatrix, "for a mutable 8bit matrix, an 8bit matrix, and a scalar",
1981+
[ Is8BitMatrixRep and IsMutable, Is8BitMatrixRep, IsFFE ],
1982+
function( dstmat, srcmat, scalar )
1983+
local i;
1984+
if DimensionsMat(dstmat) <> DimensionsMat(srcmat) then
1985+
Error("AddMatrix: matrices must have the same dimensions");
1986+
fi;
1987+
for i in [1..NrRows(dstmat)] do
1988+
ADD_COEFFS_VEC8BIT_3(dstmat[i], srcmat[i], scalar);
1989+
od;
1990+
end );
1991+
1992+
#############################################################################
1993+
##
1994+
#M MultMatrixRight( <mat>, <mult> )
1995+
##
1996+
1997+
InstallMethod( MultMatrixRight, "for a mutable matrix object and a scalar",
1998+
[ IsMatrixOrMatrixObj and IsMutable, IsScalar ],
1999+
function( mat, scalar )
2000+
local i, j;
2001+
for i in [1..NrRows(mat)] do
2002+
for j in [1..NrCols(mat)] do
2003+
mat[i,j] := mat[i,j] * scalar;
2004+
od;
2005+
od;
2006+
end );
2007+
2008+
InstallEarlyMethod( MultMatrixRight,
2009+
function( mat, scalar )
2010+
local i;
2011+
if IsPlistRep(mat) and IsScalar(scalar) then
2012+
for i in [1..NrRows(mat)] do
2013+
MultVectorRight(mat[i], scalar);
2014+
od;
2015+
else
2016+
TryNextMethod();
2017+
fi;
2018+
end );
2019+
2020+
#############################################################################
2021+
##
2022+
#M MultMatrixLeft( <mat>, <mult> )
2023+
##
2024+
2025+
InstallMethod( MultMatrixLeft, "for a mutable matrix object and a scalar",
2026+
[ IsMatrixOrMatrixObj and IsMutable, IsScalar ],
2027+
function( mat, scalar )
2028+
local i, j;
2029+
for i in [1..NrRows(mat)] do
2030+
for j in [1..NrCols(mat)] do
2031+
mat[i,j] := scalar * mat[i,j];
2032+
od;
2033+
od;
2034+
end );
2035+
2036+
InstallEarlyMethod( MultMatrixLeft,
2037+
function( mat, scalar )
2038+
local i;
2039+
if IsPlistRep(mat) and IsScalar(scalar) then
2040+
for i in [1..NrRows(mat)] do
2041+
MultVectorLeft(mat[i], scalar);
2042+
od;
2043+
else
2044+
TryNextMethod();
2045+
fi;
2046+
end );
19002047

19012048
############################################################################
19022049
## Fallback method for DeterminantMatrix
2050+
19032051
InstallMethod(DeterminantMatrix, ["IsMatrixObj"],
19042052
function( mat )
19052053
return DeterminantMat( Unpack( mat ) );
19062054
end);
1907-

lib/matobj2.gd

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2052,3 +2052,85 @@ DeclareOperationKernel( "SwapMatrixRows", [ IsMatrixOrMatrixObj and IsMutable, I
20522052
## <#/GAPDoc>
20532053
##
20542054
DeclareOperationKernel( "SwapMatrixColumns", [ IsMatrixOrMatrixObj and IsMutable, IsInt, IsInt ], SWAP_MAT_COLS );
2055+
2056+
############################################################################
2057+
##
2058+
## <#GAPDoc Label="AddMatrix">
2059+
## <ManSection>
2060+
## <Oper Name="AddMatrix" Arg='M, N[, c]'/>
2061+
##
2062+
## <Returns>nothing</Returns>
2063+
##
2064+
## <Description>
2065+
## Computes the calculation <M>M + c \cdot N</M> in-place, storing the result in <A>M</A>.
2066+
## If the optional argument <A>c</A> is omitted, then <A>N</A> is added directly.
2067+
## If both of the matrices are lists-of-lists, then the operation is delegated
2068+
## row by row to <Ref Oper="AddRowVector"/>.
2069+
## <Example><![CDATA[
2070+
## gap> mat1 := [ [ 1, 2 ], [ 3, 4 ] ];
2071+
## [ [ 1, 2 ], [ 3, 4 ] ]
2072+
## gap> mat2 := [ [ 1, 0 ], [ 3, -1 ] ];
2073+
## [ [ 1, 0 ], [ 3, -1 ] ]
2074+
## gap> AddMatrix( mat1, mat2, 2 );
2075+
## gap> mat1;
2076+
## [ [ 3, 2 ], [ 9, 2 ] ]
2077+
## gap> mat2;
2078+
## [ [ 1, 0 ], [ 3, -1 ] ]
2079+
## gap> AddMatrix( mat1, [ [ 1, 0], [ 3, -1] ] );
2080+
## gap> mat1;
2081+
## [ [ 4, 2 ], [ 12, 1 ] ]
2082+
## ]]></Example>
2083+
## </Description>
2084+
## </ManSection>
2085+
## <#/GAPDoc>
2086+
##
2087+
DeclareOperation( "AddMatrix", [ IsMatrixOrMatrixObj and IsMutable, IsMatrixOrMatrixObj ] );
2088+
DeclareOperation( "AddMatrix", [ IsMatrixOrMatrixObj and IsMutable, IsMatrixOrMatrixObj, IsScalar ] );
2089+
2090+
############################################################################
2091+
##
2092+
## <#GAPDoc Label="MultMatrix">
2093+
## <ManSection>
2094+
## <Oper Name="MultMatrix" Arg='mat, c'/>
2095+
## <Oper Name="MultMatrixLeft" Arg='mat, c'/>
2096+
## <Oper Name="MultMatrixRight" Arg='mat, c'/>
2097+
##
2098+
## <Returns>nothing</Returns>
2099+
##
2100+
## <Description>
2101+
## These functions multiply the entries of <A>mat</A> by <A>c</A> in-place.
2102+
## <Ref Oper="MultMatrixRight"/> performs the operation <A>mat</A><C>*</C><A>c</A>,
2103+
## whereas <Ref Oper="MultMatrixLeft"/> performs the operation <A>c</A><C>*</C><A>mat</A>
2104+
## and <Ref Oper="MultMatrix"/> is an alias for <Ref Oper="MultMatrixLeft"/>.
2105+
## In all of these, if the matrix <A>mat</A> is a lists-of-lists, then the
2106+
## operation is delegated row by row to <Ref Oper="MultVectorRight"/> and
2107+
## <Ref Oper="MultVectorLeft"/>.
2108+
## <Example><![CDATA[
2109+
## gap> mat1 := [ [ 1, 2 ], [ 3, 4 ] ];
2110+
## [ [ 1, 2 ], [ 3, 4 ] ]
2111+
## gap> MultMatrixRight(mat1, -2);
2112+
## gap> mat1;
2113+
## [ [ -2, -4 ], [ -6, -8 ] ]
2114+
## gap> MultMatrix(mat1, -2); # Note that this is the same as calling MultMatrixLeft(mat1, -2)
2115+
## gap> mat1;
2116+
## [ [ 4, 8 ], [ 12, 16 ] ]
2117+
## gap> A := FreeAssociativeAlgebra(Rationals, 2);
2118+
## <algebra over Rationals, with 2 generators>
2119+
## gap> mat2 := [ [ A.1, A.2 ], [ A.1 * 2, A.2 * 3 ] ];
2120+
## [ [ (1)*x.1, (1)*x.2 ], [ (2)*x.1, (3)*x.2 ] ]
2121+
## gap> MultMatrixLeft(mat2, A.1);
2122+
## gap> mat2;
2123+
## [ [ (1)*x.1^2, (1)*x.1*x.2 ], [ (2)*x.1^2, (3)*x.1*x.2 ] ]
2124+
## gap> mat2 := [ [ A.1, A.2 ], [ A.1 * 2, A.2 * 3 ] ];
2125+
## [ [ (1)*x.1, (1)*x.2 ], [ (2)*x.1, (3)*x.2 ] ]
2126+
## gap> MultMatrixRight(mat2, A.1);
2127+
## gap> mat2;
2128+
## [ [ (1)*x.1^2, (1)*x.2*x.1 ], [ (2)*x.1^2, (3)*x.2*x.1 ] ]
2129+
## ]]></Example>
2130+
## </Description>
2131+
## </ManSection>
2132+
## <#/GAPDoc>
2133+
##
2134+
DeclareOperation( "MultMatrixRight", [ IsMatrixOrMatrixObj and IsMutable, IsScalar ] );
2135+
DeclareOperation( "MultMatrixLeft", [ IsMatrixOrMatrixObj and IsMutable, IsScalar ] );
2136+
DeclareSynonym( "MultMatrix", MultMatrixLeft );

lib/vec8bit.gi

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -452,7 +452,7 @@ end );
452452
#M <vec1> = <vec2>
453453
##
454454

455-
InstallMethod( \=, "for 2 8 bit vectors",
455+
InstallMethod( \=, "for two 8 bit vectors",
456456
IsIdenticalObj, [IsRowVector and Is8BitVectorRep,
457457
IsRowVector and Is8BitVectorRep],
458458
0,
@@ -465,7 +465,7 @@ InstallMethod( \=, "for 2 8 bit vectors",
465465
## Usual lexicographic ordering
466466
##
467467

468-
InstallMethod( \<, "for 2 8 bit vectors",
468+
InstallMethod( \<, "for two 8 bit vectors",
469469
IsIdenticalObj, [IsRowVector and Is8BitVectorRep,
470470
IsRowVector and Is8BitVectorRep],
471471
0,
@@ -477,7 +477,7 @@ InstallMethod( \<, "for 2 8 bit vectors",
477477
##
478478
## scalar product
479479
#'
480-
InstallMethod( \*, "for 2 8 bit vectors",
480+
InstallMethod( \*, "for two 8 bit vectors",
481481
IsIdenticalObj, [IsRingElementList and Is8BitVectorRep,
482482
IsRingElementList and Is8BitVectorRep],
483483
0,
@@ -514,7 +514,7 @@ end);
514514
## add <mult>*<vec2> to <vec1> in place
515515
##
516516

517-
InstallOtherMethod( AddRowVector, "for 2 8 bit vectors and a field element and from and to",
517+
InstallOtherMethod( AddRowVector, "for two 8 bit vectors and a field element and from and to",
518518
IsCollsCollsElmsXX, [ IsRowVector and Is8BitVectorRep,
519519
IsRowVector and Is8BitVectorRep,
520520
IsFFE and IsInternalRep, IsPosInt, IsPosInt ], 0,
@@ -527,7 +527,7 @@ InstallOtherMethod( AddRowVector, "for 2 8 bit vectors and a field element and f
527527
## add <mult>*<vec2> to <vec1> in place
528528
##
529529

530-
InstallOtherMethod( AddRowVector, "for 2 8 bit vectors and a field element",
530+
InstallOtherMethod( AddRowVector, "for two 8 bit vectors and a field element",
531531
IsCollsCollsElms, [ IsRowVector and Is8BitVectorRep,
532532
IsRowVector and Is8BitVectorRep,
533533
IsFFE and IsInternalRep ], 0,
@@ -540,7 +540,7 @@ InstallOtherMethod( AddRowVector, "for 2 8 bit vectors and a field element",
540540
## add <vec2> to <vec1> in place
541541
##
542542

543-
InstallOtherMethod( AddRowVector, "for 2 8 bit vectors",
543+
InstallOtherMethod( AddRowVector, "for two 8 bit vectors",
544544
IsIdenticalObj, [ IsRowVector and Is8BitVectorRep,
545545
IsRowVector and Is8BitVectorRep], 0,
546546
ADD_ROWVECTOR_VEC8BITS_2);

0 commit comments

Comments
 (0)