---
title: "Weight Distributions"
canonical: "https://docs.magma-maths.org/v2.30-1/CodingTheory/LinearCodesOverZ4/weight-distribution.html"
source: "https://docs.magma-maths.org/v2.30-1/CodingTheory/LinearCodesOverZ4/weight-distribution.md"
part: "Coding Theory"
chapter: "Linear Codes over the Integer Residue Ring ${\\mathbb{Z}}_4$"
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 the Integer Residue Ring ${\\mathbb{Z}}_4$", "source": "https://docs.magma-maths.org/v2.30-1/CodingTheory/LinearCodesOverZ4/index-linear-codes-over-z4.md"}
prev: {"title": "Invariants", "source": "https://docs.magma-maths.org/v2.30-1/CodingTheory/LinearCodesOverZ4/z4-invar.md"}
next: {"title": "Weight Enumerators", "source": "https://docs.magma-maths.org/v2.30-1/CodingTheory/LinearCodesOverZ4/weight-enumerator.md"}
intrinsics:
  - {"name": "MinimumWeight", "signature": "MinimumWeight(C): Code -> RngIntElt"}
  - {"name": "MinimumDistance", "signature": "MinimumDistance(C): Code -> RngIntElt"}
  - {"name": "WeightDistribution", "signature": "WeightDistribution(C): Code -> [ <RngIntElt, RngIntElt> ]"}
  - {"name": "DualWeightDistribution", "signature": "DualWeightDistribution(C): Code -> [ <RngIntElt, RngIntElt> ]"}
  - {"name": "LeeWeight", "signature": "LeeWeight(a): RngIntRes -> RngIntElt"}
  - {"name": "LeeWeight", "signature": "LeeWeight(v): ModTupRngElt -> RngIntElt"}
  - {"name": "LeeDistance", "signature": "LeeDistance(u, v): ModTupRngElt, ModTupRngElt -> RngIntElt"}
  - {"name": "MinimumLeeWeight", "signature": "MinimumLeeWeight(C): Code -> RngIntElt"}
  - {"name": "MinimumLeeDistance", "signature": "MinimumLeeDistance(C): Code -> RngIntElt"}
  - {"name": "LeeWeightDistribution", "signature": "LeeWeightDistribution(C): Code -> SeqEnum"}
  - {"name": "DualLeeWeightDistribution", "signature": "DualLeeWeightDistribution(C): Code -> SeqEnum"}
  - {"name": "WordsOfLeeWeight", "signature": "WordsOfLeeWeight(C, w): Code, RngIntElt -> SetEnum"}
  - {"name": "WordsOfBoundedLeeWeight", "signature": "WordsOfBoundedLeeWeight(C, l, u): Code, RngIntElt, RngIntElt -> SetEnum"}
  - {"name": "EuclideanWeight", "signature": "EuclideanWeight(a): RngIntRes -> RngIntElt"}
  - {"name": "EuclideanWeight", "signature": "EuclideanWeight(v): ModTupRngElt -> RngIntElt"}
  - {"name": "EuclideanDistance", "signature": "EuclideanDistance(u, v): ModTupRngElt, ModTupRngElt -> RngIntElt"}
  - {"name": "MinimumEuclideanWeight", "signature": "MinimumEuclideanWeight(C): Code -> RngIntElt"}
  - {"name": "MinimumEuclideanDistance", "signature": "MinimumEuclideanDistance(C): Code -> RngIntElt"}
  - {"name": "EuclideanWeightDistribution", "signature": "EuclideanWeightDistribution(C): Code -> SeqEnum"}
  - {"name": "DualEuclideanWeightDistribution", "signature": "DualEuclideanWeightDistribution(C): Code -> SeqEnum"}
examples:
  - "ex-601dad"
  - "ex-587036"
  - "ex-9f8213"
---

# Weight Distributions

In the case of a linear code, weight and distance distributions are equivalent (in particular minimum weight and minimum distance are equivalent).

## Hamming Weight

For an element $x\in R$ for any finite ring $R$, the *Hamming weight* $w_H(x)$ is defined by:

$$
w_H(x) = 0 \iff x = 0, \qquad w_H(x) = 1 \iff x \ne 0
$$

The *Hamming weight* $w_H(v)$ of a vector $v\in {R^n}$ is defined to be the sum (in ${\mathbb{Z}}$) of the Hamming weights of its components.

The *Hamming weight* is often referred to as simply the *weight*.

<a id="function-minimumweight-code"></a>
### `MinimumWeight(C): Code -> RngIntElt`

<a id="function-minimumdistance-code"></a>
### `MinimumDistance(C): Code -> RngIntElt`

Determine the minimum (Hamming) weight of the words belonging to the code $C$, which is also the minimum distance between any two codewords.

<a id="function-weightdistribution-code"></a>
### `WeightDistribution(C): Code -> [ <RngIntElt, RngIntElt> ]`

Determine the (Hamming) weight distribution for the code $C$. The distribution is returned in the form of a sequence of tuples, where the $i$-th tuple contains the $i$-th weight, $w_i$ say, and the number of codewords having weight $w_i$.

<a id="function-dualweightdistribution-code"></a>
### `DualWeightDistribution(C): Code -> [ <RngIntElt, RngIntElt> ]`

Determine the (Hamming) weight distribution of the dual code of $C$. The distribution is returned in the form of a sequence of tuples, where the $i$-th tuple contains the $i$-th weight, $w_i$ say, and the number of codewords having weight $w_i$.

<a id="example-ex-601dad"></a>
### `Example: Weight Distribution of a Quadratic Residue Code (ex-601dad)`

In this example, the weight distribution of a quadratic residue code over ${\mathbb{Z}}_4$ and its dual are computed.

```magma
> C := QRCodeZ4(17);
> C;

((17, 4^9 2^0)) Cyclic Linear Code over IntegerRing(4)

Generator matrix:
[1 0 0 0 0 0 0 0 0 1 1 3 0 3 0 3 1]
[0 1 0 0 0 0 0 0 0 3 0 2 3 1 3 1 2]
[0 0 1 0 0 0 0 0 0 2 1 2 2 1 1 1 3]
[0 0 0 1 0 0 0 0 0 1 3 0 2 1 1 0 2]
[0 0 0 0 1 0 0 0 0 2 3 1 0 0 1 3 2]
[0 0 0 0 0 1 0 0 0 2 0 1 1 2 0 3 1]
[0 0 0 0 0 0 1 0 0 3 1 1 1 2 2 1 2]
[0 0 0 0 0 0 0 1 0 2 1 3 1 3 2 0 3]
[0 0 0 0 0 0 0 0 1 1 3 0 3 0 3 1 1]

> WeightDistribution(C);
[ <0, 1>, <5, 34>, <6, 68>, <7, 748>, <8, 2567>, <9, 6817>, <10, 17612>,
  <11, 34340>, <12, 50014>, <13, 56168>, <14, 50728>, <15, 30872>,
  <16, 9826>, <17, 2349> ]

> DualWeightDistribution(C);
[ <0, 1>, <6, 68>, <8, 935>, <9, 1632>, <10, 4148>, <11, 8568>, <12, 12886>,
  <13, 14280>, <14, 11968>, <15, 7752>, <16, 2890>, <17, 408> ]
```

<a id="lee"></a>
<a id="sec-lee-weight"></a>
## Lee Weight

For an element $x\in{\mathbb{Z}}_4$, the *Lee weight* $w_L(x)$ is defined by:

$$
w_L(0) = 0,\quad w_L(1) = w_L(3) = 1,\quad w_L(2) = 2.
$$

The *Lee weight* $w_L(v)$ of a vector $v\in{{\mathbb{Z}}_4^n}$ is defined to be the sum (in ${\mathbb{Z}}$) of the Lee weights of its components. See [[Wan97](https://docs.magma-maths.org/v2.30-1/references.md#cite-wan-z4), p. 16].

<a id="function-leeweight-rngintres"></a>
### `LeeWeight(a): RngIntRes -> RngIntElt`

The Lee weight of the element $a\in{\mathbb{Z}}_4$.

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

The Lee weight of the codeword $v$.

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

The Lee distance between the codewords $u$ and $v$, where $u$ and $v$ belong to the same code $C$. This is defined to be the Lee weight of $(u - v)$.

<a id="function-minimumleeweight-code"></a>
### `MinimumLeeWeight(C): Code -> RngIntElt`

<a id="function-minimumleedistance-code"></a>
### `MinimumLeeDistance(C): Code -> RngIntElt`

The minimum Lee weight of the code $C$.

<a id="function-leeweightdistribution-code"></a>
### `LeeWeightDistribution(C): Code -> SeqEnum`

The Lee weight distribution of the code $C$.

<a id="function-dualleeweightdistribution-code"></a>
### `DualLeeWeightDistribution(C): Code -> SeqEnum`

The Lee weight distribution of the dual of the code $C$ (see `LeeWeightDistribution`)

<a id="function-wordsofleeweight-code-rngintelt"></a>
### `WordsOfLeeWeight(C, w): Code, RngIntElt -> SetEnum`

```magma
NumWords: RngIntElt                    Default: Infinity()
```

Given a linear code $C$, return the set of all words of $C$ having Lee weight $w$. If `NumWords` is set to a non-negative integer $c$, then the algorithm will terminate after a total of $c$ words have been found.

<a id="function-wordsofboundedleeweight-code-rngintelt-rngintelt"></a>
### `WordsOfBoundedLeeWeight(C, l, u): Code, RngIntElt, RngIntElt -> SetEnum`

```magma
NumWords: RngIntElt                    Default: Infinity()
```

Given a linear code $C$, return the set of all words of $C$ having Lee weight between $l$ and $u$, inclusive. If `NumWords` is set to a non-negative integer $c$, then the algorithm will terminate after a total of $c$ words have been found.

<a id="example-ex-587036"></a>
### `Example: Lee Weight Distribution (ex-587036)`

We calculate the Lee weight distribution of a Reed Muller code over ${\mathbb{Z}}_4$ and enumerate all words of Lee weight $8$.

```magma
> C := ReedMullerCodeZ4(1, 3);
> C;
(8, 256, 4) Linear Code over IntegerRing(4)
Generator matrix:
[1 0 0 0 3 1 2 1]
[0 1 0 0 2 1 1 3]
[0 0 1 0 1 1 3 2]
[0 0 0 1 3 2 3 3]
> LeeWeightDistribution(C);
[ <0, 1>, <6, 112>, <8, 30>, <10, 112>, <16, 1> ]
> W := WordsOfLeeWeight(C, 8);
> #W;
30
```

<a id="euclidean"></a>
<a id="sec-euclidean-weight"></a>
## Euclidean Weight

For an element $x\in{\mathbb{Z}}_4$, the *Euclidean weight* $w_E(x)$ is defined by:

$$
w_E(0) = 0,\quad w_E(1) = w_E(3) = 1, \quad w_E(2) = 4.
$$

The *Euclidean weight* $w_E(v)$ of a vector $v\in{{\mathbb{Z}}_4^n}$ is defined to be the sum (in ${\mathbb{Z}}$) of the Euclidean weights of its components. See [[Wan97](https://docs.magma-maths.org/v2.30-1/references.md#cite-wan-z4), p. 16].

<a id="function-euclideanweight-rngintres"></a>
### `EuclideanWeight(a): RngIntRes -> RngIntElt`

The Euclidean weight of the element $a\in{\mathbb{Z}}4$.

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

The Euclidean weight of the ${\mathbb{Z}}_4$-codeword $v$.

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

The Euclidean distance between the ${\mathbb{Z}}_4$-codewords $u$ and $v$, where $u$ and $v$ belong to the same code $C$. This is defined to be the Euclidean weight of $(u - v)$.

<a id="function-minimumeuclideanweight-code"></a>
### `MinimumEuclideanWeight(C): Code -> RngIntElt`

<a id="function-minimumeuclideandistance-code"></a>
### `MinimumEuclideanDistance(C): Code -> RngIntElt`

The minimum Euclidean weight of the ${\mathbb{Z}}_4$-code C.

<a id="function-euclideanweightdistribution-code"></a>
### `EuclideanWeightDistribution(C): Code -> SeqEnum`

The Euclidean weight distribution of the ${\mathbb{Z}}_4$-code C.

<a id="function-dualeuclideanweightdistribution-code"></a>
### `DualEuclideanWeightDistribution(C): Code -> SeqEnum`

The Euclidean weight distribution of the dual of the ${\mathbb{Z}}_4$-code C.

<a id="example-ex-9f8213"></a>
### `Example: Euclidean Weight Distribution (ex-9f8213)`

The Euclidean weight distribution is calculated for a quadratic residue code over ${\mathbb{Z}}_4$

```magma
> C := QRCodeZ4(17);
> C;
(17, 262144) Cyclic Code over IntegerRing(4)
Generator matrix:
[1 0 0 0 0 0 0 0 0 1 1 3 0 3 0 3 1]
[0 1 0 0 0 0 0 0 0 3 0 2 3 1 3 1 2]
[0 0 1 0 0 0 0 0 0 2 1 2 2 1 1 1 3]
[0 0 0 1 0 0 0 0 0 1 3 0 2 1 1 0 2]
[0 0 0 0 1 0 0 0 0 2 3 1 0 0 1 3 2]
[0 0 0 0 0 1 0 0 0 2 0 1 1 2 0 3 1]
[0 0 0 0 0 0 1 0 0 3 1 1 1 2 2 1 2]
[0 0 0 0 0 0 0 1 0 2 1 3 1 3 2 0 3]
[0 0 0 0 0 0 0 0 1 1 3 0 3 0 3 1 1]
> EuclideanWeightDistribution(C);
[ <0, 1>, <7, 136>, <8, 170>, <9, 170>, <10, 408>, <11, 544>, <12, 986>,
<13, 1768>, <14, 3128>, <15, 5032>, <16, 6120>, <17, 6360>, <18, 8432>,
<19, 12512>, <20, 12682>, <21, 11152>, <22, 14416>, <23, 17680>, <24, 16048>,
<25, 15164>, <26, 17952>, <27, 16864>, <28, 13328>, <29, 14144>, <30, 14144>,
<31, 10064>, <32, 7837>, <33, 8024>, <34, 6800>, <35, 4896>, <36, 3485>,
<37, 2992>, <38, 2992>, <39, 1768>, <40, 510>, <41, 1258>, <42, 1224>,
<44, 238>, <45, 408>, <46, 136>, <47, 136>, <48, 34>, <68, 1> ]
```
