cstar_closed.py
Checks: FLATTEN: Every rank's band of failing depths ends theorem; rule; pattern.
Cited on: FLATTEN.
To run it: save cstar_closed.py, with bandend.py, bandscale.py, dualtail.py, flatten.py, prunerr.py, rho_closed.py beside it; needs mpmath, numpy, sympy.
"""cstar_closed.py -- the least-norm cofactor c* and its Gaussian limit
f*, in closed form.
QUESTION. Q_s = (1 + x)^J (1 - x)^s, c* the least ||Q_s c||^2 over real
c of degree <= d = r - 1 - s with c(1) = 1 (bandend.py's, vartail.py's
and rho_closed.py's objects); f* = (He_s + sum b_m He_(s+m)) phi least
in L2(dy) (bandscale.py's THE LIMIT). Is c* the endpoint kernel of
rho_closed.py's Jacobi weight, and f* that of a Laguerre weight, both in
closed form?
BY HAND (before any engine code).
(a) Endpoint kernel lemma. w a weight on an interval with endpoint
z0, K_e(t, z0) = sum_(n<=e) p_n(t) p_n(z0), p_n orthonormal. For
deg pi < e, int K_e(t, z0) (t - z0) pi(t) w dt = [(t - z0) pi](z0) = 0
(reproducing, (t - z0) pi of degree <= e), so K_e(., z0) is the
degree-e orthogonal polynomial of |t - z0| w; the least int h^2 w at
h(z0) = 1 is attained at K_e(t, z0) / K_e(z0, z0).
(b) Finite J. At d = 2e, c*(e^(i th)) = e^(i e th) h*(t), t = cos th,
w = (1 - t)^a (1 + t)^b, a = s - 1/2, b = J - 1/2 (rho_closed.py (a),
(b)). By (a), h* = P_e^(a+1, b)(t) / P_e^(a+1, b)(1); with
P_e^(al,be)(t) = sum_k C(e + al, e - k) C(e + be, k) ((t - 1)/2)^k
((t + 1)/2)^(e-k) and, at t = (x + 1/x)/2, (t -+ 1)/2 = (x -+ 1)^2/(4x),
c*(x) = sum_k C(e + s + 1/2, e - k) C(e + J - 1/2, k)
(1 - x)^(2k) (1 + x)^(2e-2k) / (4^e C(e + s + 1/2, e)).
At d = 2e + 1, c* = (1 + x) ct, ct the same at J + 1 (b = J + 1/2),
halved. So Q_s c* = sum_k w_k (1 + x)^(J+eps+2e-2k) (1 - x)^(s+2k),
eps = d mod 2, every w_k > 0 (each factor of C(e + J -+ 1/2, k) is at
least 1/2 at k <= e): the minimiser is a positive combination of
e + 1 products of 1 + x and 1 - x. Rank 3, s = 0, J = 1 (e = 1, b = 1/2):
(3/2)(1 + x)^2 + (3/2)(1 - x)^2 over 4 (3/2) is (1 + x^2)/2.
(c) The Gaussian limit. f-hat(om) = sqrt(2 pi) (-i om)^s g(om)
e^(-om^2/2), g(0) = 1, and int f^2 = int om^(2s) |g|^2 e^(-om^2) dom;
the odd part of g only adds, so g = h(om^2), deg h <= e = floor(d/2),
and with v = om^2 it is int_0^oo h^2 v^(s-1/2) e^(-v) dv: by (a) at
z0 = 0, h* = L_e^(s+1/2)(v) / L_e^(s+1/2)(0). Back through
FT(He_m phi) = sqrt(2 pi) (-i om)^m e^(-om^2/2):
f* = phi sum_(n<=e) C(e, n) / (s + 3/2)_n He_(s+2n),
int f*^2 = 1 / K_e(0, 0) = e! Gamma(s + 1/2) / (s + 3/2)_e
= sqrt(pi) gamma(s, e) / 4^s,
gamma = 2^s (2s - 1)!! e! / (s + 3/2)_e, rho_closed.py's gamma, the
finite-J constant again. Against bandscale.py's BY HAND: b_2 =
2/(2s + 3) at e = 1, 4/(2s + 3) and 4/((2s + 3)(2s + 5)) at e = 2;
int f^2 = sqrt(pi)/2, 2 sqrt(pi)/3, 3 sqrt(pi)/4, sqrt(pi)/5,
8 sqrt(pi)/15, 3 sqrt(pi)/14 at (r, s) = (3, 1), (3, 0), (4, 2),
(4, 1), (5, 0), (5, 2): all six.
PREDICTIONS. (1) The closed c* equals the Gram solve's c* = m_s y,
coefficient for coefficient, at every cell (ranks 3..14, every order,
J = 0..40), exact (strongly). (2) Every w_k > 0 there (a property of
(b)). (3) bandscale.exact_b(s, d) equals (c)'s b_m at s = 0..30,
d = 1..12, exact (strongly). (4) int f*^2 over sqrt(pi), by
bandscale.gram on exact_b's f, equals gamma / 4^s at the same cells
(strongly).
KILL (frozen; what the script PRINTS): "KILL cstar: X of Y cells
differ; limit: Z of W cells differ", fires iff X + Z > 0, X counting
(1), Z counting (3) or (4) failing at a cell.
CONTROLS (read before any verdict). POSITIVE: the closed c* at rank 3,
s = 0, J = 1 is (1 + x^2)/2; the Gram c* there too; exact_b(0, 2) =
(0, 2/3). PLANTS: c* with P_e^(a, b) for P_e^(a+1, b) (the kernel
unshifted) differs at some e >= 1 cell; b_(2n) with (s + 1/2)_n for
(s + 3/2)_n differs at some e >= 1 cell.
FINDINGS. The kill misses: "KILL cstar: 0 of 3690 cells differ;
limit: 0 of 372 cells differ". (1) to (4) held.
CONTROLS: the positive reads "['1/2', '0', '1/2'], ['1/2', '0',
'1/2'], ['0', '2/3']"; the plants "3198 of 3198 and 341 of 341 cells
differ" (every e >= 1 cell).
(1) "exact: 3690 of 3690 cells agree". (2) "every w_k > 0 at 3690 of
3690 cells; 12013 weights"; at (r, s, J) = (5, 0, 3) w = 1/16, 3/8,
21/80, at (8, 1, 10) 1/64, 15/64, 345/448, 115/192, w_0 4^e = 1
(c*(1) = 1) at both.
(3) "372 of 372 cells agree"; (s, d) = (2, 4): b = 0, 4/7, 0, 4/63;
(5, 6): 0, 6/13, 0, 4/65, 0, 8/3315. (4) "372 of 372 cells agree";
l2 3/14, 2/21, 189/442 at (2, 2), (2, 4), (5, 6).
So by (a)-(c), each step by hand, both forms are theorems at every
rank, order and depth, and in the limit at every s and d; the runs
read them exactly at the stated families. The l2 minimiser is a
positive combination of e + 1 products (1 + x)^A (1 - x)^B,
A + B = J + r - 1, B = s, s + 2, .., s + 2e; its limit a positive
combination of He_s phi, He_(s+2) phi, .., He_(s+2e) phi, the limits
of those products' coefficients, term by term by hand: C(e + J
-+ 1/2, k) ~ J^k / k!, against sigma^(-2k) = (4/J)^k and the 2^(-2k)
of the shorter (1 + x) power, leaves 1/k! times w_k's other factor
C(e + s + 1/2, e - k) / C(e + s + 1/2, e), and the product is
C(e, k) / (s + 3/2)_k.
The frozen (1) named rho_closed.py's Gram solve; the script carries
its own copy, gram_cstar, the same elimination returning y.
3/3 checks, the kill the third, 2.7 s, 27.3 MB under gate --run.
RUN: python cstar_closed.py.
"""
import os
import sys
import time
from fractions import Fraction
from math import factorial
os.environ.setdefault("OPENBLAS_NUM_THREADS", "1")
from rho_closed import q_coeffs, rising, dfact # noqa: E402
import bandscale # noqa: E402
RANKS = range(3, 15)
DEPTHS = range(0, 41)
LIMIT_S = range(0, 31)
LIMIT_D = range(1, 13)
HALF = Fraction(1, 2)
CHECKS = []
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 binom(x, k):
"""C(x, k) for rational x, integer k >= 0."""
out = Fraction(1)
for i in range(k):
out = out * (x - i) / (i + 1)
return out
def pmul(a, b):
out = [Fraction(0)] * (len(a) + len(b) - 1)
for i, x in enumerate(a):
if x:
for j, y in enumerate(b):
out[i + j] += x * y
return out
def ppow(base, n):
out = [Fraction(1)]
for _ in range(n):
out = pmul(out, base)
return out
def gram_cstar(r, s, J):
"""c* = m_s y, y = G_s^-1 1, by an exact solve."""
d = r - 1 - s
q = q_coeffs(J, s)
ac = [sum(q[k] * q[k + m] for k in range(len(q) - m)) if m < len(q)
else 0 for m in range(d + 1)]
n = d + 1
A = [[Fraction(ac[abs(i - k)]) for k in range(n)] + [Fraction(1)]
for i in range(n)]
for c in range(n):
p = next(i for i in range(c, n) if A[i][c] != 0)
A[c], A[p] = A[p], A[c]
for i in range(n):
if i != c and A[i][c] != 0:
f = A[i][c] / A[c][c]
A[i] = [x - f * y for x, y in zip(A[i], A[c])]
y = [A[i][n] / A[i][i] for i in range(n)]
tot = sum(y)
return [v / tot for v in y]
def weights(r, s, J, plant=None):
"""w_k of (b): c* = sum_k w_k (1 - x)^(2k) (1 + x)^(2e-2k), times
(1 + x) at odd d."""
d = r - 1 - s
e, eps = d // 2, d % 2
al = Fraction(2 * s + 1, 2) if plant != "unshift" else Fraction(
2 * s - 1, 2)
be = Fraction(2 * (J + eps) - 1, 2)
norm = 4 ** e * binom(e + al, e) * (2 if eps else 1)
return [binom(e + al, e - k) * binom(e + be, k) / norm
for k in range(e + 1)]
def closed_cstar(r, s, J, plant=None):
d = r - 1 - s
e, eps = d // 2, d % 2
one_m, one_p = [Fraction(1), Fraction(-1)], [Fraction(1), Fraction(1)]
out = [Fraction(0)] * (2 * e + 1)
for k, w in enumerate(weights(r, s, J, plant)):
term = pmul(ppow(one_m, 2 * k), ppow(one_p, 2 * e - 2 * k))
out = [a + w * b for a, b in zip(out, term)]
if eps:
out = pmul(out, one_p)
return out
def closed_b(s, d, plant=None):
"""b_1..b_d of (c): b_(2n) = C(e, n) / (s + 3/2)_n, 0 at odd m."""
e = d // 2
lo = Fraction(2 * s + 3, 2) if plant != "low" else Fraction(2 * s + 1,
2)
out = [Fraction(0)] * d
for n in range(1, e + 1):
out[2 * n - 1] = Fraction(factorial(e), factorial(n) * factorial(
e - n)) / rising(lo, n)
return out
def l2_over_sqrtpi(s, b):
c = [Fraction(1)] + list(b)
return sum(c[i] * c[j] * bandscale.gram(s + i, s + j)
for i in range(len(c)) for j in range(len(c)))
def gamma_over_4s(s, e):
return Fraction(dfact(s) * factorial(e)) / (2 ** s * rising(
Fraction(2 * s + 3, 2), e))
if __name__ == "__main__":
t0 = time.time()
print("CONTROLS")
c_hand = [HALF, Fraction(0), HALF]
c_cl, c_gr = closed_cstar(3, 0, 1), gram_cstar(3, 0, 1)
b02 = bandscale.exact_b(0, 2)
check("POSITIVE: c* at rank 3, s = 0, J = 1 is (1 + x^2)/2, closed and "
"Gram; exact_b(0, 2) = (0, 2/3)", c_cl == c_hand and c_gr == c_hand
and list(b02) == [0, Fraction(2, 3)],
f"{[str(v) for v in c_cl]}, {[str(v) for v in c_gr]}, "
f"{[str(v) for v in b02]}")
cells = [(r, s, J) for r in RANKS for s in range(r - 1) for J in DEPTHS]
gram = {c: gram_cstar(*c) for c in cells}
print(f" {len(cells)} Gram solves, {time.time() - t0:.1f} s", flush=True)
e1 = [c for c in cells if (c[0] - 1 - c[1]) // 2 >= 1]
pu = sum(closed_cstar(*c, plant="unshift") != gram[c] for c in e1)
lcells = [(s, d) for s in LIMIT_S for d in LIMIT_D]
exb = {c: list(bandscale.exact_b(*c)) for c in lcells}
pl = sum(closed_b(*c, plant="low") != exb[c] for c in lcells
if c[1] >= 2)
check("PLANT: the unshifted kernel, and (s + 1/2)_n for (s + 3/2)_n, "
"each differ somewhere", pu > 0 and pl > 0,
f"{pu} of {len(e1)} and {pl} of "
f"{sum(c[1] >= 2 for c in lcells)} cells differ")
if not all(CHECKS):
print("CONTROL FAILED: nothing below is read.")
sys.exit(1)
print("THE FINITE FORM")
X = sum(closed_cstar(*c) != gram[c] for c in cells)
print(f" (1) exact: {len(cells) - X} of {len(cells)} cells agree")
nw = [weights(*c) for c in cells]
npos = sum(all(w > 0 for w in ws) for ws in nw)
print(f" (2) every w_k > 0 at {npos} of {len(cells)} cells; "
f"{sum(len(ws) for ws in nw)} weights")
for c in [(5, 0, 3), (8, 1, 10)]:
print(f" w at (r, s, J) = {c}: {[str(w) for w in weights(*c)]}")
print("THE LIMIT")
zb = sum(closed_b(*c) != exb[c] for c in lcells)
zl = sum(l2_over_sqrtpi(c[0], exb[c]) != gamma_over_4s(c[0], c[1] // 2)
for c in lcells)
Z = sum(closed_b(*c) != exb[c] or l2_over_sqrtpi(c[0], exb[c])
!= gamma_over_4s(c[0], c[1] // 2) for c in lcells)
print(f" (3) b_m: {len(lcells) - zb} of {len(lcells)} cells agree")
print(f" (4) int f*^2 / sqrt(pi) = gamma / 4^s: {len(lcells) - zl} of "
f"{len(lcells)} cells agree")
for c in [(2, 2), (2, 4), (5, 6)]:
print(f" b at (s, d) = {c}: {[str(v) for v in exb[c]]}; "
f"l2 {l2_over_sqrtpi(c[0], exb[c])}")
print(f"KILL cstar: {X} of {len(cells)} cells differ; limit: {Z} of "
f"{len(lcells)} cells differ")
check("the kill misses: c* and f* are the closed forms at every cell",
X + Z == 0, f"{X} and {Z} cells differ")
if not all(CHECKS):
print(f"\n{sum(CHECKS)}/{len(CHECKS)} checks FAILED")
sys.exit(1)
print(f"\n{sum(CHECKS)}/{len(CHECKS)} checks, {time.time() - t0:.1f} s")