1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
//! Krawtchouk polynomials `K_k(x; n, q)` in exact arithmetic.
//!
//! These are the eigenvalues of the Hamming association scheme `H(n, q)`: the
//! `k`-th eigenvalue of the adjacency matrix of "distance `k`" on `GF(q)^n`,
//! evaluated at `x`, is `K_k(x; n, q)`. Everything the Delsarte linear
//! programme and the MacWilliams identity do is expressed in them, so they are
//! computed exactly and never in floating point — a rounded eigenvalue turns a
//! *bound* into an estimate.
//!
//! The definition used here is
//!
//! ```text
//! k
//! K_k(x; n,q) = Σ (-1)^j (q-1)^(k-j) C(x, j) C(n-x, k-j)
//! j=0
//! ```
//!
//! with `C(a, j)` the generalised binomial `a(a-1)…(a-j+1) / j!`, so `x` may be
//! any integer (values outside `0..=n` are meaningful and are used when
//! checking the recurrence at its ends). Equivalently `K_k(x; n, q)` is the
//! coefficient of `z^k` in `(1 + (q-1)z)^(n-x) (1 - z)^x`, which is where the
//! identities below come from.
//!
//! # What is here
//!
//! * [`krawtchouk`] — the value at an integer point, as a [`rug::Integer`].
//! * [`krawtchouk_poly`] — the coefficients of `K_k` as a polynomial in `x`,
//! ascending, as [`rug::Rational`]s. The polynomial has rational (not
//! integer) coefficients in general: `K_2(x; 2, 2) = 1 - 4x + 2x^2` is
//! integral, but `K_2(x; 3, 2) = 1 - 4x + 2x^2` scaled by the `1/k!` in the
//! definition is not in general, and the leading coefficient is
//! `(-q)^k / k!`.
//!
//! # Identities this module is tested against
//!
//! * `K_k(0) = C(n, k) (q-1)^k`.
//! * `K_0(x) = 1` and `K_1(x) = (q-1)n - qx`.
//! * The three-term recurrence
//! `(k+1) K_{k+1}(x) = [k + (q-1)(n-k) - qx] K_k(x) - (q-1)(n-k+1) K_{k-1}(x)`,
//! which is also how [`krawtchouk_poly`] is built, so testing the two against
//! each other tests the recurrence against the closed form.
//! * Orthogonality:
//! `Σ_i C(n,i)(q-1)^i K_k(i) K_l(i) = q^n C(n,k) (q-1)^k δ_{kl}`.
//! * Reciprocity: `C(n,i)(q-1)^i K_k(i) = C(n,k)(q-1)^k K_i(k)`.
//! * `Σ_{k=0}^{n} K_k(i) = q^n [i = 0]` — the identity that makes the Delsarte
//! programme bounded.
//!
//! # Scope
//!
//! `q` is only an *alphabet size* here, not a field order: the Hamming scheme
//! `H(n, q)` is defined for any `q ≥ 2`, and so is the Delsarte bound
//! `A_q(n, d)`. Nothing in this file requires `q` to be a prime power. Values
//! for `k > n` are zero, which the definition already gives.
use Pow;
use ;
/// The generalised binomial coefficient `C(a, j) = a(a-1)…(a-j+1) / j!`.
///
/// Defined for any integer `a`, including negative ones; agrees with the usual
/// binomial when `0 ≤ j ≤ a` and is zero when `a` is a non-negative integer
/// smaller than `j`.
/// The ordinary binomial coefficient `C(n, k)`, zero when `k > n`.
/// `K_k(x; n, q)`, the Krawtchouk polynomial evaluated at the integer `x`.
///
/// Exact: every intermediate is a [`rug::Integer`]. `x` may lie outside
/// `0..=n`.
///
/// ```
/// use alkahest_cas::experimental::krawtchouk;
/// use rug::Integer;
///
/// // K_k(0) = C(n, k) (q-1)^k
/// assert_eq!(krawtchouk(3, 0, 7, 2), Integer::from(35));
/// // The [7,4] Hamming code's dual has a weight-4 word in every position:
/// // K_1(x; 7, 2) = 7 - 2x
/// assert_eq!(krawtchouk(1, 3, 7, 2), Integer::from(1));
/// ```
/// `K_k(· ; n, q)` as a polynomial in `x`: coefficients ascending, length
/// `min(k, n) + 1` (or `[0]` when `k > n`).
///
/// Built from the three-term recurrence
/// `(k+1) K_{k+1} = [k + (q-1)(n-k) - qx] K_k - (q-1)(n-k+1) K_{k-1}`,
/// which is why comparing it against [`krawtchouk`] — built from the closed
/// form — is a real test of both.
/// `Σ_{i=0}^{n} A_i K_k(i; n, q)` — one Delsarte dual-feasibility form,
/// evaluated exactly.
///
/// This is the quantity that must be non-negative for every `k` when `A` is the
/// distance distribution of *any* code over an alphabet of size `q`, linear or
/// not. It is the only place the Hamming scheme's eigenvalues meet a code in
/// this module, so both the MacWilliams transform and the linear programme go
/// through it.