---
title: "Operations on Codewords"
canonical: "https://docs.magma-maths.org/v2.30-1/CodingTheory/LinearCodes/element.html"
source: "https://docs.magma-maths.org/v2.30-1/CodingTheory/LinearCodes/element.md"
part: "Coding Theory"
chapter: "Linear Codes over Finite Fields"
breadcrumbs:
  - {"title": "Magma Handbook", "source": "https://docs.magma-maths.org/v2.30-1/index.md"}
  - {"title": "Coding Theory", "source": "https://docs.magma-maths.org/v2.30-1/CodingTheory/index-coding-theory.md"}
  - {"title": "Linear Codes over Finite Fields", "source": "https://docs.magma-maths.org/v2.30-1/CodingTheory/LinearCodes/index-linear-codes.md"}
prev: {"title": "Invariants of a Code", "source": "https://docs.magma-maths.org/v2.30-1/CodingTheory/LinearCodes/access.md"}
next: {"title": "Coset Leaders", "source": "https://docs.magma-maths.org/v2.30-1/CodingTheory/LinearCodes/coset-leader.md"}
intrinsics:
  - {"name": "!", "signature": "C ! [a₁, ..., aₙ]: Code, [ RngElt ] -> ModTupRngElt"}
  - {"name": "elt", "signature": "elt< C | a₁, ..., aₙ>: Code, List -> ModTupRngElt"}
  - {"name": "!", "signature": "C ! u: Code, ModTupRngElt -> ModTupRngElt"}
  - {"name": "!", "signature": "C ! 0: Code, RngIntElt -> ModTupRngElt"}
  - {"name": "Random", "signature": "Random(C): Code -> ModTupRngElt"}
  - {"name": "+", "signature": "u + v: ModTupRngElt, ModTupRngElt -> ModTupRngElt"}
  - {"name": "-", "signature": "- u: ModTupRngElt -> ModTupRngElt"}
  - {"name": "-", "signature": "u - v: ModTupRngElt, ModTupRngElt -> ModTupRngElt"}
  - {"name": "*", "signature": "a * u: RngElt, ModTupRngElt -> ModTupRngElt"}
  - {"name": "Normalize", "signature": "Normalize(u): ModTupRngElt -> ModTupRngElt"}
  - {"name": "Syndrome", "signature": "Syndrome(w, C): ModTupFldElt, Code -> ModTupFldElt"}
  - {"name": "Distance", "signature": "Distance(u, v): ModTupRngElt, ModTupRngElt -> RngIntElt"}
  - {"name": "Weight", "signature": "Weight(u): ModTupRngElt -> RngIntElt"}
  - {"name": "LeeWeight", "signature": "LeeWeight(u): ModTupRngElt -> RngIntElt"}
  - {"name": "(u, v)", "signature": "(u, v): ModTupRngElt, ModTupRngElt -> RngElt"}
  - {"name": "InnerProduct", "signature": "InnerProduct(u, v): ModTupRngElt, ModTupRngElt -> RngElt"}
  - {"name": "Support", "signature": "Support(w): ModTupRngElt -> { RngIntElt }"}
  - {"name": "Coordinates", "signature": "Coordinates(C, u): Code, ModTupRngElt -> [ RngFinElt ]"}
  - {"name": "Parent", "signature": "Parent(w): ModTupRngElt -> ModTupRng"}
  - {"name": "Rotate", "signature": "Rotate(u, k): ModTupRngElt, RngIntElt -> ModTupRngElt"}
  - {"name": "Rotate", "signature": "Rotate(~u, k): ModTupRngElt, RngIntElt"}
  - {"name": "Trace", "signature": "Trace(u, S): ModTupFldElt, FldFin -> ModTupFldElt"}
  - {"name": "Trace", "signature": "Trace(u): ModTupFldElt -> ModTupFldElt"}
  - {"name": "eq", "signature": "u eq v: ModTupRngElt, ModTupRngElt -> BoolElt"}
  - {"name": "ne", "signature": "u ne v: ModTupRngElt, ModTupRngElt -> BoolElt"}
  - {"name": "IsZero", "signature": "IsZero(u): ModTupRngElt -> BoolElt"}
  - {"name": "u[i]", "signature": "u[i]: ModTupRngElt, RngIntElt -> RngElt"}
  - {"name": "[]", "signature": "u[i] := x;"}
examples:
  - "ex-75e920"
  - "ex-fb5172"
---

# Operations on Codewords

## Construction of a Codeword

<a id="operation-op-code-rngelt"></a>
### `C ! [a₁, ..., aₙ]: Code, [ RngElt ] -> ModTupRngElt`

<a id="constructor-constructor-elt-code-list-modtuprngelt"></a>
### `elt< C | a₁, ..., aₙ>: Code, List -> ModTupRngElt`

Given a code $C$ which is defined as a subset of the $R$-space $R^{(n)}$, and elements $a_1, \ldots, a_n$ belonging to $R$, construct the codeword $(a_1, \ldots, a_n)$ of $C$. It is checked that the vector ($a_1, \ldots, a_n$) is an element of $C$.

<a id="operation-op-code-modtuprngelt"></a>
### `C ! u: Code, ModTupRngElt -> ModTupRngElt`

Given a code $C$ which is defined as a subset of the $R$-space $V = R^{(n)}$, and an element $u$ belonging to $V$, create the codeword of $C$ corresponding to $u$. The function will fail if $u$ does not belong to $C$.

<a id="operation-op-code-rngintelt"></a>
### `C ! 0: Code, RngIntElt -> ModTupRngElt`

The zero word of the code $C$.

<a id="function-random-code"></a>
### `Random(C): Code -> ModTupRngElt`

A random codeword of $C$.

## Arithmetic Operations on Codewords

<a id="operation-op-plus-modtuprngelt-modtuprngelt"></a>
### `u + v: ModTupRngElt, ModTupRngElt -> ModTupRngElt`

Sum of the codewords $u$ and $v$, where $u$ and $v$ belong to the same linear code $C$.

<a id="operation-operation-modtuprngelt-modtuprngelt"></a>
### `- u: ModTupRngElt -> ModTupRngElt`

Additive inverse of the codeword $u$ belonging to the linear code $C$.

<a id="operation-op-minus-modtuprngelt-modtuprngelt"></a>
### `u - v: ModTupRngElt, ModTupRngElt -> ModTupRngElt`

Difference of the codewords $u$ and $v$, where $u$ and $v$ belong to the same linear code $C$.

<a id="operation-op-times-rngelt-modtuprngelt"></a>
### `a * u: RngElt, ModTupRngElt -> ModTupRngElt`

Given an element $a$ belonging to the field $K$, and a codeword $u$ belonging to the linear code $C$, return the codeword $a*u$.

<a id="function-normalize-modtuprngelt"></a>
### `Normalize(u): ModTupRngElt -> ModTupRngElt`

Given an element $u$ over a field, not the zero element, belonging to the linear code $C$, return ${1\over a}*u$, where $a$ is the first non-zero component of $u$. If $u$ is the zero vector, it is returned as the value of this function. 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$ is the unit of $K$.

<a id="function-syndrome-modtupfldelt-code"></a>
### `Syndrome(w, C): ModTupFldElt, Code -> ModTupFldElt`

Given an $[n, k]$ linear code $C$ over a finite field with parent vector space $V$, and a vector $w$ belonging to $V$, construct the syndrome of $w$ relative to the code $C$. This will be an element of the *syndrome space* of $C$.

## Distance and Weight

<a id="function-distance-modtuprngelt-modtuprngelt"></a>
### `Distance(u, v): ModTupRngElt, ModTupRngElt -> RngIntElt`

The Hamming distance between the codewords $u$ and $v$, where $u$ and $v$ belong to the same code $C$.

<a id="function-weight-modtuprngelt"></a>
### `Weight(u): ModTupRngElt -> RngIntElt`

The Hamming weight of the codeword $u$, i.e., the number of non-zero components of $u$.

<a id="function-leeweight-modtuprngelt"></a>
### `LeeWeight(u): ModTupRngElt -> RngIntElt`

The Lee weight of the codeword $u$.

<a id="example-ex-75e920"></a>
### `Example: Distances in the Ternary Golay Code (ex-75e920)`

We calculate all possible distances between code words of the non-extended Golay code over GF(3), and show the correspondence with all possible code word weights.

```magma
> C := GolayCode(GF(3),false);
> {Distance(v,w):v,w in C};
{ 0, 5, 6, 8, 9, 11 }
> {Weight(v):v in C};
{ 0, 5, 6, 8, 9, 11 }
```

## Vector Space and Related Operations

<a id="literal-literal-u-v-modtuprngelt-modtuprngelt-rngelt"></a>
### `(u, v): ModTupRngElt, ModTupRngElt -> RngElt`

<a id="function-innerproduct-modtuprngelt-modtuprngelt"></a>
### `InnerProduct(u, v): ModTupRngElt, ModTupRngElt -> RngElt`

Inner product of the vectors $u$ and $v$ with respect to the Euclidean norm, where $u$ and $v$ belong to the parent vector space of the code $C$.

<a id="function-support-modtuprngelt"></a>
### `Support(w): ModTupRngElt -> { RngIntElt }`

Given a word $w$ belonging to the $[n, k]$ code $C$, return its support as a subset of the integer set $\{ 1 .. n \}$. The support of $w$ consists of the coordinates at which $w$ has non-zero entries.

<a id="function-coordinates-code-modtuprngelt"></a>
### `Coordinates(C, u): Code, ModTupRngElt -> [ RngFinElt ]`

Given an $[n, k]$ linear code $C$ and a codeword $u$ of $C$ return the coordinates of $u$ with respect to $C$. The coordinates of $u$ are returned as a sequence $Q = [a_1, \ldots, a_k]$ of elements from the alphabet of $C$ so that $u = a_1 * C.1 + \ldots + a_k * C.k$.

<a id="function-parent-modtuprngelt"></a>
### `Parent(w): ModTupRngElt -> ModTupRng`

Given a word $w$ belonging to the code $C$, return the ambient space $V$ of $C$.

<a id="function-rotate-modtuprngelt-rngintelt"></a>
### `Rotate(u, k): ModTupRngElt, RngIntElt -> ModTupRngElt`

Given a vector $u$, return the vector obtained from $u$ by cyclically shifting its components to the right by $k$ coordinate positions.

<a id="procedure-rotate-modtuprngelt-rngintelt-ref"></a>
### `Rotate(~u, k): ModTupRngElt, RngIntElt`

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

<a id="function-trace-modtupfldelt-fldfin"></a>
### `Trace(u, S): ModTupFldElt, FldFin -> ModTupFldElt`

<a id="function-trace-modtupfldelt"></a>
### `Trace(u): ModTupFldElt -> ModTupFldElt`

Given a vector $u$ with components in $K$, and a subfield $S$ of $K$, construct the vector with components in $S$ obtained from $u$ by taking the trace of each component with respect to $S$. If $S$ is omitted, it is taken to be the prime field of $K$.

<a id="example-ex-fb5172"></a>
### `Example: Coordinates of a Code Word (ex-fb5172)`

We create a specific code word in the length $5$ even weight code, after a failed attempt to create a code word of odd weight. We then display its support, find its coordinates with respect to the basis and then confirm it by way of re-construction.

```magma
> C := EvenWeightCode(5);
> C![1,1,0,1,0];

>> C![1,1,0,1,0];
    ^
Runtime error in '!': Result is not in the given structure
> c := C![1,1,0,1,1];
> c;
(1 1 0 1 1)
> Support(c);
{ 1, 2, 4, 5 }
> Coordinates(C,c);
[ 1, 1, 0, 1 ]
> C.1 + C.2 + C.4;
(1 1 0 1 1)
```

## Predicates for Codewords

<a id="operation-op-eq-modtuprngelt-modtuprngelt"></a>
### `u eq v: ModTupRngElt, ModTupRngElt -> BoolElt`

The function returns `true` if and only if the codewords $u$ and $v$ are equal.

<a id="operation-op-ne-modtuprngelt-modtuprngelt"></a>
### `u ne v: ModTupRngElt, ModTupRngElt -> BoolElt`

The function returns `true` if and only if the codewords $u$ and $v$ are not equal.

<a id="function-iszero-modtuprngelt"></a>
### `IsZero(u): ModTupRngElt -> BoolElt`

The function returns `true` if and only if the codeword $u$ is the zero vector.

## Accessing Components of a Codeword

<a id="literal-literal-u-i-modtuprngelt-rngintelt-rngelt"></a>
### `u[i]: ModTupRngElt, RngIntElt -> RngElt`

Given a codeword $u$ belonging to the code $C$ defined over the ring $R$, return the $i$-th component of $u$ (as an element of $R$).

<a id="literal-literal-u-i-x"></a>
### `u[i] := x;`

Given an element $u$ belonging to a subcode $C$ of the full $R$-space $V = R^n$, a positive integer $i$, $1 \leq i\leq n$, and an element $x$ of $R$, this function returns a vector in $V$ which is $u$ with its $i$-th component redefined to be $x$.
