certclosed.py

Checks: FLATTEN: The takeover depth grows as r^(3/2) theorem; property; rule; pattern; observation.

Cited on: FLATTEN.

To run it: save certclosed.py, with bandend.py, bandscale.py, csbound.py, cstar_closed.py, dualtail.py, flatten.py, prunerr.py, rho_closed.py beside it; needs mpmath, numpy, sympy.

"""certclosed.py -- the closed certificate J^B_s: every term a Gamma ratio.

QUESTION. Order s certifies at depth J when m_s/B > I_r(J): B =
sqrt(2 m_s (pi tau + 1)) >= N = ||Q_s c*||_1 by the weighted
Cauchy-Schwarz, tau^2 = M2/m_s, and H_ch <= I_r(J) = 2^(J+r-1) B((J +
1)/2, r/2)/pi, the champion's mean modulus. So the dual bound passes H_ch
where Lambda_s(J) = log m_s - log 2 - log(pi tau + 1) - 2 log I_r(J) > 0.
J^B_s is the least J >= r - 1 with Lambda_s > 0 at every depth from J
on, and J^B = max_s J^B_s bounds the takeover depth from above. How does
J^B grow against max_s J*_s and r^(3/2), odd r = 21..401?

BY HAND (before any engine code; (b)'s derivation after the runs). (a)
m_s / C(2J, J) = gamma (J +
1/2)_u / (J + 1)_(s+u), gamma = 2^s (2s - 1)!! e! / (s + 3/2)_e. (b)
At even d, J' = J (at odd d, J' = J + 1, the even d's value), rho^2 =
2e + 1 + (2s + 4e + 3)/((4s - 2)(2s + 3)), the Gaussian limit of
M2/(m_s J/4) (csbound.py):
  M2/m_s = (J'/4) rho^2 + P_e(s)/(16(2s - 1)(2s + 3)) + (2s + 1)/(16(2J' - 1)),
  P_e(s) = (32e + 16)s^3 + 8(2e + 1)(2e + 3)s^2 + (32e^2 + 16e - 2)s
           - (4e + 1)(4e + 3),
first fitted, then derived, at every s >= 0, e >= 0, J' >= 1. Odd d is
even d at J + 1 (c* = (1 + x) ct, ct the cofactor at J + 1). At even d,
on the circle x = e^(i th), x^(-n/2) P = R = A h(t) up to a unit, t =
cos th, A = (2 cos(th/2))^J (2 sin(th/2))^s, h c*'s Jacobi factor
(cstar_closed.py (b)): m and M2 are the circle means of R^2 and R'^2.
By parts (A A' h^2 is 0 at th = 0, pi), mean R'^2 = mean A^2 h_th^2 -
mean A A'' h^2, and -A''/A = (J + s)^2/4 - J(J - 1)/(2(1 + t)) - s(s -
1)/(2(1 - t)). As A^2 dth is a multiple of w dt, w = (1 - t)^(al - 1)
(1 + t)^be, al = s + 1/2, be = J - 1/2, and h of y = P_e^(al, be),
orthogonal to lower degree under (1 - t) w:
  M2/m_s = Q'/Q0 + (J + s)^2/4 - (J(J - 1)/2) Q+/Q0 - (s(s - 1)/2) Q-/Q0,
Q0, Q+, Q- the integrals of w y^2 times 1, 1/(1 + t), 1/(1 - t), Q' of
w (1 - t^2) y'^2. Each is one single integral S_g = int (1 - t)^g
(1 + t)^be y dt = (al - g)_e 2^(g + be + 1) Gamma(g + 1)
Gamma(be + e + 1) / (e! Gamma(g + be + e + 2)), g > -1 (Rodrigues,
then e integrations by parts). Q0 = y(1) S_(al - 1), as y - y(1) is
(1 - t) times a lower degree; Q- = y(1) S_(al - 2) - y'(1) S_(al - 1)
likewise to second order (s >= 1; at s = 0 its coefficient and A's
sine are gone); Q+ = (Q0 + int (1 - t)^al (1 + t)^(be - 1) y^2)/2 by
1/(1 - t^2) = (1/(1 - t) + 1/(1 + t))/2, the second Q0's mirror
(P_e^(al, be)(-t) = (-1)^e P_e^(be, al)(t)); Q' is Q0 at (al + 1,
be + 1, e - 1), y' = ((e + al + be + 1)/2) P_(e-1)^(al+1, be+1). So
  Q+/Q0 = (al + be)/(2 be),  Q'/Q0 = e al (e + al + be + 1)/(al + 1),
  Q-/Q0 = (e + 1)(al + be + e)/(2(al - 1)) - e(e + al + be + 1)/(2(al + 1)),
and their sum is (b) identically in e, s, J, by hand (sympy's DERIVATION
below is its record). With D = (2s - 1)(2s + 3): J(J - 1)(J + s) = (2J -
1)(J^2/2 + (2s - 1)J/4 - (2s + 1)/8) - (2s + 1)/8, so the Q+ term and
(J + s)^2/4 leave (2s + 1)J/8 + s^2/4 + (2s + 1)/16 + (2s + 1)/(16(2J -
1)), the last (b)'s. The J terms of Q' and Q- add e(2s + 1)/(2s + 3) -
s(s - 1)(4e + 2s + 3)/(2D), and 8D times J's coefficient is (16e + 8)s^2
+ (16e + 10)s - (8e + 3), 8D rho^2/4. 16D times the constant is 4s^2 D +
(2s + 1)D + 16e(4s^2 - 1)(e + s + 1) - 8s(s - 1)(2s^2 + (4e + 3)s +
4e(e + 1)), the last factor (e + 1)(s + e)(2s + 3) - e(e + s + 1)(2s -
1): its s^4 terms cancel and it is P_e(s). So (b) is a theorem at
every s, e >= 0 (at s = 0, 1 the Q- term's coefficient is 0).
(c) The two-step ratios are rational:
m_s(J + 2)/m_s(J)
= 4(2J + 1 + 2u)(2J + 3 + 2u)/((J + 1 + s + u)(J + 2 + s + u)), I_r(J +
2)/I_r(J) = 4(J + 1)/(J + 1 + r), so Phi = m_s/I_r^2 steps by rho_Phi =
(2J + 1 + 2u)(2J + 3 + 2u)(J + 1 + r)^2 / (4(J + 1 + s + u)(J + 2 + s +
u)(J + 1)^2). As (pi tau' + 1)/(pi tau + 1) <= max(1, tau'/tau),
Lambda(J + 2) > Lambda(J) wherever rho_Phi > 1 and rho_Phi^2 tau^2(J) >
tau^2(J + 2): two polynomials in J, their real roots isolated exactly;
past J1, the ceiling of the largest, Lambda rises along each parity.
(d) Lambda in floats is of size J log 4, ~10^5 at r = 401, its error
~1e-10 against a step ~d/J ~ 1e-5: each crossing pair is re-read in
mpmath at 40 digits.

PREDICTIONS (frozen). (1) J^B / max_s J*_s in 1.3..1.6 at every r read,
flat; (2) the binding order d = 1 or 2; (3) J^B >= J_c at r = 21, 41
(681, 1989), near 1.45 J_c; (4) J1 below r - 1 at most orders.

KILL (frozen; what the script PRINTS). "KILL ratio: largest J^B / max_s
J*_s at r = 21..401: q (r)": KILLED iff q > 2, max_s J*_s by
csbound.Form at every order at r <= 61, d = 2's at r > 61. "KILL slope:
log-slope of J^B / r^(3/2) over r = 201..401: a": KILLED iff a > 0.05.
Family: odd r = 21, 41, 61, 81, 101, 121, 161, 201, 251, 301, 351, 401,
every order s <= r - 2.

CONTROLS, read first. DERIVATION: the three ratios of (b)'s derivation
sum to tau2's (b) as rational functions of e, s, J (sympy), and each
ratio equals its integrals by mpmath quadrature at three cells (s, J,
e) = (1, 4, 1), (2, 7, 2), (5, 12, 3), within 1e-20 relative: the
hand integrals' record. POSITIVE: (b) equals csbound.forms' M2/m exactly
at every order of r = 21, 41 at three depths and of r = 81 at one; the
exact certificate (bandend.passes against the champion's height) holds
at J^B_s at every order at r = 21 (it holds wherever this one does), and
J^B >= 681, 1989 at r = 21, 41 (at the freeze a scan's J_c, the least
depth from which the exact certificate holds at every order and every
deeper depth; here each is read one above a depth where it fails at
order r - 3, so J_c is at least it). INSTRUMENT: Lambda's
float sign equals mpmath's at J^B_s - 1, J^B_s and J^B_s + 1 at every
order read; Lambda <= 0 at J^B_s - 1 unless J^B_s = r - 1 (forced by
the downward loop's exit and the sign agreement: a record); both step
inequalities positive at J = 10^9 (forced by their leading terms,
8(r - s) - 4 and 16 rho^2 (r - s - 1): a record). Changed from
the freeze: the exact control reads the certificate at J^B_s alone, not
J^B_s against each order's exact crossing (a weaker read); mpmath
re-reads the final triple, not each bisection pair; "< 0" one below is
"<= 0"; "Lambda > 0 at the parity twin" holds by construction, unchecked;
J^B >= 681 widened to 681, 1989; (b) is also checked at each rank's
binding cell (after THE READ, not a gate), added after the first full
run; at the fold the KILL ratio's print lists its ranks, and after it
names d = 2's J*_s past r = 61; Form is csbound's since (the KILL
paragraph's name with it), and the read's "J_c" is "J_c >=". PLANT:
tau^2 times 16 at r = 21 moves J^B_s up at every order
(B grows with tau): read in the direction it pushes.

FINDINGS. "KILL ratio: largest J^B / max_s J*_s (d = 2's past r = 61) at
r = 21, 41, 61, 81, 101, 121, 161, 201, 251, 301, 351, 401: 1.4465
(401): HOLDS". "KILL slope: log-slope of J^B / r^(3/2) over r =
201..401: +0.0108: HOLDS".
  CONTROLS: the derivation's sum is (b) identically, its ratios within
  2.57e-22 of quadrature; (b) equals csbound.forms' M2/m at all 260
  cells; (a)'s log
  m_s within 5.45e-15 relative; the exact certificate holds at J^B_s at
  every order of r = 21; the plant moves J^B_s up at every order; (b)
  equals forms' M2/m at each rank's binding cell (r, r - 3, J^B).
  INSTRUMENT: float and mpmath agree at every crossing triple, Lambda
  <= 0 one below at every order; both step inequalities positive at
  J = 10^9 at every order.
  (1) held in its band, but not flat, rising: J^B / max_s J*_s (d = 2's
  J*_s past r = 61, every order's read at r <= 61) is 1.4181, 1.4320,
  1.4367, 1.4393,
  1.4409, 1.4419, 1.4435, 1.4444, 1.4452, 1.4458, 1.4462, 1.4465 at r =
  21, 41, 61, 81, 101, 121, 161, 201, 251, 301, 351, 401. (2) held: the
  binding order is d = 2 at every rank, d = 4 next (about half of it),
  d = 1 third at r = 21. (3) as read here: J^B 982 and 2876; 681 and
  1989 are lower bounds on J_c, so J^B/J_c <= 1.4420 and 1.4460, "near
  1.45" unread (J^B >= J_c itself follows from B >= N and I_r >=
  H_ch). (4) held at every
  order: J1 <= r - 1, so Lambda rises along each parity from the first
  depth, and J^B_s is one crossing.
  J^B / r^(3/2) reads 10.2043, 10.9550, 11.2169, 11.3512, 11.4331,
  11.4876, 11.5578, 11.6003, 11.6346, 11.6576, 11.6745, 11.6870, still
  rising; J^B 93847 at r = 401.
  So, (b) being proved, at each rank read the takeover depth is at
  most J^B(r), every order and every depth past it certified in closed
  form, the crossings read in 40-digit arithmetic: J_ch(r) <= 11.688
  r^(3/2) at the twelve odd ranks, beside the proved J_ch > 2(r -
  2)^(3/2) - 5(r - 2) - 1.
  Wall 37.5 s (four workers), 10/10 checks, peak 511.7 MB.

RUN RECORD. The bare run (r = 21, 41, one process): 10/10 checks,
"BARE KILL ratio: largest J^B / max_s J*_s (d = 2's past r = 61) at r =
21, 41: 1.4320 (41): HOLDS", 9.9 s. The full run reprints FINDINGS' two
KILL lines. Both
DERIVATION checks fail with Q+/Q0's 2 be made 2 al (quadrature off by
0.615).

RUN: python certclosed.py (the bare run: r = 21, 41, one process; its
kill lines tagged BARE, the slope unread). The twelve ranks on four
worker processes (about 40 s): python certclosed.py full.
"""
import os
import sys
import time
from concurrent.futures import ProcessPoolExecutor
from fractions import Fraction
from math import ceil, lgamma, log, pi, sqrt

os.environ.setdefault("OPENBLAS_NUM_THREADS", "1")

import mpmath as mp  # noqa: E402
import sympy as sp  # noqa: E402
from csbound import Form, Serial, forms  # noqa: E402
from bandend import champ_height, passes  # noqa: E402

FULL = (21, 41, 61, 81, 101, 121, 161, 201, 251, 301, 351, 401)
BARE = (21, 41)
JC = {21: 681, 41: 1989}
CHECKS = []
mp.mp.dps = 40


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 tau2(r, s, J, plant=1):
    """M2/m_s by (b), a Fraction."""
    d = r - 1 - s
    e = d // 2
    Jp = J + d % 2
    rho2 = 2 * e + 1 + Fraction(2 * s + 4 * e + 3, (4 * s - 2) * (2 * s + 3))
    P = ((32 * e + 16) * s ** 3 + 8 * (2 * e + 1) * (2 * e + 3) * s ** 2
         + (32 * e * e + 16 * e - 2) * s - (4 * e + 1) * (4 * e + 3))
    return plant * (Fraction(Jp, 4) * rho2
                    + Fraction(P, 16 * (2 * s - 1) * (2 * s + 3))
                    + Fraction(2 * s + 1, 16 * (2 * Jp - 1)))


def ratios(e, s, J):
    """(Q+/Q0, Q-/Q0, Q'/Q0) of (b)'s derivation, al = s + 1/2,
    be = J - 1/2: works on sympy symbols and on numbers."""
    al, be = s + sp.Rational(1, 2), J - sp.Rational(1, 2)
    return ((al + be) / (2 * be),
            (e + 1) * (al + be + e) / (2 * (al - 1))
            - e * (e + al + be + 1) / (2 * (al + 1)),
            e * al * (e + al + be + 1) / (al + 1))


def derivation():
    """The two records of (b)'s derivation: the sum, symbolic; each
    ratio against quadrature of its integrals."""
    e, s, J = sp.symbols("e s J", positive=True)
    qp, qm, qd = ratios(e, s, J)
    tot = qd + (J + s) ** 2 / 4 - J * (J - 1) / 2 * qp - s * (s - 1) / 2 * qm
    rho2 = 2 * e + 1 + (2 * s + 4 * e + 3) / ((4 * s - 2) * (2 * s + 3))
    P = ((32 * e + 16) * s ** 3 + 8 * (2 * e + 1) * (2 * e + 3) * s ** 2
         + (32 * e * e + 16 * e - 2) * s - (4 * e + 1) * (4 * e + 3))
    b = (J / 4 * rho2 + P / (16 * (2 * s - 1) * (2 * s + 3))
         + (2 * s + 1) / (16 * (2 * J - 1)))
    check("DERIVATION: the three ratios sum to (b), identically in e, s, J",
          sp.simplify(sp.together(tot - b)) == 0)
    worst = mp.mpf(0)
    for s_, J_, e_ in ((1, 4, 1), (2, 7, 2), (5, 12, 3)):
        al, be = mp.mpf(s_) + 0.5, mp.mpf(J_) - 0.5

        def y(t):
            return mp.jacobi(e_, al, be, t)

        def w(t):
            return (1 - t) ** (al - 1) * (1 + t) ** be

        q0 = mp.quad(lambda t: w(t) * y(t) ** 2, [-1, 1])
        got = (mp.quad(lambda t: w(t) * y(t) ** 2 / (1 + t), [-1, 1]) / q0,
               mp.quad(lambda t: w(t) * y(t) ** 2 / (1 - t), [-1, 1]) / q0,
               mp.quad(lambda t: w(t) * (1 - t * t) * mp.diff(y, t) ** 2,
                       [-1, 1]) / q0)
        for g, want in zip(got, ratios(e_, s_, J_)):
            worst = max(worst, abs(g / mp.mpf(sp.Rational(want).p)
                                   * sp.Rational(want).q - 1))
    check("DERIVATION: each ratio equals its integrals by quadrature at "
          "(s, J, e) = (1, 4, 1), (2, 7, 2), (5, 12, 3), within 1e-20",
          worst < mp.mpf(10) ** -20, f"largest relative {mp.nstr(worst, 3)}")


def logm_float(r, s, J):
    d = r - 1 - s
    e, u = d // 2, d - d // 2
    lgam = (s * log(2) + lgamma(2 * s) - lgamma(s) - (s - 1) * log(2)
            if s else 0.0)                             # log 2^s (2s-1)!!
    lgam += lgamma(e + 1) - (lgamma(s + 1.5 + e) - lgamma(s + 1.5))
    return (lgamma(2 * J + 1) - 2 * lgamma(J + 1) + lgam
            + lgamma(J + 0.5 + u) - lgamma(J + 0.5)
            - (lgamma(J + 1 + s + u) - lgamma(J + 1)))


def lam_float(r, s, J, plant=1):
    logm = logm_float(r, s, J)
    logI = ((J + r - 1) * log(2) + lgamma((J + 1) / 2) + lgamma(r / 2)
            - lgamma((J + 1 + r) / 2) - log(pi))
    t = sqrt(float(tau2(r, s, J, plant)))
    return logm - log(2) - log(pi * t + 1) - 2 * logI


def lam_mp(r, s, J, plant=1):
    d = r - 1 - s
    e, u = d // 2, d - d // 2
    lg = mp.loggamma
    lgam = s * mp.log(2) + (lg(2 * s) - lg(s) - (s - 1) * mp.log(2)
                            if s else 0)
    lgam += lg(e + 1) - (lg(s + mp.mpf(3) / 2 + e) - lg(s + mp.mpf(3) / 2))
    h = mp.mpf(1) / 2
    logm = (lg(2 * J + 1) - 2 * lg(J + 1) + lgam + lg(J + h + u)
            - lg(J + h) - (lg(J + 1 + s + u) - lg(J + 1)))
    logI = ((J + r - 1) * mp.log(2) + lg(mp.mpf(J + 1) / 2) + lg(mp.mpf(r) / 2)
            - lg(mp.mpf(J + 1 + r) / 2) - mp.log(mp.pi))
    t2 = tau2(r, s, J, plant)
    t = mp.sqrt(mp.mpf(t2.numerator) / t2.denominator)
    return logm - mp.log(2) - mp.log(mp.pi * t + 1) - 2 * logI


def j1(r, s, plant=1):
    """Past J1 Lambda rises along each parity, by (c)'s two
    polynomial inequalities, their real roots isolated exactly."""
    d = r - 1 - s
    u = d - d // 2
    x = sp.symbols("x")
    A = (2 * x + 1 + 2 * u) * (2 * x + 3 + 2 * u) * (x + 1 + r) ** 2
    Bq = 4 * (x + 1 + s + u) * (x + 2 + s + u) * (x + 1) ** 2
    e = d // 2
    eps = d % 2
    rho2 = 2 * e + 1 + sp.Rational(2 * s + 4 * e + 3, (4 * s - 2) * (2 * s + 3))
    P = ((32 * e + 16) * s ** 3 + 8 * (2 * e + 1) * (2 * e + 3) * s ** 2
         + (32 * e * e + 16 * e - 2) * s - (4 * e + 1) * (4 * e + 3))

    def t2(y):
        yp = y + eps
        return plant * (yp / 4 * rho2 + sp.Rational(P, 16 * (2 * s - 1)
                                                   * (2 * s + 3))
                        + sp.Rational(2 * s + 1, 16) / (2 * yp - 1))
    polys = [sp.Poly(sp.numer(sp.together(A - Bq)), x),
             sp.Poly(sp.numer(sp.together(A ** 2 * t2(x) - Bq ** 2
                                          * t2(x + 2))), x)]
    top = 0
    for p in polys:
        for (lo, hi), _ in p.intervals():
            top = max(top, hi)
    # past the last root each inequality keeps its sign at large x
    big = 10 ** 9
    ok = (A - Bq).subs(x, big) > 0 and (A ** 2 * t2(x) - Bq ** 2
                                        * t2(x + 2)).subs(x, big) > 0
    return max(ceil(top) + 1, 1), bool(ok)


def jb(args):
    """(r, s, J^B_s, J1, LC ok, float/mp agreement, wall)."""
    r, s, plant = args
    t0 = time.time()
    J1, ok = j1(r, s, plant)
    lo = r - 1
    start = max(J1, lo)
    # per parity: the least J >= start of that parity with Lambda > 0;
    # past it Lambda rises, so it holds from there on that parity
    least = []
    for par in (0, 1):
        a = start + ((par - start) % 2)
        if lam_float(r, s, a, plant) > 0:
            b = a
        else:
            step = 2
            b = a + step
            while lam_float(r, s, b, plant) <= 0:
                a, step = b, step * 2
                b = a + step
            while b - a > 2:                 # a fails, b holds, same parity
                mid = a + 2 * ((b - a) // 4)
                if lam_float(r, s, mid, plant) > 0:
                    b = mid
                else:
                    a = mid
        least.append(b)
    J = max(least)                # every depth past it holds by (c)
    while J - 1 >= lo and lam_float(r, s, J - 1, plant) > 0:
        J -= 1                    # each depth below read alone
    agree = all((lam_float(r, s, j, plant) > 0) == (lam_mp(r, s, j, plant) > 0)
                for j in (J - 1, J, J + 1) if j >= lo)
    below = J == lo or lam_mp(r, s, J - 1, plant) <= 0
    return r, s, J, J1, ok, agree and below, time.time() - t0


def jstar_of(args):
    r, s = args
    try:
        return r, s, float(Form(s, r - 1 - s).jstar())
    except RuntimeError:
        return r, s, None


def slope(xs, ys):
    lx = [log(x) for x in xs]
    ly = [log(y) for y in ys]
    mx, my = sum(lx) / len(lx), sum(ly) / len(ly)
    return (sum((a - mx) * (b - my) for a, b in zip(lx, ly))
            / sum((a - mx) ** 2 for a in lx))


def main(ranks, tag, pooled):
    t0 = time.time()
    print("CONTROLS", flush=True)
    derivation()
    bad, worst = [], 0.0
    cells = [(r, s, J) for r in (21, 41) for s in range(r - 1)
             for J in (r - 1, 3 * r, 400 + r)] + [(81, s, 900) for s in range(80)]
    for r, s, J in cells:
        m, M2 = forms(r, s, J)
        if M2 / m != tau2(r, s, J):
            bad.append((r, s, J))
        n = J + r - 1
        q0 = s - 1 if s >= 1 else 0
        logA = (n * log(4) + lgamma(n - q0 + 0.5) + lgamma(q0 + 0.5)
                - 2 * lgamma(0.5) - lgamma(n + 1))
        lm = logA + log(m.numerator) - log(m.denominator)
        worst = max(worst, abs(lm - logm_float(r, s, J)) / max(1, abs(lm)))
    check(f"POSITIVE: (b)'s closed M2/m equals csbound.forms' at {len(cells)} "
          "cells (every order, r = 21, 41 at three depths, r = 81 at one)",
          not bad, f"disagreements {bad[:5]}")
    check("POSITIVE: (a)'s log m_s against csbound.forms' m at those cells, "
          "within 1e-12 relative", worst < 1e-12, f"largest {worst:.2e}")
    with (ProcessPoolExecutor(max_workers=4) if pooled else Serial()) as pool:
        r = 21
        rows = list(pool.map(jb, [(r, s, 1) for s in range(r - 1)]))
        H = {}
        fails = []
        for _, s, J, *_ in rows:
            H.setdefault(J, champ_height(r, J))
            if not passes(r, J, s, H[J]):
                fails.append((s, J))
        check("POSITIVE: the exact certificate holds at J^B_s at every order, "
              "r = 21", not fails, f"fails at {fails}")
        pl = list(pool.map(jb, [(r, s, 16) for s in range(r - 1)]))
        moved = [(a[1], a[2], b[2]) for a, b in zip(rows, pl) if b[2] <= a[2]]
        check("PLANT: tau^2 x 16 at r = 21 moves J^B_s up at every order",
              not moved, f"unmoved {moved[:5]}")
        if not all(CHECKS):
            print("CONTROL FAILED: nothing below is read.")
            sys.exit(1)

        print("THE READ", flush=True)
        tasks = [(r, s, 1) for r in ranks for s in range(r - 1)]
        tasks.sort(key=lambda t: -t[0])
        out = list(pool.map(jb, tasks, chunksize=4))
        jtasks = [(r, s) for r in ranks if r <= 61 for s in range(r - 1)]
        jtasks += [(r, r - 3) for r in ranks if r > 61]
        js = list(pool.map(jstar_of, jtasks))
    jsr = {}
    for r, s, v in js:
        if v is not None:
            jsr.setdefault(r, {})[s] = v
    bad_i = [(r, s) for r, s, J, J1, ok, good, w in out if not good]
    bad_lc = [(r, s) for r, s, J, J1, ok, good, w in out if not ok]
    check("INSTRUMENT: float and mpmath agree at J^B_s - 1, J^B_s, J^B_s + 1, "
          "Lambda <= 0 one below, every order", not bad_i, f"off at {bad_i[:5]}")
    check("INSTRUMENT: both step inequalities positive at J = 10^9, every "
          "order", not bad_lc, f"off at {bad_lc[:5]}")
    bind = {}
    for r, s, J, J1, ok, good, w in out:
        if r not in bind or J > bind[r][1]:
            bind[r] = (s, J)
    off = []
    for r, (s, J) in sorted(bind.items()):
        m, M2 = forms(r, s, J)
        if M2 / m != tau2(r, s, J):
            off.append((r, s, J))
    check("POSITIVE: (b) equals csbound.forms' M2/m exactly at each rank's "
          "binding cell (r, s, J^B)", not off, f"disagreements {off}")
    by = {}
    for r, s, J, J1, ok, good, w in out:
        by.setdefault(r, []).append((J, s, J1, w))
    qs, ys = [], []
    for r in ranks:
        rows = sorted(by[r], reverse=True)
        J, s, J1, w = rows[0]
        jmax = max(jsr[r].values())
        q = J / jmax
        qs.append((q, r))
        ys.append(J / r ** 1.5)
        n_above = sum(1 for J_, s_, J1_, w_ in rows if J1_ > r - 1)
        top3 = ", ".join(f"d={r - 1 - s_}: {J_}" for J_, s_, J1_, w_ in rows[:3])
        tail = (f", J_c >= {JC[r]}, J^B over it {J / JC[r]:.4f}"
                if r in JC else "")
        print(f"  r = {r:3d}: J^B {J} (s={s}, d={r - 1 - s}), max J*_s "
              f"{jmax:.2f} ({len(jsr[r])} order(s) read), J^B/J* {q:.4f}, "
              f"J^B/r^1.5 {J / r ** 1.5:.4f}; "
              f"top {top3}; J1 > r - 1 at {n_above} of {r - 1}{tail}; "
              f"largest wall {max(x[3] for x in rows):.1f} s", flush=True)
    jcok = all(by_r >= JC[r] for r in JC if r in by
               for by_r in [max(x[0] for x in by[r])])
    below = [r for r in JC if r in by and passes(
        r, JC[r] - 1, r - 3, champ_height(r, JC[r] - 1))]
    check("POSITIVE: J^B >= 681, 1989 at r = 21, 41, each one above a depth "
          "where the exact certificate fails at order r - 3",
          jcok and not below,
          f"J^B >= each {jcok}, passes one below at r = {below}")
    q, rq = max(qs)
    print(f"{tag}KILL ratio: largest J^B / max_s J*_s (d = 2's past r = "
          f"61) at r = "
          f"{', '.join(map(str, ranks))}: {q:.4f} ({rq}): "
          f"{'KILLED' if q > 2 else 'HOLDS'}")
    big = [(r, y) for r, y in zip(ranks, ys) if r >= 201]
    if len(big) >= 2:
        a = slope([r for r, _ in big], [y for _, y in big])
        print(f"KILL slope: log-slope of J^B / r^(3/2) over r = 201..401: "
              f"{a:+.4f}: {'KILLED' if a > 0.05 else 'HOLDS'}")
    print(f"{sum(CHECKS)}/{len(CHECKS)} checks, {time.time() - t0:.1f} s")
    sys.exit(0 if all(CHECKS) else 1)


if __name__ == "__main__":
    arg = sys.argv[1:]
    if not arg:
        main(BARE, "BARE ", False)
    elif arg == ["full"]:
        main(FULL, "", True)
    else:
        sys.exit(__doc__)