Skip to content

add a manual section: how to write code for matrix objects#6320

Merged
fingolfin merged 3 commits intogap-system:masterfrom
ThomasBreuer:TB_matrixobj_rules
Apr 16, 2026
Merged

add a manual section: how to write code for matrix objects#6320
fingolfin merged 3 commits intogap-system:masterfrom
ThomasBreuer:TB_matrixobj_rules

Conversation

@ThomasBreuer
Copy link
Copy Markdown
Contributor

@ThomasBreuer ThomasBreuer commented Apr 13, 2026

Collect some rules how to write code that works for for matrix objects and if applicable also for "list of list" matrices.

Ideas for more entries are welcome.

(motivated by the discussion in gap-packages/nofoma/pull/87)

@ThomasBreuer ThomasBreuer added kind: enhancement Label for issues suggesting enhancements; and for pull requests implementing enhancements topic: documentation Issues and PRs related to documentation release notes: not needed PRs introducing changes that are wholly irrelevant to the release notes labels Apr 13, 2026
Comment thread doc/ref/matobj.xml Outdated
<Heading>How to Write Code for Vector and Matrix Objects</Heading>

Most &GAP; functions which deal with vector and matrix objects
are intended to accept any kind of these objects,
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This reads a little like "functions for dealing with X accept X" which is tautological. I think you mean: "functions for dealing with vector and matrix objects are intended to accept objects in any representation" or something similar. It might be useful to mention where one can read about the different representations here and/or may amend what you've written to say:

Vector and matrix objects have a number of different representations in &GAP; which are optimised for different things; see for example .... Most &GAP; functions accepting vector or matrix objects as arguments do not depend on any particular representation for these objects. If the output of such a function involves vector and matrix objects, then these
are expected to have the same representations as the inputs. For example, if the input to XXX belongs to the representation IsYYY, then the output will belong to IsYYY also.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I see you've already got the "For example.." sorry, should have read everything first.

Comment thread doc/ref/matobj.xml Outdated
For example, <C>ZeroMatrix( R, m, n )</C> returns a matrix object in an
internal representation that is suitable for the base domain <C>R</C>.
However, if we want a matrix object that is compatible with a given
matrix object <C>M</C> then we are not allowed to call a variant
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I'm not sure I follow this sentence: couldn't we call ZeroMatrix(IsPlistRep, BaseDomain(M), m, n); if M belonged to IsPlistRep? If so, then who or what is not allowing us to do this?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

The sentence is "... we are not allowed to call a variant that involves such choices, ...", this is the main point.

Yes, we can write ZeroMatrix( ConstructingFilter( M ), BaseDomain( M ), m, n ) but ZeroMatrix( m, n, M ) is simpler if the matrix M is given. I will rewrite the last part of the sentence accordingly.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks @ThomasBreuer, I still don't really understand the first part of the sentence, specifically what is not allowed and by who? Otherwise, the changes are great, and I'd be happy if this was merged.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

What I want to say:

  • In principle the different signatures of ZeroMatrix make sense.
  • ZeroMatrix( R, m, n ) can be used if we want to leave the decision about the representation of the result to GAP; this can be useful if the result is the first matrix we create, and then later matrices are created relative to this matrix.
  • If we have already a matrix object M and want ZeroMatrix to return a new matrix object that fits to M then we cannot leave the decision to GAP. (For example, even if we use the same R as before, GAP might decide to use a sparse representation for large enough m and n.)
  • In this situation, we have to prescribe the intended representation. Thus ZeroMatrix( R, m, n ) cannot be used. I would express this by saying say that ZeroMatrix( R, m, n ) is not allowed in this situation. What would be better?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks @ThomasBreuer I understand now, how about the following version of what you've just written?

ZeroMatrix( R, m, n ) can be used, for example, if we want to leave the decision about the representation of the result to GAP. This might be useful if the result of ZeroMatrix( R, m, n ) is the first matrix we create, and then later matrices are created relative to this matrix. If we already have a matrix object M and want ZeroMatrix to return a new matrix object that has the same representation as M, then ZeroMatrix(R, m, n) may not return a matrix with the expected representation. For example, even if we use the same R as before, GAP might decide to use a sparse representation for large enough m and n. It is possible to create a ZeroMatrix in the same representation as M by fully specifying this representation as follows ZeroMatrix( ConstructingFilter( M ), BaseDomain( M ), m, n ). It is also possible to produce the same result using the more convenient ZeroMatrix( m, n, M ).

Probably this can be improved further, but I think it clearly says what you had in mind, and it is possible to understand what it meant.

This is perhaps no longer important, but I think the issue I have with the original wording was that "cannot" and "not allowed" are too strong. Of course nothing can stop me from typing ZeroMatrix(R, m, n) I just won't get the answer I hoped for, if you see what I mean. Saying "we are not allowed" makes it sounds like we'd get an error or something would prevent us from doing this, which of course it won't.

Comment thread doc/ref/matobj.xml Outdated
Comment thread doc/ref/matobj.xml Outdated
Comment thread doc/ref/matobj.xml Outdated
Comment thread doc/ref/matobj.xml Outdated
Copy link
Copy Markdown
Contributor

@james-d-mitchell james-d-mitchell left a comment

Choose a reason for hiding this comment

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

I think this is super helpful, and could be merged as is, although I've made a few suggestions that might make it clearer still. In the "rules" at the end, it might be helpful to have some rationale for why these rules are preferable to the alternatives, but that could also be done later.

Comment thread doc/ref/matobj.xml
Comment thread doc/ref/matobj.xml
<Item>
Use <C>ExtractSubMatrix( M, rows, cols )</C> not <C>M{ rows }{ cols }</C>
for accessing submatrices,
similarly use <C>CopySubMatrix( src, dst, srows, drows, scols, dcols )</C>.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Again presumably better because the intermediate object M{rows} is not created?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

That, and also the {...} syntax may not be available at all (it can only be supported in general for row-list matrices).

Perhaps there should be a general pattern: terse "rule" to follow (just what to do), optionally followed by an explanation; perhaps in a visually distinct style: new paragraph, and then a key phrase ("The reason for this is/are"; or just "Background:" or "Motivation:", possibly in italics or so)

Comment thread doc/ref/matobj.xml
row-list matrices, see <Ref Filt="IsRowListMatrix"/>.
</Item>
<Item>
Use <C>ZeroVector( R, n )</C> not <C>[ 1 .. n ] * Zero( R )</C> for
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Maybe expand it?

Suggested change
Use <C>ZeroVector( R, n )</C> not <C>[ 1 .. n ] * Zero( R )</C> for
Use <C>ZeroVector( R, n )</C> not <C>[ 1 .. n ] * Zero( R )</C> or
<C>ListWithIdenticalEntries( n, Zero( R ) )</C> or similar constructs for

@fingolfin
Copy link
Copy Markdown
Member

Should we also point out that IdentityMat / NullMat should be replaced by IdentityMatrix / ZeroMatrix ?

Comment thread doc/ref/matobj.xml
<Item>
Use <C>ExtractSubMatrix( M, rows, cols )</C> not <C>M{ rows }{ cols }</C>
for accessing submatrices,
similarly use <C>CopySubMatrix( src, dst, srows, drows, scols, dcols )</C>.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Perhaps also ExtractSubVector and CopySubVector should be mentioned?

Copy link
Copy Markdown
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.

Great start, happy to merge it (though let's wait if @james-d-mitchell has further comments)

I wonder if some code snippets / examples here and there would be useful. But perhaps the best way to find out is to find someone (e.g. during GAP Days) who doesn't know MatrixObj stuff yet, and let them try to figure out, with the help of this guide, how to convert some functions in the GAP library or in a package to support MatrixObj

@ThomasBreuer
Copy link
Copy Markdown
Contributor Author

I wonder if some code snippets / examples here and there would be useful.

I am not sure what "examples" means here.

If it is about examples how to write functions for new matrix object implementations then I think it is better to point to available implementations in the GAP library (if they are in a reasonable state). Copying and modifying such code should be the recommended way to proceed.
(For example, point to the "generic" implementation from #6322 as soon as it is available.)

If it is about using matrix objects for example in MeatAxe like situations then perhaps it would be useful to turn the description of the dangerous unary ConvertToMatrixRep into an explicit example, and to add more such examples.

Copy link
Copy Markdown
Contributor

@james-d-mitchell james-d-mitchell left a comment

Choose a reason for hiding this comment

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

Looks good no further changes from me!

@cdwensley
Copy link
Copy Markdown
Contributor

cdwensley commented Apr 15, 2026

I wonder if some code snippets / examples here and there would be useful.
As someone not involved with this development, I find the 22 pages of chapter 26 (from 26.3 onwards), without any examples at all, quite unacceptable - it makes the operations introduced far too difficult to assimilate. Perhaps, as "someone who doesn't know MatrixObj stuff yet" I could help?

@fingolfin
Copy link
Copy Markdown
Member

@cdwensley yeah I fully agree: that chapter needs lots and lots of usage examples; and some of those will surely also benefit from discussing (and showing!) difference compared to the "old" way of doing things. However, of course this doesn't have to be done in this PR.

If you were willing to help with that, it would be greatly appreciated. And of course it wouldn't have be "everything" -- just improving one function at a time can be super helpful. My suggestion would be to just start it (and if you get stuck because something is unclear, just ask -- e.g. open a "draft" PR with the incomplete changes and ask for comments on specific sections, @ThomasBreuer and me will be happy to help!

@fingolfin fingolfin merged commit 050739d into gap-system:master Apr 16, 2026
32 checks passed
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 release notes: not needed PRs introducing changes that are wholly irrelevant to the release notes topic: documentation Issues and PRs related to documentation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants