Fast Walsh-Hadamard transform through the lens of butterfly operations

An N×NN \times N Hadamard matrix for N=2kN = 2^k can be created from H2=[111−1]H_2 = \begin{bmatrix} 1 & 1 \\ 1 & -1 \end{bmatrix} using Kronecker product:

HN=H2⊗HN/2=[HN/2HN/2HN/2−HN/2]=H2⊗H2⊗⋯⊗H2⏟k timesH_N = H_2 \otimes H_{N/2} = \begin{bmatrix} H_{N/2} & H_{N/2} \\ H_{N/2} & -H_{N/2} \end{bmatrix} = \underbrace{H_2 \otimes H_2 \otimes \cdots \otimes H_2}_{k \text{ times}}

This matrix has some nice properties:

  • 1NHN\frac{1}{\sqrt{N}}H_N is orthonormal
  • 1NHN\frac{1}{\sqrt{N}}H_N is symmetric. Since it is also orthonormal, it is its own inverse.

There are also Hadamard matrices where NN is not a power of 22 but they are not covered here.


The matrix vector product is O(Nlog⁡N)O(N \log N)

At the heart of this product lies the butterfly transform (x,y)↦(x+y,x−y)(x, y) \mapsto (x + y, x - y). The matrix vector product HNxH_N x is essentially N/2×log⁡NN/2 \times \log N number of butterfly transforms:

def fwht(x):
    x = list(x)
    N = len(x)
    h = 1
    while h < N:
        for i in range(0, N, 2 * h):
            for j in range(i, i + h):
                a, b = x[j], x[j + h]
                x[j], x[j + h] = a + b, a - b
        h *= 2
    return x

print(fwht([1, 2, 3, 4]))  # [10, -2, -4, 0]
Vectorized version using numpy
import numpy as np

def fwht_np(x):
    x = np.array(x)
    N = len(x)
    h = 1
    while h < N:
        x = x.reshape(-1, 2, h)  # blocks of size 2h, split into two halves
        a, b = x[:, 0], x[:, 1]
        x = np.stack([a + b, a - b], axis=1).reshape(N)
        h *= 2
    return x

print(fwht_np([1, 2, 3, 4]))  # [10 -2 -4  0]

Rotation matrices with the same structure

One can take a repeated Kronecker product of a 2×22 \times 2 rotation matrix R(θ)=[cos⁡θ−sin⁡θsin⁡θcos⁡θ]R(\theta) = \begin{bmatrix} \cos\theta & -\sin\theta \\ \sin\theta & \cos\theta \end{bmatrix} to create an N×NN \times N orthonormal matrix, N=2kN = 2^k, with kk parameters:

RN(θ1,…,θk)=R(θ1)⊗R(θ2)⊗⋯⊗R(θk)R_N(\theta_1, \ldots, \theta_k) = R(\theta_1) \otimes R(\theta_2) \otimes \cdots \otimes R(\theta_k)

The matrix-vector product retains the efficient O(Nlog⁡N)O(N \log N) requirement. The only change is that the butterfly transform at each level ii now looks like:

(x,y)↦(xcos⁡θi−ysin⁡θi,  xsin⁡θi+ycos⁡θi)(x, y) \mapsto (x \cos\theta_i - y \sin\theta_i,\; x \sin\theta_i + y \cos\theta_i)
from math import cos, sin, pi

def rotate(x, thetas):
    x = list(x)
    N = len(x)
    h = 1
    # R(theta_k) mixes entries 1 apart, ..., R(theta_1) mixes entries N/2 apart
    for theta in reversed(thetas):
        c, s = cos(theta), sin(theta)
        for i in range(0, N, 2 * h):
            for j in range(i, i + h):
                a, b = x[j], x[j + h]
                x[j], x[j + h] = c * a - s * b, s * a + c * b
        h *= 2
    return x

y = rotate([1, 2, 3, 4], [pi / 2, 0])
print([round(v, 4) for v in y])  # [-3.0, -4.0, 1.0, 2.0]

Rotation matrices with one parameter per 2×22 \times 2 block

To create a wider range of rotation matrices with the same structure, one can add a parameter for each 2×22 \times 2 block of a matrix. This can be viewed by looking at what the "atomic" butterfly transform now looks like.

At level ii, the butterfly operation on entries aa and b=a+N/2ib = a + N/2^i depends on aa and ii both:

(xa,xb)↦(xacos⁡θi,a−xbsin⁡θi,a,  xasin⁡θi,a+xbcos⁡θi,a)(x_a, x_b) \mapsto (x_a \cos\theta_{i,a} - x_b \sin\theta_{i,a},\; x_a \sin\theta_{i,a} + x_b \cos\theta_{i,a})

The total number of parameters in this case is N/2×log⁡NN/2 \times \log N. Eg for N=8N = 8 there are 33 levels of 44 butterflies, so 1212 angles.

The implementation now looks like:

from math import cos, sin, pi

def rotate_per_block(x, thetas):
    x = list(x)
    N = len(x)
    k = N.bit_length() - 1  # N = 2^k
    for i in range(k, 0, -1):  # level k first, as in rotate
        h = N // 2**i
        for start in range(0, N, 2 * h):
            for a in range(start, start + h):
                b = a + h
                c, s = cos(thetas[i, a]), sin(thetas[i, a])
                x[a], x[b] = c * x[a] - s * x[b], s * x[a] + c * x[b]
    return x

# theta_{i,a} for N = 4; one angle per level gives the same result as rotate above
thetas = {(1, 0): pi / 2, (1, 1): pi / 2, (2, 0): 0, (2, 2): 0}
y = rotate_per_block([1, 2, 3, 4], thetas)
print([round(v, 4) for v in y])  # [-3.0, -4.0, 1.0, 2.0]