---
title: "Construction of Additive Codes"
canonical: "https://docs.magma-maths.org/v2.30-1/CodingTheory/AdditiveCodes/creation.html"
source: "https://docs.magma-maths.org/v2.30-1/CodingTheory/AdditiveCodes/creation.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": "Introduction", "source": "https://docs.magma-maths.org/v2.30-1/CodingTheory/AdditiveCodes/introduction.md"}
next: {"title": "Invariants of an Additive Code", "source": "https://docs.magma-maths.org/v2.30-1/CodingTheory/AdditiveCodes/access.md"}
intrinsics:
  - {"name": "AdditiveCode", "signature": "AdditiveCode<F, K, n | L>: FldFin, RngIntElt, List -> Code"}
  - {"name": "AdditiveCode", "signature": "AdditiveCode(G): ModMatRngElt -> CodeAdd"}
  - {"name": "AdditiveCode", "signature": "AdditiveCode(K, G): FldFin, ModMatRngElt -> CodeAdd"}
  - {"name": "AdditiveCode", "signature": "AdditiveCode(K, C): FldFin, Code -> CodeAdd"}
  - {"name": "AdditiveZeroCode", "signature": "AdditiveZeroCode(F, K, n): FldFin, FldFin, RngIntElt -> CodeAdd"}
  - {"name": "AdditiveRepetitionCode", "signature": "AdditiveRepetitionCode(F, K, n): FldFin, FldFin, RngIntElt -> Code"}
  - {"name": "AdditiveZeroSumCode", "signature": "AdditiveZeroSumCode(F, K, n): FldFin, FldFin, RngIntElt -> Code"}
  - {"name": "AdditiveUniverseCode", "signature": "AdditiveUniverseCode(F, K, n): FldFin, FldFin, RngIntElt -> Code"}
  - {"name": "RandomAdditiveCode", "signature": "RandomAdditiveCode(F, K, n, k): FldFin, FldFin, RngIntElt, RngIntElt -> Code"}
examples:
  - "ex-f9cba7"
  - "ex-9d93b8"
  - "ex-8b74a8"
  - "ex-770cac"
  - "ex-24f23c"
---

# Construction of Additive Codes

## Construction of General Additive Codes

<a id="constructor-constructor-additivecode-fldfin-rngintelt-list-code"></a>
### `AdditiveCode<F, K, n | L>: FldFin, RngIntElt, List -> Code`

Create the $K$-additive code in $F^{(n)}$ of length $n$ which is generated by the elements specified by the list $L$, where $K$ is a subfield of $F$ and $L$ is one or more items of the following types:

**(a)**
An element of $F^{(n)}$;

**(b)**
A set or sequence of elements of $F^{(n)}$;

**(c)**
A sequence of $n$ elements of $F$, defining an element of $F^{(n)}$;

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

**(e)**
A subcode of $F^{(n)}$;

<a id="function-additivecode-modmatrngelt"></a>
### `AdditiveCode(G): ModMatRngElt -> CodeAdd`

<a id="function-additivecode-fldfin-modmatrngelt"></a>
### `AdditiveCode(K, G): FldFin, ModMatRngElt -> CodeAdd`

Given a matrix $G$ over a field $F$ and a subfield $K$ of $F$, return the $K$-additive code over $F$ generated by the rows of $G$. If no coefficient field $K$ is specified, then the prime field of $F$ is used.

<a id="example-ex-f9cba7"></a>
### `Example: Linear and Additive Codes Compared (ex-f9cba7)`

Starting with two linearly independent vectors in ${\mathbb{F}}_4^{(3)}$, we compare the linear code over $F_4$ they generate with the corresponding $F_2$-additive code.

```magma
> F<w> := GF(4);
> G := Matrix(F, 2, 3, [1,0,w^2,0,w,0]);
> G;
[  1   0 w^2]
[  0   w   0]
> C1 := LinearCode(G);
> C2 := AdditiveCode(GF(2), G);
> #C1;
16
> #C2;
4
> C2 subset C1;
true
```

The codewords of $C_2$ are arise only through addition of the generators: scalar multiplication is not permitted.

```magma
> { v : v in C2 };
{
    (  1   w w^2),
    (  0   0   0),
    (  1   0 w^2),
    (  0   w   0)
}
```

<a id="example-ex-9d93b8"></a>
### `Example: Additive Code from a Matrix (ex-9d93b8)`

We define an ${\bf F}_{2}$-additive code over ${\bf F}_{8}$ by constructing a random matrix and considering the code generated by its rows. Note that the number of generators exceeds the length of the code.

```magma
> K<w> := GF(8);
> M := KMatrixSpace(K, 5, 4);
> C := AdditiveCode(GF(2), Random(M));
> C;
[4, 1 2/3 : 5] GF(2)-Additive Code over GF(2^3)
Generator matrix:
[  1   1 w^2   0]
[  w w^2   w   1]
[w^2 w^2 w^2   1]
[  0 w^4 w^4 w^5]
[  0   0   1   0]
> WeightDistribution(C);
[ <0, 1>, <1, 1>, <2, 2>, <3, 9>, <4, 19> ]
> C;
[4, 1 2/3 : 5, 1] GF(2)-Additive Code over GF(2^3)
Generator matrix:
[  1   1 w^2   0]
[  w w^2   w   1]
[w^2 w^2 w^2   1]
[  0 w^4 w^4 w^5]
[  0   0   1   0]
```

<a id="function-additivecode-fldfin-code"></a>
### `AdditiveCode(K, C): FldFin, Code -> CodeAdd`

Given a code (linear or additive) $C$ over some finite field $F$, and a subfield $K$ of $F$ such that the wordset of $C$ forms a $K$-linear subspace, then return $C$ as a $K$-additive code.

<a id="example-ex-8b74a8"></a>
### `Example: Additive Code from a Linear Code (ex-8b74a8)`

Any linear code can be regarded as an additive code with respect to a subfield of its alphabet.

```magma
> C := RandomLinearCode(GF(4), 8, 3);
> C:Minimal;
[8, 3, 4] Linear Code over GF(2^2)
> A1 := AdditiveCode(GF(4), C);
> A1:Minimal;
[8, 3 : 3, 4] GF(2^2)-Additive Code over GF(2^2)
> { v : v in C } eq {v : v in A1 };
true
>
> A2 := AdditiveCode(GF(2), C);
> A2:Minimal;
[8, 3 : 6, 4] GF(2)-Additive Code over GF(2^2)
> { v : v in C } eq {v : v in A2 };
true
```

<a id="example-ex-770cac"></a>
### `Example: Viewing an Additive Code over a Subfield (ex-770cac)`

A $K$-additive code over $F$ can be viewed as an $E$-additive code for any subfield $E \subseteq K$.

```magma
> C4 := RandomAdditiveCode(GF(16), GF(4), 8, 5);
> C4:Minimal;
[8, 2 1/2 : 5] GF(2^2)-Additive Code over GF(2^4)
>
> C2 := AdditiveCode(GF(2), C4);
> C2:Minimal;
[8, 2 1/2 : 10] GF(2)-Additive Code over GF(2^4)
> { v : v in C2 } eq {v : v in C4 };
true
```

But for any $E$ such that $K \subset E \subseteq F$ we can create an $E$-additive code if and only if the wordset is in fact an $E$-linear subspace.

```magma
> C2:Minimal;
[8, 2 1/2 : 10] GF(2)-Additive Code over GF(2^4)
> A1 := AdditiveCode(GF(4), C2);
> A1 eq C4;
true
> A2 := AdditiveCode(GF(16), C2);

>> A2 := AdditiveCode(GF(16), C2);
                     ^
Runtime error in 'AdditiveCode': Code is not additive over given field
```

## Some Trivial Additive Codes

<a id="function-additivezerocode-fldfin-fldfin-rngintelt"></a>
### `AdditiveZeroCode(F, K, n): FldFin, FldFin, RngIntElt -> CodeAdd`

Given a field $F$ and subfield $K \subseteq F$ along with a positive integer $n$, return the $[n,0,n]$ code consisting of only the zero code word, (where the minimum weight is by convention equal to $n$).

<a id="function-additiverepetitioncode-fldfin-fldfin-rngintelt"></a>
### `AdditiveRepetitionCode(F, K, n): FldFin, FldFin, RngIntElt -> Code`

Given a field $F$ and subfield $K \subseteq F$ along with a positive integer $n$, return the $[n,1,n]$ code consisting of all repeating codewords.

<a id="function-additivezerosumcode-fldfin-fldfin-rngintelt"></a>
### `AdditiveZeroSumCode(F, K, n): FldFin, FldFin, RngIntElt -> Code`

Given a field $F$ and subfield $K \subseteq F$ along with a positive integer $n$, return the $[n,n-1,2]$ $K$-additive code over $F$ such that for all codewords $(c_1,c_2,\ldots ,c_n)$, we have $\sum_{i} c_i =0$.

<a id="function-additiveuniversecode-fldfin-fldfin-rngintelt"></a>
### `AdditiveUniverseCode(F, K, n): FldFin, FldFin, RngIntElt -> Code`

Given a field $F$ and subfield $K \subseteq F$ along with a positive integer $n$, return the $[n,n,1]$ $K$-additive code over $F$ consisting of all possible codewords.

<a id="function-randomadditivecode-fldfin-fldfin-rngintelt-rngintelt"></a>
### `RandomAdditiveCode(F, K, n, k): FldFin, FldFin, RngIntElt, RngIntElt -> Code`

Given a field $F$ and subfield $K \subseteq F$ along with positive integers $n$ and $k$, such that $0 < k \le n*[F:K]$, and $k$, return a random $K$-additive code of length $n$ and $k$ generators over the field $F$.

<a id="example-ex-24f23c"></a>
### `Example: Chain of Trivial Additive Codes (ex-24f23c)`

Over any finite field chain $K \subseteq F$, the zero code of length $n$ is contained in every code of length $n$, and similarly every code of length $n$ is contained in the universe code of length $n$.

```magma
> F := GF(9);
> K := GF(3);
> U := AdditiveUniverseCode(F, K, 5);
> Z := AdditiveZeroCode(F, K, 5);
> R := RandomAdditiveCode(F, K, 5, 2);
> (Z subset R) and (R subset U);
true
```
