Skip to content
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

Add basic operations for row/column reductions in matrix objects #4517

Merged
merged 24 commits into from
Jun 15, 2021
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
eb37b25
Add generic functions of issue 3962.
danielrademacher May 27, 2021
1b1905b
Add documentation
May 27, 2021
c1cb279
Fix daniels mistakes
May 27, 2021
02673a0
Tests for SwapMatrixRows and -Columns
May 27, 2021
d0cec97
Checks fixed
May 27, 2021
9472dbf
Corrections
May 27, 2021
1035a6f
Tests: corrections for MatrixObj
May 27, 2021
800d82d
Touch up with Sergio and Thomas' suggestions
May 27, 2021
63eb721
WIP documentation
May 28, 2021
0cf7ec9
Removed bound checks and moved doc in .gd file
May 29, 2021
7fdf647
Tests for all functions and removed checks for IsRowListMatrix
danielrademacher May 29, 2021
e334988
Improvement of swapping rows
danielrademacher Jun 2, 2021
e08f218
Newline at the end of file
danielrademacher Jun 2, 2021
42e1c70
Merge branch 'master' into MatrixObj3962
danielrademacher Jun 2, 2021
592fdbd
Old version of swapmatrixrows
danielrademacher Jun 2, 2021
d4f30c5
Move documentary of RowListMatrix
danielrademacher Jun 2, 2021
1e2226c
Synch master to PR
danielrademacher Jun 2, 2021
e31c2b4
Improvement of documentary: Change synonyms and give an explanation.
danielrademacher Jun 8, 2021
9423dca
Docu improvement: Added a explanation for the synonym choice of MultV…
danielrademacher Jun 8, 2021
334b3b1
Docu correction: Added linebreak in documentation and corrected the t…
danielrademacher Jun 8, 2021
b663332
Docu improvement: Remove old todo list.
danielrademacher Jun 9, 2021
73e5d51
Docu improvement: First and second row/column number -> two row/colum…
danielrademacher Jun 9, 2021
a77ed46
Docu improvement: First and second row/column number -> two row/colum…
danielrademacher Jun 9, 2021
6c6f503
Docu improvement: one row/column -> a row/column.
danielrademacher Jun 9, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions doc/ref/matobj.xml
Original file line number Diff line number Diff line change
Expand Up @@ -286,5 +286,22 @@ for your new type of vector or matrix objects.)

</Section>

<!-- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -->
<Section Label="Basic operations for row/column reductions">
<Heading>Basic operations for row/column reductions</Heading>

<#Include Label="MultMatrixRow">
<#Include Label="MultMatrixRowRight">
<#Include Label="MultMatrixColumn">
<#Include Label="MultMatrixColumnLeft">
<#Include Label="AddMatrixRows">
<#Include Label="AddMatrixRowsRight">
<#Include Label="AddMatrixColumns">
<#Include Label="AddMatrixColumnsLeft">
<#Include Label="SwapMatrixRows">
<#Include Label="SwapMatrixColumns">

</Section>

</Chapter>

371 changes: 371 additions & 0 deletions lib/matobj.gi
Original file line number Diff line number Diff line change
Expand Up @@ -1568,3 +1568,374 @@ InstallMethod( \[\,\]\:\=, "for a matrix object, two positions, and an object",
end );


############################################################################
# Elementary matrix operations
############################################################################

############################################################################
##
## <#GAPDoc Label="MultMatrixRow">
## <ManSection>
## <Oper Name="MultMatrixRow" Arg='mat,i,elm'/>
## <Oper Name="MultMatrixRowLeft" Arg='mat,i,elm'/>
##
## <Returns>nothing</Returns>
##
## <Description>
## <P/>
## Multiplies the <A>i</A>-th row of the mutable matrix <A>mat</A> with the scalar
## <A>elm</A> from the left in-place.
## <P/>
## <Ref Oper="MultMatrixRowLeft"/> is a synonym of <Ref Oper="MultMatrixRow"/>.
## </Description>
## </ManSection>
## <#/GAPDoc>
##
InstallMethod( MultMatrixRow, "for a mutable matrix object, one row number, and a scalar",
[ IsMatrixObj and IsMutable, IsInt, IsObject ],
function( mat, row, scalar )
local i;

# Checks
if not( 0 < row and row <= NrRows(mat) ) then
ErrorNoReturn("the second argument <row> has to fulfill 0 < row <= NrRows(mat) ");
AnnaKDS marked this conversation as resolved.
Show resolved Hide resolved
fi;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to Codecov, this function is lacking tests

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So far, we have only written tests for SwapMatrixRows and SwapMatrixColumns. We plan to also write tests for all other functions that we added.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tests for all functions have been added.

Copy link
Contributor

@danielrademacher danielrademacher May 29, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems like GAP is always using the "IsRowListMatrix" functions. Is there a way to define a matrix object which is not a "RowListMatrix"?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good question. In the end, this is what IsPlistMatrixRep (or perhaps IsStrictPlistMatrixRep) should do; see PR #2973 . It shouldn't be hard to do that atop the mentioned PR.


for i in [1..NrCols(mat)] do
mat[row,i] := scalar * mat[row,i];
od;

end );


############################################################################
##
## <#GAPDoc Label="MultMatrixColumn">
## <ManSection>
## <Oper Name="MultMatrixColumn" Arg='mat,i,elm'/>
## <Oper Name="MultMatrixColumnRight" Arg='mat,i,elm'/>
##
## <Returns>nothing</Returns>
##
## <Description>
## <P/>
## Multiplies the <A>i</A>-th column of the mutable matrix <A>mat</A> with the scalar
## <A>elm</A> from the right in-place.
## <P/>
## <Ref Oper="MultMatrixColumnRight"/> is a synonym of <Ref Oper="MultMatrixColumn"/>.
## </Description>
## </ManSection>
## <#/GAPDoc>
##
InstallMethod( MultMatrixColumn, "for a mutable matrix object, one column number, and a scalar",
[ IsMatrixObj and IsMutable, IsInt, IsObject ],
function( mat, column, scalar )
local i;

# Checks
if not( 0 < column and column <= NrCols(mat) ) then
ErrorNoReturn("the second argument column has to fulfill 0 < column <= NrCols(mat) ");
fi;

for i in [1..NrRows(mat)] do
mat[i,column] := mat[i,column] * scalar;
od;

end );


############################################################################
##
## <#GAPDoc Label="MultMatrixRowRight">
## <ManSection>
## <Oper Name="MultMatrixRowRight" Arg='mat,i,elm'/>
##
## <Returns>nothing</Returns>
##
## <Description>
## <P/>
## Multiplies the <A>i</A>-th row of the mutable matrix <A>mat</A> with the scalar
## <A>elm</A> from the right in-place.
## </Description>
## </ManSection>
## <#/GAPDoc>
##
InstallMethod( MultMatrixRowRight, "for a mutable matrix object, one row number, and a scalar",
[ IsMatrixObj and IsMutable, IsInt, IsObject ],
function( mat, row, scalar )
local i;

# Checks
if not( 0 < row and row <= NrRows(mat) ) then
ErrorNoReturn("the second argument row has to fulfill 0 < row <= NrRows(mat) ");
fi;

for i in [1..NrCols(mat)] do
mat[row,i] := mat[row,i] * scalar;
od;

end );

############################################################################
##
## <#GAPDoc Label="MultMatrixColumnLeft">
AnnaKDS marked this conversation as resolved.
Show resolved Hide resolved
## <ManSection>
## <Oper Name="MultMatrixColumnLeft" Arg='mat,i,elm'/>
##
## <Returns>nothing</Returns>
##
## <Description>
## <P/>
## Multiplies the <A>i</A>-th column of the mutable matrix <A>mat</A> with the scalar
## <A>elm</A> from the left in-place.
## </Description>
## </ManSection>
## <#/GAPDoc>
##
InstallMethod( MultMatrixColumnLeft, "for a mutable matrix object, one column number, and a scalar",
[ IsMatrixObj and IsMutable, IsInt, IsObject ],
function( mat, column, scalar )
local i;

# Checks
if not( 0 < column and column <= NrCols(mat) ) then
ErrorNoReturn("the second argument column has to fulfill 0 < column <= NrCols(mat) ");
fi;

for i in [1..NrRows(mat)] do
mat[i,column] := scalar * mat[i,column];
od;

end );


############################################################################
##
## <#GAPDoc Label="AddMatrixRows">
## <ManSection>
## <Oper Name="AddMatrixRows" Arg='mat,i,j,elm'/>
## <Oper Name="AddMatrixRowsLeft" Arg='mat,i,j,elm'/>
##
## <Returns>nothing</Returns>
##
## <Description>
## <P/>
## Adds the product of <A>elm</A> with the <A>j</A>-th row of the mutable matrix <A>mat</A> to its <A>i</A>-th
## row in-place. The <A>j</A>-th row is multiplied with <A>elm</A> from the left.
## <P/>
## <Ref Oper="AddMatrixRowsLeft"/> is a synonym of <Ref Oper="AddMatrixRows"/>.
## </Description>
## </ManSection>
## <#/GAPDoc>
##
InstallMethod( AddMatrixRows, "for a mutable matrix object, one row number, second row number, and a scalar",
[ IsMatrixObj and IsMutable, IsInt, IsInt, IsObject ] ,
function( mat, row1, row2, scalar )
local i;

# Checks
if not( 0 < row1 and row1 <= NrRows(mat) ) then
ErrorNoReturn("the second argument row has to fulfill 0 < row <= NrRows(mat) ");
fi;
if not( 0 < row2 and row2 <= NrRows(mat) ) then
ErrorNoReturn("the third argument row has to fulfill 0 < row <= NrRows(mat) ");
fi;

for i in [1..NrCols(mat)] do
mat[row1,i] := mat[row1,i] + scalar * mat[row2,i];
od;

end );


############################################################################
##
## <#GAPDoc Label="AddMatrixRowsRight">
## <ManSection>
## <Oper Name="AddMatrixRowsRight" Arg='mat,i,j,elm'/>
##
## <Returns>nothing</Returns>
##
## <Description>
## <P/>
## Adds the product of <A>elm</A> with the <A>j</A>-th row of the mutable matrix <A>mat</A> to its <A>i</A>-th
## row in-place. The <A>j</A>-th row is multiplied with <A>elm</A> from the right.
## </Description>
## </ManSection>
## <#/GAPDoc>
##
InstallMethod( AddMatrixRowsRight, "for a mutable matrix object, one row number, second row number, and a scalar",
[ IsMatrixObj and IsMutable, IsInt, IsInt, IsObject ] ,
function( mat, row1, row2, scalar )
local i;

# Checks
if not( 0 < row1 and row1 <= NrRows(mat) ) then
ErrorNoReturn("the second argument row has to fulfill 0 < row <= NrRows(mat) ");
fi;
if not( 0 < row2 and row2 <= NrRows(mat) ) then
ErrorNoReturn("the third argument row has to fulfill 0 < row <= NrRows(mat) ");
fi;

for i in [1..NrCols(mat)] do
mat[row1,i] := mat[row1,i] + mat[row2,i] * scalar;
od;

end );

############################################################################
##
## <#GAPDoc Label="AddMatrixColumns">
## <ManSection>
## <Oper Name="AddMatrixColumns" Arg='mat,i,j,elm'/>
## <Oper Name="AddMatrixColumnsRight" Arg='mat,i,j,elm'/>
##
## <Returns>nothing</Returns>
##
## <Description>
## <P/>
## Adds the product of <A>elm</A> with the <A>j</A>-th column of the mutable matrix <A>mat</A> to its <A>i</A>-th
## column in-place. The <A>j</A>-th column is multiplied with <A>elm</A> from the right.
## <P/>
## <Ref Oper="AddMatrixColumnsRight"/> is a synonym of <Ref Oper="AddMatrixColumns"/>.
## </Description>
## </ManSection>
## <#/GAPDoc>
##
InstallMethod( AddMatrixColumns, "for a mutable matrix object, one column number, second column number, and a scalar",
[ IsMatrixObj and IsMutable, IsInt, IsInt, IsObject ] ,
function( mat, column1, column2, scalar )
local i;

# Checks
if not( 0 < column1 and column1 <= NrCols(mat) ) then
ErrorNoReturn("the second argument column has to fulfill 0 < column <= NrCols(mat) ");
fi;
if not( 0 < column2 and column2 <= NrCols(mat) ) then
ErrorNoReturn("the third argument column has to fulfill 0 < column <= NrCols(mat) ");
fi;

for i in [1..NrRows(mat)] do
mat[i,column1] := mat[i,column1] + mat[i,column2] * scalar;
od;

end );

############################################################################
##
## <#GAPDoc Label="AddMatrixColumnsLeft">
## <ManSection>
## <Oper Name="AddMatrixColumnsLeft" Arg='mat,i,j,elm'/>
##
## <Returns>nothing</Returns>
##
## <Description>
## <P/>
## Adds the product of <A>elm</A> with the <A>j</A>-th column of the mutable matrix <A>mat</A> to its <A>i</A>-th
## column in-place. The <A>j</A>-th column is multiplied with <A>elm</A> from the left.
## </Description>
## </ManSection>
## <#/GAPDoc>
##
InstallMethod( AddMatrixColumns, "for a mutable matrix object, one column number, second column number, and a scalar",
[ IsMatrixObj and IsMutable, IsInt, IsInt, IsObject ] ,
function( mat, column1, column2, scalar )
local i;

# Checks
if not( 0 < column1 and column1 <= NrCols(mat) ) then
ErrorNoReturn("the second argument column has to fulfill 0 < column <= NrCols(mat) ");
fi;
if not( 0 < column2 and column2 <= NrCols(mat) ) then
ErrorNoReturn("the third argument column has to fulfill 0 < column <= NrCols(mat) ");
fi;

for i in [1..NrRows(mat)] do
mat[i,column1] := mat[i,column1] + scalar * mat[i,column2] ;
od;

end );


############################################################################
##
## <#GAPDoc Label="SwapMatrixRows">
## <ManSection>
## <Oper Name="SwapMatrixRows" Arg='mat,i,j'/>
##
## <Returns>nothing</Returns>
##
## <Description>
## <P/>
## Swaps the <A>i</A>-th row and <A>j</A>-th row of a mutable matrix <A>mat</A>.
## </Description>
## </ManSection>
## <#/GAPDoc>
##
InstallMethod( SwapMatrixRows, "for a mutable matrix object, one row number, second row number",
[ IsMatrixObj and IsMutable, IsInt, IsInt ],
function( mat, row1, row2 )
local temp, i;

if row1 <> row2 then

# Checks
if not( 0 < row1 and row1 <= NrRows(mat) ) then
ErrorNoReturn("the second argument row has to fulfill 0 < row <= NrRows(mat) ");
fi;
if not( 0 < row2 and row2 <= NrRows(mat) ) then
ErrorNoReturn("the third argument row has to fulfill 0 < row <= NrRows(mat) ");
fi;

for i in [1..NrCols(mat)] do
temp := mat[row1,i];
mat[row1,i] := mat[row2,i];
mat[row2,i] := temp;
od;

fi;


end );


############################################################################
##
## <#GAPDoc Label="SwapMatrixColumns">
## <ManSection>
## <Oper Name="SwapMatrixColumns" Arg='mat,i,j'/>
##
## <Returns>nothing</Returns>
##
## <Description>
## <P/>
## Swaps the <A>i</A>-th column and <A>j</A>-th column of a mutable matrix <A>mat</A>.
## </Description>
## </ManSection>
## <#/GAPDoc>
##
InstallMethod( SwapMatrixColumns, "for a mutable matrix object, one column number, second column number",
[ IsMatrixObj and IsMutable, IsInt, IsInt ],
function( mat, column1, column2 )
local temp, i;

if column1 <> column2 then

# Checks
if not( 0 < column1 and column1 <= NrCols(mat) ) then
ErrorNoReturn("the second argument column has to fulfill 0 < column <= NrCols(mat) ");
fi;
if not( 0 < column2 and column2 <= NrCols(mat) ) then
ErrorNoReturn("the third argument column has to fulfill 0 < column <= NrCols(mat) ");
fi;

for i in [1..NrRows(mat)] do
temp := mat[i,column1];
mat[i,column1] := mat[i,column2];
mat[i,column2] := temp;
od;

fi;


end );
Loading