# -*- coding: utf-8 -*- """Validation of the two instruments, before any measurement is reported.""" import importlib.util, math, random, sys sys.stdout.reconfigure(encoding='utf-8') spec = importlib.util.spec_from_file_location('km', 'work/kl-measure.py') km = importlib.util.module_from_spec(spec) spec.loader.exec_module(km) print("== V1: Delta_n(c) = phi_n(c) - L/n, closed form vs brute force (the corpus's PART A test)") random.seed(20260917) worst = 0.0 for _ in range(4000): L = random.randint(1, 400) n = random.randint(1, 900) c = random.randint(0, n - 1) bf = 0.0 for h in range(-(L - 1), L): if h % n == c: bf += 1 - abs(h) / L worst = max(worst, abs(bf - L / n - km.delta(L, n, c))) print(" 4000 random (L,n,c): worst |closed - brute| = %.3e" % worst) assert worst < 1e-9 print("== V2: total nu-mass sum_{nu!=0} K_L(nu/n) = n*phi_n(0) - L, by direct Fejer summation") worst = 0.0 for L, n in [(210, 7), (210, 60), (30030, 5), (30030, 210), (30030, 1001), (30030, 977)]: direct = 0.0 for nu in range(1, n): direct += km.fejer(L, n, nu) cl = n * km.phi0(L, n) - L worst = max(worst, abs(direct - cl) / max(1.0, abs(cl))) print(" six (L,n) pairs: worst relative gap = %.3e" % worst) assert worst < 1e-9 print("== V3: the corpus's own PART B object at x = 7, rebuilt here (192 (modulus,class) pairs)") L, y = 210, 13 ps = [p for p in km.primes_upto(y) if p >= 7] D = 1.0 for p in ps: D *= p * (p - 4) / ((p - 2) ** 2) five = [(0, 15.0), (1, 7.5), (4, 7.5)] K = L / 6 tot = 0.0 abst = 0.0 pairs = 0 def recp(i, n, c, w): global tot, abst, pairs if i == len(ps): for c5, w5 in five: m = 5 * n cc = 0 for a in range(m): if a % n == c and a % 5 == c5: cc = a break r = K % m R = (max(0, r - cc) + max(0, r + cc - m) - r * r / m) / K tot += w * w5 * R abst += abs(w * w5 * R) pairs += 1 return p = ps[i] inv6 = pow(6, -1, p) t = (2 * inv6) % p recp(i + 1, n, c, w) for tau, ww in [(0, 2 / (p - 4)), (t, 1 / (p - 4)), ((p - t) % p, 1 / (p - 4))]: nn = n * p cc = 0 for A in range(nn): if A % n == c and A % p == tau: cc = A break recp(i + 1, nn, cc, w * ww) recp(0, 1, 0, D) print(" lag-expansion X = %.6f (corpus 4.612929) pairs = %d (corpus 192)" % (tot, pairs)) print(" sum|terms| = %.6f loss factor = %.2f (corpus 13.991099, 3.03)" % (abst, abst / abs(tot))) assert pairs == 192 and abs(tot - 4.612929) < 2e-4 print("== V4: the band enumeration against brute force, at a level small enough to enumerate") def brute(x): L, y = km.LV[x] ps = [p for p in km.primes_upto(y) if p >= 7] N = int(L * math.log(y) ** 2) Bc = L ** 0.4 ok = [] def rec(i, m, lam): ok.append((m, lam)) for j in range(i, len(ps)): mm = m * ps[j] if mm > N: break rec(j + 1, mm, lam * 1.0) def lam0(m): v = 1.0 for p in ps: if m % p == 0: v *= 2.0 / (p - 4) return v # all valid m with 2 <= m <= N//2 vals = [] def rec2(i, m): if m >= 2: vals.append(m) for j in range(i, len(ps)): mm = m * ps[j] if mm > N // 2: break rec2(j + 1, mm) rec2(0, 1) sg = ab = 0.0 cnt = 0 for d in vals: for e in vals: if e < d or math.gcd(d, e) != 1: continue if min(d, e) > Bc: continue if d * e > N: continue n = d * e for (dd, ee) in ((d, e), (e, d)): tau = (2 * pow(dd, -1, ee)) % ee dl = km.delta(L, n, (dd * tau) % n) w = (2.0 if dd == d else 1.0) * 0 lam = 1.0 for p in ps: if dd % p == 0: lam *= 2.0 / (p - 4) if ee % p == 0: lam *= 1.0 / (p - 4) sg += lam * dl ab += abs(lam * dl) cnt += 1 return sg, ab, cnt for xx in (7, 11): bs, ba, bc = brute(xx) rx = km.m2_fast(xx) print(" x=%-2d brute: signed %+.12f abs %.12f contributions %d" % (xx, bs, ba, bc)) print(" x=%-2d enum : signed %+.12f abs %.12f contributions %d" % (xx, rx['band'][0], rx['band'][1], rx['band'][2])) assert bc == rx['band'][2], 'pair count differs at x=%s' % xx assert abs(bs - rx['band'][0]) < 1e-9 and abs(ba - rx['band'][1]) < 1e-9, 'value differs at x=%s' % xx print("== V5: measurement 2 at x = 13, the corpus's own cutoff") res = km.m2_fast(13) print(" R_band = %.6f signed=%.6f abs=%.6f contributions=%d secs=%s" % (res['R_band'], res['band'][0], res['band'][1], res['band'][2], res['secs'])) print("VALIDATION OK")