23 Row Vectors

Just as in mathematics, a vector in GAP is any object which supports appropriate addition and scalar multiplication operations (see Vector Spaces). As in mathematics, an especially important class of vectors are those represented by a list of coefficients with respect to some basis. These correspond roughly to the GAP concept of row vectors.

  • IsRowVector( obj ) C

    A row vector is a vector (see IsVector) that is also a homogeneous list. Typical examples are lists of integers, lists of finite field elements of the same characteristic, lists of polynomials from a common polynomial ring, and matrices.

    The additive operations of the vector must thus be compatible with that for lists, implying that the list entries are the coefficients of the vector with respect to some basis.

    Note that not all row vectors admit a scalar product via *; for example, matrices are row vectors but the matrix product is defined in a different way. For the installation of a scalar product of row vectors, the entries of the vector must be ring elements; note that the default method expects the row vectors to lie in IsRingElementList, and this category may not be implied by IsRingElement for all entries of the row vector (see the comment for IsVector in IsVector).

    Note that methods for special types of row vectors really must be installed with the requirement IsRowVector, since IsVector may lead to a rank of the method below that of the default method for row vectors (see file vecmat.gi).

    gap> IsRowVector([1,2,3]);
    true
    

    Because row vectors are just a special case of lists, all operations and functions for lists are applicable to row vectors as well (see chapter Lists). This especially includes accessing elements of a row vector (see List Elements), changing elements of a mutable row vector (see List Assignment), and comparing row vectors (see Comparisons of Lists).

    Note that, unless your algorithms specifically require you to be able to change entries of your vectors, it is generally better and faster to work with immutable row vectors. See section Mutability and Copyability for more details.

    Sections

    1. Operators for Row Vectors
    2. Row Vectors over Finite Fields
    3. Coefficient List Arithmetic
    4. Shifting and Trimming Coefficient Lists
    5. Functions for Coding Theory
    6. Vectors as coefficients of polynomials

    23.1 Operators for Row Vectors

    The rules for arithmetic operations between row vectors, are in fact the same as those for the arithmetic of lists, given in part in section Arithmetic for Lists, here we reiterate and complete that definition, in the language of vectors.

  • vec1 + vec2 O

    returns the sum of the two vectors vec1 and vec2, which must have the same length and be defined over a common field. The sum is a new vector where each entry is the sum of the corresponding entries of the vectors. As an exception it is also possible to add an integer vector to a vector over the finite field F, in which case the integers are interpreted as scalar * One(F).

  • scalar + vec O
  • vec + scalar O

    returns a new vector where each entry is the sum of the scalar and the corresponding entry of the vector. The elements of vec must lie in a common field with scalar. As an exception it is also possible to add an integer scalar to a vector over the finite field F, in which case the integer is interpreted as scalar * One(F).

    gap> [ 1, 2, 3 ] + [ 1/2, 1/3, 1/4 ];
    [ 3/2, 7/3, 13/4 ]
    gap>  [ 1/2, 3/2, 1/2 ] + 1/2;
    [ 1, 2, 1 ]
    

  • vec1 - vec2 O
  • scalar - vec O
  • vec - scalar O

    The difference operator - returns the componentwise difference of its two operands and is defined subject to the same restrictions as +.

    gap> [ 1, 2, 3 ] - [ 1/2, 1/3, 1/4 ];
    [ 1/2, 5/3, 11/4 ]
    gap> [ 1/2, 3/2, 1/2 ] - 1/2;
    [ 0, 1, 0 ]
    

  • vec1 * vec2 O

    returns the standard scalar product of vec1 and vec2, which must both have the same length and take their entries from the same field. As remarked above, the system only provides a general method for the case where the vectors lie in IsRingElementList. The product is the sum of the products of the corresponding entries of the vectors. As an exception it is also possible to multiply an integer vector to a finite field vector, in which case the integers are interpreted as scalar * One(GF).

  • scalar * vec O
  • vec * scalar O

    returns the product of scalar and vector. The elements of vec must lie in a common field with scalar. The product is a new vector where each entry is the product of the scalar and the corresponding entry of the vector. As an exception it is also possible to multiply an integer scalar to a finite field vector, in which case the integer is interpreted as scalar * One(GF).

    gap> [ 1, 2, 3 ] * [ 1/2, 1/3, 1/4 ];
    23/12
    gap> [ 1/2, 3/2, 1/2 ] * 2;
    [ 1, 3, 1 ]
    

    For the mutability of results of arithmetic operations, see Mutability and Copyability.

    Further operations with vectors as operands are defined by the matrix operations (see Operators for Matrices).

  • NormedRowVector( v ) A

    returns a scalar multiple w = c * v of the row vector v with the property that the first nonzero entry of w is an identity element in the sense of IsOne.

    gap> NormedRowVector([5,2,3]);
    [ 1, 2/5, 3/5 ]
    

    23.2 Row Vectors over Finite Fields

    GAP can use compact formats to store row vectors over fields of order at most 256, based on those used by the Meat-Axe Rin93. This format also permits extremely efficient vector arithmetic. On the other hand element access and assignment is significantly slower than for plain lists.

    The function ConvertToVectorRep is used to convert a list into a compressed vector, or to rewrite a compressed vector ove another field. Note that this function is much faster when it is given a field (or field size) as an argument, rather than having to scan the vector and try to decide the field. Supplying the field can also avoid errors and/or loss of performance, when one vector from some collection happens to have all of its entries over a smaller field than the "natural" field of the problem.

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

    ConvertToVectorRep( list ) converts list to an internal vector representation if possible.

    ConvertToVectorRep( list , field ) converts list to an internal vector representation appropriate for a vector over field. It is forbidden to call this function unless all elements of list lie in field.

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

    list may already be a compressed vector. In this case, if no field or fieldsize is given, then nothing happens. If one is given then the vector is rewritten as a compressed vector over the given field unless it has the filter IsLockedRepresentationVector, in which case it is not changed.

    The return value is the size of the field over which the vector ends up written, if it is written in a compressed representation. Otherwise it is true if the vector does consist entirely of elements of finite fields and fail otherwise.

    In this example, we first creat a row vector and then ask GAP to rewrite it, first over GF(2) and then over GF(4).

    gap> v := [Z(2)^0,Z(2),Z(2),0*Z(2)];
    [ Z(2)^0, Z(2)^0, Z(2)^0, 0*Z(2) ]
    gap> RepresentationsOfObject(v);
    [ "IS_PLIST_REP", "IsInternalRep" ]
    gap> ConvertToVectorRep(v);
    2
    gap> v;
    <a GF2 vector of length 4>
    gap> ConvertToVectorRep(v,4);
    4
    gap> v;
    [ Z(2)^0, Z(2)^0, Z(2)^0, 0*Z(2) ]
    gap> RepresentationsOfObject(v);
    [ "IsDataObjectRep", "Is8BitVectorRep" ]
    

    A vector in the special representation over GF(2) is always viewed as <a GF2 vector of length ...>. Over fields of orders 3 to 256, a vector of length 10 or less is viewed as the list of its coefficients, but a longer one is abbreviated.

  • NumberFFVector( vec, sz ) O

    returns an integer that gives the position of the finite field row vector (vec) in the sorted list of all row vectors over the field with sz elements in the same dimension as vec. NumberFFVector returns fail if the vector cannot be represented over the field with sz elements.

    23.3 Coefficient List Arithmetic

    The following operations all perform arithmetic on row vectors. given as homogeneous lists of the same length, containing elements of a commutative ring.

    There are two reasons for using AddRowVector in preference to arithmetic operators. Firstly, the three argument form has no single-step equaivalent. Secondly AddRowVector changes its first argument in-place, rather than allocating a new vector to hold the result, and may thus produce less garbage

  • AddRowVector( dst, src, [mul[, from, to]] ) O

    Adds the product of src and mul to dst, changing dst. If from and to are given then only the index range [from..to] is guaranteed to be affected. Other indices MAY be affected, if it is more convenient to do so. Even when from and to are given, dst and src must be row vectors of the same length

    If mul is not given either then this Operation simply adds src to dst

  • AddCoeffs( list1, poss1, list2, poss2, mul ) O
  • AddCoeffs( list1, list2, mul ) O
  • AddCoeffs( list1, list2 ) O

    AddCoeffs adds the entries of list2{poss2}, multiplied by the scalar mul, to list1{poss1}. Non-existing entries in list1 are assumed to be zero. The position of the right-most non-zero element is returned.

    If the ranges poss1 and poss2 are not given, they are assumed to span the whole vectors. If the scalar mul is omitted, one is used as a default.

    Note that it is the responsibility of the caller to ensure that the list2 has elements at position poss2 and that the result (in list1) will be a dense list.

    The function is free to remove trailing (right-most) zeros.

    gap> l:=[1,2,3,4];;m:=[5,6,7];;AddCoeffs(l,m);
    4
    gap> l;
    [ 6, 8, 10, 4 ]
    

  • MultRowVector( list1, poss1, list2, poss2, mul ) O
  • MultRowVector( list, mul ) O

    The five-argument version of this Operation replaces list1[poss1[i]] by mul*list2[poss2[i]] for i between 1 and Length(poss1)

    The two-argument version simply multiplies each element of list, in-place, by mul

  • CoeffsMod( list1, [len1, ]mod ) O

    returns the coefficient list obtained by reducing the entries in list1 modulo mod. After reducing it shrinks the list to remove trailing zeroes.

    gap> l:=[1,2,3,4];;CoeffsMod(l,2);
    [ 1, 0, 1 ]
    

    23.4 Shifting and Trimming Coefficient Lists

    The following functions change coefficient lists by shifting or trimming.

  • LeftShiftRowVector( list, shift ) O

    changes list by assigning list[i]:=list[i+shift] and removing the last shift entries of the result.

  • RightShiftRowVector( list, shift, fill ) O

    changes list by assigning list[i+shift]:=list[i] and filling the shift training entries with fill.

  • ShrinkRowVector( list ) O

  • RemoveOuterCoeffs( list, coef ) O

    removes coef at the beginning and at the end of list and returns the number of elements removed at the beginning.

    gap> l:=[1,1,2,1,2,1,1,2,1];;RemoveOuterCoeffs(l,1);
    2
    gap> l;
    [ 2, 1, 2, 1, 1, 2 ]
    

    23.5 Functions for Coding Theory

    The following functions perform operations on Finite fields vectors considered as code words in a linear code.

  • WeightVecFFE( vec ) O

    returns the weight of the finite field vector vec, i.e. the number of nonzero entries.

  • DistanceVecFFE( vec1, vec2 ) O

    returns the distance between the two vectors vec1 and vec2, which must have the same length and whose elements must lie in a common field. The distance is the number of places where vec1 and vec2 differ.

  • DistancesDistributionVecFFEsVecFFE( vecs, vec ) O

    returns the distances distribution of the vector vec to the vectors in the list vecs. All vectors must have the same length, and all elements must lie in a common field. The distances distribution is a list d of length Length(vec)+1, such that the value d[i] is the number of vectors in vecs that have distance i+1 to vec.

  • DistancesDistributionMatFFEVecFFE( mat, f, vec ) O

    returns the distances distribution of the vector vec to the vectors in the vector space generated by the rows of the matrix mat over the finite field f. The length of the rows of mat and the length of vec must be equal, and all elements must lie in f. The rows of mat must be linearly independent. The distances distribution is a list d of length Length(vec)+1, such that the value d[i] is the number of vectors in the vector space generated by the rows of mat that have distance i+1 to vec.

  • AClosestVectorCombinationsMatFFEVecFFE( mat, f, vec, l, stop ) O

    runs through the f-linear combinations of the vectors in the rows of the matrix mat that can be written as linear combinations of exactly l rows (that is without using zero as a coefficient) and returns a vector from these that is closest to the vector vec. The length of the rows of mat and the length of vec must be equal, and all elements must lie in f. The rows of mat must be linearly independent. If it finds a vector of distance at most stop, which must be a nonnegative integer, then it stops immediately and returns this vector.

  • CosetLeadersMatFFE( mat, f ) O

    returns a list of representatives of minimal weight for the cosets of a code. mat must be a check matrix for the code, the code is defined over the finite field f. All rows of mat must have the same length, and all elements must lie in f. The rows of mat must be linearly independent.

    23.6 Vectors as coefficients of polynomials

    A list of ring elements can be interpreted as a row vector or the list of coefficients of a polynomial. There are a couple of functions that implement arithmetic operations based on these interpretations. GAP contains proper support for polynomials (see Polynomials and Rational Functions), the operations described in this section are on a lower level.

    The following operations all perform arithmetic on univariate polynomials given by their coefficient lists. These lists can have different lengths but must be dense homogeneous lists containing elements of a commutative ring. Not all input lists may be empty.

    In the following descriptions we will always assume that list1 is the coefficient list of the polynomial pol1 and so forth. If length parameter leni is not given, it is set to the length of listi by default.

  • ValuePol( coeff, x ) F

    Let coeff be the coefficients list of a univariate polynomial f, and x a ring element. Then ValuePol returns the value f(x ).

    The coefficient of xi is assumed to be stored at position i+1 in the coefficients list.

    gap> ValuePol([1,2,3],4);
    57
    

  • ProductCoeffs( list1, [len1, ]list2[, len2] ) O

    Let pol1 (and pol2) be polynomials given by the first len1 (len2) entries of the coefficient list list2 (list2). If len1 and len2 are omitted, they default to the lengths of list1 and list2. This operation returns the coefficient list of the product of pol1 and pol2.

    gap> l:=[1,2,3,4];;m:=[5,6,7];;ProductCoeffs(l,m);   
    [ 5, 16, 34, 52, 45, 28 ]
    

  • ReduceCoeffs( list1[, len1], list2[, len2] ) O

    changes list1 to the coefficient list of the remainder when dividing pol1 by pol2. This operation changes list1 which therefore must be a mutable list. The operations returns the position of the last non-zero entry of the result but is not guaranteed to remove trailing zeroes.

    gap> l:=[1,2,3,4];;m:=[5,6,7];;ReduceCoeffs(l,m); 
    2
    gap> l;
    [ 64/49, -24/49, 0, 0 ]
    

  • ReduceCoeffsMod( list1, [len1, ]list2, [len2, ]mod ) O

    changes list1 to the coefficient list of the remainder when dividing pol1 by pol2 modulo mod. mod must be a positive integer. This operation changes list1 which therefore must be a mutable list. The operations returns the position of the last non-zero entry of the result but is not guaranteed to remove trailing zeroes.

    gap> l:=[1,2,3,4];;m:=[5,6,7];;ReduceCoeffsMod(l,m,3);
    1
    gap> l;
    [ 1, 0, 0, 0 ]
    

  • PowerModCoeffs( list1, [len1, ]exp, list2[, len2] ) O

    Let pol1 (and pol2) be polynomials given by the first len1 (len2) entries of the coefficient list list3 (list2). If len1 and len2 are omitted, they default to the lengths of list1 and list2. This operation returns the coefficient list of the remainder when dividing pol1^exp by pol2. The operation reduces coefficients already while computing powers and therefore avoids an explosion in list length.

    gap> l:=[1,2,3,4];;m:=[5,6,7];;PowerModCoeffs(l,5,m); 
    [ -839462813696/678223072849, -7807439437824/678223072849, 0 ]
    

  • ShiftedCoeffs( list, shift ) O

    produces a new coefficient list new obtained by the rule new[i+shift]:=list[i] and filling initial holes by the appropriate zero.

    gap> l:=[1,2,3];;ShiftedCoeffs(l,2);ShiftedCoeffs(l,-2);    
    [ 0, 0, 1, 2, 3 ]
    [ 3 ]
    

  • ShrinkCoeffs( list ) O

    removes trailing zeroes from list. It returns the position of the last non-zero entry, that is the length of list after the operation.

    gap> l:=[1,0,0];;ShrinkCoeffs(l);l;
    1
    [ 1 ]
    

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

    GAP 4 manual
    February 2000