#!/usr/bin/env python3 """`moments_fast.polygon_moments_fast` validation. * On the actual `region_planes` geometries the correction uses, the fast function must equal `radial_transform.polygon_moments` **bit-for-bit** in the degree range used (that is the property that makes it a drop-in speedup). * On random convex half-plane polygons the fast function is checked against an independent exact reference (a fan triangulation with the simplex monomial integral `int s^p t^q = p! q!/(p+q+2)!`). For several random polygons the *slow* function disagrees with the reference; the fast boundary-integral form is the correct one there. That slow-function defect is outside the region geometries actually used and does not affect the certificate. Final line `MOMENTS-FAST PASS`. """ from __future__ import annotations import os import random import sys from fractions import Fraction as Fr from math import factorial HERE = os.path.dirname(os.path.abspath(__file__)) sys.path.insert(0, os.path.join(HERE, "..", "src")) sys.path.insert(0, os.path.join(HERE, "..", "..", "..", "lib", "maynard")) from capped_numerator import polygon_vertices, region_planes # noqa from moments_fast import polygon_moments_fast # noqa from radial_transform import polygon_moments # noqa def _trinom(c0, c1, c2, n): cur = {(0, 0): Fr(1)} for _ in range(n): nxt = {} for (p, q), v in cur.items(): nxt[(p, q)] = nxt.get((p, q), Fr(0)) + v * c0 nxt[(p + 1, q)] = nxt.get((p + 1, q), Fr(0)) + v * c1 nxt[(p, q + 1)] = nxt.get((p, q + 1), Fr(0)) + v * c2 cur = nxt return cur def _tri_moment(P0, P1, P2, a, b): uy, uz = P1[0] - P0[0], P1[1] - P0[1] vy, vz = P2[0] - P0[0], P2[1] - P0[1] det = uy * vz - uz * vy A = _trinom(P0[0], uy, vy, a) B = _trinom(P0[1], uz, vz, b) tot = Fr(0) for (p1, q1), va in A.items(): for (p2, q2), vb in B.items(): p, q = p1 + p2, q1 + q2 tot += va * vb * Fr(factorial(p) * factorial(q), factorial(p + q + 2)) return tot * abs(det) def _reference(V, D): M = [[Fr(0)] * (D + 1) for _ in range(D + 1)] for i in range(D + 1): for k in range(D + 1): M[i][k] = sum(_tri_moment(V[0], V[t], V[t + 1], i, k) for t in range(1, len(V) - 1)) return M def main(): ok = True random.seed(2026) slow_defects = 0 for trial in range(150): n = random.randint(3, 6) planes = [(Fr(random.randint(-3, 3)), Fr(random.randint(-3, 3)), Fr(random.randint(0, 4))) for _ in range(n)] planes = [(a, b, c) for (a, b, c) in planes if a or b] if len(planes) < 3: continue V = polygon_vertices(planes) if len(V) < 3: continue D = random.randint(0, 6) ref = _reference(V, D) fast = polygon_moments_fast(planes, D) slow = polygon_moments(planes, D) if fast != ref: ok = False print(f"FAST vs reference mismatch trial={trial} planes={planes}") break if slow != ref: slow_defects += 1 print(f"random polygons vs reference: fast OK={ok} " f"(slow differs from reference on {slow_defects} cases)", flush=True) A_SRC = Fr(2583, 10000) eps = Fr(25, 861) U, ell = 1 + eps, 1 - eps d = Fr(3, 250) / A_SRC region_ok = True for k in (4, 5, 6): caps = [Fr(0)] + [Fr(3, 20) / A_SRC] * 2 + [Fr(4, 25) / A_SRC] * (k + 2) n = k - 1 for K in ("C", "D", "E"): for r in range(0, n + 1): s = n - r for jj in range(0, s + 1): if K == "E" and (r == 0 or r * d > ell): continue if K != "E" and r * d > caps[r]: continue if (r + jj) * d > ell: continue planes = region_planes(K, r, jj, d, U, ell, caps) for D in (2, 5, 9, 20): if polygon_moments(planes, D) != \ polygon_moments_fast(planes, D): region_ok = False print(f"region mismatch {K} r={r} jj={jj} D={D}") print("region geometry fast == slow bit-exact:", "OK" if region_ok else "FAIL", flush=True) ok &= region_ok print("MOMENTS-FAST", "PASS" if ok else "FAIL") return 0 if ok else 1 if __name__ == "__main__": raise SystemExit(main())