"""Rational trigonometry, dyadic transfer bounds, and a polynomial enclosure.""" from fractions import Fraction from math import ceil, factorial F = Fraction COS_SCALE = 10**8 PI_UP = F(314159265359, 10**11) LOG2_UP = F(69315, 100000) GAMMA = F(2001, 5000) def cosine_table(n): """Integers within 2 units of 10^8 cos(pi*j/n), for 0 <= j < 2n.""" def atan_bounds(x): partial = sum((-1)**j * x**(2*j+1)/(2*j+1) for j in range(14)) return partial, partial + x**29/29 lo5, hi5 = atan_bounds(F(1, 5)) lo239, hi239 = atan_bounds(F(1, 239)) assert PI_UP - F(1, 10**11) < 16*lo5 - 4*hi239 assert 16*hi5 - 4*lo239 < PI_UP quadrant = [] for j in range(n//2 + 1): x = PI_UP*j/n term = total = F(1) for k in range(1, 10): term *= -x*x / ((2*k-1)*(2*k)) total += term assert abs(term)*x*x/(19*20) < F(1, 10**12) quadrant.append(int(total * COS_SCALE)) upper = quadrant + [-v for v in quadrant[-2::-1]] return upper + upper[-2:0:-1] def log2_bound(): x = F(1, 3) partial = sum(x**(2*j+1)/(2*j+1) for j in range(8)) assert 2*(partial + x**17/(17*(1-x*x))) < LOG2_UP def scalar_exp_weights(): """Upper bound for 10^7 exp(k/10000)/2, indexed by sign and |k|.""" scale = 10**22 result = {} for sign in (1, -1): x = F(sign, 10000) taylor = sum(x**j/factorial(j) for j in range(4)) step = ceil((taylor + 2*x**4/24) * scale) value = scale weights = [] for _ in range(60002): weights.append((10**7*value + 2*scale-1)//(2*scale)) value = (value*step + scale-1)//scale result[sign] = weights return result def scalar_support(j, tilt, ratio_nano, support, table, exp_weights): """Reconstruct an integer witness; its row inequalities prove the bound.""" n = 8192 margin = ceil(PI_UP*COS_SCALE/n) + 2 offset = j*(2*n//128) weights = [] for branch in (0, n): indices = ((k+branch-offset) % (2*n) for k in range(n)) upper = (min(COS_SCALE, table[i] + margin) for i in indices) buckets = (-((-tilt*c)//(COS_SCALE//10)) for c in upper) weights.append([exp_weights[1 if b>=0 else -1][abs(b)] for b in buckets]) first, second = weights base = [k//2 for k in range(n)] other = [i+n//2 for i in base] def action(h): return [a*h[i] + b*h[k] for a,b,i,k in zip(first,second,base,other)] # Iteration only proposes h. Any strictly positive integer vector passing # the comparison below is equally good. There is no convergence test. h = [1]*n for _ in range(72): trial = action(h) top = max(trial) h = [max(1,x*10**18//top) for x in trial] assert min(h)>0 assert all(num*10**9 <= ratio_nano*10**7*den for num, den in zip(action(h),h)), (j, 'transfer ratio') x = F(tilt,1000)*F(support,COS_SCALE) - GAMMA*LOG2_UP assert x >= 0 exp_lo = sum(x**k/factorial(k) for k in range(25)) assert F(ratio_nano,10**9) <= exp_lo, (j, 'pressure') def polynomial_bound(supports, table): """Enclose |z^2-a*z-b|^2 on 20000 real panels of the upper half-disk.""" a,b = F(61,250),F(11,500) n,panels = 8192,10000 normals = [] for j,d in enumerate(supports): k = j*(2*n//128) normals.append((table[k],table[(n//2-k)%(2*n)],d)) worst = F(0) for j in range(-panels, panels): # y <= num/den. Store the smallest rational without intermediate division. num,den = 1,1 for cosine, sine, d in normals: cx_num = min(cosine*j, cosine*(j+1)) - 2*panels top = d*panels-cx_num if sine>2: bottom = (sine-2)*panels if top*den < num*bottom: num,den = top,bottom elif cx_num > (d+2)*panels: num = -1 break if num<0: continue lo,hi = F(j,panels),F(j+1,panels) s = min(F(1),max(lo*lo,hi*hi)+F(num,den)**2) low_sq = max(lo,-hi,0)**2 if s