"""Exact local checks. Run with Python 3.12 and sympy==1.14.0.

Analytic justifications and source comparisons are in the accompanying reports.
This code checks the displayed identities; it is not a formal proof of the paper.
No network, model calls, randomness or credential access is used.
"""
from pathlib import Path
import hashlib
import json
import platform
import sympy as s

HERE = Path(__file__).absolute().parent
SOURCE_HASH = '32e234b2ca7f7c67bb2f8b6ea4f5a29adc8facdccec63b78bb46e930b57668ad'
pdf = HERE/'paper.pdf'
if pdf.exists():
    assert hashlib.sha256(pdf.read_bytes()).hexdigest() == SOURCE_HASH

a, t = s.symbols('a t', nonnegative=True)
w = s.symbols('w', positive=True)
marginal = s.integrate(s.exp(-a)*(1-s.exp(-(3*a-w))), (a,w/3,s.oo))
joint = s.integrate((t-w)*s.exp(-t)/2, (t,w,s.oo))
assert s.simplify(marginal-s.Rational(3,4)*s.exp(-w/3)) == 0
assert s.simplify(joint-s.exp(-w)/2) == 0
# Check the boundary separately: SymPy leaves the nonnegative-symbol integral
# unevaluated at w=0, even though both elementary integrals converge there.
assert s.integrate(s.exp(-a)*(1-s.exp(-3*a)),(a,0,s.oo)) == marginal.subs(w,0)
assert s.integrate(t*s.exp(-t)/2,(t,0,s.oo)) == joint.subs(w,0)
M = s.simplify((marginal.subs(w,0)+marginal.subs(w,3*s.log(3)))/2)
J = s.simplify((joint.subs(w,0)+joint.subs(w,3*s.log(3)))/2)
assert M == s.Rational(1,2) and J == s.Rational(7,27)
assert J-M*M == s.Rational(1,108)
delta = s.symbols('delta',positive=True)
shifted_m = (marginal.subs(w,delta)+marginal.subs(w,delta+3*s.log(3)))/2
shifted_j = (joint.subs(w,delta)+joint.subs(w,delta+3*s.log(3)))/2
assert s.simplify(shifted_j/shifted_m**2-s.Rational(28,27)*s.exp(-delta/3)) == 0

c, z = s.symbols('c z',positive=True)
assert s.integrate(w*s.exp(-c*w),(w,0,s.oo)) == c**-2
assert s.integrate(z*s.exp(-z),(z,0,s.oo)) == 1
F = s.exp(-z)
assert s.diff(F,z,2)+2*s.diff(F,z)+F == 0

eps, theta = s.symbols('epsilon theta',positive=True)
mass = s.integrate(s.exp(-theta**2*t**2/2),(t,-eps/2,eps/2))
assert s.limit(mass/eps,eps,0,dir='+') == 1
assert s.limit(mass/(2*eps),eps,0,dir='+') == s.Rational(1,2)
n,k,alpha = s.symbols('n k alpha',positive=True)
factor = 2**(k/2)*alpha**k*(2*s.pi)**((n-k)/2)*(2*s.pi)**(-n/2)
assert s.simplify(factor/(alpha/s.sqrt(s.pi))**k) == 1
f = s.Function('c');u=z**s.Rational(-1,2)
rhs = u/(4*z)*(u*s.Subs(s.Derivative(f(t),(t,2)),t,u)-s.Subs(s.Derivative(f(t),t),t,u))
assert s.simplify(s.diff(z*f(u),z,2)-rhs) == 0

result = {
 'origin':'Exact symbolic checks supporting the analytic source adjudication',
 'python':platform.python_version(),'sympy':s.__version__,
 'script_sha256':hashlib.sha256(Path(__file__).read_bytes()).hexdigest(),
 'source_sha256':SOURCE_HASH,'source_pdf_bytes_checked_in_this_run':pdf.exists(),
 'counterexample':{'conditional_marginal':str(marginal),'conditional_joint':str(joint),
                   'marginal':str(M),'joint':str(J),'product':str(M*M),'violation':str(J-M*M)},
 'positive_mixing_shift':{'joint_over_product':'(28/27)*exp(-delta/3)'},
 'theorem_3_special_case':{'printed_mass':'2','corrected_mass':'1'},
 'theorem_4':{'radial_integral':'1','nonnegative_weighted_square_integral':'0'},
 'equation_8':{'left_side':'1','printed_limit':'1/2','corrected_limit':'1'},
 'lemma_14_corrected_prefactor':'(alpha/sqrt(pi))**k',
 'lemma_22_second_derivative_identity_checked':True,
 'qualification':'Analytic hypotheses, source inspection, and full-paper correctness are not mechanically certified.'}
(HERE/'local-checks.json').write_text(json.dumps(result,indent=2)+'\n')
print(json.dumps(result,indent=2))
