"""Exact check of the four-state Mpemba example (Python standard library only). Builds the generator for 0<=epsilon<=1e-6, tests the four triplet conditions, verifies the characteristic polynomial by permutation expansion, isolates the eigenvalues, encloses a left eigenvector by interval Cramer's rule, and bounds the slow-mode amplitudes at the two initial temperatures. All decisions use Fraction arithmetic and explicit exceptions (also under -O). Usage: python check_four_state.py (writes four-state-receipt.json beside it) """ from fractions import Fraction as F from itertools import permutations, combinations from pathlib import Path import hashlib import json import time def require(condition, message): if not condition: raise ValueError(message) class I: def __init__(self, lo=0, hi=None): if isinstance(lo, I): self.lo, self.hi = lo.lo, lo.hi else: self.lo, self.hi = F(lo), F(lo if hi is None else hi) require(self.lo <= self.hi, "reversed interval") def __add__(self, other): other = I(other) return I(self.lo + other.lo, self.hi + other.hi) __radd__ = __add__ def __neg__(self): return I(-self.hi, -self.lo) def __sub__(self, other): return self + (-I(other)) def __rsub__(self, other): return I(other) - self def __mul__(self, other): other = I(other) products = [a * b for a in (self.lo, self.hi) for b in (other.lo, other.hi)] return I(min(products), max(products)) __rmul__ = __mul__ def __truediv__(self, other): other = I(other) require(not other.lo <= 0 <= other.hi, "division interval contains zero") return self * I(1 / other.hi, 1 / other.lo) def __rtruediv__(self, other): return I(other) / self def parity(p): return -1 if sum(p[i] > p[j] for i in range(len(p)) for j in range(i + 1, len(p))) % 2 else 1 def determinant(a): n = len(a) result = 0 for p in permutations(range(n)): term = parity(p) for i in range(n): term = term * a[i][p[i]] result = result + term return result def padd(a, b): return [(a[i] if i < len(a) else F(0)) + (b[i] if i < len(b) else F(0)) for i in range(max(len(a), len(b)))] def pmul(a, b): c = [F(0)] * (len(a) + len(b) - 1) for i, x in enumerate(a): for j, y in enumerate(b): c[i + j] += x * y return c def charpoly(a): n = len(a) result = [F(0)] * (n + 1) for p in permutations(range(n)): term = [F(parity(p))] for i in range(n): factor = [-a[i][p[i]], F(1)] if i == p[i] else [-a[i][p[i]]] term = pmul(term, factor) result = padd(result, term) return result def peval(p, x): v = F(0) for c in reversed(p): v = v * x + c return v delta = F(1, 2**64) q = [F(1), F(1, 2), F(1, 4), delta] emax = F(1, 10**6) def generator(eps, bath_q=None): bath_q = q if bath_q is None else bath_q weights = {(0, 1): 1 + eps, (0, 2): F(5, 3), (0, 3): F(2), (1, 2): F(102), (1, 3): F(3), (2, 3): 3 + eps} m = [[F(0) for _ in range(4)] for _ in range(4)] for (i, j), d in weights.items(): m[i][j], m[j][i] = bath_q[i] * d, bath_q[j] * d for j in range(4): m[j][j] = -sum(m[i][j] for i in range(4) if i != j) return m def outward(x, digits=12): scale = 10**digits lo = (x.lo * scale).__floor__() hi = (x.hi * scale).__ceil__() return [str(F(lo, scale)), str(F(hi, scale))] def record_interval(x): return {"exact": [str(x.lo), str(x.hi)], "outward_12_digit_rationals": outward(x)} def main(): started = time.perf_counter() residual_receipt = [] for eps in (F(0), emax): m = generator(eps) for j in range(4): require(sum(m[i][j] for i in range(4)) == 0, "not column conservative") for i in range(4): require(sum(m[i][j] * q[j] for j in range(4)) == 0, "stationarity failure") for j in range(4): require(m[i][j] * q[j] == m[j][i] * q[i], "balance failure") if i != j: require(m[i][j] >= 2 * delta > 0, "rate positivity failure") for u, v, w in combinations(range(4), 3): x, y, z, s = m[u][v], m[u][w], m[v][w], q[v] / q[u] k = (y + z) / (x * (1 + s)) r1 = y * (1 + q[w] / (2 * q[u])) - z * (1 + q[w] / (2 * q[v])) - x * (1 - s) r2 = y * s - z passed = (k >= 1 and r1 > 0) or (k <= 1 and r2 > 0) require(not passed, "original triplet test passes") relevant = r1 if k >= 1 else r2 require(relevant < 0 if eps > 0 else relevant <= 0, "wrong residual strictness") residual_receipt.append({"eps": str(eps), "triplet": ''.join(str(i + 1) for i in (u, v, w)), "k": str(k), "residual_1": str(r1), "residual_2": str(r2)}) m0 = generator(F(0)) p = charpoly(m0) def claimed_cubic(d): return [(4*d+7)*(432*d**2+11598*d+9707)/96, (1008*d**2+20380*d+24331)/48, (24*d+253)/3, F(1)] f = claimed_cubic(delta) require(p == [F(0)] + f, "claimed characteristic polynomial mismatch") # Each determinant coefficient is a polynomial in d of degree at most 4, # since every entry of this 4x4 matrix is affine in d. Five exact # evaluations therefore also authenticate the displayed symbolic identity. for d in map(F, range(5)): require(charpoly(generator(F(0), [F(1), F(1, 2), F(1, 4), d])) == [F(0)] + claimed_cubic(d), "symbolic characteristic polynomial interpolation check failed") baseline = [I('-2.13660892001', '-2.13660892000'), I('-4.25000000001', '-4.24999999999'), I('-77.94672441334', '-77.94672441332')] root_signs = [] for interval in baseline: left, right = peval(f, interval.lo), peval(f, interval.hi) require(left * right < 0, "root bracket sign failure") root_signs.append([1 if left > 0 else -1, 1 if right > 0 else -1]) require(baseline[0].hi < 0 and all(baseline[i+1].hi < baseline[i].lo for i in (0, 1)), "root intervals not ordered") # The proof uses two disjoint edge vectors. Their squared norms are the # only nonzero eigenvalues of the sum of their two rank-one matrices. require(q[0] + q[1] == F(3, 2) and q[2] + q[3] < F(3, 2), "Weyl norm bound failure") spectra = [I(v.lo - F(3, 2) * emax, v.hi) for v in baseline] require(all(spectra[i+1].hi < spectra[i].lo for i in (0, 1)), "perturbed gap failure") # Solve (M^T-mu I)ell=0 with ell[1]=1. Delete row and column 1. mi = generator(I(0, emax)) mu = spectra[0] b = [[mi[j][i] - (mu if i == j else 0) for j in range(4)] for i in range(4)] unknown = [0, 2, 3] small = [[b[i][j] for j in unknown] for i in unknown] rhs = [-b[i][1] for i in unknown] den = I(determinant(small)) require(not den.lo <= 0 <= den.hi, "singular eigenvector normalization submatrix") ell = [I(0), I(1), I(0), I(0)] for column, j in enumerate(unknown): replaced = [[rhs[r] if c == column else small[r][c] for c in range(3)] for r in range(3)] ell[j] = I(determinant(replaced)) / den require(ell[0].hi < 0 < ell[3].lo and ell[3].hi < ell[2].lo < ell[2].hi < 1, "claimed eigenvector signs/order not certified") th, tc = F(255, 256), F(127, 128) require(F(1, 2) < tc < th < 1, "temperature order failure") def thermal(t): raw = [F(1), t, t*t, t**64] return [v / sum(raw) for v in raw] ph, pc = thermal(th), thermal(tc) bh, bc = sum(ell[i]*ph[i] for i in range(4)), sum(ell[i]*pc[i] for i in range(4)) gap = sum(ell[i]*(pc[i]-ph[i]) for i in range(4)) require(bh.lo > 0 and bc.lo > 0 and gap.lo > 0, "positive amplitude reversal not certified") # Coarser bounds, independent of decimal display rounding. require(bh.lo > F(398, 1000) and bc.hi < F(400, 1000) and gap.lo > F(277, 10**6), "independent coarse bounds fail") derivative_zero = (67*ell[0] + 63 + 59*ell[2] - 189*ell[3])/16 derivative_bath = -(F(1, 2) + F(1, 2)*ell[2] + 64*delta*ell[3])/(F(7, 4)+delta) require(derivative_zero.lo > 0 and derivative_bath.hi < 0, "derivative sign check failure") receipt = { "status": "PASS", "arithmetic": "Python standard-library Fraction; all comparisons exact", "scope": "Whole family 0<=epsilon<=1/1000000; all triplet relevant residuals strict for epsilon>0", "delta": str(delta), "triplets_at_affine_endpoints": residual_receipt, "characteristic_polynomial_coefficients_ascending": [str(c) for c in p], "symbolic_characteristic_identity": "5 exact delta evaluations 0,1,2,3,4; coefficient degree bound <=4", "root_endpoint_signs": root_signs, "perturbed_spectral_intervals": [record_interval(v) for v in spectra], "cramer_denominator": record_interval(den), "ell_intervals": [record_interval(v) for v in ell], "hot_amplitude": record_interval(bh), "cold_amplitude": record_interval(bc), "cold_minus_hot": record_interval(gap), "bprime_zero_over_U": record_interval(derivative_zero), "bprime_bath_over_U": record_interval(derivative_bath), "seconds": time.perf_counter()-started, "script_sha256": hashlib.sha256(Path(__file__).read_bytes()).hexdigest(), } target = Path(__file__).with_name('four-state-receipt.json') target.write_text(json.dumps(receipt, indent=2) + '\n', encoding='utf-8') print(json.dumps({"status": receipt['status'], "seconds": receipt['seconds'], "ell_outward": [outward(v) for v in ell], "hot_outward": outward(bh), "cold_outward": outward(bc), "gap_outward": outward(gap), "receipt": str(target)}, indent=2)) if __name__ == '__main__': main()