23 Matrices

Matrices are represented in GAP by lists of row vectors (see Row Vectors). The vectors must all have the same length, and their elements must lie in a common ring.

Because matrices are just a special case of lists, all operations and functions for lists are applicable to matrices also (see chapter Lists). This especially includes accessing elements of a matrix (see List Elements), changing elements of a matrix (see List Assignment), and comparing matrices (see Comparisons of Lists).

  • IsMatrix( obj ) C

    A matrix is a list of lists of equal length whose entries lie in a common ring.

    Note that matrices may have different multiplications, besides the usual matrix product there is for example the Lie product. So there are categories such as IsOrdinaryMatrix and IsLieMatrix (see IsOrdinaryMatrix, IsLieMatrix) that describe the matrix multiplication. One can form the product of two matrices only if they support the same multiplication.

    gap> mat:=[[1,2,3],[4,5,6],[7,8,9]];
    [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ]
    gap> IsMatrix(mat);
    true
    

    Note also the filter IsTable (see section IsTable) which may be more appropriate than IsMatrix for some purposes.

    Note that the empty list '[ ]' and more complex ``empty'' structures such as [[ ]] are not matrices, although special methods allow them be used in place of matrices in some situations. See EmptyMatrix below.

    gap> [[0]]*[[]];
    [ [  ] ]
    gap> IsMatrix([[]]);
    false
    

  • IsOrdinaryMatrix( obj ) C

    An ordinary matrix is a matrix whose multiplication is the ordinary matrix multiplication.

    Each matrix in internal representation is in the category IsOrdinaryMatrix, and arithmetic operations with objects in IsOrdinaryMatrix produce again matrices in IsOrdinaryMatrix.

    Note that we want that Lie matrices shall be matrices that behave in the same way as ordinary matrices, except that they have a different multiplication. So we must distinguish the different matrix multiplications, in order to be able to describe the applicability of multiplication, and also in order to form a matrix of the appropriate type as the sum, difference etc. of two matrices which have the same multiplication.

  • IsLieMatrix( mat ) C

    A Lie matrix is a matrix whose multiplication is given by the Lie bracket. (Note that a matrix with ordinary matrix multiplication is in the category IsOrdinaryMatrix, see IsOrdinaryMatrix.)

    Each matrix created by LieObject is in the category IsLieMatrix, and arithmetic operations with objects in IsLieMatrix produce again matrices in IsLieMatrix.

  • InfoMatrix V

    The info class for matrix operations is InfoMatrix.

    Sections

    1. Operators for Matrices
    2. Properties and Attibutes of Matrices
    3. Matrix Constructions
    4. Random Matrices
    5. Matrices Representing Linear Equations and the Gaussian Algorithm
    6. Elementary Divisors
    7. Echelonized Matrices
    8. Matrices as Basis of a Row Space
    9. Triangular Matrices
    10. Matrices as Linear Mappings
    11. Matrices over Finite Fields
    12. Block Matrices

    23.1 Operators for Matrices

  • mat + scalar O
  • scalar + mat O

    returns the sum of the matrix mat and the scalar scalar. The elements of mat and scalar must lie in a common field. The sum is a new matrix where each entry is the sum of the corresponding entry of mat and scalar.

  • mat1 + mat2 O

    returns the sum of the two matrices mat1 and mat2, which must have the same dimensions and whose elements must lie in a common field. The sum is a new matrix where each entry is the sum of the corresponding entries of mat1 and mat2.

  • mat - scalar O
  • scalar - mat O
  • mat1 - mat2

    The definition for the - operator are similar to the above definitions for the + operator, except that - subtracts of course.

  • mat * scalar O
  • scalar * mat O

    returns the product of the matrix mat and the scalar scalar. The elements of mat and scalar must lie in a common field. The product is a new matrix where each entry is the product of the corresponding entries of mat and scalar.

  • vec * mat O

    returns the product of the vector vec and the matrix mat. (This is the standard product of a row vector with a matrix.) The length of vec and the number of rows of mat must be equal. The elements of vec and mat must lie in a common field. If vec is a vector of length n and mat is a matrix with n rows and m columns, the product is a new vector of length m. The element at position i is the sum of vec[l] * mat[l][i] with l running from 1 to n.

  • mat * vec O

    returns the product of the matrix mat and the vector vec. (This is the standard product of a matrix with a column vector.) The number of columns of mat and the length of vec must be equal. The elements of mat and vec must lie in a common field. If mat is a matrix with m rows and n columns and vec is a vector of length n, the product is a new vector of length m. The element at position i is the sum of mat[i][l] * vec[l] with l running from 1 to n.

  • mat1 * mat2 O

    This form evaluates to the product of the two matrices mat1 and mat2. The number of columns of mat1 and the number of rows of mat2 must be equal. The elements of mat1 and mat2 must lie in a common field. If mat1 is a matrix with m rows and n columns and mat2 is a matrix with n rows and o columns, the result is a new matrix with m rows and o columns. The element in row i at position k of the product is the sum of mat1[i][l] * mat2[l][k] with l running from 1 to n.

  • Inverse(mat) O

    returns the inverse matrix of mat, which must be an invertible square matrix. If the matrix is not invertible, fail is returned.

  • mat1 / mat2 O
  • scalar / mat O
  • mat / scalar O
  • vec / mat O

    In general left / right is defined as left * right^-1. Thus in the above forms the right operand must always be invertable.

  • mat ^ int O

    returns the int-th power of the matrix mat. mat must be a square matrix, int must be an integer. If int is negative, mat must be invertible. If int is 0, the result is the identity matrix One(mat), even if mat is not invertible.

  • mat1 ^ mat2 O

    returns the conjugate of the matrix mat1 by the matrix mat2, i.e. mat2^-1 * mat1 * mat2. mat2 must be invertible and mat1 must be a square matrix of the same dimension.

  • vec ^mat O

    This is in every respect equivalent to vec * mat. This operations reflects the fact that matrices operate naturally on the vector space by multiplication from the right.

  • scalar + matlist O
  • matlist + scalar O
  • scalar - matlist O
  • matlist - scalar O
  • scalar * matlist O
  • matlist * scalar O
  • matlist / scalar O

    A scalar scalar may also be added, subtracted, multiplied with, or divide into a whole list of matrices matlist. The result is a new list of matrices where each matrix is the result of performing the operation with the corresponding matrix in matlist.

  • mat * matlist O
  • matlist * mat O

    A matrix mat may also be multiplied with a whole list of matrices matlist. The result is a new list of matrices, where each matrix is the product of mat and the corresponding matrix in matlist.

  • matlist / mat O

    This form evaluates to matlist * mat^-1. mat must of course be invertable.

  • vec * matlist O

    returns the product of the vector vec and the list of matrices mat. The lengths l of vec and matlist must be equal. All matrices in matlist must have the same dimensions. The elements of vec and the elements of the matrices in matlist must lie in a common ring. The product is the sum over vec[i] * matlist[i] with i running from 1 to l.

  • Comm( mat1, mat2 ) O

    Comm returns the commutator of the matrices mat1 and mat2, i.e., mat1^-1 * mat2^-1 * mat1 * mat2. mat1 and mat2 must be invertable and such that these product can be computed.

    There is one exception to the rule that the operands or their elements must lie in common field. It is allowed that one operand is a finite field element, a finite field vector, a finite field matrix, or a list of finite field matrices, and the other operand is an integer, an integer vector, an integer matrix, or a list of integer matrices. In this case the integers are interpreted as int * One(GF), where GF is the finite field (see Operations for Finite Field Elements).

    For all the above operations the result is a new mutable list, unless both operands are immutable, in which case the result is immutable.

    23.2 Properties and Attibutes of Matrices

  • DimensionsMat( mat ) A

    is a list of length 2, the first being the number of rows, the second being the number of columns of the matrix mat.

    gap> DimensionsMat([[1,2,3],[4,5,6]]);
    [ 2, 3 ]
    
  • DefaultFieldOfMatrix( mat ) A

    is a field (not necessarily the smallest one) containing all entries of the matrix mat.

    gap> DefaultFieldOfMatrix([[Z(4),Z(8)]]); 
    GF(2^6)
    

  • TraceMat( mat ) F
  • Trace( mat ) F

    The trace of a square matrix is the sum of its diagonal entries.

    gap> TraceMat([[1,2,3],[4,5,6],[7,8,9]]);
    15
    
  • DeterminantMat( mat ) A
  • Determinant( mat ) F

    returns the determinant of the square matrix mat

    gap> DeterminantMat([[1,2],[2,1]]);
    -3
    

  • IsMonomialMatrix( mat ) P

    A matrix is monomial if and only if it has exactly one nonzero entry in every row and every column.

    gap> IsMonomialMatrix([[0,1],[1,0]]);
    true
    

  • IsDiagonalMat( mat ) O

    returns true if mat has only zero entries off the main diagonal, false otherwise.

  • IsUpperTriangularMat( mat ) O

    returns true if mat has only zero entries below the main diagonal, false otherwise.

  • IsLowerTriangularMat( mat ) O

    returns true if mat has only zero entries below the main diagonal, false otherwise.

    23.3 Matrix Constructions

  • IdentityMat( m[, F] ) F

    returns a (mutable) m ×m identity matrix over the field given by F (i.e. the smallest field containing the element F or F itself if it is a field).

  • NullMat( m, n[, F] ) F

    returns a (mutable) m ×n null matrix over the field given by F.

    gap> IdentityMat(3,1);
    [ [ 1, 0, 0 ], [ 0, 1, 0 ], [ 0, 0, 1 ] ]
    gap> NullMat(3,2,Z(3));
    [ [ 0*Z(3), 0*Z(3) ], [ 0*Z(3), 0*Z(3) ], [ 0*Z(3), 0*Z(3) ] ]
    
  • EmptyMatrix( char ) F

    is an empty (ordinary) matrix in characteristic char that can be added to or multiplied with empty lists (representing zero-dimensional row vectors). It also acts (via ^) on empty lists.

    gap> EmptyMatrix(5);
    EmptyMatrix( 5 )
    gap> AsList(last);
    [  ]
    

  • DiagonalMat( vector ) F

    returns a diagonal matrix mat with the diagonal entries given by vector.

    gap> DiagonalMat([1,2,3]); 
    [ [ 1, 0, 0 ], [ 0, 2, 0 ], [ 0, 0, 3 ] ]
    
  • PermutationMat( perm, dim[, F] ) F

    returns a matrix in dimension dim over the field given by F (i.e. the smallest field containing the element F or F itself if it is a field) that represents the permutation perm acting by permuting the basis vectors as it permutes points.

    gap> PermutationMat((1,2,3),4,1);
    [ [ 0, 1, 0, 0 ], [ 0, 0, 1, 0 ], [ 1, 0, 0, 0 ], [ 0, 0, 0, 1 ] ]
    

  • TransposedMat( mat ) A

    TransposedMat returns the transposed of the matrix mat, i.e., a new matrix trans such that trans[i][k] = mat[k][i]. As it is an attribute it returns an immutable matrix.

  • MutableTransposedMat( mat ) F

    MutableTransposedMat returns the transposed of the matrix mat as a mutable matrix, i.e., a new matrix trans such that trans[i][k] = mat[k][i].

    gap> TransposedMat([[1,2,3],[4,5,6],[7,8,9]]);
    [ [ 1, 4, 7 ], [ 2, 5, 8 ], [ 3, 6, 9 ] ]
    

  • KroneckerProduct( mat1, mat2 ) O

    The Kronecker product of two matrices is the matrix obtained when replacing each entry a of mat1 by the product a*mat2 in one matrix.

    gap> KroneckerProduct([[1,2]],[[5,7],[9,2]]);
    [ [ 5, 7, 10, 14 ], [ 9, 2, 18, 4 ] ]
    

  • ReflectionMat( coeffs ) F
  • ReflectionMat( coeffs, root ) F
  • ReflectionMat( coeffs, conj ) F
  • ReflectionMat( coeffs, conj, root ) F

    Let coeffs be a row vector. ReflectionMat returns the matrix of the reflection in this vector.

    More precisely, if coeffs is the coefficients of a vector v w.r.t. a basis B (see nowhere), say, then the returned matrix describes the reflection in v w.r.t. B as a map on a row space, with action from the right.

    The optional argument root is a root of unity that determines the order of the reflection. The default is a reflection of order 2. For triflections one should choose a third root of unity etc. (see nowhere).

    conj is a function of one argument that conjugates a ring element. The default is ComplexConjugate.

    The matrix of the reflection in v is defined as

    M = In +
    vtr
     
    ·w-1
    v
    vtr
     
    ·v
    where w = root, n is the length of the coefficient list, and [`] denotes the conjugation.

  • PrintArray( array ) F

    pretty-prints the array array.

  • MutableIdentityMat( m[, F] ) F

    returns a (mutable) m ×m identity matrix over the field given by F. This is identical to IdentityMat and is present in GAP 4.1 only for the sake of compatibility with beta-releases. It should not be used in new code.

  • MutableNullMat( m, n[, F] ) F

    returns a (mutable) m ×n null matrix over the field given by F. This is identical to NullMat and is present in GAP 4.1 only for the sake of compatibility with beta-releases. It should not be used in new code.

    23.4 Random Matrices

  • RandomMat( m, n[, R] ) F

    RandomMat returns a new mutable random matrix with m rows and n columns with elements taken from the ring R, which defaults to Integers.

  • RandomInvertibleMat( m[, R] ) F

    RandomInvertibleMat returns a new mutable invertible random matrix with m rows and columns with elements taken from the ring R, which defaults to Integers.

  • RandomUnimodularMat( m ) F

    returns a new random mutable m ×m matrix with integer entries that is invertible over the integers.

    gap> RandomMat(2,3,GF(3));
    [ [ Z(3)^0, Z(3), Z(3)^0 ], [ Z(3), Z(3)^0, Z(3)^0 ] ]
    gap> RandomInvertibleMat(4);
    [ [ -1, 0, 1, -1 ], [ 2, 1, 3, 0 ], [ 1, 4, 0, 2 ], [ -3, 2, 1, 0 ] ]
    

    23.5 Matrices Representing Linear Equations and the Gaussian Algorithm

  • RankMat( mat ) A

    If mat is a matrix whose rows span a free module over the ring generated by the matrix entries and their inverses then RankMat returns the dimension of this free module. Otherwise fail is returned.

    Note that RankMat may perform a Gaussian elimination. For large rational matrices this may take very long, because the entries may become very large.

    gap> mat:=[[1,2,3],[4,5,6],[7,8,9]];;
    gap> RankMat(mat); 
    2
    
  • TriangulizeMat( mat ) O

    applies the Gaussian Algorithm to the mutable matrix mat and changes mat such that it is in upper triangular normal form (sometimes called ``Hermite normal form'').

    gap> m:=MutableTransposedMat(mat);
    [ [ 1, 4, 7 ], [ 2, 5, 8 ], [ 3, 6, 9 ] ]
    gap> TriangulizeMat(m);m;
    [ [ 1, 0, -1 ], [ 0, 1, 2 ], [ 0, 0, 0 ] ]
    
  • NullspaceMat( mat ) A
  • TriangulizedNullspaceMat( mat ) A

    returns a list of row vectors that form a basis of the vector space of solutions to the equation vec*mat=0. The result is an immutable matrix. This basis is not guaranteed to be in any specific form.

    The variant TriangulizedNullspaceMat returns a basis of the nullspace in triangulized form as is often needed for algorithms.

    gap> mat:=[[1,2,3],[4,5,6],[7,8,9]];;
    gap> NullspaceMat(mat);
    [ [ 1, -2, 1 ] ]
    
  • SolutionMat( mat, vec ) O

    returns a rwo vector x that is a solution of the equation x * mat = vec. It returns fail if no such vector exists.

    gap> mat:=[[1,2,3],[4,5,6],[7,8,9]];;
    gap> SolutionMat(mat,[3,5,7]); 
    [ 5/3, 1/3, 0 ]
    
  • BaseFixedSpace( mats ) F

    BaseFixedSpace returns a list of row vectors that form a base of the vector space V such that M v = v for all v in V and all matrices M in the list mats. (This is the common eigenspace of all matrices in mats for the eigenvalue 1.)

    gap> BaseFixedSpace([[[1,2],[0,1]]]);  
    [ [ 0, 1 ] ]
    

    23.6 Elementary Divisors

  • ElementaryDivisorsMat( [ring, ]mat ) O

    ElementaryDivisors returns a list of the elementary divisors, i.e., the unique d with d[i] divides d[i+1] and mat is equivalent to a diagonal matrix with the elements d[i] on the diagonal. The operations are performed over the ring ring, which must contain all matrix entries. For compatibility reasons it can be omitted and defaults to Integers.

    gap> mat:=[[1,2,3],[4,5,6],[7,8,9]];;
    gap> ElementaryDivisorsMat(mat);
    [ 1, 3, 0 ]
    
  • DiagonalizeMat( ring, mat ) O

    brings the mutable matrix mat, considered as a matrix over ring, into diagonal form by elementary row and column operations.

    gap> m:=[[1,2],[2,1]];;
    gap> DiagonalizeMat(Integers,m);m;
    [ [ 1, 0 ], [ 0, 3 ] ]
    

    See also chapter Integral Matrices and Lattices

    23.7 Echelonized Matrices

  • SemiEchelonMat( mat ) A

    A matrix over a field F is in semi-echelon form if the first nonzero element in each row is the identity of F, and all values exactly below these pivots are the zero of F.

    SemiEchelonMat returns a record that contains information about a semi-echelonized form of the matrix mat.

    The components of this record are

    vectors
    list of row vectors, each with pivot element the identity of F,

    heads
    list that contains at position i, if nonzero, the number of the row for that the pivot element is in column i.

  • SemiEchelonMatTransformation( mat ) A

    does the same as SemiEchelonMat but additionally stores the linear transformation T performed on the matrix. The additional components of the result are

    coeffs
    a list of coefficients vectors of the vectors component, with respect to the rows of mat, that is, coeffs * mat is the vectors component.

    relations
    a list of basis vectors for the (left) null space of mat.

    gap> SemiEchelonMatTransformation([[1,2,3],[0,0,1]]);
    rec(
      heads := [ 1, 0, 2 ],
      vectors := [ [ 1, 2, 3 ], [ 0, 0, 1 ] ],
      coeffs := [ [ 1, 0 ], [ 0, 1 ] ],
      relations := [  ] )
    
  • SemiEchelonMats( mats ) A

    A list of matrices over a field F is in semi-echelon form if the list of row vectors obtained on concatenating the rows of each matrix is a semi-echelonized matrix (see SemiEchelonMat).

    SemiEchelonMats returns a record that contains information about a semi-echelonized form of the list mats of matrices.

    The components of this record are

    vectors
    list of matrices, each with pivot element the identity of F,

    heads
    matrix that contains at position [i,j], if nonzero, the number of the matrix for that the pivot element is in this position

    23.8 Matrices as Basis of a Row Space

  • BaseMat( mat ) A

    returns a basis for the row space generated by the rows of mat in the form of an immutable matrix.

    gap> BaseMat(mat);
    [ [ 1, 0, -1 ], [ 0, 1, 2 ] ]
    
  • BaseOrthogonalSpaceMat( mat ) A

    Let V be the row space generated by the rows of mat (over any field that contains all entries of mat). BaseOrthogonalSpaceMat( mat ) computes a base of the orthogonal space of V.

    The rows of mat need not be linearly independent.

  • SumIntersectionMat( M1, M2 ) O

    performs Zassenhaus' algorithm to compute bases for the sum and the intersection of spaces generated by the rows of the matrices M1, M2.

    returns a list of length 2, at first position a base of the sum, at second position a base of the intersection. Both bases are in semi-echelon form (see Echelonized matrices).

    gap> SumIntersectionMat(mat,[[2,7,6],[5,9,4]]);
    [ [ [ 1, 2, 3 ], [ 0, 1, 2 ], [ 0, 0, 1 ] ], [ [ 1, -3/4, -5/2 ] ] ]
    

  • BaseSteinitzVectors( bas, mat ) F

    find vectors extending mat to a basis spanning the span of bas. Both bas and mat must be matrices of full (row) rank. It returns a record with the following components:

    subspace
    is a basis of the space spanned by mat in upper triangular form with leading ones at all echelon steps and zeroes above these ones.

    factorspace
    is a list of extending vectors in upper triangular form.

    factorzero
    is a zero vector.

    heads
    is a list of integers which can be used to decompose vectors in the basis vectors. The ith entry indicating the vector that gives an echelon step at position i. A negative number indicates an echelon step in the subspace, a positive number an echelon step in the complement, the absolute value gives the position of the vector in the lists subspace and factorspace.

    gap> BaseSteinitzVectors(IdentityMat(3,1),[[11,13,15]]);
    rec(
      factorspace := [ [ 0, 1, 15/13 ], [ 0, 0, 1 ] ],
      factorzero := [ 0, 0, 0 ],
      subspace := [ [ 1, 13/11, 15/11 ] ],
      heads := [ -1, 1, 2 ] )
    

    See also chapter Integral Matrices and Lattices

    23.9 Triangular Matrices

  • DiagonalOfMat( mat ) O

    returns the diagonal of mat as a list.

    gap> DiagonalOfMat([[1,2],[3,4]]);
    [ 1, 4 ]
    
  • UpperSubdiagonal( mat, pos ) O

    returns a mutable list containing the entries of the posth upper subdiagonal of mat.

    gap> UpperSubdiagonal(mat,1);
    [ 2, 6 ]
    
  • DepthOfUpperTriangularMatrix( mat ) A

    If mat is an upper triangular matrix this attribute returns the index of the first nonzero diagonal.

    gap> DepthOfUpperTriangularMatrix([[0,1,2],[0,0,1],[0,0,0]]);
    1
    gap> DepthOfUpperTriangularMatrix([[0,0,2],[0,0,0],[0,0,0]]);  
    2
    

    23.10 Matrices as Linear Mappings

  • JordanDecomposition( mat ) A

    JordanDecomposition( mat ) returns a list [S,N] such that S is a semisimple matrix and N is nilpotent. Furthermore, S and N commute and mat=S+N.

    gap> mat:=[[1,2,3],[4,5,6],[7,8,9]];;
    gap> JordanDecomposition(mat);
    [ [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ], 
      [ [ 0, 0, 0 ], [ 0, 0, 0 ], [ 0, 0, 0 ] ] ]
    

  • BlownUpMat( B, mat ) F

    Let B be a basis of a field extension F / K, and mat a matrix whose entries are all in F. (This is not checked.) BlownUpMat returns a matrix over K that is obtained by replacing each entry of mat by its regular representation w.r.t. B.

    More precisely, regard mat as the matrix of a linear transformation on the row space Fn w.r.t. the F-basis with vectors (v1, ldots, vn), say, and suppose that the basis B consists of the vectors (b1, ¼, bm); then the returned matrix is the matrix of the linear transformation on the row space Kmn w.r.t. the K-basis whose vectors are (b1 v1, ¼bm v1, ¼, bm vn).

    Note that the linear transformations act on row vectors, i.e., each row of the matrix is a concatenation of vectors of B-coefficients.

  • BlownUpVector( B, vector ) F

    Let B be a basis of a field extension F / K, and vector a row vector whose entries are all in F. BlownUpVector returns a row vector over K that is obtained by replacing each entry of vector by its coefficients w.r.t. B.

    So BlownUpVector and BlownUpMat (see BlownUpMat) are compatible in the sense that for a matrix mat over F, BlownUpVector( B, mat * vector ) is equal to BlownUpMat( B, mat ) * BlownUpVector( B, vector ).

    gap> B:= Basis( CF(4), [ 1, E(4) ] );;
    gap> mat:= [ [ 1, E(4) ], [ 0, 1 ] ];;  vec:= [ 1, E(4) ];;
    gap> bmat:= BlownUpMat( B, mat );;  bvec:= BlownUpVector( B, vec );;
    gap> Display( bmat );  bvec;
    [ [   1,   0,   0,   1 ],
      [   0,   1,  -1,   0 ],
      [   0,   0,   1,   0 ],
      [   0,   0,   0,   1 ] ]
    [ 1, 0, 0, 1 ]
    gap> bvec * bmat = BlownUpVector( B, vec * mat );
    true
    

  • CompanionMat( poly ) F

    computes a companion matrix of the polynomial poly. This matrix has poly as its minimal polynomial.

    23.11 Matrices over Finite Fields

    Just as for row vectors, (see section Row Vectors over Finite Fields), GAP has a special representation for matrices over small finite fields.

    To be eligible to be represented in this way, each row of a matrix must be immutable, and able to be represented as a compact row vector over the same finite field.

    gap> v := Z(2)*[1,0,0,1,1];
    [ Z(2)^0, 0*Z(2), 0*Z(2), Z(2)^0, Z(2)^0 ]
    gap> ConvertToVectorRep(v,2);
    2
    gap> v;
    <a GF2 vector of length 5>
    gap> MakeImmutable(v);
    gap> m := [v];
    <a 1x5 matrix over GF2>
    gap> m := [v,v];
    <a 2x5 matrix over GF2>
    gap> m := [v,v,v];
    <a 3x5 matrix over GF2>
    gap> v := Z(3)*[1..8];       
    [ Z(3), Z(3)^0, 0*Z(3), Z(3), Z(3)^0, 0*Z(3), Z(3), Z(3)^0 ]
    gap> ConvertToVectorRep(v);
    3
    gap> MakeImmutable(v);          
    gap> m := [v];
    [ [ Z(3), Z(3)^0, 0*Z(3), Z(3), Z(3)^0, 0*Z(3), Z(3), Z(3)^0 ] ]
    gap> RepresentationsOfObject(m);
    [ "IsPositionalObjectRep", "Is8BitMatrixRep" ]
    gap> m := [v,v,v,v];            
    < mutable compressed matrix 4x8 over GF(3) >
    

    All compressed matrices over GF(2) are viewed as <a nxm matrix over GF2>, while over fields GF(q) for q between 3 and 256, matrices with 25 or more entries are views in this way, and smaller ones as lists of lists.

    Under some circumstances, as we saw above, GAP can detect that a matrix is eligible to be represented in this way, and automatically does so. For other cases there is a function:

  • ConvertToMatrixRep( list ) F
  • ConvertToMatrixRep( list, field ) F
  • ConvertToMatrixRep( list, fieldsize ) F

    ConvertToMatrixRep( list ) converts list to an internal matrix representation if possible. ConvertToMatrixRep( list , field ) converts list to an internal matrix representation appropriate for a matrix over field. It is forbidden to call this function unless all elements of list are vectors with entries in field.

    Instead of a field also its size fieldsize may be given.

    list may already be a compressed matrix. In this case, if no field or fieldsize is given, then nothing happens.

    list itself may be mutable, but its entries must be immutable.

    The return value is the size of the field over which the matrix ends up written, if it is written in a compressed representation. Otherwise it is fail.

    Note that the main advantage of this special representation of matrices is in low dimensions, where various overheads can be reduced. In higher dimensions, a list of compressed vectors will be almost as fast. Note also that list access and assignment will be somewhat slower for compressed matrices than for plain lists.

    Finally note that a vector cannot be shared between two compressed matrices over different fields. To avoid risk of this, when a vector becomes part of a compressed matrix, the filter IsLockedRepresentationVector is set for it, and neither ConvertToVectorRep nor ConvertToMatrixRep will ever alter a vector with this filter.

    gap> v := [Z(2),Z(2)];
    <a GF2 vector of length 2>
    gap> m := [v,v]; # v is mutable, so m cannot be compressed
    [ <a GF2 vector of length 2>, <a GF2 vector of length 2> ]
    gap> ConvertToMatrixRep(m); # even if we ask for it
    fail
    gap> m;
    [ <a GF2 vector of length 2>, <a GF2 vector of length 2> ]
    gap> mi := List(m, Immutable); # but if we make the rows immutable
    <a 2x2 matrix over GF2>
    gap> m2 := [mi[1], [Z(4),Z(4)]]; # now try and mix in some GF(4)
    [ <an immutable GF2 vector of length 2>, [ Z(2^2), Z(2^2) ] ]
    gap> ConvertToMatrixRep(m2); # bur m2[1] is locked
    fail
    gap> m2 := [ShallowCopy(mi[1]), [Z(4),Z(4)]]; # a fresh copy f row 1
    [ <a GF2 vector of length 2>, [ Z(2^2), Z(2^2) ] ]
    gap> MakeImmutable(m2[1]);
    gap> MakeImmutable(m2[2]);  
    gap> ConvertToMatrixRep(m2); # now it works
    4
    gap> m2;
    [ [ Z(2)^0, Z(2)^0 ], [ Z(2^2), Z(2^2) ] ]
    gap> RepresentationsOfObject(m2);
    [ "IsPositionalObjectRep", "Is8BitMatrixRep" ]
    gap> 
    

    There are also two operations that are only available for matrices written over finite fields.

  • ProjectiveOrder( mat ) A

    Returns an integer n and a finite field element e such that A^n = eI. mat must be a matrix defined over a finite field.

    gap> ProjectiveOrder([[1,4],[5,2]]*Z(11)^0);
    [ 5, Z(11)^5 ]
    

  • SimultaneousEigenvalues( matlist, expo ) F

    The matrices in matlist must be matrices over GF(q) for some prime q. Together, they must generate an abelian p-group of exponent expo. Then the eigenvalues of mat in the splitting field GF(q^r) for some r are powers of an element x in the splitting field, which is of order expo. SimultaneousEigenvalues returns a matrix of integers mod expo, say (ai,j), such that the power xai,j is an eigenvalue of the i-th matrix in matlist and the eigenspaces of the different matrices to the eigenvalues xai,j for fixed j are equal.

    Finally, there are two operations that deal with matrices over a ring, but only care about the residues of their entries modulo some ring element. In the case of the integers and a prime number, this is effectively computation in a matrix over a finite field.

  • InverseMatMod( mat, obj ) O

    For a square matrix mat, InverseMatMod returns a matrix inv such that inv * mat is congruent to the identity matrix modulo obj, if such a matrix exists, and fail otherwise.

    gap> mat:= [ [ 1, 2 ], [ 3, 4 ] ];;  inv:= InverseMatMod( mat, 5 );
    [ [ 3, 1 ], [ 4, 2 ] ]
    gap> mat * inv;
    [ [ 11, 5 ], [ 25, 11 ] ]
    

  • NullspaceModQ( E, q ) F

    E must be a matrix of integers and q a prime power. Then NullspaceModQ returns the set of all vectors of integers modulo q, which solve the homogeneous equation system given by E modulo q.

    gap> mat:= [ [ 1, 3 ], [ 1, 2 ], [ 1, 1 ] ];;  NullspaceModQ( mat, 5 );
    [ [ 0, 0, 0 ], [ 1, 3, 1 ], [ 2, 1, 2 ], [ 4, 2, 4 ], [ 3, 4, 3 ] ]
    

    23.12 Block Matrices

    Block matrices are a special representation of matrices which can save a lot of memory if large matrices have a block structure with lots of zero blocks. GAP uses the representation IsBlockMatrixRep to store block matrices.

  • AsBlockMatrix( m, nrb, ncb ) F

    returns a block matrix with nrb row blocks and ncb column blocks which is equal to the ordinary matrix m.

  • BlockMatrix( blocks, nrb, ncb ) F
  • BlockMatrix( blocks, nrb, ncb, rpb, cpb, zero ) F

    BlockMatrix returns an immutable matrix in the sparse representation IsBlockMatrixRep. The nonzero blocks are described by the list blocks of triples, the matrix has nrb row blocks and ncb column blocks.

    If blocks is empty (i.e., if the matrix is a zero matrix) then the dimensions of the blocks must be entered as rpb and cpb, and the zero element as zero.

    Note that all blocks must be ordinary matrices (see IsOrdinaryMatrix), and also the block matrix is an ordinary matrix.

  • MatrixByBlockMatrix( blockmat ) F

    returns an ordinary matrix that is equal to the block matrix blockmat.

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

    GAP 4 manual
    July 1999