Codes and designs

Latin squares and designs

A design arranges points into blocks so that every pair of points meets equally often. Latin squares, finite geometries and Steiner triple systems are all designs, and their existence is a question about arithmetic in finite fields.

Latin squares

import { cyclicLatinSquare, isLatinSquare, orthogonalLatinSquares, areOrthogonal } from "@ratmath/discrete";

isLatinSquare(cyclicLatinSquare(6));   // { latin: true, problem: null }
const L = orthogonalLatinSquares(5);    // 4 squares: L_a[i][j] = a·i + j mod 5
areOrthogonal(L[0], L[1]);              // { orthogonal: true, repeated: null }

Each symbol appears once in every row and column. Two squares are orthogonal when superimposing them gives every ordered pair of symbols exactly once. For a prime pp, the squares La(i,j)=ai+jL_a(i, j) = ai + j with a=1,…,p−1a = 1, \dots, p-1 are mutually orthogonal, since ai+j=ai′+j′ai + j = ai' + j' and bi+j=bi′+j′bi + j = bi' + j' force i=i′i = i' when a≠ba \ne b. At most n−1n - 1 squares of order nn can be mutually orthogonal, so this is a complete set. Euler's 36 officers (n=6n = 6) have no orthogonal pair at all.

Block designs

import { blockDesign, projectivePlane, affinePlane, fromDifferenceSet, steinerTripleSystem } from "@ratmath/discrete";
const fano = blockDesign(projectivePlane(2));
[fano.v, fano.b, fano.k, fano.r, fano.lambda];   // [7, 7, 3, 3, 1]: the Fano plane
blockDesign(fromDifferenceSet([1, 3, 4, 5, 9], 11)).lambda;   // 2: the squares mod 11
blockDesign(steinerTripleSystem(15)).b;          // 35 triples

A (v,k,λ)(v, k, \lambda) design has vv points and blocks of size kk, with every pair of points in exactly λ\lambda blocks. Counting pairs gives the necessary conditions

bk=vr,r(k−1)=λ(v−1), bk = vr, \qquad r(k - 1) = \lambda (v - 1),

and Fisher's inequality b≥vb \ge v. blockDesign computes v,b,k,r,λv, b, k, r, \lambda from the blocks and checks these relations, returning lambda: null when pairs are not balanced.

Finite geometry

The projective plane of order pp has the one-dimensional subspaces of Zp3\mathbb{Z}_p^3 as points and the two-dimensional ones as lines: p2+p+1p^2 + p + 1 of each, p+1p + 1 points on a line, and any two points on exactly one line. Removing one line and its points leaves the affine plane, p2p^2 points in p2+pp^2 + p lines. A difference set D⊂ZvD \subset \mathbb{Z}_v, where every non-zero residue is a difference of elements of DD exactly λ\lambda times, gives a design with blocks D+iD + i. Steiner triple systems (k=3k = 3, λ=1\lambda = 1) exist exactly when v≡1v \equiv 1 or 3(mod6)3 \pmod 6; Bose's construction covers v≡3v \equiv 3.

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