"""Independent character and real-diagonal checks for the certified grid.""" import argparse import json import math import random import subprocess import sys from concurrent.futures import ProcessPoolExecutor from pathlib import Path import mpmath as mp import numpy as np from flint import acb, acb_series, arb, ctx, dirichlet_char from certified_kernel_lower import accelerated_factors, enclosure from experiment_dyadic import primes_to from kernel_rows import certified_transform, grid, node, residue_data, row_path def samples(job): k,layer,start,stop = job ctx.prec = 144 mp.mp.dps = 38 size = 1 << (k-2) count = grid(k)[2] alpha = 1/mp.log(1 << k) re = alpha + (mp.mpf(1)/8-alpha)*layer/4 worst = 0. for j in range(start,stop): with np.load(row_path(k,layer,j)) as data: row,rad = data["centers"],float(data["radius"]) for signed in ((j,-j) if j else (0,)): s = mp.mpc(re,mp.mpf(5)*signed/count) rng = random.Random(k*100003+layer*1009+signed) # One complex character and one other character induced from # modulo 16, selected reproducibly; signs are checked separately. first = rng.choice([1,3]) for f in (first,rng.choice([i for i in range(4) if i!=first])): chi = [0]*16 for p in range(4): a = pow(5,p,16) phase = (mp.mpc(0,-1))**(f*p) chi[a],chi[16-a] = phase,-phase u = mp.mpf(1)/2+s val = mp.dirichlet(u,chi,1)/mp.dirichlet(u,chi)/(1+s) freq = f*(size//4) mid = row[freq] if signed>=0 else row[-freq%size].conjugate() err = float(abs(val-mp.mpc(complex(mid)))) if err > rad+1e-25: raise ArithmeticError(f"mpmath sample misses ball: {k} {layer} {signed} {f}") worst = max(worst,err) return worst def primitive_checks(k): n = 1 << k size = n//4 count = grid(k)[2] chi = next(c for a in range(3,64,2) if (c:=dirichlet_char(n,a)).parity()==1 and c.conductor()==n) freq = round(-size*float(chi(5).arg())/(2*math.pi))%size phase = acb(0,-2*arb.pi()*freq/size).exp() if not phase.overlaps(chi(5)): raise ArithmeticError("Character label mismatch") worst = 0. for layer,j in ((1,0),(3,count//2),(1,-count),(3,count)): s = node(k,layer,j) shift = acb_series([s+arb(1)/2,1],prec=2) native = shift.dirichlet_l(chi) val = native[1]/native[0]/(1+s) with np.load(row_path(k,layer,abs(j))) as data: row,rad = data["centers"],float(data["radius"]) mid = row[freq] if j>=0 else row[-freq%size].conjugate() disk = abs(val-acb(mid)) if not disk < arb(rad): raise ArithmeticError("Primitive character check failed") worst = max(worst,float(disk.upper())) return worst def real_comparison(k): """Real nodes in the old output which are also in the open Hardy strip.""" table = Path(__file__).with_name("二幂均方实验.txt").read_text(encoding="utf-8").splitlines() ref = [line.split() for line in table[2:] if int(line.split()[0])==k and line.split()[2]=="True"] origin = "archived table" if not ref: script = Path(__file__).with_name("experiment_dyadic.py") fresh = subprocess.check_output([sys.executable,"-B",str(script),"--levels",str(k), "--c","1.25"],text=True) ref = [line.split() for line in fresh.splitlines() if line.startswith(f"{k} 1.25 True ")] if len(ref)!=1: raise ArithmeticError("Missing independent real point") origin = "fresh run of original script" checks = [] for r in ref: n = 1 << k s = acb(arb(r[1])/arb(n).log()) gs = certified_transform(residue_data(k),n,s) moment = sum(abs(z)**2 for z in gs)/len(gs) primes = [(int(p),arb(int(p)).log()) for p in primes_to(8192)] d,m = accelerated_factors(2*s,n,primes,8192) gamma = (arb(3)/4-s/2).gamma()/(arb(3)/4+s/2).gamma() aii = (moment-d-m*gamma**2)/(1+s)**2 recovered = (aii*(1+s)**2).real old = arb(r[3])-arb(r[4]) # Both table columns display 9 decimals. The prime-tail column may # round to two significant digits; allow 10 percent for that rounding. allowance = arb("1.1")*arb(r[8])*arb(n).log()+arb("0.000000001") if not recovered.overlaps(old+arb(0,allowance.upper())): raise ArithmeticError("Old real-diagonal check failed") checks.append(dict(c=r[1],reference=origin,certified=enclosure(recovered,10),old=enclosure(old,10), old_tail_and_rounding=float(allowance))) return checks if __name__=="__main__": parser = argparse.ArgumentParser() parser.add_argument("--levels",type=int,nargs="+",default=[15,16,17,18]) parser.add_argument("--workers",type=int,default=6) args = parser.parse_args() ctx.prec=144 allchecks={} with ProcessPoolExecutor(args.workers) as pool: for k in args.levels: count = grid(k)[2] jobs=[(k,a,j,min(j+8,count+1)) for a in (1,3) for j in range(0,count+1,8)] maximum=max(pool.map(samples,jobs)) allchecks[k]=dict(mpmath_checks=4*(2*count+1),mpmath_max_abs=maximum, native_primitive_checks=4,native_primitive_max_abs=primitive_checks(k), real_diagonal=real_comparison(k)) print(k,allchecks[k],flush=True) Path(__file__).with_name("再生核独立核对.json").write_text( json.dumps(allchecks,indent=2,ensure_ascii=False),encoding="utf-8")