---
title: "Operations on Codewords"
canonical: "https://docs.magma-maths.org/v2.30-1/CodingTheory/AdditiveCodes/element.html"
source: "https://docs.magma-maths.org/v2.30-1/CodingTheory/AdditiveCodes/element.md"
part: "Coding Theory"
chapter: "Additive Codes"
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": "Additive Codes", "source": "https://docs.magma-maths.org/v2.30-1/CodingTheory/AdditiveCodes/index-additive-codes.md"}
prev: {"title": "Invariants of an Additive Code", "source": "https://docs.magma-maths.org/v2.30-1/CodingTheory/AdditiveCodes/access.md"}
next: {"title": "Subcodes", "source": "https://docs.magma-maths.org/v2.30-1/CodingTheory/AdditiveCodes/subcode.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": "Distance", "signature": "Distance(u, v): ModTupRngElt, ModTupRngElt -> RngIntElt"}
  - {"name": "Weight", "signature": "Weight(u): ModTupRngElt -> RngIntElt"}
  - {"name": "(u, v)", "signature": "(u, v): ModTupRngElt, ModTupRngElt -> RngElt"}
  - {"name": "InnerProduct", "signature": "InnerProduct(u, v): ModTupRngElt, ModTupRngElt -> RngElt"}
  - {"name": "TraceInnerProduct", "signature": "TraceInnerProduct(K, u, v): FldFin, ModTupFldElt, ModTupFldElt -> FldFinElt"}
  - {"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;"}
---

# 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 length $n$ additive code $C$ with alphabet $F$, then the codewords of $C$ lie in $F^{(n)}$. Given elements $a_1, \ldots, a_n$ belonging to $F$, construct the codeword $(a_1, \ldots, a_n)$ of $C$. A check is made 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 an additive code $C$ which is defined as a subset of the $F$-space $V = F^{(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 additive code $C$.

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

A random codeword of the additive code $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 alphabet $F$, and a codeword $u$ belonging to the additive code $C$, return the codeword $a*u$.

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

Normalize a codeword $u$ of an additive code $C$, returning a scalar multiple of $u$ such that its first non-zero entry is $1$.

## 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 additive 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$.

## 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-traceinnerproduct-fldfin-modtupfldelt-modtupfldelt"></a>
### `TraceInnerProduct(K, u, v): FldFin, ModTupFldElt, ModTupFldElt -> FldFinElt`

Given vectors $u$ and $v$ defined over a finite field $L$ and a subfield $K$ of $L$, this function returns the trace of the inner product of the vectors $u$ and $v$ with respect to $K$.

<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\, :\,k_g]$ $K$-additive code $C$ and a codeword $u$ of $C$ return the coordinates of $u$ with respect to the current basis of $C$. The coordinates of $u$ are returned as a sequence $Q = [a_1, \ldots, a_{k_g}]$ of elements from $K$ such that $u = a_1 * C.1 + \ldots + a_{k_g} * C.{k_g}$.

<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$.

## 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$ belonging to the same additive code 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$ belonging to the same additive code 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$.
