"""Exact rational check of a four-state Mpemba witness (no floating point). The energies are integer multiples E_i = n_i U of a unit U, and the three inverse temperatures enter only through the rational numbers x_b = exp(-beta_b U), x_h = exp(-beta_h U), x_c = exp(-beta_c U), so every Boltzmann weight x^(n_i) is rational. Input JSON (rational strings): n : integer exponents 0 = n_1 < n_2 < n_3 < n_4, x_b, x_h, x_c : 0 < x_b < x_c < x_h < 1 (i.e. beta_h < beta_c < beta_b), d : symmetric conductances d12, d13, d14, d23, d24, d34 (> 0), mu_brackets : three rational intervals claimed to contain the nonzero eigenvalues of M, largest first (each verified by a strict sign change of the exact characteristic polynomial). Rates (rate j -> i, i != j): a_ij = x_b^(n_i) d_ij (time unit tau = 1), so detailed balance with respect to q_i = x_b^(n_i) holds exactly. Steps (all in exact arithmetic over fractions.Fraction): 1. build M; check column sums 0, M q = 0, detailed balance, positivity; 2. the triplet tests of arXiv:2603.04567, Eq. (3): kappa != 1, the applicable residual < 0, exact relative margins; 3. det(zI - M) by expansion over the 24 permutations; constant term 0, det(zI - M) = z f(z) with f(0) != 0; strict sign changes of f at the bracket endpoints; brackets disjoint, negative and ordered; 4. the slow left eigenvector with ell_1 = 1 by Cramer's rule on rows and columns {2,3,4} of M^T - mu I, mu ranging over the lambda_2 bracket (exact rational interval arithmetic, no rounding); 0 is not in the denominator. The omitted row then holds by the Schur argument; 5. b(beta) = ell^T pi(beta) at beta_h, beta_c with exact rational pi; same sign, and |b(beta_c)| - |b(beta_h)| > 0. Every decision raises an exception (active under python -O). Usage: python check_rational_four_state.py [witness.json] [receipt.json] (default witness: rational_witness.json beside this script). The receipt is printed and, if a second path is given, also written there. Python standard library only. """ from fractions import Fraction as F from itertools import permutations, combinations from pathlib import Path import hashlib import json import sys import time def require(condition, message): if not condition: raise ValueError(message) class I: """Closed interval [lo, hi] with exact rational endpoints.""" def __init__(self, lo, hi=None): if isinstance(lo, I): lo, hi = lo.lo, lo.hi lo = F(lo) hi = lo if hi is None else F(hi) require(lo <= hi, "reversed interval") self.lo, self.hi = lo, hi def __add__(self, o): o = o if isinstance(o, I) else I(o) return I(self.lo + o.lo, self.hi + o.hi) __radd__ = __add__ def __neg__(self): return I(-self.hi, -self.lo) def __sub__(self, o): o = o if isinstance(o, I) else I(o) return self + (-o) def __rsub__(self, o): return I(o) - self def __mul__(self, o): o = o if isinstance(o, I) else I(o) p = (self.lo * o.lo, self.lo * o.hi, self.hi * o.lo, self.hi * o.hi) return I(min(p), max(p)) __rmul__ = __mul__ def __truediv__(self, o): o = o if isinstance(o, I) else I(o) require(o.lo > 0 or o.hi < 0, "division by an interval containing 0") return self * I(min(1 / o.lo, 1 / o.hi), max(1 / o.lo, 1 / o.hi)) def contains_zero(self): return self.lo <= 0 <= self.hi def dec(x, digits=12, mode="near"): """Decimal string of a Fraction, rounded down/up/nearest at `digits`.""" s = 10 ** digits y = x * s if mode == "down": m = y.__floor__() elif mode == "up": m = y.__ceil__() else: m = round(y) sign = "-" if m < 0 else "" m = abs(m) return f"{sign}{m // s}.{m % s:0{digits}d}" def perm_sign(p): sign, seen = 1, [False] * len(p) for i in range(len(p)): if not seen[i]: j, length = i, 0 while not seen[j]: seen[j] = True j = p[j] length += 1 if length % 2 == 0: sign = -sign return sign def poly_mul(a, b): out = [F(0)] * (len(a) + len(b) - 1) for i, x in enumerate(a): for j, y in enumerate(b): out[i + j] += x * y return out def poly_eval(c, z): """c[k] is the coefficient of z^k.""" v = F(0) for coef in reversed(c): v = v * z + coef return v def det3(A): return (A[0][0] * (A[1][1] * A[2][2] - A[1][2] * A[2][1]) - A[0][1] * (A[1][0] * A[2][2] - A[1][2] * A[2][0]) + A[0][2] * (A[1][0] * A[2][1] - A[1][1] * A[2][0])) def main(): t0 = time.perf_counter() here = Path(__file__).resolve().parent wpath = Path(sys.argv[1]) if len(sys.argv) > 1 else here / "rational_witness.json" raw = wpath.read_bytes() W = json.loads(raw) n = [int(k) for k in W["n"]] xb, xh, xc = F(W["x_b"]), F(W["x_h"]), F(W["x_c"]) d = [F(v) for v in W["d"]] br = [(F(lo), F(hi)) for lo, hi in W["mu_brackets"]] N = 4 require(len(n) == N and n[0] == 0 and all(n[i] < n[i + 1] for i in range(N - 1)), "exponents must be 0 = n1 < n2 < n3 < n4") require(0 < xb < xc < xh < 1, "need 0 < x_b < x_c < x_h < 1 (beta_h < beta_c < beta_b)") require(len(d) == 6 and all(v > 0 for v in d), "need six positive conductances") require(len(br) == 3, "need three brackets") # 1. generator q = [xb ** k for k in n] D = [[F(0)] * N for _ in range(N)] for (i, j), v in zip(combinations(range(N), 2), d): D[i][j] = D[j][i] = v M = [[q[i] * D[i][j] if i != j else F(0) for j in range(N)] for i in range(N)] for j in range(N): M[j][j] = -sum(M[i][j] for i in range(N) if i != j) require(all(sum(M[i][j] for i in range(N)) == 0 for j in range(N)), "column sums") require(all(sum(M[i][j] * q[j] for j in range(N)) == 0 for i in range(N)), "M q = 0") require(all(M[i][j] * q[j] == M[j][i] * q[i] for i in range(N) for j in range(N)), "detailed balance") require(all(M[i][j] > 0 for i in range(N) for j in range(N) if i != j), "positive rates") # 2. triplet tests (a_{-0} = rate j -> i, a_{-+} = rate k -> i, a_{0+} = rate k -> j) triplets = {} worst = None for (i, j, k) in combinations(range(N), 3): am0, amp, a0p = M[i][j], M[i][k], M[j][k] e = q[j] / q[i] Cpm = 1 + q[k] / (2 * q[i]) Cp0 = 1 + q[k] / (2 * q[j]) kappa = (amp + a0p) / (am0 * (1 + e)) r1 = amp * Cpm - a0p * Cp0 - am0 * (1 - e) r2 = amp * e - a0p require(kappa != 1, "kappa = 1") branch, r = ("(3a)", r1) if kappa > 1 else ("(3b)", r2) require(r < 0, f"triplet {(i + 1, j + 1, k + 1)} passes its test") margin = -r / max(am0, amp, a0p) name = f"{i + 1}{j + 1}{k + 1}" triplets[name] = {"kappa": dec(kappa, 8), "branch": branch, "residual": dec(r, 12), "relative_margin_lower": dec(margin, 8, "down")} worst = margin if worst is None else min(worst, margin) # 3. characteristic polynomial det(zI - M), exact char = [F(0)] * (N + 1) for p in permutations(range(N)): term = [F(perm_sign(p))] for r in range(N): c = p[r] entry = [-M[r][c], F(1)] if r == c else [-M[r][c]] term = poly_mul(term, entry) for k, v in enumerate(term): char[k] += v require(char[N] == 1 and char[0] == 0, "det(zI-M) not monic with zero constant term") f = char[1:] # det(zI - M) = z f(z) require(f[0] != 0, "zero eigenvalue not simple") for lo, hi in br: require(lo < hi < 0, "bracket not negative") flo, fhi = poly_eval(f, lo), poly_eval(f, hi) require(flo != 0 and fhi != 0 and (flo > 0) != (fhi > 0), f"no strict sign change on [{lo}, {hi}]") require(br[0][0] > br[1][1] and br[1][0] > br[2][1], "brackets not disjoint and ordered") # 4. slow left eigenvector, ell_1 = 1, mu over the lambda_2 bracket mu = I(*br[0]) B = [[I(M[c][r]) - (mu if r == c else 0) for c in range(N)] for r in range(N)] # M^T - mu I J = [1, 2, 3] BJJ = [[B[r][c] for c in J] for r in J] rhs = [-B[r][0] for r in J] den = det3(BJJ) require(not den.contains_zero(), "det B_JJ contains 0") ell = [I(1)] for col in range(3): A = [[rhs[r] if c == col else BJJ[r][c] for c in range(3)] for r in range(3)] ell.append(det3(A) / den) # 5. amplitudes with exact rational pi def pi(x): w = [x ** k for k in n] Z = sum(w) return [v / Z for v in w] ph, pc = pi(xh), pi(xc) bh = sum((ell[i] * ph[i] for i in range(N)), I(0)) bc = sum((ell[i] * pc[i] for i in range(N)), I(0)) require(not bh.contains_zero() and not bc.contains_zero(), "amplitude interval contains 0") require((bh.lo > 0) == (bc.lo > 0), "amplitudes of different sign") s = 1 if bh.lo > 0 else -1 diff = sum((ell[i] * (pc[i] - ph[i]) for i in range(N)), I(0)) * s # |b_c| - |b_h| require(diff.lo > 0, "no Mpemba inequality |b(beta_h)| < |b(beta_c)|") rel = diff / (bh * s) receipt = { "status": "PASS", "witness_sha256": hashlib.sha256(raw).hexdigest(), "triplets": triplets, "worst_relative_margin_lower": dec(worst, 8, "down"), "f_coefficients_z0_to_z3": [dec(v, 10) for v in f], "mu_brackets": [[str(lo), str(hi)] for lo, hi in br], "det_BJJ": [dec(den.lo, 7, "down"), dec(den.hi, 7, "up")], "ell": [[dec(v.lo, 10, "down"), dec(v.hi, 10, "up")] for v in ell], "b_h": [dec(bh.lo, 10, "down"), dec(bh.hi, 10, "up")], "b_c": [dec(bc.lo, 10, "down"), dec(bc.hi, 10, "up")], "abs_bc_minus_abs_bh": [dec(diff.lo, 10, "down"), dec(diff.hi, 10, "up")], "relative_gap": [dec(rel.lo, 7, "down"), dec(rel.hi, 7, "up")], "seconds": round(time.perf_counter() - t0, 3), } text = json.dumps(receipt, indent=2) print(text) if len(sys.argv) > 2: Path(sys.argv[2]).write_text(text + "\n", encoding="utf-8") if __name__ == "__main__": main()