-
Notifications
You must be signed in to change notification settings - Fork 170
Added AddMatrixLeft
, MultMatrixLeft
functions as well as their "right" counterparts
#5980
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
32a7a19
d1f0623
19628eb
b375cf6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -1877,9 +1877,151 @@ | |||||||||||
|
||||||||||||
end ); | ||||||||||||
|
||||||||||||
############################################################################# | ||||||||||||
## | ||||||||||||
#M AddMatrix( <mat1>, <mat2> ) | ||||||||||||
## | ||||||||||||
|
||||||||||||
InstallMethod( AddMatrix, "for a mutable matrix object and a matrix object", | ||||||||||||
[ IsMatrixOrMatrixObj and IsMutable, IsMatrixOrMatrixObj ], | ||||||||||||
function( dstmat, srcmat ) | ||||||||||||
local i, j; | ||||||||||||
# Assert(0, NrRows(dstmat) = NrRows(srcmat)); | ||||||||||||
# Assert(0, NrCols(dstmat) = NrCols(srcmat)); | ||||||||||||
for i in [1..NrRows(dstmat)] do | ||||||||||||
for j in [1..NrCols(dstmat)] do | ||||||||||||
dstmat[i,j] := dstmat[i,j] + srcmat[i,j]; | ||||||||||||
od; | ||||||||||||
od; | ||||||||||||
end ); | ||||||||||||
|
||||||||||||
InstallEarlyMethod( AddMatrix, | ||||||||||||
function( dstmat, srcmat ) | ||||||||||||
local i; | ||||||||||||
if IsPlistRep(dstmat) and IsPlistRep(srcmat) then | ||||||||||||
# Assert(0, NrRows(dstmat) = NrRows(srcmat)); | ||||||||||||
# Assert(0, NrCols(dstmat) = NrCols(srcmat)); | ||||||||||||
for i in [1..NrRows(dstmat)] do | ||||||||||||
AddRowVector(dstmat[i], srcmat[i]); | ||||||||||||
od; | ||||||||||||
else | ||||||||||||
TryNextMethod(); | ||||||||||||
fi; | ||||||||||||
end ); | ||||||||||||
|
||||||||||||
InstallMethod( AddMatrix, "for a mutable 8bit matrix and an 8bit matrix", | ||||||||||||
[ Is8BitMatrixRep and IsMutable, Is8BitMatrixRep ], | ||||||||||||
function( dstmat, srcmat ) | ||||||||||||
local i, j; | ||||||||||||
# Assert(0, NrRows(dstmat) = NrRows(srcmat)); | ||||||||||||
# Assert(0, NrCols(dstmat) = NrCols(srcmat)); | ||||||||||||
Comment on lines
+1916
to
+1917
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These should either be removed, or turned into working code.
Suggested change
or
Suggested change
|
||||||||||||
for i in [1..NrRows(dstmat)] do | ||||||||||||
ADD_COEFFS_VEC8BIT_3(dstmat[i], srcmat[i]); | ||||||||||||
od; | ||||||||||||
end ); | ||||||||||||
|
||||||||||||
############################################################################# | ||||||||||||
## | ||||||||||||
#M AddMatrix( <mat1>, <mat2>, <mult> ) | ||||||||||||
## | ||||||||||||
|
||||||||||||
InstallMethod( AddMatrix, "for a mutable matrix object, a matrix object, and a scalar", | ||||||||||||
[ IsMatrixOrMatrixObj and IsMutable, IsMatrixOrMatrixObj, IsScalar ], | ||||||||||||
function( dstmat, srcmat, scalar ) | ||||||||||||
local i, j; | ||||||||||||
# Assert(0, NrRows(dstmat) = NrRows(srcmat)); | ||||||||||||
# Assert(0, NrCols(dstmat) = NrCols(srcmat)); | ||||||||||||
Comment on lines
+1932
to
+1933
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similar in other places which I did not mark |
||||||||||||
for i in [1..NrRows(dstmat)] do | ||||||||||||
for j in [1..NrCols(dstmat)] do | ||||||||||||
dstmat[i,j] := dstmat[i,j] + srcmat[i,j] * scalar; | ||||||||||||
od; | ||||||||||||
od; | ||||||||||||
end ); | ||||||||||||
|
||||||||||||
InstallEarlyMethod( AddMatrix, | ||||||||||||
function( dstmat, srcmat, scalar ) | ||||||||||||
local i; | ||||||||||||
if IsPlistRep(dstmat) and IsPlistRep(srcmat) then | ||||||||||||
# Assert(0, NrRows(dstmat) = NrRows(srcmat)); | ||||||||||||
# Assert(0, NrCols(dstmat) = NrCols(srcmat)); | ||||||||||||
for i in [1..NrRows(dstmat)] do | ||||||||||||
AddRowVector(dstmat[i], srcmat[i], scalar); | ||||||||||||
od; | ||||||||||||
else | ||||||||||||
TryNextMethod(); | ||||||||||||
fi; | ||||||||||||
end ); | ||||||||||||
|
||||||||||||
InstallMethod( AddMatrix, "for a mutable 8bit matrix, an 8bit matrix, and a scalar", | ||||||||||||
[ Is8BitMatrixRep and IsMutable, Is8BitMatrixRep, IsFFE ], | ||||||||||||
function( dstmat, srcmat, scalar ) | ||||||||||||
local i, j; | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The linter is unhappy, this is one of the changes needed to make it happy:
Suggested change
|
||||||||||||
# Assert(0, NrRows(dstmat) = NrRows(srcmat)); | ||||||||||||
# Assert(0, NrCols(dstmat) = NrCols(srcmat)); | ||||||||||||
for i in [1..NrRows(dstmat)] do | ||||||||||||
ADD_COEFFS_VEC8BIT_3(dstmat[i], srcmat[i], scalar); | ||||||||||||
od; | ||||||||||||
end ); | ||||||||||||
|
||||||||||||
############################################################################# | ||||||||||||
## | ||||||||||||
#M MultMatrixRight( <mat>, <mult> ) | ||||||||||||
## | ||||||||||||
|
||||||||||||
InstallMethod( MultMatrixRight, "for a mutable matrix object and a scalar", | ||||||||||||
[ IsMatrixOrMatrixObj and IsMutable, IsScalar ], | ||||||||||||
function( mat, scalar ) | ||||||||||||
local i, j; | ||||||||||||
for i in [1..NrRows(mat)] do | ||||||||||||
for j in [1..NrCols(mat)] do | ||||||||||||
mat[i,j] := mat[i,j] * scalar; | ||||||||||||
od; | ||||||||||||
od; | ||||||||||||
end ); | ||||||||||||
|
||||||||||||
InstallEarlyMethod( MultMatrixRight, | ||||||||||||
function( mat, scalar ) | ||||||||||||
local i; | ||||||||||||
if IsPlistRep(mat) and IsScalar(scalar) then | ||||||||||||
for i in [1..NrRows(mat)] do | ||||||||||||
MultVectorRight(mat[i], scalar); | ||||||||||||
od; | ||||||||||||
else | ||||||||||||
TryNextMethod(); | ||||||||||||
fi; | ||||||||||||
end ); | ||||||||||||
|
||||||||||||
############################################################################# | ||||||||||||
## | ||||||||||||
#M MultMatrixLeft( <mat>, <mult> ) | ||||||||||||
## | ||||||||||||
|
||||||||||||
InstallMethod( MultMatrixLeft, "for a mutable matrix object and a scalar", | ||||||||||||
[ IsMatrixOrMatrixObj and IsMutable, IsScalar ], | ||||||||||||
function( mat, scalar ) | ||||||||||||
local i, j; | ||||||||||||
for i in [1..NrRows(mat)] do | ||||||||||||
for j in [1..NrCols(mat)] do | ||||||||||||
mat[i,j] := scalar * mat[i,j]; | ||||||||||||
od; | ||||||||||||
od; | ||||||||||||
end ); | ||||||||||||
|
||||||||||||
InstallEarlyMethod( MultMatrixLeft, | ||||||||||||
function( mat, scalar ) | ||||||||||||
local i; | ||||||||||||
if IsPlistRep(mat) and IsScalar(scalar) then | ||||||||||||
for i in [1..NrRows(mat)] do | ||||||||||||
MultVectorLeft(mat[i], scalar); | ||||||||||||
od; | ||||||||||||
else | ||||||||||||
TryNextMethod(); | ||||||||||||
fi; | ||||||||||||
end ); | ||||||||||||
|
||||||||||||
############################################################################ | ||||||||||||
## Fallback method for DeterminantMatrix | ||||||||||||
|
||||||||||||
InstallMethod(DeterminantMatrix, ["IsMatrixObj"], | ||||||||||||
function( mat ) | ||||||||||||
return DeterminantMat( Unpack( mat ) ); | ||||||||||||
|
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -2052,3 +2052,86 @@ DeclareOperationKernel( "SwapMatrixRows", [ IsMatrixOrMatrixObj and IsMutable, I | |||
## <#/GAPDoc> | ||||
## | ||||
DeclareOperationKernel( "SwapMatrixColumns", [ IsMatrixOrMatrixObj and IsMutable, IsInt, IsInt ], SWAP_MAT_COLS ); | ||||
|
||||
############################################################################ | ||||
## | ||||
## <#GAPDoc Label="AddMatrixRight"> | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This chunk needs to be included by one of the Otherwise it won't appear in the manual. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also it should be changed to |
||||
## <ManSection> | ||||
## <Oper Name="AddMatrixRight" Arg='M, N[, c]'/> | ||||
## | ||||
## <Returns>nothing</Returns> | ||||
## | ||||
## <Description> | ||||
## <P/> | ||||
## Computes the calculation <M>M + c \cdot N</M> in-place, storing the result in <A>M</A>. | ||||
## If both of the matrices are lists-of-lists, then the program utilises <Ref Oper="AddRowVector"/> | ||||
## to perform the operation even faster. | ||||
## <Example><![CDATA[ | ||||
## gap> mat1 := [ [ 1, 2 ], [ 3, 4 ] ]; | ||||
## [ [ 1, 2 ], [ 3, 4 ] ] | ||||
## gap> mat2 := [ [ 1, 0 ], [ 3, -1 ] ]; | ||||
## [ [ 1, 0 ], [ 3, -1 ] ] | ||||
## gap> AddMatrix( mat1, mat2, 2 ); | ||||
## gap> mat1; | ||||
## [ [ 3, 2 ], [ 9, 2 ] ] | ||||
## gap> mat2; | ||||
## [ [ 1, 0 ], [ 3, -1 ] ] | ||||
## gap> AddMatrix( mat1, [ [ 1, 0], [ 3, -1] ] ); | ||||
## gap> mat1; | ||||
## [ [ 4, 2 ], [ 12, 1 ] ] | ||||
## ]]></Example> | ||||
## </Description> | ||||
## </ManSection> | ||||
## <#/GAPDoc> | ||||
## | ||||
DeclareOperation( "AddMatrix", [ IsMatrixOrMatrixObj and IsMutable, IsMatrixOrMatrixObj ] ); | ||||
DeclareOperation( "AddMatrix", [ IsMatrixOrMatrixObj and IsMutable, IsMatrixOrMatrixObj, IsScalar ] ); | ||||
|
||||
############################################################################ | ||||
## | ||||
## <#GAPDoc Label="MultMatrix"> | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This chunk also needs to be added |
||||
## <ManSection> | ||||
## <Oper Name="MultMatrix" Arg='mat, c'/> | ||||
## <Oper Name="MultMatrixLeft" Arg='mat, c'/> | ||||
## <Oper Name="MultMatrixRight" Arg='mat, c'/> | ||||
## | ||||
## <Returns>nothing</Returns> | ||||
## | ||||
## <Description> | ||||
## <P/> | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No need to start with a paragraph break here, I think?
Suggested change
|
||||
## These functions multiply the entries of <A>mat</A> by <A>c</A> in-place. | ||||
## <Ref Oper="MultMatrixRight"/> performs the operation <M>mat \cdot c</M>, | ||||
## whereas <Ref Oper="MultMatrixLeft"/> performs the operation <M>c \cdot mat</M> | ||||
## and <Ref Oper="MultMatrix"/> is an alias for <Ref Oper="MultMatrixLeft"/>. | ||||
## In all of these, if the matrix <A>mat</A> is a lists-of-lists, then the program | ||||
## utilises the fast in-place operations <Ref Oper="MultVectorRight"/> and <Ref Oper="MultVectorLeft"/>. | ||||
## to perform the operation even faster. | ||||
## <Example><![CDATA[ | ||||
## gap> mat1 := [ [ 1, 2 ], [ 3, 4 ] ]; | ||||
## [ [ 1, 2 ], [ 3, 4 ] ] | ||||
## gap> MultMatrixRight(mat1, -2); | ||||
## gap> mat1; | ||||
## [ [ -2, -4 ], [ -6, -8 ] ] | ||||
## gap> MultMatrix(mat1, -2); # Note that this is the same as calling MultMatrixLeft(mat1, -2) | ||||
## gap> mat1; | ||||
## [ [ 4, 8 ], [ 12, 16 ] ] | ||||
## gap> A := FreeAssociativeAlgebra(Rationals, 2); | ||||
## <algebra over Rationals, with 2 generators> | ||||
## gap> mat2 := [ [ A.1, A.2 ], [ A.1 * 2, A.2 * 3 ] ]; | ||||
## [ [ (1)*x.1, (1)*x.2 ], [ (2)*x.1, (3)*x.2 ] ] | ||||
## gap> MultMatrixLeft(mat2, A.1); | ||||
## gap> mat2; | ||||
## [ [ (1)*x.1^2, (1)*x.1*x.2 ], [ (2)*x.1^2, (3)*x.1*x.2 ] ] | ||||
## gap> mat2 := [ [ A.1, A.2 ], [ A.1 * 2, A.2 * 3 ] ]; | ||||
## [ [ (1)*x.1, (1)*x.2 ], [ (2)*x.1, (3)*x.2 ] ] | ||||
## gap> MultMatrixRight(mat2, A.1); | ||||
## gap> mat2; | ||||
## [ [ (1)*x.1^2, (1)*x.2*x.1 ], [ (2)*x.1^2, (3)*x.2*x.1 ] ] | ||||
## ]]></Example> | ||||
## </Description> | ||||
## </ManSection> | ||||
## <#/GAPDoc> | ||||
## | ||||
DeclareOperation( "MultMatrixRight", [ IsMatrixOrMatrixObj and IsMutable, IsScalar ] ); | ||||
DeclareOperation( "MultMatrixLeft", [ IsMatrixOrMatrixObj and IsMutable, IsScalar ] ); | ||||
DeclareSynonym( "MultMatrix", MultMatrixLeft); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The linter is unhappy, this is one of the changes needed to make it happy: