GAP (cvec) - Chapter 5: Matrices

Goto Chapter: Top 1 2 3 4 5 6 7 8 9 Bib Ind
[Top of Book] [Contents] [Previous Chapter] [Next Chapter]

[MathJax on]

5 Matrices
5.1 Creation
5.1-1 CMat
5.1-2 CMat
5.1-3 CMat
5.1-4 CMat
5.1-5 CVEC_ZeroMat
5.1-6 CVEC_IdentityMat
5.1-7 CVEC_RandomMat
5.1-8 MutableCopyMat
5.1-9 Matrix
5.2 Matrices as lists
5.2-1 Add
5.2-2 Remove
5.2-3 ELM_LIST
5.2-4 ASS_LIST
5.2-5 ELMS_LIST
5.2-6 ASSS_LIST
5.2-7 Length
5.2-8 ShallowCopy
5.2-9 Collected
5.2-10 DuplicateFreeList
5.2-11 Append
5.2-12 Filtered
5.2-13 Unbind
5.3 Arithmetic
`5.3-1 \+`
`5.3-2 \-`
5.3-3 AdditiveInverseSameMutability
5.3-4 AdditiveInverseMutable
`5.3-5 \*`
`5.3-6 \*`
`5.3-7 \^`
`5.3-8 \*`
5.3-9 ZeroSameMutability
5.3-10 ZeroMutable
5.3-11 OneSameMutability
5.3-12 OneMutable
5.3-13 InverseMutable
5.3-14 InverseSameMutability
5.3-15 TransposedMat
5.3-16 KroneckerProduct
5.4 Comparison of matrices and other information
5.4-1 =
5.4-2 LT
5.4-3 IsZero
5.4-4 IsOne
5.4-5 IsDiagonalMat
5.4-6 IsUpperTriangularMat
5.4-7 IsLowerTriangularMat
5.4-8 CVEC_HashFunctionForCMats
5.4-9 ChooseHashFunction
5.5 Slicing and submatrices
5.5-1 ExtractSubMatrix
5.5-2 CopySubMatrix
5.6 Information about matrices
5.6-1 BaseField
5.6-2 Characteristic
5.6-3 DegreeFFE
5.6-4 DefaultField
5.7 Input and output
5.7-1 CVEC_WriteMat
5.7-2 CVEC_WriteMatToFile
5.7-3 CVEC_WriteMatsToFile
5.7-4 CVEC_ReadMat
5.7-5 CVEC_ReadMatFromFile
5.7-6 CVEC_ReadMatsFromFile
5.8 Grease
5.8-1 GreaseMat
5.8-2 UnGreaseMat
5.9 Everything else
5.9-1 Randomize
5.9-2 OverviewMat
5.9-3 Unpack

### 5 Matrices

A compressed matrix (a `cmat`) behaves very much like a list of `cvec`s. However, it insists on having only `cvec`s of the same length and over the same base field as its elements, and it insists on being a list without holes. Apart from these restrictions, you can use all the standard list operations with `cmat`s (see Section 5.2.

In the rest of this chapter, we document all methods for matrices for the sake of completeness. If they behave exactly as is to be expected by the already defined operation no further explanation is given.

#### 5.1 Creation

The basic operation to create new `cmat`s is `CMat`, for which a variety of methods is available:

##### 5.1-1 CMat

`‣ CMat`( l, cl, dochecks ) | ( method )

`‣ CMat`( l, cl ) | ( method )

Returns: a new `cmat`

A new `cmat` is created with rows being in the `cvecclass` cl. All elements of the list l must be `cvec`s in that class. The boolean flag dochecks indicates, whether this should be checked or not. If the flag is omitted, checks are performed. Note that l may be the empty list.

##### 5.1-2 CMat

`‣ CMat`( l, dochecks ) | ( method )

`‣ CMat`( l ) | ( method )

Returns: a new `cmat`

A new `cmat` is created with rows being in the `cvecclass` of the vectors in l. All elements of the list l must be `cvec`s in the same class. The boolean flag dochecks indicates, whether this should be checked or not. If the flag is omitted, checks are performed. Note that l may not be the empty list.

##### 5.1-3 CMat

`‣ CMat`( l, v ) | ( method )

Returns: a new `cmat`

A new `cmat` is created with rows being in the `cvecclass` of the `cvec` v. All elements of the list l must be `cvec`s in the that same class. This is checked. Note that l may be the empty list.

##### 5.1-4 CMat

`‣ CMat`( m ) | ( method )

Returns: a new `cmat`

Creates a new `cmat` which is equal to m, which must be a compressed matrix in the filter `IsGF2MatrixRep` or the filter `Is8BitMatrixRep`.

There are some methods to create `cmat`s of special form:

##### 5.1-5 CVEC_ZeroMat

`‣ CVEC_ZeroMat`( rows, cl ) | ( function )

`‣ CVEC_ZeroMat`( rows, cols, p, d ) | ( function )

Returns: a new `cmat`

Creates a zero matrix with rows rows and cols columns over the field `GF(p,d)`. If a `cvecclass` cl is given, the number of columns and the field follow from that.

##### 5.1-6 CVEC_IdentityMat

`‣ CVEC_IdentityMat`( cl ) | ( function )

`‣ CVEC_IdentityMat`( n, p, d ) | ( function )

Returns: a new `cmat`

Creates an identity matrix with n rows and columns over the field `GF(p,d)`. If a `cvecclass` cl is given, the number of columns and the field follow from that.

##### 5.1-7 CVEC_RandomMat

`‣ CVEC_RandomMat`( rows, cl ) | ( function )

`‣ CVEC_RandomMat`( rows, cols, p, d ) | ( function )

Returns: a new `cmat`

Creates a random matrix with rows rows and cols columns over the field `GF(p,d)`. If a `cvecclass` cl is given, the number of columns and the field follow from that. Note that this is not particularly efficient.

##### 5.1-8 MutableCopyMat

`‣ MutableCopyMat`( m ) | ( method )

Returns: a mutable copy of m

Creates a mutable copy of the `cmat` m.

##### 5.1-9 Matrix

`‣ Matrix`( vectorlist, vector ) | ( method )

`‣ MatrixNC`( vectorlist, vector ) | ( method )

Returns: a new mutable `cmat`

Returns a new `cmat` containing the vectors in vectorlist as rows. The elements in vectorlist must be vectors of the same length as the sample vector vector and must live over the same base field. The sample vector is always necessary to be able to use the method selection. The vectorlist may be empty. The NC method does not check the inputs.

#### 5.2 Matrices as lists

In this section, arguments named m and n are `cmat`s and v and w are `cvec`s that fit into the corresponding matrices. pos is an integer between 1 and `Length(m)` if it applies to the matrix m.

##### 5.2-1 Add

`‣ Add`( m, v[, pos] ) | ( method )

Returns: nothing

Behaves exactly as expected. Note that one can only add `cvec`s of the right length and over the right field.

##### 5.2-2 Remove

`‣ Remove`( m[, pos] ) | ( method )

Returns: a `cvec`

Behaves exactly as expected. No holes can be made.

##### 5.2-3 ELM_LIST

`‣ ELM_LIST`( m, pos ) | ( method )

Returns: a `cvec`

Behaves exactly as expected. Note that this method is triggered when one uses the (reading) syntax "`m[pos]`".

##### 5.2-4 ASS_LIST

`‣ ASS_LIST`( m, pos, v ) | ( method )

Returns: nothing

Behaves exactly as expected. Note that one can only assign to positions such that the resulting matrix has no holes. This method is triggered when one uses the (assignment) syntax "`m[pos] :=` ".

##### 5.2-5 ELMS_LIST

`‣ ELMS_LIST`( m, poss ) | ( method )

Returns: a sub `cmat`

Behaves exactly as expected: A new matrix containing a subset of the rows is returned. Note that the row vectors are the same GAP objects as the corresponding rows of m. This operation is triggered by the expression m`{poss}`.

##### 5.2-6 ASSS_LIST

`‣ ASSS_LIST`( m, poss, vals ) | ( method )

Returns: nothing

Behaves exactly as expected. Of course all values in vals must be `cvec`s over the correct field and the `cmat` m must be a dense list afterwards. This operation is triggered by the statement m`{poss} := vals`.

##### 5.2-7 Length

`‣ Length`( m ) | ( method )

Returns: the number of rows of the `cmat` m

Behaves exactly as expected.

##### 5.2-8 ShallowCopy

`‣ ShallowCopy`( m ) | ( method )

Returns: a new matrix containing the same rows than the `cmat` m

Behaves exactly as expected. Note that the rows of the result are the very same GAP objects than the rows of the `cmat` m.

##### 5.2-9 Collected

`‣ Collected`( m ) | ( method )

Returns: the same as the collected list of the rows of m

Behaves exactly as expected. Just uses the standard `Collected` (Reference: Collected) on the list of rows.

##### 5.2-10 DuplicateFreeList

`‣ DuplicateFreeList`( m ) | ( method )

Returns: a new mutable `cmat` containing the rows of m with duplicates removed

Behaves exactly as expected. Just uses the standard `DuplicateFreeList` (Reference: DuplicateFreeList) on the list of rows.

##### 5.2-11 Append

`‣ Append`( m, n ) | ( method )

Returns: nothing

Behaves exactly as expected. Of course, the `cmat`s m and n must be over the same field and have the same number of columns. Note that the rows of n themselves (and no copies) will be put into the matrix m.

##### 5.2-12 Filtered

`‣ Filtered`( m, f ) | ( method )

Returns: a new `cmat` containing some of the rows of m

Behaves exactly as expected. The function f will be called for each row of m.

##### 5.2-13 Unbind

`‣ Unbind`( m, f ) | ( method )

Returns: nothing

Behaves exactly as expected. Of course, only the last bound row may be unbound.

#### 5.3 Arithmetic

Of course, the standard arithmetic infix operations +, - and * (for vectors and scalars) work as expected by using the methods below. The comments on the usage of scalars in arithmetic operations involving vectors from Subsection 4.2-1 apply analogously.

##### `5.3-1 \+`

`‣ \+`( m, n ) | ( method )

Returns: the sum m+n as a new `cmat`

For two `cmat`s m and n. Works as expected.

##### `5.3-2 \-`

`‣ \-`( m, n ) | ( method )

Returns: the difference m-n as a new `cmat`

For two `cmat`s m and n. Works as expected.

##### 5.3-3 AdditiveInverseSameMutability

`‣ AdditiveInverseSameMutability`( m ) | ( method )

`‣ \-`( m ) | ( method )

Returns: the additive inverse of m as a new `cmat`

For a `cmat` m. Works as expected.

##### 5.3-4 AdditiveInverseMutable

`‣ AdditiveInverseMutable`( m ) | ( method )

Returns: the additive inverse of m as a new mutable `cmat`

For a `cmat` m. Works as expected.

##### `5.3-5 \*`

`‣ \*`( m, s ) | ( method )

`‣ \*`( s, m ) | ( method )

Returns: the scalar multiple s⋅m

For a `cmat` m and a scalar s. For the format of the scalar see 4.2-1. Works as expected.

##### `5.3-6 \*`

`‣ \*`( v, m ) | ( method )

Returns: the product v⋅m

For a `cmat` m and a `cvec` s with the same length as the number of rows of m. Works as expected. Note that there is a very fast method for the case that m is pre-greased (see 5.8).

##### `5.3-7 \^`

`‣ \^`( v, m ) | ( method )

Returns: the product v⋅m

For a `cmat` m and a `cvec` s with the same length as the number of rows of m. Works as expected. Note that there is a very fast method for the case that m is pre-greased (see 5.8).

##### `5.3-8 \*`

`‣ \*`( m, n ) | ( method )

Returns: the product m⋅n

Of course, the `cmat` m must have as many columns as the `cmat` n has rows. Works as expected. Note that there is a very fast method for the case that n is pre-greased (see 5.8).

##### 5.3-9 ZeroSameMutability

`‣ ZeroSameMutability`( m ) | ( method )

Returns: the zero `cmat` over the same field and with the same dimensions as m

m must be a `cmat`.

##### 5.3-10 ZeroMutable

`‣ ZeroMutable`( m ) | ( method )

Returns: a mutable copy of the zero `cmat` over the same field and with the same dimensions as m

m must be a `cmat`.

##### 5.3-11 OneSameMutability

`‣ OneSameMutability`( m ) | ( method )

Returns: the identity `cmat` over the same field and with the same dimensions as m

m must be a square `cmat`.

##### 5.3-12 OneMutable

`‣ OneMutable`( m ) | ( method )

Returns: a mutable copy of the identity `cmat` over the same field and with the same dimensions as m

m must be a square `cmat`.

##### 5.3-13 InverseMutable

`‣ InverseMutable`( m ) | ( method )

Returns: the multiplicative inverse of m

If the `cmat` is not square or not invertible then `fail` is returned. Behaves exactly as expected.

##### 5.3-14 InverseSameMutability

`‣ InverseSameMutability`( m ) | ( method )

Returns: the multiplicative inverse of m

If the `cmat` is not square or not invertible then `fail` is returned. Behaves exactly as expected.

##### 5.3-15 TransposedMat

`‣ TransposedMat`( m ) | ( method )

Returns: the transpose of m

Behaves exactly as expected.

##### 5.3-16 KroneckerProduct

`‣ KroneckerProduct`( m, n ) | ( method )

Returns: the Kronecker product of m and n

Behaves exactly as expected.

#### 5.4 Comparison of matrices and other information

##### 5.4-1 =

`‣ =`( m, n ) | ( method )

Returns: `true` or `false`

Returns `true` if the `cmat`s m and n are equal. The matrices must be over the same field and must have equal dimensions.

##### 5.4-2 LT

`‣ LT`( m, n ) | ( method )

Returns: `true` or `false`

Returns `true` if the `cmat` m is smaller than n. The matrices must be over the same field and must have equal dimensions. The method implements the lexicographic order and uses `LT` for the ordering of vectors. Note that the operation `LT` is the same as `\<`.

##### 5.4-3 IsZero

`‣ IsZero`( m ) | ( method )

Returns: `true` or `false`

Returns `true` if the `cmat` m is equal to zero, meaning that all entries are equal to zero.

##### 5.4-4 IsOne

`‣ IsOne`( m ) | ( method )

Returns: `true` or `false`

Returns `true` iff the `cmat` m is equal to the identity matrix.

##### 5.4-5 IsDiagonalMat

`‣ IsDiagonalMat`( m ) | ( method )

Returns: `true` or `false`

Returns `true` iff the `cmat` m is a diagonal matrix.

##### 5.4-6 IsUpperTriangularMat

`‣ IsUpperTriangularMat`( m ) | ( method )

Returns: `true` or `false`

Returns `true` iff the `cmat` m is an upper triangular matrix.

##### 5.4-7 IsLowerTriangularMat

`‣ IsLowerTriangularMat`( m ) | ( method )

Returns: `true` or `false`

Returns `true` iff the `cmat` m is a lower triangular matrix.

##### 5.4-8 CVEC_HashFunctionForCMats

`‣ CVEC_HashFunctionForCMats`( m, data ) | ( function )

Returns: an integer hash value

This is a hash function usable for the `ChooseHashFunction` (orb: ChooseHashFunction) framework. It takes as arguments a `cmat` m and a list data of length 2. The first entry of data is the length of the hash table used and the second entry is the number of bytes looked at in the `cvec`s in the matrices.

##### 5.4-9 ChooseHashFunction

`‣ ChooseHashFunction`( m, l ) | ( method )

Returns: a record with entries `func` and `data`.

Chooses a hash function to be used for `cmat`s like m (that is, over the same field with the same number of columns) and for hash tables of length l. The hash function itself is stored in the `func` component of the resulting record. The hash function has to be called with two arguments: the first must be a matrix like m and the second must be the value of the `data` component of the resulting record.

#### 5.5 Slicing and submatrices

As described in Section 5.2 you can use the slicing operator `\{\}` for read and write access of a subset of the rows of a `cmat`. However, the double slicing operator is not supported. The reason for this is twofold: First there is a technical issue that the double slicing operator cannot easily be overloaded in the GAP system. The second is, that very often the double slicing operator is used to copy a part of one matrix to another part of another matrix using double slicing on both sides of an assignment. This is quite inefficient because it creates an intermediate object, namely the submatrix which is extracted.

Therefore we have chosen to support submatrix access through two operations `ExtractSubMatrix` (5.5-1) and `CopySubMatrix` (5.5-2) described below.

##### 5.5-1 ExtractSubMatrix

`‣ ExtractSubMatrix`( m, rows, cols ) | ( operation )

Returns: a submatrix of m

This operation extracts the submatrix of the matrix m consisting of the rows described by the integer list (or range) rows and of the columns described by the integer list (or range) cols. This is thus equivalent to the usage m`{`rows`}{`cols`}`. Note that the latter does not work for `cmat`s, whereas a quite efficient method for `ExtractSubMatrix` is available for `cmat`s.

##### 5.5-2 CopySubMatrix

`‣ CopySubMatrix`( src, dst, srows, drows, scols, dcols ) | ( operation )

Returns: nothing

This operation extracts the submatrix of the matrix src consisting of the rows described by the integer list (or range) srows and of the columns described by the integer list (or range) scols and copies it into the submatrix of dst described by the integer lists (or ranges) drows and dcols. No intermediate object is created. This is thus equivalent to the usage dst`{`drows`}{`dcols`} :=` src`{`srows`}{`scols`}`. Note that the latter does not work for `cmat`s, whereas a quite efficient method for `CopySubMatrix` is available for `cmat`s.

#### 5.6 Information about matrices

##### 5.6-1 BaseField

`‣ BaseField`( m ) | ( method )

Returns: the base field of m

For a `cmat` m this returns the GAP object `GF(p,d)` corresponding to the base field of m. Note that this is a relatively fast lookup.

##### 5.6-2 Characteristic

`‣ Characteristic`( m ) | ( method )

Returns: the characteristic of the base field of m

Returns the characteristic of the base field of m (see `BaseField` (5.6-1)).

##### 5.6-3 DegreeFFE

`‣ DegreeFFE`( m ) | ( method )

Returns: the degree of the base field of m over its prime field

Returns the degree of the base field of m over its prime field (see `BaseField` (5.6-1)).

##### 5.6-4 DefaultField

`‣ DefaultField`( m ) | ( method )

Returns: the base field of m

For a `cmat` m this returns the GAP object `GF(p,d)` corresponding to the base field of m. Note that this is a relatively fast lookup.

#### 5.7 Input and output

##### 5.7-1 CVEC_WriteMat

`‣ CVEC_WriteMat`( f, m ) | ( method )

Returns: `true` or `fail`

f must be a file object from the IO package (see `IsFile` (IO: IsFile)) and m must be a `cmat`. The matrix m is written to the file f. Note that the format (see Section 3.4) is platform independent, such that matrices can be exchanged between different architectures. The result is `true` or `fail` depending on whether everything worked or an error occurred respectively.

##### 5.7-2 CVEC_WriteMatToFile

`‣ CVEC_WriteMatToFile`( fn, m ) | ( method )

Returns: `true` or `fail`

fn must be a string object containing a file name and m must be a `cmat`. The matrix m is written to the file with name fn on the local storage. Note that the format (see Section 3.4) is platform independent, such that matrices can be exchanged between different architectures. The result is `true` or `fail` depending on whether everything worked or an error occurred respectively.

##### 5.7-3 CVEC_WriteMatsToFile

`‣ CVEC_WriteMatsToFile`( fnpref, l ) | ( method )

Returns: `true` or `fail`

fnpref must be a string object containing a file name prefix and m must be a list of `cmat`s. The matrices in l are written to the files with names determined by using the string fnpref and appending the natural numbers from 1 to the length of l on the local storage. Note that the format (see Section 3.4) is platform independent, such that matrices can be exchanged between different architectures. The result is `true` or `fail` depending on whether everything worked or an error occurred respectively.

##### 5.7-4 CVEC_ReadMat

`‣ CVEC_ReadMat`( f ) | ( method )

Returns: a `cmat` or `fail`

f must be a file object from the IO package (see `IsFile` (IO: IsFile)). A matrix is read from the file f. Note that the format (see Section 3.4) is platform independent, such that matrices can be exchanged between different architectures. The result is `fail` if an error occurred.

##### 5.7-5 CVEC_ReadMatFromFile

`‣ CVEC_ReadMatFromFile`( fn ) | ( method )

Returns: a `cmat` or `fail`

fn must be a string object containing a file name. A matrix is read from the file with name fn on the local storage. Note that the format (see Section 3.4) is platform independent, such that matrices can be exchanged between different architectures. The result is `fail` if an error occurred.

##### 5.7-6 CVEC_ReadMatsFromFile

`‣ CVEC_ReadMatsFromFile`( fnpref ) | ( method )

Returns: a list of `cmat`s or `fail`

fnpref must be a string object containing a file name prefix. A list of matrices is read from the files with names determined by using the string fnpref and appending the natural numbers from 1 on from the local storage. The number of matrices read is determined by the highest number such that the corresponding filename exists in the filesystem. Note that the format (see Section 3.4) is platform independent, such that matrices can be exchanged between different architectures. The result is `fail` if an error occurred.

#### 5.8 Grease

The basic idea behind the "grease" technique is that over a finite field there are only finitely many linear combinations of a fixed list of vectors. Thus, many operations including matrix multiplication can be speeded up by precomputing all possible linear combinations and then just looking up the right one. For the case of matrix multiplication this can for example gain a factor of about 4 over the field with 2 elements using "grease level8", which means that for blocks of 8 rows all linear combinations are precomputed.

The cvec uses grease whenever appropriate automatically for example for matrix multiplication. However, occasionally the user has to take a conscious decision, which matrices to grease, because this of course uses more memory.

A `cmat` can be "pre-greased" with level l, which means that it is chopped into chunks of l rows and for each such chunk all possible linear combinations are precomputed and stored. This increases the memory used to store the matrix by a factor of q^l if the base field of the matrix has q elements. However, operations like vector matrix multiplication and matrix matrix multiplication (here the right hand side matrix must be greased!) are speeded up. As a rule of thumb the factor one can hope for is about l ⋅ (q-1)/q. Note that for big matrices matrix multiplication does greasing on the fly anyway and therefore one cannot hope for such a big factor by pre-greasing.

##### 5.8-1 GreaseMat

`‣ GreaseMat`( m, l ) | ( operation )

`‣ GreaseMat`( m ) | ( operation )

Returns: nothing

m must be a `cmat`. It is pregreased with level l. Without the argument l a grease level depending of the field size is chosen automatically. Note that the matrix will need much more memory when pregreased.

##### 5.8-2 UnGreaseMat

`‣ UnGreaseMat`( m ) | ( operation )

Returns: nothing

m must be a `cmat`. The pregreased information is deleted. This can save a lot of memory.

#### 5.9 Everything else

##### 5.9-1 Randomize

`‣ Randomize`( m ) | ( method )

`‣ Randomize`( m, rs ) | ( method )

Returns: the matrix m

m must be a `cmat` and rs must be a random source object if given. This method changes the matrix m in place by (pseudo) random values in the field over which the matrix lives. If a random source is given, the pseudo random numbers used are taken from this source, otherwise the global random source in the GAP library is taken.

##### 5.9-2 OverviewMat

`‣ OverviewMat`( m ) | ( function )

Returns: nothing

An ASCII art overview over the `cmat` m is printed. Stars indicate nonzero blocks in the matrix and dots zero blocks.

##### 5.9-3 Unpack

`‣ Unpack`( m ) | ( method )

Returns: a list of lists of finite field elements

This operation unpacks a `cmat` m. A new plain list of plain lists containing the corresponding numbers as GAP finite field elements. Note that the matrix m remains unchanged.

[Top of Book] [Contents] [Previous Chapter] [Next Chapter]
Goto Chapter: Top 1 2 3 4 5 6 7 8 9 Bib Ind

generated by GAPDoc2HTML