#!/usr/bin/env python3
"""
Computation 72 -- Substrate spectral action: Higgs-quartic Taylor expansion
============================================================================
Attempts to close the Z^2 = e^{-1} conjecture (Computation 71) by directly
computing the Higgs-quartic coefficient in the Chamseddine-Connes spectral
action of the PST substrate triple at matched scaling Lambda = sqrt(D),
under explicit Higgs-perturbation models.

CONTEXT
-------
Comp 71 proposed:

    Z^2 := lambda_SM(M_*) / b(M_*) = S_sub / 2^D = e^{-1}    EXACTLY,

with the structural gap being the spectral-action Higgs-quartic identity
(Conjecture in Comp 71 docstring).  This computation carries out the
explicit symbolic Taylor expansion of the substrate spectral action
under a Higgs perturbation, extracts the phi^4 coefficient at matched
scaling, and identifies what fixing the substrate-Higgs inner-fluctuation
structure would close.

SETUP
-----
Substrate spectral triple:
    H = C^{2^D}, eigenvalues of D_sub are +/- sqrt(D), multiplicity 2^(D-1) each.

At matched scaling Lambda = sqrt(D):
    D_sub / Lambda has eigenvalues +/- 1 (at the Gaussian cutoff edge).

Higgs perturbation via Yukawa-like coupling y*phi:
    D_phi = D_sub + y * phi * Sigma_X
where Sigma_X is the chirality-flip operator on the substrate spinor.
Without loss of generality reduce to a single +/- mode pair:
    D_phi(2x2) = [[Lambda, y*phi], [y*phi, -Lambda]]
Eigenvalues: lambda_+/-(phi) = +/- sqrt(Lambda^2 + y^2 phi^2).

Bosonic spectral action with Gaussian cutoff f(x) = exp(-x^2):
    S(D_phi, Lambda, f) = Tr f(D_phi / Lambda)
                       = 2 * 2^(D-1) * f(sqrt(1 + y^2 phi^2 / Lambda^2))
                       = 2^D * f(sqrt(1 + alpha))    where alpha := y^2 phi^2 / Lambda^2

The Higgs effective potential at one loop is V_eff(phi) = S(D_phi, Lambda, f).

GOAL
----
Expand V_eff(phi) in powers of phi and extract the coefficient of phi^4
(the candidate Higgs-quartic coupling at the matched scaling).  Compare
against the predicted b(M_*) * (S_sub / 2^D) = (1/4) * e^{-1}.
"""

from __future__ import annotations
import math
import sympy as sp


def derive_higgs_quartic():
    """Symbolically derive the phi^4 coefficient of the substrate spectral action."""
    phi, y, Lambda = sp.symbols("phi y Lambda", real=True, positive=True)
    D = sp.symbols("D", integer=True, positive=True)

    # Eigenvalue magnitudes of D_phi at the (+/-) mode pair
    lam_squared = Lambda ** 2 + y ** 2 * phi ** 2
    lam_over_Lambda = sp.sqrt(lam_squared) / Lambda  # = sqrt(1 + y^2 phi^2 / Lambda^2)

    # Gaussian cutoff f(x) = exp(-x^2), apply at x = lam_over_Lambda
    f_val = sp.exp(-lam_over_Lambda ** 2)
    # f(sqrt(1 + alpha)) = f(1) * exp(-alpha) = e^{-1} exp(-y^2 phi^2 / Lambda^2)
    # equivalently: f(x)^2 evaluated at lambda_squared/Lambda^2

    # Per mode-pair the spectral action contribution is f(lam_+/Lambda) + f(lam_-/Lambda)
    # = 2 * f(lam_over_Lambda) (since f even)
    per_pair = 2 * f_val

    # Total spectral action: 2^(D-1) mode pairs, so S = 2^(D-1) * per_pair = 2^D * f_val
    S_total = 2 ** D * f_val

    # Taylor-expand S_total in phi
    S_taylor = sp.series(S_total, phi, 0, 6).removeO()

    print("Per-mode spectral-action density f(D_phi/Lambda):")
    print(f"  f(D_phi/Lambda) = exp(-1) * exp(-y^2 phi^2 / Lambda^2)")
    print(f"                  = exp(-1) * [1 - (y^2/Lambda^2) phi^2")
    print(f"                              + (1/2)(y^4/Lambda^4) phi^4 + O(phi^6)]")
    print()

    # Extract coefficient of phi^4 in S_total / 2^D
    s_density = S_total / 2 ** D  # = exp(-1) * exp(-y^2 phi^2 / Lambda^2)
    s_density_taylor = sp.series(s_density, phi, 0, 6).removeO()
    print(f"S_total / 2^D Taylor expansion in phi:")
    print(f"  {sp.simplify(s_density_taylor)}")
    print()

    # Extract coefficients
    c_phi0 = s_density_taylor.coeff(phi, 0)
    c_phi2 = s_density_taylor.coeff(phi, 2)
    c_phi4 = s_density_taylor.coeff(phi, 4)

    print(f"Coefficient of phi^0:  {sp.simplify(c_phi0)}     (= S_sub / 2^D unperturbed)")
    print(f"Coefficient of phi^2:  {sp.simplify(c_phi2)}     (Higgs mass squared, negative)")
    print(f"Coefficient of phi^4:  {sp.simplify(c_phi4)}     (Higgs quartic, positive)")
    print()

    # Substitute matched scaling Lambda = sqrt(D)
    c_phi4_matched = c_phi4.subs(Lambda, sp.sqrt(D))
    print(f"At matched scaling Lambda = sqrt(D):")
    print(f"  Coefficient of phi^4 = {sp.simplify(c_phi4_matched)}")
    print()

    # The candidate identification says:
    #   lambda_H(M_*) = b(M_*) * (S_sub / 2^D) * (some normalization)
    #               = (1/4) * exp(-1) * (some normalization)
    #
    # From the substrate spectral-action calculation we have:
    #   coefficient of phi^4 in S_total / 2^D at matched scaling = y^4 / (2 D^2) * exp(-1)
    #
    # For the identification to hold structurally as
    #   lambda_H(M_*) = b(M_*) * e^{-1} = (1/4) e^{-1},
    # we need (after appropriate normalization of phi and Yukawa coupling):
    #   y^4 / (2 D^2) = 1/4
    #   y^4 = D^2 / 2
    #   y = D^{1/2} / 2^{1/4} = Lambda / 2^{1/4}
    #
    # i.e. the substrate-Higgs Yukawa coupling must be y = Lambda / 2^{1/4} at matched scaling.

    print("=" * 100)
    print("  IDENTIFICATION CONDITION")
    print("=" * 100)
    print()
    print("  For the candidate identification")
    print("      lambda_H(M_*) = b(M_*) * (S_sub / 2^D) = (1/4) * exp(-1)")
    print("  to hold structurally as an identity between the spectral-action phi^4")
    print("  coefficient and the SM Higgs quartic at M_*, the substrate-Higgs Yukawa")
    print("  coupling must satisfy")
    print()
    print("      y^4 / (2 Lambda^4) = 1/4   <=>   y = Lambda / 2^{1/4} = Lambda * 0.8409")
    print()
    print("  In other words: at matched scaling Lambda = sqrt(D) = M_*, the substrate-Higgs")
    print("  Yukawa coupling is uniquely fixed by the identification at the value")
    print()
    print(f"      y / Lambda = 2^{{-1/4}} = {2 ** (-0.25):.6f}.")
    print()
    print("  If this Yukawa value is itself derived structurally from the substrate's")
    print("  internal algebra A_F = C + H + M_3(C) (via the Connes-Chamseddine inner")
    print("  fluctuation of D_sub by elements of A_F), then the identification closes")
    print("  STRUCTURALLY: Z^2 = e^{-1} would be exact.")
    print()
    print("  REMAINING STRUCTURAL CONTENT: derive y = Lambda * 2^{-1/4} as the unique")
    print("  Connes-Chamseddine inner-fluctuation Yukawa coupling for the substrate")
    print("  spectral triple at matched scaling.  This is a finite calculation in the")
    print("  spectral-triple framework (Chamseddine-Connes-Marcolli 2007), reducing")
    print("  the Z^2 conjecture from 'spectral-action identity' to 'inner-fluctuation")
    print("  Yukawa normalization at matched scaling'.")
    print()


def main():
    print("=" * 100)
    print("  Computation 72 -- Substrate spectral action: Higgs-quartic Taylor expansion")
    print("=" * 100)
    print()

    derive_higgs_quartic()

    print("=" * 100)
    print("  STATUS UPDATE ON CONJECTURE Z^2 = e^{-1}")
    print("=" * 100)
    print()
    print("  Before this computation: the Conjecture was 'the spectral-action Higgs-")
    print("  quartic identity', stated abstractly.")
    print()
    print("  After this computation: the Conjecture is reduced to a single scalar")
    print("  identity:")
    print()
    print("      y_substrate(M_*) = M_* * 2^{-1/4} = M_* * 0.8409")
    print()
    print("  where y_substrate(M_*) is the substrate-Higgs Yukawa coupling derived")
    print("  from the Chamseddine-Connes inner fluctuation of D_sub by an element of")
    print("  the internal algebra A_F = C + H + M_3(C).  This is a specific finite")
    print("  number that the spectral-triple framework either delivers or doesn't.")
    print()
    print("  The reduction is real progress: the open question is now a specific")
    print("  Yukawa-normalization calculation, not an open conjecture about an")
    print("  abstract identity.")


if __name__ == "__main__":
    main()
