---
title: "Codeword Operations"
canonical: "https://docs.magma-maths.org/v2.30-1/CodingTheory/LinearCodesOverFiniteRings/element.html"
source: "https://docs.magma-maths.org/v2.30-1/CodingTheory/LinearCodesOverFiniteRings/element.md"
part: "Coding Theory"
chapter: "Linear Codes over Finite Rings"
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 Rings", "source": "https://docs.magma-maths.org/v2.30-1/CodingTheory/LinearCodesOverFiniteRings/index-linear-codes-over-finite-rings.md"}
prev: {"title": "New Codes from Old", "source": "https://docs.magma-maths.org/v2.30-1/CodingTheory/LinearCodesOverFiniteRings/operation.md"}
next: {"title": "Weight Distributions", "source": "https://docs.magma-maths.org/v2.30-1/CodingTheory/LinearCodesOverFiniteRings/weight-distribution.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": "+", "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": "Weight", "signature": "Weight(v): ModTupRngElt -> RngIntElt"}
  - {"name": "Distance", "signature": "Distance(u, v): ModTupRngElt, ModTupRngElt -> RngIntElt"}
  - {"name": "Support", "signature": "Support(w): ModTupRngElt -> { RngIntElt }"}
  - {"name": "(u, v)", "signature": "(u, v): ModTupRngElt, ModTupRngElt -> RngElt"}
  - {"name": "InnerProduct", "signature": "InnerProduct(u, v): ModTupRngElt, ModTupRngElt -> RngElt"}
  - {"name": "Coordinates", "signature": "Coordinates(C, u): Code, ModTupRngElt -> [ RngFinElt ]"}
  - {"name": "Normalize", "signature": "Normalize(u): ModTupRngElt -> ModTupRngElt"}
  - {"name": "Rotate", "signature": "Rotate(u, k): ModTupRngElt, RngIntElt -> ModTupRngElt"}
  - {"name": "Rotate", "signature": "Rotate(~u, k): ModTupRngElt, RngIntElt"}
  - {"name": "Parent", "signature": "Parent(w): ModTupRngElt -> ModTupRng"}
  - {"name": "u", "signature": "u[i]: ModTupRngElt, RngIntElt -> RngElt"}
  - {"name": "[]", "signature": "u[i] := x;"}
examples:
  - "ex-c7f5f7"
  - "ex-38a7a5"
---

# Codeword Operations

## Construction

<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="example-ex-c7f5f7"></a>
### `Example: Creating Code Words (ex-c7f5f7)`

We create some elements of a code over a finite ring.

```magma
> R<w> := GR(16,2);
> P<x> := PolynomialRing(R);
> L := CyclotomicFactors(R, 7);
> C := CyclicCode(7, L[2]);
> C ! [1, 2*w, 0, w+3, 7*w, 12*w+3, w+3];
(       1      2*w        0    w + 3      7*w 12*w + 3    w + 3)
> elt< C | 0, 3, 0, 2*w + 5, 6*w + 9, 4*w + 5, 14*w + 14 >;
(        0         3         0   2*w + 5   6*w + 9   4*w + 5 14*w + 14)
```

If the given vector does not lie in the given code then an error will result.

```magma
> C ! [0,0,0,0,0,0,1];

>> C ! [0,0,0,0,0,0,1];
     ^
Runtime error in '!': Result is not in the given structure
> elt< C | 1, 0, 1, 0, 1, 0, 1>;

>> elt< C | 1, 0, 1, 0, 1, 0, 1>;
      ^
Runtime error in elt< ... >: Result is not in the lhs of the constructor
```

## Operations

<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 ring $R$, and a codeword $u$ belonging to the linear code $C$, return the codeword $a*u$.

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

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

<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-support-modtuprngelt"></a>
### `Support(w): ModTupRngElt -> { RngIntElt }`

Given a word $w$ belonging to the length $n$ 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="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-coordinates-code-modtuprngelt"></a>
### `Coordinates(C, u): Code, ModTupRngElt -> [ RngFinElt ]`

Given a length $n$ 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-normalize-modtuprngelt"></a>
### `Normalize(u): ModTupRngElt -> ModTupRngElt`

Given an element $u$ of a code defined over the ring $R$, return the normalization of $u$, which is the unique vector $v$ such that $v = a\cdot u$ for some scalar $a\in R$ such that the first non-zero entry of $v$ is the canonical associate in $R$ of the first non-zero entry of $u$ ($v$ is zero if $u$ is zero).

<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-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="example-ex-38a7a5"></a>
### `Example: Operations on Code Words (ex-38a7a5)`

Given a code over a finite ring, we explore various operations on its code words.

```magma
> R<w> := GR(4, 4);
> P<x> := PolynomialRing(R);
> g := x + 2*w^3 + 3*w^2 + w + 2;
> C := CyclicCode(3, g);
> C;
(3, 1048576) Cyclic Code over GaloisRing(2, 2, 4)
Generator matrix:
[          1           0     w^2 + w]
[          0           1 w^2 + w + 1]
[          0           0           2]
> u := C.1;
> v := C.2;
> u;
(      1       0 w^2 + w)
> v;
(          0           1 w^2 + w + 1)
> u + v;
(              1               1 2*w^2 + 2*w + 1)
> 2*u;
(          2           0 2*w^2 + 2*w)
> 4*u;
(0 0 0)
> Weight(u);
2
> Support(u);
{ 1, 3 }
```

## Accessing Components of a Codeword

<a id="indexed-indexed-u-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$.
