---
title: "Weight Distributions"
canonical: "https://docs.magma-maths.org/v2.30-1/CodingTheory/LinearCodesOverFiniteRings/weight-distribution.html"
source: "https://docs.magma-maths.org/v2.30-1/CodingTheory/LinearCodesOverFiniteRings/weight-distribution.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": "Codeword Operations", "source": "https://docs.magma-maths.org/v2.30-1/CodingTheory/LinearCodesOverFiniteRings/element.md"}
next: {"title": "Weight Enumerators", "source": "https://docs.magma-maths.org/v2.30-1/CodingTheory/LinearCodesOverFiniteRings/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> ]"}
examples:
  - "ex-bb2d09"
---

# 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{\mathbb{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-bb2d09"></a>
### `Example: Weight Distribution of a Cyclic Code (ex-bb2d09)`

We calculate the weight distribution of a cyclic code over the Galois ring of size $81$.

```magma
> R<w> := GR(9,2);
> P<x> := PolynomialRing(R);
> L := CyclotomicFactors(R, 4);
> g := L[3] * L[4];
> g;
x^2 + (8*w + 7)*x + w + 1
> C := CyclicCode(4, g);
> C;
(4, 6561, 3) Cyclic Code over GaloisRing(3, 2, 2)
Generator matrix:
[      1       0   w + 1 8*w + 7]
[      0       1       w 8*w + 8]
> WeightDistribution(C);
[ <0, 1>, <3, 320>, <4, 6240> ]
```
