"""Exact counts with and without knowledge of the positive exact-one formula.""" from collections import defaultdict from itertools import combinations, product from decide import XorBasis def orthogonal_count(columns): """Exact graph-only counter for row overlaps 0,2,4. Exponential in general.""" pairs = defaultdict(list) bit = 1 column_switches = 0 for col in columns: rows = sorted(col) if not rows: continue column_switches += 1 forms = {rows[0]: 0} for row in rows[1:]: forms[row] = 1 << bit bit += 1 for a, b in combinations(rows, 2): pairs[a,b].append(forms[a] ^ forms[b]) basis = XorBasis() disjunctions = [] for forms in pairs.values(): if len(forms) % 2: return 0 if len(forms) == 2: if not basis.add_zero(forms[0] ^ forms[1] ^ 1): return 0 elif len(forms) == 4: a,b,c,d = forms if not basis.add_zero(a ^ b ^ c ^ d): return 0 disjunctions.append((a ^ b, a ^ c)) else: raise ValueError("Maximum supported row overlap is four.") def count(space, clauses): while True: remaining = [] changed = False for a,b in clauses: a,b = space.reduce(a),space.reduce(b) if a == 1 or b == 1 or a ^ b == 1: continue if a == 0 or b == 0 or a == b: literal = b if a == 0 else a if not space.add_zero(literal ^ 1): return 0 changed = True else: remaining.append((a,b)) if not remaining: return 1 << (bit - 1 - len(space.pivots) + column_switches) clauses = remaining if not changed: break a,b = clauses[0] first = XorBasis(space.pivots) total = count(first,clauses[1:]) if first.add_zero(a ^ 1) else 0 if space.add_zero(a) and space.add_zero(b ^ 1): total += count(space,clauses[1:]) return total return count(basis,disjunctions) def formula_count(clauses, column_count): """The construction's weighted model-count identity.""" variables = sorted({x for clause in clauses for x in clause}) exponent = 37*len(clauses) - 3*len(variables) + column_count total = 0 for assignment in product((0,1), repeat=len(variables)): x = dict(zip(variables, assignment)) if all(sum(x[v] for v in clause) == 1 for clause in clauses): total += 1 << (exponent - sum(assignment)) return total def balance_model_weights(clauses): """Parsimoniously ensure that all models have the same number of true bits.""" var = sorted({x for clause in clauses for x in clause}) if not var: raise ValueError("Input must contain a clause.") z, one = max(var)+1, max(var)+2 new = [*clauses, (z,z,one)] for i, x in enumerate(var): new.append((x,one+i+1,z)) return new, len(var)+1 def matching_formula(matrix): """Parsimonious positive triples for a bipartite adjacency matrix (0/1).""" n = len(matrix) if n == 0 or any(len(row) != n or set(row)-{0,1} for row in matrix): raise ValueError("Expected a nonempty square 0/1 matrix.") z, one = 0,1 formula = [(z,z,one)] incident = [[] for _ in range(2*n)] next_var = 2 for i, row in enumerate(matrix): for j, value in enumerate(row): if value: incident[i].append(next_var) incident[n+j].append(next_var) next_var += 1 for edges in incident: if not edges: formula.append((z,z,z)) continue prev_complement = z for index, edge in enumerate(edges): if index + 1 == len(edges): formula.append((edge,z,prev_complement)) else: cur, cur_complement = next_var,next_var+1 next_var += 2 formula.extend(((edge,cur,prev_complement),(cur,cur_complement,z))) prev_complement = cur_complement return formula