cycles.py
Checks: BONACCI: The carry bound rule.
Cited on: BONACCI.
To run it: save cycles.py, with real_support.py beside it; needs mpmath, numpy.
"""
cycles.py -- does S's window graph carry a cycle avoiding W = 0?
QUESTION. S = 1 0^(d+1) 1^(d-3) 0 1 0 low digit first, P = 2d + 2. A
pair (S^inf, y'), y' any 0/1 string, has a bounded carry x, first
window sum 0, delta = S - y' with delta_t = x_t - (x_(t+1) + ... +
x_(t+d)) in {S_t - 1, S_t}; windows W_t = (x_t, ..., x_(t+d-1)) step by
x_(t+d) = 2x_t - sum W_t - delta_t. closure.py proves y' is S^inf with
the e_1 excursion at some 0^(d+1) runs when the windows vanish
infinitely often. Is the other case, windows nonzero from some N on,
empty at d = 4..10?
BY HAND (before the engine).
(1) Splice. Windows nonzero from N_0 on: the states (W_N, N mod P) are
finitely many, so (W_a, a) = (W_b, b) mod P for some N_0 <= a < b, and
x[a, b + d) repeated with period b - a is a two-sided periodic integer
sequence, delta in {S_t - 1, S_t} at each t, every window one of W_a ..
W_(b-1): a cycle of S's window graph avoiding W = 0.
(2) Bound. A two-sided bounded x with delta bounded is x_t = sum_k
h_(k-t) delta_k, h the expansion of 1/(1 - z - ... - z^d) on the unit
circle (h_m = sum_(j>=1) c_j rho_j^(-m), m >= 0, and -c_0 beta^m, m < 0,
rho_0 = 1/beta, rho_j the inverses of the other roots, c_j =
-1/(rho_j Delta'(rho_j))): the difference of two bounded solutions is a
bounded solution of the homogeneous relation, a combination of
rho_j^t, each unbounded at one end unless 0. So |x_t| <= L(d) =
sum_m |h_m|, and x being integer every such cycle lies in [-K, K]^d, K
= floor(L(d)), and in any larger box.
(3) So no cycle avoiding 0 in that box at d makes every pair's
windows vanish past every N, and with closure.py's theorem y' is S^inf
with the e_1 excursion at some of its runs: at d, legality dropped,
S^inf's other 0/1 strings carried to it by a bounded carry, first
window sum 0, are the excursion strings.
(4) The e_1 excursion is x = e_m, the unit sequence at m = d + 2: it
has delta_m = 1 where S_m = 1, delta_t = -1 at t = 2..d + 1 where S_t =
0, and 0 elsewhere, so it is a path of S's graph; its nonzero windows
sit at phases 3..d + 2, the 1 moving from the last entry to the first,
and the window at phase d + 3 is 0 again. So is the sum of such x over
any set of m = d + 2 mod P, their deltas' supports disjoint (d + 1 <
P): each excursion string is carried to S^inf.
(5) One set reads it. Let V be the nodes (N mod P, W) on bi-infinite
paths inside the box. A cycle lies in the box by (2), so its nodes lie
in V, and a step moves the phase by one, so it meets every phase: V
holding the zero window alone at one phase leaves no cycle avoiding 0.
By (4) and the zero path (delta = 0) V holds at least the zero window
at each phase and the excursion's d nodes, 3d + 2 in all.
(6) The step, for the engine. Under a fixed delta the step W -> (W_1,
..., W_(d-1), 2W_0 - sum W - delta) and the map W -> (sum W + delta,
W_0, ..., W_(d-2)) invert each other. So the windows of the box with a
successor in a set X are the images of X's members under the second
map at the two delta of the earlier phase, where the image stays in
the box, and those with a predecessor in X the images under the first,
alike.
ARMS. A window is coded sum_i (W_i + K) B^i, B = 2K + 1, and a set of
windows at a phase is a sorted array of codes. two_sided(word, d, K)
starts every phase at the whole box and sweeps in place, the phases
descending for "has a successor alive" and ascending for "has a
predecessor alive", each by (6)'s images, until a pass removes nothing.
A node removed is off V, since V lies in what is alive and a node of V
has a successor and a predecessor in V; a pass that removes nothing
leaves a set closed both ways, each node of it on a bi-infinite path:
V. No table of the steps is kept: a sweep clears one flag a window of
the box a phase and otherwise costs what is alive. Codes are int32,
the largest value formed a code times B, below B^(d+1), which fits(d,
K) asks to be under 2^31. read(d) is the record's entry at d: the
floors of the two ends of the rational enclosure of L(d) that
real_support.read proves, V in the box of the upper floor, and row(V),
five counts: nodes, phases holding the zero window, nonzero nodes, the
excursion's among them, phases holding the zero window alone. The
law's row is (3d + 2, 2d + 2, d, d, d + 2).
CONTROLS, read first; a failure stops the run. Hand rows: the codes of
three windows at d = 4 and of the zero one, their sums, successors and
predecessors at each delta at K = 1 and K = 2, an entry of size K kept
at each, K + 1 dropped on either side at K = 1 and, by the step after,
below at K = 2; meet on a hand case; the excursion's four nodes at d =
4; the row of four hand sets, a node added, a node missing, a zero
window replaced by another node; the floors of an enclosure astride 2,
of one starting at 2, and of L(2)'s, (1, 1); which (d, K) keep a code
and its shift inside int32, the refusal past them, and a step before
at d = 12, K = 2, the largest shift allowed. Two graphs whole at d = 2,
K = 1, nine windows: the word (1), where forward the deaths run three
deep and leave six, backward four deep and leave five, and V is {(0,
0), (0, -1), (-1, 0), (-1, -1)}; the word (1, 0), whose V at phase 1 is
the negative of its V at phase 0, {(0, 0), (0, -1), (1, 0), (1, -1)}.
plain(word, d, K) is V again by its definition, on tuples, every node
still kept recomputed each round: it agrees with the engine node for
node on the two graphs, on the word (0) at K = 0, a box of one window,
and on S at d = 4, 5, 6, and on the word (0, 1) it shares with the
engine's (1, 0) the two zero nodes alone. Known cycles off S,
which a prune that removed too much would lose: Y = 1 0^(d-1) carries x
= (1, -1, 0, ..., 0) of period d (delta = x), its d windows in V at K =
1, d = 4, 6; the triangle wave at d = 9, x = (0, 1, 2, 1, 0, -1, -2,
-1) of period 8, delta_t = x_t - x_(t+1), Y = [delta = 1], its 8
windows in V at K = 2, entries of size K among them (no two-sided path
reaches 2 at d = 4 or 5, L being under 2 there); beside each a
constant x, all 1 or all 2, with no legal step and no window in V.
PREDICTIONS, CONTROLS AND KILL (the first engine's, fixed before its
code: a dense prune of the box, a table a delta).
PRINT, d = 4..10: L(d) by the roots (|m| <= 4000) and by a DFT of
1/Delta on 2^16 points, K = floor(L); the nodes left when the
nonzero-window graph in [-K, K]^d is pruned of nodes with no
successor until stable (a cycle iff any is left); the two-sided
recurrent nonzero set (zero nodes kept, pruned both ways) against
the excursion's d nodes.
KILL: some d in 4..10 leaving a node in the pruned nonzero graph.
CONTROLS, read first:
C1 the prune finds a known never-closing cycle: the word Y = 1
0^(d-1) (period d), w = (1, -1, 0, ..., 0), y' = 0 1 0^(d-2),
x_t = w_(1 + t mod d): at d = 4, 6, K = 1, the pruned set holds
the d shifts of w at their phases. At K = 2 (added when the
script shipped): the triangle wave at d = 9, x = (0, 1, 2, 1, 0,
-1, -2, -1) of period 8, delta_t = x_t - x_(t+1) (its period
sums to 0), Y = [delta = 1] = (0 0 1 1 1 1 0 0): the pruned set
in [-2, 2]^9 holds x's 8 windows at their phases.
C2 the two L's agree to 1e-9 at every d, and K's floor has margin:
L - floor(L) and floor(L) + 1 - L each >= 1e-6.
ENVELOPE: d = 10 at K = 2, 5^10 windows x 22 phases, under a 2 GB
cap, estimated under 2 min (it took 276 s, peak 866 MB: the bare
run stopped at d = 9, d = 10 named by a flag, --ds).
CHANGED FROM THE FROZEN (named). The Graph kept the first coordinate
and the window sum (int32) in place of a full digit table, and the
step tables in int32, for memory; the DFT comment was corrected
before the run (index j holds h_m summed over m = j mod n).
PREDICTIONS AND KILL (the image engine's, fixed before it with (5), (6)
and every hand row; the counts at d = 4..9 had been seen in the first
engine's print, and d = 10's in its record).
X0 CONTROLS, read first, a failure stopping the run: the hand rows
of the codes and steps, the floors, the int32 bound and the row;
the two graphs at d = 2, each set whole; an evaluator written
afresh on tuples (a filter, not images; every node recomputed a
round) agreeing with the engine node for node on S at d = 4, 5 (K
= 1) and d = 6 (K = 2) and on the two graphs; the known cycles in
V(Y): "4 of 4", "6 of 6", "8 of 8".
X1 both floors of L's enclosure 1, 1, 2, 2, 2, 2, 2 at d = 4..10.
X2 the law's row at each d = 4..10: 14, 17, 20, 23, 26, 29, 32 nodes.
KILL: "TWO-SIDED: n of 7 d hold the zero path and the excursion
alone" at n < 7.
ENVELOPE: the bare run, d = 10 in it, estimated under 60 s and under
400 MB, from a probe's 1.69 s at d = 9 and a factor near 7 a d.
CHANGED FROM THE FROZEN (named). The kill's print says nodes, "n of 7
d hold the zero window and the excursion's nodes alone": V carries
every path through them, an excursion at any set of runs. Written with
the draft and not frozen: meet's hand case, the floors of an enclosure
starting at 2, the word (0, 1) against (1, 0), and the count of the
check lines; the cycles print as pairs, (legal steps, windows in V),
not "n of n". Added after
the first run, as its breaks were set down: the count of the nodes
each sweep starts from, pinned, since a box held to K = 1 would pass
every other line. Added at the draft's read: the hand step at d = 12.
Added at the first meter, each for a survivor: the constant x beside
each known cycle, the word (0) at K = 0, and the fourth hand set's
node in place of its zero window; the passes' count and V(Y)'s size,
prints no check read, were cut.
FINDINGS (copied from the runs' prints).
- The controls hold, each hand value as written: each known cycle
prints (d, d) or (8, 8), every step legal and every window in V,
its constant (0, 0), and the definition on tuples agrees with the
engine node for node on all six graphs. The refusal is read by
calling the refused size, which a broken guard would allocate.
- The box: both floors of L's enclosure are 1, 1, 2, 2, 2, 2, 2 at d
= 4..10, so the box read is [-floor L, floor L]^d.
- The KILL misses: at every d = 4..10 V is the zero window at each of
the 2d + 2 phases and the excursion's d nodes, 14, 17, 20, 23, 26,
29, 32 nodes, d + 2 phases holding the zero window alone, so by (5)
no cycle avoids W = 0. With (1)-(3) and closure.py's theorem: at d
= 4..10, legality dropped, S^inf's other 0/1 strings carried to it
by a bounded carry, first window sum 0, are exactly S^inf with the
e_1 carry at some of its 0^(d+1) runs, by a search in integers
inside a box a rational enclosure proves.
- d = 10's entry, from 214,843,750 nodes at the start, is read in
2.2 s, the enclosure's certificate in it.
- What the law's rows do not read, by breaks each run with the
controls' stop lifted: with the doubled entry dropped from the
step they hold at every d, and with the box's edge moved in by one
they hold at d = 6..10, where K = 2. The hand rows, the two graphs
and the wave read those. The box taken from the lower floor moves
no line, the floors agreeing.
- The mutation meter's two changes that move no line are equivalent:
fits with <= for <, an odd power never being 2^31; and the
descending sweep run one step past phase 0, which sweeps the last
phase a second time. Its four that never end cut or negate a stop
test, in the sweeps or in the definition on tuples.
- The first engine pruned the nonzero windows forward to 0 nodes at
each d = 4..10, which (5) reads off V, and its dense prune both
ways printed the same nonzero nodes at d = 4..9 and recorded them
at d = 10: a second engine at the d the definition on tuples
does not reach. Its floats put L at 1.848, 1.987, 2.138, 2.300,
2.471, 2.652, 2.841, by roots and by DFT to 3.5e-14 .. 1.7e-13,
the least floor margin 0.0135 (d = 5, below 2); its C1 left 41 and
99 nodes on Y's graph at d = 4, 6.
RUN RECORD: the first engine, python cycles.py then d = 4..10 (under a
2 GB cap) 16/16 (before C1's K = 2 line), peak commit 865.9 MB, wall
319.2 s, d = 10 alone
276.4 s against an estimate of under 2 min (past twice it; it finished,
the growth from d = 9's 36.8 s a factor 7.5 per d); --ds 4,5,6 8/8 and
--ds 7,8 6/6 first. Shipped bare (controls, d = 4..9) through a 512 MB
guard: 15/15, 46.2 s, 176.6 MB; C1's K = 2 plant left 660 nodes, the
wave's 8 among them.
The image engine: the bare run, controls and d = 4..10, 30/30 checks,
4.2 s, peak commit 263.2 MB under a 512 MB cap; the draft's first run
29/29, 3.7 s, before the starting nodes were pinned. The mutation
meter, every one-token and structural change of its functions, 150,
each run: 144 red, 4 never ending, 2 moving no line. 27 breaks by
hand, each one edit, each run twice, the second with the controls'
stop lifted: 26 red, the lower floor's green.
USAGE. python cycles.py reads the controls and d = 4..10.
"""
import itertools
import math
import os
import sys
import time
from fractions import Fraction
os.environ.setdefault("OPENBLAS_NUM_THREADS", "1")
import numpy as np # noqa: E402
from real_support import S_WORD, read as support # noqa: E402
DS = range(4, 11)
RECORD = {
"floors": [(1, 1), (1, 1), (2, 2), (2, 2), (2, 2), (2, 2), (2, 2)],
"start": [810, 2916, 218750, 1250000, 7031250, 39062500, 214843750],
}
LINES = 30
CHECKS = []
def fits(d, K):
"""Whether a code and its shift by one place stay inside int32."""
return (2 * K + 1) ** (d + 1) < 1 << 31
def code(W, K):
B = 2 * K + 1
return sum((w + K) * B ** i for i, w in enumerate(W))
def window(c, d, K):
B = 2 * K + 1
return tuple(int(c) // B ** i % B - K for i in range(d))
def total(c, d, K):
"""The window sums of an array of codes."""
B = 2 * K + 1
s = np.zeros(len(c), np.int32)
q = c.copy()
for _ in range(d):
s += q % B - K
q //= B
return s
def after(c, dl, d, K):
"""The codes of the windows one step after those of c under delta =
dl, where the new entry stays in the box."""
B = 2 * K + 1
new = 2 * (c % B - K) - total(c, d, K) - dl
keep = np.abs(new) <= K
return c[keep] // B + (new[keep] + K) * B ** (d - 1)
def before(c, dl, d, K):
"""The codes of the windows one step before those of c under delta =
dl, where the old entry stays in the box."""
B = 2 * K + 1
old = total(c, d, K) + dl
keep = np.abs(old) <= K
return c[keep] * B % B ** d + (old[keep] + K)
def meet(own, images, n):
"""The members of own that occur in one of the images, codes below
n; own's order is kept."""
mark = np.zeros(n, dtype=bool)
for im in images:
mark[im] = True
return own[mark[own]]
def two_sided(word, d, K):
"""V by phase, sorted codes, and the nodes the first pass started
from, counted off its arrays: the windows of [-K, K]^d on
bi-infinite paths of the word's graph, the step from phase p taking
delta in {word[p] - 1, word[p]}."""
if not fits(d, K):
raise OverflowError(f"d = {d}, K = {K}: a code leaves int32")
P, n = len(word), (2 * K + 1) ** d
box = np.arange(n, dtype=np.int32)
alive = [box] * P
start = sum(len(a) for a in alive)
while True:
size = sum(len(a) for a in alive)
for p in range(P - 1, -1, -1):
nxt = alive[(p + 1) % P]
alive[p] = meet(alive[p], [before(nxt, e, d, K)
for e in (word[p] - 1, word[p])], n)
for p in range(P):
r = (p - 1) % P
alive[p] = meet(alive[p], [after(alive[r], e, d, K)
for e in (word[r] - 1, word[r])], n)
if sum(len(a) for a in alive) == size:
return alive, start
def plain(word, d, K):
"""V by its definition, on tuples: every node of the box, kept while
one of its two successors and one of its two predecessors is kept,
every node still kept recomputed each round."""
P = len(word)
live = {(p, W) for p in range(P)
for W in itertools.product(range(-K, K + 1), repeat=d)}
while True:
keep = set()
for p, W in live:
r = (p - 1) % P
fwd = any(((p + 1) % P, W[1:] + (2 * W[0] - sum(W) - e,)) in live
for e in (word[p] - 1, word[p]))
back = any((r, (sum(W) + e,) + W[:-1]) in live
for e in (word[r] - 1, word[r]))
if fwd and back:
keep.add((p, W))
if keep == live:
return live
live = keep
def nodes(alive, d, K):
return {(p, window(c, d, K)) for p, a in enumerate(alive) for c in a}
def pack(some, P, K):
"""A set of nodes as sorted codes by phase."""
return [np.array(sorted(code(W, K) for p, W in some if p == q),
dtype=np.int32) for q in range(P)]
def excursion(d):
"""The e_1 excursion's nonzero windows: x = e_m, m = d + 2."""
m = d + 2
return {(t, tuple(int(t + i == m) for i in range(d)))
for t in range(m - d + 1, m + 1)}
def row(alive, d, K):
"""Of a set by phase on S's 2d + 2 phases: (nodes, phases holding
the zero window, nonzero nodes, the excursion's nodes among them,
phases holding the zero window alone)."""
zero = code((0,) * d, K)
held = [bool((a == zero).any()) for a in alive]
hits = sum(bool((alive[p] == code(W, K)).any()) for p, W in excursion(d))
count = sum(len(a) for a in alive)
alone = sum(h and len(a) == 1 for h, a in zip(held, alive))
return count, sum(held), count - sum(held), hits, alone
def law(d):
return 3 * d + 2, 2 * d + 2, d, d, d + 2
def deltas(x, d):
"""delta over one period of a periodic x."""
n = len(x)
return [x[t] - sum(x[(t + i) % n] for i in range(1, d + 1))
for t in range(n)]
def cycle_hits(x, word, d, K):
"""For a periodic x of the word's own period: how many of its steps
take a delta the word allows, and how many of its windows lie in
V(word) at their phases."""
P = len(word)
legal = sum(e in (word[t] - 1, word[t])
for t, e in enumerate(deltas(x, d)))
alive, _ = two_sided(word, d, K)
hits = sum(bool((alive[t] == code([x[(t + i) % P] for i in range(d)],
K)).any()) for t in range(P))
return legal, hits
def floors(lo, hi):
return math.floor(lo), math.floor(hi)
def bound(d):
"""The floors of the two ends of the proved enclosure of L(d); the
second is a K with every two-sided path inside [-K, K]^d."""
proved = support(d)
return None if proved is None else floors(*proved["L"])
def read(d):
"""The record's entry at d."""
both = bound(d)
if both is None:
return {"floors": None, "row": None, "start": None}
K = both[1]
alive, start = two_sided(S_WORD(d), d, K)
return {"floors": both, "row": row(alive, d, K), "start": start}
def check(name, ok, detail=""):
CHECKS.append(bool(ok))
print(f" [{'PASS' if ok else 'FAIL'}] {name}"
+ (f" {detail}" if detail else ""), flush=True)
def section_hand():
print("CONTROLS", flush=True)
A, C, E = (1, 0, -1, 1), (-1, 0, 1, 1), (-1, 0, 0, 0)
got = [code(A, 2), code(C, 2), code(A, 1), code(E, 1), code((0,) * 4, 1)]
back = [window(413, 4, 2), window(461, 4, 2), window(59, 4, 1),
window(39, 4, 1)]
check("codes: (1, 0, -1, 1) and (-1, 0, 1, 1) at K = 2, (1, 0, -1, 1), "
"(-1, 0, 0, 0) and the zero window at K = 1, and back",
got == [413, 461, 59, 39, 40] and back == [A, C, A, E], f"{got}")
two = np.array([413, 461], np.int32)
one = np.array([59, 39, 40], np.int32)
sums = [total(two, 4, 2).tolist(), total(one, 4, 1).tolist()]
check("their sums", sums == [[1, 1], [1, -1, 0]], f"{sums}")
one = one[:2]
fwd = [after(two, e, 4, 2).tolist() for e in (-1, 0, 1)]
check("K = 2, the steps after at delta -1, 0, 1: the second window "
"keeps the new entry -2 and drops -3, -4",
fwd == [[582, 92], [457], [332]], f"{fwd}")
fwd = [after(one, e, 4, 1).tolist() for e in (-1, 0, 1)]
check("K = 1, the steps after: the first drops the new entry 2, the "
"second -2", fwd == [[40], [73, 13], [46]], f"{fwd}")
bwd = [before(two, e, 4, 2).tolist() for e in (-1, 0, 1)]
check("K = 2, the steps before at delta -1, 0, 1: the old entry 2 kept",
bwd == [[192, 432], [193, 433], [194, 434]], f"{bwd}")
bwd = [before(one, e, 4, 1).tolist() for e in (-1, 0, 1)]
check("K = 1, the steps before: the second drops the old entry -2, "
"the first 2", bwd == [[16], [17, 36], [37]], f"{bwd}")
got = meet(np.array([2, 5, 7, 9]), [np.array([5, 5, 1]),
np.array([9, 3])], 10).tolist()
check("meet: 2, 5, 7, 9 among (5, 5, 1) and (9, 3)", got == [5, 9],
f"{got}")
got = [fits(12, 2), fits(13, 2), fits(18, 1), fits(19, 1)]
refused = None
if got == [True, False, True, False]:
try:
two_sided([1], 13, 2)
refused = False
except OverflowError:
refused = True
except MemoryError:
refused = False
top = before(np.array([244125000], np.int32), 0, 12, 2).tolist()
check("int32: 5^13 and 3^19 fit, 5^14 and 3^20 do not, d = 13 at K = "
"2 is refused, and at d = 12 the step before (-2 six times, 2 "
"six times) at delta 0 is (0, -2 six times, 2 five times)",
refused is True and top == [244062502],
f"{got}, {refused}, {top}")
F = Fraction
got = [floors(F(19, 10), F(21, 10)), floors(F(2), F(5, 2)), bound(2)]
check("floors: of an enclosure astride 2, of one starting at 2, and "
"of L(2)'s, L(2) = 1 + 2/sqrt 5",
got == [(1, 2), (2, 2), (1, 1)], f"{got}")
ex = excursion(4)
check("the excursion at d = 4: phases 3..6, the 1 last to first",
ex == {(3, (0, 0, 0, 1)), (4, (0, 0, 1, 0)), (5, (0, 1, 0, 0)),
(6, (1, 0, 0, 0))}, f"{sorted(ex)}")
zero = {(p, (0, 0, 0, 0)) for p in range(10)}
sets = [zero | ex, zero | ex | {(0, (1, 0, 0, 0))},
zero | ex - {(3, (0, 0, 0, 1))},
(zero | ex) - {(7, (0, 0, 0, 0))} | {(7, (1, 0, 0, 0))}]
rows = [row(pack(s, 10, 1), 4, 1) for s in sets]
check("rows at d = 4: the law's set; a node added at phase 0; the "
"excursion's first missing; another node for the zero window at "
"phase 7",
rows == [law(4), (15, 10, 5, 4, 5), (13, 10, 3, 3, 7),
(14, 9, 5, 4, 5)] and law(4) == (14, 10, 4, 4, 6),
f"{rows}")
v1, s1 = two_sided([1], 2, 1)
v1 = nodes(v1, 2, 1)
check("d = 2, K = 1, the word (1): V is (0, 0), (0, -1), (-1, 0), "
"(-1, -1), read from all nine windows",
v1 == {(0, (0, 0)), (0, (0, -1)), (0, (-1, 0)), (0, (-1, -1))}
and s1 == 9, f"{sorted(v1)}, from {s1} nodes")
v2 = nodes(two_sided([1, 0], 2, 1)[0], 2, 1)
half = {(0, 0), (0, -1), (1, 0), (1, -1)}
want = {(0, W) for W in half} | {(1, (-W[0], -W[1])) for W in half}
check("d = 2, K = 1, the word (1, 0): V at phase 0 is (0, 0), (0, -1), "
"(1, 0), (1, -1), at phase 1 its negative", v2 == want,
f"{sorted(v2)}")
cases = [([1], 2, 1, 4), ([1, 0], 2, 1, 8), ([0], 2, 0, 1),
(S_WORD(4), 4, 1, 14), (S_WORD(5), 5, 1, 17),
(S_WORD(6), 6, 2, 20)]
got = []
for word, d, K, size in cases:
mine, afresh = nodes(two_sided(word, d, K)[0], d, K), plain(word, d, K)
got.append((len(mine & afresh), len(mine), len(afresh)) == (size,) * 3)
check("the definition on tuples agrees with the engine node for node: "
"the two graphs, the word (0) in the box of one window, S at d "
"= 4, 5, 6", got == [True] * 6, f"{sum(got)} of 6")
other = plain([0, 1], 2, 1)
check("the definition on the word (0, 1) shares the two zero nodes "
"alone with the engine's (1, 0)",
v2 & other == {(0, (0, 0)), (1, (0, 0))} and len(other) == 8,
f"{sorted(v2 & other)} of {len(other)}")
wave = [0, 1, 2, 1, 0, -1, -2, -1]
got = [deltas([1, -1, 0, 0], 4), deltas(wave, 9)]
check("delta of (1, -1, 0, 0) at d = 4 and of the wave at d = 9",
got == [[1, -1, 0, 0], [-1, -1, 1, 1, 1, 1, -1, -1]], f"{got}")
for d in (4, 6):
Y = [1] + [0] * (d - 1)
got = [cycle_hits([1, -1] + [0] * (d - 2), Y, d, 1),
cycle_hits([1] * d, Y, d, 1)]
check(f"d = {d}, K = 1, Y = 1 0^(d-1): the cycle (1, -1, 0, ..., "
"0) is a path of Y's graph, its d windows in V; the "
"constant 1 is neither", got == [(d, d), (0, 0)], f"{got}")
Y = [0, 0, 1, 1, 1, 1, 0, 0]
got = [cycle_hits(wave, Y, 9, 2), cycle_hits([2] * 8, Y, 9, 2)]
check("d = 9, K = 2: the triangle wave is a path of its word's graph, "
"its 8 windows in V; the constant 2 is neither",
got == [(8, 8), (0, 0)], f"{got}")
def section_box(rows):
print("THE BOX d = 4..10: the floors of the enclosure of L", flush=True)
got = [rows[d]["floors"] for d in DS]
check("both floors of L's enclosure at each d, the box the second",
got == RECORD["floors"], f"{got}")
def section_law(rows):
print("THE LAW d = 4..10: V in the box", flush=True)
for d in DS:
r = rows[d]
print(f" d = {d}: {r['start']} nodes at the start, row "
f"{r['row']}, {r['wall']:.1f} s", flush=True)
check(f"d = {d}: the zero window at every phase and the "
"excursion's d nodes, no other", r["row"] == law(d),
f"{r['row']}")
got = [rows[d]["start"] for d in DS]
check("each sweep started from the whole box at every phase: (2d + "
"2) 3^d nodes at d = 4, 5, (2d + 2) 5^d at d = 6..10",
got == RECORD["start"], f"{got}")
held = sum(rows[d]["row"] == law(d) for d in DS)
print(f"TWO-SIDED: {held} of {len(DS)} d hold the zero window and "
"the excursion's nodes alone", flush=True)
check("no cycle avoiding W = 0 at any d = 4..10: d + 2 phases hold "
"the zero window alone at each",
held == len(DS) and len(DS) == 7, f"{held} of 7")
def main():
t_all = time.time()
section_hand()
if not all(CHECKS):
print("\nCONTROL FAILED: nothing below is read.")
sys.exit(1)
rows = {}
for d in DS:
t0 = time.time()
rows[d] = read(d)
rows[d]["wall"] = time.time() - t0
section_box(rows)
section_law(rows)
ran = len(CHECKS)
check("every check line of the run printed, this one aside",
ran == LINES - 1, f"{ran} of {LINES - 1}")
print()
print("%d of %d checks, %.1f s"
% (sum(CHECKS), len(CHECKS), time.time() - t_all))
sys.exit(0 if all(CHECKS) else 1)
if __name__ == "__main__":
main()