#!/usr/bin/env python3
"""Independent E8 shell enumeration and completed Epstein FE checks."""
from itertools import product
from pathlib import Path
import json
import mpmath as m
R=Path(__file__).resolve().parent;m.mp.dps=80
counts={8:0,16:0,24:0};split=[]
for coordinates in [(-4,-2,0,2,4),(-3,-1,1,3)]:
 partial={8:0,16:0,24:0}
 for v in product(coordinates,repeat=8):
  n=sum(x*x for x in v)
  if n in counts and sum(v)%4==0:counts[n]+=1;partial[n]+=1
 split.append([partial[n] for n in [8,16,24]])
shells=[counts[n] for n in [8,16,24]];assert shells==[240,2160,6720]
def sigma3(n):return sum(d**3 for d in range(1,n+1) if n%d==0)
assert shells==[240*sigma3(n) for n in [1,2,3]]
def theta(t):return 1+240*sum(sigma3(n)*m.exp(-2*m.pi*n*t) for n in range(1,301))
def Z(s):return 240*m.power(2,-s)*m.zeta(s)*m.zeta(s-3)
def completed(s):return m.power(m.pi,-s)*m.gamma(s)*Z(s)
checks=[]
for s in [m.mpf('1.3')+m.mpf('.7')*1j,m.mpf('2')+m.mpf('1.1')*1j,m.mpf('3.2')+m.mpf('.4')*1j,m.mpf('4.7')+m.mpf('1.3')*1j]:
 a,b=completed(s),completed(4-s);error=abs(a-b);assert error<m.mpf('1e-70')
 checks.append(dict(s=str(s),left=str(a),reflected=str(b),absolute_error=str(error)))
# Independent theta-Mellin continuation at a non-central point.
s=m.mpf('1.3')+m.mpf('.7')*1j
integral=-1/s-1/(4-s)+m.quad(lambda t:(theta(t)-1)*(t**(s-1)+t**(3-s)),[1,2,4,8,16,32])
assert abs(integral-completed(s))<m.mpf('1e-70')
transform=[]
for t in [m.mpf('.7'),m.mpf('1'),m.mpf('1.4')]:
 error=abs(theta(t)-t**(-4)*theta(1/t));assert error<m.mpf('1e-70')
 transform.append(dict(t=str(t),theta=str(theta(t)),modular_error=str(error)))
centre=completed(2);assert abs(centre+m.mpf(5)/6)<m.mpf('1e-75')
# A change of exponent, not a coordinate rescaling of the lattice.
rescaled=[]
for u in [m.mpf('.27')+m.mpf('.18')*1j,m.mpf('.5')+m.mpf('.3')*1j]:
 error=abs(completed(4*u)-completed(4*(1-u)));assert error<m.mpf('1e-70')
 rescaled.append(dict(u=str(u),error=str(error)))
rec=dict(lattice='E8 = {v in Z8 or (Z+1/2)^8 : sum(v) even}; standard minimum squared norm 2',
 enumeration='Enumerated twice-coordinates, even entries -4,-2,0,2,4 or odd -3,-1,1,3; sum divisible by 4; norm squared 8,16,24',
 shell_squared_norms=[2,4,6],shell_counts=shells,integer_and_half_integer_counts=split,
 formula='Z_E8(s)=sum |v|^(-2s)=240*2^(-s)*zeta(s)*zeta(s-3)',completion='pi^(-s)*Gamma(s)*Z_E8(s)',
 functional_equation='Lambda_E8(s)=Lambda_E8(4-s)',centres={'Gaussian_Z2':'1/2','quaternion_Z4':'1','E8':'2','E8_rescaled':'1/2'},
 dimension_rule='For a self-dual n-dimensional lattice with |v|^(-2s), s maps to n/2-s and centre is n/4. A non-self-dual lattice pairs with its dual instead.',
 central_value_exact='-5/6',central_value=str(centre),functional_equation_checks=checks,theta_transform_checks=transform,
 independent_mellin_value=str(integral),mellin_factorization_error=str(abs(integral-completed(s))),rescaled_checks=rescaled,
 rescaling='Ztilde(u)=Z_E8(4u)=sum |v|^(-8u); completion Lambda_E8(4u), centre u=1/2',
 zero_warning='Changing the exponent does not put every E8 zero on that centre: Riemann nontrivial-zero families come from zeta(4u) and zeta(4u-3), conditionally Re(u)=1/8 and 7/8 under RH.',
 precision_decimal_digits=80,sources=['Viazovska arXiv:1603.04246, E8 definition and E4 expansion','Nebe, Lattices and modular forms, 2009, E8 theta example'])
(R/'e8-results.json').write_text(json.dumps(rec,indent=2)+'\n');print('PASS E8 shells',shells,'completed centre',centre,'FE checks',len(checks),flush=True)
