# [project] Program C (block 4): independent zeta(s) by Euler–Maclaurin in numpy + theta(t) from scipy's complex
# loggamma. No mpmath anywhere in this file; used to double-check mpmath (siegelz/zeta) in zt_gram.py and
# toy_mollifier.py. Valid for 0 < sigma < 2, t >= 1 with the N chosen below (checked against mpmath in those scripts).
import numpy as np
from scipy.special import loggamma
B2K = [1/6, -1/30, 1/42, -1/30, 5/66, -691/2730, 7/6, -3617/510]   # Bernoulli B_2..B_16
def theta(t):
    t = np.asarray(t, float)
    return np.imag(loggamma(0.25 + 0.5j*t)) - 0.5*t*np.log(np.pi)
def zeta(sigma, t, N=None):
    """zeta(sigma+it) for arrays t (sigma scalar), Euler–Maclaurin with N terms and 8 Bernoulli corrections."""
    t = np.asarray(t, float); s = sigma + 1j*t
    if N is None: N = int(np.max(np.abs(s))/np.pi) + 20
    n = np.arange(1, N, dtype=float)
    out = np.zeros_like(s)
    for chunk in np.array_split(np.arange(len(t)), max(1, len(t)//2000)):
        out[chunk] = np.exp(-np.outer(s[chunk], np.log(n))).sum(axis=1)
    out += N**(1-s)/(s-1) + 0.5*N**(-s)
    fact = 1.0; poch = s.copy()            # poch = s(s+1)...(s+2k-2)
    for k, b in enumerate(B2K, start=1):
        fact *= (2*k-1)*(2*k) if k > 1 else 2.0
        out += b/fact*poch*N**(-s-2*k+1)
        poch = poch*(s+2*k-1)*(s+2*k)
    return out
def Z(t):
    t = np.asarray(t, float)
    return np.real(np.exp(1j*theta(t))*zeta(0.5, t))
