tailwalk.py

Checks: FLATTEN: Every rank's band of failing depths ends theorem; rule; pattern; FLATTEN: Read by rank, the failures sit in a band of depths rule; rule; property; pattern.

Cited on: FLATTEN.

To run it: save tailwalk.py, with bandend.py, dualtail.py, flatten.py, prunerr.py, vartail.py beside it; needs mpmath, sympy.

"""tailwalk.py -- the dual certificate at ranks 9..14: each order's tail,
and the walk from its start down to its crossing.

QUESTION. vartail.py closes ranks 5..8: a tail per order from which the
dual bound passes the champion's height at every depth, and below each
start the exact certificate a depth at a time, down to the order's
crossing. From which depth J_c is the champion (1 + x)^(r-1) (x - 1)^J
the only least-height vector, at every depth, at ranks 9..14?

THE ARM. vartail.py's, called at six more ranks. Order 0's start is
dualtail.py's triangle tail, built here; every order s >= 1's is
vartail.py's whole-norm tail; each order is walked down from its start
(vartail.row), and J_c is the largest crossing. This file holds no
engine of its own: every function it calls has its hand cases in
vartail.py, dualtail.py or bandend.py. Their controls there stop at
rank 8, so the ones that read a built tail are run again here, on the
tails of ranks 9..14.

PREDICTIONS, frozen before this file's first run.
  PA the starts by order s = 0..r - 2: order 0's from an earlier
     form of vartail.py, which built it; at s >= 1 that form's at
     even d and a probe of its tail at V = r - 2 at odd d:
         9: 46, 51, 81, 93, 168, 175, 559, 557
        10: 47, 59, 70, 109, 120, 215, 218, 709, 716
        11: 56, 55, 82, 93, 141, 150, 267, 265, 877, 898
        12: 58, 64, 75, 108, 117, 176, 182, 324, 316, 1063, 1098
        13: 67, 61, 87, 96, 136, 143, 213, 217, 385, 370, 1267, 1321
        14: 68, 71, 81, 112, 119, 165, 171, 253, 253, 451, 428, 1489,
            1561
  PB the crossings, read by two scans from a ceiling of 500 (bandend.py
     at ranks 9 and 10, an earlier script at 11..14):
         9: 13, 21, 35, 45, 67, 73, 157, 95
        10: 14, 23, 33, 48, 56, 87, 89, 170, 97
        11: 15, 25, 35, 47, 61, 73, 103, 105, 227, 121
        12: 16, 26, 35, 49, 56, 78, 82, 127, 121, 241, 123
        13: 17, 27, 37, 47, 63, 71, 97, 105, 147, 141, 305, 147
        14: 18, 28, 37, 49, 59, 75, 86, 109, 115, 173, 159, 319, 149
     every one at or below its start, and the depths read the sum
     over orders of start - crossing + 2: 1240, 1664, 2092, 2649,
     3183, 3872.
  PC E g^2 from the polynomials is the exact c*'s at all 228 cells,
     s >= 1, J in {r + 7, 100, 400, 1000}.
  PD the condition walked down from J_t + 40 first fails at J_t - 1
     or at J_t - 2 at each of the 57 tails.
KILL (printed): a row off the record; a control off; a crossing above
its start, the exact certificate failing at a depth a tail claims.

CONTROLS, read before the walk.
  C1 rank 5 through this file's path, order 0's tail built and not
     read off a record: vartail.py's recorded row. It reads the wiring
     alone (which tail goes to which order) and nothing that turns on
     the rank. A failure stops the run before the ranks are built.
  C2 on the tails built here, ranks 9..14. At s >= 1: vartail.peeled
     (the peeled polynomials expand back, the peel's division leaving
     no remainder at degrees no rank below 9 reaches), vartail.
     poly_cells (rho is bandend.py's, E g^2 the exact c*'s: PC) and
     vartail.vstarts (PD). At order 0: dualtail.identities and
     dualtail.starts. An open tail or a control off stops the run
     before the walk.

FINDINGS (copied from the printed output).
  (1) THE CONTROLS PASS (PC and PD hold). Rank 5 through this file's
      path is vartail.py's row. All 57 tails at s >= 1 and all 6 at
      order 0 close. At s >= 1 the peeled polynomials expand back at
      all 57 tails; rho is bandend.py's and E g^2 the exact c*'s at
      all 228 cells; walked down from J_t + 40, the condition first
      fails at J_t - 1 at 50 tails and at J_t - 2 at 7, 2401 depths
      read. At order 0 the polynomials agree at all 18 cells, and the
      condition first fails at J_t - 1 at all 6, 252 depths read.
  (2) THE RECORD (PA and PB hold, the KILL misses). Each row is the
      record's. J_t, the latest start, is 559, 716, 898, 1098, 1321,
      1561, at order r - 3 at rank 9 and at r - 2 from rank 10; J_c,
      the latest crossing, is 157, 170, 227, 241, 305, 319, at order
      r - 3. Every order's crossing is at or below its start, 57 of
      57; the depths read are 1240, 1664, 2092, 2649, 3183, 3872.

RUN RECORD. python tailwalk.py: 13 of 13 checks, 159.6 s, peak commit
117.7 MB: the build 42.3 s, the six walks 1.8, 4.0, 8.4, 16.2, 30.4
and 54.1 s. Every row was the frozen one the first time it printed.
Entered from the first print, no hand value behind them: the start's
50 and 7, and order 0's 6 and 0. By hand before their run: the depths
read; 2401 = 50 * 42 + 7 * 43; 252 = 6 * 42.
Changed after the predictions were frozen, on a second reading of
the draft: C2's peel line, rho's count and order 0's two lines were
added, the controls they repeat stopping at rank 8; the rows' line
took J_t, J_c and their orders, which a print alone had carried.
At 160 s a run, no sweep of one-site edits fits it. Edits by hand, the
FAIL lines named before each run, six, five red on the lines named:
a term of the record (rank 9 alone); the control read against rank
6's row, and order 0's start read off order 1's tail (rank 5's
control, the run stopped there); the build one rank short (the five
counted controls); the polynomials' depths one short (their line
alone). The sixth, in vartail.py and run through this file, the
peel's terms from i = 4 dropped: a tail fails to close and the run
stops there, where the peel line had been named.
ENDS takes its J_c from bandend.JC since (the run 13 of 13 again,
166.5 s), and a seventh edit, JC[12] made 242 in bandend.py, fails
rank 12 alone. PB's scan of ranks 9 and 10 has left bandend.py.
"""
import os
import sys
import time

os.environ.setdefault("OPENBLAS_NUM_THREADS", "1")
import bandend  # noqa: E402
import dualtail  # noqa: E402
import vartail  # noqa: E402

RANKS = range(9, 15)
# rank: (the starts by order s = 0..r - 2; the crossings by order; the
#        orders whose crossing is at or below its start; the depths
#        read)
RECORD = {
    9: ((46, 51, 81, 93, 168, 175, 559, 557),
        (13, 21, 35, 45, 67, 73, 157, 95), 8, 1240),
    10: ((47, 59, 70, 109, 120, 215, 218, 709, 716),
         (14, 23, 33, 48, 56, 87, 89, 170, 97), 9, 1664),
    11: ((56, 55, 82, 93, 141, 150, 267, 265, 877, 898),
         (15, 25, 35, 47, 61, 73, 103, 105, 227, 121), 10, 2092),
    12: ((58, 64, 75, 108, 117, 176, 182, 324, 316, 1063, 1098),
         (16, 26, 35, 49, 56, 78, 82, 127, 121, 241, 123), 11, 2649),
    13: ((67, 61, 87, 96, 136, 143, 213, 217, 385, 370, 1267, 1321),
         (17, 27, 37, 47, 63, 71, 97, 105, 147, 141, 305, 147), 12, 3183),
    14: ((68, 71, 81, 112, 119, 165, 171, 253, 253, 451, 428, 1489, 1561),
         (18, 28, 37, 49, 59, 75, 86, 109, 115, 173, 159, 319, 149), 13,
         3872),
}
# rank: (J_t, the latest start; the orders it is at; J_c, the latest
#        crossing, bandend.py's table; the orders it is at), read off
#        RECORD by eye
ENDS = {
    9: (559, (6,), bandend.JC[9], (6,)),
    10: (716, (8,), bandend.JC[10], (7,)),
    11: (898, (9,), bandend.JC[11], (8,)),
    12: (1098, (10,), bandend.JC[12], (9,)),
    13: (1321, (11,), bandend.JC[13], (10,)),
    14: (1561, (12,), bandend.JC[14], (11,)),
}
PEELED = (57, 57)
POLYS = (228, 228, 228)
VSTARTS = (57, 50, 7, 2401)
IDENTITIES = (18, 18, 18)
STARTS = (6, 6, 0, 252)

CHECKS = []


def check(name, ok, detail=""):
    CHECKS.append(bool(ok))
    print(f"  [{'PASS' if ok else 'FAIL'}] {name}"
          + (f"  {detail}" if detail else ""))


def section(title):
    print()
    print(title)
    print("-" * len(title))


def tails(ranks):
    """(vartail.build(ranks), the tails at s >= 1; {(r, 0):
    dualtail.tail(r, 0)}, order 0's)."""
    return (vartail.build(ranks),
            {(r, 0): dualtail.tail(r, 0) for r in ranks})


def zeros_of(low):
    """{r: order 0's start}."""
    return {r: low[(r, 0)][0] for r, _ in low}


def main():
    t_all = time.time()
    section("CONTROL: rank 5 through this file's path")
    built, low = tails([5])
    got = vartail.rows_of([5], built, zeros_of(low))[5]
    check("rank 5, order 0's tail built: vartail.py's recorded row",
          got == vartail.RECORD[5], f"{got}")
    if not all(CHECKS):
        print("\nCONTROL FAILED: nothing below is read.")
        sys.exit(1)
    built, low = tails(RANKS)
    section("THE BUILD AND ITS CONTROLS: ranks 9..14, every order  (%.1f s)"
            % (time.time() - t_all))
    shut = [k for k in sorted(built) if built[k][0] is not None]
    done = [k for k in sorted(low) if low[k][0] is not None]
    check("every tail closes: at s >= 1; at order 0",
          len(shut) == len(built) and len(done) == len(low),
          f"{len(shut)} of {len(built)}; {len(done)} of {len(low)}")
    if not all(CHECKS):
        print("\nCONTROL FAILED, a tail is open: nothing below is read.")
        sys.exit(1)
    got = vartail.peeled(built)
    check("the peel as polynomials in J, s >= 1: tails; expanding back "
          "to adj(N) 1", got == PEELED, f"{got}")
    t0 = time.time()
    got = vartail.poly_cells(built, (100, 400, 1000))
    check("the polynomials, s >= 1, J = r + 7, 100, 400 and 1000: cells; "
          "rho bandend's m_s / C(2J, J); E g^2 the exact c*'s",
          got == POLYS, f"{got}" + "  (%.1f s)" % (time.time() - t0))
    t0 = time.time()
    got = vartail.vstarts(built)
    check("the start against the condition walked down from J_t + 40, "
          "s >= 1: tails; first failing at J_t - 1; at J_t - 2; depths "
          "read", got == VSTARTS,
          f"{got}" + "  (%.1f s)" % (time.time() - t0))
    t0 = time.time()
    got = dualtail.identities(low, (150, 1000))
    check("order 0's polynomials at depths r - 1, 150 and 1000: cells; "
          "rho bandend's m_s / C(2J, J); the expansion summed back c*",
          got == IDENTITIES, f"{got}" + "  (%.1f s)" % (time.time() - t0))
    t0 = time.time()
    got = dualtail.starts(low)
    check("order 0's start against its condition walked down from J_t + "
          "40: tails; first failing at J_t - 1; at J_t - 2; depths read",
          got == STARTS, f"{got}" + "  (%.1f s)" % (time.time() - t0))
    if not all(CHECKS):
        print("\nCONTROL FAILED: nothing below is read.")
        sys.exit(1)
    section("THE RECORD: each order walked down from its start to its "
            "crossing")
    print("  the starts by order; the crossings; the orders whose "
          "crossing is at or below its start; the depths read; then "
          "J_t, the orders it is at, J_c, the orders it is at")
    zeros = zeros_of(low)
    for r in sorted(RECORD):
        t0 = time.time()
        row = vartail.rows_of([r], built, zeros)[r]
        got = (row, vartail.ends(row))
        ok = got == (RECORD[r], ENDS[r])
        check(f"rank {r}", ok, f"{got}"
              + ("" if ok else f", recorded {(RECORD[r], ENDS[r])}")
              + "  (%.1f s)" % (time.time() - t0))
        sys.stdout.flush()
    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()