# Sections 3, 4, 5, 7, 8, 10: the easy facts behind the portraits of the paper. Wiring graphs of the design arrays and the splitting # of the energy, strongly regular graphs and the crystals of the 4 x 4 board, the decoding of a coded aperture mask, the # conference matrices as passes between the sign regions, with all their fillings, and the count of the optimal matrices # at N = 5, 13, and the complements of the small submatrices of the Hadamard matrices of order 8 and 12. Usage: python3 detours.py [names of the functions to be run] import numpy as np, itertools, math, sys, functools, networkx as nx from scipy.linalg import expm, polar from designs import SETS, incidence, parameters from portraits import mask, conference, isdesign def tracenorm(S): return np.linalg.svd(S, compute_uv=False).sum() def wiring(): rng = np.random.default_rng(1) for name, (v, D) in SETS.items(): M, k, l = parameters(v, D); n = k - l L = np.block([[np.zeros((v, v)), M], [M.T, np.zeros((v, v))]]) ev = np.round(np.linalg.eigvalsh(L), 6); vals = sorted(set(ev)) expected = sorted([-k, -math.sqrt(n), math.sqrt(n), k]) ok = np.allclose(vals, expected) and [int((ev == x).sum()) for x in vals] == [1, v - 1, v - 1, 1] P4 = np.trace(np.linalg.matrix_power(L, 4)); girth = 4 if l > 1 else 6 worst = 0 for sign in (1, -1): c = (k + sign * math.sqrt(n)) / v; U = (M - c) / math.sqrt(n) for _ in range(100): a, b = rng.choice([-1, 1], v), rng.choice([-1, 1], v) worst = max(worst, abs(a @ U @ b - (a @ M @ b - c * a.sum() * b.sum()) / math.sqrt(n))) print("%s: wiring graph with %d vertices and %d edges, eigenvalues k, sqrt(n), -sqrt(n), -k with multiplicities 1, v-1, v-1, 1: %s; girth %d; splitting of the energy, largest error: %.1e" % (name, 2 * v, v * k, ok, girth, worst)) def board(): cells = [(r, c) for r in range(4) for c in range(4)] R = np.array([[1 if (y != x and (y[0] == x[0] or y[1] == x[1])) else 0 for y in cells] for x in cells]) V = list(itertools.product([0, 1], repeat=4)) C = np.array([[1 if sum(a != b for a, b in zip(x, y)) in (1, 4) else 0 for y in V] for x in V]) S = np.array([[1 if ((x[0] - y[0]) % 4, (x[1] - y[1]) % 4) in [(1, 0), (3, 0), (0, 1), (0, 3), (1, 1), (3, 3)] else 0 for y in cells] for x in cells]) J = np.ones((16, 16), dtype=int); I = np.eye(16, dtype=int) for name, A in (("rook graph", R), ("Shrikhande graph", S), ("Clebsch graph", C)): k = int(A.sum(1)[0]); A2 = A @ A; lam = int(A2[A == 1][0]); mu = int(A2[(A == 0) & (I == 0)][0]) srg = np.array_equal(A2, k * I + lam * A + mu * (J - I - A)) print("%s: strongly regular with parameters (16,%d,%d,%d): %s; eigenvalues %s; design from the %s neighborhoods: %s" % (name, k, lam, mu, srg, [int(x) for x in sorted(set(np.round(np.linalg.eigvalsh(A))))], "open" if lam == mu else "closed", isdesign(A if lam == mu else A + I, 6, 2))) H = 2 * R - J print("board: the matrix 2M - 1 is Hadamard: %s, symmetric: %s, with diagonal %d and row sums %d" % (np.array_equal(H @ H.T, 16 * I), np.array_equal(H, H.T), H[0, 0], H.sum(1)[0])) K = H * H[0][None, :] * (H[:, 0] * H[0, 0])[:, None]; rows = {tuple(x) for x in K} print("board: after normalization the rows of 2M - 1 form a group under the entrywise product, as for the Walsh matrix: %s" % all(tuple(np.array(x) * np.array(y)) in rows for x in rows for y in rows)) B = np.array(list(itertools.product([1, -1], repeat=16))) for sign, name in ((1, "U_+"), (-1, "U_-")): X = 16 * B @ R.T - (6 + 2 * sign) * B.sum(1)[:, None] # 16 sqrt(n) U b, with n = 4 good = B[(np.abs(X) == 32).all(1)] sizes = {} for b in good: sizes[int((b == -1).sum())] = sizes.get(int((b == -1).sum()), 0) + 1 print("board, %s: %d crystalline states, numbers of states by number of reversed spins: %s" % (name, len(good), dict(sorted(sizes.items())))) if sign == 1: kinds = {} for b in good: T = [cells[i] for i in range(16) if b[i] == -1] rows = tuple(sorted(sum(1 for x in T if x[0] == r) for r in range(4))); cols = tuple(sorted(sum(1 for x in T if x[1] == c) for c in range(4))) key = (len(T), min(rows, cols), max(rows, cols)); kinds[key] = kinds.get(key, 0) + 1 for key in sorted(kinds): print(" %d reversed spins, numbers of them on the rows and columns %s and %s, up to transposition: %d states" % (key + (kinds[key],))) def decoder(): m = mask(); r, s = m.shape; v = r * s; cells = [(i, j) for i in range(r) for j in range(s)] Mo = np.array([[m[(y[0] - x[0]) % r, (y[1] - x[1]) % s] for y in cells] for x in cells]); J = np.ones((v, v), dtype=int) M = J - Mo; S = 2 * M - J; n = 36 print("mask: open cells M' = 1 - M, sign matrix S = 2M - 1: M' S^t = -2n: %s, M S^t = 2n - 1 (all-one matrix): %s" % (np.array_equal(Mo @ S.T, -2 * n * np.eye(v, dtype=int)), np.array_equal(M @ S.T, 2 * n * np.eye(v, dtype=int) - J))) sv = np.round(np.linalg.svd(S, compute_uv=False), 6) print(" singular values of S: %s" % {float(x): int((sv == x).sum()) for x in sorted(set(sv))}) U = (M - (71 + 6) / v) / 6 print(" S/(2 sqrt(n)) = U_+ + (1 - 1/(2 sqrt(n))) P: %s" % np.allclose(S / 12, U + (1 - 1 / 12) * J / v)) def passes(): rng = np.random.default_rng(2) for q in (5, 9, 13): C = conference(q); N = q + 1; U = C / math.sqrt(q); worst = 0 for _ in range(20): A = rng.standard_normal((N, N)); A = A - A.T; t = 1e-6 slope = (np.abs(U @ expm(t * A)).sum() - N * math.sqrt(q)) / t worst = max(worst, abs(slope - np.abs(np.diag(U @ A)).sum()) / np.abs(np.diag(U @ A)).sum()) vals = {}; fixed = 0; bal = [] for d in itertools.product([1, -1], repeat=N): S = C + np.diag(d); x = tracenorm(S); key = round(x, 6); vals[key] = vals.get(key, 0) + 1 if np.array_equal(np.sign(polar(S)[0]), S): fixed += 1 if sum(d) == 0: bal.append(x) best = max(vals); second = N * math.sqrt(N) - (N - 2) / (4 * math.sqrt(N)) print("conference matrix of order %d: 1-norm N sqrt(N-1) = %.6f; slope of the 1-norm in a random direction against the sum of |(UA)_ii|, largest relative error: %.1e" % (N, N * math.sqrt(q), worst)) print(" fillings: %d diagonals, %d values of Tr|S|, from %.6f (%d diagonals) to %.6f (%d diagonals); sign matrices of their polar parts: %d" % (2 ** N, len(vals), min(vals), vals[min(vals)], best, vals[best], fixed)) print(" balanced diagonals: Tr|S| between %.6f and %.6f, against N sqrt(N) - (N-2)/(4 sqrt(N)) = %.6f; N sqrt(N) = %.6f" % (min(bal), max(bal), second, N * math.sqrt(N))) def equivalent(S, T): # equivalence of 2 sign matrices under permutations and sign changes of the rows and columns, as a graph isomorphism def graph(M): G = nx.Graph(); n = len(M) for i in range(n): for a in (1, -1): G.add_node(("r", i, a), side=0); G.add_node(("c", i, a), side=1) G.add_edge(("r", i, 1), ("r", i, -1)); G.add_edge(("c", i, 1), ("c", i, -1)) for i in range(n): for j in range(n): for a in (1, -1): for b in (1, -1): if a * b * M[i][j] == 1: G.add_edge(("r", i, a), ("c", j, b)) return G return nx.is_isomorphic(graph(S), graph(T), node_match=lambda x, y: x["side"] == y["side"]) def complements(): from optimal import NEXT leg = lambda x, p: 0 if x % p == 0 else (1 if pow(x, (p - 1) // 2, p) == 1 else -1) W = np.array([[(-1) ** bin(i & j).count("1") for j in range(8)] for i in range(8)]) P = np.ones((12, 12), dtype=int) for i in range(11): P[i + 1, 0] = -1 for j in range(11): P[i + 1, j + 1] = leg(j - i, 11) if i != j else 1 assert np.array_equal(P @ P.T, 12 * np.eye(12, dtype=int)) fixed = lambda D: bool(np.array_equal(np.sign(polar(D)[0]), D)) seven = np.array([[1 if x == "+" else -1 for x in row] for row in NEXT]) for name, H, ranks in (("order 8", W, (1, 2, 3)), ("order 12", P, (1, 2, 3, 5))): N = len(H) for r in ranks: found = {}; law = 0; formula = 0 for R in itertools.combinations(range(N), r): if r == 5 and R[:3] != (0, 1, 2): continue for C in itertools.combinations(range(N), r): A = H[np.ix_(R, C)]; d = int(round(abs(np.linalg.det(A)))) if d == 0: continue Rc = [i for i in range(N) if i not in R]; Cc = [j for j in range(N) if j not in C]; D = H[np.ix_(Rc, Cc)] x = tracenorm(D); law = max(law, abs(x - tracenorm(A) - (N - 2 * r) * math.sqrt(N))) V, T = polar(A, side="right") # A = V T, with T = |A| E = H[np.ix_(Rc, C)] @ np.linalg.inv(math.sqrt(N) * np.eye(r) + T) @ V.T @ H[np.ix_(R, Cc)] formula = max(formula, np.abs(polar(D)[0] - (D - E) / math.sqrt(N)).max()) key = (round(x, 4), d, int(round(abs(np.linalg.det(D))))) if key not in found: found[key] = [0, 0, D] found[key][0] += 1; found[key][1] += fixed(D) print("Hadamard matrix of %s, complements D of the invertible submatrices A of size r = %d%s: law Tr|D| = Tr|A| + (N-2r) sqrt(N), largest error %.1e; polar formula, largest error %.1e" % (name, r, " (rows 0, 1, 2 inside)" if r == 5 else "", law, formula)) for key in sorted(found): extra = "; equivalent to the matrix NEXT of optimal.py: %s" % equivalent(found[key][2], seven) if (N - r == 7 and key[1] == 48) else "" print(" Tr|D| = %.4f, |det A| = %d, |det D| = %d: %d submatrices, %d of which are the sign matrices of their polar parts%s" % (key + (found[key][0], found[key][1], extra))) idx = [0, 1, 2, 4]; rest = [3, 5, 6, 7]; U = polar(W[np.ix_(rest, rest)])[0] print("Walsh matrix of order 8, rows and columns 1, 2, 3, 5 removed: the polar part of the complement has %d zero entries" % int((np.abs(U) < 1e-12).sum())) for N in (5, 7): w = np.exp(2j * math.pi / N); D = np.array([[w ** (i * j) for j in range(1, N)] for i in range(1, N)]); U = polar(D)[0] print("Fourier matrix of order %d, r = 1: sqrt(N) Pol(D) = D - 1/(1 + sqrt(N)) (all-one matrix): %s; D is the phase matrix of Pol(D): %s" % (N, np.allclose(math.sqrt(N) * U, D - 1 / (1 + math.sqrt(N))), np.allclose(U / np.abs(U), D))) def geometry(): # the crystals of the projective space PG(3,3), with the planes as blocks, and of the projective plane PG(2,9) def space(dim, elements, add, mul, zero, one): pts = [] for v in itertools.product(elements, repeat=dim): nz = [x for x in v if x != zero] if nz and nz[0] == one: pts.append(v) dot = lambda x, y: functools.reduce(add, [mul(a, b) for a, b in zip(x, y)]) return pts, np.array([[1 if dot(x, y) == zero else 0 for x in pts] for y in pts]) def check(M, T, sign): v = len(M); k = int(M[0].sum()); G = M @ M.T; lam = int(G[0, 1]); n = k - lam; c = (k + sign * math.sqrt(n)) / v b = np.ones(v); b[T] = -1; x = (M @ b - c * b.sum()) / math.sqrt(n) return sorted(set(int(y) for y in M[:, T].sum(1))), bool(np.allclose(np.abs(x), 1)) F3 = [0, 1, 2]; pts, M = space(4, F3, lambda a, b: (a + b) % 3, lambda a, b: (a * b) % 3, 0, 1) print("PG(3,3): %d points and planes, design (40,13,4): %s" % (len(pts), isdesign(M, 13, 4))) ovoid = [i for i, x in enumerate(pts) if (x[0] * x[1] + x[2] * x[2] + x[3] * x[3]) % 3 == 0] print(" elliptic quadric: %d points, intersections with the planes %s, crystalline for U_+: %s" % ((len(ovoid),) + check(M, ovoid, 1))) span = lambda x, y: sorted({pts.index(z) for a in F3 for b in F3 if (a, b) != (0, 0) for z in [tuple((a * p + b * q) % 3 for p, q in zip(x, y))] if z in pts} | {pts.index(tuple((2 * c) % 3 for c in z)) for a in F3 for b in F3 if (a, b) != (0, 0) for z in [tuple((a * p + b * q) % 3 for p, q in zip(x, y))] if z not in pts}) lines = sorted({tuple(span(x, y)) for x, y in itertools.combinations(pts, 2)}); spread = [] for L in lines: if all(not set(L) & set(K) for K in spread): spread.append(L) T = sorted(set().union(*spread)); print(" %d lines, %d pairwise skew lines: %d points, intersections with the planes %s, crystalline for U_-: %s" % ((len(lines), len(spread), len(T)) + check(M, T, -1))) if spread[-1] == L else None F9 = [(a, b) for a in F3 for b in F3]; add = lambda x, y: ((x[0] + y[0]) % 3, (x[1] + y[1]) % 3); mul = lambda x, y: ((x[0] * y[0] - x[1] * y[1]) % 3, (x[0] * y[1] + x[1] * y[0]) % 3) pts, M = space(3, F9, add, mul, (0, 0), (1, 0)); norm = lambda x: (x[0] * x[0] + x[1] * x[1]) % 3 print("PG(2,9): %d points and lines, design (91,10,1): %s" % (len(pts), isdesign(M, 10, 1))) unital = [i for i, x in enumerate(pts) if sum(norm(c) for c in x) % 3 == 0]; baer = [i for i, x in enumerate(pts) if all(c[1] == 0 for c in x)] print(" Hermitian curve: %d points, intersections with the lines %s, crystalline for U_+: %s" % ((len(unital),) + check(M, unital, 1))) print(" Baer subplane: %d points, intersections with the lines %s, crystalline for U_-: %s" % ((len(baer),) + check(M, baer, -1))) for q in (2, 3, 4, 5): print(" q = %d: %d elliptic quadrics and %d lines in PG(3,q); %d Hermitian curves and %d Baer subplanes in PG(2,q^2)" % (q, q ** 4 * (q * q - 1) * (q ** 3 - 1) // 2, (q * q + 1) * (q * q + q + 1), q ** 3 * (q * q + 1) * (q ** 3 - 1), q ** 3 * (q ** 3 + 1) * (q * q + 1))) def corrections(): print("coefficient of sqrt(N) in N sqrt(N) - Tr|S|, at N = 10^6 + small shift:") N = 10 ** 6 + 1; print(" upper bound, N odd: %.4f" % ((N * math.sqrt(N) - (N - 1) * math.sqrt(N - 1) - math.sqrt(2 * N - 1)) / math.sqrt(N))) N = 10 ** 6 + 2; print(" upper bound, N = 2 modulo 4: %.4f" % ((N * math.sqrt(N) - (N - 2) * math.sqrt(N - 2) - 2 * math.sqrt(2 * N - 2)) / math.sqrt(N))) print(" Hadamard designs, N = v = 4n - 1: %.4f" % ((N * math.sqrt(N) - (N - 1) * math.sqrt(N + 1) - 1) / math.sqrt(N))) print(" conference matrices: %.4f, and with a balanced filling: %.4f" % ((N * math.sqrt(N) - N * math.sqrt(N - 1)) / math.sqrt(N), (N - 2) / (4 * N))) print(" complements in a Hadamard matrix of order N + r: %.4f at r = 2, and %.4f at r = 3" % tuple((N * math.sqrt(N) - (N - r) * math.sqrt(N + r) - c) / math.sqrt(N) for r, c in ((2, 2 * math.sqrt(2)), (3, 5)))) def unique(): for name, v, D, aut in (("trivial design (5,1,0)", 5, [0], 120), ("plane of order 3 (13,4,1)", 13, [0, 1, 3, 9], 5616)): M = incidence(v, D); S = 1 - 2 * M; G = S @ S.T print("%s: S = 1 - 2M has S S^t = N - 1 + (all-one matrix): %s, row sums %s; optimal matrices modulo the sign changes: (%d!)^2/%d = %d" % (name, np.allclose(G, (v - 1) * np.eye(v) + 1), [int(x) for x in sorted(set(S.sum(1)))], v, aut, math.factorial(v) ** 2 // aut)) print("orbits: 20160/168 = %d, 20160/35 = %d, 20160/280 = %d; 120960/168 = %d, 120960/280 = %d, 120960/360 = %d" % (20160 // 168, 20160 // 35, 20160 // 280, 120960 // 168, 120960 // 280, 120960 // 360)) def tensor(): # Section 3: the twisted matrix U_- of the Fano plane, tensored with the rescaled Hadamard matrix of order 2, is a critical point # of the 1-norm on O_14 which is not a local maximizer: the matrix X = S^t U has its smallest eigenvalue -sqrt(2) with multiplicity 2 from designs import matrices U = np.kron(matrices(7, [1, 2, 4])[1], np.array([[1, 1], [1, -1]]) / math.sqrt(2)); S = np.sign(U); X = S.T @ U; lam, V = np.linalg.eigh((X + X.T) / 2) A = np.outer(V[:, 0], V[:, 1]) - np.outer(V[:, 1], V[:, 0]); t = 0.05; R = np.eye(14) + math.sin(t) * A + (1 - math.cos(t)) * (A @ A) print("tensor product of the twisted Fano matrix with the Hadamard matrix of order 2: orthogonal: %s, X symmetric: %s, eigenvalues of X: %s" % (np.allclose(U @ U.T, np.eye(14)), np.allclose(X, X.T), " ".join("%.4f" % x for x in lam))) print(" 1-norm %.6f, and after the rotation of angle 0.05 in the plane of the first 2 eigenvectors: %.6f, the predicted gain being %.6f" % (np.abs(U).sum(), np.abs(U @ R).sum(), (1 - math.cos(t)) * 2 * math.sqrt(2))) if __name__ == "__main__": for f in (sys.argv[1:] or ["wiring", "board", "geometry", "decoder", "passes", "complements", "corrections", "unique", "tensor"]): globals()[f]()