#!/usr/bin/env python3
"""
SCOPE NOTE, 2026-10-11 (comp 202).  Where this file quotes lambda_SM(M_*) ~ 0.0927
(Buttazzo et al. 2013), the 0.8 percent agreement, the one-sigma Higgs match or comp 176's
0.4 sigma, that is the anchor of its day and is kept as found.  comp 202 computes the number
at three loops on current inputs: 0.0930 +- 0.0008 on the 2026 Review, 1.4 sigma from e^-1/4,
which is what the book states from v32.153 (v32.137 printed 0.0932 and 1.6 sigma on the 2024
Review).  Nothing here is re-run; the substrate side is unaffected.

PROVENANCE: NUMERICAL

Computation 174 -- the substrate regulator envelope, and what it does to C_H
============================================================================
THE DEBT THIS PAYS.  comp 173 closes the M_*/Z^2 system by reading b as an
output and finds b = 0.2313 +/- 0.0020 against P3's 0.25, a 9.5 sigma gap.  It
then declines to call that a falsification, on the ground that the coefficient
C_H multiplying M_*^2/(16 pi^2) depends on the SHAPE of the ultraviolet
regulator and not only on its scale, and that the book supplies a scale while
never specifying a shape.  It measures the sensitivity -- hard cutoff and
Gaussian both give kappa = 1, a smooth step 1.05 to 1.20, an exponential 2.00 --
and notes that closing the gap needs kappa = 1.081, inside that span.  It then
says, correctly, that the envelope is not free: sec:renorm already specifies the
substrate spectrum, so the profile is computable and PST owes the calculation.

This file does the calculation.

THE QUESTION THAT HAD TO BE SETTLED FIRST.  There are two cutoffs in this
theory and they are not the same number.  The seagull regulates 4-momentum at
M_*; the spectral action of eq:S_sub_ratio regulates the substrate Dirac
spectrum at the matched scaling Lambda = sqrt(D), which in physical units is
sqrt(D) M_*.  They differ by sqrt(D), which is astronomical.  Before any
envelope can be quoted one has to know which of the two the seagull answers to,
and the book never says.

It does not have to.  BOTH are computable from the substrate spectrum, and both
give kappa = 1.  That is the result, and it is stronger than picking one.

  Route A, at M_*.  sec:renorm gives the substrate mass spectrum as
  m_S = |S|^{1/2} M_* over the non-empty S, together with the zero mode, so it
  has a HARD GAP: nothing at all between 0 and M_*, and the first non-zero level
  sits exactly at M_* with degeneracy D.  (A convention carrying the Boolean
  Laplacian's eigenvalues as 2|S| rescales every level together and moves neither
  the gap nor its edge.)

  WHAT THAT STEP DOES AND DOES NOT REST ON, corrected on a referee pass of this
  file.  An earlier version said route A "needs only the gap".  That is false and
  the correction matters.  The EFT loop momentum is emergent 4-momentum on
  M = R x S^3, and the Standard Model lives BELOW M_*, so the EFT plainly has
  modes there while the substrate has none: the EFT's low-energy modes are
  collective, not substrate modes.  A gap in the substrate spectrum therefore
  says nothing DIRECTLY about how the emergent loop integral behaves near M_*.
  Going from "no substrate state below M_*" to "the EFT integral terminates
  sharply at M_*" requires IDENTIFYING the EFT's cutoff with the gap edge, and
  that identification is comp 105's layer factorisation, which the book carries
  as architecture rather than deriving from the Boolean Laplacian.  So route A
  rests on the gap PLUS comp 105.  kappa = 1 stands; the claim that it stood on
  the gap alone does not.  sec:renorm says so in as many words ("mass m_k = |S|^{1/2} M_*
  >= M_*"), and comp 105 gives the same architecture from the other side: the
  two layers occupy the disjoint momentum ranges [m_h^2, M_*^2] and [M_*^2, D]
  and meet at the single scale M_*, of measure zero in both.  An emergent loop
  that runs out of modes at a spectral gap edge is cut off by a step function.
  There is no envelope to choose.  kappa = 1, with no D-dependence, GIVEN
  comp 105's layer boundary; see the correction below.

  Route B, at Lambda = sqrt(D) M_*.  Take the whole Walsh ladder to furnish the
  regulator instead, which is what the spectral action does.  The comparison is
  the one comp 62 already makes, mode against mode at the matched scaling A1,
  with 1/lambda in place of the spectral action's f(lambda/Lambda): the substrate
  has no continuum momentum of its own, its Walsh modes ARE the degrees of
  freedom, and the two mode sets are matched in cardinality and in spectral range
  by A1.  Then the envelope IS computable and it is not a step: the substrate's
  density of states is binomial, C(D,j) at k^2 = j M_*^2, against the 4D
  continuum's quadratic rho(j) = 2j/D^2.  Those two distributions look nothing
  alike -- one is peaked at D/2, the other at the endpoint.  The seagull does not
  see the distribution, it sees its first inverse moment, and there they agree:

      <1/j>_continuum = 2/D exactly,   <1/j>_binomial = (2/D)(1 + 1/D + ...)

  so kappa_sub(D) = 1 + 1/D + O(1/D^2).  Part 3 computes it exactly.

  Note which route carries which premise.  Route B rests on A1, the matched
  scaling, which sec:open-structural already carries as architectural input
  rather than a P1-P3 consequence.  Route A rests on something different but not
  on nothing: the spectral gap, plus comp 105's identification of the layer
  boundary at M_* with the EFT's cutoff.  It does not need A1's matched scaling,
  which fixes the ladder's TOP at sqrt(D) M_*, and route A uses only its BOTTOM.
  So the two routes lean on different premises and agree, which is the point;
  neither is premise-free, and an earlier version of this file wrongly said route
  A was.

WHAT PART 4 ADDS, AND IT IS THE PART THAT HELPS PST.  Invert the closed form
kappa(p) = Gamma(1 + 2/p) for the family f(x) = exp(-x^p) and ask which p the
substrate envelope corresponds to.  The answer is p -> 2, with

      (2 - p_eff) * D  ->  2/(1 - gamma_E) = 4.72975...

that is, THE SUBSTRATE ENVELOPE IS THE GAUSSIAN, to O(1/D).  eq:S_sub_ratio
already uses f(x) = e^{-x^2} and the surrounding text defends the choice only by
noting that the value e^{-1} is robust across the whole family f(x) = e^{-x^p}
at the matched edge x = 1 -- which is true, but true only because the substrate
Dirac spectrum is DEGENERATE at +/- sqrt(D), so every mode sits at x = 1 and no
shape information survives.  That robustness argument cannot pin p, and the book
never claimed it did.  The mode sum can and does.  So the Gaussian stops being a
convenient choice and becomes a derived one.

WHAT PART 5 TAKES AWAY.  kappa = 1.081 corresponds to p = 1.7178, and to
kappa_sub(D) at D = 16.  The substrate cardinality is |D| = V/a_n^4 with site
spacing a_n -> 0, larger by dozens of orders of magnitude; comp 123 already
rejects a finite-D escape on exactly this ground, and it only needed D = 63
there.  So the shape freedom comp 173 invoked is not available, and comp 173's
"why that is not yet a falsification" must be replaced by this file's answer:
the envelope has been computed, it gives kappa = 1 by both routes, and the
9.5 sigma gap is real at that significance.

WHAT SURVIVES, AND WHY THIS IS NOT A FALSIFICATION OF PST.  The 9.5 sigma is a
statement about ONE of the two readings sec:ch-normalisation lays out -- the one
that feeds the canonical Standard-Model coupling into eq:ch-exact.  The other
reading, lambda_PST = 1/4 canonical, gives M_* = 1285 GeV and comp 123's 1.0
sigma Higgs match, and is untouched by anything here.  What is now excluded is
having both at once.  The whole of the remaining tension therefore localises to
the single question sec:ch-normalisation already asks, and no longer leaks into
a regulator ambiguity that was never really there.  The open register loses an
escape hatch and gains a derived regulator shape.
"""

import math
import sys
from fractions import Fraction

GAMMA_E = 0.5772156649015328606

# kappa = 1.081 is the value comp 173 reports as needed to close the b gap.
KAPPA_NEEDED = 1.081


def rule():
    print("=" * 100)


# ---------------------------------------------------------------------------
# Part 1.  The spectral gap: why route A needs no envelope at all.
# ---------------------------------------------------------------------------
def part1():
    rule()
    print("  1.  THE SUBSTRATE SPECTRAL GAP AT M_*  (route A)")
    rule()
    print("      sec:renorm mass spectrum on P([D]):  m_S^2 = |S| M_*^2, multiplicity C(D,|S|).")
    print()
    print(f"      {'D':>6}{'levels in (0, M_*^2)':>24}{'smallest non-zero':>22}{'its degeneracy':>17}")
    ok = True
    for D in (4, 8, 16, 32, 64):
        levels = sorted({j for j in range(D + 1)})          # eigenvalues in units of M_*^2
        inside = [j for j in levels if 0 < j < 1]
        lowest = min(j for j in levels if j > 0)
        deg = math.comb(D, lowest)
        ok = ok and not inside and lowest == 1 and deg == D
        print(f"      {D:>6}{len(inside):>24}{lowest:>22}{deg:>17}")
    print()
    print("      The spectrum is integer-valued in units of M_*^2, so (0, M_*^2) is empty")
    print("      by construction and the first level sits exactly AT M_*, degeneracy D.")
    print("      An emergent loop that runs out of modes at a gap edge is cut off by a step.")
    print(f"      -> route A gives kappa = 1, no D-dependence           : {ok}")
    print("      (exact GIVEN comp 105's layer boundary; the gap alone does not")
    print("       truncate an emergent loop, see the docstring)")
    print()
    return ok


# ---------------------------------------------------------------------------
# Part 2.  What kappa means, and the closed form for the exp(-x^p) family.
# ---------------------------------------------------------------------------
def kappa_of_p(p):
    """f(x) = exp(-x^p)  =>  kappa = 2 int_0^inf x f(x) dx = Gamma(1 + 2/p)."""
    return math.gamma(1.0 + 2.0 / p)


def kappa_quadrature(f, xmax=60.0, n=600001):
    """kappa = 2 int_0^xmax x f(x) dx by Simpson.

    Simpson is exact on a polynomial, so a hard cutoff is integrated over
    [0, 1] rather than straddling its own discontinuity with a node weight.
    """
    h = xmax / (n - 1)
    tot = 0.0
    for i in range(n):
        x = i * h
        w = 1 if i in (0, n - 1) else (4 if i % 2 else 2)
        tot += w * x * f(x)
    return 2.0 * tot * h / 3.0


def part2():
    rule()
    print("  2.  THE ENVELOPE FUNCTIONAL, AND THE CLOSED FORM")
    rule()
    print("      delta m_h^2 = C_H kappa Lambda^2/(16 pi^2),   kappa = 2 int_0^inf x f(x) dx.")
    print("      A hard cutoff f = theta(1-x) gives kappa = 1 by definition.")
    print()
    hard = kappa_quadrature(lambda x: 1.0, xmax=1.0, n=101)
    print(f"      hard cutoff, by quadrature over its support       : {hard:.10f}")
    print()
    print(f"      {'p':>10}{'Gamma(1+2/p)':>18}{'quadrature':>16}{'agree':>8}")
    ok = abs(hard - 1.0) < 1e-12
    for p in (1.0, 1.5, 2.0, 3.0, 6.0):
        a = kappa_of_p(p)
        b = kappa_quadrature(lambda x, p=p: math.exp(-(x ** p)))
        agree = abs(a - b) < 1e-6
        ok = ok and agree
        print(f"      {p:>10.2f}{a:>18.8f}{b:>16.8f}{str(agree):>8}")
    print()
    print("      p = 2 (Gaussian) -> 1.000 and p = 1 (exponential) -> 2.000 reproduce")
    print("      the two ends comp 173 measured, so the closed form is the same functional.")
    print(f"      -> kappa(p) = Gamma(1 + 2/p) verified                  : {ok}")
    print()
    return ok


# ---------------------------------------------------------------------------
# Part 3.  The mode-sum envelope: the calculation the book owes.
# ---------------------------------------------------------------------------
def kappa_sub_exact(D):
    """(D / 2^{D+1}) sum_{j=1}^{D} C(D,j)/j, in exact rational arithmetic."""
    S = sum(Fraction(math.comb(D, j), j) for j in range(1, D + 1))
    return Fraction(D, 2 * 2 ** D) * S


def kappa_sub(D):
    """Same quantity, via log-binomial weights; stable to large D."""
    lg = math.lgamma
    ln2 = math.log(2.0)
    tot = 0.0
    for j in range(1, D + 1):
        lp = lg(D + 1) - lg(j + 1) - lg(D - j + 1) - D * ln2
        tot += math.exp(lp) / j
    return 0.5 * D * tot


def part3():
    rule()
    print("  3.  THE MODE-SUM ENVELOPE  (route B, at Lambda = sqrt(D) M_*)")
    rule()
    print("      Substrate density of states: C(D,j) at k^2 = j M_*^2, j = 1..D.")
    print("      4D continuum density over the same range [0, D]: rho(j) = 2j/D^2.")
    print("      The seagull integrand is 1/k^2, so only the FIRST INVERSE MOMENT enters.")
    print()
    cont = "2/D exactly"
    print(f"      continuum   <1/j> = int_0^D (1/j)(2j/D^2) dj = {cont}")
    print("      substrate   <1/j> = sum_j C(D,j)/(j 2^D)")
    print()
    print(f"      kappa_sub(D) = (D/2) <1/j>_substrate")
    print()

    # exact rational check against the stable float route
    exact_ok = True
    for D in (4, 8, 12, 20, 30):
        a = float(kappa_sub_exact(D))
        b = kappa_sub(D)
        exact_ok = exact_ok and abs(a - b) < 1e-12
    print(f"      exact rational arithmetic agrees with the log-weight route : {exact_ok}")
    print()
    print(f"      {'D':>8}{'kappa_sub':>18}{'(kappa_sub - 1) * D':>24}")
    trend = []
    for D in (8, 16, 32, 64, 128, 256, 1024, 4096, 16384, 65536):
        k = kappa_sub(D)
        trend.append((D, (k - 1.0) * D))
        print(f"      {D:>8}{k:>18.10f}{(k - 1.0) * D:>24.8f}")
    print()
    converged = abs(trend[-1][1] - 1.0) < 1e-3 and all(
        trend[i][1] > trend[i + 1][1] for i in range(4, len(trend) - 1))
    print("      (kappa_sub - 1) * D -> 1, so kappa_sub(D) = 1 + 1/D + O(1/D^2).")
    print("      The binomial and the quadratic densities share their first inverse")
    print("      moment to leading order despite looking nothing alike: the binomial")
    print("      concentrates at D/2 and <1/j> -> 2/D, which is the continuum value.")
    print(f"      -> route B gives kappa = 1 + 1/D                       : {converged}")
    print()
    print("      The comparison is comp 62's, mode against mode at the matched scaling")
    print("      A1, with 1/lambda in place of f(lambda/Lambda).  So route B carries A1;")
    print("      route A rests on the gap PLUS comp 105's layer boundary, not on")
    print("      the gap alone.  It does not need A1, which fixes the ladder top.")
    print()
    print("      The reference cutoff is placed at sqrt(D) M_*, the spectrum's own")
    print("      endpoint, not at a chosen scale; kappa scales linearly in that")
    print("      placement, so the statement is 'against a sharp cutoff spanning the")
    print("      same spectral range', which is the only placement the spectrum offers.")
    print()
    return exact_ok and converged


# ---------------------------------------------------------------------------
# Part 4.  Which shape is that?  The Gaussian, derived.
# ---------------------------------------------------------------------------
def p_of_kappa(k, lo=1.02, hi=40.0):
    """Invert kappa = Gamma(1 + 2/p) on the branch p < 2 <-> kappa > 1 and its mirror."""
    if k >= 1.0:
        lo, hi = 1.0000001, 2.0
    else:
        lo, hi = 2.0, 4.0
    for _ in range(300):
        mid = 0.5 * (lo + hi)
        if (kappa_of_p(mid) - k) * (kappa_of_p(lo) - k) <= 0:
            hi = mid
        else:
            lo = mid
    return 0.5 * (lo + hi)


def part4():
    rule()
    print("  4.  THE EFFECTIVE SHAPE: THE GAUSSIAN, DERIVED RATHER THAN CHOSEN")
    rule()
    print("      Solve Gamma(1 + 2/p) = kappa_sub(D) for p.")
    print()
    print(f"      {'D':>8}{'kappa_sub':>16}{'p_eff':>16}{'(2 - p_eff) * D':>20}")
    tail = []
    for D in (16, 64, 256, 1024, 4096, 16384, 65536):
        k = kappa_sub(D)
        p = p_of_kappa(k)
        tail.append((2.0 - p) * D)
        print(f"      {D:>8}{k:>16.10f}{p:>16.10f}{(2.0 - p) * D:>20.8f}")
    target = 2.0 / (1.0 - GAMMA_E)
    print()
    print(f"      predicted limit 2/(1 - gamma_E) = {target:.8f}")
    ok = abs(tail[-1] - target) < 5e-3
    print(f"      measured at D = 65536          = {tail[-1]:.8f}")
    print(f"      -> p_eff -> 2: the envelope IS the Gaussian               : {ok}")
    print()
    print("      eq:S_sub_ratio already uses f(x) = e^{-x^2}.  Its stated defence is")
    print("      that e^{-1} is robust across f(x) = e^{-x^p} for every p, which is")
    print("      true and carries no shape information: the substrate Dirac spectrum")
    print("      is degenerate at +/- sqrt(D), so every mode sits at x = 1 and f(1)")
    print("      is all that is ever evaluated.  The mode sum does carry it, and picks")
    print("      out p = 2.  The Gaussian was the right choice for a reason the book")
    print("      had not given.")
    print()
    return ok


# ---------------------------------------------------------------------------
# Part 5.  The verdict on comp 173's shape defence.
# ---------------------------------------------------------------------------
def part5():
    rule()
    print("  5.  VERDICT ON THE SHAPE DEFENCE")
    rule()
    p_need = p_of_kappa(KAPPA_NEEDED)
    print(f"      comp 173 needs kappa = {KAPPA_NEEDED} to bring b from 0.2313 to 0.25.")
    print(f"      In the exp(-x^p) family that is p = {p_need:.6f}.")
    print()
    # descending branch crossing
    cross = None
    prev = None
    for D in range(8, 400):
        k = kappa_sub(D)
        if prev is not None and prev >= KAPPA_NEEDED > k:
            cross = D
            break
        prev = k
    print(f"      kappa_sub(D) falls through {KAPPA_NEEDED} at D = {cross}"
          f"  (kappa_sub({cross - 1}) = {kappa_sub(cross - 1):.6f},"
          f" kappa_sub({cross}) = {kappa_sub(cross):.6f})")
    print()
    print("      The substrate cardinality is |D| = V/a_n^4 with site spacing a_n -> 0,")
    print("      larger than that by dozens of orders of magnitude.  comp 123 already")
    print("      rejects a finite-D escape on this exact ground and there it only")
    print(f"      needed D = 63; here it would need D = {cross}.")
    print()
    print("      Route A: kappa = 1 exactly, from the spectral gap at M_*.")
    print(f"      Route B: kappa = 1 + 1/D -> 1, from the mode sum at sqrt(D) M_*.")
    print("      The two cutoffs differ by sqrt(D) and it does not matter which one")
    print("      the seagull answers to: the coefficient is 1 either way.")
    ok = cross is not None and cross < 100
    print(f"      -> the shape freedom comp 173 invoked is NOT available     : {ok}")
    print()
    return ok


# ---------------------------------------------------------------------------
# Part 6.  Tamper test: would this machinery notice a kappa that was not 1?
# ---------------------------------------------------------------------------
def part6():
    rule()
    print("  6.  TAMPER TEST")
    rule()
    # Feed the same functional a density of states that is deliberately NOT binomial:
    # a uniform density over j = 1..D has <1/j> = H_D/D, and kappa = H_D/2, which
    # diverges logarithmically.  If part 3 returned 1 for this too, it would be
    # measuring nothing.
    def kappa_uniform(D):
        return 0.5 * sum(1.0 / j for j in range(1, D + 1))

    print(f"      {'D':>8}{'kappa_sub (binomial)':>24}{'kappa (uniform DOS)':>24}")
    for D in (64, 1024, 16384):
        print(f"      {D:>8}{kappa_sub(D):>24.8f}{kappa_uniform(D):>24.8f}")
    moved = kappa_uniform(16384) > 4.0 and abs(kappa_sub(16384) - 1.0) < 1e-3
    print()
    print("      A uniform density of states over the same range gives kappa = H_D/2,")
    print("      which grows without bound.  The functional is sensitive to the density;")
    print("      the binomial's answer of 1 is a property of the substrate, not of the")
    print("      arithmetic.")
    print(f"      -> the test can fail, and does on a wrong density        : {moved}")
    print()
    return moved


def main():
    print()
    rule()
    print("  COMPUTATION 174.  The substrate regulator envelope, and what it does to C_H")
    rule()
    print("""
  comp 173 declined to call its 9.5 sigma gap a falsification because C_H
  depends on the regulator's shape and the book supplies only a scale.  It then
  said the envelope is computable from the substrate spectrum, and owed.  This
  file computes it, by both of the two cutoffs the theory contains, and gets
  kappa = 1 from each.
""")
    r1 = part1()
    r2 = part2()
    r3 = part3()
    r4 = part4()
    r5 = part5()
    r6 = part6()

    rule()
    print("  ASSESSMENT")
    rule()
    print(f"  1. spectral gap at M_*, first level exactly at M_*        : {r1}")
    print(f"  2. kappa(p) = Gamma(1 + 2/p) closed form verified          : {r2}")
    print(f"  3. mode-sum envelope kappa_sub(D) = 1 + 1/D               : {r3}")
    print(f"  4. effective shape p_eff -> 2, i.e. the Gaussian           : {r4}")
    print(f"  5. kappa = 1.081 needs D ~ 16, excluded                    : {r5}")
    print(f"  6. tamper test: a wrong density of states moves kappa      : {r6}")
    print()
    print("""  RESULT.  The envelope is computed and it is kappa = 1, by two independent
  routes at two cutoffs that differ by sqrt(D).  At M_* the substrate has a hard
  spectral gap, so the seagull's cutoff is a step and there is no envelope to
  choose.  At sqrt(D) M_* the Walsh ladder does furnish an envelope, and the
  binomial density of states reproduces the 4D continuum's first inverse moment
  to 1 + 1/D; inverting the closed form Gamma(1 + 2/p) identifies that envelope
  as the Gaussian, with (2 - p_eff) D -> 2/(1 - gamma_E).

  TWO CONSEQUENCES, ONE IN EACH DIRECTION.

  For PST: eq:S_sub_ratio's f(x) = e^{-x^2} is no longer a convenience.  The
  robustness argument the book gives for it carries no shape information at all,
  because the substrate Dirac spectrum is degenerate and only f(1) is ever
  evaluated.  The mode sum supplies what that argument could not, and selects
  p = 2.  A regulator shape moves from assumed to derived.

  Against comp 173: its part 4 shape defence is withdrawn.  kappa = 1.081 is not
  available at any D the theory can supply -- it would need D ~ 16, against a
  substrate cardinality V/a_n^4 -- so the b = 0.2313 vs 0.25 gap stands at the
  9.5 sigma comp 173 measured.

  WHAT THAT DOES NOT TOUCH.  The 9.5 sigma is a statement about the reading that
  feeds the canonical Standard-Model coupling into eq:ch-exact.  The reading in
  which lambda_PST = 1/4 is the canonical coupling gives M_* = 1285 GeV and comp
  123's 1.0 sigma Higgs match, and nothing here bears on it.  What is excluded is
  holding both at once.  The remaining tension therefore sits entirely in the one
  question sec:ch-normalisation asks, and no longer leaks into a regulator
  ambiguity: the register loses an escape hatch and gains a derived shape.""")
    print()
    return all([r1, r2, r3, r4, r5, r6])


if __name__ == "__main__":
    # Exit only on FAILURE.  The website runs these in the browser under Pyodide,
    # where SystemExit is fatal even at status 0: it tears the runtime down, and the
    # page prints ERROR after output that was in fact correct.  Falling off the end
    # hands the build gates the same status 0, and a failure still exits 1.
    if not main():
        sys.exit(1)
