binet.py
Checks: FLATTEN: The takeover depth grows as r^(3/2) theorem; property; rule; pattern; observation.
Cited on: FLATTEN.
To run it: save binet.py, with band.py, bandend.py, bandfamily.py, bandheight.py, bandscale.py, circlemean.py, flatten.py, prunerr.py beside it; needs mpmath, numpy, scipy.
"""binet.py -- at the takeover's exact depths, is the ratio R of
circlemean.py bounded by Stirling's limit itself, at every rank?
QUESTION. At depth J = (4e - 1) l - 1, l = r - 2, F_e's mean modulus on
the circle over the champion's pairing with a test w is R; R < 1 gives
J_ch(r) > J (circlemean.py). Stirling's formula without error terms has
R ~ sqrt(e (4e - 1)/(pi l)) at odd r. Does R sit below that limit at
every odd l and every e, so that no threshold l0 is owed? And at even
r, with the test p = 4e - 1, is R below an elementary G1 whose
condition G1 < 1 reaches e ~ 0.674 sqrt(l)?
BY HAND. Binet's function m(x) = log Gamma(x) - (x - 1/2) log x + x -
log sqrt(2 pi) is positive and strictly decreasing on x > 0 (known:
Binet's first formula, Whittaker and Watson 12.31; its integrand
(1/2 - 1/t + 1/(exp t - 1))/t is positive). So
Gamma(N)/(Gamma(a) Gamma(b)) = (2 pi)^(-1/2) sqrt(ab/N) N^N a^-a b^-b
exp(D),
D = m(N) - m(a) - m(b) < 0 whenever N > a.
(O) Odd l, w = 1. a = l/2, b = (4e - 1) l/2, N = a + b = 2el, mu = a/N
= 1/(4e). L = 2^(J+l+1) B(a + 1, b)/pi = 2^(J+l-1) B(a, b)/(pi e)
and mean = (2^(J+l-1)/pi)(8e/N) mu^a (1 - mu)^b, so R = (4e/l)
mu^a (1 - mu)^b/B(a, b). As mu^a (1 - mu)^b N^N a^-a b^-b = 1 and
ab/N = (4e - 1) l/(8e):
R = sqrt(e (4e - 1)/(pi l)) exp(D), D < 0.
So R < 1 once e (4e - 1) < pi l. The largest such e, e_g, exceeds
x0 - 1, x0 = (1 + sqrt(1 + 16 pi l))/8, and 4 x0 > 1/2 + 2 sqrt(pi
l), so 4 e_g - 1 > 2 sqrt(pi l) - 4.5 and J_ch(r) > 2 sqrt(pi)
l^(3/2) - 4.5 l - 1 at every odd r >= 5. By m(x) = 1/(12x) +
O(x^-3), 2D = -1/(3l) - 1/(3 (4e - 1) l) + 1/(12el) + O(l^-3).
(E) Even l, w = x^k (1 + x)^p (1 - x), p = 4e - 1, a, b, N, mu as in
(O). a' = (l + 3)/2, b' =
(4e - 1)(l + 1)/2, N' = a' + b' = 2e (l + 1) + 1 and R = (4/l)
2^(-p-2) C(p, (p - 1)/2) mu^a (1 - mu)^b/B(a', b'). Binet at (a',
b', N') gives R < G0 = (4/l) 2^(-p-2) C(p, (p - 1)/2) (2 pi)^(-1/2)
sqrt(a'b'/N') X, X = (a/N)^a (b/N)^b (N'/a')^a' (N'/b')^b', and
G0^2 pi is rational. Then C(p, (p - 1)/2) = C(p + 1, 2e)/2 <= 2^p
sqrt(2/(pi (p + 1))) by C(2n, n) <= 4^n/sqrt(pi n) (known); N'/a'
< 4e; a'b'/N' < (l + 3)(4e - 1)/(8e); (N'/b')^(p/2) <=
exp(1/2 + 1/(l + 1)); the rest of X, exp(N log(1 + u) - a log(1 +
3/l) - b log(1 + 1/l)), u = (2e + 1)/N, is at most exp((e + 2)/l).
So R < G1 = (sqrt 2/pi) sqrt(e (4e - 1)(l + 3))/l exp(1/2 + 1/(l +
1) + (e + 2)/l), and R < 1 under C(l, e): 2e (4e - 1)(l + 3)
exp(1 + 2/(l + 1) + 2 (e + 2)/l) < pi^2 l^2, which at e = kappa
sqrt(l) holds as l grows iff kappa < pi/(2 sqrt(2 exp 1)): J/l^(3/2)
-> pi sqrt(2) exp(-1/2) = 2.6947. A sketch, to leading order: over
w = x^k (1 + x)^p (1 - x), L = |<P, w>|/||w||_1 moves with p as
sqrt(p) (1 - mu)^(p/2) (the pairing's 2^p (1 - mu)^(p/2) over
||w||_1, about 2^(p+1) sqrt(2/(pi p))), whose log has derivative 1/(2p) +
log(1 - mu)/2, zero near p = 1/mu = 4e: p = 4e - 1 is about the
family's best (exactly, 4e - 3 once l > 4e - 2: L's ratio at p + 2
to p is (J + p + 1)(p + 3)/((J + p + l + 4)(p + 2))), so 2.6947 is
its ceiling on this route.
The controls by hand: (l, e) = (3, 1), R = 27 sqrt3/(28 pi) = 0.5316
below sqrt(1/pi) = 0.5642; (2, 1), R = 81/(28 pi) = 0.9208 below G0 =
0.958, exp(D') = 0.961.
PREDICTIONS (frozen before this engine ran).
P1 at every odd l = 3..401, e = 1..2 floor(sqrt l) + 2: R^2 < e (4e -
1)/(pi l) exactly; exp(2D) within 1e-3 relative of its leading
order.
P2 at every odd r = 5..1001, R < 1 at e_g; E = e_g at 95% of the
ranks or more, e_g + 1 at the rest (an observation).
P3 at every even l = 4..484, e = 1..2 floor(sqrt l) + 2: R < G0
exactly and G0 <= G1 (mpmath, 50 digits).
P4 e_C, the largest e with C(l, e), is at least floor(sqrt l/2) from
some small l on; 4 e_C/sqrt l rises toward 2.6947 (an
observation: the onset and the values printed).
KILLS. "KILL O: odd pairs with R^2 >= e(4e - 1)/(pi l): n of N" and
"KILL E: even pairs with R >= G0 or G0 > G1: n of N"; n > 0 kills.
CONTROLS. POSITIVE: R at (3, 1) and (2, 1) from circlemean.rho, G0 at
(2, 1). The identity R^2 pi l/(e (4e - 1)) = exp(2D) read against
Binet's function from mpmath's loggamma at every odd pair: the
instrument's own control. PLANTS: R^2 times (1 + 1/(2l)) must fire
KILL O at every pair, times (1 + 1/(6l)) at none; G1 without exp(1/2)
must fire KILL E's second arm at every pair.
FINDINGS (copied from the printed output).
(1) Controls: R(3, 1) = 0.5316 below sqrt(1/pi) = 0.5642; R(2, 1) =
0.9208, G0(2, 1) = 0.9582, G1(2, 1) = 8.9895. log_g0 is the log
of the exact G0 to 4.6e-49 at every even l = 2..20 (0.958169 at
(2, 1), 0.625344 at (4, 1)).
(2) "KILL O: odd pairs with R^2 >= e(4e - 1)/(pi l): 0 of 5578":
MISSES. The largest R^2 pi l/(e (4e - 1)) is 0.99916906; it is
exp(2D) from loggamma to 2.8e-45 at every pair. The plants read
as frozen: (1 + 1/(2l)) fires at 5578 of 5578, (1 + 1/(6l)) at 0.
(3) P2 holds: R < 1 at e_g at 499 of 499 odd r = 5..1001; E = e_g at
494, e_g + 1 at r = 21, 213, 247, 611, 991. At r = 21, 101, 1001
e_g = 3, 8, 28 (the Cauchy-Schwarz proof's 3, 7, 22), J/l^(3/2) =
2.5115, 3.1146, 3.5119.
(4) "KILL E: even pairs with R >= G0 or G0 > G1: 0 of 7366":
MISSES. Least G1/G0 1.04132, largest R/G0 0.999658, R/G0 =
exp(D') to 1.59e-45. The plant G1 exp(-1/2) fires at 7290 of
7366, its misses at even l = 4..32 and 36: G1 is loose at small
l.
(5) P4: e_C < floor(sqrt l/2) at even l = 4, 6, 16 alone of 4..484;
e_C >= 1 from l = 8. 4 e_C/sqrt l = 2.4000, 2.5455, 2.6800,
2.6920, 2.6944 at l = 100, 484, 10^4, 10^6, 10^8, against the
limit 2.6947.
(6) The hand condition at e = floor(sqrt l/2), (1 + 3/l) exp(2/(l +
1) + (sqrt l + 4)/l) < pi^2/(2 exp 1) = 1.815412: 1.84008 at l =
22, 1.765747 at l = 24, falling over even l = 24..484 (a check
added after the first run; starting it at l = 20 fails it).
FAILED AS FROZEN. P1's band: the leading order is off by 0.00152 at
worst, the m(x) term -1/(360 x^3), at l = 3 by a scratch read (by hand
0.0017); a print, never a check. The plant's "every pair": 76 pairs
miss, at l <= 36; its check reads l >= 38, a scope set after the run.
CHANGED AFTER THE FREEZE. KILL E's arms are read at 50 digits through
logs, not in exact rationals: the exact G0^2 pi ran past the ten-minute
wall in its first run. R/G0 is set against exp(D') from loggamma at
every even pair, and log_g0 against the exact G0 at even l = 2..20;
the odd arm's identity control is not in the freeze either. P3's
"exactly" is so overridden.
RUN RECORD. 13/13 checks, 221.8 s, peak 59.5 MB (one process): the
odd arm 60 s, the ranks 30 s, the even arm 132 s.
RUN: python binet.py
"""
import math
import os
import sys
import time
os.environ.setdefault("OPENBLAS_NUM_THREADS", "1")
import mpmath as mp # noqa: E402
from circlemean import depth, proof_e, rho, sure # noqa: E402
mp.mp.dps = 50
CHECKS = []
ODD_L = range(3, 402, 2)
EVEN_L = range(4, 485, 2)
WIDE_ODD_R = range(5, 1002, 2)
LIMIT_E = math.pi * math.sqrt(2) * math.exp(-0.5)
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)
def es(l):
return range(1, 2 * math.isqrt(l) + 3)
# ------------------------------------------------------------- odd arm
def odd_side(l, e, rr, num=1, den=1):
"""R^2 (num/den) against e(4e - 1)/(pi l): -1 below, 1 above; rr is
rho(l, e), R^2 pi^2."""
rn, rd = rr
return sure(rn * l * num, rd * e * (4 * e - 1) * den, 1)
def binet(x):
x = mp.mpf(x)
return mp.loggamma(x) - (x - mp.mpf(1) / 2) * mp.log(x) + x \
- mp.log(2 * mp.pi) / 2
def odd_ratio(l, e, rr):
"""(R^2 pi l/(e(4e - 1)) from the closed forms, exp(2D) from Binet,
the leading order exp(-1/(3l) - 1/(3(4e - 1)l) + 1/(12el)))."""
rn, rd = rr
got = mp.mpf(rn) / rd * l / (mp.pi * e * (4 * e - 1))
a, b = mp.mpf(l) / 2, mp.mpf((4 * e - 1) * l) / 2
D = binet(a + b) - binet(a) - binet(b)
lead = mp.exp(-mp.mpf(1) / (3 * l) - mp.mpf(1) / (3 * (4 * e - 1) * l)
+ mp.mpf(1) / (12 * e * l))
return got, mp.exp(2 * D), lead
def e_g(l):
"""The largest e with e(4e - 1) < pi l."""
e = 0
while sure((e + 1) * (4 * e + 3), l, 1) < 0:
e += 1
return e
# ------------------------------------------------------------ even arm
def g0pi(l, e):
"""G0^2 pi, a rational (num, den), at even l."""
p = 4 * e - 1
a, b = l // 2, p * l // 2
N, N2 = a + b, 2 * e * (l + 1) + 1
C = math.comb(p, (p - 1) // 2)
xn = (a ** l * b ** (p * l) * (2 * N2) ** (l + 3)
* (2 * N2) ** (p * (l + 1)))
xd = (N ** l * N ** (p * l) * (l + 3) ** (l + 3)
* (p * (l + 1)) ** (p * (l + 1)))
return (8 * C * C * (l + 3) * p * (l + 1) * xn,
4 * N2 * l * l * 4 ** (p + 2) * xd)
def g1(l, e):
l, e = mp.mpf(l), mp.mpf(e)
return (mp.sqrt(2) / mp.pi * mp.sqrt(e * (4 * e - 1) * (l + 3)) / l
* mp.exp(mp.mpf(1) / 2 + 1 / (l + 1) + (e + 2) / l))
def hand_lhs(l):
"""(1 + 3/l) exp(2/(l + 1) + (sqrt l + 4)/l): G1 < 1 at e =
floor(sqrt l/2) once it is below pi^2/(2 exp 1)."""
l = mp.mpf(l)
return (1 + 3 / l) * mp.exp(2 / (l + 1) + (mp.sqrt(l) + 4) / l)
def e_c(l):
"""The largest e >= 0 with G1(l, e) < 1 (G1 rises in e)."""
e = 0
while g1(l, e + 1) < 1:
e += 1
return e
def log_g0(l, e):
"""log G0 at even l, at mp.dps digits."""
p = 4 * e - 1
a, b = mp.mpf(l) / 2, mp.mpf(p * l) / 2
a2, b2 = mp.mpf(l + 3) / 2, mp.mpf(p * (l + 1)) / 2
N, N2 = a + b, a2 + b2
X = (a * mp.log(a / N) + b * mp.log(b / N) + a2 * mp.log(N2 / a2)
+ b2 * mp.log(N2 / b2))
return (mp.log(mp.mpf(4) / l) - (p + 2) * mp.log(2)
+ mp.log(math.comb(p, (p - 1) // 2)) - mp.log(2 * mp.pi) / 2
+ mp.log(a2 * b2 / N2) / 2 + X)
def even_row(l):
"""(pairs, R >= G0 count, G0 > G1 count, plant fired count, least
G1/G0, largest R/G0, largest |R/G0 / exp(D') - 1|)."""
n = bad0 = bad1 = fired = 0
least = largest = None
worst = mp.mpf(0)
for e in es(l):
rn, rd = rho(l, e) # R^2 pi^2
logR = (mp.log(rn) - mp.log(rd)) / 2 - mp.log(mp.pi)
lg0 = log_g0(l, e)
lg1 = mp.log(g1(l, e))
n += 1
bad0 += logR >= lg0
bad1 += lg0 > lg1
fired += lg0 > lg1 - mp.mpf(1) / 2
q, rr = mp.exp(lg1 - lg0), mp.exp(logR - lg0)
least = q if least is None or q < least else least
largest = rr if largest is None or rr > largest else largest
p = 4 * e - 1
a2, b2 = mp.mpf(l + 3) / 2, mp.mpf(p * (l + 1)) / 2
D2 = binet(a2 + b2) - binet(a2) - binet(b2)
worst = max(worst, abs(rr / mp.exp(D2) - 1))
return n, bad0, bad1, fired, least, largest, worst
# ---------------------------------------------------------------- main
def main():
t0 = time.time()
print("CONTROLS", flush=True)
r31 = math.sqrt(rho(3, 1)[0] / rho(3, 1)[1]) / math.pi
r21 = math.sqrt(rho(2, 1)[0] / rho(2, 1)[1]) / math.pi
gn, gd = g0pi(2, 1)
G021 = math.sqrt(gn / gd / math.pi)
print(f" R(3, 1) = {r31:.4f}, sqrt(1/pi) = {1 / math.sqrt(math.pi):.4f};"
f" R(2, 1) = {r21:.4f}, G0(2, 1) = {G021:.4f}, G1(2, 1) = "
f"{float(g1(2, 1)):.4f}", flush=True)
check("POSITIVE: R(3, 1) = 0.5316 and R(2, 1) = 0.9208 to 4 places",
abs(r31 - 0.5316) < 1e-4 and abs(r21 - 0.9208) < 1e-4)
check("POSITIVE: G0(2, 1) = 0.958 by hand, to 3 places",
abs(G021 - 0.958) < 1e-3, f"{G021:.5f}")
worst_g = mp.mpf(0)
for l in range(2, 21, 2):
for e in es(l):
gn, gd = g0pi(l, e)
exact = mp.log(mp.mpf(gn) / gd / mp.pi) / 2
worst_g = max(worst_g, abs(log_g0(l, e) - exact))
print(f" log_g0 against the exact G0^2 pi at even l = 2..20: worst "
f"{mp.nstr(worst_g, 3)}; at (2, 1), (4, 1): "
f"{mp.nstr(mp.exp(log_g0(2, 1)), 6)}, "
f"{mp.nstr(mp.exp(log_g0(4, 1)), 6)}", flush=True)
check("CONTROL: log_g0 is the log of the exact G0 at every even l = "
"2..20, to 1e-30", worst_g < mp.mpf(10) ** -30,
mp.nstr(worst_g, 3))
print("ODD ARM", flush=True)
n = bad = fire2 = fire6 = 0
worst_id = worst_lead = mp.mpf(0)
top = None
for l in ODD_L:
for e in es(l):
n += 1
rr = rho(l, e)
bad += odd_side(l, e, rr) >= 0
fire2 += odd_side(l, e, rr, 2 * l + 1, 2 * l) >= 0
fire6 += odd_side(l, e, rr, 6 * l + 1, 6 * l) >= 0
got, b2, lead = odd_ratio(l, e, rr)
worst_id = max(worst_id, abs(got / b2 - 1))
worst_lead = max(worst_lead, abs(got / lead - 1))
top = got if top is None or got > top else top
print(f" KILL O: odd pairs with R^2 >= e(4e - 1)/(pi l): {bad} of {n}",
flush=True)
print(f" largest R^2 pi l/(e(4e - 1)) over them: {mp.nstr(top, 8)}",
flush=True)
check("KILL O misses", bad == 0)
check("CONTROL: R^2 pi l/(e(4e - 1)) is exp(2D) from loggamma, to 1e-30",
worst_id < mp.mpf(10) ** -30, mp.nstr(worst_id, 3))
print(f" P1's band, exp(2D) within 1e-3 relative of its leading "
f"order: worst {mp.nstr(worst_lead, 3)}, "
f"{'held' if worst_lead < 1e-3 else 'missed'}",
flush=True)
check("PLANT: R^2 (1 + 1/(2l)) fires KILL O at every pair",
fire2 == n, f"{fire2} of {n}")
check("PLANT: R^2 (1 + 1/(6l)) fires KILL O at no pair",
fire6 == 0, f"{fire6} of {n}")
print(f" ({time.time() - t0:.1f} s)", flush=True)
print("ODD RANKS, e_g AND E", flush=True)
below = eq = one = more = 0
ones = []
for r in WIDE_ODD_R:
l = r - 2
g = e_g(l)
below += sure(*rho(l, g)) < 0
if sure(*rho(l, g + 1)) >= 0:
eq += 1
elif sure(*rho(l, g + 2)) >= 0:
one += 1
ones.append(r)
else:
more += 1
nr = len(WIDE_ODD_R)
print(f" odd r = 5..1001: R < 1 at e_g at {below} of {nr}; E = e_g at "
f"{eq}, e_g + 1 at {one}, more at {more}", flush=True)
print(f" the ranks with E = e_g + 1: {ones}", flush=True)
check("P2: R < 1 at e_g at every odd r = 5..1001", below == nr)
check("P2: E = e_g at 95% or more, e_g + 1 at the rest",
eq >= 0.95 * nr and eq + one == nr, f"{eq}, {one}, {more}")
for r in (21, 101, 1001):
l = r - 2
g = e_g(l)
print(f" r = {r}: e_g = {g}, J = {depth(l, g)}, J/l^(3/2) = "
f"{depth(l, g) / l ** 1.5:.4f}; floor(sqrt(l/2)) = "
f"{proof_e(l)}", flush=True)
print(f" ({time.time() - t0:.1f} s)", flush=True)
print("EVEN ARM", flush=True)
tot = [0, 0, 0, 0]
least = largest = None
worst = mp.mpf(0)
missed = []
for l in EVEN_L:
n, b0, b1, f, q, rr, w = even_row(l)
worst = max(worst, w)
if f < n:
missed.append(l)
for i, v in enumerate((n, b0, b1, f)):
tot[i] += v
least = q if least is None or q < least else least
largest = rr if largest is None or rr > largest else largest
print(f" KILL E: even pairs with R >= G0 or G0 > G1: "
f"{tot[1] + tot[2]} of {tot[0]} ({tot[1]} and {tot[2]})",
flush=True)
print(f" least G1/G0 {mp.nstr(least, 6)}, largest R/G0 "
f"{mp.nstr(largest, 6)}", flush=True)
check("KILL E misses", tot[1] + tot[2] == 0)
check("CONTROL: R/G0 is exp(D') from loggamma, to 1e-30",
worst < mp.mpf(10) ** -30, mp.nstr(worst, 3))
print(f" the plant G1 exp(-1/2) fires G0 > G1 at {tot[3]} of {tot[0]};"
f" the even l with a miss: {missed}", flush=True)
check("PLANT: G1 exp(-1/2) fires G0 > G1 at every pair with l >= 38",
all(l <= 36 for l in missed), f"misses at l = {missed}")
bar = mp.pi ** 2 / (2 * mp.e)
lhs = [hand_lhs(l) for l in range(22, 485, 2)]
print(f" the hand condition at e = floor(sqrt l/2): (1 + 3/l) exp(2/(l"
f" + 1) + (sqrt l + 4)/l) = {mp.nstr(lhs[0], 7)} at l = 22, "
f"{mp.nstr(lhs[1], 7)} at l = 24, against pi^2/(2 exp 1) = "
f"{mp.nstr(bar, 7)}", flush=True)
check("HAND: the condition fails at l = 22, holds at l = 24 and falls "
"over even l = 24..484", lhs[0] >= bar and lhs[1] < bar
and all(u > v for u, v in zip(lhs[1:], lhs[2:])))
lag = [l for l in EVEN_L if e_c(l) < proof_e(l)]
first = next(l for l in EVEN_L if e_c(l) >= 1)
print(f" even l = 4..484 with e_C < floor(sqrt l/2): {lag}", flush=True)
print(f" least even l with e_C >= 1: {first}", flush=True)
for l in (100, 484, 10 ** 4, 10 ** 6, 10 ** 8):
c = e_c(l)
print(f" l = {l}: e_C = {c}, floor(sqrt l/2) = {proof_e(l)}, "
f"4 e_C/sqrt l = {4 * c / math.sqrt(l):.4f}", flush=True)
print(f" the limit pi sqrt2 exp(-1/2) = {LIMIT_E:.4f}", flush=True)
print(f" ({time.time() - t0:.1f} s)", flush=True)
ok = all(CHECKS)
print(f"{sum(CHECKS)}/{len(CHECKS)} checks pass", flush=True)
return 0 if ok else 1
if __name__ == "__main__":
sys.exit(main())