"""Explicit bistochastic forms of the Dita products H (x)_Q K, with H, K of the same order N. (H (x)_Q K)_{ia,jb} = Q_ib H_ij K_ab. The matrix B_{ia,jb} = conj(K_ai) conj(H_bj) Q_ib H_ij K_ab / Q_bb is obtained by multiplying the rows by conj(K_ai) and the columns by conj(H_bj)/Q_bb, and has all its row and column sums equal to N. The program checks this for several H, K and random Q, then checks that Butson inputs give Butson outputs over the same roots, and that real inputs give real Hadamard matrices with constant row sums. """ import numpy as np from itertools import product def say(*a): print(*a, flush=True) def fourier(N): return np.exp(2j*np.pi*np.outer(range(N), range(N))/N) def sylvester(n): H = np.array([[1.]]) for _ in range(n): H = np.block([[H, H], [H, -H]]) return H def tao(): E = [[0]*6, [0,0,1,1,2,2], [0,1,0,2,2,1], [0,1,2,0,1,2], [0,2,2,1,0,1], [0,2,1,2,1,0]] return np.exp(2j*np.pi*np.array(E)/3) def dita(H, K, Q): N = len(H) return np.einsum('ib,ij,ab->iajb', Q, H, K).reshape(N*N, N*N) def form(H, K, Q): N = len(H) B = np.einsum('ai,bj,ib,ij,ab,b->iajb', K.conj(), H.conj(), Q, H, K, 1/np.diag(Q)) return B.reshape(N*N, N*N) def check(H, K, Q): N = len(H) L, B = dita(H, K, Q), form(H, K, Q) M = N*N assert np.allclose(L @ L.conj().T, M*np.eye(M)) assert np.allclose(B @ B.conj().T, M*np.eye(M)) assert np.allclose(B.sum(1), N) and np.allclose(B.sum(0), N) # B is obtained from L by row and column phases rows = B[:, 0]/L[:, 0]; cols = B[0, :]/L[0, :]/rows[0] assert np.allclose(B, rows[:, None]*L*cols[None, :]) return B def scramble(H, rng): N = len(H) H = H*np.exp(2j*np.pi*rng.random((N, 1)))*np.exp(2j*np.pi*rng.random((1, N))) return H[rng.permutation(N)][:, rng.permutation(N)] def main(): rng = np.random.default_rng(2026) cases = [] for N in range(2, 9): cases.append((f'F_{N} x_Q F_{N}', fourier(N), fourier(N))) cases.append((f'scrambled F_{N} x_Q F_{N}', scramble(fourier(N), rng), scramble(fourier(N), rng))) cases += [('T_6 x_Q F_6', tao(), fourier(6)), ('F_6 x_Q T_6', fourier(6), tao()), ('W_4 x_Q F_4', sylvester(2), fourier(4)), ('W_8 x_Q W_8', sylvester(3), sylvester(3))] for name, H, K in cases: for _ in range(5): check(H.astype(complex), K.astype(complex), np.exp(2j*np.pi*rng.random((len(H), len(H))))) say(f'{name}: 5 random Q, bistochastic with sums N') for q in (3, 4, 5, 6, 8): N = q if q < 8 else 4 H = fourier(N)[rng.permutation(N)] K = fourier(N)[:, rng.permutation(N)]*np.exp(2j*np.pi*rng.integers(0, q, (N, 1))/q) Q = np.exp(2j*np.pi*rng.integers(0, q, (N, N))/q) B = check(H, K, Q) e = np.angle(B)*q/(2*np.pi) assert np.allclose(e, np.round(e)) say(f'Butson: H, K, Q with entries in mu_{q}, order {N*N}: form has entries in mu_{q}') for n in (1, 2, 3): N = 2**n; H = sylvester(n) for t in range(3): K = sylvester(n)[rng.permutation(N)][:, rng.permutation(N)]*rng.choice([-1, 1], (N, 1)) Q = rng.choice([-1., 1.], (N, N)) B = check(H.astype(complex), K.astype(complex), Q.astype(complex)) assert np.allclose(B.imag, 0) say(f'Real: H, K of order {N}, Q with signs: regular real Hadamard of order {N*N}, row sums {N}') say('All Dita form checks passed.') if __name__ == '__main__': main()