#!/usr/bin/env python3 """band_family.py -- the Vaughan cutoff family on the low part *and* on the band, at x = 2^j. Job #4209 (rescue, route 89). Every definition is the served one, cited, not invented here: research/fixed-endpoint-discrepancy.md (2.1), (2.5), (2.6), (2.8)-(2.9), section 4.3; return #1097 `window_increments.py` (the family U |-> tI(U) at fixed V); return #1831 `fam2070.c` (the chain, the c-array, the main term MT(U), the controls); research/moving-cutoff-parity.md (13) for the 2x/25 allowance. x = 2^j, J = (x/2, x], I_e = (x/(2e), x/e] integers, EPS = 1/60 e_0 = floor(x^(1/2-EPS)), e_1 = floor(x^(1/2+EPS)) low = odd squarefree e < e_0 band = odd squarefree e in [e_0, e_1) P_R = -sum_{e in R} sum_{m in I_e, (m,e)=1} mu(m) log m Lambda(em-2) (flip, (2.1)) c_U(r) = sum_{dk=r, d<=U, k<=V} mu(d) mu(k) ((2.6)) tI_R(U) = sum_{e in R} sum_{m in I_e, m>V, (m,e)=1} log m Lambda(em-2) TI_U(m), TI_U(m) = sum_{r|m, r<=U*V} c_U(r) ((2.6)) tII_R(U) = P_R - tI_R(U) (identity (2.5) applied to R) MT(U) = sum_{eV, (m,e)=1} log m Lambda(em-2), A_e(U) = sum_{r<=UV, r odd, (r,e)=1} c_U(r)/phi(r) (#1831 (2)) D^(e_1) = P_low + P_band - (M S_1(e_1) - F_1 S_0(e_1)) with M = sum_{n in J} mu(n)Lambda(n-2), F_1 = sum_{n in J} mu(n) Lambda(n-2) log n, S_k(u) = sum_{e list, exactly as fam2070.c accumulates it.""" R = chain[-1] * V c = {} for U in chain: arr = [0.0] * (R + 1) for d in range(1, U + 1): if mu_d(d) != 0: for k in range(1, V + 1): if mu_d(k) != 0: arr[d * k] += float(mu_d(d)) * float(mu_d(k)) c[U] = arr return c def mu_d(n): """mu(n) for the small n the c-array needs.""" if n < 1: return 0 t, m = n, 1 p = 2 while t > 1: while p * p <= t and t % p: p += 1 if p * p > t: p = t t //= p if t % p == 0: return 0 m = -m return m def spf_sieve(x): spf = np.zeros(x + 1, dtype=np.int64) for p in range(2, x + 1): if spf[p] == 0: spf[p::p] = np.where(spf[p::p] == 0, p, spf[p::p]) return spf def divisors_le(m, lim, spf): """All divisors r <= lim of m (not only the squarefree ones: c_U(r) != 0 at r = 4, 9, ...).""" t, pr = m, [] while t > 1: p = int(spf[t]) k = 0 while t % p == 0: t //= p k += 1 pr.append((p, k)) out = [1] for p, k in pr: cur = list(out) pk = 1 for _ in range(k): pk *= p for d in cur: if d * pk <= lim: out.append(d * pk) return out def sqfree_divisors(m, spf): """(d, mu(d)) for every squarefree d | m.""" t, pr = m, [] while t > 1: p = int(spf[t]) pr.append(p) while t % p == 0: t //= p ds, sg = [1], [1] for p in pr: for d, s in list(zip(ds, sg)): ds.append(d * p) sg.append(-s) return ds, sg def pairs(x, e_hi, mu, lam, min_m): """Surviving (e, m) pairs with x/2 < e m <= x, e < e_hi odd squarefree, m >= min_m, (m,e)=1. min_m = 1 gives the validator's own P-loop set (the invariant's pieces); min_m = V+1 gives the set the Vaughan family is defined on (m > max(U,V), so the linear terms of (V) vanish). """ E, M, N, W = [], [], [], [] for e in range(1, e_hi): if e % 2 == 0 or mu[e] == 0: continue lo = x // (2 * e) hi = x // e start = max(lo + 1, min_m) if start > hi: continue ms = np.arange(start, hi + 1, dtype=np.int64) ns = ms * int(e) lv = lam[ns - 2] keep = lv > 0.0 if not keep.any(): continue ms, ns, lv = ms[keep], ns[keep], lv[keep] if e > 1: cp = np.gcd(ms, np.int64(e)) == 1 if not cp.all(): ms, ns, lv = ms[cp], ns[cp], lv[cp] if ms.size == 0: continue w = np.log(ms.astype(np.float64)) * lv E.append(np.full(ms.size, e, dtype=np.int64)) M.append(ms) N.append(ns) W.append(w) if not E: z = np.zeros(0, dtype=np.int64) return {"e": z, "m": z, "n": z, "w": np.zeros(0), "mu_m": np.zeros(0)} e_arr = np.concatenate(E) m_arr = np.concatenate(M) n_arr = np.concatenate(N) w_arr = np.concatenate(W) return {"e": e_arr, "m": m_arr, "n": n_arr, "w": w_arr, "mu_m": mu[m_arr].astype(np.float64)} def tii_direct(m, U, V, spf): """TII_U(m) = sum_{a|m, a>U} mu(a) gamma_V(m/a), gamma_V(b) = sum_{k|b, k>V} mu(k).""" ds, sg = sqfree_divisors(m, spf) tot = 0.0 for d, s in zip(ds, sg): if d <= U: continue b = m // d ks, ksgn = sqfree_divisors(b, spf) ggt = sum(sg2 for kk, sg2 in zip(ks, ksgn) if kk > V) tot += s * ggt return tot def main() -> int: ap = argparse.ArgumentParser() ap.add_argument("--j", required=True) ap.add_argument("--V", required=True) ap.add_argument("--chain", default="1,2,4,8,16,32") ap.add_argument("--refine", action="store_true") ap.add_argument("--profile", action="store_true") ap.add_argument("--sample", type=int, default=100, help="pairs sampled for the I2/I3 controls") ap.add_argument("--out") a = ap.parse_args() sys.stdout.reconfigure(newline="\n", encoding="utf-8", errors="replace") js = [] for part in a.j.split(","): if "-" in part: lo, hi = part.split("-") js += list(range(int(lo), int(hi) + 1)) else: js.append(int(part)) Vs = [int(v) for v in a.V.split(",")] chain_in = [int(u) for u in a.chain.split(",")] lines = [] for j in js: for V in Vs: chain = [u for u in chain_in if u <= V] or [1] chain_ref = sorted(set(chain + [(chain[i] + chain[i + 1]) // 2 for i in range(len(chain) - 1)])) x = 1 << j mu, phi, lam = sieve(x) g = g_le(x, mu, V) ti = ti_arrays(x, mu, V, sorted(set(chain) | set(chain_ref)), g) e0 = int(math.floor(x ** (0.5 - EPS))) e1 = int(math.floor(x ** (0.5 + EPS))) # PF: the validator's own P-set (no m restriction) -- the invariant's pieces. # P: the Vaughan set (m > V) -- the family, tI/tII and every control. PF = pairs(x, e1, mu, lam, 1) P = pairs(x, e1, mu, lam, V + 1) low_m = P["e"] < e0 band_m = ~low_m lowF = PF["e"] < e0 bandF = ~lowF res = {"j": j, "V": V, "x": x, "e0": e0, "e1": e1, "chain": chain, "chain_ref": chain_ref, "pairs_low": int(low_m.sum()), "pairs_band": int(band_m.sum()), "pairs_full_low": int(lowF.sum()), "pairs_full_band": int(bandF.sum())} # the flipped term: -mu(m) log m Lambda(em-2) = -mu(m) * w, w = log m Lambda(em-2) rvF = -PF["mu_m"] * PF["w"] # one factor of log m Lambda(em-2) inside w P_low = float(rvF[lowF].sum()) / x P_band = float(rvF[bandF].sum()) / x res["P_low_over_x"] = round(P_low, 12) res["P_band_over_x"] = round(P_band, 12) res["invariant_over_x"] = round(P_low + P_band, 12) nn = np.arange(x // 2 + 1, x + 1, dtype=np.int64) f = mu[nn].astype(np.float64) * lam[nn - 2] M = float(f.sum()) F1 = float(np.dot(f, np.log(nn.astype(np.float64)))) odd = np.arange(1, e1, 2, dtype=np.int64) s0 = float(np.sum(mu[odd].astype(np.float64) / phi[odd])) s1 = float(np.sum(mu[odd].astype(np.float64) * np.log(odd.astype(np.float64)) / phi[odd])) res["M_over_x"] = round(M / x, 12) res["F1_over_x"] = round(F1 / x, 12) res["S0_e1"] = round(s0, 12) res["S1_e1"] = round(s1, 12) res["D_over_x"] = round((P_low + P_band) - (M * s1 - F1 * s0) / x, 12) for name, mask in (("low", low_m), ("band", band_m)): blk = {} for cname, cset in (("C", chain), ("Cref", chain_ref)): if cname == "Cref" and not a.refine: continue vals = [float(np.dot(P["w"][mask], ti[u][P["m"][mask]])) for u in cset] incr = [vals[i + 1] - vals[i] for i in range(len(vals) - 1)] Vx = sum(abs(v) for v in incr) / x Tx = sum(incr) / x members = [round(v / x, 12) for v in vals] P_R = P_low if name == "low" else P_band # the complement is the exact remainder P_R - tI(U); its *direct* TII form is # checked separately (mu_violations), which is the validator's own tI+tII=Plow # control written as mu(m) = TII_U(m) - TI_U(m). blk[cname] = { "members_over_x": members, "increments_over_x": [round(v / x, 12) for v in incr], "complement_over_x": [round(P_R - v / x, 12) for v in vals], "Vx": round(Vx, 12), "Tx": round(Tx, 12), "ratio": round(Tx / Vx, 9) if Vx else None, "allowance_ratio": round(max((abs(v) for v in incr), default=0.0) / x / ALLOWANCE, 9), } res[name] = blk # the linear-term correction: what the m > V restriction removes from P_R. # (V) makes mu(m) = TII(m) - TI(m) only for m > max(U,V); for smaller m the two # linear terms mu_{<=U}, mu_{<=V} are present, so the family's pieces sum to P_R minus # this mass. Measured, not assumed, and reported per part. fam_low_sum = -float(np.dot(P["w"][low_m], P["mu_m"][low_m])) / x fam_band_sum = -float(np.dot(P["w"][band_m], P["mu_m"][band_m])) / x res["linear_term_low_over_x"] = round(P_low - fam_low_sum, 12) res["linear_term_band_over_x"] = round(P_band - fam_band_sum, 12) # main term MT(U) of #1831, recomputed as a control on the low part if V >= 2: cU = {U: c_array(V, [U])[U] for U in chain} ev = np.arange(1, e0, dtype=np.int64) Ws = np.zeros(x + 1, dtype=np.float64) np.add.at(Ws, P["e"][low_m], P["w"][low_m]) MT = [] for U in chain: A = np.zeros(e0, dtype=np.float64) for r in range(1, U * V + 1, 2): if cU[U][r] != 0.0: A[ev] += (cU[U][r] / phi[r]) * (np.gcd(ev, np.int64(r)) == 1) MT.append(round(float(np.dot(Ws[1:e0], A[1:e0])) / x, 12)) res["MT_over_x"] = MT if a.profile: contrib = rvF / x prof, edge = [], 1 while edge < e1: nxt = min(edge * 2, e1) m = (PF["e"] >= edge) & (PF["e"] < nxt) prof.append({"e_lo": edge, "e_hi": nxt, "partial_over_x": round(float(contrib[m].sum()), 12)}) edge = nxt res["profile"] = prof # controls I2 (Vaughan identity), I3 (window identity + c-array agreement) spf = spf_sieve(x) idx = np.nonzero(P["e"] < e1)[0] if idx.size: step = max(1, idx.size // max(1, a.sample)) bad, wres, cbad, checked = 0, 0.0, 0, 0 cg = c_array(V, chain) for k in idx[::step]: m = int(P["m"][k]) for U in (chain[0], chain[-1]): tii = tii_direct(m, U, V, spf) checked += 1 if abs((tii - float(ti[U][m])) - float(mu[m])) > 1e-9: bad += 1 ds, sg = sqfree_divisors(m, spf) for U in chain: from_sieve = float(ti[U][m]) from_c = sum(cg[U][r] for r in divisors_le(m, U * V, spf)) if abs(from_sieve - from_c) > 1e-9: cbad += 1 for i in range(len(chain) - 1): Ua, Ub = chain[i], chain[i + 1] direct = 0.0 for d, _ in zip(ds, sg): if Ua < d <= Ub: direct += float(mu[d]) * float(g[m // d]) wres = max(wres, abs((float(ti[Ub][m]) - float(ti[Ua][m])) - direct)) res["mu_violations"] = bad res["mu_checked"] = checked res["c_gather_mismatches"] = cbad res["window_resid"] = "%.3g" % wres line = json.dumps(res, sort_keys=True) lines.append(line) print(line, flush=True) note("j=%d V=%d e0=%d e1=%d pairs %d/%d done" % (j, V, e0, e1, int(low_m.sum()), int(band_m.sum()))) if a.out: with open(a.out, "w", encoding="utf-8", newline="\n") as fh: fh.write("\n".join(lines) + "\n") return 0 if __name__ == "__main__": raise SystemExit(main())