#!/usr/bin/env python3
"""
Computation 96 -- Bridge Premise (B): Polchinski-Wilsonian derivation
                  of claim (***) on the discrete substrate
======================================================================
Open-research item 1.1 (Bridge Premise B for the Z^2 closure).
Comp 95 reduced (B) to claim (***): the substrate Wilsonian RG flow
generator equals the Boltzmann partition function
exp(-beta_KO * H_Higgs(C)) on substrate configurations.

This computation attempts the Polchinski-Wilsonian derivation of (***)
adapted to the discrete substrate spectral triple.

THE STANDARD POLCHINSKI EXACT RG EQUATION
==========================================
For a continuum spectral triple with cutoff function K and Lagrangian L:
   d L / d log Lambda  =  (-1/2) Tr[(dK/d log Lambda) * G * delta^2 L / delta phi^2]
                          + (1/2) ((delta L / delta phi) * G * (delta L / delta phi))

where G is the propagator with cutoff K.  This delivers the
heat-kernel form for the RG flow generator in the continuum CC framework.

ADAPTATION TO THE DISCRETE SUBSTRATE
=====================================
The PST substrate has FINITE-DIMENSIONAL Hilbert space L^2(P(D), mu)
with the Walsh basis {chi_S} for S subset D.  The Polchinski continuum
RG equation does not directly apply because:

  - No continuous spectrum; modes are labelled by SUBSETS S
  - No heat-kernel asymptotic; Tr exp(-t D^2) collapses to f(1) exactly
    at the matched scaling Lambda = sqrt(D) (Comp 62)
  - "Integrating out modes" means averaging over the substrate measure mu

THE DISCRETE-SUBSTRATE WILSONIAN FLOW
======================================
On the substrate, the "RG flow" is the limit D -> infinity of the
configuration-space averaging.  At each finite D, the partition function
over configurations:

   Z_D(beta)  =  E_mu[exp(-beta * H_Higgs(C))]
              =  sum_{C subset D} 2^(-D) * exp(-beta * |C|/D)
              =  ((1 + exp(-beta/D)) / 2)^D

As D -> infinity:
   Z_D(beta)  ->  exp(beta * mu_site)  =  exp(-beta/2)

  (using mu_site = 1/2 and standard CLT/LLN argument)

At beta = beta_KO = 2:
   Z_infinity(2)  =  exp(-1).

THIS IS the Bernoulli MGF identification of Comp 88, structurally derived
from the per-site Boltzmann factor exp(-beta_KO * (1/D)) = exp(-2/D)
at matched scaling.

THE STRUCTURAL CLAIM (***)
==========================
Claim (***) states: the substrate Wilsonian RG flow generator equals
the Boltzmann partition function exp(-beta_KO * H_Higgs(C)).

Plausibility argument:
  (i) On the substrate, "integrating out modes" = averaging over mu.
  (ii) The averaging operator for a Hamiltonian H at inverse temperature
       beta is E_mu[exp(-beta H)] -- the Boltzmann partition function.
  (iii) For PST's KO-temperature beta_KO = 2 (Bott periodicity on KO_total
        = 10 mod 8) and H_Higgs = X_bar (Comp 89), this gives Z_H(beta_KO).

The heuristic argument supports (***); a rigorous Polchinski-style
proof requires:
  (a) Establishing the exact RG flow equation on the discrete substrate
      (not the Polchinski continuum equation)
  (b) Showing the unique solution at the matched scaling is the
      Boltzmann form
  (c) Identifying the cutoff-function evolution from Lambda = sqrt(D)
      to M_* with the asymptotic D -> infinity limit

REGION OF CONFIDENCE
====================
Heuristic / structural: STRONG plausibility (Comp 95 already noted this).
Rigorous theorem: would require a formal Polchinski-style derivation
for discrete spectra, which is non-trivial.

Comp 96 status:
  - Establishes the partition-function form structurally via the
    configuration-Hamiltonian / Boltzmann averaging
  - Identifies the missing rigorous content: an exact RG flow equation
    for the discrete substrate triple with the Polchinski uniqueness
    argument
  - Reduces (B) further: claim (***) is the partition-function ANSATZ;
    rigorising it requires the discrete-substrate Polchinski theorem

This is significant but PARTIAL progress.  The reduction is from
'undefined structural claim' (Comp 95) to 'Polchinski-style theorem for
discrete substrate triple' (Comp 96).  The remaining content is a known
problem in mathematical physics: Wilson-Polchinski RG on finite-
dimensional Hilbert spaces.
"""
import math


def main():
    print("=" * 100)
    print("  Computation 96 -- Polchinski-Wilsonian derivation of claim (***)")
    print("=" * 100)
    print()

    print("PARTITION FUNCTION FROM CONFIGURATION-HAMILTONIAN AVERAGING")
    print("-" * 100)
    print()
    beta_KO = 2.0
    print(f"  Per-configuration Boltzmann: P(C) * exp(-beta_KO * H_Higgs(C))")
    print(f"     P(C)  =  2^(-D)  (Bernoulli measure)")
    print(f"     H_Higgs(C)  =  X_bar(C)  =  |C|/D  (Comp 89, structural)")
    print(f"     beta_KO  =  2  (Comp 88, KO mod 8)")
    print()
    print(f"  Partition function:")
    print(f"     Z_D(beta_KO)  =  E_mu[exp(-2 X_bar)]")
    print(f"                  =  ((1 + exp(-2/D)) / 2)^D")
    print()
    print(f"  {'D':>8}  {'Z_D(beta_KO)':>15}  {'Diff vs e^-1':>15}")
    for D in [10, 100, 1000, 10000, 100000]:
        Z_D = ((1 + math.exp(-2 / D)) / 2) ** D
        diff = abs(Z_D - math.exp(-1))
        print(f"  {D:>8}  {Z_D:>15.8f}  {diff:>15.2e}")
    print()
    print(f"  Asymptotic D -> infinity: Z_infinity(beta_KO) = exp(-1) = {math.exp(-1):.6f}")
    print()

    print("THE HEURISTIC POLCHINSKI ARGUMENT FOR (***)")
    print("-" * 100)
    print()
    print("  In standard Polchinski-Wilsonian RG:")
    print("    - Integrating out modes contributes the Boltzmann partition function")
    print("      over those modes")
    print("    - At the matched-scaling limit, the dominant contribution is the")
    print("      ground-state Boltzmann factor exp(-beta * E_ground)")
    print()
    print("  On the discrete substrate:")
    print("    - 'Modes' are substrate configurations C in P(D)")
    print("    - 'Integrating out' = averaging over mu = (x)_a Bern(1/2)")
    print("    - 'Boltzmann factor' = exp(-beta * H(C)) per configuration")
    print()
    print("  Under this identification, the Wilsonian RG flow generator is:")
    print("    R  =  E_mu[exp(-beta_KO * H_Higgs(C))]  =  Z_H(beta_KO)")
    print()
    print("  This is the Boltzmann partition function on substrate configurations,")
    print("  with beta_KO = KO mod 8 = 2 (spectral-triple-inherent temperature) and")
    print("  H_Higgs = X_bar = empirical mean (structurally derived from additivity +")
    print("  matched scaling + uniformity).")
    print()
    print("  Therefore claim (***) reads structurally:")
    print("    R = Z_H(beta_KO) = e^(-1)  asymptotically.")
    print()

    print("RIGOROUS-PROOF GAP")
    print("-" * 100)
    print()
    print("  The heuristic argument above identifies (***) as structurally plausible.")
    print("  A fully rigorous theorem would require:")
    print()
    print("    (a) Formal Polchinski-style exact RG flow equation for the discrete")
    print("        substrate spectral triple (analogous to the continuum Polchinski 1984")
    print("        equation but adapted to finite-dimensional Hilbert space L^2(P(D), mu)).")
    print()
    print("    (b) Uniqueness theorem showing the Boltzmann form exp(-beta_KO * H_Higgs)")
    print("        is the unique solution at the matched scaling Lambda = sqrt(D).")
    print()
    print("    (c) Identification of the cutoff-function evolution from Lambda = sqrt(D)")
    print("        to M_* with the asymptotic D -> infinity limit.")
    print()
    print("  These are well-posed mathematical-physics tasks within the Polchinski")
    print("  RG framework adapted to discrete spectra.")
    print()

    print("STATUS")
    print("-" * 100)
    print()
    print("  Comp 96 partially closes claim (***) at the HEURISTIC / STRUCTURAL level:")
    print("    - The Boltzmann form is the natural partition function on substrate")
    print("      configurations.")
    print("    - The beta_KO = 2 and H_Higgs = X_bar identifications are structurally")
    print("      established (Comps 88, 89).")
    print("    - The asymptotic D -> infinity limit gives Z -> exp(-1) exactly.")
    print()
    print("  Rigorous theorem-level proof requires the Polchinski-discrete framework")
    print("  (items (a)-(c) above).  Comp 96 status: STRUCTURAL CONFIRMATION + IDENTIFIED")
    print("  rigorous-proof items.")


if __name__ == "__main__":
    main()
