import sympy as sp
from fractions import Fraction as F
from scipy.optimize import brentq
import math

out = []
def ok(name, c): out.append((name, bool(c)))

# Omega numeral
ok("0.376412 - 47/125 = 103/250000", F(376412, 10**6) - F(47,125) == F(103, 250000))

# Selmer roots lambda_D^D = lambda_D + 1
lam = {D: brentq(lambda x: x**D - x - 1, 1.0, 2.0, xtol=1e-15) for D in range(2, 101)}
ok("lambda_9", abs(lam[9] - 1.085070245491451) < 1e-13)
ok("lambda_10", abs(lam[10] - 1.075766066086837) < 1e-13)
ok("lambda_11", abs(lam[11] - 1.068297188920841) < 1e-13)
ok("ratio D=10 = (lambda+1)^2 = 4.30880476111762", abs(lam[10]**20 - (lam[10]+1)**2) < 1e-12 and abs((lam[10]+1)**2 - 4.30880476111762) < 1e-11)
ratios = {D: (lam[D] + 1)**2 for D in lam}
ok("all ratios > 4, min at D=100 = 4.028011613090", min(ratios.values()) > 4 and min(ratios, key=ratios.get) == 100 and abs(ratios[100] - 4.028011613090) < 1e-9)
ok("lambda_2 = golden ratio, lambda_3 = plastic number", abs(lam[2] - (1+5**0.5)/2) < 1e-12 and abs(lam[3] - 1.324717957244746) < 1e-12)
x = sp.symbols('x')
ok("x^D - x - 1 irreducible over Q for D = 2..40", all(len(sp.factor_list(x**D - x - 1)[1]) == 1 and sp.factor_list(x**D - x - 1)[1][0][1] == 1 for D in range(2, 41)))

# Euler-Lagrange: sign of the rho dS/dt term (1D, flat)
t, X, a, kap = sp.symbols('t x a kappa')
rho = sp.Function('rho')(t, X); S = sp.Function('S')(t, X); U = sp.Function('U')
L = a*rho*sp.diff(S, t) - sp.Rational(1, 2)*rho*sp.diff(S, X)**2 - U(rho) - kap*sp.diff(rho, X)**2
EL_S = sp.euler_equations(L, [S], [t, X])[0].lhs
cont = sp.diff(rho, t) + sp.diff(rho*sp.diff(S, X), X)
ok("delta/deltaS = continuity iff a = -1", sp.simplify(EL_S.subs(a, -1) - cont) == 0 or sp.simplify(EL_S.subs(a, -1) + cont) == 0)
ok("a = +1 does not give continuity", sp.simplify(EL_S.subs(a, 1) - cont) != 0 and sp.simplify(EL_S.subs(a, 1) + cont) != 0)
EL_rho = sp.euler_equations(L.subs(a, -1), [rho], [t, X])[0].lhs
HJ_kappa = sp.diff(S, t) + sp.diff(S, X)**2/2 + sp.diff(U(rho), rho) - 2*kap*sp.diff(rho, X, 2)
ok("delta/deltarho gives dS/dt + |S'|^2/2 + U'(rho) - 2 kappa rho'' = 0", sp.simplify(EL_rho + HJ_kappa) == 0 or sp.simplify(EL_rho - HJ_kappa) == 0)

# Fisher term instead of kappa|grad rho|^2 -> Bohm potential -> Schrodinger (hbar = m = 1)
Lf = -rho*sp.diff(S, t) - sp.Rational(1, 2)*rho*sp.diff(S, X)**2 - U(rho) - sp.diff(rho, X)**2/(8*rho)
EL_rho_f = sp.euler_equations(Lf, [rho], [t, X])[0].lhs
Qbohm = -sp.Rational(1, 2)*sp.diff(sp.sqrt(rho), X, 2)/sp.sqrt(rho)
HJ_q = sp.diff(S, t) + sp.diff(S, X)**2/2 + sp.diff(U(rho), rho) + Qbohm
ok("Fisher term gives the quantum Hamilton-Jacobi equation", sp.simplify(EL_rho_f + HJ_q) == 0 or sp.simplify(EL_rho_f - HJ_q) == 0)
# Madelung: continuity + quantum HJ  <=>  i Psi_t = -Psi''/2 + U'(|Psi|^2) Psi
R = sp.sqrt(rho); Psi = R*sp.exp(sp.I*S)
Up = sp.Function('Up')(rho)   # stands for U'(rho)
lhs = sp.I*sp.diff(Psi, t) - (-sp.diff(Psi, X, 2)/2 + Up*Psi)
# impose the two equations: replace rho_t and S_t
rho_t = -sp.diff(rho*sp.diff(S, X), X)
S_t = -(sp.diff(S, X)**2/2 + Up + Qbohm)
expr = sp.simplify(sp.expand(lhs.subs({sp.Derivative(rho, t): rho_t, sp.Derivative(S, t): S_t}) / sp.exp(sp.I*S)))
ok("continuity + quantum HJ is the nonlinear Schrodinger equation", sp.simplify(expr) == 0)

# RG alpha sector at D = 10: stability
al, B = sp.symbols('alpha B', real=True)
beta = (4 - 10)*al - B*al**2
d = sp.diff(beta, al)
ok("alpha* = 0 attracts (beta' = -6), alpha* = -6/B repels (beta' = +6)", d.subs(al, 0) == -6 and sp.simplify(d.subs(al, -6/B)) == 6)
Dc = sp.symbols('D_c', positive=True)
beta2 = (Dc - 10)*al - B*al**2
ok("with D_c > 10: alpha* = (D_c-10)/B, beta' = -(D_c-10) < 0", sp.simplify(sp.solve(beta2, al)[1] - (Dc - 10)/B) == 0 and sp.simplify(sp.diff(beta2, al).subs(al, (Dc-10)/B) + (Dc - 10)) == 0)

# Grok appendix finite iterations (x0 = 1)
x50 = 0.376 + 0.624*0.9**50; x200 = 0.376 + 0.624*0.95**200
ok("eps=0.1, 50 steps: 0.3792159557293677", abs(x50 - 0.3792159557293677) < 1e-15)
ok("eps=0.05, 200 steps: 0.3760218728637392", abs(x200 - 0.3760218728637392) < 1e-15)

for n, c in out: print(("PASS " if c else "FAIL ") + n)
print(sum(c for _, c in out), "/", len(out))
