# -*- coding: utf-8 -*- """The two measurements return #775 pre-registered, on the corpus's own objects. M1 (nu-mass profile). For a modulus n, K_L(nu/n) = Fejer kernel, and sum_{nu mod n} K_L(nu/n) e(-nu c/n) = n * phi_n(c), phi_n(c) = sum_{|h| 1 (the two-branch type), de <= L ln^2 y (the corpus's own effective cutoff). """ import math, sys, time sys.stdout.reconfigure(encoding='utf-8') # (L, y) read from the corpus's own producer, research/history/staging/varE-theta2-proof.js LV = {7: (210, 13), 11: (2310, 47), 13: (30030, 173), 17: (510510, 709), 19: (9699690, 3109), 23: (223092870, 14929)} def primes_upto(n): s = bytearray([1]) * (n + 1) s[0:2] = b'\x00\x00' for i in range(2, int(n ** 0.5) + 1): if s[i]: s[i * i::i] = bytearray(len(s[i * i::i])) return [i for i in range(2, n + 1) if s[i]] # ---------------------------------------------------------------- M1 def phi0(L, n): """phi_n(0) = sum_{|h| 0 else float('nan') # 99% support scale: smallest T with 2*sum_{nu<=T}/Mn >= 0.99 T, s2 = 0, 0.0 if frac < 0.99: nu = 1 while s2 < 0.99 * Mn and nu < n // 2: s2 += 2 * fejer(L, n, nu) nu += 1 T = nu out.append(dict(n=n, n_over_L=n / L, sqrt_n=r, mass=Mn, frac_within_sqrtn=frac, T99=T, T99_over_sqrtn=(T / r if r else None))) return out # ---------------------------------------------------------------- M2 def delta(L, n, c): """phi_n(c) - L/n, closed form; c in [0,n).""" # positive side: h = c + j n, j >= 0, h < L -> Jp = (L-1-c)//n Jp = (L - 1 - c) // n if L - 1 - c >= 0 else -1 # negative side: h = c - j n, j >= 1, c - j n > -L -> j <= ceil((L+c)/n) - 1 Jm = ((L + c + n - 1) // n) - 1 ph = 0.0 if Jp >= 0: ph += (Jp + 1) * (1 - c / L) - (n / L) * Jp * (Jp + 1) / 2 if Jm >= 1: ph += Jm * (1 + c / L) - (n / L) * Jm * (Jm + 1) / 2 return ph - L / n def norm_lam(L, ps): """The corpus's own normalisation D_y = prod p(p-4)/(p-2)^2 (not needed for a ratio).""" D = 1.0 for p in ps: D *= p * (p - 4) / ((p - 2) ** 2) return D def m2_fast(x, N_div=1, max_s=None, floor=None, chunk=(0, None)): """The blocking-band pair sum, canonicalised by s = min(d,e) -- NOT by smallest prime. These differ, and the difference is not cosmetic: with s = the part carrying the smallest prime, a pair like {7*101, 103} has s = 707 > L^{2/5} while min(d,e) = 103 is inside the band, so the band test on s would drop a genuine band pair. Here s = min(d,e) by construction (t >= s is enforced at accumulation) and every prime not dividing s is allowed in t. Both ordered contributions are added for each unordered pair, since lam0 attaches to d and lam1 to e and the roles are not symmetric: (d=s,e=t) and (d=t,e=s) carry different classes c/n = 2 inv(d)/e mod 1 and different weights. """ L, y = LV[x] ps = [p for p in primes_upto(y) if p >= 7] N = int(L * math.log(y) ** 2) // N_div Bc = L ** 0.4 # exact, not truncated lo = floor if floor is not None else 2 # d,e >= 2: the two-branch type needs both nontrivial hi = max_s if max_s is not None else Bc clo, chi = chunk chi = hi if chi is None else chi # the chunk windows the s-range; hi is the cap # s = min(d,e) <= sqrt(N) always: without this the s-walk descends every y-smooth product # up to the cap and never returns (found live on the complement pass, which sets no band cap) s_cap = min(hi, math.isqrt(N)) t0 = time.time() acc = [0.0, 0.0, 0] # signed, absolute, ordered contributions rows = [] pset = ps def add_pair(s, l0s, l1s, t, l0t, l1t): """Both orders, each with ITS OWN role assignment: lam0 goes to d, lam1 to e. (d=s,e=t) carries lam0(s) lam1(t); (d=t,e=s) carries lam0(t) lam1(s). Keeping the two role values separately is not bookkeeping pedantry -- using one set for both orders made the x = 7 unit test disagree with brute force by a factor 1.31. """ n = s * t tau = (2 * pow(s, -1, t)) % t d1 = delta(L, n, (s * tau) % n) w1 = l0s * l1t tau2 = (2 * pow(t, -1, s)) % s d2 = delta(L, n, (t * tau2) % n) w2 = l0t * l1s acc[0] += w1 * d1 + w2 * d2 acc[1] += abs(w1 * d1) + abs(w2 * d2) acc[2] += 2 def rec_s(i, s, sprimes, l0s, l1s): for j in range(i, len(ps)): p = ps[j] ss = s * p if ss > s_cap: break ns = sprimes + (p,) nl0, nl1 = l0s * 2.0 / (p - 4), l1s * 1.0 / (p - 4) if max(lo, clo) <= ss <= min(hi, chi): lim = N // ss if lim >= ss: # every prime not dividing ss, t built in increasing order so each t once allowed = [q for q in pset if q <= lim and q not in ns] def rec_t(k, t, l0t, l1t): if t >= ss: add_pair(ss, nl0, nl1, t, l0t, l1t) for m in range(k, len(allowed)): q = allowed[m] if t * q > lim: break rec_t(m + 1, t * q, l0t * 2.0 / (q - 4), l1t * 1.0 / (q - 4)) for k in range(len(allowed)): q = allowed[k] if q > lim: break rec_t(k + 1, q, 2.0 / (q - 4), 1.0 / (q - 4)) rows.append((ss, acc[0], acc[1], acc[2])) rec_s(j + 1, ss, ns, nl0, nl1) rec_s(0, 1, (), 1.0, 1.0) rows.sort() step = max(1, len(rows) // 40) return dict(x=x, L=L, y=y, N=N, Bc=Bc, lo=lo, hi=hi, chunk=[clo, chi], band=acc, R_band=(abs(acc[0]) / acc[1] if acc[1] else None), profile=[[r[0], r[1], r[2], r[3], (abs(r[1]) / r[2] if r[2] else None)] for r in rows[::step]], secs=round(time.time() - t0, 1)) if __name__ == '__main__': what = sys.argv[1] if what == 'm1': x = int(sys.argv[2]) L, y = LV[x] ns = [int(v) for v in sys.argv[3:]] print(m1(L, ns)) elif what == 'm2': x = int(sys.argv[2]) div = int(sys.argv[3]) if len(sys.argv) > 3 else 1 ms = int(sys.argv[4]) if len(sys.argv) > 4 else None print(m2_fast(x, N_div=div, max_s=ms))