#!/usr/bin/env python3
"""
PST Computation 5 — Stage 4 of the 10-foundational programme
============================================================================
Newton's constant from the spectral action principle, replacing the
Kaluza-Klein dimensional-reduction identity (which is meaningless once the
three compact spatial dimensions are gone in the 10-foundational reading).

The bosonic spectral action of a spectral triple is

    S = Tr f(D / Lambda),

f a positive cutoff function, Lambda a cutoff scale. Its heat-kernel
asymptotic expansion in 4 dimensions is (Chamseddine-Connes 1996;
Chamseddine-Connes-Marcolli 2007)

    S ~ 2 f_4 Lambda^4 a_0  +  2 f_2 Lambda^2 a_2  +  f_0 a_4  +  O(Lambda^-2),

with moments  f_2 = int_0^inf u f(u) du,  f_4 = int_0^inf u^3 f(u) du,
f_0 = f(0), and a_{2k} the Seeley-DeWitt coefficients of D^2. The terms are:

    Lambda^4 a_0  -> cosmological constant (+ volume),
    Lambda^2 a_2  -> EINSTEIN-HILBERT  (1/16 pi G) int R sqrt(g) d^4x,
    Lambda^0 a_4  -> Yang-Mills + Higgs + Weyl^2 (the SM bosonic sector).

So gravity is not put in by hand and not obtained by Kaluza-Klein reduction;
the Einstein-Hilbert term is the Lambda^2 coefficient of the universal
spectral action, with

    1 / (16 pi G)  =  (f_2 Lambda^2 / pi^2) * kappa_grav * N_dof,

kappa_grav an O(1) scheme constant (the exact CCM normalisation is
1/kappa_0^2 = (96 f_2 Lambda^2 - f_0 c)/(24 pi^2), kappa_0^2 = 8 pi G, c a
Yukawa trace), N_dof the fermionic multiplicity of the internal space.

What this script checks (order-of-magnitude / structural, not precision):
  M1  The cutoff moments f_0, f_2, f_4 are finite positive numbers for a
      sample f (here f(u) = e^{-u}): the cutoff supplies finite constants.
  M2  G ~ 1/(f_2 Lambda^2 N_dof): solving for Lambda given the observed G and
      the SM fermion count puts Lambda at a high (unification/Planck-ish)
      scale, the recognisable CCM result. Gravity's weakness = a high cutoff.
  M3  Dimensional consistency and the replacement of the KK identity.

Run:
    python3 computation_05.py
"""
import numpy as np
from math import gamma, pi, sqrt

SEP = "=" * 78
def hdr(s): print(f"\n{SEP}\n  {s}\n{SEP}")

print(SEP)
print("  PST Computation 5 — Newton's G from the spectral action (Stage 4)")
print(SEP)

# ─────────────────────────────────────────────────────────────────────
# §1. Cutoff moments are finite positive constants
# ─────────────────────────────────────────────────────────────────────
hdr("§1 — Cutoff moments f_0, f_2, f_4 (sample f(u) = e^{-u})")
print("""
  The spectral action needs a positive cutoff f with finite moments
      f_2 = int_0^inf u   f(u) du,
      f_4 = int_0^inf u^3 f(u) du,   f_0 = f(0).
  For f(u) = e^{-u}:  f_k = int_0^inf u^{k-1} e^{-u} du = Gamma(k), and
  f_0 = 1. These are the finite constants that set the action coefficients.
""")
# numeric integration as a cross-check on the closed forms Gamma(k)
_trapz = getattr(np, "trapezoid", getattr(np, "trapz", None))
u = np.linspace(0, 60, 600001)
f = np.exp(-u)
f2_num = _trapz(u * f, u)          # int u f
f4_num = _trapz(u**3 * f, u)       # int u^3 f
f0 = 1.0
print(f"  f_2 = int u e^-u du   = {f2_num:.5f}   (closed form Gamma(2) = {gamma(2):.5f})")
print(f"  f_4 = int u^3 e^-u du = {f4_num:.5f}   (closed form Gamma(4) = {gamma(4):.5f})")
print(f"  f_0 = f(0)            = {f0:.5f}")
print(f"  all finite and positive: {f2_num>0 and f4_num>0 and f0>0}")

# ─────────────────────────────────────────────────────────────────────
# §2. G ~ 1/(f_2 Lambda^2 N): the cutoff scale that reproduces gravity
# ─────────────────────────────────────────────────────────────────────
hdr("§2 — Solving 1/(16 pi G) ~ (f_2 Lambda^2/pi^2) kappa N  for Lambda")
print("""
  The Einstein-Hilbert coefficient gives M_P^2 = 1/G ~ f_2 Lambda^2 N_dof
  (up to O(1) scheme constants). Inverting: the cutoff Lambda that
  reproduces the observed Newton constant is

      Lambda ~ M_P / sqrt(kappa * N_dof).

  We use the reduced Planck mass and the CCM fermionic multiplicity of one
  generation embedded in the full count.
""")
M_P_reduced = 2.435e18    # GeV, reduced Planck mass (1/sqrt(8 pi G))
M_P_full    = 1.221e19    # GeV, Planck mass (1/sqrt(G))

# SM fermionic multiplicity entering the spectral action (CCM): per
# generation the finite Hilbert space is 32-complex-dim (incl. nu_R, L/R,
# particle/antiparticle); three generations give ~96.
for label, N in [("one generation (N=32)", 32), ("three generations (N=96)", 96)]:
    for kappa in (1.0, 96/(24*pi**2)):   # naive O(1), and CCM-like coefficient
        Lam_reduced = M_P_reduced / sqrt(kappa * N)
        Lam_full    = M_P_full    / sqrt(kappa * N)
        print(f"  {label}, kappa={kappa:.3f}:  "
              f"Lambda ~ {Lam_reduced:.2e} GeV (from M_P_red), "
              f"{Lam_full:.2e} GeV (from M_P)")
print("""
  Reading: across reasonable choices, Lambda lands in the 10^17 - 10^18 GeV
  band, i.e. a high unification/Planck-ish scale. This is the recognisable
  CCM outcome: gravity's weakness (M_P enormous) is the statement that the
  spectral-action cutoff Lambda is a high scale and the field content N is
  O(10-100). G is DERIVED from (Lambda, f, N), not posited and not obtained
  by Kaluza-Klein reduction.
""")

# ─────────────────────────────────────────────────────────────────────
# §3. Replacement of the Kaluza-Klein identity; what is calibration
# ─────────────────────────────────────────────────────────────────────
hdr("§3 — What this replaces, and what stays calibration-dependent")
print("""
  Replaced (dies in the 10-foundational reading):
    The current PST identity G = 6 pi d_0^2 / (c r_0^2) is a Kaluza-Klein
    dimensional-reduction relation. With no compact spatial dimensions, it
    has no referent. It is replaced by the spectral-action gravitational
    coefficient: Einstein-Hilbert is the Lambda^2 term of Tr f(D/Lambda),
    G ~ 1/(f_2 Lambda^2 N).

  Survives, re-expressed:
    PST currently states G as a consistency relation among (m_h, M_P, v).
    The spectral action keeps G a derived quantity, now a relation among
    (Lambda, f-moments, field content N). The PST modal scale
    M_* = 4 pi m_h ~ 1.573 TeV is the natural PST-specific candidate for a
    low cutoff; whether the gravitational Lambda is M_* or the high
    unification scale above is a calibration to be fixed (the spectral
    action's standard running-to-unification assumption). The d_0 length
    scale is re-read as the LG vacuum scale, not a compactification radius.

  Honest status of Stage 4:
    - STRUCTURE (rigorous, standard CCM): the spectral action yields
      cosmological + Einstein-Hilbert + Yang-Mills + Higgs + Weyl^2, with
      G the Lambda^2 gravitational coefficient. Gravity is derived, not KK-
      reduced. This is the correct replacement for the dying identity.
    - NUMBER (calibration-dependent): the precise G requires fixing f_2,
      f_0, Lambda and the exact CCM normalisation; the order of magnitude
      (Lambda high, M_P ~ Lambda sqrt(N)) is robust and checks out (§2).
    - This carries the same scheme-dependence as CCM itself; PST inherits
      it rather than introducing new freedom.
""")
