import sympy as sp, numpy as np, math
from itertools import permutations
from sympy.combinatorics import Permutation
res = []
def ok(name, cond):
    res.append((name, bool(cond))); print(("PASS " if cond else "FAIL ") + name, flush=True)

# (c) identity map on S^3 of radius a: Skyrme energy density and charge, by exact evaluation at points
a, chi, th, ph = sp.symbols('a chi theta phi', real=True)
I2 = sp.eye(2); sx = sp.Matrix([[0, 1], [1, 0]]); sy = sp.Matrix([[0, -sp.I], [sp.I, 0]]); sz = sp.Matrix([[1, 0], [0, -1]])
n = [sp.sin(th)*sp.cos(ph), sp.sin(th)*sp.sin(ph), sp.cos(th)]
N = n[0]*sx + n[1]*sy + n[2]*sz
G = sp.cos(chi)*I2 + sp.I*sp.sin(chi)*N
Ginv = sp.cos(chi)*I2 - sp.I*sp.sin(chi)*N
Ls = [Ginv*sp.diff(G, q) for q in (chi, th, ph)]
hd = [a**2, a**2*sp.sin(chi)**2, a**2*sp.sin(chi)**2*sp.sin(th)**2]
e2 = -sp.Rational(1, 2)*sum(sp.trace(Ls[i]*Ls[i])/hd[i] for i in range(3))
e4 = -sp.Rational(1, 16)*sum(sp.trace((Ls[i]*Ls[j] - Ls[j]*Ls[i])**2)/(hd[i]*hd[j]) for i in range(3) for j in range(3))
fe = sp.lambdify((a, chi, th, ph), e2 + e4, 'numpy')
rng = np.random.default_rng(47)
pts = [(av, rng.uniform(0.2, 2.9), rng.uniform(0.2, 2.9), rng.uniform(0, 6.2)) for av in (0.5, 1.0, 1.7, 3.0) for _ in range(6)]
ok("identity map: energy density = 3/a^2 + 3/a^4 at 24 sample points", all(abs(complex(fe(*p)).imag) < 1e-12 and abs(complex(fe(*p)).real - (3/p[0]**2 + 3/p[0]**4)) < 1e-10 for p in pts))
A = sp.symbols('A', positive=True)
EA = sp.simplify((3/A**2 + 3/A**4)*2*sp.pi**2*A**3)      # uniform density x volume 2 pi^2 a^3
ok("E_Sk(a) = 6 pi^2 (a + 1/a), minimum 12 pi^2 at a = 1", sp.simplify(EA - 6*sp.pi**2*(A + 1/A)) == 0 and sp.solve(sp.diff(EA, A), A) == [1] and sp.simplify(EA.subs(A, 1) - 12*sp.pi**2) == 0)
bd = 0
for p in permutations(range(3)):
    bd += Permutation(list(p)).signature()*sp.trace(Ls[p[0]]*Ls[p[1]]*Ls[p[2]])
fb = sp.lambdify((chi, th, ph), -bd/(24*sp.pi**2), 'numpy')
# charge density for the identity map should be (1/2pi^2) sin^2(chi) sin(theta) up to sign
ok("charge density = +-(1/2 pi^2) sin^2 chi sin theta at 18 points", all(abs(abs(complex(fb(*p[1:])).real) - math.sin(p[1])**2*math.sin(p[2])/(2*math.pi**2)) < 1e-12 for p in pts[:18]))
ok("so |Q| = (1/2pi^2) * 2 pi^2 = 1", abs(sp.integrate(sp.sin(chi)**2, (chi, 0, sp.pi))*sp.integrate(sp.sin(th), (th, 0, sp.pi))*2*sp.pi/(2*sp.pi**2) - 1) == 0)

# (d) local Hamiltonian constraint on time-symmetric round S^3
Gn, al = sp.symbols('G alpha', positive=True)
sol = sp.solve(sp.Eq((6/A**2)/(16*sp.pi*Gn), al*(3/A**2 + 3/A**4)), A**2)
ok("R/(16 pi G) = alpha e  =>  a^2 = 8 pi alpha G/(1 - 8 pi alpha G)", len(sol) == 1 and sp.simplify(sol[0] - 8*sp.pi*al*Gn/(1 - 8*sp.pi*al*Gn)) == 0)

# (e) scalar curvature: round S^3, and the weak-field limit with a concrete test potential
def ricci_scalar(g, X):
    gi = g.inv(); d = len(X)
    Gam = [[[sp.simplify(sum(gi[i, l]*(sp.diff(g[l, j], X[k]) + sp.diff(g[l, k], X[j]) - sp.diff(g[j, k], X[l])) for l in range(d))/2)
             for k in range(d)] for j in range(d)] for i in range(d)]
    Rs = 0
    for j in range(d):
        for k in range(d):
            if gi[j, k] == 0: continue
            Rjk = sum(sp.diff(Gam[i][j][k], X[i]) - sp.diff(Gam[i][j][i], X[k])
                      + sum(Gam[i][i][l]*Gam[l][j][k] - Gam[i][k][l]*Gam[l][j][i] for l in range(d)) for i in range(d))
            Rs += gi[j, k]*Rjk
    return sp.simplify(Rs)
ok("round S^3 of radius a: R = 6/a^2", sp.simplify(ricci_scalar(sp.diag(a**2, a**2*sp.sin(chi)**2, a**2*sp.sin(chi)**2*sp.sin(th)**2), (chi, th, ph)) - 6/a**2) == 0)
x, y, z, eps = sp.symbols('x y z epsilon', real=True)
Phi = sp.sin(x)*sp.cos(2*y)*sp.exp(z/3) + x*y*z**2
gw = sp.diag(*[1 - 2*eps*Phi]*3)
Rw = ricci_scalar(gw, (x, y, z))
lap = sum(sp.diff(Phi, v, 2) for v in (x, y, z))
ok("weak field h = (1 - 2 Phi/c^2) delta: R = 4 lap Phi / c^2 + O(c^-4)", sp.simplify(sp.diff(Rw, eps).subs(eps, 0) - 4*lap) == 0 and sp.simplify(Rw.subs(eps, 0)) == 0)
# time-symmetric Hamiltonian constraint R = 16 pi G E/c^4 with E = m c^2 rho  =>  Poisson equation
mm, rr, cc = sp.symbols('m rho c', positive=True)
ok("4 lap Phi / c^2 = 16 pi G m rho / c^2  <=>  lap Phi = 4 pi G m rho", sp.simplify(sp.solve(sp.Eq(4*sp.Symbol('L')/cc**2, 16*sp.pi*Gn*mm*cc**2*rr/cc**4), sp.Symbol('L'))[0] - 4*sp.pi*Gn*mm*rr) == 0)

# (f) RG stability
g, Ac, dC, Dc, B, ell, sig, D0 = sp.symbols('g A d_C D_c B ell sigma D_0', real=True)
bg = (2 - dC)*g - Ac*g**2
ok("coherence: g* = (2 - d_C)/A, beta' = -(2 - d_C): stable iff d_C < 2", sp.simplify(sp.diff(bg, g).subs(g, (2 - dC)/Ac) + (2 - dC)) == 0)
ba = (Dc - 10)*g - B*g**2
ok("alpha at D = 10: alpha* = (D_c - 10)/B, beta' = -(D_c - 10): stable iff D_c > 10", sp.simplify(sp.diff(ba, g).subs(g, (Dc - 10)/B) + (Dc - 10)) == 0)
ok("displayed D_c = 4: alpha* = 0 attracts (beta'(0) = -6)", sp.diff((4 - 10)*g - B*g**2, g).subs(g, 0) == -6)
Dsol = 10 + (D0 - 10)*sp.exp(-sig*ell)
ok("D(l) = 10 + (D0 - 10) e^{-sigma l} solves dD/dl = sigma (10 - D)", sp.simplify(sp.diff(Dsol, ell) - sig*(10 - Dsol)) == 0)

print(sum(c for _, c in res), "/", len(res))
