"""Build the two initial-coordinate systems for the five-way permanent in characteristic 2.""" from collections import defaultdict from itertools import combinations, product import argparse from pathlib import Path SYMBOLS = tuple(range(5)) CASE_A = frozenset((i, 4) for i in SYMBOLS) PAIR_NAMES = ( ("15", "25", "35", "45", "55"), ("25", "35", "44", "45", "55"), ("34", "35", "44", "45", "55"), ("33", "35", "44", "45", "55"), ("22", "33", "44", "45", "55"), ("11", "22", "33", "44", "55"), ) PAIR_SETS = tuple(frozenset((int(a)-1, int(b)-1) for a,b in names) for names in PAIR_NAMES) ALL = frozenset.union(*PAIR_SETS) NON_A = frozenset.union(*PAIR_SETS[1:]) def borel_pair_subspaces(): """Enumerate all torus-coordinate five-planes stable under elementary raises in Sym^2.""" monomials = tuple((a, b) for a in SYMBOLS for b in SYMBOLS if a <= b) def stable(subset): for w in subset: for a in set(w): for b in range(a+1, 5): positions = [i for i,v in enumerate(w) if v == a] for count in range(1, len(positions)+1): support = set() for take in combinations(positions, count): z = tuple(sorted(b if i in take else v for i,v in enumerate(w))) support.symmetric_difference_update((z,)) if not support <= subset: return False return True return {frozenset(s) for s in combinations(monomials, 5) if stable(set(s))} class Weight: def __init__(self): self.orbits = [] self.variables = [] self.index = {} def pair_orbits(J, upper): """Every basis vector sums one orbit; discarded orbits contain a forbidden square.""" spaces = defaultdict(Weight) seen = set() edges = [(i, j, upper[J[i], J[j]]) for i, j in combinations(range(len(J)), 2)] for w in product(SYMBOLS, repeat=len(J)): if w in seen: continue orbit = {w} seen.add(w) stack = [w] killed = False while stack: y = stack.pop() for i, j, allowed in edges: a, b = y[i], y[j] if tuple(sorted((a,b))) in allowed: continue if a == b: killed = True else: z = list(y) z[i], z[j] = z[j], z[i] z = tuple(z) if z not in orbit: orbit.add(z) seen.add(z) stack.append(z) if not killed: weight = spaces[tuple(sorted(w))] assert min(orbit) == w weight.index[w] = len(weight.orbits) weight.orbits.append(tuple(sorted(orbit))) return spaces class CNF: def __init__(self): self.top = 0 self.clauses = [] def variable(self): self.top += 1 return self.top def exactly(self, xs, count): """Truncated unary cardinality counter. row[j] represents count-so-far >=j+1.""" false = self.variable() self.clauses.append([-false]) row = [false]*(count+1) for x in xs: nxt = [] for j, prev in enumerate(row): y = self.variable() nxt.append(y) lower = row[j-1] if j else -false # y <=> prev OR (lower AND x) self.clauses.extend(([-prev, y], [-lower, -x, y], [-y, prev, lower], [-y, prev, x])) row = nxt self.clauses.extend(([row[count-1]], [-row[count]])) def write(self, dest): with Path(dest).open('w') as f: print('p cnf', self.top, len(self.clauses), file=f) for c in self.clauses: print(*c, 0, file=f) def linear_implications(source, target, action, output): """Generate all suffix-echelon implications for one genuine linear transformation.""" cols = [] for orbit in source.orbits: support = set() for w in orbit: for z in action(w): support.symmetric_difference_update((z,)) col = 0 for i, target_orbit in enumerate(target.orbits): if target_orbit[0] in support: col |= 1 << i # The map carries the given ambient type space to the target one. assert all(v in support for v in target_orbit) support.difference_update(target_orbit) assert not support cols.append(col) basis = {} for x, col in reversed(tuple(zip(source.variables, cols))): while col: h = (col & -col).bit_length()-1 if h not in basis: possible = [target.variables[j] for j in sorted(basis) if j < h] output.append([-x, *possible, target.variables[h]]) basis[h] = col break col ^= basis[h] def build(case): assert set(PAIR_SETS) == borel_pair_subspaces() assert PAIR_SETS[0] == CASE_A pairs = list(combinations(SYMBOLS, 2)) if case == 'X': upper = {e: NON_A for e in pairs} elif case == 'A': upper = {e: (CASE_A if e == (0,1) else ALL) for e in pairs} else: raise ValueError(case) js = [J for k in range(1,6) for J in combinations(SYMBOLS,k)] spaces = {J:pair_orbits(J, upper) for J in js} cnf = CNF() for J in js: for alpha, space in spaces[J].items(): space.variables = [cnf.variable() for orbit in space.orbits] if len(alpha) == len(set(alpha)): # The full alternating word on this letter set belongs to W_J. assert space.orbits[0][0] == alpha cnf.clauses.append([space.variables[0]]) primary = cnf.top empty = Weight() for J in js: all_vars = [x for space in spaces[J].values() for x in space.variables] cnf.exactly(all_vars, 5 if len(J) == 1 else 15) for alpha, src in spaces[J].items(): for a in sorted(set(alpha)): for b in range(a+1,5): for h in range(1,alpha.count(a)+1): beta = list(alpha) for _ in range(h): beta.remove(a) beta.append(b) tgt = spaces[J].get(tuple(sorted(beta)), empty) def raising(w): ids = [i for i,v in enumerate(w) if v==a] for take in combinations(ids,h): yield tuple(b if i in take else v for i,v in enumerate(w)) linear_implications(src, tgt, raising, cnf.clauses) if len(J)>1: beta = list(alpha) beta.remove(a) for pos in range(len(J)): sub = J[:pos]+J[pos+1:] tgt = spaces[sub].get(tuple(beta),empty) def contraction(w): if w[pos]==a: yield w[:pos]+w[pos+1:] linear_implications(src, tgt, contraction, cnf.clauses) return primary, cnf if __name__ == '__main__': arg=argparse.ArgumentParser(description=__doc__) arg.add_argument('case',choices=('A','X')) arg.add_argument('destination', type=Path) args=arg.parse_args() p,c=build(args.case) c.write(args.destination) print(f'{args.case}: {p} primary variables, {c.top} variables, {len(c.clauses)} clauses')