"""Symbolic tables for the linear regime (2m-1)a <= b <= 2ma of T(a,b), any m >= 2. alpha = b - (2m-1)a, beta = 2ma - b (so a = alpha+beta, b = 2m*alpha + (2m-1)*beta). 1. Compute the window table of many coprime games, merge equal consecutive rows/columns. 2. Sort the games by b/a and cut the sorted list wherever the merged table (shape and values) changes: these are the candidate ratio ranges. The cut point between two neighbouring groups is the simplest fraction between them. 3. In each group, fit every breakpoint as i*alpha + j*beta with one (i, j) valid for all games of the group. Output: data/three_symbolic_m{m}.json with, per range: lo, hi (fractions), xforms, yforms, table. The output is only a CANDIDATE; scripts/check_symbolic.py proves it for all parameters. Usage: python experiments/three_symbolic_gen.py m [amin amax] """ import sys, json, math from fractions import Fraction import numpy as np from multiprocessing import Pool sys.path.insert(0, 'experiments') from three_fast import sg_fast def window(m, a, b): p = 2 * m * a + b; r = 2 * m * b + a X = 3 * p + r + a + b Y = max(2 * p + 2 * r + 2 * b, 6 * m * b + a + 1) # the second term is Y1 + r + 1 with Y1 = 4mb return X, Y def table(arg): m, a, b = arg X, Y = window(m, a, b) G = sg_fast([(a, b), (b, a), (a + b, a + b)], X, Y).astype(np.int8) bx = [0] + [x for x in range(1, X) if np.any(G[x] != G[x - 1])] by = [0] + [y for y in range(1, Y) if np.any(G[:, y] != G[:, y - 1])] return a, b, bx, by, G[np.ix_(bx, by)].tolist() def simplest_between(lo, hi): """fraction with the smallest denominator strictly between lo and hi (Stern-Brocot)""" a, b, c, d = 0, 1, 1, 0 while True: med = Fraction(a + c, b + d) if med <= lo: a, b = a + c, b + d elif med >= hi: c, d = a + c, b + d else: return med def fit(S, m, axis): n = len(S[0][axis]); forms = [] for k in range(n): cands = None for s in S: a, b = s[0], s[1]; al, be = b - (2 * m - 1) * a, 2 * m * a - b; v = s[axis][k] if be == 0: c = {(v // al, 0)} if v % al == 0 else set() # end point b = 2ma: forms i*alpha only else: c = {(i, (v - i * al) // be) for i in range(0, v // al + 1) if (v - i * al) % be == 0} cands = c if cands is None else cands & c if not cands: break if len(cands) != 1: return None, (k, len(cands)) forms.append(list(next(iter(cands)))) return forms, None if __name__ == '__main__': m = int(sys.argv[1]); amin = int(sys.argv[2]) if len(sys.argv) > 2 else 7; amax = int(sys.argv[3]) if len(sys.argv) > 3 else 19 games = [(m, a, b) for a in range(amin, amax + 1) for b in range((2 * m - 1) * a + 1, 2 * m * a) if math.gcd(a, b) == 1] with Pool(120) as P: R = P.map(table, games, chunksize=1) R.sort(key=lambda s: Fraction(s[1], s[0])) groups = [] for s in R: key = json.dumps(s[4]) if groups and groups[-1][0] == key: groups[-1][1].append(s) else: groups.append((key, [s])) print(f'm={m}: {len(R)} games, a in [{amin},{amax}]; {len(groups)} groups along the ratio axis') cuts = [Fraction(2 * m - 1)] for g, h in zip(groups, groups[1:]): cuts.append(simplest_between(Fraction(g[1][-1][1], g[1][-1][0]), Fraction(h[1][0][1], h[1][0][0]))) cuts.append(Fraction(2 * m)) out = []; good = True for k, (key, S) in enumerate(groups): fx, ex = fit(S, m, 2); fy, ey = fit(S, m, 3) print(f' range {k}: ({cuts[k]}, {cuts[k+1]}) games {len(S)} cells {len(S[0][2])} x {len(S[0][3])} ' f'x-forms {"ok" if fx else "FAIL " + str(ex)} y-forms {"ok" if fy else "FAIL " + str(ey)}') if fx and fy: out.append(dict(lo=[cuts[k].numerator, cuts[k].denominator], hi=[cuts[k + 1].numerator, cuts[k + 1].denominator], xforms=fx, yforms=fy, table=S[0][4], ngames=len(S))) else: good = False # the end point b = 2ma (beta = 0) gets its own table, fitted on the scaled games (k, 2mk); the last regular range # is then taken half-open. (Needed for m >= 4, where the window top carries a constant term; harmless otherwise.) with Pool(8) as P: E = P.map(table, [(m, k, 2 * m * k) for k in range(1, 7)]) assert len({json.dumps(s[4]) for s in E}) == 1, 'end-point games do not share one table' fx, ex = fit(E, m, 2); fy, ey = fit(E, m, 3) print(f' end point b = {2*m}a: games {len(E)} cells {len(E[0][2])} x {len(E[0][3])} x-forms {"ok" if fx else "FAIL"} y-forms {"ok" if fy else "FAIL"}') if fx and fy and out: out[-1]['open_hi'] = True out.append(dict(lo=[2 * m, 1], hi=[2 * m, 1], xforms=fx, yforms=fy, table=E[0][4], ngames=len(E))) else: good = False json.dump(dict(m=m, ranges=out, complete=good), open(f'data/three_symbolic_m{m}.json', 'w')) print('cut points:', ', '.join(str(c) for c in cuts), '| all ranges fitted' if good else '| SOME RANGES NOT FITTED')