hulls.py
Checks: LATENCY: The threshold-miss law theorem; rule; pattern; LATENCY: The matching law rule.
Cited on: LATENCY.
To run it: save hulls.py, with chains.py, manystream.py, poles.py, redundant.py beside it; needs numpy.
"""
hulls.py -- a producer with a hull per string: does x y feed z·U past the
threshold-miss law?
QUESTION. chains.py's (7) predicts the two-unit chain x y -> z·U dead at
(L1*, L2* - 1) wherever Lam2 - 1 > (Mh/w) min(S(L1*), room), on the
budget (3),
which puts one hull at each value. A level-n prefix of U is a digit
STRING; its children are one per integer, but strings that share a
value are distinct nodes, each covering its own fibre. Is the budget
the law of that tree, or can a producer hand z·U narrower hulls?
THE MODEL (frozen before this file). Level units, the
deep limit made exact by bounding: every head image is at most
d = 2 Mh w / b^L1 wide (level-n units), and z·U accepts every hull of
width at most eta_max = (b^L2 (w - 1) - tau w)/Mh, tau = Mh^2, at any
position: its image of a z box times the hull is at most
(Mh |H| + tau w)/b^L2 wide in level n - L2 units (chains.py's (6)),
and an interval no longer than w - 1 lies in a tile. z·U's verdict on a hull is a statement about the set alone
(the tile lemma), never about the string or value holding it, and
accepted hulls are downward closed, so a head image is homeless iff it
is rejected as a hull. That needs tau w + Mh d > b^L2 (w - 1), at
L2 = 1 Mh^2 (1 + 2/b^L1) > 1: never at Mh < 1, L1 >= 2. There the
converse cannot rest on one image; it lives in the tree.
A STATE (o, F) is a string whose fibre is held in
[u + o, u + o + F] inside its box (-M- <= o, o + F <= M+), F <= eta.
It is SUSTAINED when children e in D (one per integer, taken in
increasing e), each a sustained state, cover [b o, b (o + F)] so that
every window of width d lies in one child's interval. The greatest
fixpoint of F(o) on a grid of offsets (step 1/G, G = (b - 1) K, so
M+- are exact; d rounded up, eta down, the root range outward) is a
certificate: a producer sending each (x, y) box to a child whose
interval holds its image is legal and hands z·U hulls <= eta_max, so
the chain lives. Children in increasing order and the grid only lose
producers, never invent one. The certificate is re-verified by an
exact checker over the recorded children. A root whose range, read at
level 1, is no wider than d holds no window of width d, so the cover
would pass it with no child, though its images, narrower than d, must
each reach one: the grid gives such a root no verdict.
THE DRIFT (the estimate behind the prediction). Children at
consecutive integers e_1 < ... < e_m have offsets
o_(k+1) <= o_k + F_k - d - 1, so each drifts down by 1 + d - F_k inside
a window of length w - F, and bF = sum_(k<m) (F_k - d) + F_m. At a
uniform width F = 1 + d - x, m - 1 = (b - 1) F/(F - d) and the drift
(m - 1) x <= w - F = S + x, so x is about S/(b - 2). The drift resets
at the next parent, since strings that share a value cover their own
intervals; the budget's one hull per value is the case x = 0.
PREDICTIONS (frozen). The least sustaining width eta* is below 1 + d
(the budget's hull, w - S) by about S/(b - 2) at every b >= 3; cells
where eta* <= eta_max < 1 + d gain past (7).
KILL. eta* >= 1 + d at every slack-1 product-fed cell, radices 3..40,
Mh < 1: the budget is the tree's law.
CONTROL, read first. At eta = 1 + d the narrowed reader's state
(o = -M-, F = 1 + d) is in the fixpoint; at eta < d the root is not
covered; the checker rejects a certificate with one child moved past
its box. CC's 17 gaining cells certified at (2, 1) is read by H1's
check over the sweep, not in the control.
As run, against the frozen control: "at eta < d the fixpoint is
empty" is read as "the root is not covered" (a state with no children
keeps F = d//b, holding no image), and the narrowed state is read at
o = -M- alone.
H2 (designed after the record run, so the bare run checks the gain it
found): (20, 8, 12) and its mirror at K = 200, (L1*, L2* - 1) = (2, 1),
live at eta_max with 0 checker faults, where chains.two_unit's least
is the naive sum.
H3, SLACK 2 OR MORE (a+- <= b - 1, representable; q = b - 1,
m = max a+-, s = a- + a+ = q + rho). Does z.U read below L1* + L2*?
BY HAND. (A) L2* <= 1 iff b rho q^2 >= m (m + q) s, and L2* = 2 needs
b rho q^2 < m (m + q)(q + rho) <= 2 q^2 (q + rho), so rho (q - 1) <
2q: rho <= 2 (at q = 2, rho <= q). At rho = 2, 2 (q + 1) q^2 <
m (m + q)(q + 2) holds at m = q and fails at m <= q - 1 ((q - 1)
(2q - 1)(q + 2) = 2q^3 + q^2 - 5q + 2 < 2q^3 + 2q^2); a- = q, a+ = 2 is
representable only at q = 2. So L2* = 2 only at THE FAMILY (b, 2,
b - 1), b >= 3, where L2* <= 2 as b^2 >= b + 1; L2* = 0 nowhere, as
2 Mh (1 - Mh - Mh^2) <= 10/27 < 1.
(B) Off the family the one pair below the sum is (L1*, 0), and
eta_max(0) < 0 iff w (1 - Mh^2) < 1, while w (1 - Mh^2) <= 2 Mh
(1 - Mh^2) <= 4/(3 sqrt 3): no hull is narrow enough. THE GENERIC
MULTIPLIER makes it a death with no limit, behind any producer.
(G) Every u in U's range is a value of U, so at every level some
string's fibre holds u. At consumer delay L2 (output level n - L2,
tiles [k - M-, k + M+] in its units) z's level-n prefix N (any
integer in range) has box [N - M-, N + M+], and with U = u alone the
image [N - M-, N + M+] t, t = u/b^L2, lies in a tile iff an integer
lies in a window of length l(u) = w (1 - |t|) whose left ends, over q
consecutive N, q = den(t), are a coset of (1/q)Z mod 1: all fit only if
l(u) >= 1 - 1/q. Take u -> tau, U's extreme of largest size (Mh^2
behind a product; 2 M+/b or -2 M-/b behind a sum at lead 1), with q
prime -> infinity: dead wherever eta_max(L2) < 0. The pin at tau itself
can fit: at Mh = 1, U = 1 gives z . 1 = z. (G') A string whose fibre
lies within O(b^-n) of u, its hull F wide (level-n units), leaves at
|N| near Mh b^n the window l(u) - Mh F/b^L2 + o(1), its left ends a
coset shifted by the string's offset, + o(1); so, q held as n grows,
it is killed once F > eta(u) + b^L2/(q Mh), eta(u) = (b^L2 (w - 1) -
w|u|)/Mh, which tends to eta_max(L2) as u -> tau (the bar closes to
eta_max(L2) only as q grows and u -> tau). The fibre holding u holds the image of the input box
read, an interval d wide at a place fixed by the string and not by z:
dead wherever eta_max(L2) < d, d the width of the fibres holding
values near tau (2 Mh w/b^L1 behind a product near its corner,
2 w/b^L1 behind a sum at lead 1). So behind a product z.U never reads
at L2 = 0 at a representable cell: w (1 - Mh^2) < 1 at Mh < 1 (above)
and is 0 at Mh = 1. PREDICTIONS (frozen): (R1) the integer coset sweep
at u = tau - eps, den(u) a prime q > 10^4, misses at some N exactly
where l(u) < 1 - 1/q, every representable cell, rho >= 1, radices
3..40, L2 in {0, 1}, both heads; (R2) behind a sum, slack >= 2,
L2* = 1, the sweep pinned AT tau all-hits at some cell with
w (1 - tau) < 1. CONTROL, read first: at L2 = 1 the generic sweep
behind a product hits every N wherever L2* = 1; at Mh = 1, L2 = 0 the
pin at tau = 1 hits every N.
(C) At the family L1* = 2, d = 2 (b + 1)/((b - 1) b^2), eta_max(1) = 1
exactly, the room 0 (U's range is its whole box).
(D) THE FREE ROOT. The root above caps the level-1 states at eta, but
a level-n string feeds output level n - L2, so at L2 = 1 the level-1
fibres feed the root tile, which holds the range: each level-1 state
is as wide as its box and the fixpoint's children allow, and the root
is covered by those. At L2 = 1 the free root is the model; the capped
one is exact at L2 = 0.
(E) The private-zone floor at slack 2 is at most 1 + d - S/(b - 2) =
1 - 2/(b^2 (b - 1)(b - 2)) < eta_max: it decides no family cell.
THE TOP CORNER FORCES 1 + d in the uniform tree wherever the range's
top is the box top (hi = M+). A value above M+ - b^-n lies only on the
all-top string, so its hull is top-aligned at every level: its top
child e = a+ ends at a+ + M+ = b M+, a lower child at most at
(a+ - 1) + M+, and a window of width d across the top child's start
lies in a lower child, so that start is at most a+ - 1 + M+ - d and
the top child, itself top-aligned, holds F >= 1 + d. The windows exist
where images are d wide and dense (floor.py's (G)), a limit derived,
not proved. At (10, 6, 4), a- = 6, a+ = 4, hi = 36/81 = 4/9 = M+: the
lower child tops at 3 + 4/9.
THE CORNER (the family, M+ = 1, a- = 2): z.U's images are pinned at
U's top. A hull t_hi..t_lo below it (level-n units), L2 = 1: z >= 0
kills iff an integer lies in (2 - zeta t_lo, 1 - zeta t_hi), zeta in
[0, 1]; z <= 0 iff one lies in (2 + zeta t_hi, 1 + zeta t_lo), zeta in
[0, M-]; finite n only shrinks them. The corner tree holds corner
states at box distance 0..Sc, Sc = min(b^2 - 3, 40), under that
criterion, the eta fixpoint
beyond.
PREDICTIONS (frozen). (P1) L2* = 2 exactly at the family; (P2) (C)'s
values; (P3) the capped root covers no family cell, even at eta = w;
(P4) the free root certifies no family cell at eta_max; (P5) over the
slack-1 cells the free root lives exactly where the capped one does;
(P6) no hull the corner criterion accepts is killed by an exact sweep
of every z box at level 3; (P7) the corner tree certifies no family
cell. KILL (of "the tree prices the family"): P4 and P7 hold; then
the family is open between (E) and the tree. A certificate at eta_max
would be a gain, (x y).z at 3 against the naive 4.
CONTROLS, read first: (20, 8, 12) live under both roots; the free
checker passes a free certificate and finds a fault once one level-1
child is moved past its box; capped at eta, the free code is
Grid.live at every slack-1 cell; the corner tree is live at the free eta* and, its corner off, is the free
root there and a step below. As run: P3 AS FROZEN WAS FALSE and is
checked as (D) derives it, covered at eta = w and not at eta_max; P6's
sweep reads even widths at distances 0..6, level 3 only, a
criterion/accepts() disagreement folded in at weight 100; the free
checker is planted three ways, a root child past its box, its hull a
grid step short of the next child's first window, a level-1 state's
child past its box; the corner tree's child map is read against the
strings' integers.
USAGE: python hulls.py [K [bmax]], K = 40 and bmax = 12 bare; the
record runs are python hulls.py 1000 40 (H1) and 200 40 (H3). The
counts each printed are in H1_RECORD, H3_RECORD and P6_RECORD, read by
the checks' conditions: a run at other arguments fails them. Exit 1 if
any check fails.
FINDINGS (entered after the runs: H1's numbers are the K = 1000 run's
and H3's the K = 200 run's, radices 3..40; the counts are read by
checks, the eta* values, the gaps and the family's rows are prints).
- The controls read as by hand: at (4, 2, 2) the narrowed state holds
F = 1 + d (3334/3000) in one pass; at eta = d - 1/G the root is not
covered; the checker passes the certificate at eta_max with 0
faults and finds 2 once one child is moved past its box. CC's 17
gaining cells are all certified, 0 faults.
- THE KILL MISSED. eta* < 1 + d at 440 of the 441 cells (slack 1,
product-fed, representable, Mh < 1, radices 3..40); level only at
(10, 6, 4), where a-^2 = a+ (b - 1) puts the range's top on the box
(M+ - hi = 0), which forces 1 + d where images are dense (THE TOP
CORNER). The gap, in units
of S/(b - 2): at least 0.6 at 438 cells, 0.435 at (4, 2, 2), 0.544 at
(5, 2, 3), up to 0.947, tending up with b but not monotone in it.
eta* is the grid greedy's least, an upper bound on the tree's.
- A GAIN PAST (7): (20, 8, 12) and its mirror, at (L1*, L2*) - (0, 1)
= (2, 1): eta* 19018/19000 <= eta_max = 1085/1083 < 1 + d =
19064/19000. x y -> z·U reads at 3 against the naive 4, which the
threshold-miss law and the inset law both call dead. The
certificate at eta_max is exact (0 faults, as at every one of the
19 live cells; eta* itself is not run through the checker); the
K = 200 run finds the same
pair, eta* 3804/3800, and nothing else. H2, run bare, prints both
live at eta_max 1085/1083 with 0 checker faults, (7) reading 4.
- The budget (chains.py's (3)) is not the law of the tree: it is the
drift-free corner x = 0 of a producer whose strings each cover
their own fibre.
- H3 (K = 200, radices 3..40): 6203 cells of slack >= 2;
L2* = 2 at 38, the family exactly; max w (1 - Mh^2) off it
6765/8788 = 0.7698. The family: eta_max 1, room 0, the floor minus eta_max -1/48 at radix 4,
-1/3600 at 10, -1/1185600 at 40. The capped root covers 38 of 38
at eta = w and 0 at eta_max; the free root lives at the capped
root's 19 slack-1 cells and at no family cell, its eta* above 1 at
38 of 38, at 1 + d on the grid (1.44500 against 1.44444 at radix 3,
1.00141 against 1.00131 at 40). The eta = 1 interior holds a window
at 0 of 38 radices; the corner tree is dead at eta_max at all 38
and a step below the free eta* at all 38 (monotonicity in the cap
unproved). P6: 567, 847, 1183 hulls at radices 3, 4, 5, 0 accepted
and killed; 86 rejected at each, 16, 38, 48 of them killed at level
3. So the tree certifies nothing below 1 + d at the family, and the
family is open between (E) and the tree.
- THE GENERIC MULTIPLIER (radices 3..40, every representable cell,
rho >= 1, q = 10007; these prints do not read K): the controls read
as by hand (0 cells miss at L2 = 1 where L2* = 1, 0 with the pin at
tau = 1, L2 = 0). R1 holds: over 26728 (cell, head, L2) the sweep at
the generic u misses at 7199, exactly where l(u) < 1 - 1/q (0
differ), l(u) in the band [1 - 1/q, 1) at 0, so the sweep reads the
proof's direction and the trivial converse l >= 1; behind a product
at L2 = 0 it misses at 6682 of 6682 cells, the death at Mh = 1
included. Broken in a scratch copy (the window one unit
longer), the sweep misses nowhere and both R1 checks fail; one unit
shorter, C5 fails (132 and 65 cells miss), R1 and R2 with it
(radices 3..12). R1 checks (G)'s arithmetic; the deaths by (G')
rest on its proof.
- R2 HOLDS: behind a sum at slack >= 2, L2* = 2 only at (3, 2, 2);
L2* = 1 with w (1 - tau) < 1 at 37 cells, >= 1 at 6165, and the pin
AT tau fits every N at all 37, which the generic u kills. By hand
they are the family (b, 2, b - 1), b = 4..40: w (1 - 2 Mh/b) < 1 at
rho >= 2 needs Mh = 1 and rho (b - 2) < 2 (b - 1), so rho = 2; there
tau = 2/b, q = b or b/2, and l = 1 - 2/(b (b - 1)) >= 1 - 2/b >=
1 - 1/q. lattice.py reads the 6165.
RUN RECORD: K = 40, radices 3..6, 0.4 s, 16.5 MB (the rehearsal);
K = 200, radices 3..40, 18.2 s, 33.9 MB; K = 1000, 97.1 s, 112.4 MB
(with the eta* side counts printed, 97.2 s, 112.1 MB: below 1 + d at
440, level at (10, 6, 4), live at 19);
again with eta_max printed, 97.4 s, 112.4 MB, every verdict the same;
with the gap and certificate totals printed (438 of 441, 17 of 17),
97.4 s, 112.9 MB; all under a memory guard. Bare, with H2: 5/5
checks, 0.3 s, 22.0 MB;
with H1's live faults checked, 6/6, 0.3 s, 22.9 MB. With H3: bare
20/20, 3.1 s; K = 200, radices 3..40, 20/20, 42.0 s, 35.5 MB, under a
memory guard. With the generic multiplier: bare 24/24, 3.8 s; K =
200, radices 3..40, 24/24, 123.8 s, 60.0 MB, under a memory guard.
With P8 (the free eta* within a grid step above 1 + d, dead a step
below): bare 26/26, 3.6 s; K = 200, radices 3..40, 38 and 38 of 38,
26/26, 102.4 s. With the counts read by the checks, the free checker's
three plants and the child map's check: bare 26/26, 3.7 s, 32.8 MB;
K = 200, radices 3..40, 26/26, 115.3 s, 58.8 MB; K = 1000, 26/26,
266.9 s, 116.3 MB, with H3 at K = 1000 too, both under a memory guard
(the plants' fault counts pinned after, on these runs' prints). As
the file stands: bare 26/26, 3.9 s, 34.0 MB; K = 200, radices 3..40,
26/26, 106.7 s,
58.1 MB; K = 1000, 26/26, 260.4 s, 116.1 MB.
"""
import os
os.environ.setdefault("OPENBLAS_NUM_THREADS", "1")
import math # noqa: E402
import sys # noqa: E402
import time # noqa: E402
from fractions import Fraction as Fr # noqa: E402
import numpy as np # noqa: E402
import chains # noqa: E402
import manystream as ms # noqa: E402
CHECKS = []
# What the recorded runs printed, read by the checks' conditions. A run
# at arguments with no row fails its pinned checks by design.
H1_RECORD = { # (K, bmax): cells, eta* below 1 + d, level at, live,
# gaps at least 0.6, law gains, live past (7)
(40, 12): (30, 29, [(10, 6, 4)], 5, 13, 5, []),
(200, 40): (441, 440, [(10, 6, 4)], 19, 438, 17,
[(20, 8, 12), (20, 12, 8)]),
(1000, 40): (441, 440, [(10, 6, 4)], 19, 438, 17,
[(20, 8, 12), (20, 12, 8)]),
}
H3_RECORD = { # bmax: slack >= 2 cells, max w(1 - Mh^2) off the family,
# (cell, head, L2), the sweep's misses, product-fed cells,
# sum-fed with w(1 - tau) < 1, with w(1 - tau) >= 1
12: (136, Fr(96, 125), 704, 226, 176, 9, 126),
40: (6203, Fr(6765, 8788), 26728, 7199, 6682, 37, 6165),
}
P6_RECORD = {3: (567, 86, 16), 4: (847, 86, 38), 5: (1183, 86, 48)}
def check(name, ok, detail=""):
CHECKS.append(bool(ok))
tail = f" ({detail})" if detail else ""
print(f" {'PASS' if ok else 'FAIL'} {name}{tail}")
class Grid:
"""A slack-1 product-fed cell at (L1, L2) on offsets of step 1/G."""
def __init__(self, cell, L1, L2, K):
b = cell.b
self.cell, self.b, self.G = cell, b, (b - 1) * K
G = self.G
self.Mm, self.Mp = cell.am * K, cell.ap * K
self.N = self.Mm + self.Mp + 1
Mh, w = cell.Mh, cell.w
tau = Mh ** 2
self.d_fr = 2 * Mh * w / Fr(b) ** L1
self.eta_fr = (Fr(b) ** L2 * (w - 1) - tau * w) / Mh
self.d = math.ceil(self.d_fr * G)
self.eta_max = math.floor(self.eta_fr * G)
lo, hi = -cell.Mm * cell.Mp, Mh ** 2
self.lo, self.hi = math.floor(lo * G), math.ceil(hi * G)
self.o = np.arange(-self.Mm, self.Mp + 1, dtype=np.int64)
def greedy(self, F, start, record=False):
"""The furthest cover end from each start, children e in D in
increasing order, each the state of largest top among offsets
<= the limit. Vectorized over the starts."""
top = self.o + F
PM = np.maximum.accumulate(top)
if record:
idx = np.where(top == PM, np.arange(self.N), 0)
PA = np.maximum.accumulate(idx)
me = start + self.d
picks = []
for e in range(-self.cell.am, self.cell.ap + 1):
j = me - self.d - e * self.G + self.Mm
ok = j >= 0
jj = np.clip(j, 0, self.N - 1)
cand = e * self.G + PM[jj]
take = ok & (cand > me)
if record:
picks.append((e, np.where(take, PA[jj], -1)))
me = np.where(take, cand, me)
return (me, picks) if record else me
def fixpoint(self, eta):
F = np.minimum(eta, self.Mp - self.o)
it = 0
while True:
it += 1
me = self.greedy(F, self.b * self.o)
new = np.minimum(F, (me - self.b * self.o) // self.b)
if np.array_equal(new, F):
return F, it
F = new
def live(self, eta):
F, it = self.fixpoint(eta)
if self.b * (self.hi - self.lo) <= self.d:
return False, F, it
me = self.greedy(F, np.array([self.b * self.lo]))
return bool(me[0] >= self.b * self.hi), F, it
def certificate(self, F):
"""Every state with its recorded children, and the root's."""
starts = np.concatenate([self.b * self.o, [self.b * self.lo]])
Fx = F
me, picks = self.greedy(Fx, starts, record=True)
cert = []
for s in range(len(starts)):
kids = [(e, int(p[s])) for e, p in picks if p[s] >= 0]
cert.append(kids)
return cert
def check(self, F, cert, eta, plant=None):
"""Exact, pure-integer: each state's interval (and the root's)
is covered by its recorded children (a state with b F <= d holds
no window and needs none), each a state of the set,
inside its box, F <= eta, every window of width d in one."""
b, G, d = self.b, self.G, self.d
Fl = [int(x) for x in F]
bad = sum(x > eta for x in Fl)
for s, kids in enumerate(cert):
if s < self.N:
o0 = s - self.Mm
lo, hi = b * o0, b * (o0 + Fl[s])
else:
lo, hi = b * self.lo, b * self.hi
bad += hi - lo <= d
reach, last_e = lo + d, None
for e, i in kids:
oe = i - self.Mm
Fe = Fl[i]
if plant is not None and (s, e) == plant:
oe += G
if not (-self.cell.am <= e <= self.cell.ap):
bad += 1
if last_e is not None and e <= last_e:
bad += 1
if not (-self.Mm <= oe and oe + Fe <= self.Mp):
bad += 1
st = e * G + oe
if st > reach - d:
bad += 1
reach = max(reach, st + Fe)
last_e = e
if reach < hi:
bad += 1
return bad
def eta_star(self, hi):
"""The least eta (grid) whose fixpoint covers the root."""
lo = self.d
if not self.live(hi)[0]:
return None
while hi - lo > 1:
mid = (lo + hi) // 2
if self.live(mid)[0]:
hi = mid
else:
lo = mid
return hi
def cells1(bmin, bmax):
for b in range(bmin, bmax + 1):
for am in range(1, b):
cell = ms.Cell(b, am, b - am)
if chains.representable(cell) and cell.Mh < 1:
yield cell
def section_control(K):
print("H0 controls")
hand = ms.Cell(4, 2, 2)
L1 = chains.floor_xy(hand)
g = Grid(hand, L1, 1, K)
narrow = g.G + g.d
F, it = g.fixpoint(narrow)
check("(4,2,2): at eta = 1 + d the state o = -M- has F = 1 + d",
F[0] == narrow, f"L1 {L1}, d {g.d}/{g.G}, F {F[0]}, {it} passes")
check("at eta = d - 1/G the root is not covered",
not g.live(g.d - 1)[0])
lv, F, _ = g.live(g.eta_max)
cert = g.certificate(F)
bad = g.check(F, cert, g.eta_max)
s = next(s for s, k in enumerate(cert) if len(k) >= 2 and F[s] >= g.d)
planted = g.check(F, cert, g.eta_max, plant=(s, cert[s][0][0]))
check("(4,2,2) live at eta_max, 0 checker faults, two (its box, its "
"start) once one child is moved past its box",
lv and bad == 0 and planted == 2,
f"faults {bad}, planted {planted}")
g.hi = g.lo + g.d // g.b
root = [] if not lv else cert[-1]
check("a root narrower than a window gets no verdict (live, free, "
"the checker)", not g.live(g.eta_max)[0]
and not free_live(g, g.eta_max)[0]
and g.check(F, cert[:-1] + [root], g.eta_max) > 0,
f"b (hi - lo) {g.b * (g.hi - g.lo)}, d {g.d}")
def section_tree(K=200):
"""H2: the gain past (7), at (L1*, L2* - 1), checked bare."""
print(f"H2 (20, 8, 12) and its mirror, K {K}")
for cell in (ms.Cell(20, 8, 12), ms.Cell(20, 12, 8)):
L1 = chains.floor_xy(cell)
naive, least, _, _ = chains.two_unit(cell, chains.xy_head)
g = Grid(cell, L1, 1, K)
lv, F, _ = g.live(g.eta_max)
bad = g.check(F, g.certificate(F), g.eta_max) if lv else None
check(f"({cell.b},{cell.am},{cell.ap}) at (2, 1): the tree lives "
f"at eta_max 1085/1083 where (7) reads the naive 4",
lv and bad == 0 and least == naive == 4 and L1 == 2
and g.eta_fr == Fr(1085, 1083),
f"checker faults {bad}, L1 {L1}, naive {naive}, least "
f"{least}, eta_max {g.eta_fr}")
# ---- H3: slack 2 or more; the free root and the corner ----
def free_level1(g, F, cap=None):
"""Each level-1 state's widest hull: its box and the cover its
children from F reach; capped at eta when cap is given."""
me = g.greedy(F, g.b * g.o)
F1 = np.minimum(g.Mp - g.o, (me - g.b * g.o) // g.b)
return F1 if cap is None else np.minimum(F1, cap)
def free_live(g, eta, cap_level1=False):
F, _ = g.fixpoint(eta)
F1 = free_level1(g, F, eta if cap_level1 else None)
if g.b * (g.hi - g.lo) <= g.d:
return False, F, F1
me = g.greedy(F1, np.array([g.b * g.lo]))
return bool(me[0] >= g.b * g.hi), F, F1
def cover_faults(g, lo, hi, kids, tierF, plant=None):
"""Exact: kids (e, index) cover [lo, hi], e in D increasing, each
child inside its box, every window of width d in one child. plant
"box" moves the first child past its box; "window" cuts its hull to
end d - 1 past the second child's start, a grid step short of
holding the last window before it."""
G, d, bad = g.G, g.d, 0
reach, last = lo + d, None
for k, (e, i) in enumerate(kids):
oe, Fe = i - g.Mm, int(tierF[i])
if plant == "box" and k == 0:
oe += G
st = e * G + oe
if plant == "window" and k == 0:
Fe = kids[1][0] * G + kids[1][1] - g.Mm + d - 1 - st
bad += not (-g.cell.am <= e <= g.cell.ap)
bad += last is not None and e <= last
bad += not (-g.Mm <= oe and oe + Fe <= g.Mp)
bad += st > reach - d
reach = max(reach, st + Fe)
last = e
return bad + (reach < hi)
def free_check(g, eta, F, F1, plant=None):
"""The free certificate checked: the fixpoint's states by
Grid.check, the level-1 states and the root by cover_faults. plant
"box" or "window" is cover_faults's, on the root's cover; "level1"
is its "box" on the cover of the first level-1 state holding a
window."""
bad = g.check(F, g.certificate(F)[:g.N], eta)
_, picks1 = g.greedy(F, g.b * g.o, record=True)
_, picks0 = g.greedy(F1, np.array([g.b * g.lo]), record=True)
root = [(e, int(p[0])) for e, p in picks0 if p[0] >= 0]
bad += cover_faults(g, g.b * g.lo, g.b * g.hi, root, F1,
None if plant == "level1" else plant)
bad += g.b * (g.hi - g.lo) <= g.d
below = "box" if plant == "level1" else None
for s in sorted({i for _, i in root}):
o0 = s - g.Mm
kids = [(e, int(p[s])) for e, p in picks1 if p[s] >= 0]
if g.b * int(F1[s]) > g.d:
bad += cover_faults(g, g.b * o0, g.b * (o0 + int(F1[s])),
kids, F, below)
below = None
bad += int(F1[s]) > g.Mp - o0
return bad
def least_L2(cell, tau):
L = 0
while chains.consumer_escape(cell, tau, L) > 0:
L += 1
return L
def family(b):
return ms.Cell(b, 2, b - 1)
QPRIME = 10007
def coset_misses(cell, u, L):
"""U pinned at u (either sign), z's level-n prefix N over one period
of N t mod 1, t = u / b^L: how many N have an image
[N - M-, N + M+] t in no tile [k - M-, k + M+], k an integer.
Exact integers in units of 1/(T (b - 1)), t = P/T."""
t = u / Fr(cell.b) ** L
P, T, q = t.numerator, t.denominator, cell.b - 1
N = np.arange(T, dtype=np.int64)
D = T * q
a1 = (N * q - cell.am) * P
a2 = (N * q + cell.ap) * P
lo, hi = np.minimum(a1, a2), np.maximum(a1, a2)
need = hi - cell.ap * T
room = lo + cell.am * T
return int((-((-need) // D) > room // D).sum())
def extreme(cell, head):
"""U's extreme value of largest size, signed: Mh^2 behind a product,
2 M+/b or -2 M-/b behind a sum read at lead 1."""
if head == "xy":
return cell.Mh ** 2
return 2 * cell.Mp / cell.b if cell.Mp >= cell.Mm else -2 * cell.Mm / cell.b
def generic(tau):
"""u = tau - eps on tau's side of 0, den(u) the prime QPRIME."""
s = 1 if tau > 0 else -1
return s * Fr(math.floor(abs(tau) * QPRIME) - 1, QPRIME)
def corner_cap(g, s):
"""The widest hull z.U accepts at each offset o of a string whose
box top is s level units below U's top 1 (grid units): the box,
and the corner criterion's z >= 0 and z <= 0 halves."""
G, b = g.G, g.b
Tlo = s * G + g.Mp - g.o
cap = g.Mp - g.o
m = -(-Tlo // G) - 1
cap = np.where(Tlo > 2 * G, np.minimum(cap, Tlo // np.maximum(m, 1)),
cap)
p = -(-(2 * Tlo) // ((b - 1) * G)) - 1
return np.where(p >= 2, np.minimum(cap, Tlo // np.maximum(p, 1)), cap)
def accepts(g, s, o, F):
"""The criterion read directly from its two open intervals, exact."""
G = g.G
tlo = Fr(s * G + g.Mp - o, G)
thi = tlo - Fr(F, G)
for k in range(0, int(tlo) + 2):
if tlo > k + 2 and (k + 2) * thi < (k + 1) * tlo:
return False
Mm = g.cell.Mm
for N in range(3, int(Mm * tlo) + 3):
if Mm * tlo > N - 1 and (N - 1) * thi < (N - 2) * tlo:
return False
return True
def greedy_multi(g, tables, start):
"""Grid.greedy with a table of states per child digit e."""
me = start + g.d
for e in range(-g.cell.am, g.cell.ap + 1):
top = g.o + tables[e]
PM = np.maximum.accumulate(top)
j = me - g.d - e * g.G + g.Mm
cand = e * g.G + PM[np.clip(j, 0, g.N - 1)]
me = np.where((j >= 0) & (cand > me), cand, me)
return me
def child_distance(g, s, e):
"""The box distance of child e of a string at box distance s."""
return g.b * s + g.cell.ap - e
def corner_tree(g, eta_int, Sc, off=False):
"""Interior states: the eta_int fixpoint. Corner states at box
distance s = 0..Sc: the greatest fixpoint under corner_cap,
children by distance. Level-1 states free; the root covered by
them. off: the corner capped at eta_int, the free root."""
Fint, _ = g.fixpoint(eta_int)
Fc = [np.minimum(g.Mp - g.o, eta_int) if off else
np.maximum(corner_cap(g, s), np.minimum(g.Mp - g.o, eta_int))
for s in range(Sc + 1)]
D = range(-g.cell.am, g.cell.ap + 1)
def kids(s):
return {e: Fc[child_distance(g, s, e)]
if child_distance(g, s, e) <= Sc else Fint for e in D}
changed = True
while changed:
changed = False
for s in range(Sc + 1):
me = greedy_multi(g, kids(s), g.b * g.o)
new = np.minimum(Fc[s], (me - g.b * g.o) // g.b)
if not np.array_equal(new, Fc[s]):
Fc[s], changed = new, True
F1 = {}
for e in D:
s1 = g.cell.ap - e
me = greedy_multi(g, kids(s1) if s1 <= Sc else
{x: Fint for x in D}, g.b * g.o)
F1[e] = np.minimum(g.Mp - g.o, (me - g.b * g.o) // g.b)
return (g.b * (g.hi - g.lo) > g.d and
bool(greedy_multi(g, F1, np.array([g.b * g.lo]))[0]
>= g.b * g.hi))
def corner_sweep(b, n=3, Sc=6):
"""At level n every hull the criterion accepts survives an exact
sweep of every z box; even widths, distances 0..Sc."""
c = family(b)
g = Grid(c, 2, 1, 4)
G, B = g.G, b ** n
js = range(-c.am * (B - 1) // (b - 1), B)
bad, rej, conf, tot = 0, 0, 0, 0
for s in range(Sc + 1):
cap = corner_cap(g, s)
for i, o in enumerate(g.o):
for F in range(0, int(g.Mp - o) + 1, 2):
tlo = Fr(s * G + int(g.Mp - o), G)
thi = tlo - Fr(F, G)
ulo, uhi = 1 - tlo / B, 1 - thi / B
if ulo <= 0:
continue
tot += 1
ok = F <= cap[i]
bad += 100 * (ok != accepts(g, s, int(o), F))
kill = False
for j in js:
zl, zh = Fr(j, B) - c.Mm / B, Fr(j + 1, B)
pr = [zl * ulo, zl * uhi, zh * ulo, zh * uhi]
lo_, hi_ = min(pr) * B / b, max(pr) * B / b
if math.floor(lo_ + c.Mm) + 1 < hi_:
kill = True
break
bad += ok and kill
rej += not ok
conf += (not ok) and kill
return tot, bad, rej, conf
def section_slack2(K, bmax):
"""H3: slack 2 or more, a+- <= b - 1, representable."""
print(f"H3 slack >= 2, K {K}, radices 3..{bmax}")
c = ms.Cell(20, 8, 12)
g = Grid(c, chains.floor_xy(c), 1, 200)
fl, F, F1 = free_live(g, g.eta_max)
check("C1 (20,8,12) live under both roots", g.live(g.eta_max)[0] and fl)
bad = free_check(g, g.eta_max, F, F1)
planted = [free_check(g, g.eta_max, F, F1, plant=x)
for x in ("box", "window", "level1")]
check("C2 free certificate: 0 faults; two (its box, its start) once "
"a child of the root is moved past its box, one once its hull "
"stops a grid step short of the next child's first window, two "
"once a child of a level-1 state is moved past its box",
bad == 0 and planted == [2, 1, 2],
f"faults {bad}, planted {planted}")
n_rec, worst_rec = H3_RECORD.get(bmax, (None,) * 7)[:2]
two, worst, n, form, zero = [], Fr(0), 0, 0, 0
for cell in chains.cells(3, bmax):
if cell.rho < 2 or not chains.representable(cell):
continue
tau = cell.Mh ** 2
L2 = least_L2(cell, tau)
q, m = cell.b - 1, max(cell.am, cell.ap)
form += (cell.b * cell.rho * q * q
>= m * (m + q) * (cell.am + cell.ap)) != (L2 <= 1)
n += 1
zero += L2 == 0
if L2 == 2:
two.append((cell.b, cell.am, cell.ap))
else:
worst = max(worst, cell.w * (1 - tau))
fam = [(b, 2, b - 1) for b in range(3, bmax + 1)]
print(f" slack >= 2: {n} cells; L2* = 2 at {len(two)}; max "
f"w(1 - Mh^2) elsewhere {worst} = {float(worst):.4f}")
check("P1 L2* = 2 exactly at the family (b, 2, b - 1)", two == fam)
check("P1 (A)'s closed form for L2* <= 1 agrees with "
"chains.consumer_escape", form == 0, f"{form} differ")
check(f"P1 off it w(1 - Mh^2) is at most {worst_rec} < 1 (eta_max(0) "
f"< 0), over the {n_rec} cells of slack >= 2, L2* = 0 at none",
worst < 1 and (n, worst) == (n_rec, worst_rec) and zero == 0,
f"{n} cells, max {worst}, L2* = 0 at {zero}")
ok = 0
for b in range(3, bmax + 1):
f = family(b)
d = 2 * f.Mh * f.w / Fr(b) ** 2
eta = (b * (f.w - 1) - f.Mh ** 2 * f.w) / f.Mh
room = chains.room(f, (-f.Mm * f.Mp, f.Mh ** 2))
gap = d - (f.w - 1 - d) / (b - 2) if b > 3 else None
ok += (chains.floor_xy(f) == 2 and eta == 1 and room == 0
and d == Fr(2 * (b + 1), (b - 1) * b * b)
and (gap is None or gap == Fr(-2, b * b * (b - 1) * (b - 2))))
if b in (4, 10, 40):
print(f" ({b},2,{b - 1}) d {d}, eta_max {eta}, room {room}, "
f"floor - eta_max {gap}")
check("P2 L1* = 2, eta_max = 1, room 0, d and the floor's gap as by "
"hand, every radix", ok == bmax - 2, f"{ok} of {bmax - 2}")
wide_cov, cap_cov, free_cov, above, cl, ce, cd, same, held = (0,) * 9
near = 0 # eta* within one grid step above 1 + d
start, maps = 0, 0
for b in range(3, bmax + 1):
g = Grid(family(b), 2, 1, K)
wide = g.Mm + g.Mp
wide_cov += g.live(wide)[0]
cap_cov += g.live(g.eta_max)[0]
free_cov += free_live(g, g.eta_max)[0]
es = g.G + g.d + 2
start += free_live(g, es)[0]
while free_live(g, es - 1)[0]:
es -= 1
above += es > g.eta_max
near += 0 <= Fr(es, g.G) - (1 + g.d_fr) < Fr(1, g.G)
Sc = min(b * b - 3, 40)
cl += corner_tree(g, g.eta_max, Sc)
ce += corner_tree(g, es, Sc)
cd += not corner_tree(g, es - 1, Sc)
same += sum(corner_tree(g, e, Sc, off=True) == free_live(g, e)[0]
for e in (es, es - 1))
held += int((g.fixpoint(g.eta_max)[0] > g.d // g.b).sum()) > 0
# a level-n string of integer u: its box top u + M+ against
# U's top b^n, and its child b u + e a level on
maps += all(child_distance(g, b ** n - u - 1, e)
== b ** (n + 1) - (b * u + e) - 1
for n in (1, 2) for u in range(b ** n - 4, b ** n)
for e in range(-2, b))
if b in (3, 4, 10, 40):
print(f" ({b},2,{b - 1}) free eta* {float(Fr(es, g.G)):.5f}, "
f"1 + d {float(1 + g.d_fr):.5f}")
r = bmax - 2
check("P3 the capped root covers every family cell at eta = w and "
"none at eta_max", wide_cov == r and cap_cov == 0,
f"{wide_cov}, {cap_cov}")
check("P4 the free root certifies no family cell at eta_max, its "
"eta* above 1 at every one, its search started live",
free_cov == 0 and above == r and start == r,
f"{free_cov}, {above}, {start}")
check("C4 the corner tree is live at the free eta*, and with its "
"corner off is the free root there and a step below; a child's "
"box distance is the one its string's integer gives",
ce == r and same == 2 * r and maps == r, f"{ce}, {same}, {maps}")
print(f" interior at eta 1 holding a window at {held} of {r}; corner "
f"tree live at eta_max at {cl}, dead a step below eta* at {cd}")
check("P8 the free eta* sits within a grid step above 1 + d, the "
"corner tree dead a step below, at every family cell",
near == r and cd == r,
f"{near}, {cd} of {r}")
check("P7 the corner tree certifies no family cell at eta_max",
cl == 0 and held == 0)
for b in (3, 4, 5):
tot, bad, rej, conf = corner_sweep(b)
t, r6, c6 = P6_RECORD[b]
check(f"P6 ({b},2,{b - 1}) no hull the criterion accepts is "
f"killed at level 3: of {t} hulls {r6} rejected, {c6} of "
f"them killed", bad == 0 and (tot, rej, conf) == (t, r6, c6),
f"accepted and killed {bad}; {tot}, {rej}, {conf}")
section_generic(bmax)
def section_generic(bmax):
"""H3's generic multiplier: R1 the coset arithmetic, its corollary
behind a product at L2 = 0, R2 the pin at tau behind a sum."""
print(f" the generic multiplier, q = {QPRIME}, radices 3..{bmax}")
n_rec, kills_rec, xy_rec, neg_rec, pos_rec = H3_RECORD.get(
bmax, (None,) * 7)[2:]
ctl, top1, n, bad, kills, xy0, xy0n, band = 0, 0, 0, 0, 0, 0, 0, 0
off, eps = 0, Fr(1, QPRIME)
for cell in chains.cells(3, bmax):
if not chains.representable(cell):
continue
if least_L2(cell, cell.Mh ** 2) == 1:
ctl += coset_misses(cell, generic(cell.Mh ** 2), 1) > 0
if cell.Mh == 1:
top1 += coset_misses(cell, Fr(1), 0) > 0
for head in ("xy", "sum"):
tau = extreme(cell, head)
u = generic(tau)
off += not (u * tau > 0 and u.denominator == QPRIME
and eps <= abs(tau) - abs(u) < 2 * eps)
for L in (0, 1):
t = u / Fr(cell.b) ** L
l, q1 = cell.w * (1 - abs(t)), 1 - Fr(1, t.denominator)
pred = l < q1
band += q1 <= l < 1
got = coset_misses(cell, u, L) > 0
n += 1
bad += pred != got
kills += got
if head == "xy" and L == 0:
xy0n += 1
xy0 += got
check("C5 every generic u lies 1/q to 2/q inside tau at denominator "
"q; at L2 = 1 the generic sweep hits every N wherever L2* = 1, "
"and the pin at tau = 1 every N at Mh = 1, L2 = 0 (z . 1 = z)",
off == 0 and ctl == 0 and top1 == 0,
f"{off} u off; {ctl}, {top1} cells miss")
check(f"R1 the generic sweep misses exactly where l(u) < 1 - 1/q: at "
f"{kills_rec} of {n_rec} (cell, head, L2), l(u) in [1 - 1/q, 1) "
f"at none", bad == 0 and (n, kills) == (n_rec, kills_rec)
and band == 0,
f"{bad} differ; misses {kills} of {n}, the band {band}")
check(f"R1 behind a product at L2 = 0 it misses at every one of the "
f"{xy_rec} cells", xy0 == xy0n == xy_rec, f"{xy0} of {xy0n}")
two, fit, neg, pos = [], [], 0, 0
for cell in chains.cells(3, bmax):
if cell.rho < 2 or not chains.representable(cell):
continue
tau = 2 * cell.Mh / cell.b
L = least_L2(cell, tau)
if L == 2:
two.append((cell.b, cell.am, cell.ap))
if L != 1:
continue
if cell.w * (1 - tau) >= 1:
pos += 1
continue
neg += 1
if coset_misses(cell, extreme(cell, "sum"), 0) == 0:
fit.append((cell.b, cell.am, cell.ap))
print(f" sum-fed, slack >= 2: L2* = 2 at {two}; L2* = 1 with "
f"w(1 - tau) < 1 at {neg}, >= 1 at {pos}; the pin at tau fits "
f"every N at {len(fit)}: {fit[:6]}")
check(f"R2 behind a sum at slack >= 2 L2* = 2 at (3, 2, 2) alone; "
f"the pin at tau fits every N at all {neg_rec} cells of L2* = 1 "
f"with w(1 - tau) < 1, (b, 2, b - 1) from radix 4; w(1 - tau) "
f">= 1 at {pos_rec}",
two == [(3, 2, 2)] and (neg, pos) == (neg_rec, pos_rec)
and fit == [(b, 2, b - 1) for b in range(4, bmax + 1)],
f"{two}; {neg}, {pos}; fits {len(fit)}")
def main():
K = int(sys.argv[1]) if len(sys.argv) > 1 else 40
bmax = int(sys.argv[2]) if len(sys.argv) > 2 else 12
t0 = time.time()
section_control(K)
if not all(CHECKS):
print("CONTROL FAILED; nothing below is read")
raise SystemExit(1)
section_tree()
print(f"H1 eta* against 1 + d, K {K}, radices 3..{bmax}")
rows, side, tops, recap, freediff, ffaults = [], [], [], 0, 0, 0
for cell in cells1(3, bmax):
L1 = chains.floor_xy(cell)
lam2 = cell.Mh ** 2 + cell.Mh
L2 = 1 if lam2 > 1 else 0
naive, least, _, S0 = chains.two_unit(cell, chains.xy_head)
law = least < naive
g = Grid(cell, L1, L2, K)
lv, F, it = g.live(g.eta_max) if g.eta_max >= g.d else (False, None, 0)
cap = g.G + g.d + 2 * g.G
es = g.eta_star(min(cap, g.Mm + g.Mp))
S = cell.w - 1 - g.d_fr
gap = None if es is None else (Fr(g.G + g.d - es, g.G)) / (
S / (cell.b - 2)) if cell.b > 2 else None
bad = None
if lv:
bad = g.check(F, g.certificate(F), g.eta_max)
if g.eta_max >= g.d:
fr, Ff, Ff1 = free_live(g, g.eta_max)
recap += free_live(g, g.eta_max, cap_level1=True)[0] != lv
freediff += fr != lv
ffaults += free_check(g, g.eta_max, Ff, Ff1) if fr else 0
rows.append((cell.b, cell.am, cell.ap, L1, L2, law, lv, bad, es, gap))
side.append(None if es is None else (es > g.G + g.d) - (es < g.G + g.d))
if g.hi == g.Mp:
tops.append((cell.b, cell.am, cell.ap))
tag = "GAIN" if lv and not law else ("gain" if lv else "")
print(f" ({cell.b},{cell.am},{cell.ap}) L1 {L1} L2 {L2}: law "
f"{'gain' if law else 'none'}, model {'live' if lv else 'dead'}"
f"{'' if bad is None else f' (faults {bad})'}; eta* "
f"{es}/{g.G} vs 1+d {g.G + g.d}, eta_max {g.eta_fr}, gap/(S/(b-2)) "
f"{'-' if gap is None else f'{float(gap):.3f}'} {tag}")
level = [r[:3] for r, x in zip(rows, side) if x == 0]
print(f" eta* below 1 + d at {side.count(-1)}, level at "
f"{level}, above at "
f"{side.count(1)}, none at {side.count(None)}; model live at "
f"{sum(1 for r in rows if r[6])}")
gaps = [r[9] for r in rows if r[9] is not None]
wide = sum(1 for x in gaps if x >= Fr(3, 5))
sure = sum(1 for r in rows if r[5] and r[6] and r[7] == 0)
gains = sum(1 for r in rows if r[5])
print(f" gap/(S/(b-2)) at least 0.6 at {wide} of {len(gaps)}; law "
f"gains certified with 0 faults at {sure} of {gains}")
past = [r[:3] for r in rows if r[6] and not r[5]]
lost = [r[:3] for r in rows if r[5] and not r[6]]
print(f" cells {len(rows)}; model live where (7) says none: {past}; "
f"(7) gains the model misses: {lost}")
live = [r for r in rows if r[6]]
rec = H1_RECORD.get((K, bmax), (None,) * 7)
check(f"H1 at K {K}, radices 3..{bmax}, of {rec[0]} cells eta* is "
f"below 1 + d at {rec[1]} and level at {rec[2]}, the cells "
f"where U's top is its box's; the tree lives at {rec[3]}, 0 "
f"checker faults, at each of the {rec[5]} law gains and past "
f"(7) at {rec[6]}; the gap is at least 0.6 at {rec[4]}",
(len(rows), side.count(-1), level, len(live), wide, gains, past)
== rec and level == tops and len(gaps) == len(rows)
and sure == gains and lost == []
and all(r[7] == 0 for r in live),
f"{len(rows)}, {side.count(-1)}, {level}, {len(live)}, {wide}, "
f"{gains}, {past}; tops {tops}, certified {sure}, lost {lost}")
check("H1 the free root capped at eta is Grid.live; freed it lives "
"at the same cells, 0 checker faults",
recap == 0 and freediff == 0 and ffaults == 0,
f"{recap}, {freediff}, {ffaults}")
section_slack2(K, bmax)
print(f"{sum(CHECKS)}/{len(CHECKS)} checks passed, "
f"{time.time() - t0:.1f} s")
raise SystemExit(0 if all(CHECKS) else 1)
if __name__ == "__main__":
main()