3.8 Vectors and Matrices

This section describes how GAP uses lists to represent row vectors and matrices. A row vector is a dense list of elements from a common field. A matrix is a dense list of row vectors over a common field and of equal length.

gap> v:= [3, 6, 2, 5/2];;  IsRowVector(v);
true
Row vectors may be added and multiplied by scalars from their field. Multiplication of row vectors of equal length results in their scalar product.
gap> 2 * v;  v * 1/3;  v * v;
[ 6, 12, 4, 5 ]
[ 1, 2, 2/3, 5/6 ]
# the scalar product of `v' with itself
221/4
Note that the expression v * 1/3 is actually evaluated by first multiplying v by 1 (which yields again v) and by then dividing by 3. This is also an allowed scalar operation. The expression v/3 would result in the same value.

Such arithmetical operations (if the results are again vectors) result in mutable vectors except if the operation is binary and both operands are immutable; thus the vectors shown in the examples above are all mutable.

So if you want to produce a mutable list with 100 entries equal to 25, you can simply say 25 + 0 * [ 1 .. 100 ]. Note that ranges are also vectors (over the rationals), and that [ 1 .. 100 ] is mutable.

A matrix is a dense list of row vectors of equal length.

gap> m:= [[1,-1, 1],
>         [2, 0,-1],
>         [1, 1, 1]];
[ [ 1, -1, 1 ], [ 2, 0, -1 ], [ 1, 1, 1 ] ]
gap> m[2][1];
2
Syntactically a matrix is a list of lists. So the number 2 in the second row and the first column of the matrix m is referred to as the first element of the second element of the list m via m[2][1].

A matrix may be multiplied by scalars, row vectors and other matrices. The row vectors and matrices involved in such a multiplication must however have suitable dimensions.

gap> [1, 0, 0, 0] * m;
Vector *: <right> must have the same length as <left> (4)
gap> [1, 0, 0] * m;
[ 1, -1, 1 ]
gap> m * [1, 0, 0, 0];
Vector *: <right> must have the same length as <left> (3)
gap> m * [1, 0, 0];
[ 1, 2, 1 ]
Note that multiplication of a row vector with a matrix will result in a linear combination of the rows of the matrix, while multiplication of a matrix with a row vector results in a linear combination of the columns of the matrix. In the latter case the row vector is considered as a column vector.

A vector or matrix of integers can also be multiplied with a finite field scalar and vice versa. Such products result in a matrix over the finite field with the integers mapped into the finite field in the obvious way. Finite field matrices are nicer to read when they are Displayed rather than Printed. (Here we write Z(q) to denote a primitive root of the finite field with q elements.)

gap> Display( m * One( GF(5) ) );
 1 4 1
 2 . 4
 1 1 1
gap> Display( m^2 * Z(2) + m * Z(4) );
z = Z(4)
 z^1 z^1 z^2
   1   1 z^2
 z^1 z^1 z^2
Submatrices can easily be extracted using the expression mat{rows}{columns}. They can also be assigned to, provided the big matrix is mutable (which it is not if it is the result of an arithmetical operation, see above).
gap> sm := m{ [ 1, 2 ] }{ [ 2, 3 ] };
[ [ -1, 1 ], [ 0, -1 ] ]
gap> sm{ [ 1, 2 ] }{ [2] } := [[-2],[0]];;  sm;
[ [ -1, -2 ], [ 0, 0 ] ]
The first curly brackets contain the selection of rows, the second that of columns.

Matrices appear not only in linear algebra, but also as group elements, provided they are invertible. Here we have the opportunity to meet a group-theoretical function, namely Order, which computes the order of a group element.

gap> Order( m * One( GF(5) ) );
8
gap> Order( m );
infinity
For matrices whose entries are more complex objects, for example rational functions, GAP's Order methods might not be able to prove that the matrix has infinite order, and one gets the following warning.
|#I  Order: warning, order of <mat> might be infinite
In such a case, if the order of the matrix really is infinite, you will have to interrupt GAP by pressing ctl-C (followed by ctl-D or quit; to leave the break loop).

To prove that the order of m is infinite, we also could look at the minimal polynomial of m over the rationals.

gap> f:= MinimalPolynomial( Rationals, m );;  Factors( f );
[ -2+x_1, 3+x_1^2 ]
Factors returns a list of irreducible factors of the polynomial f. The first irreducible factor X-2 reveals that 2 is an eigenvalue of m, hence its order cannot be finite.

[Top] [Previous] [Up] [Next] [Index]

GAP 4 manual
February 2000