qaenthrix-eve / verify_exact.py
PureOne's picture
Release QAENTHRIX 3.0.0: proofs and reproducible research
81a3ea5 verified
Raw History Blame Contribute Delete
3.21 kB
"""Independent exact-rational checker. Python standard library only."""
from fractions import Fraction as F
from itertools import product
def eye(n):
return [[F(int(i == j)) for j in range(n)] for i in range(n)]
def zero(n):
return [[F(0) for _ in range(n)] for _ in range(n)]
def tr(a):
return [list(v) for v in zip(*a)]
def mul(a, b):
return [[sum((x*y for x, y in zip(row, col)), F(0)) for col in zip(*b)] for row in a]
def add(a, b, sign=1):
return [[x + sign*y for x, y in zip(ra, rb)] for ra, rb in zip(a, b)]
def outer(u):
return [[x*y for y in u] for x in u]
def scale(a, c):
return [[c*x for x in row] for row in a]
def rank(a):
if not a:
return 0
a = [r[:] for r in a]
pivot = 0
for col in range(len(a[0])):
at = next((i for i in range(pivot, len(a)) if a[i][col]), None)
if at is None:
continue
a[pivot], a[at] = a[at], a[pivot]
v = a[pivot][col]
a[pivot] = [x/v for x in a[pivot]]
for i in range(len(a)):
if i != pivot:
v = a[i][col]
a[i] = [x-v*y for x, y in zip(a[i], a[pivot])]
pivot += 1
if pivot == len(a):
break
return pivot
def check_word(word):
n = 2
p, q = eye(n), eye(n)
g = [zero(n), zero(n)]
rows, active = [[], []], []
previous = [0, 0]
prefixes = 0
for u, c, colour, unitary in word:
a = add(eye(n), scale(outer(u), c-1))
v = mul([list(u)], p)[0]
g[colour] = add(g[colour], scale(outer(v), 1-c*c))
if c != 1:
rows[colour].append(v)
active.append(mul([list(u)], q)[0])
p = mul(mul(unitary, a), p)
q = mul(unitary, q)
total = add(g[0], g[1])
assert total == add(eye(n), mul(tr(p), p), -1)
r = [rank(g[0]), rank(g[1])]
pooled = rank(total)
assert pooled == rank(active)
assert r == [rank(rows[0]), rank(rows[1])]
assert all(x >= y for x, y in zip(r, previous))
assert 0 <= sum(r)-pooled <= pooled <= n
intersection = rank(g[0]) + rank(g[1]) - rank([ra+rb for ra, rb in zip(g[0], g[1])])
assert intersection == sum(r)-pooled
previous = r
prefixes += 1
return prefixes
def run():
normals = [(F(1), F(0)), (F(0), F(1)), (F(3, 5), F(4, 5))]
cosines = [F(0), F(3, 5), F(1)]
symbols = [(u, c, colour, eye(2)) for u, c, colour in product(normals, cosines, range(2))]
words, prefixes = 0, 0
for length in (1, 2, 3):
for word in product(symbols, repeat=length):
prefixes += check_word(word)
words += 1
rotations = [eye(2), [[F(0), F(-1)], [F(1), F(0)]], [[F(0), F(1)], [F(1), F(0)]]]
for index in range(96):
word = [(normals[(index+t) % 3], cosines[(index+2*t) % 3], (index+t) % 2,
rotations[(index+3*t+t*t) % 3]) for t in range(1, 7)]
prefixes += check_word(word)
words += 1
return {"exact_words": words, "exact_prefixes": prefixes,
"arithmetic": "fractions.Fraction", "status": "PASS"}
if __name__ == "__main__":
import json
print(json.dumps(run(), indent=2))