SORILUN-RSI / research /src /synthesis.py
PureOne's picture
Add SORILUN research dataset release
aff9293 verified
Raw History Blame Contribute Delete
4.73 kB
"""Finite, exhaustive discovery of an integral 2-by-2 bilinear convolution law.
No multiplication formula is stored in the candidate generator. The search
space is explicitly small and human-designed; the winning identity is a
classical one, not a claim of a new multiplication algorithm.
"""
from __future__ import annotations
from fractions import Fraction as Q
from itertools import combinations, product
import hashlib
import json
import time
FORMS = ((0, 1), (1, 0), (1, -1), (1, 1))
TARGETS = ((1, 0, 0, 0), (0, 1, 1, 0), (0, 0, 0, 1))
def digest(value):
return hashlib.sha256(json.dumps(value, sort_keys=True,
separators=(",", ":")).encode()).hexdigest()
def solve(columns, counter):
"""Solve all three output systems together, over exact rationals."""
a = [[Q(columns[j][i]) for j in range(3)] +
[Q(TARGETS[k][i]) for k in range(3)] for i in range(4)]
row = 0
pivots = []
for col in range(3):
pivot = next((r for r in range(row, 4) if a[r][col]), None)
counter['pivot_tests'] += 4 - row
if pivot is None:
continue
a[row], a[pivot] = a[pivot], a[row]
q = a[row][col]
for j in range(col, 6):
a[row][j] /= q
counter['rational_ops'] += 1
for r in range(4):
if r != row and a[r][col]:
q = a[r][col]
for j in range(col, 6):
a[r][j] -= q * a[row][j]
counter['rational_ops'] += 2
pivots.append(col)
row += 1
if row < 3:
return None
for r in range(row, 4):
if any(a[r][3:]):
return None
out = [[Q(0)] * 3 for _ in range(3)]
for r, p in enumerate(pivots):
for k in range(3):
out[k][p] = a[r][3 + k]
if any(x.denominator != 1 or abs(x) > 1 for v in out for x in v):
return None
return [[int(x) for x in v] for v in out]
def linear_adds(row):
nz = [x for x in row if x]
if not nz:
return 0
return len(nz) - 1 + int(nz[0] == -1)
def verify_tensor(law):
"""Independent integer checker: 12 exact coefficient identities.
This checker never invokes a learned multiplier. Integer arithmetic,
this routine, and Python are in the trusted computing base.
"""
try:
A, B, C = law['A'], law['B'], law['C']
if len(A) != 3 or len(B) != 3 or len(C) != 3:
return False, 0
if any(len(r) != 2 for r in A + B) or any(len(r) != 3 for r in C):
return False, 0
if any(type(x) is not int or x not in (-1, 0, 1)
for r in A + B + C for x in r):
return False, 0
work = 0
for k, i, j in product(range(3), range(2), range(2)):
total = 0
for t in range(3):
total += C[k][t] * A[t][i] * B[t][j]
work += 3
work += 1
if total != int(i + j == k):
return False, work
return True, work
except (KeyError, TypeError, IndexError):
return False, 0
def discover():
start = time.perf_counter()
atoms = [(a, b, tuple(x*y for x in a for y in b))
for a in FORMS for b in FORMS]
counts = {'candidates': 0, 'valid': 0, 'rational_ops': 0,
'pivot_tests': 0, 'certificate_ops': 0}
valid = []
for ids in combinations(range(len(atoms)), 3):
counts['candidates'] += 1
C = solve([atoms[i][2] for i in ids], counts)
if C is None:
continue
law = {'A': [list(atoms[i][0]) for i in ids],
'B': [list(atoms[i][1]) for i in ids], 'C': C}
ok, work = verify_tensor(law)
counts['certificate_ops'] += work
if not ok:
raise ArithmeticError('Independent tensor verification failed')
counts['valid'] += 1
ia = sum(linear_adds(r) for r in law['A'] + law['B'])
oa = sum(linear_adds(r) for r in law['C'])
# The score is an explicit structural proxy, not a held-out result.
score = ia + 2*oa
valid.append((score, digest(law), law))
if not valid:
raise RuntimeError('No valid identity in the declared grammar')
valid.sort(key=lambda x: (x[0], x[1]))
law = valid[0][2]
return {'law': law, 'law_sha256': digest(law), 'counts': counts,
'structural_score': valid[0][0],
'wall_seconds': time.perf_counter() - start,
'known_equivalence': 'Karatsuba-type three-product identity',
'candidate_space': 'Three distinct atoms from 16 bilinear atoms',
'valid_laws': [x[2] for x in valid]}
if __name__ == '__main__':
print(json.dumps(discover(), indent=2))