rank2.py
Checks: FLATTEN: At ranks 1 and 2 the least height is a pure product's theorem; rule.
Cited on: FLATTEN.
To run it: save rank2.py; standard library only.
"""rank2.py -- the rank-2 flattening lattice: its least height, its
second minimum, where the row's largest step sits, and the gap between
the two minima read in integers.
QUESTION. At width M = J + 2 the integer polynomials of degree < M that
vanish to order J at 1 are exactly (a + b x)(x - 1)^J, a lattice of
rank 2. What is its least height (the largest coefficient in absolute
value), what attains it, how far above it does the next independent
vector sit, and is the least height the height of a product of factors
x^d - 1?
THE OBJECTS. c_k = C(J, k), zero outside 0 <= k <= J. The coefficient
of x^k in (a + b x)(x - 1)^J is (-1)^(J-k) (a c_k - b c_(k-1)), so the
height is H(a, b) = max_k |a c_k - b c_(k-1)|, k = 0..J+1. U = c_mu
with mu = floor(J/2) is the row's largest entry, attained first at mu.
The STEPS are g(k) = c_k - c_(k-1), k = 0..J+1, and V = max_k |g(k)| is
the largest. The RESIDUAL eps_J is defined by
U/V = (sqrt(e)/2) sqrt(J + 1) (1 + eps_J).
THE PROOF (theorem, every J >= 2).
(i) Symmetries. H(a, b) = H(-a, -b), and the row's palindrome
c_k = c_(J-k) sends index k to J + 1 - k and gives
H(a, b) = H(b, a).
(ii) V <= U - 1. For 1 <= k <= J the step is below max(c_k, c_(k-1))
<= U in absolute value because both entries are at least 1, and
the end steps are c_0 = 1 and -c_J = -1; U >= 2 at J >= 2
finishes it. (At J = 1, U = V = 1 and (1, 0) ties (1, 1).)
(iii) Opposite signs or a zero coordinate. At k = mu,
|a c_mu - b c_(mu-1)| = |a| U + |b| c_(mu-1) >= U when a != 0,
and when a = 0 the index k = mu + 1 gives |b| U >= U.
(iv) Same sign, a != b. By (i) take a > b >= 1. At k = mu the row is
still strictly rising, c_(mu-1) < c_mu, so
a c_mu - b c_(mu-1) = (a - b) U + b (c_mu - c_(mu-1)) >= U + 1.
(v) a = b = t. H = |t| V.
So H >= U > V off the line a = b and H = |t| V on it: the least
height is V, attained only at +-(1, 1), which is +-(1 + x)(x - 1)^J.
Among vectors independent of (1, 1), (iv) gives at least U + 1 and
(iii) gives exactly U only when |a| U + |b| c_(mu-1) = U, which is
(a, b) = +-(1, 0), or when a = 0 and |b| = 1; so the second minimum
is U, attained there and nowhere else off the line. THE LIST: the
pairs at height <= U are +-t(1, 1) with 1 <= t <= floor(U/V), at
heights tV, and +-(1, 0), +-(0, 1) at U. (At J = 1, outside the
theorem, the list holds by hand: the six pairs of row (1, 1).)
(vi) The pure products at rank 2 with at least J parts and degree at
most J + 1 are (x - 1)^J, (x - 1)^(J+1) and (x - 1)^(J-1)(x^2 - 1),
cofactors 1, x - 1 and 1 + x, of heights U, C(J+1, floor((J+1)/2))
and V; so the least pure height is V and no rank-2 cell fails.
THE LARGEST STEP (theorem, every J >= 1). With n = J + 1, g(k) =
C(n, k)(n - 2k)/n, so g(n - k) = -g(k) and g > 0 for k < n/2. For
0 <= k < n/2 - 1 put j = n - 2k:
g(k+1) >= g(k) iff (n - k)(n - 2k - 2) >= (k + 1)(n - 2k)
iff (n + j)(j - 2) >= (n - j + 2) j
iff (j - 1)^2 >= n + 1,
equality with equality. As k rises (j - 1)^2 falls, so g rises
strictly, then falls strictly, with at most one tie, and its largest
value is at KAPPA, the least k >= 0 with (n - 2k - 1)^2 <= n + 1, which
is J - 2k <= isqrt(J + 2):
kappa = ceil((J - isqrt(J + 2))/2),
V = C(J + 1, kappa)(J + 1 - 2 kappa)/(J + 1).
g equals V a second time, at kappa + 1, exactly when J - 2 kappa =
sqrt(J + 2), which is J + 2 a perfect square s^2 (J - s = s(s - 1) - 2
is even at every s): a TIE. So the largest step sits sqrt(J + 2)/2 from
the row's centre, to within one index.
THE RESIDUAL IN INTEGERS. With n = J + 1, n eps = n (2U/(V sqrt(e n))
- 1), so for an integer q >= 1, with X = 4 U^2 q^2 n / V^2,
floor(q n eps) = isqrt(floor(X/e)) - q n,
since floor(sqrt(x)) = isqrt(floor(x)). e is irrational, so q n eps is
no integer: p/q < n eps < (p + 1)/q strictly at p the floor, and
"n eps > c/q" is "the floor is at least c". e is bracketed by its
series: with A = sum_(i<=m) m!/i!,
A/m! < e < (m A + 1)/(m m!),
the tail sum_(i>m) 1/i! being below (1/m!)(1/(m+1) + 1/(m+1)^2 + ...) =
1/(m m!). floor(X/e) lies between the floors at the bracket's two ends;
where their integer roots agree, that root is the answer, and where
they differ the function refuses.
THE ENGINE.
row(J); height(a, b, c), H on any row c; steps(c) and
largest_step(c): the largest |step| and the indices where the step
itself equals it (the rising side, on a palindrome).
kappa(J), tied(J) (J + 2 a square), closed_step(J) = (V, kappa),
minima(J) = (V, U): the closed forms.
search(c, B): every (a, b) != (0, 0) with H(a, b) <= B on a row c of
positive integers, as sorted (height, a, b). It is complete: index 0
gives |a c_0| <= B; and at the index after the row's first largest
entry T, where the row reads c' (zero past its end), |a c' - b T| <=
B confines b to ceil((a c' - B)/T) .. floor((a c' + B)/T), at most
2B/T + 1 values. Every pair in the box so cut is scored.
expected(V, U): THE LIST. search_row(J) = (V, U, search(row(J), U)).
step_row(J): the closed V and kappa, tied(J), and largest_step of
the row. e_bracket(m); residual_floor(J, q, bracket), refusing by
ArithmeticError; ratio_row(J): the floors at q = 10^6 and q = 12,
and tied(J).
search_record, step_record, parity_line and ratio_record: the
records, functions of plain rows.
What the script does not prove: the theorem (the proof above does; the
search reads it at fifteen depths); that eps_J tends to 0 or stays in
any band past the depths read; the second-order reading below.
BY HAND, the controls' values.
Heights, the values a c_k - b c_(k-1) in brackets. Row (1, 2, 1):
H(1, 1) = 1, H(1, 0) = 2, H(2, 1) = 3 [2, 3, 0, -1], H(1, -1) = 3.
Row (1, 3): H(a, b) = max(|a|, |3a - b|, 3|b|), so H(2, 1) = 5,
H(1, 2) = 6, H(1, -2) = 6. Row (1, 3, 3, 1): H(1, 1) = 2, H(2, 1) =
5 [2, 5, 3, -1, -1]. Row (1, 4, 6, 4, 1): H(1, 0) = 6, H(-1, 1) = 10
= C(5, 2), H(1, 1) = 3. Row (2, 3): H(3, 2) = max(6, 5, 6) = 6. Row
(3, 1): H(1, 1) = 3 [3, -2, -1], the first value alone the largest;
row (1, 3): H(1, 1) = 3 [1, 2, -3], the last alone.
Steps. (1, 2, 1): 1, 1, -1, -1, largest 1 at 0 and 1. (1, 3): 1, 2,
-3, largest 3, the step itself nowhere 3. (3, 1): 3, -2, -1, largest
3 at 0. Row 4: 1, 3, 2, largest 3 at 1. Row 7: 1, 6, 14, 14, 0,
largest 14 at 2 and 3.
The closed forms. kappa, V at J = 1: 0, 1. J = 2: 0, 1, a tie. J = 3:
1, 2. J = 7: 2, 14, a tie. J = 10: 4, 90. J = 14: 5, 1001, a tie
(steps 637, 1001, 1001, 429). J = 16: 6, 3640. J = 23: 9, C(23, 9) -
C(23, 8) = 817190 - 490314 = 326876 = C(23, 10) - C(23, 9) = 1144066
- 817190, a tie, and C(24, 9) 6/24 = 1307504/4. Ties at J <= 30: 2,
7, 14, 23. (V, U) at J = 1, 2, 10, 16: (1, 1), (1, 2), (90, 252),
(3640, 12870).
The search, each pair with its height in brackets, negatives left
out. Row (1, 2, 1), bound 2: (1, 1) [1], (2, 2) [2], (1, 0) [2],
(0, 1) [2]: 8 pairs; (2, 1), (1, 2) and (1, -1) pay 3, (2, 0) pays 4.
Row (1, 3, 3, 1), bound 3: |3a - 3b| <= 3 forces |a - b| <= 1; a = b
leaves (1, 1) [2]; a = b + 1 with |2b + 3| <= 3 and |1 - 2b| <= 3
leaves (0, -1) [3], (1, 0) [3]: 6 pairs. Row (1, 1), bound 1: the
nine of {-1, 0, 1}^2 less (0, 0), (1, -1), (-1, 1): 6 pairs at 1. Row
(1, 3), bound 6: |b| <= 2, and (2, 0) [6], (2, 1) [5], (2, 2) [6],
(1, -2) [6], (1, -1) [4], (1, 0) [3], (1, 1) [3], (1, 2) [6], (0, 1)
[3], (0, 2) [6]: 20 pairs; (2, -1) pays 7 and a = 3 needs b >= 3. It
is no palindrome: (2, 1) pays 5 and (1, 2) pays 6. At bound 4: (1, 0),
(1, 1), (0, 1) at 3 and (1, -1) at 4: 8 pairs. Row (2, 3), bound 6:
H = max(2|a|, |3a - 2b|, 3|b|), |a| <= 3, |b| <= 2: (3, 2) [6],
(2, 0) [6], (2, 1) [4], (2, 2) [6], (1, -1) [5], (1, 0) [3], (1, 1)
[3], (1, 2) [6], (0, 1) [3], (0, 2) [6]: 20 pairs, a reaching
B/c_0 = 3. Row (1, 3, 2), bound 6, no palindrome and its largest
entry inside: H = max(|a|, |3a - b|, |2a - 3b|, 2|b|), |b| <= 3:
(1, 0) [3], (2, 0) [6], (-1, 1) [5], (0, 1) [3], (1, 1) [2], (2, 1)
[5], (0, 2) [6], (1, 2) [4], (2, 2) [4], (2, 3) [6], (3, 3) [6]: 22
pairs; the strip is |2a - 3b| <= 6, and one read off the entry
before the largest, |a - 3b| <= 6, would lose (2, 3). Row
(1, 4, 6, 4, 1), bound 3, below U: (1, 1) alone.
The list. V, U = 297, 924: (1, 0), (0, 1) at 924 and t(1, 1) at 297,
594, 891: 10 pairs.
e. m = 1: (2, 3). m = 2: (5/2, 11/4). m = 4: A = 24 + 24 + 12 + 4 +
1 = 65: (65/24, 261/96).
The floor. J = 2 (U 2, V 1, n 3), q = 1, m = 1: X = 48, 48/3 = 16
and 48/2 = 24 both root to 4: 4 - 3 = 1. q = 10, m = 1: 4800/3 = 1600
roots to 40 and 4800/2 = 2400 to 48: refused. q = 10, m = 4:
4800 96/261 = 1765.5 and 4800 24/65 = 1772.3 both root to 42: 12.
J = 3 (U 3, V 2, n 4), q = 1, m = 4: X = 36; 36 96/261 = 13.2 and
36 24/65 = 13.3 root to 3: 3 - 4 = -1. q = 10: 3600 96/261 = 1324.1
and 3600 24/65 = 1329.2 root to 36: -4. To six places from e =
2.718281828459: J = 2: 3(4/sqrt(3e) - 1), sqrt(8.154845485) =
2.855669008, 1.400722559 - 1, times 3: 1.202167. J = 3: 6/sqrt(e) -
4 = 3.639183958 - 4 = -0.360816..., floor -360817. J = 7 (U 35, V
14, n 8): 20/sqrt(2e) - 8, sqrt(5.436563657) = 2.331643982:
0.577638. At q = 12: J = 2: floor(14.4) = 14; J = 3: floor(-4.3) =
-5; J = 7: floor(6.9) = 6.
The records' rows are made up, no two fields alike: see the control.
THE SECOND ORDER, a reading and no proof. With j = n - 2 kappa and
t = j - sqrt(n), expanding the binomials to order 1/n,
(J + 1) eps_J = t^2 - 5/12 + (1/2 at even J) + o(1),
and t lies in (d - 1, d + 1], d = sqrt(n + 1) - sqrt(n) < 1/(2
sqrt(n)), j being the largest integer of n's parity with j - 1 <=
sqrt(n + 1). (n even: U/V = (n/2j)
prod_(i=1..s) (N + i)/(N - i + 1) with N = n/2, s = j/2; the log of the
product is s^2/N - s^2/(2N^2) + s^4/(6N^3) + ..., which at j = x
sqrt(n) is x^2/2 - x^2/(2n) + x^4/(12n), and exp((x^2 - 1)/2)/x = 1 +
t^2/n + ... . n odd: the factor (n + 1)/n and the centre's half step
add 1/(2n).) It says where the extremes sit: the least where t = 0,
n a square of j's parity, near -5/12 at odd J and 1/12 at even; the
largest at the ties, t = d + 1, near 7/12 and 13/12.
PREDICTIONS, frozen before the first run of this form.
S1 The search at J = 1..16: V < U at 15 depths, THE LIST at all 16,
and (V, U, pairs) by depth as RECORD holds them, every entry read
off Pascal's rows by hand (pairs = 4 + 2 floor(U/V)).
T1 The scan at J = 2..300: the closed V at 299 of 299, its place or
places at 299, and 16 ties, at J = m^2 - 2, m = 2..17. The closed
V and its index were seen first against a scratch scan, J = 1..600.
R1 The floors of 10^6 (J + 1) eps at J = 2..4000: odd J least
-416614 first at 3843, largest 583321 first at 3967; even J least
83363 first at 3968, largest 1202167 at 2. Seen first in a
scratch run in decimals; the three depths then derived from the
reading: 3843 + 1 = 62^2 and 3968 + 1 = 63^2 are the largest even
and odd squares up to 4001, and 3967 + 2 = 63^2 is the last odd
tie.
R2 Unseen as a count: (J + 1) eps is above 13/12 at exactly
the even ties, J + 2 = m^2 with m = 2, 4, ..., 62: 31 depths, 31
of them even ties. The reading puts only the ties near 13/12; that
they sit above it is the scratch run's five largest even values,
all at ties, above 13/12 and falling, and no consequence of an
o(1).
FINDINGS (copied from the printed output).
(1) THE SEARCH (S1 holds). At J = 1..16 the pairs at height <= U are
THE LIST at 16 of 16 depths, and V < U at 15, every depth but
J = 1: V, U = 1, 2 at J = 2; 90, 252 at J = 10; 3640, 12870 at
J = 16; 6, 8 or 10 pairs a depth.
(2) THE LARGEST STEP (T1 holds). At J = 2..300 the row scan's
largest step is the closed V at 299 of 299 depths, at kappa
alone or at kappa and kappa + 1 as the theorem places it at 299,
with 16 ties, at J = 2, 7, 14, ..., 287.
(3) THE RATIO (R1 and R2 hold). Over J = 2..4000 the floors of 10^6
(J + 1) eps run from -416614 (first at J = 3843) to 583321
(first at J = 3967) at the 1999 odd depths, and from 83363
(first at J = 3968) to 1202167 (at J = 2) at the 2000 even ones.
So (J + 1) eps lies in (-0.416614, 1.202168) at every depth
read. It is above 13/12 at 31 depths, all 31 even with J + 2 a
square: R2's count, unseen before the run.
RUN RECORD. python rank2.py: 18 of 18 checks, 1.6 s (search 0.2 s,
steps 0.0 s, ratio 1.4 s), peak commit 7.9 MB under a 512 MB guard.
The form it replaces ran 2.2 s at 7.9 MB; a form before that walked
the whole row at every depth of the ratio and cost 389.5 s.
Left with their arms, read by the earlier form's run. The lattice
route, a reduction and an enumeration, gave V at J = 2..30, 29 of 29;
a scan of rank 2 by an exact walk is another script's. A hill climb
from a float start equalled the row scan at J = 2..300. eps itself, in
50-digit decimals: +0.400723 at J = 2, +0.024105 at J = 10, -0.004146
at J = 99, +0.008928 at J = 100, +0.000075 at J = 2000, +0.000038 at
J = 4000; and (J + 1) eps in [-0.4167, 1.2022], the ends rounded
outward from a float, which are (3)'s two extremes.
That form's frozen lines. P1 (the search at J = 2..16) held: (1). P2
(the route) held, its arm cut. P3 (-1 < (J + 1) eps <= 5/2 at J =
2..4000, a transplant) held, and is (3)'s extremes. K1..K3 were their
kills and share their fates.
Changed after the lines were frozen. The row (2, 3) was added after
the list of hand values and before the first run. From a reading of
the first draft, each by hand before its run: the heights where the
first or the last value alone is the largest, the row (1, 3, 2), and
in the records' made-up rows an order that is not ascending, a V
above U, a scan below the closed V, a largest attained twice, a
second even tie above 13/12. One hand value was wrong as first
written, the root of 3e (2.855668985), with the floor it leads to
unmoved. R2 was first written as the reading's own prediction: to
o(1) the reading does not decide the side of 13/12 (at J = 3842 it
gives 1.0995 where the value is 1.0834, 0.0001 above 13/12, by a
scratch run in decimals).
Edits no check notices. Of the meter's 167 sites, 162 red, 5 moving no
line, each equivalent: the series a term longer, which adds
floor(m!/(m + 1)!) = 0; kappa on isqrt(J + 3), since where J + 3 is a
square J less its root is odd and the ceiling absorbs the step; tied
with its root taken of J + 3, J + 2 being no square where that root
differs; the search's range of a, and of b, one wider, the added
pairs failing index 0 or the strip's own index. By hand three more
pass: the floor taken from the upper root (the two are equal wherever
the function does not refuse), the places read by >= (no step exceeds
the largest), and a tie counted as "not one place" (a row of no place
or of three already fails the count of places). Two checks no site
fails, each behind the controls' stop: the 13/12 line and the count of
check lines; a hand break fails each.
Broken by hand, single edits, each named by the lines it would fail
before its run, the control stop lifted. On the first draft 20: each
stage's range moved at either end, 10^6 made 10^5, the height's two
entries swapped, the scan without its absolute value, the places by
|step|, kappa by floor, tied one off, the refusal's two ends made
one, 13/12 read at 12, each first depth made the last, the search's a
from 0 and its strip a b short, the list a multiple short, the count
of control lines, and e's bracket at 4 terms: 20 red, the lines as
named, the last a refusal at the controls. Then 20 a reader proposed,
seven of them predicted to pass: with the hand cases above in, 15 red
and 5 green, the three by hand above and the meter's two on isqrt.
"""
import sys
import time
from math import comb, factorial, isqrt
Q = 10 ** 6
# ------------------------------------------------------------- the engine
def row(J):
return [comb(J, k) for k in range(J + 1)]
def height(a, b, c):
"""max_k |a c_k - b c_(k-1)| over k = 0..len(c), c zero outside."""
ext = [0] + list(c) + [0]
return max(abs(a * ext[k + 1] - b * ext[k]) for k in range(len(c) + 1))
def steps(c):
"""c_k - c_(k-1), k = 0..len(c)."""
ext = [0] + list(c) + [0]
return [ext[k + 1] - ext[k] for k in range(len(c) + 1)]
def largest_step(c):
"""(max_k |step|, the indices where the step itself equals it)."""
s = steps(c)
v = max(abs(x) for x in s)
return v, tuple(k for k, x in enumerate(s) if x == v)
def kappa(J):
"""ceil((J - isqrt(J + 2))/2)."""
return -((isqrt(J + 2) - J) // 2)
def tied(J):
return isqrt(J + 2) ** 2 == J + 2
def closed_step(J):
"""(V, kappa) by the closed form."""
k = kappa(J)
return comb(J + 1, k) * (J + 1 - 2 * k) // (J + 1), k
def minima(J):
"""(V, U)."""
return closed_step(J)[0], comb(J, J // 2)
def search(c, B):
"""Every (a, b) != (0, 0) with height(a, b, c) <= B, as sorted
(height, a, b), on a row c of positive integers."""
i = c.index(max(c))
top = c[i]
nxt = c[i + 1] if i + 1 < len(c) else 0
out = []
for a in range(-(B // c[0]), B // c[0] + 1):
for b in range(-((B - a * nxt) // top), (a * nxt + B) // top + 1):
if (a, b) != (0, 0):
h = height(a, b, c)
if h <= B:
out.append((h, a, b))
return sorted(out)
def expected(V, U):
"""THE LIST: the pairs at height <= U the theorem names."""
out = [(U, 1, 0), (U, -1, 0), (U, 0, 1), (U, 0, -1)]
for t in range(1, U // V + 1):
out += [(t * V, t, t), (t * V, -t, -t)]
return sorted(out)
def e_bracket(m):
"""((A, m!), (m A + 1, m m!)): A/m! < e < (m A + 1)/(m m!)."""
f = factorial(m)
A = sum(f // factorial(i) for i in range(m + 1))
return (A, f), (m * A + 1, m * f)
E30 = e_bracket(30)
def residual_floor(J, q, bracket):
"""floor(q (J + 1) eps_J), refusing where the bracket of e leaves
two candidates."""
n = J + 1
V, U = minima(J)
(ln, ld), (hn, hd) = bracket
num, den = 4 * U * U * q * q * n, V * V
lo = isqrt(num * hd // (den * hn))
hi = isqrt(num * ld // (den * ln))
if lo != hi:
raise ArithmeticError("undecided by this bracket of e")
return lo - q * n
def refuses(f, *args):
"""Whether f(*args) refuses."""
try:
f(*args)
except ArithmeticError:
return True
return False
# ---------------------------------------------------------------- the rows
def search_row(J):
"""(V, U, every pair at height <= U)."""
V, U = minima(J)
return V, U, search(row(J), U)
def step_row(J):
"""(the closed V, kappa, tied(J), the row scan's largest step and
its places)."""
V, k = closed_step(J)
return V, k, tied(J), largest_step(row(J))
def ratio_row(J):
"""(floor(10^6 (J + 1) eps), floor(12 (J + 1) eps), tied(J))."""
return residual_floor(J, Q, E30), residual_floor(J, 12, E30), tied(J)
# ------------------------------------------------------------- the records
def search_record(rows):
"""rows: search_row's, by depth."""
depths = sorted(rows)
return {
"depths; with V < U; with the pairs at height <= U the list": (
len(depths),
sum(1 for J in depths if rows[J][0] < rows[J][1]),
sum(1 for J in depths
if rows[J][2] == expected(rows[J][0], rows[J][1]))),
"by depth: J, V, U, pairs at height <= U": tuple(
(J, rows[J][0], rows[J][1], len(rows[J][2])) for J in depths),
}
def step_record(rows):
"""rows: step_row's, by depth."""
depths = sorted(rows)
two = tuple(J for J in depths if len(rows[J][3][1]) == 2)
return {
"depths; the scan's largest step the closed V; its places kappa "
"alone, or kappa and kappa + 1 where J + 2 is a square; ties": (
len(depths),
sum(1 for J in depths if rows[J][3][0] == rows[J][0]),
sum(1 for J in depths
if rows[J][3][1] == ((rows[J][1], rows[J][1] + 1)
if rows[J][2] else (rows[J][1],))),
len(two)),
"tie depths": two,
}
def parity_line(rows, par):
"""(depths of parity par, the least first field, the first depth
attaining it, the largest, its first depth)."""
depths = [J for J in rows if J % 2 == par]
lo = min(rows[J][0] for J in depths)
hi = max(rows[J][0] for J in depths)
return (len(depths), lo, min(J for J in depths if rows[J][0] == lo),
hi, min(J for J in depths if rows[J][0] == hi))
def ratio_record(rows):
"""rows: ratio_row's, by depth."""
high = [J for J in rows if rows[J][1] >= 13]
return {
"odd J: depths; the least floor of 10^6 (J + 1) eps, its first "
"depth; the largest, its first depth": parity_line(rows, 1),
"even J: depths; the least floor of 10^6 (J + 1) eps, its first "
"depth; the largest, its first depth": parity_line(rows, 0),
"depths with (J + 1) eps above 13/12; of them even with J + 2 a "
"square": (len(high),
sum(1 for J in high if J % 2 == 0 and rows[J][2])),
}
# -------------------------------------------------------------- the stages
def stage_search(jlo, jhi):
return search_record({J: search_row(J) for J in range(jlo, jhi + 1)})
def stage_steps(jlo, jhi):
return step_record({J: step_row(J) for J in range(jlo, jhi + 1)})
def stage_ratio(jlo, jhi):
return ratio_record({J: ratio_row(J) for J in range(jlo, jhi + 1)})
# ------------------------------------------------------------- the record
RECORD = {
"search": {
"depths; with V < U; with the pairs at height <= U the list":
(16, 15, 16),
"by depth: J, V, U, pairs at height <= U": (
(1, 1, 1, 6), (2, 1, 2, 8), (3, 2, 3, 6), (4, 3, 6, 8),
(5, 5, 10, 8), (6, 9, 20, 8), (7, 14, 35, 8), (8, 28, 70, 8),
(9, 48, 126, 8), (10, 90, 252, 8), (11, 165, 462, 8),
(12, 297, 924, 10), (13, 572, 1716, 10), (14, 1001, 3432, 10),
(15, 2002, 6435, 10), (16, 3640, 12870, 10)),
},
"steps": {
"depths; the scan's largest step the closed V; its places kappa "
"alone, or kappa and kappa + 1 where J + 2 is a square; ties":
(299, 299, 299, 16),
"tie depths": (2, 7, 14, 23, 34, 47, 62, 79, 98, 119, 142, 167,
194, 223, 254, 287),
},
"ratio": {
"odd J: depths; the least floor of 10^6 (J + 1) eps, its first "
"depth; the largest, its first depth":
(1999, -416614, 3843, 583321, 3967),
"even J: depths; the least floor of 10^6 (J + 1) eps, its first "
"depth; the largest, its first depth":
(2000, 83363, 3968, 1202167, 2),
"depths with (J + 1) eps above 13/12; of them even with J + 2 a "
"square": (31, 31),
},
}
CONTROL_LINES = 10
CHECKS = []
def check(name, ok, detail=""):
CHECKS.append(bool(ok))
print(f" [{'PASS' if ok else 'FAIL'}] {name}"
+ (f" {detail}" if detail else ""))
def check_hand(name, pairs):
hits = sum(1 for got, want in pairs if got == want)
check(name, hits == len(pairs), f"{hits} of {len(pairs)} cases")
def check_record(stage, got):
want = RECORD[stage]
for key in got:
ok = got[key] == want.get(key)
check(f"{stage}: {key}", ok,
f"{got[key]}" + ("" if ok else f", recorded {want.get(key)}"))
def section(title):
print()
print(title)
print("-" * len(title))
def both(half):
"""The hand pairs and their negatives, sorted."""
return sorted(half + [(h, -a, -b) for h, a, b in half])
def section_controls():
section("CONTROLS: each function on values written by hand")
check_hand("row and height on six rows", [
(row(4), [1, 4, 6, 4, 1]), (row(1), [1, 1]),
(height(1, 1, [1, 2, 1]), 1), (height(1, 0, [1, 2, 1]), 2),
(height(2, 1, [1, 2, 1]), 3), (height(1, -1, [1, 2, 1]), 3),
(height(2, 1, [1, 3]), 5), (height(1, 2, [1, 3]), 6),
(height(1, -2, [1, 3]), 6),
(height(1, 1, row(3)), 2), (height(2, 1, row(3)), 5),
(height(1, 0, row(4)), 6), (height(-1, 1, row(4)), 10),
(height(1, 1, row(4)), 3), (height(3, 2, [2, 3]), 6),
(height(1, 1, [3, 1]), 3), (height(1, 1, [1, 3]), 3)])
check_hand("steps and largest_step on five rows", [
(steps([1, 2, 1]), [1, 1, -1, -1]), (steps([1, 3]), [1, 2, -3]),
(largest_step([1, 2, 1]), (1, (0, 1))),
(largest_step([1, 3]), (3, ())),
(largest_step([3, 1]), (3, (0,))),
(largest_step(row(4)), (3, (1,))),
(largest_step(row(7)), (14, (2, 3)))])
check_hand("kappa and closed_step at eight depths, tied to depth 30, "
"minima at four", [
([closed_step(J) for J in (1, 2, 3, 7, 10, 14, 16, 23)],
[(1, 0), (1, 0), (2, 1), (14, 2), (90, 4), (1001, 5),
(3640, 6), (326876, 9)]),
([kappa(J) for J in (1, 2, 3, 7, 10, 14, 16, 23)],
[0, 0, 1, 2, 4, 5, 6, 9]),
([J for J in range(1, 31) if tied(J)], [2, 7, 14, 23]),
([minima(J) for J in (1, 2, 10, 16)],
[(1, 1), (1, 2), (90, 252), (3640, 12870)])])
check_hand("search on seven rows at eight bounds", [
(search([1, 2, 1], 2),
both([(1, 1, 1), (2, 2, 2), (2, 1, 0), (2, 0, 1)])),
(search(row(3), 3), both([(2, 1, 1), (3, 0, -1), (3, 1, 0)])),
(search([1, 1], 1), both([(1, 1, 1), (1, 1, 0), (1, 0, 1)])),
(search([1, 3], 6),
both([(6, 2, 0), (5, 2, 1), (6, 2, 2), (6, 1, -2), (4, 1, -1),
(3, 1, 0), (3, 1, 1), (6, 1, 2), (3, 0, 1), (6, 0, 2)])),
(search([1, 3], 4),
both([(3, 1, 0), (3, 1, 1), (3, 0, 1), (4, 1, -1)])),
(search([2, 3], 6),
both([(6, 3, 2), (6, 2, 0), (4, 2, 1), (6, 2, 2), (5, 1, -1),
(3, 1, 0), (3, 1, 1), (6, 1, 2), (3, 0, 1), (6, 0, 2)])),
(search([1, 3, 2], 6),
both([(3, 1, 0), (6, 2, 0), (5, -1, 1), (3, 0, 1), (2, 1, 1),
(5, 2, 1), (6, 0, 2), (4, 1, 2), (4, 2, 2), (6, 2, 3),
(6, 3, 3)])),
(search(row(4), 3), both([(3, 1, 1)]))])
check_hand("expected at four pairs of minima", [
(expected(1, 2),
both([(2, 1, 0), (2, 0, 1), (1, 1, 1), (2, 2, 2)])),
(expected(2, 3), both([(3, 1, 0), (3, 0, 1), (2, 1, 1)])),
(expected(1, 1), both([(1, 1, 0), (1, 0, 1), (1, 1, 1)])),
(expected(297, 924),
both([(924, 1, 0), (924, 0, 1), (297, 1, 1), (594, 2, 2),
(891, 3, 3)]))])
units = [(1, 1, 0), (1, 0, 1), (1, 1, 1)]
check_hand("search_row at two depths; search_record on five rows by "
"hand, out of order, three off the list, one with V = U, "
"one with V above U", [
(search_row(2),
(1, 2, both([(1, 1, 1), (2, 2, 2), (2, 1, 0),
(2, 0, 1)]))),
(search_row(1), (1, 1, both(units))),
(list(search_record({
8: (3, 7, []),
2: (1, 2, both([(2, 1, 0), (2, 0, 1), (1, 1, 1),
(2, 2, 2)])),
9: (5, 4, []),
5: (2, 6, both([(6, 1, 0), (6, 0, 1), (2, 1, 1),
(4, 2, 2)])),
1: (1, 1, both(units))}).values()),
[(5, 3, 2),
((1, 1, 1, 6), (2, 1, 2, 8), (5, 2, 6, 8),
(8, 3, 7, 0), (9, 5, 4, 0))])])
check_hand("step_row at two depths; step_record on six rows by hand, "
"out of order, two values off either way, three places "
"off", [
(step_row(7), (14, 2, True, (14, (2, 3)))),
(step_row(4), (3, 1, False, (3, (1,)))),
(list(step_record({
6: (9, 2, False, (9, (2, 3))),
2: (1, 0, True, (1, (0, 1))),
7: (14, 2, False, (13, (3,))),
3: (2, 1, False, (2, (1,))),
5: (5, 2, True, (5, (2,))),
4: (3, 1, False, (4, (1,)))}).values()),
[(6, 4, 3, 2), (2, 6)])])
check_hand("e_bracket at three depths", [
(e_bracket(1), ((2, 1), (3, 1))),
(e_bracket(2), ((5, 2), (11, 4))),
(e_bracket(4), ((65, 24), (261, 96)))])
check_hand("residual_floor on small brackets, refusing once, and at "
"three depths to six places and in twelfths", [
(residual_floor(2, 1, e_bracket(1)), 1),
(refuses(residual_floor, 2, 10, e_bracket(1)), True),
(refuses(residual_floor, 2, 10, e_bracket(4)), False),
(residual_floor(2, 10, e_bracket(4)), 12),
(residual_floor(3, 1, e_bracket(4)), -1),
(residual_floor(3, 10, e_bracket(4)), -4),
(ratio_row(2), (1202167, 14, True)),
(ratio_row(3), (-360817, -5, False)),
(ratio_row(7), (577638, 6, True))])
check_hand("parity_line and ratio_record on eight rows by hand", [
(list(ratio_record({
2: (5, 13, True), 3: (-7, 2, False), 4: (5, 13, False),
5: (-7, 14, True), 6: (9, 12, True), 7: (3, 0, False),
8: (5, 15, True), 9: (3, 13, False)}).values()),
[(4, -7, 3, 3, 7), (4, 5, 2, 9, 6), (5, 2)])])
def section_stage(stage, title, got, t0):
section(title)
check_record(stage, got)
print(" %s: %.1f s" % (stage, time.time() - t0))
def main():
t_all = time.time()
section_controls()
if not all(CHECKS):
print("\nCONTROL FAILED: nothing below is read.")
sys.exit(1)
lines = CONTROL_LINES + sum(len(RECORD[s]) for s in RECORD)
t0 = time.time()
section_stage("search", "THE SEARCH: J = 1..16, every pair at height "
"<= U", stage_search(1, 16), t0)
t0 = time.time()
section_stage("steps", "THE LARGEST STEP: J = 2..300, the row scan "
"against the closed form", stage_steps(2, 300), t0)
t0 = time.time()
section_stage("ratio", "THE RATIO: J = 2..4000, the residual in "
"integers", stage_ratio(2, 4000), t0)
ran = len(CHECKS)
check("every check line of the run printed, this one aside",
ran == lines, f"{ran} of {lines}")
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()