losses.py
Checks: FLATTEN: The takeover depth grows as r^(3/2) theorem; property; rule; pattern; observation.
Cited on: FLATTEN.
To run it: save losses.py, with band.py, bandend.py, bandfamily.py, bandheight.py, bandscale.py, circlemean.py, flatten.py, prunerr.py beside it; needs mpmath, numpy, scipy.
"""losses.py -- the lower bound reads height(F_e) < H_ch through two
losses; which one carries the gap between 2 sqrt(pi) and the depth where
some F_e last beats the champion?
QUESTION. After x -> -x, at rank r, l = r - 2, depth J, the champion is
P = (1 + x)^J (1 - x)^(l+1), of height H_ch, and F_e = (1 + x)^J (1 - x)^
(l-1) (x + e (1 - x)^2). The proof at J_e = (4e - 1) l - 1 reads (A)
height(F_e) <= mean(F_e), the mean modulus on the unit circle, and (B)
H_ch >= L, L = |<P, w>|/|w|_1 (at odd r the central coefficient). With
A = height(F_e)/mean(F_e) <= 1 and B = H_ch/L >= 1, height(F_e)/H_ch =
R A/B, R = mean(F_e)/L. Which factor moves the depth?
BY HAND.
(H1) Q = (1 + x)^(J+1) (1 - x)^l, n = (J + l + 1)/2. Then x Q' - n Q =
-(1 + x)^J (1 - x)^(l-1) [n (1 - x)^2 + 2l x], so at n = 2el,
i.e. J = J_e, F_e's coefficient k is +-(k - n) q_k/(2l): height(F_e)
= max_k |k - n| |q_k|/(2l), at every l and e.
(H2) At odd r J_e is even and P(e^(it)) is a phase times a real factor
(2 cos(t/2))^J (2 sin(t/2))^(l+1) >= 0, so H_ch = L = mean(P): B = 1.
(H3) In the Gaussian limit of f = sin^l(t/2) cos^(J+1)(t/2) about its
peak, (k - n) q_k is the transform of f', whose sup over k is
sqrt(2 pi) e^(-1/2) f_max against the mean's 2 f_max: A -> K =
sqrt(pi/2) e^(-1/2) = 0.76017. TRANSPLANT, corrections ~ 1/sqrt(l).
(H4) R ~ 2e/sqrt(pi l); a factor c on R moves the crossing to e ~
sqrt(pi l)/(2c) and the depth to (2 sqrt(pi)/c) l^(3/2). At c = K:
2 sqrt(2 exp 1) = 4.66329, bandheight.py's hand J_hi* constant.
(H5) Even r: (H1) holds, A -> K; P's real factor is odd and H_ch/mean(P)
-> 1; the test x^k (1 + x)^p (1 - x) pays K again: 2 sqrt(pi) K =
2.69474, the even test's limit (binet.py).
PREDICTIONS (frozen before the engine; P5, P6 as run, see CHANGED).
P1 (H1) in integers at every (r, e), r = 5..61, e = 1..2 isqrt(l) + 2.
P2 B = 1 at J_e, every odd r = 21..201, every e read.
P3 A at E (R's reach), odd r = 101..201, in [0.70, 0.80].
P4 e_T, the largest e with height(F_e) < H_ch at J_e read from E up,
has J_(e_T)/l^(3/2) in [4.0, 4.8] at odd r = 101..201.
P5 at the last beat J_F, walked down from ceil(5 r^(3/2)), odd r = 21,
31, 41, 51, 101, 151, 201: A in [0.70, 0.85] and mean(P)/H_ch in
[1, 1.05].
P6 even r = 22..200: A at e_T in [0.70, 0.85] at r >= 100; H_ch/mean(P)
in [0.9, 1].
KILLS. K1 "identity failures" above 0 over P1's family. K2 J_(e_T)/
l^(3/2) above 5.0 at some odd r = 101..201, or below 4.0 at more than
half of them. K3 B > 1 at some J_e, odd r.
CONTROLS. circlemean.py's record: R's reach E at every odd r = 5..201;
at r = 5..41 the identity's height equals exact_height_F at the
proof's e and L = H_ch at odd r. bandfamily.py's far row at r = 30,
(692, 6, 6, 88), reproduced by this script's walk. A's mean at J_e,
by Beta functions in mpmath, against circlemean's closed form.
FINDINGS (copied from the runs' prints; (5) is the walk's).
K1, K3 and K2 miss; P1..P6 hold.
(1) The identity holds at all 670 pairs, r = 5..61. B > 1 at 0 of the
448 (r, e) read at odd r = 21..201: (B) costs nothing at odd r.
(2) A(E) over odd r = 101..201 is 0.7502..0.7603, against K = 0.76017.
e_T/E there is 1.2222..1.4444, against 1/K = 1.3155.
(3) J(e_T)/l^1.5 over odd r = 101..201 is 4.2777..4.6567, against
2 sqrt(2 exp 1) = 4.66329; at r = 21, 31, 41, 51 it is 4.3468,
4.2646, 4.3194, 4.4257. The theorem's J(E)/l^1.5 is 2.779..3.4863
at the ranks shown.
(4) Even r = 100..200: A at e_T is 0.7493..0.7597, H_ch/mean(P)
0.9886..0.9967. J(e_T)/l^1.5 is 4.2373, 4.4715, 4.3426, 4.5204,
4.4769 at r = 22, 50, 100, 150, 200. F_e at J_e reaches the odd
ranks' constant at even r: the parity gap is the test's.
(5) The walk reproduces bandfamily's (692, 6, 6, 88) at r = 30. The last
beat J_F at odd r = 21, 31, 41, 51, 101, 151, 201 is 402, 714, 1128,
1590, 4454, 8386, 12852 (4.5217..4.8540 l^1.5), one e each (5, 6,
7, 8, 11, 14, 16). Every J_F is even, and mean(P)/H_ch = 1.0000
there; A is 0.7263..0.7694.
So (A) carries the whole gap at odd r, and the gap is the constant K
of (H3). Through (H4) the theorem's 2 sqrt(pi) over K is the hand
J_hi* constant 2 sqrt(2 exp 1).
THE SHIFTED CIRCLE (frozen after the runs above, before its code). Cauchy's estimate on |z| = e^(-y) with the L1 mean, through
(H1): |t q_(n-t)| <= 2^(2n) |t| e^(-|t| y) I(y)/pi, I(y) = int_0^pi
(sin^2(t/2) + S)^(l/2) (cos^2(t/2) + S)^((J+1)/2) dt, S = sinh^2(y/2),
and sup_t t e^(-ty) = 1/(exp(1) y): height(F_e)/H_ch <= ratio_C =
inf_y I(y)/(exp(1) y)/(2l B((l + 2)/2, (J + 1)/2)) at odd r. In the
Gaussian limit the inf sits at y = 1/sqrt(n) and equals the sup, so
the bound is sharp to leading order (TRANSPLANT, (H3)'s bump). A
float probe (trapezoid, golden
section); its I(0) is read against the Beta value. Frozen: P7 e_C (the
largest e with ratio_C < 1, read from E up) >= e_T - 1 at every odd
r = 101..201 and >= E + 1 at every odd r = 21..201; P8 ratio_C over
the exact height(F_e)/H_ch in [1, 1.15] at r >= 101, below 1 an
engine fault; K4 e_C <= E at more than a quarter of odd r = 101..201.
(6) K4 misses, P7 and P8 hold. ratio_C over the exact
height(F_e)/H_ch is 1.0008..1.0755 over every pair read, 1.0008..
1.0200 at r >= 101. e_C = e_T at r = 21, 31, 41, 51, 101, 151, 201;
e_C >= e_T - 1 at all 51 odd r = 101..201, e_C >= E + 1 at all 91.
J(e_C)/l^1.5 over odd r = 101..201 is 4.2360..4.6208; at the seven
printed pairs (r, e_C + 1) the inf's y is 0.986..0.999 of
1/sqrt(n).
(7) With N(e^(-y)) = (2 cosh y)^n M(sech y), M(b) = (1/pi) int_0^pi
(1 + b cos t)^((J+1)/2) (1 - b cos t)^(l/2) dt, l (M(sech y)/M(1)
- 1) at y = 1/sqrt(n), over every (r, e) of the odd stage at r =
21, 51, 101, 151, 201: 0.2555..0.2864 over 24 pairs. Above 1, by
O(1/l): a pattern at these five ranks. N(rho) is the mean of
|z^(-n) Q(z)| on |z| = rho, 2^(2n) I(y)/pi at rho = e^(-y).
THE BOUND (derived and frozen before its code). At y = n^(-1/2)
the shifted circle gives a closed bound, every parity, l >= 18:
(H6) With u = sin^2(t/2), v = (u + S)/cosh y: u(1 - u) = cosh^2 y (W -
tau), W = v(1 - v), tau = tanh^2(y)/4, so I(y) = cosh^n(y) I(0) Phi,
I(0) = B(a, b), a = (l + 1)/2, b = (J + 2)/2, and exactly Phi = E[(1
- tau/W)^(-1/2); W > tau], V ~ Beta(a, b), W = V(1 - V) ((7)'s M
ratio).
(H7) On W >= 2 tau, (1 - x)^(-1/2) <= 1 + x/2 + c2 x^2, c2 = (3/8)
2^(5/2); 1/W = 1/V + 1/(1 - V) and the Beta moments (a + b - 1 = n)
give, with tau <= 1/(4n): Phi <= 1 + delta, delta = 1/(4(l - 1)) +
1/(4J) + (c2/4)[1/((l - 1)(l - 3)) + 1/(n(l - 1)) + 1/(nJ) + 1/(J(J
- 2))] + exp(4.5) sqrt2 2^(-(l-1)/2) + exp(1.1) sqrt2 2^(-J/2). The
two tails are W < 2 tau, where v or 1 - v < 4 tau: there int (W/(W -
tau))^(1/2) dv <= 8 sqrt2 tau, and the density, monotone there, is
at most 2^(-(a-1)) exp(4.5)/(8 tau) (resp. 2^(-(b-1)) exp(1.1)/(8
tau)) by comparing it with [8 tau, 16 tau] (n >= 2l >= 36; the
exp(1.1) as (a - 1) 16 tau/(1 - 16 tau) <= 2(l - 1)/(n - 4) <= (l -
1)/(l - 2)).
(H8) Wendel: B(a, b)/B((l + 2)/2, (J + 1)/2) <= sqrt((J + 1)(l + 2))/(l
+ 1); cosh^n(y) <= exp(1/2). So height(F_e)/mean(P) <= G =
exp(-1/2) (1 + delta) sqrt(2e (4e - 1)(l + 2))/(2(l + 1)). Every
term of delta falls in J and n, so delta <= delta_u(l), its value
at e = 1 (J = 3l - 1, n = 2l), and there every term of l delta_u(l)
falls in l (l > 3).
(H9) Even r: z^(-n) P is a fixed phase times g(th) = (2 cos(th/2))^J (2
sin(th/2))^(l+1), odd on (-pi, pi), so H_ch >= |p_(n-t)| =
mean(P) E[sin(t Th)], Th ~ g on (0, pi), U = sin^2(Th/2) ~ Beta((l
+ 2)/2, (J + 1)/2). sin x >= 1 - (x - pi/2)^2/2 and arcsin
sqrt2-Lipschitz on U <= 1/2 give H_ch/mean(P) >= 1 - eta, eta = 4t^2
(Var U + (EU - u*)^2)/u* + (pi^2 t^2/2) 2^k E[U^k], u* = sin^2(pi/
(4t)), k = floor((l + 2)/2). At t nearest T = pi/(4 arcsin sqrt(EU))
(u* = EU at T), as l and e grow: t^2 u* -> pi^2/16 and Var U <=
EU/(a2 + b2), a2 = (l + 2)/2, b2 = (J + 1)/2, so 4t^2 Var/u* <=
pi^2/(2l)(1 + o(1)); |EU - u*| <= max |du*/dt|/2, so 4t^2 (EU -
u*)^2/u* <= pi^2/(4t^2)(1 + o(1)), t ~ (pi/2) sqrt e; 2^k E[U^k] <= (2(l + 2)/
(n + 1))^k (each factor of E[U^k] is below (l + 2)/(n + 1)), base
~ 1/e, e the depth integer, k ~ l/2, against t^2 ~ e. So eta -> 0.
Frozen: Q1 G >= ratio_C at every pair the shift stage reads, G/ratio_C
in [1, 1.02] at r >= 101; Q2 Phi (an mpmath quadrature in v) <= 1 +
delta at every pair of the odd stage, l delta in [0.25, 0.40] at r >=
101; Q3 e_G (the largest e with G < 1) >= e_C - 1 and > E at every odd
r = 101..201; Q4 l delta_u(l) <= 0.40 and falling over odd l =
99..10001; Q5 at every even r = 22..200 at e_T, 1 - eta (t minimizing
eta) <= H_ch/mean(P) and G/(1 - eta) >= height(F_e)/H_ch, eta < 0.15
at r >= 150. KA G below ratio_C, KB Phi above 1 + delta, KC either
half of Q5 failing: at any pair, a step of the derivation is wrong.
Plants: G without (H8)'s exp(1/2), delta without 1/(4(l - 1)), eta with
Var halved and 4t^2 cut to t^2/2, each must fire its kill.
(8) KA, KB, KC miss; Q1..Q5 hold; the three plants fire (436 of
436, 431 of 448, 90 of 90 pairs). KA's and KC's plants are caught
by size alone (G sits 0.2% above ratio_C); KB and its plant read
nothing at r <= 29, where delta's tail term passes Phi - 1. G/ratio_C is 1.0013..1.2636 over the 436 pairs,
1.0013..1.0027 at r >= 101; l delta there 0.2578..0.2666. e_G =
e_C = e_T at r = 51, 101, 151, 201 (8, 11, 14, 16); J(e_G)/l^1.5 over
odd r = 101..201 is 4.2360..4.6066. l delta_u(l) is 0.3460, 0.3346,
0.3335 at l = 99, 999, 10001. Even r: eta = 0.2184, 0.1205, 0.0497,
0.0457, 0.0341 at r = 22, 50, 100, 150, 200 (t = 3, 4, 5, 6, 6),
against H_ch/mean(P) = 0.9642, 0.9817, 0.9932, 0.9946, 0.9953.
So, by hand: at odd r with l >= 99 (l delta_u falls by (H8); 0.3460 at 99),
e(4e - 1) < X = 2 exp(1) l/(1 + 0.35/l)^2 gives G < 1 ((l + 1)^2 >= l(l
+ 2)); the largest such e exceeds (1 + sqrt(1 + 16X))/8 - 1, so 4e - 1
> 2 sqrt(X) - 4.5 and J_ch(r) > J_e > 2 sqrt(2 exp 1) l^(3/2)/(1 +
0.35/l) - 4.5 l - 1;
at even r, e = floor(kappa sqrt l), kappa < sqrt(exp(1)/2), has G/(1 -
eta) -> sqrt2 exp(-1/2) kappa < 1. liminf J_ch/r^(3/2) >= 2 sqrt(2 exp
1) = 4.66328 over both parities.
CHANGED AFTER THE FREEZE. P5 froze B = H_ch/L at J_F; the code reads
mean(P)/H_ch, equal to it at even J, and every J_F printed is even.
P6 froze A and H_ch/mean(P) "at e = E_even's next", the latter at
every even r = 22..200; even_row reads both at e_T, and the check
reads r >= 100 alone. The probe froze mpmath quadrature; it runs a numpy
trapezoid, its I(0) read against mpmath's Beta. (7)'s print was added
after its scratch read (1 + 0.2536/l .. 1 + 0.3051/l, r = 21..401).
Q2 froze the shift stage's pairs (e to e_C + 1); the code reads the
odd stage's 448 (e to e_T + 1). Added with the move to this directory:
the THEOREM line, l delta_u(99) < 0.35, the constant the proof spends
(0.345951); the shift and the bound became the bare run.
RUN RECORD. Bare 26/26, 35.4 s, peak 142.3 MB (the gate's run). The
walk adds two checks and about 63 s (13/13 alone with the odd and even
stages, 78.3 s, peak 169.9 MB); its r = 201 took 46.0 s.
RUN: python losses.py [--walk]
"""
import math
import os
import sys
import time
os.environ.setdefault("OPENBLAS_NUM_THREADS", "1")
import mpmath as mp # noqa: E402
import bandend # noqa: E402
import bandfamily # noqa: E402
import circlemean # noqa: E402
mp.mp.dps = 40
CHECKS = []
K = math.sqrt(math.pi / 2) * math.exp(-0.5)
ODD = range(21, 202, 2)
FAR_ODD = range(101, 202, 2)
EVEN = range(22, 201, 2)
WALK = (21, 31, 41, 51, 101, 151, 201)
def check(name, ok, detail=""):
CHECKS.append(bool(ok))
tail = f" ({detail})" if detail else ""
print(f" {'PASS' if ok else 'FAIL'} {name}{tail}", flush=True)
# ------------------------------------------------------------- the engine
def coeffs(A, B):
"""(1 + x)^A (1 - x)^B by (1 - x^2) f' = ((A - B) - (A + B) x) f:
(k + 1) f_(k+1) = (A - B) f_k - (A + B - k + 1) f_(k-1)."""
f = [1, A - B]
for k in range(1, A + B):
num = (A - B) * f[k] - (A + B - k + 1) * f[k - 1]
q, rem = divmod(num, k + 1)
assert rem == 0
f.append(q)
return f[:A + B + 1]
def f_direct(J, l, e):
"""F_e's coefficients from its factors."""
V, U = bandfamily.exact_cols(coeffs(J, l - 1))
return [v + e * u for v, u in zip(V, U)]
def f_identity(J, l):
"""(k - n) q_k at n = (J + l + 1)/2, q = (1 + x)^(J+1) (1 - x)^l."""
q = coeffs(J + 1, l)
n2 = J + l + 1
return [(2 * k - n2) * c // 2 if n2 % 2 == 0 else None
for k, c in enumerate(q)]
def height_at_je(l, e):
q = coeffs(circlemean.depth(l, e) + 1, l)
n = 2 * e * l
top = max(abs((k - n) * c) for k, c in enumerate(q))
assert top % (2 * l) == 0
return top // (2 * l)
def log_mean_f(J, l, e):
"""log of F_e's mean modulus: (2^(J+l-1)/pi) B(a, b) E|1 - 4eU|, U ~
Beta(a, b), a = l/2, b = (J + 1)/2."""
a, b = mp.mpf(l) / 2, mp.mpf(J + 1) / 2
u0 = mp.mpf(1) / (4 * e)
mu = a / (a + b)
i0 = mp.betainc(a, b, 0, u0, regularized=True)
i1 = mp.betainc(a + 1, b, 0, u0, regularized=True)
dev = (1 - 4 * e * mu) + 2 * (4 * e * mu * (1 - i1) - (1 - i0))
return ((J + l - 1) * mp.log(2) - mp.log(mp.pi) + mp.log(mp.beta(a, b))
+ mp.log(dev))
def log_mean_p(J, l):
"""log of P's mean modulus: 2^(J+l+1) B((l + 2)/2, (J + 1)/2)/pi."""
return ((J + l + 1) * mp.log(2) - mp.log(mp.pi)
+ mp.log(mp.beta(mp.mpf(l + 2) / 2, mp.mpf(J + 1) / 2)))
def lg(n):
return mp.log(mp.mpf(n))
def reach_e(l):
"""R's reach E (circlemean's)."""
return circlemean.reach_of(l, lambda l, e: circlemean.r_side(l, e) < 0)
def odd_row(r):
"""At odd r: E, e_T, and for each e = E..e_T + 1 read: (e, height,
H_ch, L, A, R)."""
l = r - 2
E = reach_e(l)
rows = []
e = E
while True:
J = circlemean.depth(l, e)
h = height_at_je(l, e)
P = coeffs(J, l + 1)
H = max(abs(c) for c in P)
L = abs(P[(J + l + 1) // 2])
lm = log_mean_f(J, l, e)
A = float(mp.exp(lg(h) - lm))
R = float(mp.exp(lm - lg(L)))
rows.append((e, h, H, L, A, R))
if h >= H:
break
e += 1
eT = rows[-2][0] if len(rows) > 1 else None
return E, eT, rows
def even_row(r):
"""At even r, from the proof's e up to the first e that fails at J_e:
(e_T, A at e_T, H_ch/mean(P) there)."""
l = r - 2
e = max(1, circlemean.proof_e(l))
last = None
while True:
J = circlemean.depth(l, e)
h = height_at_je(l, e)
P = coeffs(J, l + 1)
H = max(abs(c) for c in P)
if h >= H:
return last
A = float(mp.exp(lg(h) - log_mean_f(J, l, e)))
hp = float(mp.exp(lg(H) - log_mean_p(J, l)))
last = (e, J, A, hp, h / H)
e += 1
def walk_last(r, top):
"""(J_F, (first, last) e, depths read above J_F): the first depth from
top down where some F_e is below H_ch."""
Qc = coeffs(top, r - 3)
read = 0
for J in range(top, 0, -1):
V, U = bandfamily.exact_cols(Qc)
H = max(abs(u) for u in U)
got = bandfamily.beats(V, U, H)
if got:
return J, got, read, V, U, H
read += 1
Qc = bandfamily.down(Qc)
return None
# ------------------------------------------------------------- the stages
def controls():
print("CONTROLS", flush=True)
small = [bandend.Q(9, 4), bandend.Q(12, 7)]
check("coeffs equals bandend.Q at (9, 4) and (12, 7)",
small == [coeffs(9, 4), coeffs(12, 7)])
want = circlemean.RECORD["odd"]["odd r = 5..201: E, in rank order"]
got = tuple(reach_e(r - 2) for r in range(5, 202, 2))
check("POSITIVE: R's reach E equals circlemean's record at every odd "
"r = 5..201", got == want)
same = lh = 0
for r in circlemean.INTS:
l = r - 2
e = circlemean.proof_e(l)
J = circlemean.depth(l, e)
same += height_at_je(l, e) == circlemean.exact_height_F(r, J, e)
if r % 2:
P = coeffs(J, l + 1)
lh += max(abs(c) for c in P) == abs(P[(J + l + 1) // 2])
check("POSITIVE: the identity's height is exact_height_F at the "
"proof's e, r = 5..41", same == len(circlemean.INTS),
f"{same} of {len(circlemean.INTS)}")
check("POSITIVE: L = H_ch at every odd r = 5..41 (circlemean's 19)",
lh == 19, f"{lh}")
worst = mp.mpf(0)
for l, e in ((3, 1), (19, 3), (49, 4), (99, 7)):
J = circlemean.depth(l, e)
mn, md = circlemean.mean2(l, e)
exact = (lg(mn) - lg(md)) / 2 - mp.log(mp.pi)
worst = max(worst, abs(log_mean_f(J, l, e) - exact))
check("CONTROL: the Beta mean equals circlemean's closed form at J_e, "
"four (l, e), to 1e-25", worst < mp.mpf(10) ** -25,
mp.nstr(worst, 3))
def identity():
print("P1: THE DERIVATIVE IDENTITY", flush=True)
bad = n = 0
for r in range(5, 62):
l = r - 2
for e in range(1, 2 * math.isqrt(l) + 3):
J = circlemean.depth(l, e)
d = f_direct(J, l, e)
g = f_identity(J, l)
n += 1
ok = (len(d) == len(g)
and all(x is not None and x % (2 * l) == 0 for x in g))
if ok:
s = [x // (2 * l) for x in g]
ok = d == s or d == [-x for x in s]
bad += not ok
print(f" identity failures: {bad} of {n} pairs (r = 5..61)", flush=True)
check("K1 misses: (H1) at every pair read", bad == 0)
def odd_stage():
print("ODD r: A, B, R AT J_e", flush=True)
rows = {r: odd_row(r) for r in ODD}
b_over = sum(1 for r in ODD for row in rows[r][2] if row[2] != row[3])
n_read = sum(len(rows[r][2]) for r in ODD)
print(f" B > 1 at {b_over} of {n_read} (r, e) read", flush=True)
check("K3 misses, P2 holds: B = 1 at every J_e read", b_over == 0)
print(" r E e_T A(E) A(e_T) R(e_T) J(e_T)/l^1.5 "
"J(E)/l^1.5", flush=True)
ratios, aE = {}, {}
for r in ODD:
E, eT, rr = rows[r]
l = r - 2
byE = {row[0]: row for row in rr}
aE[r] = byE[E][4]
ratios[r] = circlemean.depth(l, eT) / l ** 1.5
if r in (21, 31, 41, 51, 101, 151, 201) or r % 20 == 1:
print(f" {r:3d} {E:3d} {eT:3d} {aE[r]:.4f} "
f"{byE[eT][4]:.4f} {byE[eT][5]:.4f} {ratios[r]:.4f}"
f" {circlemean.depth(l, E) / l ** 1.5:.4f}",
flush=True)
far_a = [aE[r] for r in FAR_ODD]
print(f" A(E) over odd r = 101..201: {min(far_a):.4f}..{max(far_a):.4f}"
f"; K = {K:.5f}", flush=True)
check("P3: A(E) in [0.70, 0.80] at odd r = 101..201",
all(0.70 <= a <= 0.80 for a in far_a))
far_j = [ratios[r] for r in FAR_ODD]
above, below = sum(x > 5.0 for x in far_j), sum(x < 4.0 for x in far_j)
print(f" J(e_T)/l^1.5 over odd r = 101..201: {min(far_j):.4f}.."
f"{max(far_j):.4f}; above 5.0: {above}, below 4.0: {below} of "
f"{len(far_j)}; 2 sqrt(2 exp 1) = {2 * math.sqrt(2 * math.e):.5f}",
flush=True)
check("K2 misses: none above 5.0, at most half below 4.0",
above == 0 and 2 * below <= len(far_j))
check("P4: J(e_T)/l^1.5 in [4.0, 4.8] at every odd r = 101..201",
all(4.0 <= x <= 4.8 for x in far_j))
eTE = [rows[r][1] / rows[r][0] for r in FAR_ODD]
print(f" e_T/E over odd r = 101..201: {min(eTE):.4f}..{max(eTE):.4f}; "
f"1/K = {1 / K:.4f}", flush=True)
return rows
def even_stage():
print("EVEN r: A AND H_ch/mean(P) AT e_T", flush=True)
rows = {r: even_row(r) for r in EVEN}
for r in (22, 50, 100, 150, 200):
e, J, A, hp, _ = rows[r]
print(f" r = {r}: e_T = {e}, J/l^1.5 = {J / (r - 2) ** 1.5:.4f}, "
f"A = {A:.4f}, H_ch/mean(P) = {hp:.4f}", flush=True)
big = [rows[r] for r in EVEN if r >= 100]
print(f" r = 100..200: A {min(x[2] for x in big):.4f}.."
f"{max(x[2] for x in big):.4f}, H_ch/mean(P) "
f"{min(x[3] for x in big):.4f}..{max(x[3] for x in big):.4f}",
flush=True)
check("P6: A in [0.70, 0.85] and H_ch/mean(P) in [0.9, 1] at even r = "
"100..200", all(0.70 <= x[2] <= 0.85 and 0.9 <= x[3] <= 1
for x in big))
return rows
def walk_stage():
print("THE LAST BEAT", flush=True)
t = time.time()
J, got, read, _, _, _ = walk_last(30, int(1.1 * bandfamily.jstar(30)))
check("POSITIVE: the walk reproduces bandfamily's far row at r = 30, "
"(692, 6, 6, 88)", (J, got[0], got[1], read) == (692, 6, 6, 88),
f"{(J, got, read)}, {time.time() - t:.1f} s")
ok = []
for r in WALK:
t = time.time()
top = math.ceil(5 * r ** 1.5)
J, got, read, V, U, H = walk_last(r, top)
l = r - 2
e = got[0]
h = max(abs(v + e * u) for v, u in zip(V, U))
A = float(mp.exp(lg(h) - log_mean_f(J, l, e)))
B = float(mp.exp(log_mean_p(J, l) - lg(H)))
print(f" r = {r}: J_F = {J} ({J / l ** 1.5:.4f} l^1.5, {read} read "
f"below the top {top}), e = {got}, A = {A:.4f}, mean(P)/H_ch "
f"= {B:.4f}, {time.time() - t:.1f} s", flush=True)
ok.append(read > 0 and 0.70 <= A <= 0.85 and 1 <= B <= 1.05)
check("P5: A in [0.70, 0.85] and mean(P)/H_ch in [1, 1.05] at every "
"last beat, each below its top", all(ok))
def log_i(l, J, y, half=40, pts=8001):
"""log I(y), I(y) = int_0^pi (sin^2(t/2) + S)^(l/2) (cos^2(t/2) +
S)^((J+1)/2) dt, S = sinh^2(y/2): trapezoid on t0 +- half/sqrt(n),
the integrand scaled by its largest grid value."""
import numpy as np
n = (J + l + 1) / 2
t0 = 2 * math.atan(math.sqrt(l / (J + 1)))
w = half / math.sqrt(n)
t = np.linspace(max(0.0, t0 - w), min(math.pi, t0 + w), pts)
S = math.sinh(y / 2) ** 2
with np.errstate(divide="ignore"):
f = (l / 2) * np.log(np.sin(t / 2) ** 2 + S) + ((J + 1) / 2) * np.log(
np.cos(t / 2) ** 2 + S)
m = f.max()
return m + math.log(np.trapezoid(np.exp(f - m), t))
def ratio_c(l, e):
"""inf_y I(y)/(exp(1) y) / (2l B((l + 2)/2, (J + 1)/2)) at J_e, the
inf by golden section on log I(y) - log y (convex) over [sigma/20,
20 sigma], sigma = 1/sqrt(n)."""
J = circlemean.depth(l, e)
sig = 1 / math.sqrt(2 * e * l)
g = lambda y: log_i(l, J, y) - math.log(y)
a, b = math.log(sig / 20), math.log(20 * sig)
phi = (math.sqrt(5) - 1) / 2
c, d = b - phi * (b - a), a + phi * (b - a)
gc, gd = g(math.exp(c)), g(math.exp(d))
for _ in range(60):
if gc < gd:
b, d, gd = d, c, gc
c = b - phi * (b - a)
gc = g(math.exp(c))
else:
a, c, gc = c, d, gd
d = a + phi * (b - a)
gd = g(math.exp(d))
best = min(gc, gd)
lb = float(mp.log(mp.beta(mp.mpf(l + 2) / 2, mp.mpf(J + 1) / 2)))
return math.exp(best - 1 - math.log(2 * l) - lb), math.exp(c) / sig
def shift_stage(rows):
print("THE SHIFTED CIRCLE (a float probe)", flush=True)
worst = 0.0
for l, e in ((19, 3), (99, 7), (199, 12)):
J = circlemean.depth(l, e)
exact = float(mp.log(mp.beta(mp.mpf(l + 1) / 2, mp.mpf(J + 2) / 2)))
worst = max(worst, abs(log_i(l, J, 0.0) - exact))
check("CONTROL: the trapezoid's I(0) is B((l + 1)/2, (J + 2)/2) to "
"1e-9 in the log, (l, e) = (19, 3), (99, 7), (199, 12)",
worst < 1e-9, f"{worst:.2e}")
eC, low, over, rcs = {}, [], [], {}
for r in ODD:
l = r - 2
E, eT, rr = rows[r]
exact = {row[0]: row[1] / row[2] for row in rr}
e = E
while True:
rc, ys = ratio_c(l, e)
rcs[(r, e)] = rc
if e in exact:
q = rc / exact[e]
low.append(q)
if r >= 101:
over.append(q)
if rc >= 1:
break
e += 1
eC[r] = e - 1
if r in (21, 31, 41, 51, 101, 151, 201):
print(f" r = {r}: E = {E}, e_C = {eC[r]}, e_T = {eT}; "
f"J(e_C)/l^1.5 = {circlemean.depth(l, eC[r]) / l ** 1.5:.4f}"
f"; ratio_C at e_C + 1 = {rc:.4f}, y/sigma = {ys:.3f}",
flush=True)
print(f" ratio_C/(height/H_ch) over every pair read: {min(low):.4f}.."
f"{max(low):.4f}; at r >= 101: {min(over):.4f}..{max(over):.4f}",
flush=True)
check("P8 floor: ratio_C >= height(F_e)/H_ch at every pair read",
min(low) >= 1)
check("P8: ratio_C/(height/H_ch) <= 1.15 at r >= 101", max(over) <= 1.15)
far = [r for r in FAR_ODD]
kill = sum(1 for r in far if eC[r] <= rows[r][0])
print(f" e_C <= E at {kill} of {len(far)} odd r = 101..201; e_C >= "
f"e_T - 1 at {sum(1 for r in far if eC[r] >= rows[r][1] - 1)}; "
f"e_C >= E + 1 at {sum(1 for r in ODD if eC[r] >= rows[r][0] + 1)}"
f" of {len(ODD)} odd r = 21..201", flush=True)
check("K4 misses: e_C <= E at no more than a quarter of odd r = "
"101..201", 4 * kill <= len(far))
check("P7: e_C >= e_T - 1 at every odd r = 101..201 and e_C >= E + 1 "
"at every odd r = 21..201",
all(eC[r] >= rows[r][1] - 1 for r in far)
and all(eC[r] >= rows[r][0] + 1 for r in ODD))
jc = [circlemean.depth(r - 2, eC[r]) / (r - 2) ** 1.5 for r in far]
print(f" J(e_C)/l^1.5 over odd r = 101..201: {min(jc):.4f}.."
f"{max(jc):.4f}", flush=True)
mm = []
for r in (21, 51, 101, 151, 201):
l = r - 2
for row in rows[r][2]:
J = circlemean.depth(l, row[0])
n = (J + l + 1) / 2
y = 1 / math.sqrt(n)
m = math.exp(log_i(l, J, y) - log_i(l, J, 0.0)
- n * math.log(math.cosh(y)))
mm.append(l * (m - 1))
print(f" l (M(sech y)/M(1) - 1) at y = 1/sqrt(n), every (r, e) of the "
f"odd stage at r = 21, 51, 101, 151, 201: {min(mm):.4f}.."
f"{max(mm):.4f} over {len(mm)} pairs", flush=True)
return rcs, eC
# ---------------------------------------------------------------- the bound
C2 = 3 / 8 * 2 ** 2.5
def delta(l, J, drop_first=False):
"""Phi <= 1 + delta at y = n^(-1/2), n = (J + l + 1)/2: (H7)."""
n = (J + l + 1) / 2
d = 1 / (4 * J) + C2 / 4 * (1 / ((l - 1) * (l - 3)) + 1 / (n * (l - 1))
+ 1 / (n * J) + 1 / (J * (J - 2)))
d += math.exp(4.5) * math.sqrt(2) * 2.0 ** (-(l - 1) / 2)
d += math.exp(1.1) * math.sqrt(2) * 2.0 ** (-J / 2)
return d if drop_first else d + 1 / (4 * (l - 1))
def g_bound(l, e, cosh_term=True, drop_first=False):
"""G = exp(-1/2)(1 + delta) sqrt(2e(4e - 1)(l + 2))/(2(l + 1)) >=
height(F_e)/mean|P| at J_e."""
J = circlemean.depth(l, e)
c = math.exp(-0.5) if cosh_term else math.exp(-1.0)
return (c * (1 + delta(l, J, drop_first))
* math.sqrt(2 * e * (4 * e - 1) * (l + 2)) / (2 * (l + 1)))
def phi_v(l, J, y):
"""Phi = E[(1 - tau/W)^(-1/2); W > tau], V ~ Beta((l + 1)/2, (J + 2)/2),
W = V(1 - V), tau = tanh^2(y)/4: mpmath quad split about the mean."""
a, b = mp.mpf(l + 1) / 2, mp.mpf(J + 2) / 2
tau = mp.tanh(y) ** 2 / 4
root = mp.sqrt(1 - 4 * tau)
vm, vp = (1 - root) / 2, (1 + root) / 2
lb = mp.log(mp.beta(a, b))
def f(v):
w = v * (1 - v)
if w <= tau:
return mp.mpf(0)
return mp.exp((a - 1) * mp.log(v) + (b - 1) * mp.log(1 - v) - lb
) * mp.sqrt(w / (w - tau))
mu = a / (a + b)
sd = mp.sqrt(mu * (1 - mu) / (a + b + 1))
pts = [vm] + [mu + k * sd for k in (-12, -6, -3, 0, 3, 6, 12, 40)
if vm < mu + k * sd < vp] + [vp]
return float(mp.quad(f, sorted(set(pts))))
def eta_bound(l, J, plant=False):
"""1 - eta <= E[sin(t Th)] <= H_ch/mean|P| at even r, min over t:
(H9). Returns (eta, t)."""
a2, b2 = (l + 2) / 2, (J + 1) / 2
mu = a2 / (a2 + b2)
var = a2 * b2 / ((a2 + b2) ** 2 * (a2 + b2 + 1))
k = int(a2)
lp = k * math.log(2) + sum(math.log((a2 + i) / (a2 + b2 + i))
for i in range(k))
ptail = math.exp(lp)
best = None
t_mid = math.pi / (4 * math.asin(math.sqrt(mu)))
for t in range(1, int(2 * t_mid) + 3):
us = math.sin(math.pi / (4 * t)) ** 2
if plant:
eta = t * t / 2 * (var / 2 + (mu - us) ** 2) / us
else:
eta = 4 * t * t * (var + (mu - us) ** 2) / us
eta += math.pi ** 2 * t * t / 2 * ptail
if best is None or eta < best[0]:
best = (eta, t)
return best
def bound_stage(rows, rcs, eC, erows):
print("THE BOUND ((H6)..(H9))", flush=True)
worst = 0.0
for l, e in ((19, 3), (99, 7), (199, 12)):
J = circlemean.depth(l, e)
n = (J + l + 1) / 2
y = 1 / math.sqrt(n)
tform = log_i(l, J, y) - log_i(l, J, 0.0) - n * math.log(math.cosh(y))
worst = max(worst, abs(math.log(phi_v(l, J, y)) - tform))
check("CONTROL: Phi by the v-form equals the t-form's I(y)/(cosh^n I(0))"
" to 1e-8 in the log, (l, e) = (19, 3), (99, 7), (199, 12)",
worst < 1e-8, f"{worst:.2e}")
ka = [(r, e) for (r, e), rc in rcs.items() if g_bound(r - 2, e) < rc]
ka_p = [(r, e) for (r, e), rc in rcs.items()
if g_bound(r - 2, e, cosh_term=False) < rc]
q = [g_bound(r - 2, e) / rc for (r, e), rc in rcs.items()]
qf = [g_bound(r - 2, e) / rc for (r, e), rc in rcs.items() if r >= 101]
print(f" KA: G below ratio_C at {len(ka)} of {len(rcs)} pairs; PLANT "
f"(no exp(1/2)): at {len(ka_p)}", flush=True)
print(f" G/ratio_C over every pair: {min(q):.4f}..{max(q):.4f}; at r >= "
f"101: {min(qf):.4f}..{max(qf):.4f}", flush=True)
check("PLANT KA caught: G without exp(1/2) falls below ratio_C",
len(ka_p) > 0)
check("KA misses, Q1: G >= ratio_C at every pair; G/ratio_C in [1, 1.02]"
" at r >= 101", not ka and 1 <= min(qf) and max(qf) <= 1.02)
kb, kb_p, ld = 0, 0, []
for r in ODD:
l = r - 2
for row in rows[r][2]:
J = circlemean.depth(l, row[0])
y = 1 / math.sqrt((J + l + 1) / 2)
ph = phi_v(l, J, y)
kb += ph > 1 + delta(l, J)
kb_p += ph > 1 + delta(l, J, drop_first=True)
if r >= 101:
ld.append(l * delta(l, J))
print(f" KB: Phi above 1 + delta at {kb} pairs; PLANT (no 1/(4(l - 1))):"
f" at {kb_p}; l delta at r >= 101: {min(ld):.4f}..{max(ld):.4f}",
flush=True)
check("PLANT KB caught: delta without 1/(4(l - 1)) falls below Phi - 1",
kb_p > 0)
check("KB misses, Q2: Phi <= 1 + delta at every pair; l delta in "
"[0.25, 0.40] at r >= 101", kb == 0 and 0.25 <= min(ld)
and max(ld) <= 0.40)
eG, q3 = {}, []
for r in ODD:
l, e = r - 2, 1
while g_bound(l, e + 1) < 1:
e += 1
eG[r] = e
for r in FAR_ODD:
q3.append(eG[r] >= eC[r] - 1 and eG[r] > rows[r][0])
for r in (21, 51, 101, 151, 201):
l = r - 2
print(f" r = {r}: E = {rows[r][0]}, e_G = {eG[r]}, e_C = {eC[r]}, "
f"e_T = {rows[r][1]}; J(e_G)/l^1.5 = "
f"{circlemean.depth(l, eG[r]) / l ** 1.5:.4f}", flush=True)
jg = [circlemean.depth(r - 2, eG[r]) / (r - 2) ** 1.5 for r in FAR_ODD]
print(f" J(e_G)/l^1.5 over odd r = 101..201: {min(jg):.4f}.."
f"{max(jg):.4f}", flush=True)
check("Q3: e_G >= e_C - 1 and e_G > E at every odd r = 101..201",
all(q3))
lu = [l * delta(l, 3 * l - 1) for l in range(99, 10002, 2)]
print(f" l delta_u(l) at odd l = 99, 999, 10001: {lu[0]:.4f}, "
f"{lu[450]:.4f}, {lu[-1]:.4f}", flush=True)
check("THEOREM: l delta_u(l) < 0.35 at l = 99, so at every l >= 99 "
"by (H8)'s fall", lu[0] < 0.35, f"{lu[0]:.6f}")
check("Q4: l delta_u(l) <= 0.40 and falling over odd l = 99..10001",
lu[0] <= 0.40 and all(x > y for x, y in zip(lu, lu[1:])))
kc_n, kc_m, kc_p, etas = 0, 0, 0, {}
for r in EVEN:
e, J, A, hp, hH = erows[r]
l = r - 2
eta, t = eta_bound(l, J)
etas[r] = (eta, t)
kc_n += 1 - eta > hp
kc_m += eta < 1 and g_bound(l, e) / (1 - eta) < hH
kc_p += 1 - eta_bound(l, J, plant=True)[0] > hp
print(f" KC: 1 - eta above H_ch/mean|P| at {kc_n}, G/(1 - eta) below "
f"height/H_ch at {kc_m} of {len(EVEN)} even r at e_T; PLANT (Var/2,"
f" 8 -> 1): at {kc_p}", flush=True)
for r in (22, 50, 100, 150, 200):
print(f" r = {r}: e_T = {erows[r][0]}, t = {etas[r][1]}, eta = "
f"{etas[r][0]:.4f}, 1 - eta against H_ch/mean|P| = "
f"{erows[r][3]:.4f}", flush=True)
check("PLANT KC caught", kc_p > 0)
check("KC misses, Q5: eta bound holds at every even r = 22..200; eta < "
"0.15 at r >= 150", kc_n + kc_m == 0
and all(etas[r][0] < 0.15 for r in EVEN if r >= 150))
def main():
t0 = time.time()
controls()
identity()
rows = odd_stage()
erows = even_stage()
if "--walk" in sys.argv:
walk_stage()
rcs, eC = shift_stage(rows)
bound_stage(rows, rcs, eC, erows)
print(f"{sum(CHECKS)}/{len(CHECKS)} checks, {time.time() - t0:.1f} s",
flush=True)
return 0 if all(CHECKS) else 1
if __name__ == "__main__":
sys.exit(main())