from fractions import Fraction from functools import cache from itertools import combinations, product from math import comb, factorial import sys @cache def partitions(size): if size == 0: return ((),) return tuple(part + (block,) for part in partitions(size - 1) for block in range(max(part, default=-1) + 2)) def mobius(part): weight = 1 for block in set(part): size = part.count(block) weight *= (-1) ** (size - 1) * factorial(size - 1) return weight def connected_hom(left, right): nleft, nright = max(left) + 1, max(right) + 1 graph = [set() for _ in range(nleft + nright)] for row, col in zip(left, right): col += nleft graph[row].add(col) graph[col].add(row) component, frontier = {0}, [0] while frontier: new = graph[frontier.pop()] - component component.update(new) frontier.extend(new) if len(component) != len(graph): return (0,) * 5 core = set(range(len(graph))) while leaves := {vertex for vertex in core if len(graph[vertex] & core) < 2}: core -= leaves if not core: return 13 * 4 ** (len(graph) - 1), 0, 0, 0, 0 scale = 4 ** (len(graph) - len(core)) left_size = sum(vertex < nleft for vertex in core) right_size = len(core) - left_size if (left_size, right_size) == (2, 2): return 364 * scale, 4 * scale, 0, 0, 0 if (left_size, right_size) == (3, 3): assert all(len(graph[vertex] & core) == 2 for vertex in core) return 4420, 36, 1, 0, 0 assert sorted((left_size, right_size)) == [2, 3] assert sum(len(graph[vertex] & core) for vertex in core) == 12 return 988, 12, 0, 12 if left_size == 2 else 0, 12 if right_size == 2 else 0 def verify_moments(): ring_left = (0, 0, 1, 1, 2, 2) ring_right = (0, 1, 1, 2, 2, 0) injective = [0] * 5 for left, right in product(partitions(3), repeat=2): value = connected_hom([left[index] for index in ring_left], [right[index] for index in ring_right]) for index, term in enumerate(value): injective[index] += mobius(left) * mobius(right) * term assert injective == [1404, -36, 1, 0, 0] print('Six-cycle identity: 6*C6 = 1404 - 36*C4 + tr(D^3)') expected = ( (52, 0, 0, 0, 0), (364, 0, 0, 0, 0), (3016, 0, 0, 0, 0), (27196, -4, 0, 0, 0), (258232, -120, 0, 0, 0), (2538172, -2340, -1, 24, 24), ) for size, target in enumerate(expected, 1): totals = [0] * 5 data = [(part, mobius(part)) for part in partitions(size)] for (left, left_weight), (right, right_weight) in product(data, repeat=2): for index, value in enumerate(connected_hom(left, right)): totals[index] += left_weight * right_weight * value actual = tuple(Fraction((-1) ** (size - 1) * value, factorial(size - 1)) for value in totals) assert actual == target, (size, actual, target) print(f'p{size} [1, C4, tr(D^3), row K23, col K23]: {target}') moments = [(13, 0, 0, 0, 0), *expected] for c4, tr3, row_k23, col_k23 in [(0, 0, 0, 0), (1, 0, 0, 0), (0, 6, 0, 0), (0, 0, 1, 0), (0, 0, 0, 1)]: inputs = (1, c4, tr3, row_k23, col_k23) powers = [sum(coef * value for coef, value in zip(moment, inputs)) for moment in moments] rooks = [1] for degree in range(1, 7): dividend = sum((-1) ** (index - 1) * powers[index] * rooks[degree - index] for index in range(1, degree + 1)) assert dividend % degree == 0 rooks.append(dividend // degree) assert rooks == [1, 52, 1170, 14976, 120627 + c4, 638820 + 28 * c4, 2258100 + 312 * c4 + tr3 // 6 - 4 * (row_k23 + col_k23)] print('Newton inversion: all rook formulas through m6 checked as affine identities') def permanent(rows): size = len(rows) state = [0] * (1 << size) state[0] = 1 for used in range((1 << size) - 1): if not state[used]: continue free = rows[used.bit_count()] & ~used while free: bit = free & -free state[used | bit] += state[used] free ^= bit return state[-1] def verify_spectral_and_quadrature(): def g(alpha, eigen): return (eigen + 3) * (eigen - alpha) ** 2 single = ((8, 26, 11, 3, 5, 54), (8, 130, 43, 1, 2, 36), (9, 39, 11, 1, 2, 24), (9, 26, 5, 1, 5, 0), (9, 26, 11, 7, 10, 66), (10, 26, 11, 7, 10, 72), (11, 26, 11, 4, 5, 78), (12, 26, 11, 9, 10, 84), (13, 26, 11, 9, 10, 84), (10, 39, 11, 1, 2, 30), (11, 39, 11, 3, 5, 36), (12, 39, 11, 7, 10, 42), (13, 39, 11, 4, 5, 48)) for c4, eig_n, eig_d, alpha_n, alpha_d, threshold in single: alpha, lower = Fraction(alpha_n, alpha_d), Fraction(eig_n, eig_d) assert lower >= alpha >= 0 assert 36 * alpha ** 2 + (3 - 2 * alpha) * 4 * c4 - g(alpha, lower) < threshold alpha = Fraction(2, 5) for cross in range(-4, 4): first = max(alpha, 2 - Fraction(cross, 2)) second = max(alpha, Fraction(26, 9) + Fraction(13 * cross, 18)) assert 36 * alpha ** 2 + (3 - 2 * alpha) * 32 - g(alpha, first) - g(alpha, second) < 36 for degree in (6, 7, 8): for power in range(6): integral = sum(Fraction((-1) ** index * comb(degree, index) * factorial(index + power), factorial(index)) for index in range(degree + 1)) assert integral == 0 print('Exact Rayleigh/interlacing constants through C4=13 and Laguerre orthogonality checked') def cycles4(rows): return sum(comb((first & second).bit_count(), 2) for first, second in combinations(rows, 2)) def verify_pg(): points = [point for point in product(range(3), repeat=3) if next((entry for entry in point if entry), 0) == 1] rows = [sum(1 << index for index, point in enumerate(points) if sum(first * second for first, second in zip(point, line)) % 3 == 0) for line in points] mask = (1 << len(rows)) - 1 complement = [mask ^ row for row in rows] assert len(rows) == 13 and all(row.bit_count() == 4 for row in rows) assert cycles4(rows) == 0 assert (permanent(rows), permanent(complement)) == (3852, 64803969) switched = list(rows) first_only, second_only = rows[0] & ~rows[1], rows[1] & ~rows[0] change = (first_only & -first_only) | (second_only & -second_only) switched[0] ^= change switched[1] ^= change assert (cycles4(switched), permanent(switched), permanent([mask ^ row for row in switched])) == (4, 3908, 64839709) print('PG(2,3): per=3852; complement=64803969; one switch: C4=4, per=3908, complement=64839709') def verify_branch_obstruction(): ha, hb, anchor = 1, 4, 5 roots = (Fraction(3), Fraction(9, 2)) picked = min((root for root in roots if ha < root < hb), key=lambda root: abs(root - anchor)) assert picked < roots[1] < 2 * anchor - picked assert not (ha < roots[1] < hb) print('Asymmetric hole counterexample to branch rule: hole=(1,4), anchor=5, roots=(3,9/2)') def defect_rows(rows, baseline): return [[0 if row == col else (left & right).bit_count() - baseline for col, right in enumerate(rows)] for row, left in enumerate(rows)] def verify_witnesses(): witness = [int(text, 2) for text in ( '11000011010', '01001011001', '01110000110', '00010010111', '11011100000', '11000100101', '00110111000', '10110001001', '00001101110', '00101100011', '10101010100')] transpose = [sum(((row >> col) & 1) << index for index, row in enumerate(witness)) for col in reversed(range(11))] assert all(row.bit_count() == 5 for row in witness + transpose) for actual in (defect_rows(witness, 2), defect_rows(transpose, 2)): assert all(row.count(1) == row.count(-1) == 1 for row in actual if any(row)) assert sum(any(row) for row in actual) == 10 start = next(index for index, row in enumerate(actual) if any(row)) seen, todo = {start}, [start] while todo: for index, entry in enumerate(actual[todo.pop()]): if entry and index not in seen: seen.add(index) todo.append(index) assert len(seen) == 10 complement = [((1 << 11) - 1) ^ row for row in witness] assert (cycles4(witness), permanent(witness), permanent(complement)) == (60, 12185, 75790) print('n=3 witness: e4=5, both defects are signed C10; permanents (12185, 75790)') hadamard = [sum(1 << (col - 1) for col in range(1, 16) if (row & col).bit_count() % 2 == 0) for row in range(1, 16)] field_product = ((0, 0, 0, 0), (0, 1, 2, 3), (0, 2, 3, 1), (0, 3, 1, 2)) points = [point for point in product(range(4), repeat=3) if next((entry for entry in point if entry), 0) == 1] projective = [sum(1 << index for index, point in enumerate(points) if (field_product[point[0]][line[0]] ^ field_product[point[1]][line[1]] ^ field_product[point[2]][line[2]]) == 0) for line in points] for matrix, degree, baseline in ((hadamard, 7, 3), (projective, 5, 1)): assert all(row.bit_count() == degree for row in matrix) assert all((first & second).bit_count() == baseline for first, second in combinations(matrix, 2)) old = cycles4(matrix) first_only, second_only = matrix[0] & ~matrix[1], matrix[1] & ~matrix[0] change = (first_only & -first_only) | (second_only & -second_only) matrix[0] ^= change matrix[1] ^= change assert cycles4(matrix) - old == 6 print('n=4 witnesses: switches of (15,7,3) and PG(2,4) attain e4=6') if __name__ == '__main__': if not __debug__: raise SystemExit('Do not run this checker with Python optimization enabled') print(sys.version) verify_moments() verify_spectral_and_quadrature() verify_pg() verify_branch_obstruction() verify_witnesses()