Codes and designs

Error-correcting codes

Add redundancy to a message so that a few corrupted symbols can be found and fixed. A linear code is a subspace of Zpn\mathbb{Z}_p^n; its minimum distance says how many errors it can correct, and its parity-check matrix says where they are.

The Hamming code

import { hammingCode, isPerfect } from "@ratmath/discrete";

const h = hammingCode(3);         // [7, 4] over Z_2
h.minimumDistance().distance;     // 3: corrects one error
isPerfect(h);                     // true
const cw = h.encode([1, 0, 1, 1]);   // [1, 0, 1, 1, 0, 1, 0]
h.decode([1, 0, 0, 1, 0, 1, 0]).steps;
// ["received 1001010", "syndrome H·rᵀ = 110",
//  "the lightest error with that syndrome is 0010000 (weight 1)", "corrected codeword 1011010, message 1011"]

A code with generator matrix GG (k×nk \times n) encodes mm as mGmG. Put GG in systematic form [Ik∣A][I_k \mid A]; then H=[−A⊤∣In−k]H = [-A^{\top} \mid I_{n-k}] satisfies GH⊤=0G H^{\top} = 0, and a word rr is a codeword exactly when its syndrome Hr⊤Hr^{\top} is zero. If r=c+er = c + e, the syndrome is He⊤He^{\top}, so decoding is finding the lightest error with that syndrome, the coset leader. In the Hamming code the columns of HH are all non-zero vectors, so the syndrome of a single error is its position written in binary.

Distance and bounds

A code with minimum distance dd corrects t=⌊(d−1)/2⌋t = \lfloor (d-1)/2 \rfloor errors, because balls of radius tt round the codewords do not overlap. Counting the words in those balls gives the Hamming (sphere-packing) bound, and a code that meets it is perfect:

∣C∣⋅∑i=0t(ni)(q−1)i≤qn. |C| \cdot \sum_{i=0}^{t} \binom{n}{i}(q-1)^i \le q^n.
import { codeBounds, ballSize } from "@ratmath/discrete";
codeBounds(7, 3, 2);   // { hamming: 16n, singleton: 32n, gilbertVarshamov: 5n }
ballSize(7, 1);        // 8n, and 16 · 8 = 2^7: the Hamming code is perfect
h.weightDistribution();             // [1, 0, 0, 7, 7, 0, 0, 1]
h.dual().minimumDistance().distance;   // 4: the [7, 3] simplex code

Reed–Solomon

import { reedSolomon } from "@ratmath/discrete";
const rs = reedSolomon({ p: 11, n: 10, k: 4 });   // corrects 3 errors
const w = rs.encode([3, 1, 4, 1]);                // the values of 3 + x + 4x² + x³ at 1, …, 10
const y = w.slice(); y[0] += 1; y[5] += 3; y[9] += 7;   // three errors (mod 11)
rs.decode(y.map((v) => v % 11)).errors;           // [0, 5, 9]

A message is a polynomial ff of degree below kk, sent as its values at nn distinct points. Two such polynomials agree in at most k−1k - 1 places, so the distance is n−k+1n - k + 1, meeting the Singleton bound. Berlekamp–Welch decoding looks for an error locator EE of degree ee (monic) and QQ of degree below e+ke + k with Q(αi)=yiE(αi)Q(\alpha_i) = y_i E(\alpha_i) for every ii: a linear system over Zp\mathbb{Z}_p. Then f=Q/Ef = Q / E, and EE vanishes at the error positions. The steps show the system, EE, QQ and the division.

Try itEdit the code and run it with @ratmath/discrete and Core's Rational in scope. The last line's value is shown; PRINT(…) adds lines, TEX(…) typesets math.
Ctrl/⌘ + Enter runs