---
title: "Subcodes"
canonical: "https://docs.magma-maths.org/v2.30-1/CodingTheory/LinearCodes/subcode.html"
source: "https://docs.magma-maths.org/v2.30-1/CodingTheory/LinearCodes/subcode.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": "Coset Leaders", "source": "https://docs.magma-maths.org/v2.30-1/CodingTheory/LinearCodes/coset-leader.md"}
next: {"title": "Properties of Codes", "source": "https://docs.magma-maths.org/v2.30-1/CodingTheory/LinearCodes/Boolean.md"}
intrinsics:
  - {"name": "sub", "signature": "sub<C | L>: Code, List -> Code"}
  - {"name": "Subcode", "signature": "Subcode(C, k): Code, RngIntElt -> Code"}
  - {"name": "Subcode", "signature": "Subcode(C, S): Code, { RngIntElt } -> Code"}
  - {"name": "SubcodeBetweenCode", "signature": "SubcodeBetweenCode(C1, C2, k): Code, Code, RngIntElt -> Code"}
  - {"name": "SubcodeWordsOfWeight", "signature": "SubcodeWordsOfWeight(C, S): Code, { RngIntElt } -> Code"}
  - {"name": "+", "signature": "C + D: Code, Code -> Code"}
  - {"name": "meet", "signature": "C meet D: Code, Code -> Code"}
  - {"name": "Dual", "signature": "Dual(C): Code -> Code"}
  - {"name": "HermitianDual", "signature": "HermitianDual(C): CodeLinFld -> CodeLinFld"}
  - {"name": "Hull", "signature": "Hull(C): CodeLinFld -> CodeLinFld"}
  - {"name": "HermitianHull", "signature": "HermitianHull(C): CodeLinFld -> CodeLinFld"}
  - {"name": "in", "signature": "u in C: ModTupRngElt, Code -> BoolElt"}
  - {"name": "notin", "signature": "u notin C: ModTupRngElt, Code -> BoolElt"}
  - {"name": "subset", "signature": "C subset D: Code, Code -> BoolElt"}
  - {"name": "notsubset", "signature": "C notsubset D: Code, Code -> BoolElt"}
  - {"name": "eq", "signature": "C eq D: Code, Code -> BoolElt"}
  - {"name": "ne", "signature": "C ne D: Code, Code -> BoolElt"}
examples:
  - "ex-db10dc"
  - "ex-377d6d"
  - "ex-cfd60d"
---

# Subcodes

## The Subcode Constructor

<a id="constructor-constructor-sub-code-list-code"></a>
### `sub<C | L>: Code, List -> Code`

Given an $[n, k]$ linear code $C$ over $R$, construct the subcode of $C$, generated by the elements specified by the list $L$, where $L$ is a list of one or more items of the following types:

**(a)**
An element of $C$;

**(b)**
A set or sequence of elements of $C$;

**(c)**
A sequence of $n$ elements of $R$, defining an element of $C$;

**(d)**
A set or sequence of sequences of type (c);

**(e)**
A subcode of $C$;

<a id="function-subcode-code-rngintelt"></a>
### `Subcode(C, k): Code, RngIntElt -> Code`

Given an $[n, k]$ linear code $C$ and an integer $t$, $1 \le t < n$, return a subcode of $C$ of dimension $t$.

<a id="function-subcode-code-rngintelt-2"></a>
### `Subcode(C, S): Code, { RngIntElt } -> Code`

Given an $[n, k]$ linear code $C$ and a set $S$ of integers, each of which lies in the range $[1,k]$, return the subcode of $C$ generated by the basis elements whose positions appear in $S$.

<a id="function-subcodebetweencode-code-code-rngintelt"></a>
### `SubcodeBetweenCode(C1, C2, k): Code, Code, RngIntElt -> Code`

Given a linear code $C_1$ and a subcode $C_2$ of $C_1$, return a subcode of $C_1$ of dimension $k$ containing $C_2$.

<a id="function-subcodewordsofweight-code-rngintelt"></a>
### `SubcodeWordsOfWeight(C, S): Code, { RngIntElt } -> Code`

Given an $[n, k]$ linear code $C$ and a set $S$ of integers, each of which lies in the range $[1,n]$, return the subcode of $C$ generated by those words of $C$ whose weights lie in $S$.

<a id="example-ex-db10dc"></a>
### `Example: Subcode between Two Codes (ex-db10dc)`

We give an example of how `SubcodeBetweenCode` may be used to create a code nested in between a subcode pair.

```magma
> C1 := RepetitionCode(GF(2),6);
> C1;
[6, 1, 6] Cyclic Code over GF(2)
Generator matrix:
[1 1 1 1 1 1]
> C3 := EvenWeightCode(6);
> C3;
[6, 5, 2] Linear Code over GF(2)
Generator matrix:
[1 0 0 0 0 1]
[0 1 0 0 0 1]
[0 0 1 0 0 1]
[0 0 0 1 0 1]
[0 0 0 0 1 1]
> C1 subset C3;
true
> C2 := SubcodeBetweenCode(C3, C1, 4);
> C2;
[6, 4, 2] Linear Code over GF(2)
Generator matrix:
[1 0 0 0 1 0]
[0 1 0 0 0 1]
[0 0 1 0 0 1]
[0 0 0 1 0 1]
> (C1 subset C2) and (C2 subset C3);
true
```

## Sum, Intersection, Duals, and Hulls

For the following operators, $C$ and $D$ are codes defined as subsets (or subspaces) of the same $R$-space $V$.

<a id="operation-op-plus-code-code"></a>
### `C + D: Code, Code -> Code`

The (vector space) sum of the linear codes $C$ and $D$, where $C$ and $D$ are contained in the same $K$-space $V$.

<a id="operation-op-meet-code-code"></a>
### `C meet D: Code, Code -> Code`

The intersection of the linear codes $C$ and $D$, where $C$ and $D$ are contained in the same $K$-space $V$.

<a id="function-dual-code-2"></a>
### `Dual(C): Code -> Code`

The dual $D$ of the linear code $C$. The dual consists of all codewords in the $K$-space $V$ which are orthogonal to all codewords of $C$.

<a id="example-ex-377d6d"></a>
### `Example: Sums and Intersections of Subcodes (ex-377d6d)`

Verify some simple results from the sum and intersection of subcodes with known basis.

```magma
> C := EvenWeightCode(5);
> C;
[5, 4, 2] Linear Code over GF(2)
Generator matrix:
[1 0 0 0 1]
[0 1 0 0 1]
[0 0 1 0 1]
[0 0 0 1 1]
> C1 := sub< C | C.1 >;
> C2 := sub< C | C.4 >;
> C3 := sub< C | { C.1 , C.4} >;
> (C1 + C2) eq C3;
true
> (C1 meet C3) eq C1;
true
```

<a id="example-ex-cfd60d"></a>
### `Example: Dual of a Reed-Solomon Code (ex-cfd60d)`

Verify the orthogonality of codewords in the dual for a `ReedSolomonCode`.

```magma
> K<w> := GF(8);
> R := ReedSolomonCode(K, 3);
> R;
[7, 5, 3] BCH code (d = 3, b = 1) over GF(2^3)
Generator matrix:
[  1   0   0   0   0 w^3 w^4]
[  0   1   0   0   0   1   1]
[  0   0   1   0   0 w^3 w^5]
[  0   0   0   1   0   w w^5]
[  0   0   0   0   1   w w^4]
> D := Dual(R);
> D;
[7, 2, 6] Cyclic Code over GF(2^3)
Generator matrix:
[  1   0 w^3   1 w^3   w   w]
[  0   1 w^4   1 w^5 w^5 w^4]
> {<u,v> : u in R, v in D | InnerProduct(u,v) ne 0};
{}
```

<a id="function-hermitiandual-codelinfld"></a>
### `HermitianDual(C): CodeLinFld -> CodeLinFld`

The Hermitian dual $D$ of the linear code $C$. The Hermitian dual consists of all codewords in the $K$-space $V$ which are orthogonal to all codewords of $C$ with respect to the Hermitian inner product for quadratic extensions.

<a id="function-hull-codelinfld"></a>
### `Hull(C): CodeLinFld -> CodeLinFld`

The (Euclidean) hull of the linear code $C$ is the intersection of the code with its dual.

<a id="function-hermitianhull-codelinfld"></a>
### `HermitianHull(C): CodeLinFld -> CodeLinFld`

The Hermitian hull of the linear code $C$ is the intersection of the code with its Hermitian dual.

## Membership and Equality

For the following operators, $C$ and $D$ are codes defined as a subset (or subspace) of the $R$-space $V$.

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

Return `true` if and only if the vector $u$ of $V$ belongs to the code $C$.

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

Return `true` if and only if the vector $u$ of $V$ does not belong to the code $C$.

<a id="operation-op-subset-code-code"></a>
### `C subset D: Code, Code -> BoolElt`

Return `true` if and only if the code $C$ is a subcode of the code $D$.

<a id="operation-operation-notsubset-code-code-boolelt"></a>
### `C notsubset D: Code, Code -> BoolElt`

Return `true` if and only if the code $C$ is not a subcode of the code $D$.

<a id="operation-op-eq-code-code"></a>
### `C eq D: Code, Code -> BoolElt`

Return `true` if and only if the codes $C$ and $D$ are equal.

<a id="operation-op-ne-code-code"></a>
### `C ne D: Code, Code -> BoolElt`

Return `true` if and only if the codes $C$ and $D$ are not equal.
