ioxhiru-vq / code /spectral_lift.py
PureOne's picture
Release Ioxhiru Vq spectral signing thresholds
0dd9df7 verified
Raw History Blame Contribute Delete
6.57 kB
"""Ioxhiru Vq. Unweighted degree-three graph lift and exact terminal coefficients."""
from dataclasses import dataclass
from fractions import Fraction as F
@dataclass
class SignedGraph:
order: int
edges: list
def vertices(self, count):
start = self.order
self.order += count
return list(range(start, self.order))
def edge(self, u, v, sign=1):
self.edges.append((u, v, sign))
def path(self, start, length, end=None, sign=1):
"""Append a path of this many edges. With no end specified it is pendant."""
if length < (end is not None):
raise ValueError("A wire must have positive length.")
nodes = [start] + self.vertices(length - int(end is not None))
if end is not None:
nodes.append(end)
for index, (u, v) in enumerate(zip(nodes, nodes[1:])):
self.edge(u, v, sign if index == 0 else 1)
def validate(graph, degree_bound):
neighbors = [set() for _ in range(graph.order)]
for u, v, sign in graph.edges:
if not (0 <= u < graph.order and 0 <= v < graph.order):
raise ValueError("Endpoint outside vertex set.")
if sign not in (-1, 1) or u == v or v in neighbors[u]:
raise ValueError("Expected simple graph with unit edge signs.")
neighbors[u].add(v)
neighbors[v].add(u)
if any(len(ns) > degree_bound for ns in neighbors):
raise ValueError(f"Input exceeds degree {degree_bound}.")
return neighbors
def path_polys(x, length):
"""P_0,...,P_length; path continuants. x may be a Fraction."""
values = [1]
if length:
values.append(x)
for _ in range(2, length + 1):
values.append(x * values[-1] - values[-2])
return values
def coefficients(length):
if length < 2 or length % 2:
raise ValueError("Use an even wire length of at least two.")
t = F(5, 2)
poly = path_polys(t, length)
scale = poly[length - 1]
r = poly[length - 2] / scale
half = length // 2
r_short = poly[half - 2] / poly[half - 1] if half > 1 else F(0)
z = 1 / (t - 2*r)
y = 1 / (t - r - z)
c = 1 / (t - 2*y)
return scale, r, r_short, c
def cubic_lift(base, length=14):
"""Return lifted graph and its ordered backbone ports. Canonical tree gauge."""
if length < 2 or length % 2:
raise ValueError("Use an even wire length (the theorem guarantees ≥14).")
validate(base, 8)
out = SignedGraph(0, [])
cores = [out.vertices(15) for _ in range(base.order)]
used = [0] * base.order
def cap(endpoint):
root, = out.vertices(1)
out.edge(endpoint, root)
for _ in range(2):
mid, bottom = out.vertices(2)
out.edge(root, mid)
out.edge(mid, bottom)
out.path(mid, length - 1)
out.path(bottom, length - 1)
out.path(bottom, length - 1)
for u, v, sign in base.edges:
a, b = cores[u][2*used[u]], cores[v][2*used[v]]
used[u] += 1
used[v] += 1
out.path(a, length, b, sign)
for i, sites in enumerate(cores):
for u, v in zip(sites, sites[1:]):
out.edge(u, v)
for j, u in enumerate(sites):
if not j % 2 and j // 2 < used[i]:
continue
out.path(u, length // 2 - 1 if j in (1, 3, 5) else length - 1)
cap(sites[0])
cap(sites[-1])
return out, cores
def padded_subdivision(base, length):
if length < 1:
raise ValueError("Subdivision length must be positive.")
neighbors = validate(base, 3)
out = SignedGraph(base.order, [])
for u, v, sign in base.edges:
out.path(u, length, v, sign)
for v, ns in enumerate(neighbors):
for _ in range(3 - len(ns)):
out.path(v, length - 1)
return out
def integer_lift(base, threshold=3):
"""Promote the 5/2 test to any fixed integer threshold at least three."""
if not isinstance(threshold,int) or threshold < 3:
raise ValueError("Use an integer threshold of at least three.")
validate(base,3)
out=SignedGraph(base.order,list(base.edges))
for v in range(base.order):
root, = out.vertices(1)
out.edge(v,root)
for w in out.vertices(3):
out.edge(root,w)
if threshold>3:
core_count=out.order
for v in range(core_count):
for w in out.vertices(threshold*(threshold-3)):
out.edge(v,w)
return out
def transfer_polynomial(x, j):
if j < 1:
raise ValueError("The index must be positive.")
if j == 1:
return x
p = path_polys(x, j)
return p[j] - 2*p[j - 2]
def base_from_support(support):
return SignedGraph(support.nrows + len(support.columns),
[(r, support.nrows+j, s)
for j, column in enumerate(support.columns)
for r, s in sorted(column.items())])
if __name__ == "__main__":
import argparse
import json
from signings import build
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("formula", help="Nonempty JSON list of positive triples")
parser.add_argument("--assignment", help="JSON list assigning all variables")
parser.add_argument("--length", type=int, default=14)
parser.add_argument("--subdivision", type=int, default=1)
parser.add_argument("--integer-threshold",type=int,help="Promote the 5/2 test to integer t >= 3")
parser.add_argument("--edges", action="store_true", help="Include the full signed edge list")
args = parser.parse_args()
assignment = json.loads(args.assignment) if args.assignment else None
base = base_from_support(build(json.loads(args.formula), assignment))
graph, _ = cubic_lift(base, args.length)
if args.subdivision != 1:
graph = padded_subdivision(graph, args.subdivision)
if args.integer_threshold is not None:
if args.subdivision != 1:
parser.error("Use integer promotion on the 5/2 lift.")
graph=integer_lift(graph,args.integer_threshold)
bound=3 if args.integer_threshold is None else 4+args.integer_threshold*(args.integer_threshold-3)
neighbors = validate(graph, bound)
result = dict(base_vertices=base.order, vertices=graph.order,
edge_count=len(graph.edges), maximum_degree=max(map(len, neighbors)),
wire_length=args.length, subdivision=args.subdivision)
if args.edges:
result["signed_edges"] = graph.edges
print(json.dumps(result))