"""Proof: an orthogonal rotation spreads one outlier's energy across
every coordinate, without changing the vector's length or the matmul
it feeds (H is orthogonal). The max shrinks; the format's points get
used again."""
import torch

def hadamard(n):
    H = torch.tensor([[1.0]])
    while H.shape[0] < n:
        H = torch.cat([torch.cat([H, H], 1), torch.cat([H, -H], 1)], 0) / (2 ** 0.5)
    return H

torch.manual_seed(3)
n = 64
x = torch.randn(n) * 0.5
x[7], x[23], x[51] = 8.0, -7.0, 6.0
H = hadamard(n)
y = x @ H
print(f"norm preserved: {torch.allclose(x.norm(), y.norm())}")
for name, v in (("before", x), ("after H", y)):
    print(f"{name:8s} max {v.abs().max():.3f}   mean {v.abs().mean():.3f}   "
          f"max/mean {(v.abs().max()/v.abs().mean()).item():.2f}")
