# Creation of Vector Spaces and Arithmetic with Vectors

## Construction of a Vector Space

### `VectorSpace(K, n): Fld, RngIntElt -> ModTupFld`

### `KSpace(K, n): Fld, RngIntElt -> ModTupFld`

Given a field $K$ and a non-negative integer $n$, create the $n$-dimensional vector space $V = K^{(n)}$, consisting of all $n$-tuples over $K$. The vector space is created with respect to the standard basis, $e_1, \ldots, e_n$, where $e_i$ $(i = 1, \ldots, n)$ is the vector containing a $1$ in the $i$-th position and zeros elsewhere.

Use of the functions `VectorSpace` and `KSpace` ensures that subspaces of $V$ will be presented in embedded form.

### `KModule(K, n): Fld, RngIntElt -> ModFld`

Given a field $K$ and a non-negative integer $n$, create the $n$-dimensional vector space $V = K^{(n)}$, consisting of all $n$-tuples over $K$. The vector space is created with respect to the standard basis, $e_1, \ldots, e_n$, where $e_i$ $(i = 1, \ldots, n)$ is the vector containing a $1$ in the $i$-th position and zeros elsewhere.

Use of the function `KModule` ensures that subspaces of $V$ will be presented in reduced form. In all other respects, a vector space created by this function is identical to one created by `KSpace`.

### `KMatrixSpace(K, m, n): Fld, RngIntElt, RngIntElt -> ModMatFld`

Given a field $K$ and integers $m$ and $n$ greater than one, create the vector space $K^{(m \times n)}$, consisting of all $m \times n$ matrices over $K$. The vector space is created with the standard basis, $\{E_{ij}\  |\  i = 1 \ldots, m, j = 1 \ldots, n\}$, where $E_{ij}$ is the matrix having a $1$ in the ($i, j$)-th position and zeros elsewhere. Note that for a matrix space, subspaces will always be presented in embedded form, i.e. there is no reduced mode available for matrix spaces.

### `Hom(V, W): ModTupFld, ModTupFld -> ModMatFld`

If $V$ is the vector space $K^{(m)}$ and $W$ is the vector space $K^{(n)}$, create the matrix space ${\operatorname{Hom}}_{K}(V, W)$ as the vector space $K^{(m \times n)}$, represented as the set of all $m \times n$ matrices over $K$. The vector space is created with the standard basis, $\{E_{ij}\  |\  i = 1 \ldots, m, j = 1 \ldots, n\}$, where $E_{ij}$ is the matrix having a $1$ in the ($i, j$)-th position and zeros elsewhere.

### `Example: Create Q6 (ex-d5b27e)`

We construct the vector space $V$ consisting of $6$-tuples over the rational field.

```magma
> Q := RationalField();
> V := VectorSpace(Q, 6);
> V;
Vector space of dimension 6 over Rational Field

```

### `Example: Create K35 (ex-4fca7f)`

We construct the matrix space $M$ consisting of $3 \times 5$ matrices over the field ${\mathbb{Q}}(\sqrt{5})$.

```magma
> K<w> := QuadraticField(5);
> V := KMatrixSpace(K, 3, 5);
> V;
Full Vector Space of 3 by 5 matrices over Quadratic Field Q(w)

```

## Construction of a Vector Space with Inner Product Matrix

### `VectorSpace(K, n, F): Fld, RngIntElt, Mtrx -> ModTupFld`

### `KSpace(K, n, F): Fld, RngIntElt, Mtrx -> ModTupFld`

Given a field $K$, a non-negative integer $n$ and a square $n \times n$ symmetric matrix $F$, create the $n$-dimensional vector space $V = K^{(n)}$ (in embedded form), with inner product matrix $F$. This is the same as `VectorSpace(K, n)`, except that the functions `Norm` and `InnerProduct` (see below) will be with respect to the inner product matrix $F$.

## Construction of a Vector

### `elt<V | L>: ModTupFld, List -> ModTupFldElt`

**(1)**
Suppose $V$ is a subspace of the vector space $K^{(n)}$. Given elements $a_1, \ldots, a_n$ belonging to $K$, construct the vector $v = (a_1, \ldots, a_n)$ as a vector of $V$. Note that if $v$ is not an element of $V$, an error will result.

**(2)**
Suppose $V$ is a subspace of the matrix space $K^{(m \times n)}$. Given elements $a_1, \ldots, a_{mn}$ belonging to $K$, construct the matrix $m = (a_1, \ldots, a_{mn})$ as an element of $V$. Note that if $m$ is not an element of $V$, an error will result.

### `V ! Q: ModTupFld, [RngElt] -> ModTupFldElt`

**(1)**
Suppose $V$ is a subspace of the vector space $K^{(n)}$. Given elements $a_1, \ldots, a_n$ belonging to $K$, construct the vector $v = (a_1, \ldots, a_n)$ as a vector of $V$. Note that if $v$ is not an element of $V$, an error will result.

**(2)**
Suppose $V$ is a subspace of the matrix space $K^{(m \times n)}$. Given elements $a_1, \ldots, a_{mn}$ belonging to $K$, construct the matrix $m = (a_1, \ldots, a_{mn})$ as an element of $V$. Note that if $m$ is not an element of $V$, an error will result.

### `CharacteristicVector(V, S): ModTupFld, { RngElt } -> ModTupFldElt`

Given a subspace $V$ of the vector space $K^{(n)}$ together with a set $S$ of integers lying in the interval $[1, n]$, return the characteristic number of $S$ as a vector of $V$.

### `V ! 0: ModTupFld, RngIntElt -> ModTupFldElt`

### `Zero(V): ModTupFld -> ModTupFldElt`

The zero element for the vector space $V$.

### `Random(V): ModTupFld -> ModTupFldElt`

Given a vector space $V$ defined over a finite field, return a random vector.

### `Example: Vectors (ex-df9b72)`

We create the 5-dimensional vector space $V$ over ${\bf F}_{4}$ and define the vector $u = (1, w, 1+w, 0, 0)$, where $w$ is a primitive element of ${\bf F}_{4}$.

```magma
> K<w> := GaloisField(4);
> V    := VectorSpace(K, 5);
> u    := V ! [1, w, 1+w, 0, 0];
> u;
(1 w w + 1 0 0)
> zero := V ! 0;
> zero;
(0 0 0 0 0)
r := Random(V);
(1 0 w 1 w + 1)

```

### `Example: Matrices (ex-d17116)`

We create an element belonging to the space of $3 \times 4$ matrices over the number field ${\mathbb{Q}}(w)$, where $w$ is a root of $x^7 - 7x + 3$.

```magma
> R<x> := PolynomialRing(RationalField());
> L<w> := NumberField(x^7 - 7*x + 3);
> L34 := KMatrixSpace(L, 3, 4);
> a := L34 ! [ 1, w, 0, -w, 0, 1+w, 2, -w^3, w-w^3, 2*w, 1/3, 1 ];
> a;
[1    w    0    -1 * w]
[0    w + 1    2    -1 * w^3]
[-1 * w^3 + w    2 * w    1/3    1]

```

## Deconstruction of a Vector

### `ElementToSequence(u): ModTupFldElt -> [RngElt]`

### `Eltseq(u): ModTupFldElt -> [RngElt]`

Given an element $u$ belonging to the $K$-vector space $V$, return $u$ in the form of a sequence $Q$ of elements of $V$. Thus, if $u$ is an element of $K^{(n)}$, then $Q[i] = u[i]$, $1 \leq i \leq n$.

## Arithmetic with Vectors

For the following operations the vectors $u$ and $v$ must belong to the same vector space i.e. the same tuple space $K^{(n)}$ or the same matrix space $K^{(m \times n)}$. The scalar $a$ must belong to the field $K$.

### `u + v: ModTupFldElt, ModTupFldElt -> ModTupFldElt`

Sum of the vectors $u$ and $v$, where $u$ and $v$ lie in the same vector space.

### `- u: ModTupFldElt -> ModTupFldElt`

Additive inverse of the vector $u$.

### `u - v: ModTupFldElt, ModTupFldElt -> ModTupFldElt`

Difference of the vectors $u$ and $v$, where $u$ and $v$ lie in the same vector space.

### `x * u: FldElt, ModTupFldElt -> ModTupFldElt`

### `u * x: ModTupFldElt, FldElt -> ModTupFldElt`

The scalar product of the vector $u$ belonging to the $K$-vector space and the field element $x$ belonging to $K$.

### `u / x: ModTupFldElt, FldElt -> ModTupFldElt`

The scalar product of the vector $u$ belonging to the $K$-vector space and the field element $1/x$ belonging to $K$ where $x$ is non-zero.

### `NumberOfColumns(u): ModTupFldElt -> RngIntElt`

### `Ncols(u): ModTupFldElt -> RngIntElt`

The number of columns in the vector $u$.

### `Depth(u): ModTupRngElt -> RngIntElt`

The index of the first non-zero entry of the vector $u$ (0 if none such).

### `(u, v): ModTupFldElt, ModTupFldElt -> FldElt`

### `InnerProduct(u, v): ModTupFldElt, ModTupFldElt -> FldElt`

Return the inner product of the vectors $u$ and $v$ with respect to the inner product defined on the space. If an inner product matrix $F$ is given when the space is created, then this is defined to be $u\cdot F\cdot v^{tr}$. Otherwise, this is simply $u\cdot v^{tr}$.

### `IsZero(u): ModElt -> BoolElt`

Returns `true` iff the vector $u$ belonging to a vector space is the zero element.

### `Norm(u): ModTupFldElt -> FldElt`

Return the norm product of the vector $u$ with respect to the inner product defined on the space. If an inner product matrix $F$ is given when the space is created, then this is defined to be $u\cdot F\cdot u^{tr}$. Otherwise, this is simply $u\cdot u^{tr}$.

### `Normalise(u): ModTupFldElt -> ModTupFldElt`

### `Normalize(u): ModTupFldElt -> ModTupFldElt`

Given an element $u$, not the zero element, belonging to the $K$-vector space $V$, return ${1\over a}*u$, where $a$ is the first non-zero component of $u$. If $u$ is the zero vector, it is returned. The net effect is that `Normalize(u)` always returns a vector $v$ in the subspace generated by $u$, such that the first non-zero component of $v$ (if existent) is $K!1$.

### `Rotate(u, k): ModTupFldElt, RngIntElt -> ModTupFldElt`

Given a vector $u$, return the vector obtained from $u$ by rotating by k coordinate positions.

### `Rotate(~u, k): ModTupFldElt, RngIntElt`

Given a vector $u$, destructively rotate $u$ by k coordinate positions.

### `NumberOfRows(u): ModTupFldElt -> RngIntElt`

### `Nrows(u): ModTupFldElt -> RngIntElt`

The number of rows in the vector $u$ (1 of course; included for completeness).

### `Support(u): ModTupFldElt -> { RngElt }`

A set of integers giving the positions of the non-zero components of the vector $u$.

### `TensorProduct(u, v): ModTupFldElt, ModTupFldElt -> FldElt`

The tensor (Kronecker) product of the vectors $u$ and $v$. The resulting vector has degree equal to the product of the degrees of $u$ and $v$.

### `Trace(u, F): ModTupFldElt, Fld -> ModTupFldElt`

### `Trace(u): ModTupFldElt -> ModTupFldElt`

Given a vector belonging to the space $K^{(n)}$, and a subfield $F$ of $K$, return the vector obtained by replacing each component of $u$ by its trace over the subfield $F$. If $F$ is the prime field of $K$, it may be omitted.

### `Weight(u): ModTupFldElt -> RngIntElt`

The number of non-zero components of the vector $u$.

### `Example: Arithmetic (ex-ca5d9a)`

We illustrate the use of the arithmetic operators for module elements by applying them to elements of the $4$-dimensional vector space over the field ${\mathbb{Q}}(w)$, where $w$ is an $8$-th root of unity.

```magma
> K<w> := CyclotomicField(8);
> V := VectorSpace(K, 4);
> x := V ! [ w, w^2, w^4, 0];
> y := V ! [1, w, w^2, w^4];
> x + y;
(  w + 1 w^2 + w w^2 - 1      -1)
> -x;
(  -w -w^2    1    0)
> x - y;
(   w - 1  w^2 - w -w^2 - 1        1)
> w * x;
(w^2 w^3  -w   0)
> y * w^ -4;
(  -1   -w -w^2    1)
> Normalize(x);
(  1   w w^3   0)
> InnerProduct(x, y);
w^2 + 2*w
> z := V ! [1, 0, w, 0 ];
> z;
(1 0 w 0)
> Support(z);
{ 1, 3 }

```

### `Example: Inner Product (ex-e5eb13)`

We illustrate how one can define a non-trivial inner product on a space.

```magma
> Q := RationalField();
> F := SymmetricMatrix(Q, [1, 0,2, 0,0,3, 1,2,3,4]);
> F;
[1 0 0 1]
[0 2 0 2]
[0 0 3 3]
[1 2 3 4]
> V := VectorSpace(Q, 4, F);
> V;
Full Vector space of degree 4 over Rational Field
Inner Product Matrix:
[1 0 0 1]
[0 2 0 2]
[0 0 3 3]
[1 2 3 4]
> v := V![1,0,0,0];
> Norm(v);
1
> w := V![0,1,0,0];
> Norm(w);
2
> InnerProduct(v, w);
0
> z := V![0,0,0,1];
> Norm(z);
4
> InnerProduct(v, z);
1
> InnerProduct(w, z);
2

```

## Indexing Vectors and Matrices

The indexing operations have a different meaning depending upon whether they are applied to a tuple space or a matrix space.

### `u[i]: ModTupFldElt, RngIntElt -> RngElt`

### `u[i]: ModTupFldElt, RngIntElt -> ModTupFldElt`

### `u[i, j]: ModTupFldElt, RngIntElt, RngIntElt -> ModTupFldElt`

Given a vector $u$ belonging to a $K$-vector space $V$, the result of this operation depends upon whether $V$ is a tuple or matrix space. If $V$ is a subspace of $K^{(n)}$, and $i$, $1 \leq i\leq n$, is a positive integer, the $i$-th component of the vector $u$ is returned (as an element of the field $K$). If $V$ is a subspace of $K^{(m \times n)}$, and $i$, $1 \leq i\leq m$, is a positive integer, $u[i]$ will return the $i$-th row of the matrix $u$ (as an element of the vector space $K^{(n)}$). Similarly, if $i$ and $j$, $1 \leq i\leq m$, $1 \leq j\leq n$, are positive integers, $u[i, j]$ will return the $(i,j)$-th component of the matrix $u$ (as an element of $K$).

### `u[i] := x: ModTupFldElt, RngIntElt, RngElt -> ModTupFldElt`

### `u[i] := x: ModTupFldElt, RngIntElt, ModTupFldElt -> ModTupFldElt`

### `u[i, j] := x: ModTupFldElt, RngIntElt, RngIntElt, ModTupFldElt -> ModTupFldElt`

Given a vector $u$ belonging to a $K$-vector space $V$, and an element $x$ of $K$, the result of this operation depends upon whether $V$ is a tuple or matrix space. If $V$ is a subspace of $K^{(n)}$, and $i$, $1 \leq i\leq n$, is a positive integer, the $i$-th component of the vector $u$ is redefined to be $x$. If $V$ is a subspace of $K^{(m \times n)}$ and $1 \leq i\leq m$ is a positive integer and $x$ is an element of $K^{(n)}$, `u[i] := x` will redefine the $i$-th row of the matrix $u$ to be the vector $x$, where $x$ must be an element of $K^{(n)}$. Similarly, if $1 \leq i\leq m$, $1 \leq j\leq n$, are positive integers, `u[i, j] := x` will redefine the $(i,j)$-th component of the matrix $u$ to be $x$, where $x$ must be an element of $K$.

### `Example: Indexing (ex-876f3e)`

We illustrate the use of the indexing operators for vector space elements by applying them to a $3$-dimensional tuple space and a $2 \times 3$ matrix space over the field ${\mathbb{Q}}(w)$, where $w$ is an $8$-th root of unity.

```magma
> K<w> := CyclotomicField(8);
> V := VectorSpace(K, 3);
> u := V ! [ 1 + w, w^ 2, w^ 4];
> u;
((1 + w)     (w^2)           -1)
> u[3];
-1
> u[3] := 1 + w - w^7;
> u;
((1 + w) (w^2) (1 + w + w^3))
> // We now demonstrate indexing a matrix space
> W := KMatrixSpace(K, 2, 3);
> l := W ! [ 1 - w, 1 + w, 1 + w + w^2, 0, 1 - w^7, 1 - w^3 + w^6 ];
> l;
[(1 - w) (1 + w) (1 + w + w^2)]
[0 (1 + w^3) (1 - w^2 - w^3)]
> l[2];
(0 (1 + w^3) (1 - w^2 - w^3))
> l[2,2];
(1 + w^3)
> m := l[2];
> m;
(0 (1 + w^3) (1 - w^2 - w^3))
> l[2] := u;
> l;
[(1 - w) (1 + w) (1 + w + w^2)]
[(1 + w) (w^2) -1]
> l[2, 3] := 1 + w - w^7;
> l;
[(1 - w) (1 + w) (1 + w + w^2)]
[(1 + w) (w^2) (1 + w + w^3)]

```
