Skip to content
Merged
Changes from 1 commit
Commits
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
86 changes: 86 additions & 0 deletions doc/ref/matobj.xml
Original file line number Diff line number Diff line change
Expand Up @@ -402,4 +402,90 @@ The following conventions hold for such a group <C>G</C>.

</Section>

<!-- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -->
<Section Label="How to Write Code for Vector and Matrix Objects">
<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.

and if the output involves vector and matrix objects then these
are expected to be compatible with the inputs.
For example, a function that computes the Kronecker product of two
matrix objects <C>mat1</C>, <C>mat2</C> may take the two inputs,
which must have the same
<Ref Filt="ConstructingFilter" Label="for a matrix object"/> value,
create a new matrix object which also lies in this filter,
for example by calling <C>ZeroMatrix( m, n, mat1 )</C>,
and then set the entries in this matrix.

<P/>

Functions like
<Ref Oper="ZeroMatrix" Label="for dimensions and matrix object"/>,
<Ref Oper="IdentityMatrix" Label="for dimension and matrix object"/>,
and <Ref Oper="Matrix" Label="for a list and a matrix object"/>
admit different kinds of inputs,
some of which leave the decision about the type of the output to &GAP;.
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.

that involves such choices,
and we have to call <C>ZeroMatrix( m, n, M )</C>.

<P/>

This approach works also in many situation where the input involves
the <Q>list of list</Q> matrices in the filter <Ref Filt="IsMatrix"/>.
In practice, the question is often the other way round:
One has old &GAP; code that was written for objects in <Ref Filt="IsMatrix"/>,
Comment thread
james-d-mitchell marked this conversation as resolved.
Outdated
and wants to rewrite it such that it works also for general matrix objects.
Comment thread
james-d-mitchell marked this conversation as resolved.
Outdated
Comment thread
james-d-mitchell marked this conversation as resolved.
Outdated
In such cases, the following rules may be useful.
Comment thread
james-d-mitchell marked this conversation as resolved.
Outdated

<List>
<Item>
Use <C>M[i, j]</C> not <C>M[i][j]</C>
for accessing/assigning matrix entries.
Comment thread
james-d-mitchell marked this conversation as resolved.
</Item>
<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)

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?

</Item>
<Item>
Use <C>ZeroVector( R, n )</C> not <C>[ 1 .. n ] * Zero( R )</C> for a
given domain <C>R</C>.
</Item>
<Item>
Use <C>ZeroVector( n, M )</C> not <C>0 * M[1]</C> for a
given matrix object <C>M</C> with <C>n</C> columns.
</Item>
<Item>
Use <Ref Attr="BaseDomain" Label="for a matrix object"/>
not <Ref Attr="DefaultFieldOfMatrix"/>.
</Item>
<Item>
Do not use the unary versions of
<Ref Func="ConvertToVectorRep" Label="for a list (and a field)"/>
and <Ref Func="ConvertToMatrixRep" Label="for a list (and a field)"/>.
</Item>
<Item>
Use <Ref Func="ImmutableMatrix"/> and the binary versions of
<Ref Func="ConvertToVectorRep" Label="for a list (and a field)"/>
and <Ref Func="ConvertToMatrixRep" Label="for a list (and a field)"/>
only when creating initial objects
which need not be compatible with given vectors or matrices.
</Item>
</List>

Conversely, do <E>not</E> use functions for vector and matrix objects
when you want to create an object that shall be used just as a list.
For example, use <C>ListWithIdenticalEntries( n, Zero( R ) )</C>
not <C>Vector( R, n )</C> in this case.
For creating a list with <C>n</C> entries, you can also first call
<Ref Func="EmptyPlist"/> for creating a big enough list,
and then enter the values.

</Section>

</Chapter>
Loading