quad_check.py

back to table · edit · history · where entries came from · files · download

3637 bytes, as of the version from 2026-11-18 12:45 (current). Recorded here, not run.

"""Independent original-symbol two-angle quadrature; no recurrence coefficients.

Usage: sage -python quad_check.py
The Gauss quadrature errors are measured by order comparison and controls,
not certified by the arithmetic balls. high=True selects shells 1,2,6.
"""
import numberdb.sage
from sage.rings.real_arb import RealBallField
from sage.rings.real_mpfr import RealField
import numpy as np
from scipy.special import roots_legendre
import json,math,time,sys
from itertools import product

def rows():
 out=[]
 for L in ['sc','bcc','fcc']:
  for l in range(1,7):
   for m in range(l+1):
    for n in range(m+1):
     if L=='bcc' and not l%2==m%2==n%2:continue
     if L=='fcc' and (l+m+n)%2:continue
     d=l+m+n if L=='sc' else l if L=='bcc' else max(l,(l+m+n)//2)
     if d<=6:out.append((L,l,m,n,d))
 return sorted(out,key=lambda r:(r[4],['sc','bcc','fcc'].index(r[0]),r[1:4]))

def low(N):
 x,w=roots_legendre(N);x=(x+1)/2;w=w/2
 X,Y=np.meshgrid(x,x,indexing='ij');W=w[:,None]*w[None,:];h=math.pi/2;out={r[:4]:0.0 for r in rows()}
 for cu,cv,sw in product([0,1],repeat=3):
  U=(math.pi if cu else 0)+( -h if cu else h)*(X*Y if sw else X)
  V=(math.pi if cv else 0)+( -h if cv else h)*(X if sw else X*Y)
  C,D=np.cos(U),np.cos(V);weight=W*h*h*X/(math.pi**2)
  cp=[np.cos(k*U) for k in range(7)];cq=[np.cos(k*V) for k in range(7)]
  for L in ['sc','bcc','fcc']:
   a,b=(3-C-D,np.ones_like(C)) if L=='sc' else (np.ones_like(C),C*D) if L=='bcc' else (3-C*D,C+D)
   root=np.sqrt((a-b)*(a+b));rho=b/(a+root)
   for r in rows():
    if r[0]!=L:continue
    _,l,m,n,d=r
    out[r[:4]]+=float(np.sum(weight*(1-cp[l]*cq[m]*rho**n)/root))/(1 if L=='sc' else 4 if L=='bcc' else 2)
 return {','.join(map(str,k)):v for k,v in out.items()}

def gauss(N,F):
 # Newton roots in MPFR, independent of Arb generator constants.
 pts=[];pi=F.pi()
 for i in range(1,N+1):
  z=(pi*(F(i)-F(1)/4)/(F(N)+F(1)/2)).cos()
  for it in range(15):
   a,b=F(1),z
   for k in range(2,N+1):a,b=b,((2*k-1)*z*b-(k-1)*a)/k
   deriv=N*(z*b-a)/(z*z-1);delta=b/deriv;z-=delta
   if abs(delta)<F(2)**(-F.precision()+10):break
  assert it<14
  pts.append(((z+1)/2, 1/((1-z*z)*deriv*deriv)))
 return pts

def high(N):
 R=RealBallField(560);F=RealField(560);pts=[(R(x),R(w)) for x,w in gauss(N,F)];h=R.pi()/2;pi2=R.pi()**2
 chosen=[r for r in rows() if r[4] in (1,2,6)];out={r[:4]:R(0) for r in chosen};t=time.time()
 # One corner and orientation at a time keeps memory below the run cap.
 for cu,cv,sw in product([0,1],repeat=3):
  sums={r[:4]:R(0) for r in chosen}
  for x,wx in pts:
   for y,wy in pts:
    u=(R.pi() if cu else R(0))+(-h if cu else h)*(x*y if sw else x)
    v=(R.pi() if cv else R(0))+(-h if cv else h)*(x if sw else x*y)
    c,d=u.cos(),v.cos();cp=[R(1),c];cq=[R(1),d]
    for k in range(2,7):cp.append(2*c*cp[-1]-cp[-2]);cq.append(2*d*cq[-1]-cq[-2])
    weight=wx*wy*h*h*x/pi2
    for L in ['sc','bcc','fcc']:
     a,b=(3-c-d,R(1)) if L=='sc' else (R(1),c*d) if L=='bcc' else (3-c*d,c+d)
     root=((a-b)*(a+b)).sqrt();rho=b/(a+root);powers=[R(1)]
     for k in range(6):powers.append(powers[-1]*rho)
     fac=weight/root/(1 if L=='sc' else 4 if L=='bcc' else 2)
     for r in chosen:
      if r[0]==L:
       _,l,m,n,_=r;sums[r[:4]]+=fac*(1-cp[l]*cq[m]*powers[n])
  for k in sums:out[k]+=sums[k]
  print('progress',N,cu,cv,sw,'seconds',round(time.time()-t,1),flush=True)
 return {','.join(map(str,k)):str(v) for k,v in out.items()}

if __name__=='__main__':
 print('LOW64',json.dumps(low(64)),flush=True);print('LOW96',json.dumps(low(96)),flush=True)
 print('HIGH256',json.dumps(high(256)),flush=True)
 print('HIGH352',json.dumps(high(352)),flush=True)