every.py
Cited on: READING.
To run it: save every.py, with edge.py, heat.py, majorant.py, real_support.py, remainder.py, uniform.py beside it; needs mpmath, numpy, scipy.
"""
every.py -- the remainder R_maj read with the majorants at P0 = P,
at even and odd P, and a candidate P-free envelope Cbar of P R_maj(P)
from the sup over P of P^3 B beside each sign change.
QUESTION E. With the majorants read at P0 = P (valid at P, smaller than
at P0 = 399), is there a P-free Cbar with P R_maj(P) <= Cbar at every P
>= 399 and Cbar < 99.71, so that R_maj + F + 2.1e-4 stays below 1/4 +
1/(4P) - 1/P^2 at every P? Notation as uniform.py; T(P) = 1 + ln(P/399)
/(2 pi^2), c = 400.
BY HAND.
(E1) majorant.py's bounds hold for every P >= P0, so at P0 = P they
hold at P. With P0 = P: an image's f(U) = P(e^(U/P) - 1), U
falling in P, falls; f(V) = |V| falls; A_k's P0-scaled terms fall;
sigma_k's self-map holding at P0 holds at every P >= P0. So in P^3
units each image's min(G A(P), P^3 crude(P)) has a falling first
arm and a rising second (P^3 |phi_k| = P G), and its sup over P >=
399 is finite: past the crossing P* ~ l/ln 3 it is about 2 P* G ~
|xi|^3 e^(-xi^2/(4 tau))/tau^2. The modes' P^3 env A_k/P = 4 pi k
e^(-4 pi^2 k^2 tau) A_k(P) fall in P, their sup at 399.
(E2) With bbar(x0, tau) the sup over P of P^3 B at the classes beside
x0 and Gamma = |K_xx(x0, tau)|, uniform.py's limit form gives
Cbar(c) = int_(1/c)^4 sum_(x0) Sbar dtau,
Sbar = 2 bbar (2 floor(bbar/Gamma) + 1) at x0 = 0, and at x0 = 1/2
the larger of that (lattice Z, even P) and Gamma + 2 bbar (1 + nh +
(nh - 1)^+), nh = floor(bbar/Gamma + 1/2) (Z + 1/2, odd P, the
wrong-signed class's Gamma). TRANSPLANT from the P -> inf limit
(uniform.py's X2): the count linear in rho at b(x0); at finite P it
reads |K_x(x0 + rho/P)| and B per class (owed: a lower bound kappa
on |K_x| over a window and the sup of bbar over it), and the
midpoint rule in q owes a cell bound. The sup over P is read here
on a grid of P, both parities, geometric to 1e12: an observation.
(E3) c = 400 holds F <= 0.01 to P = 1.8e10; past it c(P) ~ 16 ln(1600
P/c), 2000 near P = 1e54, so Cbar(2000) - Cbar(400) prices the cut's
move. The cut past T(P) P^3 costs about 3e-13 (uniform.py).
PREDICTIONS, CONTROL AND KILL (frozen before code).
E0 CONTROL. (a) R_quad at P = 399 with P0 = P reads uniform.py's P R
25.3408, 1e-4 relative. (b) At d = 800 with P0 = 799 the majorants
dominate 30-digit exact errors (images and modes, largest ratio <=
1 + 1e-12) on 2000 and 2000 sampled m; PLANT (majorant.py's W0e): l's
sign flipped and beta1 = 0, a ratio passes 1 on each kind.
E1 P R_maj(P; P0 = P), c = 400, M = T(P) P^3, at P = 400, 399, 800,
799, 3200, 3199, 1e4, 1e4 + 1, 1e6, 1e6 + 1, 1e10, 1e10 + 1: at odd P
at or below uniform.py's P0 = 399 reads (22.06 at 799, 20.82 at
3199, 20.72 at 1e4 + 1), falling toward the first orders' 17.94; at
even P below the odd neighbour by 0.5 to 1.5, toward 16.82.
E2 Cbar(400) in [25.3, 40], Cbar(2000) - Cbar(400) < 0.1.
KILL. (K1) a P read with R_maj + F(400, P) + 2.1e-4 at or above 1/4 +
1/(4P) - 1/P^2. (K2) Cbar(400) >= 99.71: no one P-free envelope from
399, a split owed. (K3) a P read with P R_maj above Cbar(400): the
envelope's form is not an envelope.
AMENDED AFTER A REHEARSAL (n_tau 600, 16 P), BEFORE THE RUN. (i) An
instrument fault: uniform.py's kimg grows K by ln(P/P0), which at
P0 = P is 0, so the walk's tail past K, about P^3 e^-64, grew like
P^3 (5e26 at tau < 1/400 in the rehearsal); K's growth is now read
against 399. (ii) The bar of K2 is 399 (0.250620 - 2.1e-4 - 5.6e-8)
= 99.91, 99.71 a slip by hand. (iii) The rehearsal read Cbar(400)
21.69 below P R_maj(399) 25.38, the TRANSPLANT's count at b(x0)
alone; beside the frozen form, printed and not predicted, the
windowed form Chat = int sum_o max_P [Bad] (|phi| - s phi + 2B) P^3
over offsets o from each sign change (lattice Z, and Z + 1/2 at
x0 = 1/2, odd P), each class given to its nearer sign change.
(iv) A second rehearsal read Cbar on [1/2000, 1/400) at P = 399,
1.3e7: R reads tau < 1/c only once c(P) > 400, past P = 1.8e10, so
there the sup runs over the grid's P >= 1.8e10 alone. (v) The
control as first frozen read E0b on 600 and 300 samples with A and
A_k halved as its plant; after the first rehearsal (its 30 sampled
m for the modes missed beta1's effect near tau = 0.02)
it reads 2000 and 2000 with majorant.py's W0e plant, the halving
never run. E0a's "25.3408" is uniform.py's printed P R of the
frozen 0.06351, and E1's 1e4 is uniform.py's 1e4 + 1.
FINDINGS (copied from the run's prints).
- E0 holds: P R_maj(399) at P0 = P is 25.3408 (n_tau 12000); at d =
800, P0 = 799, the largest ratios are 0.999542 over 33044 image
terms and 0.988623 over 23942 mode terms; the plant reads 4.5e29 and
1.3713.
- E1 holds (n_tau 6000): P R_maj at P = 400, 399, 800, 799, 3200, 3199,
1e4, 1e4 + 1, 1e6, 1e6 + 1, 1e10, 1e10 + 1 is 24.1821, 25.3379,
18.4391, 19.5471, 17.1282, 18.2447, 16.9156, 18.0354, 16.8198,
17.9434, 16.8175, 17.9413: at P0 = P the majorants fall onto the first
orders' limits (17.9415 odd, 16.8206 even, uniform.py) by 1e6, odd
above even by 1.108 to 1.156. R_maj + F + 2.1e-4 is largest at 399,
0.063713; at 1e6 it is the 2.1e-4 and R (0.000227), at 1e10 F's
(0.005765). E0a reads the wiring alone (P0 = P is uniform.py's own
setting at 399); E0b reads the P0 = P path.
- The KILL fires on K3 and the frozen form: Cbar(400) = 21.7111, below
P R_maj at 399 and 400 (E2's band missed with it); Cbar(2000) adds
4e-7. The sup over the grid's P sits at 399 or 400 at every tau >=
1/400 (images and modes), as (E1) said, so the miss is the count at
b(x0) alone: the limit form is not an envelope at finite P. K2 does
not fire (21.71 < 99.91).
- The windowed form, printed: Chat(400) = 26.3828 (12.4804 below 1/64,
13.9024 past), above every P R_maj read (largest 25.3379 at 399), no
Bad class in the window's outer 10 past P = 800 at the 40 P read:
the envelope's live candidate, a quarter of 99.91, with the sup over
P, the per-class count and the q sum still read, not bounded.
RUN RECORD: 6/8 checks, the misses E2 and K3, as above; 418.0 s under
the 512 MB guard, peak commit 207.1 MB; with uniform.py's majorants
taking P0 as an argument (no module state set), the same prints, 417.8
s, 208.0 MB, exiting 0 iff exactly MISSED fail. Two rehearsals (n_tau 600) came
first and fixed the faults amended above. The P printed as 1e+06 and
1e+10 are 1e6, 1e6 + 1 and 1e10, 1e10 + 1, in that order. The run
warned of an overflow in majorant.py's image_A (P0 expm1(700), at a
P0 of E2's grid; E1's P did not raise it), clamped by img_B at 1e300 before use.
"""
import math
import os
import sys
import time
os.environ.setdefault("OPENBLAS_NUM_THREADS", "1")
import numpy as np # noqa: E402
import majorant as MJ # noqa: E402
import uniform as UN # noqa: E402
from heat import kernel # noqa: E402
from remainder import F_edge # noqa: E402
CHECKS, NAMES = [], []
MISSED = ("E2 Cbar(400) in [25.3, 40], Cbar(2000) - Cbar(400) < 0.1",
"KILL K3 not fired: every P R_maj read at or below Cbar(400)")
C, K4 = 400, 22
PR_399 = 25.3408 # uniform.py's P R_maj at 399
CBAR_MAX = 399 * (0.250620 - 2.1e-4 - 5.6e-8) # 99.91
W_OFF = 200
REHEARSE = "--rehearse" in sys.argv
def check(name, ok, detail=""):
CHECKS.append(bool(ok))
NAMES.append(name)
print(f" [{'PASS' if ok else 'FAIL'}] {name}" +
(f" -- {detail}" if detail else ""), flush=True)
def at_P(P0):
"""The 22 modes' constants read at P0."""
cst = []
for k in range(1, K4 + 1):
th = 2 * math.pi * k / P0
sg, mb, ec, rho = MJ.mode_const(th, P0)
cst.append((k, th, sg, mb, P0 * ec, rho * P0 * P0))
return cst
def T_of(P):
return 1 + math.log(P / 399) / (2 * math.pi ** 2)
def budget(P):
return 0.25 + 1 / (4 * P) - 1 / P ** 2
def R_at(P, n_tau):
cst = at_P(P)
T = T_of(P)
b, bo = UN.R_quad(P, C, T, n_tau, cst, P0=P)
return b.sum() + UN.past_T(P, T), bo
def section_control():
print("E0 CONTROL", flush=True)
t0 = time.time()
R, bo = R_at(399, 600 if REHEARSE else 12000)
check("E0a R_quad at P = 399, P0 = P, reads uniform.py's P R 25.3408",
abs(399 * R / PR_399 - 1) < 1e-4,
f"P R {399 * R:.4f} ({time.time() - t0:.0f} s)")
cst = at_P(799)
mi, mo = MJ.samples(799, 2000, 2000, 13)
t0 = time.time()
ri, _ = MJ.image_ratios(800, 799, mi)
rm, _ = MJ.mode_ratios(800, 799, cst, mo)
check("E0b majorants at P0 = P = 799 dominate the exact errors at d = 800",
ri.max() <= 1 + 1e-12 and rm.max() <= 1 + 1e-12,
f"largest ratios {ri.max():.6f} ({len(ri)} images), "
f"{rm.max():.6f} ({len(rm)} modes) ({time.time() - t0:.0f} s)")
good_A, good_Ak = MJ.image_A, MJ.mode_A
def bad_A(a, tau, s, P0, arm="uniform"):
return good_A(a, tau, -s, P0, arm)
def bad_Ak(k, th0, Ec, zc, tau, P0):
b2 = (2 * math.pi * k) ** 3 * tau
z0 = zc * tau
return (np.sqrt(b2 ** 2) + Ec * np.exp(z0)
+ math.sqrt(1 + 2.25 * th0 * th0) * P0 * np.expm1(z0))
MJ.image_A, MJ.mode_A = bad_A, bad_Ak
try:
pi, _ = MJ.image_ratios(800, 799, mi)
pm, _ = MJ.mode_ratios(800, 799, cst, mo)
finally:
MJ.image_A, MJ.mode_A = good_A, good_Ak
check("E0b PLANT: l's sign flipped and beta1 = 0, a ratio passes 1 on each",
pi.max() > 1 + 1e-12 and pm.max() > 1 + 1e-12,
f"largest ratios {pi.max():.4g}, {pm.max():.4f}")
def section_read():
print("E1 P R_maj(P) at P0 = P, c = 400, M = T(P) P^3", flush=True)
Ps = [400, 399] if REHEARSE else [400, 399, 800, 799, 3200, 3199, 10000,
10001, 10 ** 6, 10 ** 6 + 1, 10 ** 10,
10 ** 10 + 1]
rows = {}
for P in Ps:
t0 = time.time()
R, bo = R_at(P, 600 if REHEARSE else 6000)
F = F_edge(C, P)[0]
rows[P] = (P * R, R + F + 2.1e-4, budget(P))
print(f" P = {P:.6g}: P R_maj {P * R:.4f}, R + F + 2.1e-4 "
f"{R + F + 2.1e-4:.6f} against {budget(P):.6f}; outer Bad {bo},"
f" widest {UN.R_quad.widest} ({time.time() - t0:.0f} s)",
flush=True)
odd = {P: v[0] for P, v in rows.items() if P % 2}
uni = {799: 22.0587, 3199: 20.8247, 10001: 20.7245}
below = all(odd[P] <= uni[P] + 1e-4 for P in uni if P in odd)
dif = [odd[P] - rows[P + (1 if P + 1 in rows else -1)][0] for P in odd]
print(f" odd minus even neighbour: {' '.join(f'{v:+.3f}' for v in dif)}",
flush=True)
check("E1 odd P at or below uniform.py's P0 = 399 reads; even below the "
"odd neighbour by 0.5 to 1.5", below and all(0.5 <= v <= 1.5
for v in dif))
over = [P for P, v in rows.items() if v[1] >= v[2]]
check("KILL K1 not fired: R_maj + F + 2.1e-4 below the budget at every P "
"read", not over, f"at or above at {over}")
return rows
def bbar(ts, Ps):
"""max over the P grid of P^3 B at the classes beside 0 and 1/2."""
out = {0.0: np.zeros(len(ts)), 0.5: np.zeros(len(ts))}
arg = {0.0: np.zeros(len(ts)), 0.5: np.zeros(len(ts))}
lo, hi = ts < 1 / 4, ts >= 1 / 64
for P in Ps:
cst = at_P(P)
q = np.floor(ts * P * P)
Bf = np.full(len(ts), np.inf)
Bf[hi] = UN.mode_B(ts[hi], P, cst, P0=P)
live = (ts >= 1 / C) | (P >= 1.8e10)
for x0, rhos in ((0.0, [0.0]),
(0.5, [P / 2] if P % 2 == 0 else
[(P - 1) / 2, (P + 1) / 2])):
for rho in rhos:
B = Bf.copy()
if lo.any():
r = np.full(int(lo.sum()), rho)
tau_hi = float(((q[lo] + rho / P) / P ** 2).max())
B[lo] = np.minimum(B[lo], UN.img_B(q[lo], r, P, tau_hi, P))
v = np.where(live, P ** 3 * B, 0.0)
upd = v > out[x0]
out[x0] = np.where(upd, v, out[x0])
arg[x0] = np.where(upd, P, arg[x0])
return out, arg
def Cbar(ts, ws, bb):
S = np.zeros(len(ts))
for x0 in (0.0, 0.5):
G = np.abs(kernel(x0, ts)[0])
b = bb[x0]
r = b / G
even = 2 * b * (2 * np.floor(r) + 1)
if x0 == 0.0:
S += even
else:
nh = np.floor(r + 0.5)
S += np.maximum(even, G + 2 * b * (1 + nh + np.maximum(nh - 1, 0)))
return S * ws
def chat(ts, ws, Ps):
"""The windowed form: per offset o from 0 (lattice Z) and from 1/2
(Z at even P, Z + 1/2 at odd), the max over the P grid of P^3 (|phi|
- s phi + 2B) on Bad classes, |o| < P/4 (the nearer sign change);
integrated, with the Bad count at |o| >= W_OFF - 10 past P = 800."""
env, edge = envs(ts, Ps)
return combine(env, ws), edge
def combine(env, ws):
"""The offsets' sum per node, the two lattices at 1/2 by parity."""
return (env["0"].sum(axis=1) + np.maximum(env["hZ"].sum(axis=1),
env["hZ2"].sum(axis=1))) * ws
def envs(ts, Ps, p0=lambda P: P, half=True, w=W_OFF):
"""chat's per-offset max over Ps, the majorants read at P0 = p0(P), the
offsets within w; half=False reads odd P's lattice at 1/2 on integer
offsets (a plant)."""
o_int = np.arange(-w, w + 1, dtype=float)
o_half = np.arange(-w, w, dtype=float) + 0.5
env = {"0": np.zeros((len(ts), len(o_int))),
"hZ": np.zeros((len(ts), len(o_int))),
"hZ2": np.zeros((len(ts), len(o_half)))}
edge = 0
lo, hi = ts < 1 / 4, ts >= 1 / 64
for P in Ps:
P0 = p0(P)
cst = at_P(P0)
q = np.floor(ts * P * P)
Bf = np.full(len(ts), np.inf)
Bf[hi] = UN.mode_B(ts[hi], P, cst, P0=P0)
oh = o_half if half else o_int[:-1]
lats = [("0", 0.0, o_int), ("hZ2" if P % 2 else "hZ", P / 2,
oh if P % 2 else o_int)]
for key, base, offs in lats:
keep = np.abs(offs) < P / 4 if key == "0" else np.abs(offs) <= P / 4
rho = base + offs[keep]
Q = np.broadcast_to(q[:, None], (len(ts), len(rho)))
R2 = np.broadcast_to(rho[None, :], Q.shape)
B = np.broadcast_to(Bf[:, None], Q.shape).copy()
if lo.any():
tau_hi = float(((q[lo] + rho.max() / P) / P ** 2).max())
B[lo] = np.minimum(B[lo], UN.img_B(Q[lo], R2[lo], P, tau_hi,
P0))
x = np.mod(R2, P) / P
tau = (Q + R2 / P) / P ** 2
ph = -UN.kx(x.ravel(), tau.ravel()).reshape(Q.shape) / P ** 2
s = UN.sign_of(np.mod(R2, P), P)
bad = s * ph <= B
v = np.where(bad, (np.abs(ph) - s * ph + 2 * B) * P ** 3, 0.0)
sub = env[key][:, keep]
env[key][:, keep] = np.maximum(sub, v)
if P > 800:
edge += int(bad[:, np.abs(offs[keep]) >= w - 10].sum())
return env, edge
def section_envelope(rows):
print("E2 the envelope Cbar from the sup over P of P^3 B", flush=True)
g = np.unique(np.geomspace(399, 1e12, 8 if REHEARSE else 40).round())
Ps = sorted({int(p) for p in g} | {int(p) + 1 for p in g})
t0 = time.time()
ts, ws = UN.nodes(2000, 4.0, 400 if REHEARSE else 4000)
bb, arg = bbar(ts, Ps)
part = Cbar(ts, ws, bb)
c400 = float(part[ts >= 1 / 400].sum())
c2000 = float(part.sum())
for lo_, hi_ in ((1 / 2000, 1 / 400), (1 / 400, 1 / 64), (1 / 64, 1 / 16),
(1 / 16, 1 / 4), (1 / 4, 1), (1, 4)):
sel = (ts >= lo_) & (ts < hi_)
a0 = np.unique(arg[0.0][sel])
ah = np.unique(arg[0.5][sel])
print(f" tau in [{lo_:.5f}, {hi_:.4f}): Cbar part "
f"{part[sel].sum():.4f}; sup's P at 0: {a0.min():.4g}.."
f"{a0.max():.4g}, at 1/2: {ah.min():.4g}..{ah.max():.4g}",
flush=True)
print(f" Cbar(400) {c400:.4f}, Cbar(2000) {c2000:.4f}, against "
f"{CBAR_MAX:.2f} ({len(Ps)} P, {time.time() - t0:.0f} s)",
flush=True)
t1 = time.time()
g2 = np.unique(np.geomspace(399, 1e12, 4 if REHEARSE else 20).round())
P2 = sorted({int(p) for p in g2} | {int(p) + 1 for p in g2})
t2, w2 = UN.nodes(C, 4.0, 300 if REHEARSE else 1500)
ch, edge = chat(t2, w2, P2)
print(f" Chat(400) {ch.sum():.4f}, by band [1/400, 1/64) "
f"{ch[t2 < 1 / 64].sum():.4f}, past 1/64 {ch[t2 >= 1 / 64].sum():.4f}"
f" (printed, not predicted; {len(P2)} P, Bad in the window's outer "
f"10 past P = 800: {edge}; {time.time() - t1:.0f} s)", flush=True)
check("E2 Cbar(400) in [25.3, 40], Cbar(2000) - Cbar(400) < 0.1",
25.3 <= c400 <= 40 and c2000 - c400 < 0.1,
f"{c400:.4f}, {c2000 - c400:.2e}")
check("KILL K2 not fired: Cbar(400) below 99.91", c400 < CBAR_MAX,
f"{c400:.4f}")
above = [P for P, v in rows.items() if v[0] > c400]
check("KILL K3 not fired: every P R_maj read at or below Cbar(400)",
not above, f"above at {above}")
def main():
t0 = time.time()
section_control()
if not all(CHECKS) and not REHEARSE:
print("control failed; stopping")
sys.exit(1)
rows = section_read()
section_envelope(rows)
print(f"{sum(CHECKS)}/{len(CHECKS)} checks passed, "
f"{time.time() - t0:.0f} s")
sys.exit(as_recorded(MISSED))
def as_recorded(missed):
"""0 iff the checks that failed are exactly the record's misses, by name."""
got = {n for n, ok in zip(NAMES, CHECKS) if not ok}
print("misses %s; the record's %s: %s" % (
"; ".join(sorted(got)) or "none", "; ".join(sorted(missed)) or "none",
"as recorded" if got == set(missed) else "NOT AS RECORDED"))
return 0 if got == set(missed) else 1
if __name__ == "__main__":
main()