Algorithm Search⌘K

Proofs: Fast Walsh-Hadamard transform

These are the human proofs of Fast Walsh-Hadamard transform, at full length.

FWHT computes the Walsh-Hadamard transform, and INVERSE-FWHT inverts it full

This proof shows: correctness, complexity.

Let RR be a commutative ring, n=2tn = 2^t, and x∈Rnx \in R^n the input. For 0≤i,j<n0 \le i, j < n write iqi_q for the binary digit of ii at position qq, so ⟨i,j⟩=∑qiqjq\langle i, j \rangle = \sum_q i_q j_q and (Hn)ji=(−1)⟨i,j⟩(H_n)_{ji} = (-1)^{\langle i, j \rangle}. Write x′x' for the array as the algorithm changes it. For 0≤q≤t0 \le q \le t, let i∼qji \sim_q j mean that ii and jj agree in every position r≥qr \ge q, and let ⟨i,j⟩<q=∑r<qirjr\langle i, j \rangle_{<q} = \sum_{r < q} i_r j_r.

Invariant (I). When line L3 tests h=2qh = 2^q,

xj′=∑i∼qj(−1)⟨i,j⟩<q xifor each 0≤j<n.x'_j = \sum_{i \sim_q j} (-1)^{\langle i, j \rangle_{<q}} \, x_i \qquad \text{for each } 0 \le j < n.

Initialization. For q=0q = 0, i∼0ji \sim_0 j means i=ji = j, and the empty sum in the exponent is 00, so (I) says xj′=xjx'_j = x_j, which holds before the first pass.

Maintenance. Let (I) hold for h=2q<nh = 2^q < n. Lines L4 and L5 visit each jj with jq=0j_q = 0 exactly once: the block start ss is a multiple of 2h2h and s≤j<s+hs \le j < s + h. Its partner j+hj + h differs from jj only in position qq. The pairs {j,j+h}\{j, j + h\} are disjoint, and line L7 reads and writes only the two cells of one pair, so the new values are xj′′=xj′+xj+h′x''_j = x'_j + x'_{j+h} and xj+h′′=xj′−xj+h′x''_{j+h} = x'_j - x'_{j+h}, computed from the values of (I).

Fix jj with jq=0j_q = 0 and let j+=j+hj^+ = j + h. The set {i:i∼q+1j}\{i : i \sim_{q+1} j\} is the disjoint union of {i∼qj}\{i \sim_q j\} (those with iq=0i_q = 0) and {i∼qj+}\{i \sim_q j^+\} (those with iq=1i_q = 1). For i∼qj+i \sim_q j^+, ⟨i,j+⟩<q=⟨i,j⟩<q\langle i, j^+ \rangle_{<q} = \langle i, j \rangle_{<q} because jj and j+j^+ agree below qq. Hence

xj′′=∑i∼q+1j(−1)⟨i,j⟩<qxi=∑i∼q+1j(−1)⟨i,j⟩<q+1xi,x''_j = \sum_{i \sim_{q+1} j} (-1)^{\langle i, j \rangle_{<q}} x_i = \sum_{i \sim_{q+1} j} (-1)^{\langle i, j \rangle_{<q+1}} x_i,

since jq=0j_q = 0 makes the term iqjqi_q j_q zero, and

xj+′′=∑i∼qj, iq=0(−1)⟨i,j⟩<qxi−∑i∼qj+, iq=1(−1)⟨i,j⟩<qxi=∑i∼q+1j+(−1)⟨i,j+⟩<q+iqxi,x''_{j^+} = \sum_{i \sim_q j,\ i_q = 0} (-1)^{\langle i, j \rangle_{<q}} x_i - \sum_{i \sim_q j^+,\ i_q = 1} (-1)^{\langle i, j \rangle_{<q}} x_i = \sum_{i \sim_{q+1} j^+} (-1)^{\langle i, j^+ \rangle_{<q} + i_q} x_i,

and iq=iqjq+i_q = i_q j^+_q because jq+=1j^+_q = 1, so the exponent is ⟨i,j+⟩<q+1\langle i, j^+ \rangle_{<q+1}. Every index is jj or j+j^+ for exactly one pair, so (I) holds for 2h2h after line L8.

Termination. The loop ends at h=nh = n, q=tq = t. Then i∼tji \sim_t j holds for every pair, and ⟨i,j⟩<t=⟨i,j⟩\langle i, j \rangle_{<t} = \langle i, j \rangle, so xj′=∑i(−1)⟨i,j⟩xi=(Hnx)jx'_j = \sum_i (-1)^{\langle i, j \rangle} x_i = (H_n x)_j.

Theorem 1. FWHT(x)(x) replaces xx by HnxH_n x.

Proof. The invariant and its termination. □\square

Lemma 2. HnHn=nIH_n H_n = n I.

Proof. (HnHn)jk=∑i(−1)⟨i,j⟩+⟨i,k⟩=∑i(−1)⟨i,j⊕k⟩(H_n H_n)_{jk} = \sum_i (-1)^{\langle i, j \rangle + \langle i, k \rangle} = \sum_i (-1)^{\langle i, j \oplus k \rangle}, since jq+kq≡(j⊕k)q(mod2)j_q + k_q \equiv (j \oplus k)_q \pmod 2. If j=kj = k every term is 11 and the sum is nn. Otherwise pick a position rr with (j⊕k)r=1(j \oplus k)_r = 1. The map i↦i⊕2ri \mapsto i \oplus 2^r is an involution of {0,…,n−1}\{0, \ldots, n-1\} with no fixed points that changes the sign of each term. So the terms split into pairs {i,i⊕2r}\{i, i \oplus 2^r\} whose two terms cancel, and the sum is 00 in each ring. □\square

Theorem 2. If nn is a unit of RR, INVERSE-FWHT(Hnx)(H_n x) returns xx.

Proof. Line L9 computes HnHnx=nxH_n H_n x = n x by Theorem 1 and Lemma 2, and line L10 divides by nn. ■\blacksquare

Theorem 3 (XOR convolution). For a,b∈Rna, b \in R^n let ck=∑i⊕j=kaibjc_k = \sum_{i \oplus j = k} a_i b_j. Then Hnc=Hna⋅HnbH_n c = H_n a \cdot H_n b entrywise.

Proof. (Hnc)m=∑k(−1)⟨k,m⟩∑i⊕j=kaibj=∑i,jaibj(−1)⟨i⊕j,m⟩(H_n c)_m = \sum_k (-1)^{\langle k, m \rangle} \sum_{i \oplus j = k} a_i b_j = \sum_{i, j} a_i b_j (-1)^{\langle i \oplus j, m \rangle}, and (−1)⟨i⊕j,m⟩=(−1)⟨i,m⟩(−1)⟨j,m⟩(-1)^{\langle i \oplus j, m \rangle} = (-1)^{\langle i, m \rangle} (-1)^{\langle j, m \rangle} by the same parity identity. The double sum then factors as (Hna)m(Hnb)m(H_n a)_m (H_n b)_m. □\square

Operation count. The loop of line L3 runs for h=1,2,…,n/2h = 1, 2, \ldots, n/2, that is t=lg⁡nt = \lg n times. Each pass visits n/(2h)n/(2h) blocks of hh pairs, so n/2n/2 pairs, and line L7 does one addition and one subtraction per pair. In total nlg⁡nn \lg n additions and subtractions and no multiplications; INVERSE-FWHT adds nn divisions.

Back to Fast Walsh-Hadamard transform