Skip to content

Add AddMatrix, MultMatrix (plus explicit left/right side variants) for in-place modification of matrices#5980

Open
OldAnchovyTopping wants to merge 4 commits intogap-system:masterfrom
OldAnchovyTopping:mh/AddMatrix
Open

Add AddMatrix, MultMatrix (plus explicit left/right side variants) for in-place modification of matrices#5980
OldAnchovyTopping wants to merge 4 commits intogap-system:masterfrom
OldAnchovyTopping:mh/AddMatrix

Conversation

@OldAnchovyTopping
Copy link
Contributor

@OldAnchovyTopping OldAnchovyTopping commented Apr 11, 2025

  • Implemented the AddMatrixLeft/Right and MultMatrixLeft/Right methods.
  • Implemented the method MultVectorRight for lists to support MultMatrixRight method.

To make progress on issue #5952 I implemented the operations of summing and multiplying matrices in-place.

Further work

These functions should now be ready to be used in meatauto.gi to speed up those computations (as well as in other other place that might benefit from in-place matrix operations).

The functions also do NOT have coverage tests, that should be added sometime later too.

@OldAnchovyTopping OldAnchovyTopping changed the title TBAL Added AddMatrixLeft, MultMatrixLeft functions as well as their "right" counterparts Apr 11, 2025
Copy link
Member

@fingolfin fingolfin left a comment

Choose a reason for hiding this comment

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

Thanks! Some quick comments

@fingolfin
Copy link
Member

@OldAnchovyTopping did you see the comments I left you? Just wondering, no pressure -- if you can work some more on this that would be great, but of course you need to know if and when you have time for it.

@fingolfin
Copy link
Member

I've used Codex to update this PR.

@fingolfin fingolfin changed the title Added AddMatrixLeft, MultMatrixLeft functions as well as their "right" counterparts Add AddMatrix, MultMatrix (plus explicit left/right side variants) for in-place modification of matrices Mar 24, 2026
@fingolfin fingolfin changed the title Add AddMatrix, MultMatrix (plus explicit left/right side variants) for in-place modification of matrices Add AddMatrix, MultMatrix (plus explicit left/right side variants) for in-place modification of matrices, and add some missing MultVectorRight methods Mar 24, 2026
fingolfin and others added 3 commits March 25, 2026 13:58
Co-authored-by: Max Horn <max@quendi.de>
Address the outstanding review feedback for AddMatrix and
MultMatrix. This adds explicit dimension checks, fixes the 8-bit
AddMatrix fast path, completes the right-multiplication
specializations, and wires the new manual entries into the matrix
object chapter.

AI assistance: Codex was used to implement the changes, expand the
MatrixObj test coverage, and update the documentation.

Co-authored-by: Codex <codex@openai.com>
@fingolfin fingolfin marked this pull request as ready for review March 25, 2026 13:00
@fingolfin fingolfin requested a review from ThomasBreuer March 25, 2026 13:00
@fingolfin fingolfin added kind: enhancement Label for issues suggesting enhancements; and for pull requests implementing enhancements topic: library labels Mar 25, 2026
@fingolfin fingolfin changed the title Add AddMatrix, MultMatrix (plus explicit left/right side variants) for in-place modification of matrices, and add some missing MultVectorRight methods Add AddMatrix, MultMatrix (plus explicit left/right side variants) for in-place modification of matrices Mar 25, 2026
Copy link
Contributor

@ThomasBreuer ThomasBreuer left a comment

Choose a reason for hiding this comment

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

A few fixes and some improvements.


AddMatrix(mat, src);
AddMatrix(copy, srccopy);
if not same_entries(mat, copy) then Error("AddMatrix(", scalar, ") failure"); fi;
Copy link
Contributor

Choose a reason for hiding this comment

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

In the current test, scalar plays no role.

AddMatrix(mat, src);
AddMatrix(copy, srccopy);
if not same_entries(mat, copy) then Error("AddMatrix(", scalar, ") failure"); fi;
if not same_entries(src, srccopy) then Error("AddMatrix source modified"); fi;
Copy link
Contributor

Choose a reason for hiding this comment

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

Both src and srccopy have been involved in an AddMatrix call. If we suspect that this call has modified its second argument then it would be more natural to compare the second argument with a copy that was made before the call and was not used since then.

lib/matobj2.gd Outdated
##
## <Description>
## 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>,
Copy link
Contributor

Choose a reason for hiding this comment

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

GAPDoc syntax

Suggested change
## <Ref Oper="MultMatrixRight"/> performs the operation <M>mat \cdot c</M>,
## <Ref Oper="MultMatrixRight"/> performs the operation <A>mat</A><C>*</C><A>c</A>,

lib/matobj2.gd Outdated
## <Description>
## 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>
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
## whereas <Ref Oper="MultMatrixLeft"/> performs the operation <M>c \cdot mat</M>
## whereas <Ref Oper="MultMatrixLeft"/> performs the operation <A>c</A><C>*</C><A>mat</A>

lib/matobj2.gd Outdated
## Computes the calculation <M>M + c \cdot N</M> in-place, storing the result in <A>M</A>.
## If the optional argument <A>c</A> is omitted, then <A>N</A> is added directly.
## If both of the matrices are lists-of-lists, then the program utilises <Ref Oper="AddRowVector"/>
## to perform the operation even faster.
Copy link
Contributor

Choose a reason for hiding this comment

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

Is it clear that AddRowVector is faster? (What can AddRowVector do what AddMatrix cannot do?)

Copy link
Member

Choose a reason for hiding this comment

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

I am not sure I understand the question: AddMatrix and AddRowVector take different argument types a priori? And this is about adding a generic AddMatrix method for lists-of-row-vector" style matrices by using AddRowVector` (for which already several optimized variants exist, e.g. for compressed vectors)

Copy link
Member

Choose a reason for hiding this comment

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

anyway, updated the text

Copy link
Contributor

Choose a reason for hiding this comment

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

This documentation is not about a generic method but about the operation itself. Statements about the efficiency of the generic method would belong to a separate documentation of this method.

lib/matobj2.gd Outdated
## 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.
Copy link
Contributor

Choose a reason for hiding this comment

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

Is it clear that the MultVector operations are faster? (What can they do what MultMatrix cannot do?)

Refine the AddMatrix and MultMatrix documentation in response to
review feedback by using GAPDoc-safe infix markup and describing
the row-wise delegation without claiming a speedup.

Also strengthen the whole-matrix regression helper so it checks
the AddMatrix source argument against an untouched pre-call
snapshot.

AI assistance: Codex was used to apply the review-driven test and
documentation updates.

Co-authored-by: Codex <codex@openai.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

kind: enhancement Label for issues suggesting enhancements; and for pull requests implementing enhancements topic: library

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

3 participants