#!/usr/bin/env python3 """ Numerical companion to the paper "Explicit linear bounds in Waring's problem". Usage python3 verify_explicit_waring.py run all checks (about ten minutes) python3 verify_explicit_waring.py --appendix run the checks for Appendix A only (Part 15, about eight minutes) python3 verify_explicit_waring.py --list print the bound of Theorem 2.3, the number of k-th powers of Theorem 2.4 and the number E^sharp(k) of Section 11.5 for every 37 <= k <= 5000 python3 verify_explicit_waring.py --list-appendix print the numbers of Appendix A: n, c_n, Y^flat(k), G^flat(k) and l^flat(k) for 17 <= k <= 5000 (Section A.7, first case); T_k, n, c_n, Y^VW(k), G^VW(k) and l^VW(k) for 13 <= k <= 20 (second case); Theta^nat(k), B^nat(k), k B^nat(k), 2 s_0^nat(k), b^nat(k) and E^nat(k) for 37 <= k <= 5000 (Section A.8); and the tables of [VW2000] for 13 <= k <= 20 as they are used (about four minutes) (in Python) import verify_explicit_waring as V; V.bound(k, Theta) evaluates B(k, Theta); G(k) <= floor(k B) V.A_rec_std([k]) for k >= 15: the recursion of Theorem A.21 in the first case (entries 'Y', 'n', 'c'); G(k) <= floor(Y) + 1; V.A_rec_vw(k) for 13 <= k <= 20: the same in the second case, with the tables of [VW2000] (entries 'Y', 'G', 'n', 'c', and the truth values of the hypotheses in 'checks') V.A_bound_rig(k) returns (B^nat(k), Theta^nat(k)) of Section A.8, computed with directed rounding V.frac_exponent(k, Theta, b) evaluates the number E of Theorem 2.7: min ||alpha n^k|| < N^(-rho), rho < 1/E What the proofs in the paper use from this file Part 5 only: the evaluation of the closed expression B(k, Theta) = C(h_k) - 2T(Theta) + 2R_k(Theta) + W_k(Theta) + 1/k of Theorem 2.3 at Theta = Theta^sharp(k) for 37 <= k <= 5000 (Proposition 10.5, Table 3), and the elementary numerical inequalities quoted in the proofs of Theorems 1.3 and 2.3, of Corollary 2.6 and of Propositions 10.6, 10.7 and 11.10. Part 5 also evaluates the least number l of k-th powers that is admissible in Theorem 2.4, and the numbers E^sharp(k) of Section 11.5 (Proposition 11.10, Table 4), which are simple functions of B. All quantities are given by the closed formulae of Lemmas 10.1-10.4; the only infinite series is truncated at D = 10^6 with the rigorous tail bounds of Lemma 10.2(i). Part 5 ends with a second, independently written implementation of B(k, Theta^sharp(k)) in 40-digit decimal arithmetic (function decimal_bounds), which is compared with the double precision values. It uses the same closed formulae, so it guards against programming and rounding errors; the formulae themselves are compared with the definitions in Parts 1, 2 and 5. Part 15, for Appendix A. The proofs depend on the following computations. (1) The constants Lambda^star and C_0^star of (A.1): the series is summed up to D = 10^6, and the remainder is bounded by means of 2/(D^2+D+1) <= Lambda_D <= 2/D^2 (A_LAMBDA_R, A_C0_R; the intervals are confirmed in decimal arithmetic with a correctly rounded logarithm, A_const_certify). (2) The sequences c_0 < c_1 < ... < c_n of Theorem A.21 (Sections A.6 and A.7) for 17 <= k <= 5000 in the first case and for 13 <= k <= 20 in the second case (Propositions A.22 and A.23, Tables 6 and 7; functions A_chainmax_lo, A_rec_step_lo, A_rec_std, A_rec_vw). Here c_0 is a floating point number below tau_k (below 1/T_k in the second case), and c_j is a floating point number below a rigorous lower bound for inf{ v/(1 - theta_k(2v)/k) : v >= c_(j-1) }: by Lemma A.14(iv), (v) the bound c_(j-1)/(1 - theta~(2 c_(j-1))/k) holds for a chain as long as its tents are defined, and where the range of v crosses a point 1/(2D(D+1)) it is continued with the chains that are defined beyond that point, used with their values there. The values of the chains are rounded downwards; these steps use the four basic operations only. In the first case condition (R3) is tested with the logarithm rounded downwards, and Y^flat(k) is an upper bound for k Psi(c_n) + 2n; both are verified a second time in decimal arithmetic with 40 digits and a correctly rounded logarithm (A_rec_certify), so that they do not depend on the accuracy of the logarithm of the floating point library. In the second case the tables of permissible exponents of Vaughan and Wooley [VW2000, Sections 15-22] for 13 <= k <= 20 are part of the program (A_VW4, with the column phi_1 of the same tables in A_VW4_PHI); everything that depends on the tables is computed in exact rational arithmetic, including (V1)-(V3), (R3) and the numbers Y^VW(k). The transcription of the tables is tested by means of the relation lambda_s = lambda_(s-1)(1 - phi_1) + 1 + (2s - 2) phi_1 [VW2000, (4.10)], which involves every entry, and by means of the numbers tau(k) that are printed below the tables. The inequalities of Proposition A.22 are decided in exact rational arithmetic. (3) The numbers B^nat(k) and Theta^nat(k) of Section A.8 for 37 <= k <= 5000 (Proposition A.24(i), Table 8). B^nat(k) is obtained from an upper bound for an integral over [2 tau_k, 2 z_k], from which the term (1/k) log(z_k/tau_k) is subtracted; the integral is bounded by means of the chains of Lemma A.14, each of which yields a concave minorant of theta_k, by replacing the minorant by its chord on each cell of a subdivision (functions A_grid_rig, A_band_values_lo, A_cell_sum_up, A_bound_rig). These computations use directed rounding as well, and in them the logarithm of the library is allowed an error of four units in the last place; so they are rigorous under IEEE 754 arithmetic if the library logarithm is accurate to that extent on the arguments that occur (on 60000 sample arguments its error is below one unit). The inequalities of Proposition A.24(i) are then decided in exact rational arithmetic. Directed rounding means that every floating point operation whose result enters a bound is followed by a step to the adjacent floating point number in the safe direction (numpy.nextafter), and that constants are rounded from exact fractions. The hypotheses on which the rigour of a bound depends (for instance, that every tent of a chain is defined at the level at which the chain is evaluated) are enforced by the function _require, which raises an exception, and not by assert statements. The elementary numerical inequalities in the proofs of Lemma A.18 and of Propositions A.19 and A.24(ii), which can be checked by hand, are confirmed as well. Everything else is a consistency check that is not needed for the proofs; Section 10.4 of the paper summarises them. 1. Definition 2.1 / Lemma 10.1: varsigma, its running minimum sigma and the inverse theta*, against the tables of Lemma 10.1. The minimum over z_0 of Psi(z_0) + 2 int_0^z_0 du/(u theta*(2u)), computed from Definition 2.1 on a grid, is 6.3895, attained at z_0 = 0.1 (cf. Section 2.2). 2. Lemma 10.2: the integral I and the constant C_0 (closed form against numerical quadrature); Lemma 10.3. 3. Lemma 4.5 / Table 2: the exponents delivered by Lemmas 4.3 and 4.4 at 5.4e5 parameter sets (k, Theta, theta, U, vartheta) with 37 <= k <= 1e9, chosen at random and at the boundary of the admissible region. In every case the exponent of the row of Table 2 is at least (1 - eta_k(theta)) varsigma(U), and the side conditions hold; equality occurs, for example, for k = 100 and U = theta = Theta = 50/13. 4. Section 8: the potential Phi for thirteen pairs (k, Theta) between (40, 5/2) and (1e9, 28400): (8.1)-(8.3), Lemma 8.1(ii)-(v), Cases 1 and 2 of Proposition 8.2, and the inequality (8.7) of Corollary 8.3 for the value of b of the proof of Theorem 2.3 and for seven further values, at random points or on a grid (in four of the cases a_* is negative). The tabulated value of calG(u_Theta) is smaller than the bound of Lemma 8.1(i) by at most 0.007, and the number (9.19), computed from a table with delta_0 = eps_2 = 1e-8, does not exceed the number calY of the proof of Theorem 2.3. Part 5 also compares the closed form of R_k (Lemma 10.4) with Riemann-Stieltjes sums of its definition. 6. The recursion behind Proposition 8.2, solved by dynamic programming without the potential, for (k, Theta) = (40, 5/2), (100, 50/13) and (250, 750/121): with b as in the proof of Theorem 2.3, the least r/k with sup_lambda (Phi~(1, lambda) - r lambda) <= b (Phi~ the solution of the recursion displayed in Section 10.4) is 6.385, 6.531 and 6.566, against the upper bounds 6.470, 6.562 and 6.573 for calG(u_Theta) + a_* on which Theorem 2.3 rests. 7. A falsification test of the exponents of Lemmas 4.3 and 4.4 on 400 exponential sums calS, with k in {9, 11, 13}, prime power and composite moduli and X <= 6e4: the largest observed value of |calS|/X^(1 - varsigma(U)) is 0.59. 8. Section 12.1: bounds on each residue class with an intermediate coefficient, (12.1) and (12.2). 9. Section 12.3: the constant in the paper of Chow and Dietmann as written. 10. Section 9: the identity and the inequality (9.9) on random trigonometric polynomials with arbitrary common factors of q and d; the rational approximations of Lemma 9.5 in exact arithmetic; the inequality (9.11) behind Lemma 9.4 on small sets with random coefficients xi_m; and the three elementary counting bounds in the proof of Lemma 9.9 against a direct count. 11. Section 11: the spacing Lemma 11.4 in exact arithmetic in 1500 random cases (the count is attained); Lemma 11.3, and Lemmas 11.3 and 11.4 combined as in the proof of Proposition 11.6, on random data; the numerical statements of Section 11.4 ((11.2) and Remark 11.8); and Proposition 11.9. The function E(1/2; v_A, v_B) of (11.3), with the exponents d_inf, has the largest value 0.110782 on a grid, at v_A = v_B = 1.985, and the largest value 0.105662 = 1/9.4642 in the range v_A >= v_c >= v_B, at v_A = v_c and v_B = 1.24. 12. Sections 3, 9 and 11: the set of variables m p_1 ... p_J <= P, R < p_1 < ... < p_J <= 2^H R, on a small example (k = 3, J = 3, R = 40, H = 1 and 2, so that the primes lie in (40, 80] or in (40, 160]): the identities of Lemma 3.1(b)-(d) and the inequalities for Pi in the proof of Lemma 3.2, exactly; the decomposition into the sets calC(h_1, ..., h_J) of Section 3.2 for H = 2 (the two sets calC(h, h, h) contain 2144 of the 9500 elements); the bijection and the kernel identities of Lemmas 9.6 and 11.5; the inequality (9.16) for H = 1; and the approximation of Lemma 9.10 at rational points with q <= 7. 13. Section 9.6: the k-th power residues modulo 4k and 8k for k = 4, 8, ..., 128 (Lemma 9.13), and the least number of k-th powers whose sum lies in a given residue class modulo 8k, for k = 8 and 16: it is j for the class of j when 1 <= j < 4k, and 4k for the class of 4k. 14. Sections 9.7 and 11.5: the lower bound for |F| in the proof of Theorem 2.5 on random sets; the lemma of Harman quoted in the proof of Theorem 2.7 on 600 sets of points, random and extremal (the sum on its left hand side was never smaller than 0.73 times the number of points); the arithmetic at the end of that proof, exactly, on random examples; and, for several pairs (k, Theta) and several b, the number k(calG(u_Theta) + a_*(b)) computed from a table of the potential with delta_0 = eps_2 = 1e-8, against the right hand side of the condition on s_0 in Theorem 2.7. (Part 5 computes the integers s_0^sharp(k) a second time from the 40-digit values of B^sharp(k).) 15. Appendix A (in addition to what is listed above): the function (A.5) against Definition A.11 with vartheta = 0 (Lemma A.12), and the constant 6.2821 from the definition on a grid; Lemmas A.7 and A.8 by exact counts, on seven configurations of the kind that occurs in Lemma A.9 and on 200 random ones (A_pairs_test), and the last step of the proof of Lemma A.9 in exact arithmetic; a falsification test of Corollary A.10 on 60 explicit sums; the chains of Lemma A.14 against Definition A.11 at random points, and the coefficients in the proof of Lemma A.14(iv); the lower bounds of Lemma A.18 against Definition A.11; the numbers quoted in Remarks A.25, A.26 and A.27. By Lemma A.14(i) and its converse, theta_k(t) can be computed directly from Definition A.11, without chains (A_theta_direct, and A_theta_upper with directed rounding): this gives Theta^nat(k) = Theta_k at eight values of k between 37 and 1e6, confirms Lemma A.18 at 1200 points, and gives rigorous lower Riemann sums for the optimal value Y_k(Theta_k) + 1 of Theorem A.17(i) (A_Y_lower). For the recursion: the inequality of (R2) for 108 single steps, with theta_k from Definition A.11 on a grid; the whole recursion in plain double precision with theta_k from Definition A.11 instead of chains (A_rec_plain), for seven values of k and for 13 <= k <= 20 with the tables; and the potential of the proof of Theorem A.21 on a grid, for k = 21, 37 and 100 (A_rec_potential). For Section A.8: a plain double precision version (A_bound) and a second implementation in 40-digit decimal arithmetic (A_exact), in which the conditions on a chain are derived mechanically from the end points of the intervals of Lemma A.14 instead of being taken from solved formulae. Notation as in the paper: calG is the potential of Section 8, frak_g(z) = -z calG'(z), c_Theta = 2(1+eps_2)/Theta its slope (8.4), calG_inf its limit as k -> infinity, and chi_0 = 1 + log(9.027901/11) the constant of Section 11.5. Only numpy is required. """ import sys import random import numpy as np from fractions import Fraction from math import log, log1p, ceil, floor, gcd, sqrt, pi, e as E # ===================================================================================================================== # Definitions (Sections 1, 2 and 10 of the paper) # ===================================================================================================================== def vD(D): """v_D = 1/(D^2+3D+1) (4.6)""" return 1.0/(D*D + 3*D + 1) def UD(D): """U_D = D (D+1)^2 v_D (4.6)""" return D*(D+1)**2*vD(D) def varsigma(U): """the saving function of Definition 2.1""" if U <= 1.5: return (2-U)/2 if U <= 9/4: return (3-U)/6 if U <= 25/8: return (4-U)/14 if U <= 50/13: return (5-U)/30 if U <= 4: return U/100 if U <= 160/37: return (5-U)/25 if U <= 540/121: return (6-U)/62 if U <= 5: return U/180 D = int(floor(U)) if D == U and D > 5: D -= 1 # U = D+1 belongs to two intervals; same value return max((D+1-U)/(D+1)**2, U/((D+1)*(D+2)**2)) def branch(U): """the row of Table 2: ('V', j) = Lemma 4.3 with j (j = 0: part (i)); ('M', D) = Lemma 4.4 with D""" if U <= 1.5: return ('V', 0) if U <= 9/4: return ('V', 1) if U <= 25/8: return ('V', 2) if U <= 50/13: return ('V', 3) if U <= 160/37: return ('M', 4) if U <= 540/121: return ('V', 4) if U <= 5: return ('M', 5) D = int(floor(U)) if D == U and D > 5: D -= 1 return ('M', D) if (D+1-U)/(D+1)**2 >= U/((D+1)*(D+2)**2) else ('M', D+1) def sigma(th): """sigma(theta) = min_{1<=U<=theta} varsigma(U), by the table of Lemma 10.1(ii)""" if th <= 1.5: return (2-th)/2 if th <= 9/4: return (3-th)/6 if th <= 25/8: return (4-th)/14 if th <= 50/13: return (5-th)/30 if th <= 105/26: return 1/26 if th <= 160/37: return (5-th)/25 if th <= 540/121: return (6-th)/62 if th <= 618/121: return 3/121 if th <= 294/55: return (6-th)/36 D = max(6, int(floor(th))-1) while UD(D) > th: D -= 1 # U_D <= theta while UD(D+1) < th: D += 1 # theta <= U_{D+1} flat_end = D+1-(D+1)**2*vD(D) return vD(D) if th <= flat_end else (D+1-th)/(D+1)**2 def theta_star(s): """theta*(s) = max{theta: sigma(theta) >= s}, by the table of Lemma 10.1(iii)""" if s >= 1/4: return 2-2*s if s >= 1/8: return 3-6*s if s >= 1/16: return 4-14*s if s > 1/26: return 5-30*s if s >= 1/37: return 5-25*s if s > 3/121: return 6-62*s if s > 1/55: return 6-36*s D = max(6, int(floor((-3+sqrt(5+4/s))/2))) while vD(D) < s: D -= 1 while vD(D+1) >= s: D += 1 # v_{D+1} < s <= v_D return D+1-(D+1)**2*s # ---- the integral I(s) = int_0^s dt/(t theta*(t)) (Definition 2.1, Lemma 10.2) DMAX = 10**6 _Darr = np.arange(6, DMAX, dtype=np.float64) _suffix = np.cumsum((np.log1p(2.0/_Darr)/(_Darr+1.0))[::-1])[::-1] # _suffix[i] = sum_{D=6+i}^{DMAX-1} def tail_sum(N, upper=True): """sum_{D>=N} log(1+2/D)/(D+1) for N >= 6. The part beyond DMAX lies in [2/(DMAX+1), 2/DMAX] (Lemma 10.2(i)); upper=True adds the upper bound for it, upper=False the lower bound.""" if N >= DMAX: return 2.0/N if upper else 2.0/(N+1) return float(_suffix[N-6]) + (2.0/DMAX if upper else 2.0/(DMAX+1)) PIECES = [(1/3)*log(2), (1/4)*log(25/9), (1/5)*log(2), (1/5)*log(32/21), (1/6)*log(9/8), (1/6)*log(147/103)] LAMBDA_LO = sum(PIECES) + tail_sum(6, False) LAMBDA = sum(PIECES) + tail_sum(6, True) # upper value of Lambda, (1.2) C0_LO = 2 + log(9) + 2*LAMBDA_LO C0 = 2 + log(9) + 2*LAMBDA # upper value of C_0, (1.3) def I(s, upper=True): """I(s) for 0 < s <= 1/4, from Lemma 10.2 (upper/lower refers to the truncation of the series only)""" prim = lambda A, B, x: log(x/(A-B*x))/A # a primitive of 1/(x(A-Bx)) tot = 0.0 for lo, hi, A, B in [(1/8, 1/4, 3, 6), (1/16, 1/8, 4, 14), (1/26, 1/16, 5, 30), (1/37, 1/26, 5, 25), (3/121, 1/37, 6, 62), (1/55, 3/121, 6, 36)]: if s > lo: tot += prim(A, B, min(s, hi)) - prim(A, B, lo) if s >= 1/55: return tot + tail_sum(6, upper) D = max(6, int(floor((-3+sqrt(5+4/s))/2))) while vD(D) < s: D -= 1 while vD(D+1) >= s: D += 1 # v_{D+1} < s <= v_D return prim(D+1, (D+1)**2, s) - prim(D+1, (D+1)**2, vD(D+1)) + tail_sum(D+1, upper) def T(Th): """T(Theta) = I(sigma(Theta)); the lower value is returned, because T enters Theorem 2.3 with a minus sign""" return I(sigma(Th), upper=False) # ---- Definition 2.2 def kappa(th): if th <= 9/4: return 4.0 if th <= 25/8: return 8.0 if th <= 160/37: return 13.0 return max(21.0, th+5.0) def varpi(th): if th <= 25/8: return 1/16 if th <= 160/37: return 1/26 return 3/121 def eta_k(th, k): return th*kappa(th)/k def admissible(k, Th): """Definition 2.2 (the relative tolerance 1e-12 only guards the case Theta = varpi(Theta) k against rounding)""" return k >= 37 and Th >= 9/4 and Th <= varpi(Th)*k*(1+1e-12) and Th*(Th+5) < k def a_k(k): """the positive root a of a(a+5) = k (Lemma 10.4); Theta(Theta+5) < k iff Theta < a_k""" return 2.0*k/(sqrt(4.0*k+25.0)+5.0) def R_k(k, Th, d0=0.0): """Lemma 10.4: the Riemann-Stieltjes integral of 1/theta against -log(1 - d0 - eta_k(theta)) over [9/5, Theta]""" kk = k*(1-d0) pts = [1.8, 9/4, 25/8, 160/37, 16.0]; ks = [4.0, 8.0, 13.0, 21.0] tot = 0.0 for i, kap in enumerate(ks): a, b = pts[i], min(Th, pts[i+1]) if b > a: tot += (kap/kk)*(log(b/a) + log1p(kap*(b-a)/(kk-kap*b))) # (kap/k) log( b(k-kap a) / (a(k-kap b)) ) if Th <= pts[i+1]: return tot if i < 3: t = pts[i+1]; tot += (1/t)*log1p((ks[i+1]-kap)*t/(kk-ks[i+1]*t)) # jump (1/t) log((k-kap t)/(k-kap' t)) a = a_k(kk) # int_16^Theta (2t+5) dt / (t (k - t^2 - 5t)), Lemma 10.4 return tot + (5.0/kk)*log(Th/16.0) + log1p((Th-16.0)/(a-Th))/a + log1p((Th-16.0)/(a+21.0))/(a+5.0) TAU_INV = 9.027901 # tau_k = 1/(9.027901 k) (2.3) def tau_k(k): return 1.0/(TAU_INV*k) def y_k(k, Th): """y_k(Theta) = (1 - eta_k(Theta)) sigma(Theta) / (2 tau_k) (2.3)""" return 0.5*(1-eta_k(Th, k))*sigma(Th)*TAU_INV*k def W_k(k, Th): """W_k(Theta) = (2/Theta) log y_k(Theta) (2.3); it is negative when y_k(Theta) < 1""" return (2/Th)*log(y_k(k, Th)) def a_star(k, c, uTh, b): """a_*(b) = c_Theta log(k c_Theta u_Theta/(e b)) (8.8), with c = c_Theta; it is negative when b > k c_Theta u_Theta/e""" return c*log(k*c*uTh/(E*b)) def b_choice(k, c): """the choice b = k c_Theta tau_k in the proof of Theorem 2.3 (c = c_Theta): it minimises k a_*(b) + b/tau_k""" return k*c*tau_k(k) def Cfun(eta): """C(eta) of Section 2 (upper value)""" return 2 + 2*LAMBDA + log((45+6*eta-eta*eta)/((1-eta)*(5-eta))) - (2/3)*log((15+2*eta)/(15-3*eta)) def h_k(k): return 36/(5*(k-1)) def bound(k, Th): """B(k, Theta) of Section 10.2. Theorem 2.3: G(k) <= floor(k * bound(k, Theta)) for admissible Theta (None if Theta is not admissible)""" if not admissible(k, Th): return None return Cfun(h_k(k)) - 2*T(Th) + 2*R_k(k, Th) + W_k(k, Th) + 1/k def Theta_max(k): """the largest Theta with Theta <= varpi(Theta) k (Section 10.2)""" if k >= 175: return 3*k/121 if k >= 82: return min(k/26, 160/37) return min(k/16, 25/8) def Theta_sharp(k): """Theta^sharp(k) = min{Theta_max(k), (9/10) a_k} (Section 10.2)""" return min(Theta_max(k), 0.9*a_k(k)) KNUM = 5000 # Proposition 10.5 covers 37 <= k <= KNUM CHI0 = 1 + log(TAU_INV/11) # chi_0 = 1 + log(9.027901/11) = 0.80242... (Section 11.5) def frac_exponent(k, Th, b): """Theorem 2.7. Returns (E, s_0, y), where y = Y - (2k/Theta)(1 + log(9.027901 Theta b/2)) with Y = k B(k,Theta) - 1, s_0 >= k is the least natural number with 2 s_0 > y, and E = (2 s_0 + 4.5139505 k)/(1 - b). Then min_{1 <= n <= N} ||alpha n^k|| < N^(-rho) for every rho < 1/E, all real alpha and all large N.""" y = k*bound(k, Th) - 1 - (2*k/Th)*(1 + log(TAU_INV*Th*b/2)) s0 = max(k, floor(y/2) + 1) return (2*s0 + 0.5*TAU_INV*k)/(1 - b), s0, y def E_sharp(k): """E^sharp(k) of Section 11.5: Theta = Theta^sharp(k) and b = 2/(11 Theta), so that 9.027901 Theta b/2 = 9.027901/11""" Th = Theta_sharp(k) return frac_exponent(k, Th, 2/(11*Th))[0] def phi_as(lam): """phi(lambda) of Proposition 10.7: with Theta = lambda sqrt(k) the bound is C_0 k + phi(lambda) sqrt(k) + 5 log k + O(1)""" return -4/lam + 2*log((1+lam)/(1-lam)) + (2/lam)*log(TAU_INV*(1-lam*lam)/(2*lam*lam)) def BW(k): """the bound (1.1) of Bruedern and Wooley""" return ceil(k*(log(k)+4.20032)) # ===================================================================================================================== # Checks # ===================================================================================================================== OK = True def check(cond, msg): global OK print((" [ok] " if cond else " [FAIL] ") + msg) OK = OK and bool(cond) def part1(rng): print("1. Definition 2.1 and Lemma 10.1: varsigma, sigma, theta*") Ugrid = np.exp(np.linspace(log(1+1e-9), log(3000.0), 1_000_001)) vs = np.array([varsigma(u) for u in Ugrid]) sig = np.minimum.accumulate(vs) check(np.max(np.abs(np.diff(vs))) < 1e-3, "varsigma is continuous (no jumps on a grid of 10^6 points up to U = 3000)") def sigma_bruteforce(th): cands = [varsigma(th)] + [varsigma(u) for u in (50/13, 540/121) if u <= th] D = 5 while UD(D+1) <= th: cands.append(vD(D+1)); D += 1 return min(cands) pts = np.concatenate([np.exp(rng.uniform(0, log(3000.0), 20000)), [1.5, 9/4, 25/8, 50/13, 4, 105/26, 160/37, 540/121, 5, 618/121, 294/55, 6, 336/55]]) dev = max(abs(sigma(t)-sigma_bruteforce(t))/sigma_bruteforce(t) for t in pts) check(dev < 1e-12, f"table of sigma (Lemma 10.1(ii)) = running minimum of varsigma (max rel. deviation {dev:.1e} at {len(pts)} points)") se = np.array([sigma(u) for u in Ugrid[::50]]) check(np.max(np.abs(se-sig[::50])/sig[::50]) < 1e-3, "... and it matches the running minimum computed on the grid") bad = 0 for s in np.exp(rng.uniform(log(2e-7), log(0.4999), 4000)): th = theta_star(s) if not (sigma(th) >= s*(1-1e-12) and sigma(th*(1+1e-9)+1e-9) <= s*(1+1e-6)): bad += 1 check(bad == 0, "table of theta* (Lemma 10.1(iii)) is the generalised inverse of sigma (4000 random s)") return Ugrid, vs, sig def part2(rng, Ugrid, vs, sig): print("2. Lemma 10.2 (the integral I, the constant C_0) and Lemma 10.3") print(f" Lambda in [{LAMBDA_LO:.9f}, {LAMBDA:.9f}], C_0 in [{C0_LO:.9f}, {C0:.9f}]") check(abs(tail_sum(6)-0.307897) < 1e-6 and abs(LAMBDA-1.096145) < 1e-6 and abs(C0-6.389515) < 1e-6, "sum_{D>=6} log(1+2/D)/(D+1) = 0.307897..., Lambda = 1.096145..., C_0 = 6.389515...") # brute force: min over z0 of Psi(z0) + 2 int_0^{z0} du/(u theta*(2u)), computed from varsigma alone logs = np.log(sig); dl = -(logs[1:]-logs[:-1]); mid = np.sqrt(Ugrid[1:]*Ugrid[:-1]) suffix = np.concatenate([np.cumsum((dl/mid)[::-1])[::-1], [0.0]]) + 2/3000.0 z = sig/2 val = 2 + np.log((1-z)/z) + 2*suffix i = int(np.argmin(val)) check(abs(val[i]-C0) < 2e-4 and abs(z[i]-0.1) < 1e-3, f"brute-force optimisation over the switching point (Section 2.2): min = {val[i]:.5f} at z = {z[i]:.5f}; closed form C_0 = {C0:.5f} at z = 1/10") check(abs(I(0.2)-LAMBDA) < 1e-12, "I(1/5) = Lambda (2.1)") check(all(abs(I(2*w)-(LAMBDA-log((3-12*w)/(18*w))/3)) < 1e-12 for w in np.linspace(1/16, 0.1, 50)), "I(2w) = Lambda - (1/3) log((3-12w)/(18w)) for 1/16 <= w <= 1/10 (Lemma 10.2(iii))") sg = np.exp(np.linspace(log(1e-6), log(0.2), 2_000_001)); fg = np.array([1/theta_star(x) for x in sg]) quad = float(np.sum(0.5*(fg[1:]+fg[:-1])*np.diff(np.log(sg)))) check(abs(quad-(I(0.2)-I(1e-6))) < 1e-6, f"closed form of I against quadrature of 1/(t theta*(t)): {quad:.7f} vs {I(0.2)-I(1e-6):.7f}") check(all(2/(N+1) <= tail_sum(N, False) and tail_sum(N, True) <= 2/N for N in list(range(6, 2000))+[10**4, 10**5, DMAX-1]), "2/(N+1) <= I(v_N) <= 2/N (Lemma 10.2(i))") ths5 = np.concatenate([np.linspace(5, 60, 20001), np.exp(rng.uniform(log(5), log(3000), 5000))]) check(all(sigma(t) <= 1/(t*t+t-1)+1e-15 for t in ths5), "sigma(Theta) <= 1/(Theta^2+Theta-1) for Theta >= 5 (Lemma 10.3)") check(all(T(t) >= 2/(t+2) for t in ths5), "T(Theta) >= 2/(Theta+2) for Theta >= 5 (Lemma 10.3)") def rounded_exponent(U, vth): """exponent delivered by the row of Table 2 for (U, vartheta), and whether the side conditions of the lemma hold""" kind, par = branch(U) if kind == 'V': j = par if j == 0: return 1-U/2, True rho = (j+2-U)/(2**(j+2)-2) return rho - vth/2, (rho >= vth*(1-1e-12) and U >= j+2.0**(-j)-1e-12) # rho = vartheta occurs (e.g. k = 100, U = theta = 50/13) D = par val = min(U/(D+1)-D*vth, D*(1-U/(D+1)-vth))/(D*(D+1)) return val, (U/(D+1)+vth < 1 and D >= 4) def top_admissible(k): """supremum of the admissible Theta""" return min(Theta_max(k), (-5+sqrt(25+4*k))/2*(1-1e-12)) def part3(rng): print("3. Lemma 4.5 and Table 2: exponents after rounding") worst = 1e9; side = True; degok = True; ntest = 0 klist = [37, 40, 50, 52, 67, 81, 82, 100, 113, 150, 175, 211, 400, 1000, 10**4, 10**5, 10**6, 10**9] special = [1.5, 9/4, 25/8, 50/13, 4, 160/37, 540/121, 5.0] for k in klist: Thmax = top_admissible(k) for it in range(30000): Th = Thmax if it % 10 == 0 else rng.uniform(9/4, Thmax) # every tenth test at the largest admissible Theta if not admissible(k, Th): continue th = Th if it % 20 == 0 else rng.uniform(1.0000002, Th) U = rng.uniform(1.0000001, th) r = rng.random() if r < 0.3: U = min(th, special[int(rng.integers(len(special)))]) elif r < 0.4: U = th vth = max(U/k, th/k*(1.0 if rng.random() < 0.5 else rng.uniform(0, 1))) # U <= k*vartheta <= theta ex, sc = rounded_exponent(U, vth) side &= sc degok &= (branch(U)[1] <= k-2) worst = min(worst, ex - (1-eta_k(th, k))*varsigma(U)); ntest += 1 check(worst >= -1e-12 and side and degok, f"{ntest} parameter sets (k, Theta, theta, U, vartheta), k from {klist[0]} to {klist[-1]:g}, including Theta = largest admissible value," f" U = theta and the end points of the ranges: min[(exponent of Table 2) - (1-eta_k(theta)) varsigma(U)] = {worst:.1e}" f" (zero up to rounding: equality occurs, e.g. for k = 100, U = theta = Theta = 50/13); side conditions hold") # last column of Table 2 okc = True for U in np.concatenate([np.linspace(1.000001, 40, 400001), special]): kind, par = branch(float(U)); v = varsigma(float(U)) delta = 0.0 if (kind, par) == ('V', 0) else (0.5 if kind == 'V' else 1/(par+1)) lim = {('V', 0): 0.0, ('V', 1): 4, ('V', 2): 8, ('V', 3): 13, ('M', 4): 7.4, ('V', 4): 20.2, ('M', 5): 6.8}.get((kind, par) if U <= 5 else None, U+4.2) okc &= (delta/v <= lim + 1e-9) check(okc, "last column of Table 2: Delta/varsigma(U) is at most the stated multiple of vartheta (grid of U up to 40)") class Pot: """the potential of Section 8 for given (k, Theta), with d0 = delta_0 and eps2 = eps_2 small. the potential calG is tabulated along the curve u = M(theta) (exact up to second order in the mesh).""" def __init__(s, k, Theta, eps2=1e-6, d0=1e-6, n=400001): assert admissible(k, Theta) s.k = k; s.Th = Theta; s.eps2 = eps2; s.d0 = d0 s.gamma = 1/(100*k*(log(k)+9)**2); s.L = log(1/s.gamma) s.etac = 2*d0 + h_k(k); s.wc = 0.1 - s.etac/50 - eps2/4; s.zc = (1-s.etac)*s.wc s.c = 2*(1+eps2)/Theta s.uTh = s.M(Theta) s.thc = s._theta_bisect(s.zc, 1.0, Theta) special = [9/4, 25/8, 160/37, 50/13, 105/26, 540/121, 618/121, s.thc] special += [x*(1+1e-13) for x in (9/4, 25/8, 160/37)] D = 5 while UD(D+1) <= Theta: special.append(UD(D+1)); special.append(D+2-(D+2)**2*vD(D+1)); D += 1 tg = np.unique(np.concatenate([np.exp(np.linspace(log(s.thc), log(Theta), n)), [x for x in special if s.thc <= x <= Theta], [Theta]])) s.tg = tg s.Mg = np.array([s.M(float(t)) for t in tg]) assert np.all(np.diff(s.Mg) <= 0) and abs(s.Mg[0]-s.zc) < 1e-12 s.Gzc = 2+log((1-s.zc)/s.zc) inc = 2*(1+eps2)*np.log(s.Mg[:-1]/s.Mg[1:])/np.sqrt(tg[:-1]*tg[1:]) s.Ghat = s.Gzc + np.concatenate([[0.0], np.cumsum(inc)]) # Ghat[i] = calG(M(tg[i])) s.G0 = float(s.Ghat[-1]) # calG(u_Theta) s.Ggamma = s.G0 + s.c*s.L # calG_gamma = calG(u_Theta) + c_Theta log(1/gamma) s.b = b_choice(k, s.c) # the choice of b in the proof of Theorem 2.3 s.astar = a_star(k, s.c, s.uTh, s.b) # = c_Theta (log(u_Theta/tau_k) - 1); it may be negative s.r = k*(s.G0+s.astar) s.t = s.b/tau_k(k) # s = r + t with t tau_k = b (in the limit) def M(s, t): return 0.5*(1-s.d0-eta_k(t, s.k))*sigma(t) def _theta_bisect(s, z, lo, hi): if s.M(hi) >= z: return hi for _ in range(70): mid = 0.5*(lo+hi) if s.M(mid) >= z: lo = mid else: hi = mid return lo def theta(s, z): """theta(z) = max{theta in [1,Theta]: M(theta) >= z}""" if z <= s.uTh: return s.Th if z >= s.zc: return s._theta_bisect(z, 1.0, s.Th) i = len(s.Mg) - int(np.searchsorted(s.Mg[::-1], z, side='left')) - 1 # largest i with Mg[i] >= z return s._theta_bisect(z, float(s.tg[i]), float(s.tg[min(i+1, len(s.tg)-1)])) def G(s, z): if z >= 0.5: return 1/z if z >= s.zc: return 2+log((1-z)/z) if z <= s.uTh: return s.G0 + s.c*log(s.uTh/z) i = len(s.Mg) - int(np.searchsorted(s.Mg[::-1], z, side='left')) - 1 th = s._theta_bisect(z, float(s.tg[i]), float(s.tg[min(i+1, len(s.tg)-1)])) return float(s.Ghat[i]) + 2*(1+s.eps2)*log(float(s.Mg[i])/z)/sqrt(float(s.tg[i])*th) def Phi(s, x, lam): return 0.0 if lam <= 0 else s.k*lam*s.G(max(lam, s.gamma*s.uTh)/x) POT_CASES = [(40, 2.5), (67, 3.125), (100, 100/26), (150, 160/37), (211, 5.23), (1000, 14.27), (1000, 24.79), (10**4, 36.5), (10**4, 87.7), (10**6, 197.0), (10**6, 897.0), (10**9, 2190.0), (10**9, 28400.0)] def part4(rng): print("4. Section 8: the potential Phi") for (k, Th) in POT_CASES: P = Pot(k, Th); g = P.gamma thc = P.thc hyp = thc >= 2*(1+P.eps2)*(1-P.zc) and thc >= 3-12*P.wc and P.uTh < 1/16 < P.zc < 0.1 # (8.2), (8.3) w = dict(adm=1e9, iv=1e9, mono=1e9, iii=1e9, lip=-1e9, a=1e9) zlo = g*P.uTh*1e-3 for _ in range(2500): x = float(np.exp(rng.uniform(log(4*g), 0))) zz = float(np.exp(rng.uniform(log(zlo), log(P.zc))))*(1-1e-9); lam = zz*x th = P.theta(zz) # (8.1): (1-eta_k(theta)) sigma(theta) >= 2z + delta_0 sigma(Theta) w['adm'] = min(w['adm'], ((1-eta_k(th, k))*sigma(th) - 2*zz - P.d0*sigma(Th))/sigma(Th) + 1e-9) for d in (th*x/k/(1+P.eps2), th*x/k): w['iv'] = min(w['iv'], (P.Phi(x, lam)-P.Phi(x-d, lam))/lam - 2 + 1e-5) # Lemma 8.1(iv) mus = np.sort(rng.uniform(0, lam*(1+1e-6), 5)) vals = [P.Phi(x-d, m)-m for m in mus] w['mono'] = min(w['mono'], float(np.min(np.diff(vals)))) # Lemma 8.1(ii) nu = lam*(1+1e-6); t = rng.uniform(0, min(nu, x/2)) w['iii'] = min(w['iii'], (P.Phi(x, nu)-P.Phi(x-t, nu-t)-2*k*t)/max(k*t, 1e-300) + 1e-6) # Lemma 8.1(iii) l2 = lam*rng.uniform(0.5, 1.5) w['lip'] = max(w['lip'], (P.Phi(x, max(lam, l2))-P.Phi(x, min(lam, l2)))/max(abs(lam-l2), 1e-300)/k - P.Ggamma) z2 = float(rng.uniform(P.zc, 3.0)); lam2 = z2*x # Cases 1-2 of Proposition 8.2 ap = k*x if z2 >= 0.5 else min(k*x, k*lam2*(2+log((1-z2)/z2))) w['a'] = min(w['a'], (P.Phi(x, lam2)-ap)/(k*x) + 1e-9) lams = np.exp(np.linspace(log(zlo), 0, 40001)) beta = max(P.Phi(1.0, float(l)) - P.r*float(l) for l in lams) # Corollary 8.3 for x = 1 for _ in range(40): # ... and for gamma <= x <= 1 x = float(np.exp(rng.uniform(log(g), 0))) beta = max(beta, max((P.Phi(x, float(l)*x) - P.r*float(l)*x)/x for l in lams[::50])) closed = 2+log((1-P.zc)/P.zc) + 2*(1+P.eps2)*(I(2*P.wc) - T(Th) + R_k(k, Th, P.d0)) # Lemma 8.1(i) # Lemma 8.1(v): calG(z) <= calG(u_Theta) + c_Theta log(u_Theta/z) for 0 < z <= 1, and z calG(z) is concave zs = np.unique(np.concatenate([np.exp(np.linspace(log(1e-12), 0.0, 3001)), P.uTh*np.exp(np.linspace(-3, 3, 401)), [P.zc, 0.5]])) zs = zs[zs <= 1.0] Gz = np.array([P.G(float(z)) for z in zs]) line = float(np.max(Gz - (P.G0 + P.c*np.log(P.uTh/zs)))) sl = np.diff(zs*Gz)/np.diff(zs) conc = float(np.max(np.diff(sl))) # Corollary 8.3 for other b: a_*(b) = -c_Theta w for b = (k c_Theta u_Theta/e) e^w; then k z (calG(z) - calG(u_Theta) - a_*) <= b cor = max(float(np.max(k*zs*(Gz - P.G0 + P.c*wv)))/(k*P.c*P.uTh/E*np.exp(wv)) for wv in (3.0, 1.3, 1.0, 0.5, 0.0, -1.0, -5.0)) good = hyp and all(v >= -1e-9 for kk, v in w.items() if kk != 'lip') and w['lip'] <= 1e-6 and beta <= P.b*(1+1e-6) \ and line <= 1e-7 and conc <= 1e-6 and cor <= 1+1e-7 and P.r >= k+1 if not good: print(" details:", hyp, {kk: float(v) for kk, v in w.items()}, beta, P.b, line, conc, cor) check(good and P.G0 <= closed + 1e-4, f"k={k:g}, Theta={Th:.4f}: (8.1)-(8.3), Lemma 8.1(ii)-(v), Cases 1-2 of Prop. 8.2 at 2500 random states; " f"a_* = {P.astar:+.4f}, sup[(Phi(x,l)-r l)/x] = {beta:.4f} <= b = {P.b:.4f}; Cor. 8.3 for seven further b (ratio {cor:.4f}); " f"calG(u_Theta) = {P.G0:.5f} <= bound of Lemma 8.1(i) = {closed:.5f}") def print_list(): """the bound of Theorem 2.3 at Theta^sharp(k) for every 37 <= k <= KNUM (Proposition 10.5), the least l that is admissible in Theorem 2.4, and the number E^sharp(k) of Section 11.5 with the bound k(log k + 8.02113) of [BW2025]""" print("# k Theta^sharp(k) B(k,Theta^sharp(k)) floor(k B) ceil(k(log k+4.20032)) l (Thm 2.4) E^sharp(k) k(log k+8.02113)") for k in range(37, KNUM+1): Th = Theta_sharp(k); v = bound(k, Th) print(f"{k:5d} {Th:12.6f} {v:.6f} {floor(k*v):6d} {BW(k):6d} {(floor(k*v)+1)//2:6d} {E_sharp(k):11.3f} {k*(log(k)+8.02113):11.3f}") # the inequalities of Proposition 10.6 (k >= KNUM, Theta = (9/10) a_k); the constants are those of the paper C_T, C_R, C_W = -0.84, 39.76, 3.98 C3 = 2*log(19) - 40/9 + (20/9)*log(19*TAU_INV/162) # the coefficient of sqrt(k) in Proposition 10.6; C3 = phi(9/10) def part5(): print("5. Theorem 2.3, Propositions 10.5-10.7, Table 3, Theorems 1.1 and 1.2") # --- C(eta) def f_top(eta): w = 0.1-eta/50; zz = (1-eta)*w return 2 + log((1-zz)/zz) + 2*I(2*w) et = np.linspace(0, 0.2, 2001); cv = np.array([Cfun(x) for x in et]); sl = np.diff(cv)/np.diff(et) check(max(abs(Cfun(x)-f_top(x)) for x in et) < 1e-12 and abs(Cfun(0)-C0) < 1e-13, "C(h) = Psi(z) + 2 I(2w) with w = 1/10 - h/50, z = (1-h) w; C(0) = C_0") check(0 < sl.min() and sl.max() < 1.4 and all(k*Cfun(h_k(k)) <= C0_LO*k + 11 for k in range(37, 100000)), f"0 < C' < 7/5 on [0, 1/5] (slopes between {sl.min():.3f} and {sl.max():.3f}); k C(h_k) <= C_0 k + 11 for k >= 37") # --- the Weyl exponent tau_k d = 0.3 for _ in range(60): d -= (d+log(d)+1)/(1+1/d) check(abs(d-0.2784645428) < 1e-9 and 0 < TAU_INV - 4/(1-2*d) < 1e-6, f"delta_* + log delta_* + 1 = 0 for delta_* = {d:.10f}; 4/(1-2 delta_*) = {4/(1-2*d):.7f} < {TAU_INV} (9.13)") # --- the choice of b (Section 9.1 and the proof of Theorem 2.3): k a_*(b) + b/tau_k >= k c_Theta log(u_Theta/tau_k), with equality for b = k c_Theta tau_k okb = True for (k, Th) in [(37, 37/16), (100, 100/26), (1000, 3000/121), (5000, 0.9*a_k(5000)), (10**6, 0.9*a_k(10**6))]: c = 2/Th; u = y_k(k, Th)*tau_k(k) # the limits of c_Theta and u_Theta as delta_0, eps_2 -> 0 target = k*c*log(u/tau_k(k)); tol = 1e-9*(1+abs(target)) vals = np.array([k*a_star(k, c, u, float(b)) + float(b)/tau_k(k) for b in b_choice(k, c)*np.exp(np.linspace(-6, 6, 2401))]) okb = okb and bool(np.all(vals >= target - tol)) and abs(k*a_star(k, c, u, b_choice(k, c)) + b_choice(k, c)/tau_k(k) - target) < tol \ and abs(k*W_k(k, Th) - target) < tol and abs(a_star(k, c, u, b_choice(k, c)) - c*(log(y_k(k, Th)) - 1)) < 1e-12 check(okb and all(4.19*k >= 4*k+1 for k in range(37, 10**5)) and 2+log(9) > 4.19 and 2+log(9)-1 > 3, "k a_*(b) + b/tau_k >= k c_Theta log(u_Theta/tau_k) for all b > 0, with equality at b = k c_Theta tau_k, where a_* = c_Theta(log(u_Theta/tau_k) - 1);" " Psi(1/10) > 4.19, 4.19 k >= 4k+1 (Section 9.1)") # --- worked example of Section 10.2 k, Th = 37, 37/16 print(f" example k = 37, Theta = 37/16: C(h_k) = {Cfun(h_k(k)):.5f}, T = {T(Th):.5f}, R_k = {R_k(k, Th):.5f}, y_k = {y_k(k, Th):.5f}," f" W_k = {W_k(k, Th):.5f}, B = {bound(k, Th):.5f}, floor(37 B) = {floor(k*bound(k, Th))}") # --- Proposition 10.5 and Table 3 tab = {} for k in range(37, KNUM+1): Th = Theta_sharp(k); v = bound(k, Th) assert v is not None, (k, Th) tab[k] = (Th, v) G = {k: floor(k*tab[k][1]) for k in tab} # G(k) <= G[k] frac_hi = min(1 - (k*tab[k][1] - G[k]) for k in tab); frac_lo = min(k*tab[k][1] - G[k] for k in tab) check(all(admissible(k, Theta_sharp(k)) for k in range(37, KNUM+1)) and frac_hi > 1e-6 and frac_lo > 1e-6, f"Theta^sharp(k) is admissible for 37 <= k <= {KNUM}; the distance of k B^sharp(k) from the nearest integer is at least {min(frac_hi, frac_lo):.1e} in all cases") print(" Table 3: k Theta B floor(kB) (1.1)") for k in [37, 38, 39, 40, 41, 50, 67, 81, 100, 150, 174, 200, 212, 250, 330, 500, 700, 927, 1000, 2000, 5000]: print(f" {k:8d} {tab[k][0]:8.4f} {tab[k][1]:.4f} {G[k]:8d} {BW(k):9d}") big = {} for k in [10**4, 10**5, 10**6]: Th = Theta_sharp(k); v = bound(k, Th); big[k] = v print(f" {k:8d} {Th:8.3f} {v:.4f} {floor(k*v):8d} {BW(k):9d} (Theta = Theta^sharp(k) = (9/10) a_k; fractional part of kB: {k*v-floor(k*v):.4f})") check((floor(10**4*big[10**4]), floor(10**5*big[10**5]), floor(10**6*big[10**6])) == (64112, 639520, 6391170), "Table 3 for k = 10^4, 10^5, 10^6: 64112, 639520, 6391170") def last_fail(pred, lo=37): bad = [k for k in range(lo, KNUM+1) if not pred(k)] return (bad[-1]+1) if bad else lo kmax = max(tab, key=lambda j: tab[j][1]) check(kmax == 37 and tab[37][1] < 7.3898 and all(tab[k][1] < 7.3567 for k in range(38, KNUM+1)), f"Proposition 10.5(i): max of B^sharp over 37 <= k <= {KNUM} is {tab[37][1]:.5f} (k = 37) < 7.3898; B^sharp(k) <= {max(tab[k][1] for k in range(38, KNUM+1)):.5f} < 7.3567 for k >= 38") k7 = last_fail(lambda k: tab[k][1] < 7); k65 = last_fail(lambda k: G[k] < 6.5*k); k65B = last_fail(lambda k: tab[k][1] < 6.5) check(k7 == 212 and k65 == 927 and max(tab[k][1] for k in range(212, KNUM+1)) < 6.9982 and min(6.5*k - G[k] for k in range(927, KNUM+1)) >= 0.5, f"Proposition 10.5(ii): B^sharp(k) < 7 for {k7} <= k <= {KNUM} (max {max(tab[k][1] for k in range(212, KNUM+1)):.5f}), not for k = {k7-1};" f" floor(k B^sharp(k)) < 6.5 k for {k65} <= k <= {KNUM}, not for k = {k65-1} (B^sharp < 6.5 from k = {k65B})") ex = {k: (G[k]-C0_LO*k)/sqrt(k) for k in tab} kex = max(ex, key=lambda j: ex[j]) k4 = last_fail(lambda k: ex[k] < 4); k3 = last_fail(lambda k: ex[k] < 3) kex4 = max(range(697, KNUM+1), key=lambda j: ex[j]); kex3 = max(range(1339, KNUM+1), key=lambda j: ex[j]) check(kex == 175 and ex[kex] < 9.82 and G[175] == 1248 and k4 == 697 and ex[kex4] < 3.9995 and k3 == 1339 and ex[kex3] < 2.9985, f"Proposition 10.5(iii): max of (floor(k B^sharp) - C_0 k)/sqrt(k) over 37 <= k <= {KNUM} is {ex[kex]:.4f} (k = {kex}) < 9.82" f" [C_0 k + 10 sqrt k - floor(k B^sharp) >= {min(C0_LO*k+10*sqrt(k)-G[k] for k in tab):.3f}];" f" it is < 4 for {k4} <= k <= {KNUM} (max {ex[kex4]:.4f} at k = {kex4}), not for k = {k4-1};" f" it is < 3 for {k3} <= k <= {KNUM} (max {ex[kex3]:.4f} at k = {kex3}), not for k = {k3-1}; value at k = {KNUM}: {ex[KNUM]:.4f}") m4 = min(C0_LO*k+4*sqrt(k)-G[k] for k in range(697, KNUM+1)); m65 = min(6.5*k-G[k] for k in range(927, KNUM+1)) m10 = min(C0_LO*k+10*sqrt(k)-G[k] for k in tab); m3 = min(C0_LO*k+3*sqrt(k)-G[k] for k in range(1339, KNUM+1)) mB = min(7.3898 - tab[37][1], min(7.3567 - tab[k][1] for k in range(38, KNUM+1)), min(7 - tab[k][1] for k in range(212, KNUM+1))) check(min(m4, m65, m10, m3) >= 0.025 and mB >= 4e-5 and all(Theta_sharp(k) == 0.9*a_k(k) for k in range(1137, 20000)) and all(Theta_sharp(k) == Theta_max(k) < 0.9*a_k(k) for k in range(37, 1137)), f"margins: the bounds for B^sharp in Proposition 10.5(i), (ii) hold with a margin of at least {mB:.1e}; C_0 k + 10 sqrt k - G >= {m10:.3f}," f" C_0 k + 4 sqrt k - G >= {m4:.3f} (k >= 697), C_0 k + 3 sqrt k - G >= {m3:.3f} (k >= 1339), 6.5 k - G >= {m65:.1f} (k >= 927);" f" Theta^sharp(k) = (9/10) a_k exactly for k >= 1137") check(all(Theta_max(k) >= 37/16 for k in range(37, 20000)) and 0.9*a_k(37) > 9/4 and all(abs(a_k(k)*(a_k(k)+5) - k) < 1e-9*k for k in range(37, 20000)), "Theta_max(k) >= 37/16, (9/10) a_37 > 9/4, a_k (a_k + 5) = k (admissibility of Theta^sharp, Section 10.2)") bw36 = max(BW(k)/k for k in range(1, 37)); k36 = max(range(1, 37), key=lambda k: BW(k)/k) check(bw36 < 7.806 and k36 == 36 and BW(36) == 281, f"Theorem 1.2 for k <= 36: max of ceil(k(log k+4.20032))/k = {bw36:.4f} (k = {k36}, bound 281) < 7.81") kbw = last_fail(lambda k: BW(k)-G[k] > k) check(all(G[k] < BW(k) for k in range(37, KNUM+1)) and BW(37)-G[37] == 17 and kbw == 49, f"the bound is smaller than (1.1) for every 37 <= k <= {KNUM}: by 17 at k = 37, by more than k for k >= {kbw}") check((G[37], G[50], G[100], G[1000]) == (273, 353, 709, 6492) and (BW(37), BW(50), BW(100), BW(1000)) == (290, 406, 881, 11109), "examples of Section 1: G(37) <= 273, G(50) <= 353, G(100) <= 709, G(1000) <= 6492; (1.1) gives 290, 406, 881, 11109") # how far is Theta^sharp from the optimum? gap = 0.0; gapN = 0 for k in list(range(37, 1001, 3)) + list(range(1000, KNUM+1, 40)): top = top_admissible(k) best = min(bound(k, float(t)) for t in np.linspace(9/4, top, 1500)) gap = max(gap, tab[k][1]-best); gapN = max(gapN, G[k]-floor(k*best)) check(gap < 1e-5 and gapN <= 1, f"optimising Theta numerically instead of using Theta^sharp lowers B by at most {gap:.1e}, and floor(kB) by at most {gapN}, for 37 <= k <= {KNUM} (sample)") # the derivative of B(k, Theta) with respect to Theta is -(2/Theta^2) log y_k(Theta), and B is continuous in Theta (Remark 10.8) devB = 0.0; jumpB = 0.0; rs = np.random.default_rng(7) for k in (37, 60, 100, 150, 300, 1000, 5000, 10**5): top = top_admissible(k) for Th in rs.uniform(9/4, top, 60): Th = float(Th); hh = 1e-6*Th if any(abs(Th - x) < 3*hh for x in (9/4, 25/8, 160/37, 50/13, 105/26, 540/121, 618/121, 16.0)) or Th + hh > top or Th - hh < 9/4: continue if any(abs(Th - x) < 3*hh for D in range(5, int(Th)+3) for x in (UD(D+1), D+2-(D+2)**2*vD(D+1))): continue fdv = (bound(k, Th+hh) - bound(k, Th-hh))/(2*hh) devB = max(devB, abs(fdv + (2/Th**2)*log(y_k(k, Th)))*Th) for x in (9/4, 25/8, 160/37): if 9/4 <= x*(1-1e-9) and x*(1+1e-9) <= top: jumpB = max(jumpB, abs(bound(k, x*(1+1e-9)) - bound(k, x*(1-1e-9)))) def opt_theta(k): lo, hi = 9/4, top_admissible(k) if y_k(k, hi) >= 1: return hi for _ in range(200): mid = 0.5*(lo+hi) if y_k(k, mid) >= 1: lo = mid else: hi = mid return lo nlow = sum(1 for k in range(37, KNUM+1) if floor(k*bound(k, opt_theta(k))) == G[k]-1) gapo = max(tab[k][1] - bound(k, opt_theta(k)) for k in range(37, KNUM+1)) oko = all(G[k]-1 <= floor(k*bound(k, opt_theta(k))) <= G[k] for k in range(37, KNUM+1)) t6 = opt_theta(10**6) nab = sum(1 for k in range(1133, KNUM+1) if y_k(k, Theta_sharp(k)) > 1) # the solution of y_k = 1 exceeds Theta^sharp(k) okab = all(y_k(k, Theta_sharp(k)) > 1 for k in range(2313, KNUM+1)) oky = all((y_k(k, top_admissible(k)) >= 1) == (k <= 1132) and (k > 1132 or top_admissible(k) == Theta_sharp(k)) for k in range(37, KNUM+1)) check(devB < 1e-5 and jumpB < 1e-7 and oko and oky and nab == 3623 and okab and nlow == 18 and 0 <= gapo < 1e-5 and abs(t6 - 902.36) < 5e-3 and floor(10**6*bound(10**6, t6)) == 6391169, f"Remark 10.8: dB/dTheta = -(2/Theta^2) log y_k(Theta) (finite differences, deviation {devB:.1e}), B is continuous in Theta (jumps {jumpB:.1e});" f" the least value of B(k,.) is attained at the largest admissible Theta (= Theta^sharp(k)) for k <= 1132 and where y_k = 1 for k >= 1133" f" (for 1133 <= k <= {KNUM} this point exceeds Theta^sharp(k) for {nab} of the {KNUM-1132} values of k, among them all k >= 2313):" f" it is smaller than B^sharp(k) by at most {gapo:.1e}," f" and the integer is floor(k B^sharp) - 1 for {nlow} values of k <= {KNUM}, floor(k B^sharp) otherwise; k = 10^6: Theta = {t6:.2f} gives 6391169") # --- Proposition 10.6: k >= KNUM, Theta = (9/10) a_k Lc = log(19*TAU_INV/162) ks = np.unique(np.concatenate([np.arange(KNUM, 30001), np.exp(np.linspace(log(3e4), log(1e12), 20000))])) ok = True; slack = [1e9, 1e9, 1e9] for k in ks: k = float(k); K = sqrt(k); a = a_k(k); Th = 0.9*a if not (admissible(k, Th) and Th > 16 and K-2.5 < a < K-2.45 and a > 68.2): ok = False; print(' fails at', k); break s1 = (-(40/9)*K + C_T) - (-2*k*T(Th)) s2 = (2*K*log(19) + 5*log(k) + C_R) - 2*k*R_k(k, Th) yp = (19*TAU_INV/162)*(1 + ((260/171)*a + 100/81)/(a*a + (10/9)*a - 100/81)) s3 = ((20/9)*Lc*K + C_W) - k*W_k(k, Th) e1 = abs((1-eta_k(Th, k)) - (19*a+50)/(100*(a+5))) # 1 - eta_k(Theta) = (19a+50)/(100(a+5)) e2 = abs(4*k/(Th+2) - ((40/9)*a + 1000/81 - (20000/729)/(a+20/9)))/k # 4k/(Theta+2) e3 = abs(2*k*R_k(k, Th) - (2*k*R_k(k, 16.0) + 10*log(Th/16) + 2*(a+5)*log(10*(1-16/a)) + 2*a*log(1.9*(1-(21-50/19)/(a+21)))))/k if not (min(s1, s2, s3) >= 0 and y_k(k, Th) <= yp*(1+1e-12) and yp <= (19*TAU_INV/162)*(1+1.53/a) and max(e1, e2, e3) < 1e-11): ok = False; print(' fails at', k, s1, s2, s3, e1, e2, e3); break slack = [min(slack[0], s1), min(slack[1], s2), min(slack[2], s3)] tot = 12 + C_T + C_R + C_W elem = (100/9 - 1000/81 + 0.39 < C_T and (20000/729)/(68.2+20/9) < 0.39 and 10*log(160/9) > 28.77 and 10*log(10) - 32 < -8.97 and 2*18.368*68.2/89.2 > 28.08 and 21 - 50/19 > 18.368 and 4.9*log(19) > 14.42 and 120 - 28.77 - 8.97 - 28.08 - 14.42 <= C_R + 1e-12 and (20/9)*(5*Lc + 1.53 + 7.65/68.2 - 2.45*Lc) < C_W and sqrt(KNUM) - 2.5 > 68.2) check(ok and elem and tot < 55 and abs(C3-1.57146) < 1e-5 and C3 < 1.572, f"Proposition 10.6: for k >= {KNUM} and Theta = (9/10) a_k: -2kT <= -(40/9) sqrt k {C_T:+}, 2k R_k <= 2 sqrt k log 19 + 5 log k + {C_R}," f" k W_k <= (20/9) log(19 D/162) sqrt k + {C_W} ({len(ks)} values of k up to 1e12, least slacks {slack[0]:.2f}, {slack[1]:.2f}, {slack[2]:.2f});" f" the elementary inequalities of the proof hold; constants add up to {tot:.3f} < 55; coefficient of sqrt k: c_3 = {C3:.5f} < 1.572") Kg = np.exp(np.linspace(log(sqrt(KNUM)), log(1e15), 200001)) check(bool(np.all(1.572*Kg + 10*np.log(Kg) + 55 < 3*Kg)) and 1.428 - 10/sqrt(KNUM) > 0 and C0 + 3/sqrt(KNUM) < 6.45, f"1.572 sqrt k + 5 log k + 55 < 3 sqrt k for k >= {KNUM} (hence G(k) < C_0 k + 3 sqrt k < 6.45 k there)") # --- Theorem 1.3 (almost all natural numbers). For Theta = Theta^sharp(k) the number Y of the proof of Theorem 2.3 is # k B^sharp(k) - 1, and the least integer l with 2l > Y is admissible in Theorem 2.4 Yv = {k: k*tab[k][1] - 1 for k in tab} Gp = {k: floor(Yv[k]/2) + 1 for k in tab} BWp = lambda k: ceil(0.5*k*(log(k) + 4.20032)) # the bound of [BW2023, Theorem 9.1] exq = max((Gp[k] - C0_LO*k/2)/sqrt(k) for k in tab) pw2 = [k for k in tab if k & (k-1) == 0] check(all(Gp[k] == (G[k]+1)//2 for k in tab) and all(Yv[k] < 7.39*k for k in tab) and all(G[k] <= C0_LO*k + 9.82*sqrt(k) for k in tab) and all(2.09*k < Gp[k] < 4*k for k in tab) and 0.09*sqrt(37) > 0.5 and exq < 4.94 and min(C0_LO*k/2 + 5*sqrt(k) - Gp[k] for k in tab) > 0.84, f"Theorem 1.3 for 37 <= k <= {KNUM}: the least l with 2l > Y is ceil(floor(k B^sharp)/2); Y < 7.39 k; 2.09 k < l < 4k;" f" l < C_0 k/2 + 5 sqrt k (largest value of (l - C_0 k/2)/sqrt k: {exq:.4f})") check((Gp[37], Gp[50], Gp[100], Gp[1000]) == (137, 177, 355, 3246) and (BWp(37), BWp(50), BWp(100), BWp(1000)) == (145, 203, 441, 5555) and pw2 == [64, 128, 256, 512, 1024, 2048, 4096] and [Gp[k] for k in pw2] == [229, 450, 881, 1693, 3323, 6605, 13164] and all(Gp[k] < BWp(k) for k in tab), "Theorem 1.3, examples: G^+(37) <= 137, G^+(50) <= 177, G^+(100) <= 355, G^+(1000) <= 3246, where [BW2023, Theorem 9.1] gives" " 145, 203, 441, 5555; powers of 2: l = 229, 450, 881, 1693, 3323, 6605, 13164 for k = 64, 128, ..., 4096 (to be compared with 4k)") totp = 11 + C_T + C_R + C_W okp = all(floor((float(k)*bound(float(k), 0.9*a_k(float(k))) - 1)/2) + 1 <= C0_LO*float(k)/2 + 0.786*sqrt(float(k)) + 2.5*log(float(k)) + 28 for k in ks[::25]) check(totp < 54 and C3/2 < 0.786 and 54/2 + 1 <= 28 and okp and bool(np.all(0.786*Kg + 5*np.log(Kg) + 28 < 5*Kg)) and bool(np.all(C0*Kg*Kg + 3*Kg < 8*Kg*Kg)), f"Theorem 1.3 for k >= {KNUM}: Y <= C_0 k + c_3 sqrt k + 5 log k + 54 (constants add up to {totp:.2f});" f" floor(Y/2) + 1 <= C_0 k/2 + 0.786 sqrt k + (5/2) log k + 28 < C_0 k/2 + 5 sqrt k (also compared with the exact values" f" at {len(ks[::25])} values of k); Y < C_0 k + 3 sqrt k < 8k") small = {k: BWp(k) for k in range(1, 37)} check(all(small[k] < 4*k for k in small) and max(small[k]/k for k in small) == 141/36 == 47/12 and all(Gp[k] <= (7.39*k + 1)/2 < 4*k for k in tab) and C0/2 + 5/sqrt(KNUM) < 4, f"G^+(k) <= 4k for every k that is not a power of 2: ceil(k(log k + 4.20032)/2) < 4k for k <= 36 (largest ratio 47/12, at k = 36);" f" l <= (7.39 k + 1)/2 < 4k for 37 <= k <= {KNUM}; C_0 k/2 + 5 sqrt k < 4k for k >= {KNUM}") # --- Corollary 2.6 (the mean value estimate of Theorem 2.5 with s_1 = A_1 k + A_2 sqrt k for k >= k_1): what is needed is s_1 > Y cols = [(37, 7.39, 0), (37, C0_LO, 10), (212, 7, 0), (697, C0_LO, 4), (927, 6.5, 0), (1339, C0_LO, 3)] okc = all(Yv[k] < A1*k + A2*sqrt(k) for (k1, A1, A2) in cols for k in tab if k >= k1) Kh = np.exp(np.linspace(log(264.0), log(1e15), 100001)) check(okc and C0 + 3/sqrt(KNUM) < 6.45 and C3 < 1.572 and bool(np.all(1.572*Kh + 10*np.log(Kh) + 54 < 2*Kh)) and sqrt(70000) > 264 and 0.428*264 > 10*log(264) + 54 and phi_as(0.90478) < 1.5712, f"Corollary 2.6: Y < A_1 k + A_2 sqrt k for k_1 <= k <= {KNUM} and (k_1, A_1, A_2) = (37, 7.39, 0), (37, C_0, 10), (212, 7, 0), (697, C_0, 4)," f" (927, 6.5, 0), (1339, C_0, 3); for k >= {KNUM}: Y <= C_0 k + 1.572 sqrt k + 5 log k + 54 < C_0 k + 3 sqrt k < 6.45 k," f" and < C_0 k + 2 sqrt k for k >= 70000; phi(0.90478) = {phi_as(0.90478):.6f} < 1.5712") # --- Theorems 1.5 and 2.7, Proposition 11.10 (fractional parts). E^sharp(k) = (2 s_0 + 4.5139505 k)/(1 - b) with Theta = Theta^sharp(k), # b = 2/(11 Theta), and s_0 >= k least with 2 s_0 > Y - 2 chi_0 k/Theta CF = 10.9035 fr = {k: frac_exponent(k, tab[k][0], 2/(11*tab[k][0])) for k in tab} Es = {k: fr[k][0] for k in tab} BW25 = lambda k: k*(log(k) + 8.02113) # [BW2025, Theorem 1.2] dist = min(min(fr[k][2]/2 - floor(fr[k][2]/2), floor(fr[k][2]/2) + 1 - fr[k][2]/2) for k in tab) check(abs(CHI0 - 0.8024246) < 1e-7 and C0 + TAU_INV/2 < CF and abs(C0 + TAU_INV/2 - 10.903466) < 1e-6 and all(abs(fr[k][2] - (Yv[k] - 2*CHI0*k/tab[k][0])) < 1e-9 for k in tab) and all(fr[k][1] == floor(fr[k][2]/2) + 1 > 3*k for k in tab) and all(0 < 2/(11*tab[k][0]) < 0.08 for k in tab) and all(abs(Es[k] - E_sharp(k)) < 1e-12 for k in (37, 100, 1000, 5000)) and dist > 1e-6, f"Section 11.5: chi_0 = {CHI0:.7f}, C_0 + 4.5139505 = {C0 + TAU_INV/2:.6f} < 10.9035; for 37 <= k <= {KNUM} the number s_0^sharp(k) is" f" floor(y/2) + 1 > 3k, where y = Y - 2 chi_0 k/Theta^sharp, and y/2 has distance at least {dist:.1e} from the integers") exf = {k: (Es[k] - CF*k)/sqrt(k) for k in tab} kxf = max(exf, key=lambda j: exf[j]) k4f = last_fail(lambda k: exf[k] < 4); k12 = last_fail(lambda k: Es[k] < 12*k); k11 = last_fail(lambda k: Es[k] < 11*k) kbw = last_fail(lambda k: Es[k] < BW25(k)) m4 = min(CF*k + 4*sqrt(k) - Es[k] for k in tab if k >= 900); m12 = min(12*k - Es[k] for k in tab if k >= 42) m11 = min(11*k - Es[k] for k in tab if k >= 1339); mbw = min(BW25(k) - Es[k] for k in tab if k >= 46) x900 = max(exf[k] for k in tab if k >= 900); r42 = max((Es[k]/k, k) for k in tab if k >= 42) check(kxf == 175 and 11.5025 < exf[175] < 11.5026 and k4f <= 900 and k12 <= 42 and k11 <= 1339 and kbw == 46 and m4 > 0.5 and m12 > 1 and m11 > 1.5 and mbw > 1 and 3.9808 < x900 < 3.9809 and r42[1] == 80 and 11.9863 < r42[0] < 11.9864 and all(Es[k] > BW25(k) for k in range(37, 46)), f"Proposition 11.10(i): the largest value of (E^sharp(k) - 10.9035 k)/sqrt k for 37 <= k <= {KNUM} is {exf[175]:.5f} (k = 175) < 12;" f" it is < 4 for k >= {k4f} (largest value {x900:.5f} for k >= 900); E^sharp(k) < 12k for k >= {k12} (largest E^sharp(k)/k for k >= 42:" f" {r42[0]:.5f} at k = {r42[1]}), < 11k for k >= {k11}, < k(log k + 8.02113) for k >= {kbw} and not for 37 <= k <= 45;" f" margins for k >= 900, 42, 1339, 46: {m4:.2f}, {m12:.2f}, {m11:.2f}, {mbw:.2f}") print(" Table 4: k Theta^sharp b^sharp 2 s_0^sharp E^sharp(k) k(log k+8.02113) 9.028 k") t4 = {} for k in [37, 46, 50, 100, 200, 500, 1000, 2000, 5000, 10**4, 10**5, 10**6]: Th = Theta_sharp(k); e = frac_exponent(k, Th, 2/(11*Th)); t4[k] = e print(f" {k:8d} {Th:9.4f} {2/(11*Th):.5f} {2*e[1]:10d} {e[0]:13.2f} {BW25(k):13.2f} {9.028*k:11.1f}") check([2*t4[k][1] for k in (37, 46, 50, 100, 200, 500, 1000, 2000, 5000, 10**4, 10**5, 10**6)] == [248, 302, 328, 668, 1342, 3244, 6428, 12818, 31986, 63930, 638952, 6389382] and [round(t4[k][0], 2) for k in (37, 46, 50, 100, 1000, 5000)] == [450.43, 544.05, 587.90, 1174.94, 11022.78, 54717.71] and t4[100][0] < 1175 and t4[1000][0] < 11023 and 1262.6 < BW25(100) < 1262.7 and 14928.8 < BW25(1000) < 14928.9 and abs(TAU_INV/2 - 4.5139505) < 1e-12, "Table 4 and the examples of Section 1: E^sharp(100) < 1175, E^sharp(1000) < 11023; 100(log 100 + 8.02113) = 1262.6...," " 1000(log 1000 + 8.02113) = 14928.8...") # Proposition 11.10(ii), k >= KNUM: the elementary inequalities of the proof, and the bound compared with the exact values Kq = sqrt(KNUM) el = (20/(99*68.2) < 0.003 and 1/(1 - 0.003) < 1.0031 and Kq/(Kq - 2.5) < 1.037 and (10*log(Kq) + 56)/(Kq - 2.5) < 1.446 and bool(np.all(np.diff((10*np.log(Kg) + 56)/(Kg - 2.5)) < 0)) and (20/99)*(1.572*1.037 + 1.446) < 0.622 and 1.0031*2*CF/11 - 2*CHI0 < 0.3838 and 0.3838*(10/9) < 0.42645 and 0.3838*(10/9)*2.55 < 1.09 and 56 + 1.0031*0.622 + 1.09 < 58 and C3 + 0.42645 < 2 and 2*KNUM < C0_LO*KNUM - 2*CHI0*(10/9)*(Kq + 2.55) and bool(np.all(2*Kg + 10*np.log(Kg) + 58 < 3.5*Kg)) and CF + 3.5/Kq < 11) slk = [CF*k + 2*sqrt(k) + 5*log(k) + 58 - E_sharp(float(k)) for k in ks] check(el and min(slk) > 0, f"Proposition 11.10(ii): the elementary inequalities of the proof; E^sharp(k) < 10.9035 k + 2 sqrt k + 5 log k + 58 at {len(ks)} values of k" f" between {KNUM} and 1e12 (least slack {min(slk):.1f}); 2 sqrt k + 5 log k + 58 < 3.5 sqrt k and 10.9035 k + 3.5 sqrt k < 11 k for k >= {KNUM}") kR16 = [k*R_k(k, 16.0) for k in np.exp(np.linspace(log(1000), log(1e12), 2001))] check(max(kR16) < 60 and kR16[0] == max(kR16) and all(np.diff(kR16) < 1e-9), f"k R_k(16) <= {max(kR16):.2f} < 60 for k >= 1000, decreasing in k (Lemma 10.4)") # --- Proposition 10.7: the function phi lg = np.linspace(0.3, 0.995, 139001); pv = np.array([phi_as(float(l)) for l in lg]); i0 = int(np.argmin(pv)) lam0 = sqrt(TAU_INV/(TAU_INV+2)); c2 = -4/lam0 + 2*log((1+lam0)/(1-lam0)) dphi = lambda l: -(2/(l*l))*log(TAU_INV*(1-l*l)/(2*l*l)) # the derivative of phi devd = max(abs((phi_as(l+1e-6)-phi_as(l-1e-6))/2e-6 - dphi(l)) for l in np.linspace(0.31, 0.99, 69)) check(abs(pv[i0]-c2) < 1e-9 and abs(lg[i0]-lam0) < 1e-4 and abs(c2-1.57112) < 1e-5 and abs(lam0-0.90478) < 1e-5 and abs(phi_as(lam0)-c2) < 1e-12 and abs(phi_as(0.9)-C3) < 1e-12 and devd < 1e-6 and all(dphi(l) < 0 for l in lg if l < lam0-1e-9) and all(dphi(l) > 0 for l in lg if l > lam0+1e-9), f"Proposition 10.7: phi' = -(2/lambda^2) log(D(1-lambda^2)/(2 lambda^2)); min of phi = {pv[i0]:.5f} at lambda_0 = sqrt(D/(D+2)) = {lam0:.5f};" f" phi(9/10) = c_3 = {phi_as(0.9):.5f} < 1.572") rows = [] for lam in (0.5, 0.75, 0.9, lam0, 0.95): vals = [k*bound(k, lam*sqrt(k)) - C0*k - phi_as(lam)*sqrt(k) - 5*log(k) for k in (1e6, 1e8, 1e10)] rows.append((lam, vals)) check(all(-200 < v < 200 for lam, vals in rows for v in vals), "k B(k, lambda sqrt k) - C_0 k - phi(lambda) sqrt k - 5 log k stays bounded: " + "; ".join(f"lambda = {lam:.4f}: " + ", ".join(f"{v:.1f}" for v in vals) for lam, vals in rows) + " (k = 1e6, 1e8, 1e10)") # the tabulated potentials of part 4 agree with the closed formula in the limit delta_0, eps_2 -> 0 dvs = []; dvf = [] for (k, Th) in [(40, 2.5), (67, 3.125), (100, 100/26), (150, 160/37), (211, 5.23), (1000, 14.27), (1000, 24.79), (10**4, 87.7)]: P = Pot(k, Th, eps2=1e-8, d0=1e-8, n=200001) dvs.append(((k*bound(k, Th) - 1) - (P.r + P.t))/k) # Y = k B - 1 for b in (2/(11*Th), 0.01, 0.5, 0.99): # Theorem 2.7: k(calG(u_Theta) + a_*(b)) against its limit dvf.append((frac_exponent(k, Th, b)[2] - k*(P.G0 + a_star(k, P.c, P.uTh, b)))/k) check(min(dvs) >= 0 and max(dvs) < 1e-2, f"the number (9.19), r + t = k(calG(u_Theta)+a_*) + b/tau_k, from the potential tabulated with delta_0 = eps_2 = 1e-8 is at most the number Y" f" in the proof of Theorem 2.3 (slack/k between {min(dvs):.1e} and {max(dvs):.1e})") check(min(dvf) >= 0 and max(dvf) < 1e-2, f"likewise k(calG(u_Theta) + a_*(b)) is at most Y - (2k/Theta)(1 + log(9.027901 Theta b/2)), the number in Theorem 2.7, for b = 2/(11 Theta)," f" 0.01, 0.5, 0.99 (slack/k between {min(dvf):.1e} and {max(dvf):.1e})") # --- R_k: closed form of Lemma 10.4 against Riemann-Stieltjes sums of the definition devR = 0.0 for (k, Th) in [(37, 37/16), (50, 25/8), (100, 100/26), (175, 525/121), (400, 9.0), (1000, 23.7), (5000, 53.0), (5000, 61.4), (10**4, 75.0), (10**6, 812.5), (10**6, 897.7)]: tg = np.unique(np.concatenate([np.linspace(1.8, Th, 400001), [x for x in (9/4, 25/8, 160/37) if x < Th], [x*(1+1e-13) for x in (9/4, 25/8, 160/37) if x*(1+1e-13) < Th]])) psi = np.array([-log1p(-eta_k(float(t), k)) for t in tg]) rs = float(np.sum(np.diff(psi)/np.sqrt(tg[:-1]*tg[1:]))) devR = max(devR, abs(rs-R_k(k, Th))/R_k(k, Th)) check(devR < 1e-6, f"Lemma 10.4: closed form of R_k agrees with Riemann-Stieltjes sums of its definition, also for Theta > 16 (max rel. deviation {devR:.1e})") # --- independent implementation in decimal arithmetic dec = decimal_bounds(list(range(37, KNUM+1)) + [10**4, 10**5, 10**6]) dev = max(abs(float(dec[k]) - tab[k][1]) for k in range(37, KNUM+1)) same = all(int((dec[k]*k).to_integral_value(rounding='ROUND_FLOOR')) == G[k] for k in range(37, KNUM+1)) devbig = max(abs(float(dec[k]) - big[k]) for k in big) samebig = all(int((dec[k]*k).to_integral_value(rounding='ROUND_FLOOR')) == floor(k*big[k]) for k in big) check(dev < 1e-9 and same and devbig < 1e-9 and samebig, f"independent 40-digit implementation of B(k,Theta^sharp(k)), 37 <= k <= {KNUM}: max deviation from double precision {dev:.1e};" f" the integers floor(k B) agree for every k (also for k = 10^4, 10^5, 10^6: deviation {devbig:.1e})") from decimal import Decimal as Dc def s0_dec(k): # s_0^sharp(k) from the decimal values of B Thd = min(Dc(Fraction(Theta_max(k)).limit_denominator(1000).numerator)/Dc(Fraction(Theta_max(k)).limit_denominator(1000).denominator), Dc(9)/Dc(10)*(Dc(2*k)/((Dc(4*k+25)).sqrt()+5))) y = dec[k]*k - 1 - 2*(1 + (Dc('9.027901')/11).ln())*k/Thd return int((y/2).to_integral_value(rounding='ROUND_FLOOR')) + 1 check(all(s0_dec(k) == fr[k][1] for k in range(37, KNUM+1)), f"the integers s_0^sharp(k) of Section 11.5 computed from the 40-digit values agree with those above for 37 <= k <= {KNUM}") return tab def decimal_bounds(klist, prec=40, NT=200000): """B(k, Theta^sharp(k)) for the k in klist, written independently of the functions above, directly from Definitions 2.1, 2.2, Theorem 2.3 and Lemmas 10.1-10.4, in decimal arithmetic with `prec` digits. Upper bounds: the series of Lemma 10.2(i) is summed to NT and its tail bounded from the right side.""" from decimal import Decimal as Dc, getcontext from fractions import Fraction as Fr getcontext().prec = prec ln = lambda x: Dc(x).ln() q = lambda a, b: Dc(a)/Dc(b) tau_inv = Dc('9.027901') v = lambda n: Dc(1)/(Dc(n)*Dc(n)+3*Dc(n)+1) U = lambda n: Dc(n)*(Dc(n)+1)**2*v(n) def sig(th): # Lemma 10.1(ii) if th <= q(3, 2): return (2-th)/2 if th <= q(9, 4): return (3-th)/6 if th <= q(25, 8): return (4-th)/14 if th <= q(50, 13): return (5-th)/30 if th <= q(105, 26): return q(1, 26) if th <= q(160, 37): return (5-th)/25 if th <= q(540, 121): return (6-th)/62 if th <= q(618, 121): return q(3, 121) if th <= U(6): return (6-th)/36 n = max(6, int(th)-1) while not (U(n) <= th <= U(n+1)): n += 1 return v(n) if th <= n+1-(n+1)**2*v(n) else (n+1-th)/(Dc(n+1)**2) terms = [ln(1+Dc(2)/Dc(n))/Dc(n+1) for n in range(6, NT)] part = sum(terms) prefix = [Dc(0)] for t in terms[:4000]: prefix.append(prefix[-1]+t) # prefix[j] = sum of the terms with 6 <= D < 6+j lo6, hi6 = part + Dc(2)/Dc(NT+1), part + Dc(2)/Dc(NT) # bounds for sum_{D>=6} lam_hi = ln(2)/3 + ln(q(25, 9))/4 + ln(2)/5 + ln(q(32, 21))/5 + ln(q(9, 8))/6 + ln(q(147, 103))/6 + hi6 def I_lo(s): # Lemma 10.2, lower bound inc = lambda A, B, s1, s2: ln(s2*(A-B*s1)/(s1*(A-B*s2)))/A if s <= q(1, 55): n = max(6, int(((Dc(5)+4/s).sqrt()-3)/2)-1) while not (v(n+1) < s <= v(n)): n += 1 return lo6 - prefix[n+1-6] + inc(Dc(n+1), Dc(n+1)**2, v(n+1), s) val = lo6 for (s1, s2, A, B) in [(q(1, 55), q(3, 121), 6, 36), (q(3, 121), q(1, 37), 6, 62), (q(1, 37), q(1, 26), 5, 25), (q(1, 26), q(1, 16), 5, 30), (q(1, 16), q(1, 8), 4, 14), (q(1, 8), q(1, 4), 3, 6)]: if s > s1: val += inc(Dc(A), Dc(B), s1, min(s, s2)) return val def kap(th): if th <= q(9, 4): return Dc(4) if th <= q(25, 8): return Dc(8) if th <= q(160, 37): return Dc(13) return max(Dc(21), th+5) def Rk(k, Th): # Lemma 10.4 t = [q(9, 5), q(9, 4), q(25, 8), q(160, 37), Dc(16)]; kp = [Dc(4), Dc(8), Dc(13), Dc(21)] tot = Dc(0) for i in range(1, 5): if t[i-1] < Th: tp = min(t[i], Th) tot += kp[i-1]/k*ln(tp*(k-kp[i-1]*t[i-1])/(t[i-1]*(k-kp[i-1]*tp))) for i in range(1, 4): if t[i] < Th: tot += ln((k-kp[i-1]*t[i])/(k-kp[i]*t[i]))/t[i] if Th > 16: a = ((4*k+25).sqrt()-5)/2 tot += 5/k*ln(Th/16) + ln((a-16)/(a-Th))/a + ln((a+5+Th)/(a+21))/(a+5) return tot out = {} for k in klist: kd = Dc(k) tf = min(Fr(k, 16), Fr(25, 8)) if k <= 81 else (min(Fr(k, 26), Fr(160, 37)) if k <= 174 else Fr(3*k, 121)) # Theta_max(k) wf = Fr(1, 16) if tf <= Fr(25, 8) else (Fr(1, 26) if tf <= Fr(160, 37) else Fr(3, 121)) assert tf >= Fr(9, 4) and tf <= wf*k # Theta_max(k) <= varpi(Theta_max(k)) k, exactly Th = min(q(tf.numerator, tf.denominator), q(9, 10)*((4*kd+25).sqrt()-5)/2) # Theta^sharp(k) assert Th >= q(9, 4) and Th*(Th+5) < kd h = q(36, 5)/(kd-1) C = 2 + 2*lam_hi + ln((45+6*h-h*h)/((1-h)*(5-h))) - q(2, 3)*ln((15+2*h)/(15-3*h)) sg = sig(Th); eta = Th*kap(Th)/kd y = (1-eta)*sg*tau_inv*kd/2 W = (2/Th)*ln(y) out[k] = C - 2*I_lo(sg) + 2*Rk(kd, Th) + W + 1/kd return out def dp(k, Th, h, b, xmin_log=16.0, NL=500, lam_min=1e-11): """V(x,l) = min{ k x, k l Psi(l/x), min_theta max_{mu<=l} [3 l - mu + V(x(1-theta/k), mu)] } on a grid x = exp(-j h), l log-spaced; a step theta = k(1-exp(-n h)) in [9/5, Theta] is allowed iff (1-eta_k(theta)) sigma(theta) >= 2 l/x. Returns min{ r : sup_l V(1,l) - r l <= b } / k.""" lam = np.exp(np.linspace(log(lam_min), 0.0, NL)) steps = [n for n in range(1, 10**6) if 1.8 <= k*(1-np.exp(-n*h)) <= Th] levels = [(1-eta_k(k*(1-np.exp(-n*h)), k))*sigma(k*(1-np.exp(-n*h)))/2 for n in steps] nmax = max(steps); Jx = int(xmin_log/h) buf = np.zeros((nmax+1, NL)) # buf[j] = cummax_{mu<=l} (V_j(mu) - mu) for j in range(Jx+nmax, Jx, -1): buf[j % (nmax+1)] = np.maximum.accumulate(k*np.exp(-j*h)-lam) for j in range(Jx, -1, -1): x = np.exp(-j*h); zz = lam/x with np.errstate(divide='ignore', invalid='ignore'): apr = np.where(zz <= 0.5, k*lam*(2+np.log((1-np.minimum(zz, 0.5))/zz)), np.inf) bestv = np.minimum(k*x, apr) for n, lev in zip(steps, levels): bestv = np.where(zz <= lev, np.minimum(bestv, 3*lam + buf[(j+n) % (nmax+1)]), bestv) bestv = np.minimum.accumulate(bestv[::-1])[::-1] # D_S(X,W) is nondecreasing in W buf[j % (nmax+1)] = np.maximum.accumulate(bestv-lam) return float(np.max((bestv-b)/lam))/k def part6(): print("6. the recursion behind Proposition 8.2 (Section 10.4) solved by dynamic programming, independently of the potential") for (k, Th, h) in [(40, 2.5, 0.0006), (100, 100/26, 0.0005), (250, 750/121, 0.0002)]: b = b_choice(k, 2/Th) rdp = dp(k, Th, h, b); rth = (k*bound(k, Th) - 1 - b/tau_k(k))/k check(rdp <= rth, f"k={k}, Theta={Th:.4f} (mesh {h}), b = {b:.4f}: least r/k admitted by the recursion = {rdp:.4f} <= {rth:.4f} = r/k of Theorem 2.3" f" (t/k = b/(k tau_k) = {b/tau_k(k)/k:.4f})") def part7(): print("7. falsification test of Lemmas 4.3 and 4.4 on actual sums (|S| / X^(1-varsigma(U)))") random.seed(5) def expsum(q, m, k, X, a): ns = [n for n in range(1, X+1) if gcd(n, m) == 1] r = np.array([(a*pow(n, k, q)) % q for n in ns], dtype=np.float64)/q return abs(np.exp(2j*np.pi*r).sum()) worst = 0.0; ntests = 0 plist = [11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61] while ntests < 400: k = random.choice([9, 11, 13]) primes = [p for p in random.sample(plist, random.choice([1, 2, 3, 4])) if p > k] if not primes: continue m = 1 for p in primes: m *= p es = [random.randint(0, k) for _ in primes] q = 1 for p, ee in zip(primes, es): q *= p**ee if not (10**4 < q < 10**16): continue U = random.choice([1.2, 1.5, 1.8, 2.0, 2.3, 2.7, 3.0, 3.4, 3.9, 4.2, 4.4, 4.8, 5.3, 6.2]) X = int(round(q**(1/U))) if X < 250 or X > 60000: continue Ue = log(q)/log(X) kind, par = branch(Ue) if (kind == 'V' and par > k-2) or (kind == 'M' and par > k-1): continue a = random.randrange(1, q) while gcd(a, q) != 1: a = random.randrange(1, q) ratio = expsum(q, m, k, X, a)/X**(1-varsigma(Ue)) worst = max(worst, ratio); ntests += 1 check(worst < 1.0, f"{ntests} sums with k in {{9, 11, 13}} and X up to 60000: largest |S|/X^(1-varsigma(U)) = {worst:.3f}") def part8(): print("8. Section 12.1: bounds on each residue class with an intermediate coefficient") def per_class(Ug, third=True): """max over D >= 2 and integers r in (U, D] of min{ (D+1-U)/(D+1), U(D+1-r)/(D+1), r-U } / (D(D-1)); third=False drops the term q_r Y^-r""" out = np.zeros_like(Ug) for D in range(2, int(2*Ug.max())+9): lo = np.searchsorted(Ug, max(1.0, (D-8)/2.0)); hi = np.searchsorted(Ug, D+1.0) if hi <= lo: continue U = Ug[lo:hi]; cap = (D+1-U)/(D+1) if third: rstar = 2*U*(D+1)/(D+1+U); v = np.zeros_like(U) for r in (np.floor(U)+1, np.floor(rstar), np.ceil(rstar)): okr = (r > U) & (r <= D) & (r >= 2) v = np.maximum(v, np.where(okr, np.minimum(np.minimum(cap, U*(D+1-r)/(D+1)), r-U), 0.0)) else: v = np.minimum(cap, U*(D-1)/(D+1)) out[lo:hi] = np.maximum(out[lo:hi], v/(D*(D-1))) return out U8 = np.exp(np.linspace(log(1+1e-9), log(600.0), 200001)) vs8 = np.array([varsigma(u) for u in U8]) pp = per_class(U8); pp0 = per_class(U8, third=False) def slow(U): return max(min((D+1-U)/(D+1), U*(D+1-r)/(D+1), r-U)/(D*(D-1)) for D in range(2, int(3*U)+8) for r in range(2, D+1) if r > U) check(all(abs(slow(float(U8[i]))-pp[i]) < 1e-14 for i in np.searchsorted(U8, [1.3, 1.5, 2.2, 3.7, 4.46, 5.345, 8.5, 20.3, 77.7])), "vectorised evaluation agrees with the direct maximum over all (D, r)") rat = pp/vs8 check(not np.any(pp > vs8*(1+1e-12)) and not np.any(pp0 > vs8*(1+1e-9)), f"the saving (12.2) never exceeds varsigma(U) for 1 < U <= 600 (max ratio {rat.max():.4f} at U = {U8[int(np.argmax(rat))]:.3f}); nor when the term q_r Y^-r is dropped") check(rat[U8 >= 2].max() < 0.59 and rat[U8 >= 50/13].max() < 0.52 and abs(rat[-1]-4/27) < 2e-3, f"ratio <= {rat[U8 >= 2].max():.4f} for U >= 2, <= {rat[U8 >= 50/13].max():.4f} for U >= 50/13, {rat[-1]:.4f} at U = 600 (4/27 = {4/27:.4f})") def const_from(v, U, tail): sg = np.minimum.accumulate(v); lg = np.log(sg) suf = np.concatenate([np.cumsum((-(lg[1:]-lg[:-1])/np.sqrt(U[1:]*U[:-1]))[::-1])[::-1], [0.0]]) + tail zz = sg/2; return float(np.min(2 + np.log((1-zz)/zz) + 2*suf)) def vdc_all(U): b = (2-U)/2 if U < 2 else 0.0; j = 1 while j + 2.0**(-j) <= U: if U < j+2: b = max(b, (j+2-U)/(2**(j+2)-2)) j += 1 return b c_cur = const_from(vs8, U8, 2/600); c_max = const_from(np.maximum(vs8, pp), U8, 2/600) c_rep = const_from(np.maximum(np.array([vdc_all(u) for u in U8]), pp), U8, 2/600) c_bou = const_from(np.maximum(np.where(U8 < 2, (2-U8)/2, 0.0), pp), U8, 2/600) check(abs(c_cur-C0) < 2e-4 and abs(c_max-c_cur) < 1e-12 and abs(c_rep-6.916) < 1e-3 and abs(c_bou-7.404) < 1e-3, f"constant: {c_cur:.4f} with varsigma; unchanged if (12.1) is added; {c_rep:.4f} if (12.1) replaces Lemma 4.4; {c_bou:.4f} if it also replaces Lemma 4.3(ii)") def lemma44(U, D): return min(U/(D+1), D*(1-U/(D+1)))/(D*(D+1)) if U < D+1 else 0.0 worst = 0.0 for U in np.linspace(2.0, 30.0, 141): std = max(lemma44(U, D) for D in range(2, 45)); b = 0.0 for D in range(2, 45): hi_tau = min(1.0/D, 0.999999/U) if hi_tau <= 1.0/(D+1): continue tau = np.linspace(1.0/(D+1), hi_tau, 201)[:-1]; a = U*tau for r in range(1, D+1): b = max(b, float(np.max(np.minimum(np.minimum(r*(1-a), U*(1-r*tau)), np.minimum(r-U+a, a))))/(D*(D+1))) worst = max(worst, b/std) check(worst <= 1+1e-9, f"using alpha_r with r < D (and any A = q^tau) inside the proof of Lemma 4.4 gives at most the saving of Lemma 4.4 (max ratio {worst:.6f})") def hyp(U): b = varsigma(U) for D in range(4, int(U)+3): if U < D+1: b = max(b, min(U/(D+1), D*(D+1-U)/2)/(D*(D+1))) return b print(f" (hypothetical: if the count in Lemma 4.4 were as small as the pigeonhole principle allows, the constant would be {const_from(np.array([hyp(u) for u in U8]), U8, 2/600):.4f})") # the constant with the saving of Chow and Dietmann, Lemma 3.1: sigma(theta) = c/theta^2, c = 1/1000 cc = 1e-3 val = min(2 + log((1-z0)/z0) + 4*sqrt(2*z0/cc) for z0 in np.exp(np.linspace(log(1e-9), log(0.4), 200001))) print(f" (with the saving X^(-c/theta^2), c = 1/1000, the constant would be {val:.3f} = 4 + log(8000) = {4+log(8000):.3f})") def part9(): print("9. Section 12.3: the constant in the paper of Chow and Dietmann as written") A0 = 4_000_001; b = A0**-0.5 check(b < 1/2000 and A0 >= 100/b and A0 >= b**-2 - 1e-6, f"A_0 = {A0}, b = A_0^(-1/2) < c/2 = 1/2000; s = 2k + 3 A_0 k + 1 = {2+3*A0} k + 1") def cd_ok(k): L = 3*log(k); U = np.linspace(1.0000001, L, 200001); Dd = np.ceil(4*U) return bool(np.all(U/(2*Dd+1) - Dd*L/k >= 1/12)) and 12*log(k)+2 <= k lo, hi = 10**3, 10**7 while hi-lo > 1: midk = (lo+hi)//2 if cd_ok(midk): hi = midk else: lo = midk check(hi == 122878, f"inequality (3.5) of Chow and Dietmann holds for all U in (1, 3 log k] exactly when k >= {hi}") check(max(BW(k)/k for k in range(1, 67)) <= 8.4 < BW(67)/67, "ceil(k(log k+4.20032)) <= 8.4 k exactly for k <= 66") def part10(rng): print("10. Section 9: consistency checks for the lemmas on arcs (not needed for the proofs)") from fractions import Fraction as Fr from math import pi # (a) the change of variables in the proof of Lemma 9.2, tested on random trigonometric polynomials def make_G(N): c = rng.normal(size=N) + 1j*rng.normal(size=N) g = np.correlate(c, c, mode='full') # coefficient of e(j beta), j = -(N-1)..N-1, of |sum c_n e(n beta)|^2 return np.arange(-(N-1), N), g def integ(js, g, lo, hi, mult=1): tot = 0.0 for j, gj in zip(js, g): f = j*mult tot += (gj*(hi-lo)).real if f == 0 else (gj*(np.exp(2j*pi*f*hi)-np.exp(2j*pi*f*lo))/(2j*pi*f)).real return tot def arcs(q, Q, Yk): # the arcs of M(Q;Y) with denominator q; Yk = Y^k return [(a/q - Q/(q*Yk), a/q + Q/(q*Yk)) for a in range(1, q+1) if gcd(a, q) == 1] phi = lambda n: sum(1 for a in range(1, n+1) if gcd(a, n) == 1) rad_part = lambda m, q: int(np.prod([p for p in (2, 3, 5, 7, 11, 13) if m % p == 0 and q % p == 0] or [1])) w1, w2, w3, n1 = 0.0, -1e9, 0.0, 0 while n1 < 500: k = int(rng.integers(2, 4)); m = int(rng.choice([2, 3, 5, 6, 7, 10, 15, 21, 30])); q = int(rng.integers(1, 41)) gg = gcd(q, m**k); q2 = q//gg; d = rad_part(m, q) Q = float(rng.uniform(q, q+12)); Y = (2*Q)**(2/k)*float(rng.uniform(1.0, 2.0)); P = Y*m # Q <= (1/2) (P/m)^(k/2) js, g = make_G(5) lhs = sum(integ(js, g, lo, hi, m**k) for lo, hi in arcs(q, Q, P**k)) exact = m**(-k)*(phi(q)/phi(q2))*sum(integ(js, g, lo, hi) for lo, hi in arcs(q2, Q/gg, Y**k)) upper = (m//d)**(-k)*sum(integ(js, g, lo, hi) for lo, hi in arcs(q2, Q, Y**k)) w1 = max(w1, abs(lhs-exact)/max(abs(exact), 1e-12)); w2 = max(w2, (lhs-upper)/max(abs(upper), 1e-12)) w3 = max(w3, m**(-k)*(phi(q)/phi(q2))/((m//d)**(-k))); n1 += 1 check(w1 < 1e-6 and w2 < 1e-6 and w3 <= 1+1e-12, f"(9.9): int over M_q(Q;P) of f(d^k alpha) = d^(-k) (phi(q)/phi(q'')) int over M_q''(Q/g;P/d) of f <= (d/d_q(d))^(-k) int over M_q''(Q;P/d) of f" f" ({n1} random cases with (q,d) arbitrary; rel. deviation {w1:.1e})") # (b) Lemma 9.5 (rational approximations) def convergents(x): h0, h1, k0, k1 = 0, 1, 1, 0 while True: a = x.numerator//x.denominator h0, h1, k0, k1 = h1, a*h1+h0, k1, a*k1+k0 yield h1, k1 fr = x - a if fr == 0: return x = 1/fr okb = True; nb = 0; worst = 0.0 for _ in range(4000): Xk = int(10**rng.uniform(4, 14)); rt = int(sqrt(Xk)) q = int(rng.integers(1, rt+1)) if rng.random() < 0.5 else int(np.exp(rng.uniform(0, log(rt)))) a = int(rng.integers(0, q+1)) if gcd(a, q) != 1: continue dev = Fr(int(rng.integers(0, 10**9)), 10**9)*Fr(1, rt+1) # |q beta - a| <= X^(-k/2) if rng.random() < 0.3: dev = dev*Fr(1, int(10**rng.uniform(0, 6))) beta = Fr(a, q) + (dev/q if rng.random() < 0.5 else -dev/q) Xi = q + Xk*abs(q*beta-a) if 2*q >= Xi: b, r = a, q else: QD = 4*Fr(Xk)/Xi; b, r = None, None for hh, kk in convergents(beta): if kk > QD: break b, r = hh, kk okb &= (Fr(b, r) != Fr(a, q) and 2*r*Xi >= Xk) okb &= (abs(beta-Fr(b, r)) <= Fr(1, r*r) and Fr(1, r) + Fr(r, Xk) <= 12/Xi and gcd(b, r) == 1) worst = max(worst, float((Fr(1, r) + Fr(r, Xk))*Xi)); nb += 1 check(okb, f"Lemma 9.5: an approximation b/r with |beta-b/r| <= r^(-2) and 1/r + r/X^k <= 12/Xi exists as constructed ({nb} random cases in exact arithmetic; largest (1/r+r/X^k) Xi = {worst:.2f})") # (c) Lemma 9.4 before the appeal to Lemma 2.2 of Vaughan's book: the exact inequality (9.11), tested on small sets def Jcount(S, j, k): from collections import Counter cnt = Counter([0]) for _ in range(j): new = Counter() for v0, c0 in cnt.items(): for x in S: new[v0+x**k] += c0 cnt = new return sum(c*c for c in cnt.values()) worst = 0.0; nc = 0 for _ in range(400): k = int(rng.integers(2, 4)); A = int(rng.integers(3, 9)); B = int(rng.integers(3, 9)) SA = [m for m in range(1, A+1) if rng.random() < 0.7] or [A]; SB = [n for n in range(1, B+1) if rng.random() < 0.7] or [B] j = int(rng.integers(1, 3)); w = int(rng.integers(1, 3)); al = float(rng.uniform(0, 1)) mode = rng.random(); th = float(rng.uniform(0, 1)) # coefficients: all 1, or e(theta m), or arbitrary with |xi_m| <= 1 if mode < 0.3: xi = {m: 1.0 for m in SA} elif mode < 0.6: xi = {m: complex(np.exp(2j*pi*th*m)) for m in SA} else: xi = {m: complex(np.exp(2j*pi*rng.uniform(0, 1)))*(1.0 if rng.random() < 0.7 else float(rng.uniform(0, 1))) for m in SA} lhs = sum(abs(sum(xi[m]*np.exp(2j*pi*al*(m*n)**k) for m in SA)) for n in SB)**(2*j*w) nd = lambda x: abs(x-round(x)) geo = sum(min(j*A**k, 1/(2*nd(al*dd)) if nd(al*dd) > 0 else 1e300) for dd in range(-w*B**k, w*B**k+1)) rhs = len(SB)**(2*j*w-2*w)*len(SA)**(2*j*w-2*j)*Jcount(SA, j, k)*Jcount(SB, w, k)*geo worst = max(worst, lhs/rhs); nc += 1 check(worst <= 1+1e-9, f"Lemma 9.4, inequality (9.11) with coefficients |xi_m| <= 1 (equal to 1, of the form e(theta m), or random; Hoelder twice, as in Wooley (1995), Lemma 3.1): largest lhs/rhs = {worst:.3f} in {nc} random cases with k in {{2,3}}") # (d) Lemma 9.9: the counting function N(p^e) and the two sums over pairs okd = True; worst = 0.0 for p in (2, 3, 5, 7, 11): for e in range(1, 7): if p**e > 800: continue for k in (2, 3, 4, 5, 6, 8): pw = np.array([pow(x, k, p**e) for x in range(p**e)]); cnt = np.bincount(pw, minlength=p**e) rho = int(np.sum(cnt.astype(np.int64)**2)) okd &= (rho <= 2*k*(e+1)*p**(2*e-2*e/k)*(1+1e-12)); worst = max(worst, rho/(2*k*(e+1)*p**(2*e-2*e/k))) def N_of(g, k): pw = np.array([pow(x, k, g) for x in range(g)]); cnt = np.bincount(pw, minlength=g) return int(np.sum(cnt.astype(np.int64)**2)) worst2 = 0.0 for k in (2, 3, 4): N = 60; nk = np.arange(1, N+1, dtype=np.int64)**k; hmat = nk[:, None]-nk[None, :] for q in (1, 2, 6, 8, 9, 12, 16, 25, 27, 30, 36, 48, 49, 60): lhs = float(np.sum(np.gcd(q, np.abs(hmat)).astype(float)**(2/k))) rhs = 4*N*N*sum(g**(2/k-2)*N_of(g, k) for g in range(1, q+1) if q % g == 0) worst2 = max(worst2, lhs/rhs) worst3 = 0.0 for _ in range(300): k = int(rng.integers(3, 7)); N = int(rng.integers(2, 60)); M = float(np.exp(rng.uniform(0, 6))); be = float(np.exp(rng.uniform(-45, 2))) nk = np.arange(1, N+1, dtype=float)**k; hm = np.abs(nk[:, None]-nk[None, :]); off = hm > 0 lhs = float(np.sum(np.minimum(M*M, (be*hm[off])**(-2/k)))) rhs = 12*(1+log(N))*(M*N)**2*(1+(M*N)**k*be)**(-2/k) worst3 = max(worst3, lhs/rhs) check(okd and worst2 <= 1 and worst3 <= 1, f"Lemma 9.9: N(p^e) <= 2k(e+1)p^(2e-2e/k) (p^e <= 800; largest ratio {worst:.3f}); sum over d1, d2 <= M_1 of (q,d1^k-d2^k)^(2/k) <= 4 M_1^2 sum_(g|q) g^(2/k-2) N(g)" f" (largest ratio {worst2:.3f}); sum over d1 != d2 of min(Y^2,|beta h|^(-2/k)) <= 12(1+log M_1)(M_1 Y)^2(1+(M_1 Y)^k|beta|)^(-2/k) (largest ratio {worst3:.3f})") # (e) the arithmetic of the pruning argument oke = all(2-(4*k+1)/(2*k) <= -1/(2*k)+1e-15 and (4*k+1)/k-2 >= 2 and (1+1/k)/8+1/(4*k) <= 0.25 and 9/(32*k)+0.25 <= 0.5 and 1/(10*k)+1/(320*k**4)+1/(50*k) < 1/(4*k) and 0.25 <= 3/8-1/(4*k) and 0.25 <= 0.5-1/(4*k) for k in range(37, 10**5)) \ and (9/5)*4*(7/8)/1.01 > 6 and (9/8)*3 < 4 and 0.5*(1+1/16)**-1/1.001 > 1/3 Mg = np.exp(np.linspace(0, 60, 6001)); th = np.linspace(-0.5, 0.5, 20001)[1:-1] okk = True for M1 in (1.0, 2.5, 10.0, 137.0): ker = np.minimum(M1, 1/(2*np.abs(th)+1e-300)); okk &= (float(np.sum(ker)*(th[1]-th[0])) <= 1+log(M1)+1e-3) check(oke and okk, "the numerical inequalities in the proofs of Proposition 8.2 (Case 3), Lemmas 3.2, 9.7, 9.8 and 9.9, and of (9.4)-(9.6), for k >= 37;" " the integral of min(M, 1/(2|theta|)) over [-1/2,1/2] is 1 + log M") # ===================================================================================================================== # Part 11: Section 11 (a large sieve estimate). Not needed for the theorems of Section 1. # ===================================================================================================================== def G_inf(z): """the limit calG_inf of the potential calG of Section 8 as k -> infinity (Sections 2.2 and 11.4): 1/z for z >= 1/2, Psi(z) = 2 + log((1-z)/z) for 1/10 <= z <= 1/2, Psi(1/10) + 2 (I(1/5) - I(2z)) = C_0 - 2 I(2z) for z < 1/10""" if z >= 0.5: return 1/z if z >= 0.1: return 2 + log((1-z)/z) return 2 + log(9) + 2*(I(0.2) - I(2*z)) def fd(v): """the solution d in (0,1] of d + log d = 1 - v; k d is the exponent for the moment of order v k of smooth Weyl sums (this is the number written fd(vk) in the paper)""" lo, hi = 1e-300, 1.0 for _ in range(300): mid = sqrt(lo*hi) if mid + log(mid) > 1 - v: hi = mid else: lo = mid return hi def classes_mod(q, k): """for each reduced residue r mod q the index j(r) of r among the solutions x of x^k = r^k mod q; and the largest number of solutions""" roots = {} for r in range(q): if gcd(r, q) == 1 or q == 1: roots.setdefault(pow(r, k, q), []).append(r) idx = {} for c, lst in roots.items(): for j, r in enumerate(lst): idx[r] = j return idx, max(len(l) for l in roots.values()) def max_in_window(xs, delta): """the largest number of the points xs (Fractions in [0,1)) in a closed arc of length delta of R/Z""" xs = sorted(xs); n = len(xs) if n == 0: return 0 ext = xs + [x + 1 for x in xs] best = 1; j = 0 for i in range(n): if j < i: j = i while j + 1 < i + n and ext[j+1] - ext[i] <= delta: j += 1 best = max(best, j - i + 1) return best def num_prime_factors(q): n = 0; p = 2 while p*p <= q: if q % p == 0: n += 1 while q % p == 0: q //= p p += 1 return n + (1 if q > 1 else 0) def part11(): print("11. Section 11: the large sieve estimate (not needed for the theorems of Section 1)") rng = random.Random(20261124) # (a) Lemma 11.4 (spacing), in exact arithmetic: V_0 = M0, V_1 = M1, U V_0 = X worst = 0.0; ncase = 0; okcl = True; kinds = {"q > V_1": 0, "q <= V_1, count bound > 3": 0, "q <= V_1, count bound <= 3": 0} for trial in range(1500): k = rng.choice([2, 3, 4]) M0 = rng.randint(20, 120); M1 = rng.randint(M0 + 5, 2*M0) mode = rng.random() if mode < 0.35: q = rng.randint(1, 12) elif mode < 0.7: q = rng.randint(1, M1) else: q = rng.randint(M1 + 1, 40*M1) a = rng.randint(0, q) while gcd(a, q) != 1: a = rng.randint(0, q) tt = Fraction(rng.randint(-10**6, 10**6), 10**6) # |q beta - a| <= 1/(2 V_1^k) if rng.random() < 0.3: tt = tt/rng.choice([10, 1000, 10**5]) beta = Fraction(a, q) + tt/(2*q*M1**k) U = Fraction(rng.choice([1, 2, 3, 5, 10, 30, 100, 1000])) Xi = q + (U*M0)**k*abs(q*beta - a) delta1 = min(Fraction(1, 4*q), 1/U**k) K = 1 + 2*Fraction(M1)/Xi idx, nsol = classes_mod(q, k) okcl = okcl and nsol <= (2*k)**num_prime_factors(q) cls = {} for m in range(M0 + 1, M1 + 1): if gcd(m, q) == 1: cls.setdefault(idx[m % q] if q > 1 else 0, []).append((beta*m**k) % 1) for j, pts in cls.items(): wn = max_in_window(pts, delta1) worst = max(worst, float(wn/K)); ncase += 1 if wn > K: check(False, "Lemma 11.4 violated: k=%d q=%d V_0=%d V_1=%d" % (k, q, M0, M1)); return kinds["q > V_1" if q > M1 else ("q <= V_1, count bound > 3" if K > 3 else "q <= V_1, count bound <= 3")] += 1 check(okcl, "x^k = u (mod q) has at most (2k)^nu(q) solutions for (u,q) = 1, nu(q) the number of prime factors of q (the moduli of the next test)") check(True, "Lemma 11.4: every arc of length delta_1 contains at most 1 + 2 V_1/(q + (Y V_0)^k |q beta - a|) of the points beta m^k of a class:" " %d classes in 1500 random cases %s; largest ratio count/bound = %.3f" % (ncase, kinds, worst)) # (b) Lemmas 11.3 and 11.4 combined, as in the proof of Proposition 11.6 (an inequality without implied constants): # sum_{m in V_j} |f(beta m^k)|^(2 s0) <= A (1/delta_1 + pi N) sum_y |z_y|^2, f(x) = sum_{n in B} eps_n e(x n^k), f^s0 = sum_y z_y e(xy) worst = 0.0; ncl = 0 for trial in range(400): k = rng.choice([2, 3]); s0 = rng.choice([1, 2, 3]) M0 = rng.randint(15, 60); M1 = rng.randint(M0 + 5, 2*M0) U = rng.randint(3, 9) Bset = sorted(rng.sample(range(1, U), rng.randint(1, U - 1))) eps = {n: complex(np.exp(2j*pi*rng.random())) for n in Bset} mode = rng.random() q = rng.randint(1, 8) if mode < 0.35 else (rng.randint(1, M1) if mode < 0.7 else rng.randint(M1 + 1, 30*M1)) a = rng.randint(0, q) while gcd(a, q) != 1: a = rng.randint(0, q) tt = Fraction(rng.randint(-10**6, 10**6), 10**6) if rng.random() < 0.3: tt = tt/rng.choice([10, 1000, 10**5]) beta = Fraction(a, q) + tt/(2*q*M1**k) Xi = q + (U*M0)**k*abs(q*beta - a) delta1 = min(Fraction(1, 4*q), Fraction(1, U**k)); K = 1 + 2*Fraction(M1)/Xi coef = {0: 1.0+0j} # the coefficients of g^s0 for _ in range(s0): nc = {} for y, c in coef.items(): for n in Bset: nc[y + n**k] = nc.get(y + n**k, 0) + c*eps[n] coef = nc N = s0*(U - 1)**k; norm2 = sum(abs(c)**2 for c in coef.values()) idx, nsol = classes_mod(q, k) cls = {} for m in range(M0 + 1, M1 + 1): if gcd(m, q) == 1: x = float((beta*m**k) % 1) gval = sum(eps[n]*np.exp(2j*pi*x*n**k) for n in Bset) cls.setdefault(idx[m % q] if q > 1 else 0, []).append(abs(gval)**(2*s0)) for j, vals in cls.items(): ratio = sum(vals)/(float(K)*(float(1/delta1) + pi*N)*norm2); worst = max(worst, ratio); ncl += 1 if ratio > 1 + 1e-9: check(False, "combined inequality violated: k=%d s0=%d q=%d" % (k, s0, q)); return check(True, "Lemmas 11.3 and 11.4 combined, as in the proof of Proposition 11.6: %d classes in 400 random cases, largest ratio lhs/rhs = %.3f" % (ncl, worst)) # (c) Lemma 11.3: sum_j |Z(x_j)|^2 <= A (1/delta + pi N) sum |z_y|^2 worst = 0.0 for trial in range(300): N = rng.randint(5, 60); J = rng.randint(1, 40) b = np.array([complex(rng.gauss(0, 1), rng.gauss(0, 1)) for _ in range(N)]) xs = sorted(rng.random() if rng.random() < 0.6 else rng.choice([0.1, 0.1001, 0.5]) + 1e-3*rng.random() for _ in range(J)) delta = rng.choice([0.01, 0.03, 0.1, 0.3]) K = max_in_window([Fraction(x).limit_denominator(10**12) for x in xs], Fraction(delta).limit_denominator(10**12)) y = np.arange(1, N+1) lhs = sum(abs(np.sum(b*np.exp(2j*pi*x*y)))**2 for x in xs) worst = max(worst, lhs/(K*(1/delta + pi*N)*float(np.sum(abs(b)**2)))) check(worst <= 1.0, "Lemma 11.3 (large sieve with multiplicities): 300 random cases, largest ratio lhs/rhs = %.3f" % worst) # (d) the numbers following Corollary 11.2, and those of Section 11.4 D = TAU_INV xi1 = 1/(1 + 2*C0/D) check(0.4139 < xi1 < 0.4140 and 10.9034 < C0 + D/2 < 10.9035 and abs(D/2 - 4.5139505) < 1e-12 and 0.2922 < 1 - C0/D < 0.2923 and abs(2*C0 - 12.78) < 0.005 and abs(C0 - 6.39) < 0.005 and 0.00450 < fd(C0) < 0.00459, "xi_1 -> 1/(1 + 2 C_0/D) = %.5f; 1/tau^(1) = (C_0 + D/2) k = %.4f k; the large sieve exponent exceeds tau_k for xi < 1 - C_0/D = %.4f;" " 2 C_0 = %.3f; fd(C_0 k) = %.5f (D = 9.027901)" % (xi1, C0 + D/2, 1 - C0/D, 2*C0, fd(C0))) tv = tuple(round(2*C0*x/(1-x), 2) for x in (0.05, 0.1, 0.2, 0.3, 0.414, 0.5)) gv = tuple(round(G_inf(1/(2*C0*h)), 2) for h in (1, 2, 5, 10, 50)) # arcs of height P^h (Remark 11.8) c2 = lambda DD: -4/sqrt(DD/(DD+2)) + 2*log((1+sqrt(DD/(DD+2)))/(1-sqrt(DD/(DD+2)))) check(tv == (0.67, 1.42, 3.19, 5.48, 9.03, 12.78) and gv == (4.45, 5.02, 5.55, 5.82, 6.15) and (round(c2(8), 3), round(c2(C0), 3), round(c2(4), 3)) == (1.302, 0.794, -0.314) and abs(c2(D) - 1.57112) < 1e-5, "(11.2): 2 C_0 xi/(1-xi) = %s for xi = 0.05, 0.1, 0.2, 0.3, 0.414, 0.5; calG_inf(1/(2 C_0 h)) = %s for h = 1, 2, 5, 10, 50;" " c_2(8) = %.3f, c_2(C_0) = %.3f, c_2(4) = %.3f, c_2(9.027901) = %.5f (Remark 11.8)" % (tv, gv, c2(8), c2(C0), c2(4), c2(D))) # (e) Proposition 11.9: mean value exponents of F, and the exponent of the bilinear estimate (Lemma 9.4) zs = np.exp(np.linspace(log(1e-9), 0.0, 60001)) Gz = np.array([G_inf(float(z)) for z in zs]) d_inf = lambda v: max(0.0, float(np.max(zs*(Gz - v)))) vc = 1 - 1/9 + log(9) vs = np.linspace(0.05, 7.0, 1391) dFs = np.array([d_inf(float(v)) for v in vs]); dSs = np.array([fd(float(v)) for v in vs]) check(abs(vc - 3.0861) < 1e-4 and max(abs(dFs[i]-dSs[i]) for i in range(len(vs)) if vs[i] <= vc) < 2e-6 and all(0 < dFs[i] < dSs[i] for i in range(len(vs)) if vc + 0.02 < vs[i] < C0 - 0.01) and d_inf(C0) < 1e-9 and bool(np.all(np.diff(Gz) <= 1e-12)) and Gz[0] < C0 and C0 - Gz[0] < 1e-3, "Proposition 11.9(i): d_inf(v) = fd(vk) for v <= v_c = %.4f, 0 < d_inf(v) < fd(vk) for v_c < v < C_0, d_inf(C_0) = 0 (grid); calG_inf < C_0" % vc) # E(mu; v_A, v_B) = 2 [min(mu, 1-mu) - mu d_A - (1-mu) d_B]/(v_A v_B) (11.3) okmu = True; best = (0.0, None) H = lambda mu: 2*(min(mu, 1-mu) - mu*dFs[:, None] - (1-mu)*dFs[None, :])/(vs[:, None]*vs[None, :]) Hhalf = H(0.5) for mu in np.linspace(0.02, 0.98, 97): Hm = H(float(mu)); okmu = okmu and bool(np.all(Hm <= Hhalf + 1e-12)) i, j = np.unravel_index(np.argmax(Hm), Hm.shape) if Hm[i, j] > best[0]: best = (float(Hm[i, j]), (float(mu), float(vs[i]), float(vs[j]))) e22 = (1 - 2*fd(2))/4 om = 3.548292; bw = (1 - 2/om)/(1 - 1/om + log(om))**2 # Bruedern and Wooley (2023), Section 7: 1/9.026725 at v = 1 - 1/om + log(om) = 1.98464 ia = vs >= vc both = float(np.max(Hhalf[np.ix_(ia, ia)])) vA = np.linspace(vc, C0 + 0.3, 801); vB = np.linspace(0.02, vc, 1501) # the mixed range v_A >= v_c >= v_B dA = np.array([d_inf(float(v)) for v in vA]); dB = np.array([fd(float(v)) for v in vB]) Hmix = (1 - dA[:, None] - dB[None, :])/(vA[:, None]*vB[None, :]) im, jm = np.unravel_index(np.argmax(Hmix), Hmix.shape); mixed = float(Hmix[im, jm]) check(okmu and abs(best[0] - bw) < 2e-7 and abs(best[1][0]-0.5) < 1e-9 and abs(best[1][1]-1.985) < 0.006 and abs(best[1][2]-1.985) < 0.006 and abs(1/bw - 9.026725) < 2e-5 and abs(1 - 1/om + log(om) - 1.98464) < 1e-5 and abs(1/e22 - 9.02790) < 1e-5 and 1/vc**2 < 1/9.52 and both <= 1/vc**2 and mixed < 1/9.46, "Proposition 11.9(ii) and the remarks after it: E(mu) <= E(1/2) on the grid; E(1/2; 2, 2) = 1/%.5f; largest value on the grid %.6f = 1/%.4f" " at v_A = v_B = %.3f (Bruedern and Wooley (2023): 1/9.026725 at 1.98464); E <= 1/v_c^2 = 1/%.3f if v_A, v_B >= v_c (grid: %.4f);" " E(1/2) <= %.6f = 1/%.4f < 1/9.46 if v_A >= v_c >= v_B (attained at v_A = %.4f, v_B = %.3f)" % (1/e22, best[0], 1/best[0], best[1][1], vc**2, both, mixed, 1/mixed, vA[im], vB[jm])) dst = fd(2); zst = dst/(1+dst) check(abs(zst - 0.218) < 5e-4 and abs(theta_star(2*zst) - 1.13) < 5e-3 and 2/theta_star(2*zst) > 1/(1-zst), "at z = delta_*/(1+delta_*) = %.4f the largest permitted step is theta*(2z) = %.3f, and 2/theta*(2z) > 1/(1-z)" % (zst, theta_star(2*zst))) # ===================================================================================================================== # Part 12: the set of variables of Section 3 on a small example. Not needed for the proofs. # ===================================================================================================================== def part12(rng): print("12. Sections 3, 9 and 11: the set of variables on a small example (not needed for the proofs)") import itertools from math import comb, factorial, prod KK, RR, JJ = 3, 40, 3 # k, R, J; P = R^(J+1) PP = RR**(JJ+1) NMOD = 1000003 # exponential sums are evaluated at alpha = c/NMOD (a prime), exactly isprime = lambda p: p > 1 and all(p % t for t in range(2, int(p**0.5)+1)) members = {} for H in (1, 2): zeta = 2**H PR = [p for p in range(RR+1, zeta*RR+1) if isprime(p)] # the primes in (R, 2^H R] def cD(j, excl=()): # D_j^e, as increasing tuples of primes return list(itertools.combinations([p for p in PR if p not in excl], j)) def cC(j, X, excl=()): # C_j^e(X), as pairs (tuple of primes, m) return [(t, m) for t in cD(j, excl) for m in range(1, int(min(RR, X // prod(t)))+1)] attached = lambda j, X: float(zeta)**(j-JJ)*RR**(j+1)*(1-1e-12) <= X <= RR**(j+1) print(" H = %d: primes in (%d, %d], %d of them; P = %d" % (H, RR, zeta*RR, len(PR), PP)) # --- Lemma 3.1(b) ok = True; lo = 1e9; hi = 0.0 for j in range(JJ+1): for X in (float(RR**(j+1)), float(zeta)**((j-JJ)/2)*RR**(j+1), float(zeta)**(j-JJ)*RR**(j+1)): ok &= attached(j, X) S = cC(j, X); vals = [prod(t)*m for t, m in S] ok &= len(vals) == len(set(vals)) and all(v <= X for v in vals) and len(vals) <= X ok &= len(S) == sum(int(X // prod(t)) for t in cD(j)) # the condition m <= R is implied by dm <= X for t in cD(j): lo = min(lo, X/prod(t)/RR); hi = max(hi, X/prod(t)/RR) check(ok and lo >= float(zeta)**(-JJ)*(1-1e-12) and hi <= 1, "Lemma 3.1(b): n determines d and m; m <= R is implied by dm <= X; #C_j(X) <= X; 2^(-HJ) R <= X/d <= R (extreme values %.4f R and %.4f R)" % (lo, hi)) # --- Lemma 3.1(c) def Fx(jj, XX, mult, ex, c): # F_jj^ex(mult^k alpha; XX) at alpha = c/NMOD return sum(np.exp(2j*np.pi*((c*pow(mult*prod(t)*m, KK, NMOD)) % NMOD)/NMOD) for t, m in cC(jj, XX, ex)) ok = True; okatt = True; worst = 0.0; ncase = 10 if H == 1 else 6 for trial in range(ncase): j = int(rng.integers(1, JJ+1)); i = int(rng.integers(1, j+1)) X = float(rng.uniform(float(zeta)**(j-JJ), 1.0))*RR**(j+1) excl = tuple(int(x) for x in rng.choice(PR, size=int(rng.integers(0, 3)), replace=False)) c = int(rng.integers(1, NMOD)) lhs = comb(j, i)*Fx(j, X, 1, excl, c); rhs = 0 for t in cD(i, excl): d = prod(t); okatt &= attached(j-i, X/d) rhs += Fx(j-i, X/d, d, tuple(excl)+t, c) worst = max(worst, abs(lhs-rhs)); ok &= abs(lhs-rhs) < 1e-6 s1 = sum(1.0/p for p in PR) check(ok and okatt and s1 <= 1 and all(sum(1.0/prod(t) for t in cD(i)) <= 1 for i in range(1, JJ+1)), "Lemma 3.1(c): binom(j,i) F_j^e(alpha;X) = sum over d in D_i^e of F_(j-i)^(e+[d])(d^k alpha; X/d), with X/d attached to j-i" " (%d random cases; largest deviation %.1e); sum of 1/d over D_i <= 1 (sum of 1/p = %.4f)" % (ncase, worst, s1)) # --- Lemma 3.1(d), and the inclusion-exclusion in the proof of Lemma 6.1 ok = True for trial in range(ncase): j = int(rng.integers(1, JJ+1)) X = float(rng.uniform(float(zeta)**(j-JJ), 1.0))*RR**(j+1) excl = tuple(int(x) for x in rng.choice(PR, size=int(rng.integers(0, 2)), replace=False)) c = int(rng.integers(1, NMOD)) ph = lambda n: np.exp(2j*np.pi*((c*pow(n, KK, NMOD)) % NMOD)/NMOD) full = cC(j, X, excl) i1 = int(rng.integers(1, j+1)); cand = cD(i1, excl); t1 = cand[int(rng.integers(0, len(cand)))]; d1 = prod(t1) lhs = sum(ph(prod(t)*m) for t, m in full if (prod(t)*m) % d1 == 0) ok &= abs(lhs - Fx(j-i1, X/d1, d1, tuple(excl)+t1, c)) < 1e-6 i = int(rng.integers(1, min(j, 2)+1)); cand = cD(i, excl); td = cand[int(rng.integers(0, len(cand)))]; d = prod(td) cop = sum(ph(prod(t)*m) for t, m in full if gcd(prod(t)*m, d) == 1) fam = Fx(j, X, 1, tuple(excl)+td, c) ie = sum((-1)**r*Fx(j-r, X/prod(sub), prod(sub), tuple(excl)+sub, c) for r in range(0, i+1) for sub in itertools.combinations(td, r)) ok &= abs(cop-fam) < 1e-6 and abs(cop-ie) < 1e-6 check(ok, "Lemma 3.1(d): the terms with d_1 | n form F_(j-i_1)^(e+[d_1])(d_1^k alpha; X/d_1); the terms coprime to d form F_j^(e+[d])," " which equals the alternating sum over the divisors d_1 of d used in the proof of Lemma 6.1 (%d random cases)" % ncase) # --- the bijection in the proof of Lemma 9.6 (and of Lemma 11.5) ok = True for trial in range(ncase): j = int(rng.integers(1, JJ+1)); i = int(rng.integers(1, j+1)) X = float(rng.uniform(float(zeta)**(j-JJ), 1.0))*RR**(j+1) excl = tuple(int(x) for x in rng.choice(PR, size=int(rng.integers(0, 2)), replace=False)) M0 = RR**i; M1 = (zeta*RR)**i; Y = X/M0 cB = cC(j-i, Y, excl) pairs = [prod(td)*prod(tn)*m for td in cD(i, excl) for tn, m in cB if prod(td)*prod(tn)*m <= X and (max(tn) if tn else 0) < min(td)] ok &= sorted(pairs) == sorted(prod(t)*m for t, m in cC(j, X, excl)) ok &= all(M0 < prod(td) <= M1 for td in cD(i, excl)) and all(prod(tn)*m <= Y for tn, m in cB) check(ok, "Lemma 9.6: every element of C_j^e(X) is dn for exactly one pair with d in D_i^e, n in C_(j-i)^e(X/R^i), dn <= X and p^+(n) < p^-(d);" " D_i^e lies in (M_0, M_1] (%d random cases)" % ncase) # --- the two applications of Cauchy's inequality in (9.16), at alpha = c/NMOD if H == 1: i = (JJ+1)//2; M0 = RR**i; Y = PP//M0 Bset = sorted(prod(t)*m for t, m in cC(JJ-i, Y)); Di = [prod(t) for t in cD(i)] dk = np.array([pow(d, KK, NMOD) for d in Di], dtype=np.int64) Bk = np.array([pow(n, KK, NMOD) for n in Bset], dtype=np.int64) nk = np.array([pow(n, KK, NMOD) for n in range(1, Y+1)], dtype=np.int64) worst = 0.0 for trial in range(3): c = int(rng.integers(1, NMOD)); xi = np.exp(2j*np.pi*rng.uniform(0, 1, len(Di))) ph = np.exp(2j*np.pi*((c*((dk[:, None]*Bk[None, :]) % NMOD)) % NMOD)/NMOD) lhs = float(np.sum(np.abs(xi @ ph)))**2 rhs = 0.0 for a_ in range(len(Di)): hh = (dk[a_]-dk) % NMOD rhs += float(np.sum(np.abs(np.exp(2j*np.pi*((c*((hh[:, None]*nk[None, :]) % NMOD)) % NMOD)/NMOD).sum(axis=1)))) worst = max(worst, lhs/(Y*rhs)) check(worst <= 1, "(9.16): (sum over n in B of |sum over d of xi_d e(alpha d^k n^k)|)^2 <= Y sum over d_1, d_2 of |sum over n <= Y of e(alpha h n^k)|" " for i = 2, #D_i = %d, #B = %d, Y = %d (3 random cases; largest ratio %.3f)" % (len(Di), len(Bset), Y, worst)) # --- Lemma 3.2: the density DJ = cD(JJ); Pi = sum(1.0/prod(t) for t in DJ); s2 = sum(1.0/p**2 for p in PR) allC = cC(JJ, PP); members[H] = set(prod(t)*m for t, m in allC) check(s1**JJ - comb(JJ, 2)*s2*s1**(JJ-2) <= factorial(JJ)*Pi*(1+1e-12) and factorial(JJ)*Pi <= s1**JJ*(1+1e-12) and abs(len(allC) - Pi*PP) <= len(DJ), "Lemma 3.2: S_1^J - binom(J,2) S_2 S_1^(J-2) <= J! Pi <= S_1^J (%.6f <= %.6f <= %.6f); #C = %d = Pi P + O(#D_J) with Pi P = %.1f, #D_J = %d" % (s1**JJ - comb(JJ, 2)*s2*s1**(JJ-2), factorial(JJ)*Pi, s1**JJ, len(allC), Pi*PP, len(DJ))) if H == 2: cls = {} for t, m in allC: hv = tuple(sorted(0 if p <= 2*RR else 1 for p in t)); cls.setdefault(hv, set()).add(prod(t)*m) sizes = {hv: len(v) for hv, v in sorted(cls.items())} check(sum(sizes.values()) == len(members[2]) and cls[(0, 0, 0)] == members[1] and len(sizes) == comb(H+JJ-1, JJ), "Section 3.2 (H = 2): C is the disjoint union of the %d sets C(h_1,h_2,h_3), of sizes %s; C(0,0,0) is the set C that belongs to H = 1;" " the two sets C(h,h,h) contain %d of the %d elements" % (len(sizes), sizes, sizes[(0, 0, 0)]+sizes[(1, 1, 1)], len(members[2]))) # --- Lemma 9.10: the classical approximation with the factor Pi ns = np.array(sorted(prod(t)*m for t, m in allC), dtype=np.int64) uu = (np.arange(400000)+0.5)/400000 worst = 0.0; ok = True for q in (1, 2, 3, 5, 7): for a in range(1, q+1): if gcd(a, q) != 1: continue Sq = sum(np.exp(2j*np.pi*((a*pow(x, KK, q)) % q)/q) for x in range(1, q+1)) frac = ((a*np.array([pow(int(n), KK, q) for n in ns])) % q)/q for xi in (0.0, 0.5, -2.0, 7.0): Fv = np.sum(np.exp(2j*np.pi*(frac + xi*(ns/PP)**KK))) main = Pi*(Sq/q)*PP*np.mean(np.exp(2j*np.pi*xi*uu**KK)) bound_ = 3*len(DJ)*sqrt(q)*sqrt(1+abs(xi)) worst = max(worst, abs(Fv-main)/bound_); ok &= abs(Fv-main) <= bound_ check(ok, "Lemma 9.10: |F(a/q + xi P^-k) - Pi q^-1 S(q,a) P V(xi)| <= 3 #D_J q^(1/2) (1+|xi|)^(1/2) for q <= 7 and xi = 0, 0.5, -2, 7" " (largest ratio %.3f; main term up to %.0f)" % (worst, Pi*PP)) # --- the kernels of Lemmas 9.6 and 11.5 (the integrands are trigonometric polynomials; the quadrature is exact) M1 = 50; Nq = 4*M1+8; th = (np.arange(Nq)+0.5)/Nq - 0.5 ex = lambda x: np.exp(2j*np.pi*np.asarray(x, dtype=float)) ok = True for d in range(1, M1+1): for y in (0.5, 3.0, 17.2, 49.0, 60.0): Ky = sum(ex(-l*th) for l in range(1, int(min(y, M1))+1)) if min(y, M1) >= 1 else 0*th ok &= abs(np.mean(ex(d*th)*Ky) - (1.0 if d <= y else 0.0)) < 1e-9 B = 160; Nq = 4*B+8; th = (np.arange(Nq)+0.5)/Nq - 0.5 for a in range(0, B+1, 7): Ka = sum(ex(l*th) for l in range(a+1, B+1)) if a < B else 0*th # K~_a = sum over a < l <= 2^H R Kc = sum(ex(-l*th) for l in range(0, a)) if a >= 1 else 0*th # K'_c with c = a for b in range(0, B+1, 5): if b >= 1: ok &= abs(np.mean(ex(-b*th)*Ka) - (1.0 if b > a else 0.0)) < 1e-9 ok &= abs(np.mean(ex(b*th)*Kc) - (1.0 if b < a else 0.0)) < 1e-9 check(ok, "the kernels of Lemmas 9.6 and 11.5: int e(d psi) K_y = [d <= y], int e(-c psi) K~_a = [c > a], int e(a psi) K'_c = [a < c]" " (d <= 50; a, c <= 160)") def part13(): print("13. Section 9.6: the congruences of Lemma 9.13") okr = True for k in (4, 8, 16, 32, 64, 128): odd4 = {pow(x, k, 4*k) for x in range(1, 8*k+1, 2)}; even4 = {pow(x, k, 4*k) for x in range(2, 8*k+1, 2)} okr &= (odd4 == {1} and even4 == {0}) even8 = {pow(x, k, 8*k) for x in range(2, 16*k+1, 2)} okr &= (even8 == {0}) if k >= 8 else (even8 == {0, 16}) check(okr, "k = 4, 8, ..., 128: x^k = 1 (mod 4k) for odd x and x^k = 0 (mod 4k) for even x; x^k = 0 (mod 8k) for even x when k >= 8" " (for k = 4 the residue 16 modulo 32 occurs)") okd = True; msg = [] for k in (8, 16): q = 8*k res = sorted({pow(x, k, q) for x in range(q)}) reach = {0}; first = {} # reach: the sums of exactly i k-th powers modulo 8k; first[a]: the least such i for i in range(1, 4*k+1): reach = {(a+b) % q for a in reach for b in res} for a in reach: first.setdefault(a, i) okd &= res == [0, 1, 4*k+1] and first[4*k] == 4*k and all(first[j] == j for j in range(1, 4*k)) msg.append(f"k = {k}: the class of 4k = {4*k} modulo {q} needs {first[4*k]} k-th powers") check(okd, "least number of k-th powers whose sum lies in a given class modulo 8k: it is j for the class of j when 1 <= j < 4k; " + "; ".join(msg)) def part14(rng): print("14. Sections 9.7 and 11.5: Theorem 2.5(i) from below, the lemma of Harman, the end of the proof of Theorem 2.7") # |F(alpha)| >= #C/2 for |alpha| <= 1/(4 pi P^k), for any set C of natural numbers n <= P ok = True for trial in range(200): k = int(rng.integers(2, 7)); P = int(rng.integers(20, 400)) Cset = np.unique(rng.integers(1, P+1, size=int(rng.integers(5, P)))).astype(float) al = np.linspace(-1, 1, 41)/(4*np.pi*float(P)**k) Fv = np.abs(np.exp(2j*np.pi*np.outer(al, Cset**k)).sum(axis=1)) ok &= bool(np.all(Fv >= 0.5*len(Cset) - 1e-9)) check(ok, "|sum_{n in C} e(alpha n^k)| >= #C/2 for |alpha| <= 1/(4 pi P^k) (200 random sets C in [1, P], 2 <= k <= 6)") # the lemma of Harman: if ||x_n|| >= 1/L for all n <= N, then sum_{l <= L} |sum_n e(l x_n)| >= N/6 worst = 1e9 for trial in range(600): L = int(rng.integers(2, 80)); N = int(rng.integers(3, 500)); mode = trial % 6 if mode == 0: x = rng.uniform(1/L, 1 - 1/L, N) elif mode == 1: x = np.where(rng.random(N) < 0.5, 1/L, 1 - 1/L) elif mode == 2: x = 1/L + (1 - 2/L)*rng.beta(0.3, 0.3, N) elif mode == 3: x = 1/L + (1 - 2/L)*(np.arange(N) + 0.5)/N elif mode == 4: x = 1/L + (1 - 2/L)*rng.beta(2.0, 2.0, N) else: # density close to 1 - Fejer kernel, cut off at 1/L y = rng.random(8*N); keep = rng.random(8*N) < 1 - (np.sin(np.pi*(L+1)*y)/((L+1)*np.sin(np.pi*y)))**2 x = y[keep & (np.minimum(y, 1-y) >= 1/L)][:N] if len(x) < 3: x = rng.uniform(1/L, 1 - 1/L, N) assert np.all(np.minimum(x, 1-x) >= 1/L - 1e-12) l = np.arange(1, L+1)[:, None] tot = float(np.abs(np.exp(2j*np.pi*l*x[None, :]).sum(axis=1)).sum()) worst = min(worst, tot/len(x)) check(worst > 1/6, f"if ||x_n|| >= 1/L for all n, then sum_{{l <= L}} |sum_n e(l x_n)| >= N/6: 600 data sets, least value of the ratio to N: {worst:.3f}") # the end of the proof of Theorem 2.7: q <= P^(k/(k+1)), |q l alpha - a| <= P^(k/(k+1)-k), l <= L < P^(1/(k+1)) give n = q l <= P and # ||alpha n^k|| <= L^(k-1) P^(-k/(k+1)) < 1/L oke = all(Fraction(k-1)*Fraction(k, k+1) + Fraction(k, k+1) - k == -Fraction(k, k+1) for k in range(2, 2000)) okn = True for trial in range(300): k = int(rng.integers(2, 6)); P = int(rng.integers(10**3, 10**5)) Lmax = int(P**(1/(k+1))) + 1 while Lmax**(k+1) >= P: Lmax -= 1 # L < P^(1/(k+1)) Q = int(P**(k/(k+1))) + 1 while Q**(k+1) > P**k: Q -= 1 # Q <= P^(k/(k+1)) L = int(rng.integers(1, Lmax+1)); q = int(rng.integers(1, Q+1)); l = int(rng.integers(1, L+1)); a = int(rng.integers(0, q*l+1)) delta = Fraction(int(rng.integers(-10**6, 10**6+1)), 10**6)*Fraction(Q, P**k) # q l alpha - a, |delta| <= Q P^(-k) alpha = (Fraction(a) + delta)/(q*l) n = q*l; z = alpha*n**k; dist = abs(z - round(z)) okn &= (n <= P) and dist <= (q*l)**(k-1)*abs(delta) and dist*L < 1 check(oke and okn, "the exponent identity (k-1)k/(k+1) + k/(k+1) - k = -k/(k+1); n = ql <= P and ||alpha n^k|| <= (ql)^(k-1) |q l alpha - a| < 1/L" " on 300 random examples in exact arithmetic") # ===================================================================================================================== # Appendix A: the mean value with two coefficients, chains, and the improved bounds # ===================================================================================================================== def _require(cond, msg="a hypothesis on which the rigour of a bound depends is not satisfied"): """the hypotheses on which the rigour of a bound depends are enforced by this function and not by assert statements, so that they are tested also when Python is run with the option -O""" if not cond: raise RuntimeError(msg) A_NT = 10**6 _A_D = np.arange(4, A_NT+1, dtype=np.float64) A_CS = np.concatenate([[0.0], np.cumsum(np.log1p(2*(_A_D+1)/_A_D**2)/(_A_D+1))]) # A_CS[i] = sum_{4 <= D < 4+i} Lambda_D A_S4_LO = float(A_CS[-1]) + 2/(A_NT+1.5) # 2/(D^2+D+1) <= Lambda_D <= 2/D^2 A_S4_HI = float(A_CS[-1]) + 2/A_NT A_PIECES = [log(2)/3, log(17/9)/4, log(5/3)/4, log(7/6)/5] A_LAMBDA_LO = sum(A_PIECES) + A_S4_LO A_LAMBDA = sum(A_PIECES) + A_S4_HI # upper value of Lambda^star, (A.1) A_C0_LO = 2 + log(9) + 2*A_LAMBDA_LO A_C0 = 2 + log(9) + 2*A_LAMBDA # upper value of C_0^star, (A.1) def A_lam(D): """Lambda_D = log(1 + 2(D+1)/D^2)/(D+1)""" return log1p(2*(D+1)/D**2)/(D+1) def A_tail_lo(n): """lower bound for sum_{D >= n} Lambda_D (n >= 67): sum_{D >= N} Lambda_D >= 2/N - 2/N^3 for N >= 67 (proof of Proposition A.19)""" N = A_NT + 1 return (float(A_CS[-1] - A_CS[n-4]) + 2/N - 2/N**3) if n <= A_NT else 2/n - 2/n**3 def A_saving(U, vt, k, window=14): """The number varsigma^star(U, vartheta) of Definition A.11, from the definition; U is an array. Only the degrees D with |D - U| <= window are used, so that the value returned is a lower bound in general.""" U = np.asarray(U, dtype=np.float64) best = np.maximum(1 - U/2, 0.0) for j in (1, 2, 3): rho = (j+2-U)/(2**(j+2)-2) best = np.maximum(best, np.where((U >= j+2.0**(-j)) & (rho >= vt), rho - vt/2, 0.0)) D0 = np.floor(U) for off in range(-3, window+1): D = np.minimum(np.maximum(D0+off, 3), k-1); a0 = U/(D+1) E1 = 2*a0-(D-1)*vt; E2 = D-1-(D-2)*a0-(2*D-1)*vt; E3 = (2*D-2)*(1-a0)-(3*D-1)*vt; E4 = D+1-U-D*vt val = np.minimum.reduce([np.ones_like(U), E1, E2, E3, E4])/(D*(D+1)) best = np.maximum(best, np.where(a0 + vt < 1, val, 0.0)) return best # --- chains (Lemma A.14): for a level t and a height vt, the intervals # I_0 = (1, 2-2t], I_j = [j+2^-j, min(j+2-(2^(j+2)-2)(t+vt/2), j+2-(2^(j+2)-2)vt)] (j = 1, 2, 3), # T_D = [l_D, r_D] (D >= 3, D(D+1)t <= 1) # consist of numbers U with varsigma^star(U, vt) >= t. For a chain I_0, I_1, [I_2], T_Ds, ..., T_Dl (Ds = 3 or 4; I_3 # may be inserted between T_3 and T_4) the conditions "each interval begins before its predecessor ends" and # "k vt lies below the right end of the last interval" are linear in (t, vt); solved for vt they read vt <= ... . def A_junctions(t, Dlo, Dhi): """J[D-Dlo, :] = the largest vt with l_{D+1} <= r_D at the levels t, for Dlo <= D < Dhi""" D = np.arange(Dlo, Dhi, dtype=np.float64)[:, None]; tt = t[None, :] tauD = D*(D+1)*tt; tauN = (D+1)*(D+2)*tt; lhs = D*(D+2)/2 c4 = (D+1 - tauD - (D+2)*tauN/2) / (lhs + D) c3 = ((D+1) - (D+1)*tauD/(2*D-2) - (D+2)*tauN/2) / (lhs + (D+1)*(3*D-1)/(2*D-2)) c2 = ((D+1)*(D-1-tauD)/(D-2) - (D+2)*tauN/2) / (lhs + (D+1)*(2*D-1)/(D-2)) return np.minimum(np.minimum(c4, c3), c2) def A_ends(k, t, Dlo, Dhi): """E[D-Dlo, :] = the largest vt with k vt <= r_D at the levels t, for Dlo <= D <= Dhi""" D = np.arange(Dlo, Dhi+1, dtype=np.float64)[:, None]; tt = t[None, :]; tauD = D*(D+1)*tt e4 = (D+1-tauD)/(k+D) e3 = (D+1)*(1-tauD/(2*D-2))/(k+(D+1)*(3*D-1)/(2*D-2)) e2 = (D+1)*(D-1-tauD)/(D-2)/(k+(D+1)*(2*D-1)/(D-2)) return np.minimum(np.minimum(e4, e3), e2) def A_band_static(t, Dband): """the conditions that do not depend on k, at the levels t (all with D(D+1)t <= 1 for D <= Dband)""" n = t.shape[0]; one8 = np.full_like(t, 0.125) pre = [np.minimum((3-30*t)/7, (3-24*t)/10), # I_0, I_1, T_3 np.minimum.reduce([0.25-2*t, one8, (4-38*t)/11, (4-24*t)/18]), # I_0, I_1, I_2, T_3 np.minimum((3-56*t)/10.5, (3-50*t)/13.5), # I_0, I_1, T_4 np.minimum.reduce([0.25-2*t, one8, (4-64*t)/14.5, (4-50*t)/21.5])] # I_0, I_1, I_2, T_4 inf = np.full((1, n), np.inf) chains = [] # (first tent, c) with c[i] = minimum of the conditions for the last tent Ds+i if Dband > 3: J = A_junctions(t, 3, Dband) Jb = J.copy() # T_3, I_3, T_4 in place of T_3, T_4 Jb[0] = np.minimum.reduce([(7/8-12*t)/3, (7/8-12*t)/8, (39/8-48*t)/20, (5-80*t)/22.5, (5-50*t)/37.5]) c3 = np.vstack([inf, np.minimum.accumulate(J, axis=0)]) c3b = np.vstack([inf, np.minimum.accumulate(Jb, axis=0)]) c4 = np.vstack([inf, np.minimum.accumulate(J[1:], axis=0)]) if Dband > 4 else inf for pi in (0, 1): chains.append((3, np.minimum(pre[pi][None, :], c3))) chains.append((3, np.minimum(pre[pi][None, :], c3b))) for pi in (2, 3): chains.append((4, np.minimum(pre[pi][None, :], c4))) else: for pi in (0, 1): chains.append((3, pre[pi][None, :].copy())) r3 = np.minimum.reduce([(7/8-12*t)/3, (7/8-12*t)/8, (39/8-48*t)/20]) return dict(chains=chains, j3=[np.minimum(pre[0], r3), np.minimum(pre[1], r3)]) # j3: ..., T_3, I_3 (last) def A_top_types(k, t): """chains I_0, I_1 and I_0, I_1, I_2 (no tent)""" a = np.minimum((3-6*t)/(1+3.0/k), 3/(1+6.0/k)) b = np.minimum.reduce([(4-14*t)/(1+7.0/k), np.full_like(t, 4/(1+14.0/k)), k*(0.25-2*t), np.full_like(t, k/8.0)]) return np.array([a, b]) def A_band_values(k, t, Dband, static=None): """lower bounds for theta_k(t) at the levels t, one row for each chain (rows = type of the chain, last interval)""" Dband = min(Dband, k-1) if static is None: static = A_band_static(t, Dband) E = A_ends(k, t, 3, Dband) vals = [np.minimum(c, E[Ds-3:]) for Ds, c in static['chains']] e = np.minimum((5-30*t)/(k+15.0), np.full_like(t, 5/(k+30.0))) vals.append(np.vstack([np.minimum(p, e) for p in static['j3']])) return np.vstack([k*np.vstack(vals), A_top_types(k, t)]) def A_cell_sum(edges, th): """An upper bound for the integral of dt/(t theta(t)) over [edges[0], edges[-1]]: each row of th contains the values at the edges of a concave minorant of theta_k (one chain); on each cell one row is used throughout, and its chord is a minorant. The integral of dt/(t (A - B t)) is evaluated in closed form.""" t1, t2 = edges[:-1], edges[1:]; a, b = th[:, :-1], th[:, 1:] good = (a > 1.0) & (b > 1.0) & (a >= b) am = np.where(good, a, 0.0); bm = np.where(good, b, 0.0) cols = np.arange(a.shape[1]); best = np.full(a.shape[1], np.inf) for idx in (am.argmax(axis=0), bm.argmax(axis=0), (am+bm).argmax(axis=0)): aa, bb = am[idx, cols], bm[idx, cols] with np.errstate(divide='ignore', invalid='ignore'): A = (aa*t2 - bb*t1)/(t2 - t1) c = np.log(t2*aa/(t1*bb))/A best = np.minimum(best, np.where((bb > 1.0) & np.isfinite(c), c, np.inf)) _require(np.all(np.isfinite(best)), "no chain is usable on some cell") return float(best.sum()) A_M, A_MTOP, A_MTAIL = 32, 64, 256 # numbers of cells _A_CACHE = {} def A_band_edges(D): if D not in _A_CACHE: hi = 1.0/(D*(D+1)); lo = 1.0/((D+1)*(D+2)) e = np.geomspace(lo, hi, A_M+1); e[0] = lo; e[-1] = hi _A_CACHE[D] = (e, A_band_static(e, D)) return _A_CACHE[D] def A_zk(k): h = h_k(k); return (1-h)*(0.1-h/50) def A_Dc(k): """the last band of the subdivision is that of D = A_Dc(k) + 1 at most, so that the chains end with a tent T_D with D <= min{ceil(1.45 sqrt k) + 7, floor(k/2) - 1}, as in Section A.7; the second term, which keeps the values of the chains below k/2, matters only for k < 37 (in fact only for k = 17)""" return min(int(ceil(1.45*sqrt(k))) + 6, k//2 - 2) def A_grid(k): """the subdivision of [2 tau_k, 2 z_k]: list of (edges, Dband) with Dband = 0 for the range (1/12, 2 z_k]; the points hat t = (k-9)/(8k) and check t = (k-20)/(6(3k+10)) are inserted where they fall (Section A.8)""" tlo, thi = 2*tau_k(k), 2*A_zk(k); kh, kc = (k-9)/(8*k), (k-20)/(6*(3*k+10)) e = np.geomspace(1/12, thi, A_MTOP+1); e[0] = 1/12; e[-1] = thi if 1/12 < kh < thi: e = np.sort(np.append(e, kh)) out = [(e, 0)]; D = 3 while True: hi = 1.0/(D*(D+1)); lo = 1.0/((D+1)*(D+2)) if lo <= tlo or D > A_Dc(k) or D >= k-1: break e = A_band_edges(D)[0] if D == 3 and lo < kc < hi: e = np.sort(np.append(e, kc)) out.append((e, D)); D += 1 e = np.geomspace(tlo, hi, A_MTAIL+1); e[0] = tlo; e[-1] = hi out.append((e, D)) return out def A_bound(k): """(B^natural(k), Theta^natural(k)) in plain double precision (a cross-check for A_bound_rig): an upper bound for Psi(z_k) + 2 int_{2tau_k}^{2z_k} dt/(t theta_k(t)) - (1/k) log(z_k/tau_k) + 1/k = (Y_k(Theta_k) + 1)/k, and a lower bound for Theta_k = theta_k(2 tau_k)""" total = 0.0; grid = A_grid(k) for i, (e, D) in enumerate(grid): if D == 0: th = A_top_types(k, e) elif i < len(grid)-1 and e is A_band_edges(D)[0]: th = A_band_values(k, e, D, A_band_edges(D)[1]) else: th = A_band_values(k, e, D) total += A_cell_sum(e, th) z = A_zk(k) return 2 + log((1-z)/z) + 2*total + 1.0/k - log(TAU_INV*k*z)/k, float(th[:, 0].max()) def A_frac(k, Y, Th): """(s_0, b, E, x) of Section A.8 for the cap Th, with Th' = Th/(1 - Th/(2k)), so that k/Th' = k/Th - 1/2: x = Y - 2 chi_0 k/Th', 2 s_0 > x, b = 2/(11 Th'), E = (2 s_0 + k/(2 tau_k))/(1-b)""" x = Y - 2*CHI0*(k/Th - 0.5) s0 = max(k, floor(x/2) + 1); b = 2/(11*Th) - 1/(11*k) return s0, b, (2*s0 + (TAU_INV/2)*k)/(1-b), x # --- large k (Lemma A.18, Proposition A.19) def A_dC(k): """C^star(k) - C_0^star (the series cancels)""" h = h_k(k) return (log((45+6*h-h*h)/(9*(1-h)*(5-h))) + (2/3)*((1+3/k)*log(6*(k+3)*(1-h)*(5-h)/((k-9)*(15+12*h-2*h*h))) - log(2)) + 0.5*((1+7/k)*log(17*(k-9)/(9*(k+7))) - log(17/9)) + 0.5*((1+8/k)*log(5*(k+8)/(3*(k-20))) - log(5/3)) + 0.4*((1+15/k)*log(7*(k-20)/(6*(k+15))) - log(7/6))) def A_D0(k): """the largest integer D_0 with D_0^2 + 6 D_0 + 32 <= k""" n = int(sqrt(k-23)) - 3 while (n+1)**2 + 6*(n+1) + 32 <= k: n += 1 while n*n + 6*n + 32 > k: n -= 1 return n def A_Yreg_terms(k): """the five terms of Y^reg(k) - C_0^star k (Proposition A.19), with Theta = D_0/(1 + D_0/k); the last one is -log(z_k/tau_k), from the exact length of the steps""" n = A_D0(k); Th = n/(1+n/k) Ds = np.arange(4, n, dtype=np.float64); S = float((Ds*np.log1p(2*(Ds+1)/Ds**2)/(Ds+1)).sum()) return k*A_dC(k), -2*k*A_tail_lo(n), 2*S, (2*k/Th)*log(TAU_INV*k/(2*n*(n+1))), -log(TAU_INV*k*A_zk(k)), n, Th def A_exact(k, prec=40, grid=None): """A second, independently written evaluation of k B^natural(k) in decimal arithmetic with prec digits. The end points of the intervals of Lemma A.14 are written down as affine functions of vt, directly from the definitions, and the conditions on a chain are solved for vt mechanically. The levels are the (binary) numbers of A_grid(k), or of grid.""" from decimal import Decimal as Dm, getcontext getcontext().prec = prec kk = Dm(k); INF = Dm('Infinity'); half = Dm(1)/2 def solve(L, R): # largest vt with L0 + L1 vt <= R0 + R1 vt (L1 > R1) return (R[0]-L[0])/(L[1]-R[1]) def end(R): # largest vt with k vt <= R0 + R1 vt return R[0]/(kk-R[1]) def I_right(j, t): # right end points of I_j, j = 1, 2, 3: saving >= t, and rho >= vt c = Dm(2**(j+2)-2) return [(j+2-c*t, -c/2), (Dm(j+2), -c)] def T_left(D, t): tau = D*(D+1)*t return ((D+1)*tau/2, Dm((D+1)*(D-1))/2) def T_right(D, t): # E_2, E_3, E_4 >= tau tau = D*(D+1)*t; D = Dm(D) return [((D+1)*(D-1-tau)/(D-2), -(D+1)*(2*D-1)/(D-2)), ((D+1)*(1-tau/(2*D-2)), -(D+1)*(3*D-1)/(2*D-2)), (D+1-tau, -D)] def types_at(tf, Dband): """dictionary type -> largest theta = k vt for which the chain of this type works at the level tf""" t = Dm(tf); out = {} if not (Dm(3)/2 <= 2-2*t): return out r1 = I_right(1, t); r2 = I_right(2, t); r3 = I_right(3, t) p1 = INF # I_0, I_1 p2 = min(solve((Dm(9)/4, Dm(0)), R) for R in r1) # I_0, I_1, I_2 out[('top', 1)] = kk*min(p1, min(end(R) for R in r1)) out[('top', 2)] = kk*min(p2, min(end(R) for R in r2)) Dband = min(Dband, k-1) if Dband < 3: return out TL = {D: T_left(D, t) for D in range(3, Dband+1)} TR = {D: T_right(D, t) for D in range(3, Dband+1)} junc = {D: min(solve(TL[D+1], R) for R in TR[D]) for D in range(3, Dband)} bridge = min(min(solve((Dm(25)/8, Dm(0)), R) for R in TR[3]), min(solve(TL[4], R) for R in r3)) if Dband >= 4 else None ends = {D: min(end(R) for R in TR[D]) for D in range(3, Dband+1)} for pi, (pre, rr) in enumerate([(p1, r1), (p2, r2)]): for Ds in (3, 4): if Ds > Dband: continue start = min(pre, min(solve(TL[Ds], R) for R in rr)) for br in ((0, 1) if (Ds == 3 and Dband >= 4) else (0,)): cur = start for Dl in range(Ds, Dband+1): if Dl > Ds: cur = min(cur, bridge if (br and Dl == 4) else junc[Dl-1]) out[(pi, Ds, br, Dl)] = kk*min(cur, ends[Dl]) s3 = min(pre, min(solve(TL[3], R) for R in rr), min(solve((Dm(25)/8, Dm(0)), R) for R in TR[3])) out[('j3', pi)] = kk*min(s3, min(end(R) for R in r3)) return out total = Dm(0); Theta = None; one = Dm(1) for item in (grid if grid is not None else A_grid(k)): e, D = item[0], item[1] vals = [types_at(float(x), D) for x in e] for i in range(len(e)-1): t1, t2 = Dm(float(e[i])), Dm(float(e[i+1])); va, vb = vals[i], vals[i+1] keys = [ky for ky in va if ky in vb and va[ky] > one and vb[ky] > one and va[ky] >= vb[ky]] cand = {max(keys, key=lambda ky: va[ky]), max(keys, key=lambda ky: vb[ky]), max(keys, key=lambda ky: va[ky]+vb[ky])} best = None for ky in cand: a, b = va[ky], vb[ky] A = (a*t2 - b*t1)/(t2 - t1) c = (t2*a/(t1*b)).ln()/A best = c if best is None or c < best else best total += best Theta = max(vals[0].values()) z = (1-Dm(36)/(5*(k-1)))*(Dm(1)/10-Dm(36)/(5*(k-1))/50) B = 2 + ((1-z)/z).ln() + 2*total + one/kk - (Dm('9.027901')*kk*z).ln()/kk # - (1/k) log(z_k/tau_k) return kk*B, Theta # --- theta_k directly from Definition A.11 (Lemma A.14(i) and its converse): for t > 0 the set of U > 1 with # varsigma^star(U, vt) >= t is exactly the union of the intervals I_0, I_1, I_2, I_3 and T_D (3 <= D <= k-1, # D(D+1)t <= 1) of Lemma A.14. Hence sigma_k(theta) >= t if and only if these intervals, formed with # vt = theta/k, cover (1, theta]; this condition becomes weaker as theta decreases, and theta_k(t) is found by # bisection. No chain structure is assumed. def A_covered(k, t, th): """for arrays t, th of equal length: True where the intervals of Lemma A.14 with vt = th/k cover (1, th]""" t = np.asarray(t, float); th = np.asarray(th, float); vt = th/k; n = t.shape[0] Dmax = 3 while (Dmax+1)*(Dmax+2)*float(t.min()) <= 1 and Dmax+1 <= k-1: Dmax += 1 LO = [np.ones(n)]; HI = [2 - 2*t] for j in (1, 2, 3): c = 2.0**(j+2) - 2 LO.append(np.full(n, j + 2.0**(-j))); HI.append(np.minimum(j+2 - c*(t + vt/2), j+2 - c*vt)) D = np.arange(3, Dmax+1, dtype=np.float64)[None, :]; tt = t[:, None]; vv = vt[:, None] tD = D*(D+1)*tt l = 0.5*(D+1)*(tD + (D-1)*vv) r = np.minimum.reduce([(D+1)*(D-1-tD-(2*D-1)*vv)/(D-2), (D+1)*(1 - (tD + (3*D-1)*vv)/(2*D-2)), D+1-tD-D*vv]) ok = (tD <= 1) & (l <= r) LO = np.hstack([np.array(LO).T, np.where(ok, l, np.inf)]); HI = np.hstack([np.array(HI).T, np.where(ok, r, -np.inf)]) o = np.argsort(LO, axis=1, kind='stable'); LO = np.take_along_axis(LO, o, 1); HI = np.take_along_axis(HI, o, 1) cm = np.maximum.accumulate(HI, axis=1) gap = LO[:, 1:] > cm[:, :-1] first = np.argmax(gap, axis=1) reach = np.where(gap.any(axis=1), cm[np.arange(n), first], cm[:, -1]) return (th <= 1) | (reach >= th) def A_theta_direct(k, t, iters=64): """(lo, hi) with lo <= theta_k(t) < hi for an array of levels 0 < t <= 1/5 (bisection on theta in [1, k/2])""" t = np.atleast_1d(np.asarray(t, float)) lo = np.ones_like(t); hi = np.full_like(t, k/2.0) top = A_covered(k, t, hi); lo = np.where(top, hi, lo) for _ in range(iters): mid = 0.5*(lo + hi); c = A_covered(k, t, mid) | top lo = np.where(c, mid, lo); hi = np.where(c, hi, mid) return lo, np.where(top, lo, hi) def A_V_quadrature(k, cap=None, sub=8, nodes=12): """Psi(z_k) + 2 int_{2 tau_k}^{2 z_k} dt/(t min{cap, theta_k(t)}) - (1/k) log(z_k/tau_k) + 1/k by Gauss-Legendre quadrature in log t on the ranges between the points 1/(D(D+1)), 1/8, 1/18, 1/20 (not rigorous; theta_k may jump inside a range)""" a0, b0 = 2*tau_k(k), 2*A_zk(k) bps = {a0, b0, 1/8, 1/18, 1/20} D = 3 while 1/(D*(D+1)) > a0 and D < k: bps.add(1/(D*(D+1))); D += 1 bps = sorted(x for x in bps if a0 <= x <= b0) x, w = np.polynomial.legendre.leggauss(nodes) S, W = [], [] for a, b in zip(bps[:-1], bps[1:]): e = np.geomspace(a, b, sub+1) for c, d in zip(e[:-1], e[1:]): sa, sb = log(c), log(d) S.append(0.5*(sb-sa)*x + 0.5*(sb+sa)); W.append(0.5*(sb-sa)*w) S = np.concatenate(S); W = np.concatenate(W) th = A_theta_direct(k, np.exp(S))[0] if cap is not None: th = np.minimum(th, cap) z = A_zk(k) return 2 + log((1-z)/z) + 2*float(np.dot(W, 1/th)) + 1/k - log(TAU_INV*k*z)/k def A_pairs_test(seed=1, ntr=200): """Lemma A.8 on random configurations: exact counts against the bound of the lemma and against the intermediate bounds of its proof. A configuration consists of 3 <= k <= 12, 2 <= D <= k-1 (u = k - D), one to three primes exceeding k, moduli r_1 | r_2 composed of these primes (r_1 = 1 and r_1 = r_2 occur), integers b_1, b_2 coprime to them, an interval of L consecutive integers, which may contain negative ones, and rational numbers delta_1, delta_2 of several sizes; the sets V are the largest class of equivalent integers in the interval and one further class. Returns (number of sets V, number of failures, the largest ratio count/bound, the numbers of configurations with r_1 = 1 and with r_1 = r_2, the number of sets V with pairs of the third kind (r_1 does not divide n - n')).""" rnd = random.Random(seed); Fr = Fraction P = [p for p in range(2, 200) if all(p % d for d in range(2, int(p**0.5) + 1))] def classes_mod_p(p, u): Gam = [w for w in range(1, p) if pow(w, u*(u+1), p) == 1]; label = {} for x in range(1, p): if x in label: continue rp = min(x*w % p for w in Gam) for w in Gam: label[rp*w % p] = w return label def dist(num, q): r = num % q return Fr(min(r, q - r), q) nV = nbad = n1 = neq = nthird = done = 0; worst = Fr(0) while done < ntr: k = rnd.randint(3, 12); D = rnd.randint(2, k-1); u = k - D; J = rnd.choice([1, 1, 2, 2, 3]) ps = rnd.sample([x for x in P if x > k][:6], J); m = 1 for p in ps: m *= p e2 = [rnd.randint(0, 4 if J == 1 else 2) for _ in ps]; e1 = [rnd.randint(0, e) for e in e2]; r1 = r2 = 1 for p, a, b in zip(ps, e1, e2): r1 *= p**a; r2 *= p**b if r2 > 3*10**5: continue def unit(q): while True: b = rnd.randint(1, 10*q + 5) if gcd(b, q) == 1: return b b1, b2 = unit(r1), unit(r2) L = rnd.choice([rnd.randint(5, 60), rnd.randint(60, 400), rnd.randint(400, 900)]); start = rnd.randint(-2*L, 10**4) labs = {p: classes_mod_p(p, u) for p in ps}; cls = {} for n in range(start, start + L): if gcd(n, m) == 1: cls.setdefault(tuple(labs[p][n % p] for p in ps), []).append(n) def rdelta(q): t = rnd.random() if t < 0.3: return Fr(rnd.randint(1, 4), max(1, q))*rnd.choice([1, 1, 2, 3]) if t < 0.6: return Fr(1, rnd.randint(2, 50)) if t < 0.8: return Fr(1, rnd.randint(50, 5000)) return Fr(rnd.randint(1, 9), 10) d1, d2 = rdelta(r1), rdelta(r2); dr2 = sum(1 for d in range(1, r2 + 1) if r2 % d == 0) if r2 <= 2000 else None if dr2 is None: dr2 = 1 for e in e2: dr2 *= e + 1 N1 = lambda dl: Fr(2*L*L, r2) + 4*k**J*dr2*dl*(Fr(L*r2, r1*r1) + Fr(L*L, r1)) def N1mid(dl): # the bound before the last step of the proof tot = Fr(2*L*L, r2) for c in range(r1, r2, r1): if r2 % c == 0: tot += Fr(2*L, c)*2*u**J*dl*(Fr(r2, c) + L) return tot done += 1; n1 += r1 == 1; neq += r1 == r2 if not cls: continue keys = sorted(cls, key=lambda t: -len(cls[t]))[:1] + [rnd.choice(list(cls))] for key in keys: V = cls[key]; pu = {n: n**u for n in V}; pu1 = {n: n**(u+1) for n in V} def count_star(dl): return sum(1 for n in V for n2 in V if n != n2 and (n - n2) % r1 == 0 and dist(b2*(pu1[n] - pu1[n2]), r2) <= dl) M = Md = Ma = Mb = 0 for n in V: for n2 in V: if dist(b1*(pu[n] - pu[n2]), r1) <= d1 and dist(b2*(pu1[n] - pu1[n2]), r2) <= d2: M += 1 if n == n2: Md += 1 elif (n - n2) % r1 == 0: Ma += 1 else: Mb += 1 Ns1 = count_star(d2); Ns2 = count_star(2*d2) bound = L + N1(d2) + 2*d1*(r1*L + (r1 + L)*N1(2*d2)) ok = (Ma == Ns1) and (Ns1 <= N1mid(d2) <= N1(d2)) and (Ns2 <= N1mid(2*d2) <= N1(2*d2)) \ and (Mb <= 2*d1*r1*L + 2*d1*(r1 + L)*Ns2) and (M <= bound) and (Md == len(V) <= L) nV += 1; nbad += not ok; nthird += Mb > 0; worst = max(worst, Fr(M)/bound) return nV, nbad, float(worst), n1, neq, nthird # --- Appendix A: the evaluation with outward rounding. Every floating point operation whose result enters a bound is # followed by a step to the adjacent floating point number in the safe direction (np.nextafter), the logarithm is # allowed an error of A_LOG_ULP units in the last place, and all constants are rounded in the safe direction from # exact fractions. With IEEE 754 arithmetic (rounding to nearest) the numbers returned are therefore rigorous # bounds. The algorithm is that of A_bound (chains, chords, the same subdivision), except that the points of # division are the largest floating point numbers not exceeding 1/(D(D+1)), the subdivision covers a range that # contains [2 tau_k, 2 z_k], and the chords are capped at the lower bound Theta' for Theta^nat(k). from fractions import Fraction as _Fr from math import fsum A_LOG_ULP = 4 _U53 = 2.0**-53 def _dn(x): return np.nextafter(x, -np.inf) def _up(x): return np.nextafter(x, np.inf) def _dnk(x, n): for _ in range(n): x = np.nextafter(x, -np.inf) return x def _upk(x, n): for _ in range(n): x = np.nextafter(x, np.inf) return x def _flo(fr): """the largest floating point number <= the fraction fr""" x = float(fr) return x if _Fr(x) <= fr else float(np.nextafter(x, -np.inf)) def _fup(fr): """the smallest floating point number >= the fraction fr""" x = float(fr) return x if _Fr(x) >= fr else float(np.nextafter(x, np.inf)) def _lin_lo(P_lo, Q_up, R_lo, R_up, t): """a lower bound for (P - Q t)/R, where P >= P_lo, 0 <= Q <= Q_up, 0 < R_lo <= R <= R_up and t >= 0 is exact""" num = _dn(P_lo - _up(Q_up*t)) return np.where(num >= 0, _dn(num/R_up), _dn(num/R_lo)) _A_JC, _A_EC = {}, {} def _A_jconst(D): """the junction conditions l_{D+1} <= r_D as (P - Q t)/R: (P_lo, Q_up, R_lo, R_up) for the three terms of r_D""" if D not in _A_JC: d = _Fr(D); lhs = d*(d+2)/2; q2 = (d+2)*(d+1)*(d+2)/2 terms = [(d+1, d*(d+1) + q2, lhs + d), (d+1, (d+1)*d*(d+1)/(2*d-2) + q2, lhs + (d+1)*(3*d-1)/(2*d-2)), ((d+1)*(d-1)/(d-2), (d+1)*d*(d+1)/(d-2) + q2, lhs + (d+1)*(2*d-1)/(d-2))] _A_JC[D] = [(_flo(P), _fup(Q), _flo(R), _fup(R)) for P, Q, R in terms] return _A_JC[D] def _A_econst(D): """the conditions k vt <= r_D as (P - Q t)/(k + c): (P_lo, Q_up, c_lo, c_up) for the three terms of r_D""" if D not in _A_EC: d = _Fr(D) terms = [(d+1, d*(d+1), d), (d+1, d*(d+1)**2/(2*d-2), (d+1)*(3*d-1)/(2*d-2)), ((d+1)*(d-1)/(d-2), d*(d+1)**2/(d-2), (d+1)*(2*d-1)/(d-2))] _A_EC[D] = [(_flo(P), _fup(Q), _flo(c), _fup(c)) for P, Q, c in terms] return _A_EC[D] _A_TAB = dict(n=2) def _A_tables(Dhi): """the constants of _A_jconst and _A_econst for 3 <= D <= Dhi, as arrays of shape (D-3, term, 4)""" if _A_TAB['n'] < Dhi: n = max(Dhi, 2*_A_TAB['n'], 64) _A_TAB.update(n=n, J=np.array([_A_jconst(D) for D in range(3, n+1)]), E=np.array([_A_econst(D) for D in range(3, n+1)])) return _A_TAB['J'], _A_TAB['E'] def A_junctions_lo(t, Dlo, Dhi): """rigorous lower bounds for the numbers of A_junctions (rows Dlo <= D < Dhi)""" if Dhi <= Dlo: return np.zeros((0, t.shape[0])) C = _A_tables(Dhi)[0][Dlo-3:Dhi-3] P, Q, Rl, Ru = (C[:, :, i][:, :, None] for i in range(4)) num = _dn(P - _up(Q*t[None, None, :])) return np.where(num >= 0, _dn(num/Ru), _dn(num/Rl)).min(axis=1) def A_ends_lo(k, t, Dlo, Dhi): """rigorous lower bounds for the numbers of A_ends (rows Dlo <= D <= Dhi)""" C = _A_tables(Dhi)[1][Dlo-3:Dhi-2] P, Q, cl, cu = (C[:, :, i][:, :, None] for i in range(4)) num = _dn(P - _up(Q*t[None, None, :])) return np.where(num >= 0, _dn(num/_up(k + cu)), _dn(num/_dn(k + cl))).min(axis=1) def A_band_static_lo(t, Dband): n = t.shape[0]; L = lambda P, Q, R: _lin_lo(P, Q, R, R, t); one8 = np.full(n, 0.125) pre = [np.minimum(L(3, 30, 7), L(3, 24, 10)), np.minimum.reduce([L(0.25, 2, 1), one8, L(4, 38, 11), L(4, 24, 18)]), np.minimum(L(3, 56, 10.5), L(3, 50, 13.5)), np.minimum.reduce([L(0.25, 2, 1), one8, L(4, 64, 14.5), L(4, 50, 21.5)])] r3 = np.minimum.reduce([L(0.875, 12, 3), L(0.875, 12, 8), L(4.875, 48, 20)]) inf = np.full((1, n), np.inf); chains = [] if Dband > 3: J = A_junctions_lo(t, 3, Dband) Jb = J.copy(); Jb[0] = np.minimum.reduce([r3, L(5, 80, 22.5), L(5, 50, 37.5)]) c3 = np.vstack([inf, np.minimum.accumulate(J, axis=0)]) c3b = np.vstack([inf, np.minimum.accumulate(Jb, axis=0)]) c4 = np.vstack([inf, np.minimum.accumulate(J[1:], axis=0)]) if Dband > 4 else inf for pi in (0, 1): chains.append((3, np.minimum(pre[pi][None, :], c3))); chains.append((3, np.minimum(pre[pi][None, :], c3b))) for pi in (2, 3): chains.append((4, np.minimum(pre[pi][None, :], c4))) else: for pi in (0, 1): chains.append((3, pre[pi][None, :].copy())) return dict(chains=chains, j3=[np.minimum(pre[0], r3), np.minimum(pre[1], r3)]) def A_top_types_lo(k, t): def R(c): # bounds for 1 + c/k return _dn(1 + _dn(c/k)), _up(1 + _up(c/k)) r3, r6, r7, r14 = R(3.0), R(6.0), R(7.0), R(14.0) a = np.minimum(_lin_lo(3, 6, r3[0], r3[1], t), np.full_like(t, _dn(3/r6[1]))) b = np.minimum.reduce([_lin_lo(4, 14, r7[0], r7[1], t), np.full_like(t, _dn(4/r14[1])), _dn(k*_lin_lo(0.25, 2, 1, 1, t)), np.full_like(t, k/8.0)]) return np.array([a, b]) def A_band_values_lo(k, t, Dband, static=None): """rigorous lower bounds for the chain values at the levels t (cf. A_band_values): the chains with tents T_d, 3 <= d <= Dband. They are lower bounds for theta_k(t) only if every tent is defined at every level, that is, t <= 1/(Dband(Dband+1)), and if Dband <= k - 1 and max{5, Dband + 1} <= k/2 (so that the values are below k/2); these conditions are enforced here.""" _require(3 <= Dband <= k - 1 and max(5, Dband + 1) <= k/2 and bool(np.all(t <= _A_point(Dband))), "A_band_values_lo: a tent is not defined at some level, or the chain values are not known to be below k/2") if static is None: static = A_band_static_lo(t, Dband) E = A_ends_lo(k, t, 3, Dband) vals = [np.minimum(c, E[Ds-3:]) for Ds, c in static['chains']] e = np.minimum(_lin_lo(5, 30, float(k+15), float(k+15), t), np.full_like(t, _dn(5/float(k+30)))) vals.append(np.vstack([np.minimum(p, e) for p in static['j3']])) return np.vstack([_dn(k*np.vstack(vals)), A_top_types_lo(k, t)]) def A_cell_sum_up(edges, th, cap): """a rigorous upper bound for the integral of dt/(t min{cap, theta_k(t)}) over [edges[0], edges[-1]], where the rows of th are lower bounds for the values of the chains at the edges (cf. A_cell_sum)""" t1, t2 = edges[:-1], edges[1:] a = np.minimum(th[:, :-1], cap); b = np.minimum(th[:, 1:], cap) a = np.maximum(a, b) # still a lower bound at t1, since each chain value is nonincreasing good = b > 1.0 am = np.where(good, a, 0.0); bm = np.where(good, b, 0.0) cols = np.arange(a.shape[1]); best = np.full(a.shape[1], np.inf) for idx in (am.argmax(axis=0), bm.argmax(axis=0), (am+bm).argmax(axis=0)): aa, bb = am[idx, cols], bm[idx, cols]; ok = bb > 1.0 aa = np.where(ok, aa, 2.0); bb = np.where(ok, bb, 2.0) L = _upk(np.log(_up(_up(t2*aa)/_dn(t1*bb))), A_LOG_ULP) A = _dn(_dn(_dn(aa*t2) - _up(bb*t1))/_up(t2 - t1)) c = _up(L/A) best = np.minimum(best, np.where(ok & (A > 0), c, np.inf)) _require(np.all(np.isfinite(best)), "no chain is usable on some cell") n = best.size return float(_up(float(np.sum(best))*(1 + 4*n*_U53))) _A_PD = {} def _A_point(D): """the largest floating point number <= 1/(D(D+1))""" if D not in _A_PD: _A_PD[D] = _flo(_Fr(1, D*(D+1))) return _A_PD[D] def A_tau2_exact(k): return _Fr(2*10**6, 9027901*k) def A_zk_exact(k): h = _Fr(36, 5*(k-1)); return (1-h)*(_Fr(1, 10) - h/50) def A_kinks(k): """the points hat t = (k-9)/(8k) and check t = (k-20)/(6(3k+10)) of Lemma A.18 (rounded), at which the best chain changes in the range (1/12, 2 z_k] and in the band of D = 3, respectively""" return _flo(_Fr(k-9, 8*k)), _flo(_Fr(k-20, 6*(3*k+10))) def A_grid_rig(k, refine=1, rtop=None, rband=None, rtail=None): """the subdivision of a range [t_-, t^+] that contains [2 tau_k, 2 z_k] (cf. A_grid): a list of triples (edges, D, std), where D = 0 for the range above 1/12 and std is False when a point hat t or check t has been inserted (so that the edges depend on k)""" rtop = rtop or refine; rband = rband or refine; rtail = rtail or refine tlo = _flo(A_tau2_exact(k)); thi = _fup(2*A_zk_exact(k)); p3 = _A_point(3); kh, kc = A_kinks(k) def ins(e, p): if e[0] < p < e[-1] and p not in e: return np.sort(np.append(e, p)), False return e, True e = np.geomspace(p3, thi, A_MTOP*rtop+1); e[0] = p3; e[-1] = thi out = [(*ins(e, kh)[:1], 0, False)]; D = 3 while True: hi = _A_point(D); lo = _A_point(D+1) if lo <= tlo or D > A_Dc(k) or D >= k-1: break e = np.geomspace(lo, hi, A_M*rband+1); e[0] = lo; e[-1] = hi if D == 3: e, std = ins(e, kc) else: std = True out.append((e, D, std)); D += 1 e = np.geomspace(tlo, hi, A_MTAIL*rtail+1); e[0] = tlo; e[-1] = hi out.append((e, D, False)) for e, _, _ in out: _require(np.all(np.diff(e) > 0), "the points of a subdivision do not increase") return out def A_psi_bounds(k): """lower and upper bounds for Psi(z_k) = 2 + log((1 - z_k)/z_k)""" z = A_zk_exact(k); r = (1-z)/z return float(_dn(2 + _dnk(np.log(_flo(r)), A_LOG_ULP))), float(_up(2 + _upk(np.log(_fup(r)), A_LOG_ULP))) def A_step_log(k): """lower and upper bounds for log(z_k/tau_k) = log(9.027901 k z_k), the amount by which the exact length of the steps lowers Y_k (Section A.5)""" r = A_zk_exact(k)*_Fr(9027901*k, 10**6) return float(_dnk(np.log(_flo(r)), A_LOG_ULP)), float(_upk(np.log(_fup(r)), A_LOG_ULP)) _A_RCACHE = {} def A_bound_rig(k, refine=1): """(B, Theta'): Theta' <= Theta_k is a lower bound for the largest chain value at 2 tau_k (so 9/4 <= Theta' is checked), and B is a rigorous upper bound for (Y_k(Theta') + 1)/k, so that (A.21) holds; here Y_k(Theta) = k(Psi(z_k) + 2 int_{2 tau_k}^{2 z_k} dt/(t min{Theta, theta_k(t)})) - log(z_k/tau_k)""" grid = A_grid_rig(k, refine) e_t, Dt, _ = grid[-1] tplus = _fup(A_tau2_exact(k)); _require(tplus < e_t[-1]) Th = float(A_band_values_lo(k, np.array([tplus]), Dt).max()); _require(Th >= 2.25, "Theta' < 9/4") # every chain value is at most the right end point of its last interval, hence < max{5, Dt + 1} <= k/2, and the # tents used are T_D with D <= Dt <= k-1 (Section A.7); A_band_values_lo enforces this _require(max(5, Dt + 1) <= k/2 and Dt <= k-1) total = 0.0 for e, D, std in grid: if D == 0: _require(bool(np.all(e <= 0.25)) and k >= 13); th = A_top_types_lo(k, e) elif std and D <= 160: # the k-independent parts are kept for the bands that recur for many k key = (D, refine) if key not in _A_RCACHE: _A_RCACHE[key] = (e.copy(), A_band_static_lo(e, D)) _require(np.array_equal(_A_RCACHE[key][0], e), "the stored subdivision of a band differs from the present one") th = A_band_values_lo(k, e, D, _A_RCACHE[key][1]) else: th = A_band_values_lo(k, e, D) total = float(_up(total + A_cell_sum_up(e, th, Th))) psi_up = A_psi_bounds(k)[1] B = float(_up(_up(psi_up + _up(2*total)) + _fup(_Fr(1, k)))) B = float(_up(B - _dn(A_step_log(k)[0]/k))) return B, Th A_CHI0_LO = float(_dn(1 + _dnk(np.log(_flo(_Fr(9027901, 11*10**6))), A_LOG_ULP))) # chi_0 >= A_CHI0_LO A_HALF_TAUINV_UP = _fup(_Fr(45139505, 10**7)) # 4.5139505 <= this def A_values_rig(k, B, Th): """the integers and bounds of Section A.8 (Proposition A.24(i), Remark A.27) from an upper bound B and Theta'""" kB = float(_up(k*B)); Y = float(_up(kB - 1)) # Theta'' := Th/(1 - Th/(2k)), so that k/Theta'' = k/Th - 1/2 and 2/(11 Theta'') = 2/(11 Th) - 1/(11 k) x = float(_up(Y - _dn(_dn(2*A_CHI0_LO)*_dn(_dn(k/Th) - 0.5)))) s0 = max(k, floor(x/2) + 1) b = float(_up(_up(2/_dn(11*Th)) - _dn(1/_up(11.0*k)))) E1 = float(_up(_up(2*s0 + _up(A_HALF_TAUINV_UP*k))/_dn(1 - b))) return dict(B=B, Th=Th, kB=kB, Y=Y, G=floor(kB), l=floor(Y/2)+1, x=x, s0=s0, b=b, E=E1) A_REFINE_BELOW = 0.01 def A_table_rig(ks=None): """Section A.8: the values for 37 <= k <= KNUM; when the fractional part of k B is smaller than A_REFINE_BELOW, the subdivision is refined eightfold (and the smaller bound is used)""" out = {} for k in (ks if ks is not None else range(37, KNUM+1)): B, Th = A_bound_rig(k); r = A_values_rig(k, B, Th); r['ref'] = 1 if r['kB'] - floor(r['kB']) < A_REFINE_BELOW: B8, Th8 = A_bound_rig(k, refine=8); _require(Th8 == Th) if B8 < B: r = A_values_rig(k, B8, Th); r['ref'] = 8 out[k] = r return out A_KREC = 17 # the recursion of Section A.7 is carried out, in the first case, for A_KREC <= k <= KNUM def A_previous_plus(k): """the bound ceil(k(log k + 4.20032)/2) for G^+(k) of [BW2023, Theorem 9.1] (for k other than 4, 8, 16 and 32)""" v = _Fr(k, 2)*(_Fr(A_log_lo(k)) + _Fr(420032, 10**5)); w = _Fr(k, 2)*(_Fr(float(_upk(np.log(float(k)), A_LOG_ULP))) + _Fr(420032, 10**5)) _require(floor(v) == floor(w)) # so that the ceiling below is determined return floor(w) + 1 def A_previous(k): """the bound (1.1) for G(k), that is, ceil(k(log k + 4.20032)), for 1 <= k <= 36 (the bound for G(k) that was known before, when 21 <= k <= 36)""" if k == 1: return 5 v = _Fr(k)*(_Fr(A_log_lo(k)) + _Fr(420032, 10**5)); w = _Fr(k)*(_Fr(float(_upk(np.log(float(k)), A_LOG_ULP))) + _Fr(420032, 10**5)) _require(floor(v) == floor(w)) # so that the ceiling below is determined return floor(w) + 1 # --- rigorous bounds for Lambda^star and C_0^star: each term of the partial sum is computed with a relative error of a # few units in the last place, math.fsum adds the computed terms with one rounding, and the absolute allowance # 1e-13 exceeds the accumulated error by a factor of more than 100; the tail lies between 2/(N + 3/2) and 2/N def _A_lambda_bounds(): D = np.arange(4, A_NT+1, dtype=np.float64) S = fsum((np.log1p(2*(D+1)/D**2)/(D+1)).tolist()) + fsum([log(2)/3, log(17/9)/4, log(5/3)/4, log(7/6)/5]) lo = float(_dn(_dn(S - 1e-13) + _flo(_Fr(4, 2*A_NT+3)))); hi = float(_up(_up(S + 1e-13) + _fup(_Fr(2, A_NT)))) return lo, hi A_LAMBDA_R = _A_lambda_bounds() _A_LOG9 = (float(_dnk(np.log(9.0), A_LOG_ULP)), float(_upk(np.log(9.0), A_LOG_ULP))) A_C0_R = (float(_dn(_dn(2 + _A_LOG9[0]) + 2*A_LAMBDA_R[0])), float(_up(_up(2 + _A_LOG9[1]) + 2*A_LAMBDA_R[1]))) def A_lt_sqrt(x, c, k): """exact test of x < c sqrt(k) for rational x and c (Fractions or floats, converted exactly)""" x = _Fr(x); c = _Fr(c) if c >= 0: return x < 0 or x*x < c*c*k return x < 0 and x*x > c*c*k def A_log_lo(k): """a lower bound for log k""" return float(_dnk(np.log(float(k)), A_LOG_ULP)) # --- rigorous upper bounds for theta_k(t) (outer intervals), and lower bounds for Y_k(Theta_k) + 1 def A_covered_outer(k, t, th): """False where it is certain that the intervals of Lemma A.14, formed with vt = th/k, do not cover (1, th], so that theta_k(t) < th: the end points are rounded outwards, and vt is rounded down""" t = np.asarray(t, float); th = np.asarray(th, float); vt = _dn(th/k); n = t.shape[0] Dmax = 3 while (Dmax+1)*(Dmax+2)*float(t.min()) <= 1.0000001 and Dmax+1 <= k-1: Dmax += 1 LO = [np.ones(n)]; HI = [_up(2 - 2*t)] for j in (1, 2, 3): c = 2.0**(j+2) - 2 LO.append(np.full(n, j + 2.0**(-j))) HI.append(np.minimum(_up((j+2) - _dn(c*_dn(t + vt/2))), _up((j+2) - _dn(c*vt)))) D = np.arange(3, Dmax+1, dtype=np.float64)[None, :]; tt = t[:, None]; vv = vt[:, None] tD = _dn(D*(D+1)*tt) l = _dn(_dn(0.5*(D+1))*_dn(tD + _dn((D-1)*vv))) r1 = _up(_up((D+1)*_up((D-1) - _dn(tD + _dn((2*D-1)*vv))))/(D-2)) r2 = _up((D+1)*_up(1 - _dn(_dn(tD + _dn((3*D-1)*vv))/(2*D-2)))) r3 = _up((D+1) - _dn(tD + _dn(D*vv))) r = np.minimum.reduce([r1, r2, r3]); ok = tD <= 1 LO = np.hstack([np.array(LO).T, np.where(ok, l, np.inf)]); HI = np.hstack([np.array(HI).T, np.where(ok, r, -np.inf)]) o = np.argsort(LO, axis=1, kind='stable'); LO = np.take_along_axis(LO, o, 1); HI = np.take_along_axis(HI, o, 1) cm = np.maximum.accumulate(HI, axis=1) gap = LO[:, 1:] > cm[:, :-1]; first = np.argmax(gap, axis=1) reach = np.where(gap.any(axis=1), cm[np.arange(n), first], cm[:, -1]) return reach >= th def A_theta_upper(k, t, start, iters=48): """certified upper bounds hi > theta_k(t), starting from lower bounds start <= theta_k(t)""" t = np.asarray(t, float); hi = _up(np.maximum(start, 1.0)*(1 + 1e-12) + 1e-12) cov = A_covered_outer(k, t, hi) if cov.any(): idx = np.where(cov)[0]; lo = hi[idx].copy(); h2 = np.full(idx.size, k/2.0) _require(not A_covered_outer(k, t[idx], h2).any()) for _ in range(iters): mid = 0.5*(lo + h2); c = A_covered_outer(k, t[idx], mid) lo = np.where(c, mid, lo); h2 = np.where(c, h2, mid) hi[idx] = h2 return hi def A_Y_lower(k, rtop=32, rband=16, rtail=16): """a rigorous lower bound for Y_k(Theta_k) + 1 = k(Psi(z_k) + 2 int_{2 tau_k}^{2 z_k} dt/(t theta_k(t))) - log(z_k/tau_k) + 1, by lower Riemann sums: theta_k is nonincreasing, and theta_k(t_i) < hi_i on [t_i, t_{i+1}]""" grid = A_grid_rig(k, rtop=rtop, rband=rband, rtail=rtail) t_lo, t_hi = _fup(A_tau2_exact(k)), _flo(2*A_zk_exact(k)) S = [] for e, D, _ in grid: e = np.clip(e, t_lo, t_hi); e = np.unique(e) if e.size < 2: continue start = (A_top_types_lo(k, e[:-1]) if D == 0 else A_band_values_lo(k, e[:-1], D)).max(axis=0) hi = A_theta_upper(k, e[:-1], start) S.append(_dn(_dnk(np.log(_dn(e[1:]/e[:-1])), A_LOG_ULP)/hi)) S = np.concatenate(S); n = S.size; _require(np.all(S >= 0)) s = float(_dn(float(np.sum(S))*(1 - 4*n*_U53))) V = float(_dn(_dn(A_psi_bounds(k)[0] + _dn(2*s)) + _flo(_Fr(1, k)))) return float(_dn(_dn(k*V) - A_step_log(k)[1])) # --- Remark A.26: Definition A.11 formed with the estimates of Section 4 alone, that is, with (b) for 1 <= j <= k-2 and # with Lemma 4.4 (4 <= D <= k-1) in place of (c). The level sets are I_0, the intervals I_j (j <= k-2) and # T'_D = [(D+1)(D(D+1)t + D vt), (D+1)(1 - vt - (D+1)t)], and theta'_k is computed by bisection, as in A_theta_upper. def A_covered4(k, t, th, inner): """inner=True: True only where it is certain that these intervals, formed with vt = th/k, cover (1, th] (end points rounded inwards, vt rounded up); inner=False: False only where it is certain that they do not""" t = np.asarray(t, float); th = np.asarray(th, float); n = t.shape[0] a, b = (_up, _dn) if inner else (_dn, _up) # a: for left end points, b: for right end points vt = _up(th/k) if inner else _dn(th/k) jmax = min(k-2, 60) LO = [np.ones(n)]; HI = [b(2 - (2*t if inner else 2*t))] for j in range(1, jmax+1): c = 2.0**(j+2) - 2 LO.append(np.full(n, j + 2.0**(-j))) HI.append(np.minimum(b((j+2) - a(c*a(t + vt/2))), b((j+2) - a(c*vt)))) Dmax = 4 while (Dmax+2)**2*float(t.min()) <= 1.0000001 and Dmax+1 <= k-1: Dmax += 1 D = np.arange(4, Dmax+1, dtype=np.float64)[None, :]; tt = t[:, None]; vv = vt[:, None] l = a((D+1)*a(a(D*(D+1)*tt) + a(D*vv))) r = b((D+1)*b(1 - a(vv + a((D+1)*tt)))) LO = np.hstack([np.array(LO).T, l]); HI = np.hstack([np.array(HI).T, r]) o = np.argsort(LO, axis=1, kind='stable'); LO = np.take_along_axis(LO, o, 1); HI = np.take_along_axis(HI, o, 1) cm = np.maximum.accumulate(HI, axis=1) gap = LO[:, 1:] > cm[:, :-1]; first = np.argmax(gap, axis=1) reach = np.where(gap.any(axis=1), cm[np.arange(n), first], cm[:, -1]) return reach >= th def A_theta4(k, t, lower, iters=46, chunk=40000): """rigorous lower (lower=True) or upper bounds for theta'_k(t), by bisection on [1, k/2]""" t = np.asarray(t, float); out = np.empty_like(t) for i0 in range(0, t.size, chunk): tc = t[i0:i0+chunk]; lo = np.ones_like(tc); hi = np.full_like(tc, k/2.0) top = A_covered4(k, tc, hi, inner=lower) for _ in range(iters): mid = 0.5*(lo + hi); c = A_covered4(k, tc, mid, inner=lower) lo = np.where(c, mid, lo); hi = np.where(c, hi, mid) out[i0:i0+chunk] = np.where(top, k/2.0, lo) if lower else np.where(top, k/2.0, hi) return out def A_Y4_bounds(k, n_top, n_low, lower=True, iters=32): """(lower bound for Y'_k(Theta'_k) + 1 or None, upper bound for Y'_k(Theta') + 1, Theta'), by Riemann sums on a geometric subdivision of [2 tau_k, 2 z_k] (n_low cells below 1/12 and n_top above); Theta' <= theta'_k(2 tau_k)""" t0, t1 = _flo(A_tau2_exact(k)), _fup(2*A_zk_exact(k)); p = _A_point(3) e = np.concatenate([np.geomspace(t0, p, n_low+1)[:-1], np.geomspace(p, t1, n_top+1)]); e[0] = t0; e[-1] = t1 _require(np.all(np.diff(e) > 0)) Th = float(A_theta4(k, np.array([_fup(A_tau2_exact(k))]), True, iters=50)[0]); _require(Th >= 2.25) up = _up(_upk(np.log(_up(e[1:]/e[:-1])), A_LOG_ULP)/np.minimum(Th, A_theta4(k, e[1:], True, iters=iters))) n = up.size; ps = A_psi_bounds(k) su = float(_up(float(np.sum(up))*(1 + 4*n*_U53))) slo, shi = A_step_log(k) U = float(_up(_up(k*_up(_up(ps[1] + _up(2*su)) + _fup(_Fr(1, k)))) - slo)) if not lower: return None, U, Th e2 = np.clip(e, _fup(A_tau2_exact(k)), _flo(2*A_zk_exact(k))) lo = _dn(_dnk(np.log(_dn(e2[1:]/e2[:-1])), A_LOG_ULP)/A_theta4(k, e2[:-1], False, iters=iters)) sl = float(_dn(float(np.sum(np.maximum(lo, 0)))*(1 - 4*n*_U53))) L = float(_dn(_dn(k*_dn(_dn(ps[0] + _dn(2*sl)) + _flo(_Fr(1, k)))) - shi)) return L, U, Th # ===================================================================================================================== # The second case of Section A.4: 13 <= k <= 20, with the tables of permissible exponents of Vaughan and Wooley, # Further improvements in Waring's problem, IV: higher powers, Acta Arith. 94 (2000) [VW2000, Sections 15-22]. # Everything that involves the tables is computed in exact rational arithmetic. The tables record, for each s, the # exponent lambda_s and the parameter phi_1 of the iteration that produces it; the two are related by # lambda_s = lambda_(s-1)(1 - phi_1) + 1 + (2s - 2) phi_1 [VW2000, (4.10)]. Both columns are part of the program, # and the relation is verified for every entry (A_vw_tables_check), as a test of the transcription. # ===================================================================================================================== A_VW4 = { # the permissible exponents lambda_s, s = 3, 4, ..., as printed in the tables of [VW2000] for 13 <= k <= 20 13: """3.0139128 4.0980713 5.2480334 6.4550068 7.7136017 9.0257224 10.3855801 11.7953083 13.2521623 14.7557170 16.3039366 17.8953488 19.5276970 21.1988910 22.9062626 24.6473038 26.4190897 28.2190701 30.0445818 31.8929000 33.7616279 35.6483904 37.5510006 39.4675269 41.3961543 43.3352806 45.2834077 47.2392765 49.2018815 51.1701090 53.1431803 55.1203776 57.1010835 59.0849135""", 14: """3.0113494 4.0856057 5.2216967 6.4118310 7.6492691 8.9350975 10.2671180 11.6442024 13.0662900 14.5316795 16.0399382 17.5892784 19.1785686 20.8058286 22.4692736 24.1667258 25.8960050 27.6547883 29.4407200 31.2515093 33.0849211 34.9387022 36.8107871 38.6992193 40.6021475 42.5178927 44.4449274 46.3818646 48.3274553 50.2805449 52.2401670 54.2054937 56.1756866 58.1501035 60.1281620 62.1093556 64.0933213 66.0795485""", 15: """3.0092359 4.0742295 5.1987873 6.3738989 7.5936427 8.8571841 10.1649995 11.5141981 12.9059619 14.3381411 15.8111209 17.3230870 18.8736139 20.4609219 22.0838491 23.7405373 25.4293639 27.1483206 28.8955107 30.6688816 32.4664265 34.2861492 36.1261310 37.9844896 39.8594364 41.7493055 43.6525286 45.5676489 47.4933457 49.4284091 51.3717431 53.3223547 55.2793490 57.2419678 59.2094705 61.1812515 63.1567645 65.1355287 67.1171222 69.1012148 71.0874163""", 16: """3.0074816 4.0638321 5.1787385 6.3412246 7.5457229 8.7902441 10.0769155 11.4019639 12.7671799 14.1702162 15.6119303 17.0906280 18.6062852 20.1573791 21.7431712 23.3621148 25.0129891 26.6941169 28.4039654 30.1407641 31.9028113 33.6883143 35.4955330 37.3227332 39.1682523 41.0304795 42.9078717 44.7989875 46.7024729 48.6170673 50.5416092 52.4750359 54.4163771 56.3647518 58.3193587 60.2794807 62.2444892 64.2137908 66.1868812 68.1633061 70.1426626 72.1245941 74.1088058 76.0949856 78.0829008""", 17: """3.0061404 4.0554335 5.1619828 6.3136071 7.5040992 8.7323022 10.0004288 11.3043577 12.6459140 14.0233197 15.4369512 16.8860867 18.3702735 19.8886761 21.4405373 23.0247863 24.6403737 26.2860071 27.9604002 29.6620874 31.3896134 33.1414245 34.9159903 36.7117441 38.5271601 40.3607211 42.2109803 44.0765377 45.9560566 47.8482757 49.7520144 51.6661665 53.5897087 55.5216990 57.4612719 59.4076368 61.3600741 63.3179278 65.2806096 67.2475940 69.2183942 71.1925852 73.1697840 75.1496484 77.1318848 79.1162028 81.1023673 83.0901643""", 18: """3.0050870 4.0484489 5.1469671 6.2893077 7.4676627 8.6816032 9.9332381 11.2185989 12.5388988 13.8936352 15.2823305 16.7049932 18.1609021 19.6497248 21.1706906 22.7231221 24.3060925 25.9186447 27.5596769 29.2280189 30.9224199 32.6415685 34.3841221 36.1487034 37.9339369 39.7384452 41.5608774 43.3999089 45.2542644 47.1227175 49.0040975 50.8972954 52.8012695 54.7150443 56.6377106 58.5684269 60.5064172 62.4509681 64.4014262 66.3571950 68.3177304 70.2825406 72.2511825 74.2232486 76.1983771 78.1762412 80.1565468 82.1390360 84.1234628 86.1096189 88.0973151 90.0863822""", 19: """3.0042273 4.0423620 5.1333114 6.2663273 7.4343072 8.6357245 9.8725415 11.1414574 12.4430359 13.7775620 15.1440547 16.5429734 17.9734687 19.4355306 20.9284296 22.4517574 24.0047167 25.5866021 27.1964861 28.8334329 30.4963841 32.1842434 33.8958472 35.6300014 37.3854842 39.1610622 40.9555027 42.7675849 44.5961098 46.4399090 48.2978543 50.1688626 52.0518997 53.9459843 55.8501910 57.7636509 59.6855524 61.6151397 63.5517128 65.4946250 67.4432807 69.3971334 71.3556823 73.3184698 75.2850793 77.2551336 79.2282862 81.2042263 83.1826716 85.1633672 87.1460856 89.1306147 91.1167691 93.1043803 95.0932973 97.0833838""", 20: """3.0035377 4.0372112 5.1214726 6.2457501 7.4042791 8.5946271 9.8176862 11.0720258 12.3569582 13.6733507 15.0199527 16.3974554 17.8050250 19.2428091 20.7101660 22.2068593 23.7322237 25.2857385 26.8666319 28.4741610 30.1074344 31.7655438 33.4474901 35.1522491 36.8787481 38.6258968 40.3925867 42.1777082 43.9801557 45.7988398 47.6326920 49.4806744 51.3417836 53.2150565 55.0995728 56.9944581 58.8988854 60.8120763 62.7333008 64.6618772 66.5971707 68.5385926 70.4855980 72.4376841 74.3943883 76.3552854 78.3199857 80.2881335 82.2594024 84.2334960 86.2101439 88.1891004 90.1701423 92.1530669 94.1376919 96.1238487 98.1113875 100.1001722 102.0900795 104.0809983""", } A_VW4_PHI = { # the parameters phi_1 that are printed next to lambda_s in the same tables (8 decimals) 13: """0.00695637 0.02818356 0.03843282 0.04355532 0.04663575 0.04965016 0.05159785 0.05380951 0.05568206 0.05756333 0.05930365 0.06099508 0.06257992 0.06409230 0.06549065 0.06679816 0.06798261 0.06907751 0.07007187 0.07095680 0.07175359 0.07245756 0.07307633 0.07362249 0.07409771 0.07451110 0.07486365 0.07516706 0.07543499 0.07565390 0.07584409 0.07600614 0.07614400 0.07627230""", 14: """0.00567466 0.02484611 0.03476683 0.03979118 0.04248943 0.04500717 0.04699577 0.04876376 0.05051435 0.05209364 0.05367993 0.05515430 0.05660418 0.05796461 0.05926701 0.06048642 0.06162954 0.06268866 0.06366289 0.06455699 0.06537338 0.06610731 0.06676863 0.06736052 0.06788535 0.06835016 0.06876037 0.06912079 0.06943614 0.06970829 0.06994609 0.07015541 0.07033184 0.07048574 0.07061847 0.07073278 0.07083658 0.07091753""", 15: """0.00461792 0.02173146 0.03172823 0.03647237 0.03905794 0.04113748 0.04309441 0.04456907 0.04616698 0.04752336 0.04895330 0.05024754 0.05156238 0.05278515 0.05398415 0.05510909 0.05618735 0.05719334 0.05813950 0.05901573 0.05982581 0.06056957 0.06125062 0.06186873 0.06242704 0.06293025 0.06338099 0.06378269 0.06414041 0.06445755 0.06473789 0.06498462 0.06520081 0.06539241 0.06555771 0.06570293 0.06582965 0.06594009 0.06603623 0.06612247 0.06619342""", 16: """0.00374079 0.01883046 0.02919246 0.03370200 0.03613827 0.03788514 0.03976160 0.04102548 0.04247669 0.04365257 0.04493631 0.04608149 0.04726736 0.04836825 0.04946474 0.05049786 0.05150184 0.05244686 0.05334847 0.05419218 0.05498480 0.05572055 0.05640278 0.05703073 0.05760740 0.05813390 0.05861191 0.05904508 0.05943588 0.05978708 0.06010180 0.06038318 0.06063404 0.06085714 0.06105480 0.06122977 0.06138528 0.06152144 0.06164181 0.06174778 0.06184097 0.06192285 0.06199601 0.06205826 0.06211345""", 17: """0.00307019 0.01646473 0.02701166 0.03134018 0.03349964 0.03513032 0.03689292 0.03799316 0.03927903 0.04034662 0.04145984 0.04251950 0.04356583 0.04457565 0.04556572 0.04651864 0.04744334 0.04832721 0.04917555 0.04997915 0.05074142 0.05145731 0.05212921 0.05275480 0.05333611 0.05387253 0.05436691 0.05482022 0.05523415 0.05561097 0.05595308 0.05626250 0.05654168 0.05679304 0.05701880 0.05722114 0.05740216 0.05756359 0.05770757 0.05783611 0.05794990 0.05805112 0.05814094 0.05822056 0.05829175 0.05835376 0.05840894 0.05845772""", 18: """0.00254346 0.01447855 0.02493153 0.02933022 0.03123178 0.03275099 0.03438389 0.03537489 0.03647482 0.03749420 0.03846045 0.03943607 0.04036376 0.04128885 0.04218253 0.04306012 0.04390870 0.04473174 0.04552348 0.04628304 0.04700798 0.04769656 0.04834828 0.04896179 0.04953749 0.05007502 0.05057526 0.05103871 0.05146692 0.05186115 0.05222287 0.05255372 0.05285563 0.05313037 0.05337974 0.05360563 0.05380985 0.05399414 0.05416015 0.05430945 0.05444347 0.05456372 0.05467161 0.05476794 0.05485415 0.05493118 0.05499994 0.05506161 0.05511611 0.05516484 0.05520823 0.05524686""", 19: """0.00211363 0.01272951 0.02298073 0.02733191 0.02929710 0.03067723 0.03215755 0.03308734 0.03404381 0.03500339 0.03585180 0.03674656 0.03757479 0.03842022 0.03922960 0.04003558 0.04081410 0.04157726 0.04231369 0.04302673 0.04371136 0.04436768 0.04499335 0.04558788 0.04615045 0.04668075 0.04717878 0.04764484 0.04807945 0.04848337 0.04885769 0.04920355 0.04952220 0.04981500 0.05008342 0.05032891 0.05055296 0.05075705 0.05094263 0.05111110 0.05126380 0.05140202 0.05152695 0.05163973 0.05174146 0.05183320 0.05191569 0.05198993 0.05205667 0.05211663 0.05217061 0.05221882 0.05226214 0.05230098 0.05233578 0.05236696""", 20: """0.00176883 0.01123775 0.02126317 0.02547440 0.02754990 0.02885933 0.03012126 0.03108406 0.03191458 0.03281045 0.03356385 0.03438079 0.03512761 0.03589874 0.03663479 0.03737393 0.03808882 0.03879475 0.03947826 0.04014501 0.04078835 0.04140990 0.04200611 0.04257718 0.04312142 0.04363868 0.04412832 0.04459040 0.04502494 0.04543236 0.04581314 0.04616803 0.04649788 0.04680367 0.04708646 0.04734736 0.04758756 0.04780826 0.04801065 0.04819593 0.04836528 0.04851983 0.04866069 0.04878890 0.04890547 0.04901133 0.04910737 0.04919448 0.04927331 0.04934469 0.04940924 0.04946760 0.04952031 0.04956792 0.04961094 0.04964967 0.04968462 0.04971613 0.04974452 0.04977011""", } A_VW_T = {13: '104.95', 14: '114.19', 15: '123.40', 16: '132.60', 17: '141.78', 18: '150.95', 19: '160.07', 20: '169.18'} # the numbers T_k of Section A.4; tau_k^VW = 1/T_k A_BW_GPLUS = {14: 45, 15: 49, 16: 53, 17: 57, 18: 61, 19: 65, 20: 69, 32: 123} # [BW2023, Theorem 9.1 and Table 3]; for k = 16 # and 32 the numbers of [BW2023, Theorem 9.2] (residue classes modulo 4k) def A_gplus_before(k): """the bound for G^+(k) recorded in [BW2023, Section 9], 13 <= k <= 36: Table 3 of Theorem 9.1 for 14 <= k <= 20, the number of Theorem 9.2 for k = 16 and k = 32 (an admissible number of k-th powers in the residue classes modulo 4k), and ceil(k(log k + 4.20032)/2) otherwise""" return A_BW_GPLUS[k] if k in A_BW_GPLUS else A_previous_plus(k) A_VW_PREVIOUS = {13: 81, 14: 89, 15: 97, 16: 105, 17: 113, 18: 121, 19: 129, 20: 137} # [Woo2016, Theorem 1.1] for k = 13, [BW2023, Theorem 1.3] for k >= 14 A_VW_FOOTER = {13: (9545, 12), 14: (8770, 13), 15: (8114, 14), 16: (7549, 15), 17: (7060, 16), 18: (6630, 17), 19: (6252, 18), 20: (5915, 19)} # the numbers 10^6 tau(k), and the values of s to which they belong, that are printed below the tables of [VW2000] def A_vw_delta(k): """the admissible exponents Delta_s = lambda_s - 2s + k (0 <= s <= S_k) as exact fractions; lambda_0 = 0, lambda_1 = 1 and lambda_2 = 2 (the trivial bounds and Hua's lemma), and lambda_s for s >= 3 from the table for k""" lam = [_Fr(0), _Fr(1), _Fr(2)] + [_Fr(x) for x in A_VW4[k].split()] return [lam[s] - 2*s + k for s in range(len(lam))] def A_vw_zeta(Dl, s): """z^(s) = (Delta_s - Delta_{s+1})/2: for z^(s) <= z <= z^(s-1) the minimum defining Psi^VW(z) is attained at s""" return (Dl[s] - Dl[s+1])/2 def A_vw_psi(k, Dl, z): """Psi^VW(z) = min over s of (2s + Delta_s/z)/k, exactly""" return min((2*s + Dl[s]/z)/k for s in range(len(Dl))) def A_vw_tables_check(k): """tests of the transcription of the table for k: (the largest value of |lambda_s - lambda_(s-1)(1 - phi_1) - 1 - (2s - 2) phi_1| over 3 <= s <= S_k, as an exact fraction; True if every entry has the printed number of decimals; floor(10^6 max_s (k - 2 Delta_s)/(4s^2 - 1)) and the s at which this maximum is attained, to be compared with the footer of the table; the sum of the lambda_s)""" ls = A_VW4[k].split(); ps = A_VW4_PHI[k].split() ok = len(ls) == len(ps) and all(len(x.split('.')[1]) == 7 for x in ls) and all(len(x.split('.')[1]) == 8 for x in ps) lam = [_Fr(0), _Fr(1), _Fr(2)] + [_Fr(x) for x in ls] dev = max(abs(lam[s] - lam[s-1]*(1 - _Fr(p)) - 1 - (2*s - 2)*_Fr(p)) for s, p in zip(range(3, len(lam)), ps)) Dl = A_vw_delta(k); S = len(Dl) - 1 v, q = max(((k - 2*Dl[q])/(4*q*q - 1), q) for q in range(1, S+1)) return dev, ok, (floor(v*10**6), q), sum(lam[3:]) # --- Appendix A, Sections A.6 and A.7: the potential defined by a recursion (Theorem A.21), with directed rounding. # By Lemma A.14(iv), (v), for a chain of Section A.7 the function v -> v/(1 - theta~(2v)/k) is increasing as long # as the tents of the chain are defined; so on such a range v/(1 - theta_k(2v)/k) >= c/(1 - theta~(2c)/k) for # v >= c. Starting from c_0 < tau_k, the program puts c_j := a floating point number below the least of these # bounds, taken over the pieces into which the points p_D = 1/(D(D+1)) divide the range, and evaluates # k Psi(c_n) + 2n. def A_chainmax_lo(kv, tv, D): """rigorous lower bounds for theta_k(t) at the levels tv, for the exponents kv (arrays of the same length; kv has integer values): the largest value of a chain of Section A.7 whose tents T_d have d <= D (these are defined for 0 < t <= 1/(D(D+1)); D >= 3), or of a chain without tents (D = 0; these are defined for t <= 1/4), and the trivial bound theta_k >= 1. The conditions under which the values of the chains are lower bounds for theta_k are enforced: the tents are defined at every level, D <= k - 1, and max{5, D + 1} <= k/2, so that the values are below k/2.""" if D == 0: _require(bool(np.all(tv <= 0.25)) and bool(np.all(kv >= 13)), "A_chainmax_lo: a level exceeds 1/4") else: _require(D >= 3 and bool(np.all(tv <= _A_point(D))) and bool(np.all(D <= kv - 1)) and bool(np.all(max(5, D + 1) <= kv/2)), "A_chainmax_lo: a tent is not defined at some level, or the chain values are not known to be below k/2") top = A_top_types_lo(kv, tv) if D == 0: return np.maximum(top.max(axis=0), 1.0) static = A_band_static_lo(tv, D) E = A_ends_lo(kv, tv, 3, D) vals = [np.minimum(c, E[Ds-3:]) for Ds, c in static['chains']] e = np.minimum(_lin_lo(5, 30, kv + 15.0, kv + 15.0, tv), _dn(5/(kv + 30.0))) vals.append(np.vstack([np.minimum(p, e) for p in static['j3']])) allv = np.vstack([_dn(kv*np.vstack(vals)), top]) return np.maximum(allv.max(axis=0), 1.0) _A_PASC = {} def A_band_index(t, Dmax): """for an array t: the largest D in [3, Dmax] with t <= _A_point(D) (the floating point number just below 1/(D(D+1))), and 0 if t > _A_point(3)""" if Dmax not in _A_PASC: _A_PASC[Dmax] = np.array([_A_point(D) for D in range(Dmax, 2, -1)]) # ascending p = _A_PASC[Dmax] cnt = p.size - np.searchsorted(p, t, side='left') # the number of D in [3, Dmax] with t <= _A_point(D) return np.where(cnt == 0, 0, cnt + 2) def A_rec_step_lo(kv, c, D): """one step of the recursion, for elements whose chains have tents T_d with d <= D (D = 0: no tents), where 2c <= _A_point(D) if D >= 3: floating point numbers c' > c with v/(1 - theta_k(2v)/k) > c' for all v >= c, together with the lower bounds for theta_k(2c) that were used. (For v >= c' this is trivial. For c <= v < c' the range of 2v is divided at the points p = _A_point(d), d = D, D-1, ..., and on the piece that begins at p the chains with tents T_e, e <= d - 1, are used, with their values at p.)""" def nxt(cc, th, kk): return _dn(_dn(cc/_up(1 - _dn(th/kk)))) _require(D == 0 or bool(np.all(2*c <= _A_point(D))), "A_rec_step_lo: the starting point lies beyond the range of the chains") th = A_chainmax_lo(kv, 2*c, D) cand = nxt(c, th, kv) idx = np.arange(c.size); Dcur = D while Dcur >= 3: lim = _A_point(Dcur) idx = idx[2*cand[idx] > lim] if idx.size == 0: break Dn = Dcur - 1 if Dcur > 3 else 0 p = np.full(idx.size, lim) cand[idx] = np.minimum(cand[idx], nxt(p/2, A_chainmax_lo(kv[idx], p, Dn), kv[idx])) Dcur = Dn _require(bool(np.all(cand > c)) and bool(np.all(cand < 0.125)), "A_rec_step_lo: a step does not advance, or is too long") return cand, th A_REC_CMAX = float(_dn(_flo(_Fr(1, 10)))) # the points c_n are kept below 1/10 def A_psi_up(c): """an upper bound for Psi(c) = 2 + log((1-c)/c)""" return _up(2 + _upk(np.log(_up(_up(1 - c)/c)), A_LOG_ULP)) def A_rec_std(ks): """Theorem A.21 in the first case, that is, with (Psi, tau_k), for the exponents in ks (15 <= k): arrays Y, n, c, with Y >= k Psi(c_n) + 2n for a sequence c_0 < ... < c_n that satisfies (R1)-(R3); further cp = c_{n-1}, Th (the lower bound for theta_k(2 c_0) that was used) and steps (the number of points below 1/10). (R1): c_0 < tau_k and c_n < 1/10, and theta_k(2 c_n) >= 3/2 by Lemma A.13(iii), as k >= 15; (R2): by A_rec_step_lo; (R3): Psi(c_{n-1}) - Psi(c_n) >= 2/k is verified with the logarithm rounded downwards. Among the n with (R3), the one with the smallest bound Y is taken. (The two uses of the logarithm are verified a second time by A_rec_certify.)""" ks = np.array(sorted(int(k) for k in ks)); _require(ks[0] >= 15, "A_rec_std requires k >= 15") kv = ks.astype(float); N = ks.size c = np.array([float(_dn(_flo(A_tau2_exact(int(k))/2))) for k in ks]) _require(all(_Fr(float(x))*2 < A_tau2_exact(int(k)) for x, k in zip(c, ks)), "c_0 >= tau_k") Dlim = np.array([min(A_Dc(int(k)) + 1, int(k) - 1) for k in ks]) _require(all(max(5, D + 1) <= k/2 and D <= k - 1 for k, D in zip(ks, Dlim))) Dmax = int(Dlim.max()) Deff = np.minimum(A_band_index(2*c, Dmax), Dlim) n = np.zeros(N, int); done = np.zeros(N, bool) best = np.full(N, np.inf); nbest = np.zeros(N, int); cbest = np.zeros(N); cprev = np.zeros(N); th0 = np.zeros(N) two_k = _up(2.0/kv) for D in list(range(Dmax, 2, -1)) + [0]: act = np.nonzero((~done) & (Deff == D))[0] while act.size: cand, th = A_rec_step_lo(kv[act], c[act], D) first = n[act] == 0 th0[act[first]] = th[first] stop = cand > A_REC_CMAX done[act[stop]] = True go = act[~stop]; cn = cand[~stop]; co = c[go] if go.size: num = _dn(_dn(1 - co)*cn); den = _up(_up(1 - cn)*co) worth = _dnk(np.log(_dn(num/den)), A_LOG_ULP) >= two_k[go] n[go] += 1; c[go] = cn Yup = _up(_up(kv[go]*A_psi_up(cn)) + 2.0*n[go]) better = worth & (Yup < best[go]); bi = go[better] best[bi] = Yup[better]; nbest[bi] = n[bi]; cbest[bi] = cn[better]; cprev[bi] = co[better] Deff[go] = np.minimum(A_band_index(2*cn, Dmax), Dlim[go]) act = go[Deff[go] == D] _require(done.all() and np.all(np.isfinite(best)) and np.all(nbest >= 1) and np.all(cbest <= A_REC_CMAX) and np.all(cprev < cbest)) return dict(k=ks, Y=best, n=nbest, c=cbest, cp=cprev, Th=th0, steps=n) _A_REC = {} def A_rec_table(): """the numbers of Section A.7 for A_KREC <= k <= KNUM (first case): k -> dict(Y, G, l, n, c, cp, Th) with Y = Y^flat(k) (an upper bound for k Psi(c_n) + 2n), G = G^flat(k) = floor(Y) + 1, l = l^flat(k) = floor(Y/2) + 1, c = c_n and cp = c_(n-1)""" if 'tab' not in _A_REC: r = A_rec_std(range(A_KREC, KNUM+1)) _A_REC['tab'] = {int(k): dict(Y=float(y), G=floor(y) + 1, l=floor(_Fr(float(y))/2) + 1, n=int(n), c=float(c), cp=float(cp), Th=float(t)) for k, y, n, c, cp, t in zip(r['k'], r['Y'], r['n'], r['c'], r['cp'], r['Th'])} return _A_REC['tab'] def A_rec_certify(rec, prec=40): """a second verification, in decimal arithmetic with prec digits, of the two places at which the recursion uses logarithms in the first case: (R3), that is, log((1 - c_(n-1)) c_n/((1 - c_n) c_(n-1))) >= 2/k, and Y >= k(2 + log((1 - c_n)/c_n)) + 2n. The floating point numbers c_(n-1), c_n and Y are used exactly (as fractions); the arguments of the logarithms are rounded in the safe direction to prec digits; the method ln of the module decimal is correctly rounded, and an error of 10^(3-prec) is allowed for it; the remaining operations are rounded in the safe direction. Returns (True if both inequalities hold for every k, the least value of the left hand side of (R3) minus 2/k, the least and the largest value of Y minus the right hand side).""" from decimal import Decimal as Dm, localcontext, ROUND_FLOOR, ROUND_CEILING ok = True; m3 = mY = xY = None with localcontext() as ctx: ctx.prec = prec; eps = Dm(10)**(3 - prec) for k, r in rec.items(): c = _Fr(r['c']); cp = _Fr(r['cp']); a3 = (1 - cp)*c/((1 - c)*cp); aY = (1 - c)/c ctx.rounding = ROUND_FLOOR l3 = (Dm(a3.numerator)/Dm(a3.denominator)).ln() - eps # a lower bound for the logarithm in (R3) ctx.rounding = ROUND_CEILING lY = (Dm(aY.numerator)/Dm(aY.denominator)).ln() + eps # an upper bound for log((1 - c_n)/c_n) d3 = l3 - Dm(2)/Dm(k) # rounded upwards: the comparison below is exact rhs = Dm(k)*(2 + lY) + 2*r['n'] Yf = _Fr(r['Y']); ctx.rounding = ROUND_FLOOR dY = Dm(Yf.numerator)/Dm(Yf.denominator) - rhs ok &= (l3 >= Dm(2)/Dm(k)) and (dY >= 0) and 0 < cp < c < _Fr(1, 10) m3 = d3 if m3 is None or d3 < m3 else m3; mY = dY if mY is None or dY < mY else mY; xY = dY if xY is None or dY > xY else xY return ok, float(m3), float(mY), float(xY) def A_rec_vw(k, T=None): """Theorem A.21 in the second case, that is, with (Psi^VW, tau^VW), for 13 <= k <= 20: a dictionary with the exact rational number Y = k Psi^VW(c_n) + 2n, the integer G = floor(Y) + 1, n, c_n, a lower bound th for theta_k(2 c_n), the list 'hist' of the triples (j, c_j, k Psi^VW(c_j) + 2j, (R3) holds for n = j), and the truth values of the hypotheses: (V1), (V2), (V3) of Section A.4, (R1): c_0 < 1/T_k, c_n < 1/10 and theta_k(2 c_n) >= 3/2, (R3), Y >= 4k + 1, and Y >= 4.19 k (for part (ii) of Theorem A.21, through Theorem 2.4); and l = floor(Y/2) + 1. The points c_j are floating point numbers, and Psi^VW is evaluated at them exactly.""" Dl = A_vw_delta(k); S = len(Dl) - 1; u = k - 1 tau = 1/_Fr(A_VW_T[k] if T is None else T) c = float(_dn(_flo(tau))); c0 = c Dlim = min(A_Dc(k) + 1, k - 1); _require(max(5, Dlim + 1) <= k/2 and Dlim <= k - 1) kv = np.array([float(k)]); n = 0; best = None; hist = [] fam = lambda x: int(min(A_band_index(np.array([2*x]), Dlim)[0], Dlim)) psi_old = A_vw_psi(k, Dl, _Fr(c)) while True: cand, th = A_rec_step_lo(kv, np.array([c]), fam(c)) cn = float(cand[0]) if cn > A_REC_CMAX: break n += 1; psi_new = A_vw_psi(k, Dl, _Fr(cn)) worth = psi_old - psi_new >= _Fr(2, k); Y = k*psi_new + 2*n thn = float(A_chainmax_lo(kv, np.array([2*cn]), fam(cn))[0]) hist.append((n, cn, Y, worth)) if worth and thn >= 1.5 and (best is None or Y < best[0]): best = (Y, n, cn, thn) c = cn; psi_old = psi_new Y, nb, cb, thb = best d2 = [Dl[s+1] - 2*Dl[s] + Dl[s-1] for s in range(1, S)] tex = (k - 2*Dl[u])/(4*u*u) checks = { 'V1': all(x >= 0 for x in d2) and all(Dl[s+1] < Dl[s] for s in range(S)) and Dl[S] > 0, 'V2': A_vw_zeta(Dl, k-1) >= _Fr(1, 5), 'V3': tau < tex and 8*u*u >= 9*k and _Fr(1, 8)*(1 + Dl[u]/(2*u*u)) + _Fr(k, 4*u*u) < _Fr(1, 4), 'R1': _Fr(c0) < tau and _Fr(cb) < _Fr(1, 10) and _Fr(thb) >= _Fr(3, 2), 'R3': hist[nb-1][3] and hist[nb-1][0] == nb, 'Y': Y >= 4*k + 1, 'Y419': Y >= _Fr(419, 100)*k, } return dict(k=k, Y=Y, G=floor(Y) + 1, l=floor(Y/2) + 1, n=nb, c=cb, th=thb, hist=hist, tau=tau, checks=checks, ok=all(checks.values())) def A_rec_plain(k, vw=False): """a plain double precision version of the recursion, in which theta_k is computed directly from Definition A.11 (A_theta_direct) instead of chains: the least value of k Psi(c_n) + 2n, where c_0 = tau and c_j = inf{ v/(1 - theta_k(2v)/k) : v >= c_{j-1} } (the infimum is examined at the points 1/(2D(D+1))), over the n with c_n < 1/10 and Psi(c_{n-1}) - Psi(c_n) >= 2/k. A cross-check, not rigorous.""" if vw: Dl = [float(x) for x in A_vw_delta(k)]; tau = 1/float(A_VW_T[k]) psi = lambda z: min((2*s + Dl[s]/z)/k for s in range(len(Dl))) else: tau = 1/(TAU_INV*k); psi = lambda z: 2 + log((1-z)/z) th = lambda t: float(A_theta_direct(k, np.array([t]))[0][0]) c = tau; n = 0; best = None while True: cn = c/(1 - th(2*c)/k); D = 3 while 1.0/(D*(D+1)) > 2*c: e = 1.0/(D*(D+1)) if e < 2*cn: cn = min(cn, (e/2)/(1 - th(e*(1 + 1e-13))/k)) D += 1 if cn >= 0.1: break n += 1 if psi(c) - psi(cn) >= 2.0/k: Y = k*psi(cn) + 2*n if best is None or Y < best[0]: best = (Y, n, cn) c = cn return best def A_rec_potential(k, npts=40000): """the potential of the proof of Theorem A.21 in the limit delta_0 = eps_2 = 0, on a grid (plain double precision, theta_k from Definition A.11; a cross-check, not rigorous): G = Psi on [z_0, 1/5], where Psi(z_0) = Psi(c_n) + 2/k, and G(z) = max{Psi(z_0), 2/k + G(S_(z))} for z < z_0, where S_(z) is the infimum of v/(1 - theta_k(2v)/k) over v >= z, and G is interpolated linearly in log z. Returns (the largest increase of G between neighbouring grid points, the largest value of g - (G - 2) with g = -dG/dlog z, the least value of G(z) - G(z/(1 - theta_k(2z)/k)) - 2/k for z < z_0, and k G(c_0) - Y^flat(k)).""" tau = tau_k(k); z = np.exp(np.linspace(log(tau/3), log(0.2), npts)); lz = np.log(z) S = z/(1 - A_theta_direct(k, 2*z)[0]/k); Slow = np.minimum.accumulate(S[::-1])[::-1] r = A_rec_std([k]); cn = float(r['c'][0]); psi = lambda v: 2 + np.log((1 - v)/v) z0 = 1/(1 + float(np.exp(2.0/k))*(1 - cn)/cn); p0 = float(psi(z0)) G = np.empty(npts); top = z >= z0; G[top] = psi(z[top]); i0 = int(np.argmax(top)) for i in range(i0 - 1, -1, -1): G[i] = max(p0, 2.0/k + float(np.interp(log(Slow[i]), lz[i+1:i+4000], G[i+1:i+4000]))) slope = -np.diff(G)/np.diff(lz); below = z < z0 c0 = float(_dn(_flo(A_tau2_exact(k)/2))) return (float(np.max(np.diff(G))), float(np.max(slope - (0.5*(G[1:] + G[:-1]) - 2))), float(np.min(G[below] - np.interp(np.log(S[below]), lz, G)) - 2.0/k), k*float(np.interp(log(c0), lz, G)) - float(r['Y'][0])) def A_const_certify(prec=30): """enclosures for Lambda^star and C_0^star of (A.1) in decimal arithmetic with prec digits, independent of the logarithm of the floating point library: the method ln of the module decimal is correctly rounded, an error of 10^(3-prec) is allowed for each of its values, and all other operations are rounded in the safe direction. The series is summed up to D = A_NT, and the remainder lies between 2/(A_NT + 3/2) and 2/A_NT, because 2/((D + 1/2)(D + 3/2)) <= 2/(D^2 + D + 1) <= Lambda_D <= 2/D^2. Returns (lo, hi) for Lambda^star and (lo, hi) for C_0^star, as fractions.""" from decimal import Decimal as Dm, localcontext, ROUND_FLOOR, ROUND_CEILING res = [] with localcontext() as ctx: ctx.prec = prec; eps = Dm(10)**(3 - prec) for mode, sg in ((ROUND_FLOOR, -1), (ROUND_CEILING, 1)): ctx.rounding = mode ln = lambda a, b: (Dm(a)/Dm(b)).ln() + sg*eps # a bound for log(a/b), in the direction of sg s = ln(2, 1)/3 + ln(17, 9)/4 + ln(5, 3)/4 + ln(7, 6)/5 for D in range(4, A_NT + 1): s += ln(D*D + 2*D + 2, D*D)/(D + 1) s += Dm(4)/Dm(2*A_NT + 3) if sg < 0 else Dm(2)/Dm(A_NT) c = 2 + ln(9, 1) + 2*s res.append((_Fr(str(s)), _Fr(str(c)))) return (res[0][0], res[1][0]), (res[0][1], res[1][1]) def A_previous_plus_certify(prec=30): """the numbers ceil(k(log k + 4.20032)/2), 13 <= k <= 36, of [BW2023, Theorem 9.1], in decimal arithmetic with prec digits: True if they agree with A_previous_plus(k) and the integer parts of the two bounds agree""" from decimal import Decimal as Dm, localcontext, ROUND_FLOOR, ROUND_CEILING ok = True with localcontext() as ctx: ctx.prec = prec; eps = Dm(10)**(3 - prec) for k in range(13, 37): ctx.rounding = ROUND_FLOOR; lo = Dm(k)*(Dm(k).ln() - eps + Dm('4.20032'))/2 ctx.rounding = ROUND_CEILING; hi = Dm(k)*(Dm(k).ln() + eps + Dm('4.20032'))/2 ok &= floor(lo) == floor(hi) and floor(lo) + 1 == A_previous_plus(k) and lo != floor(lo) return ok def A_previous_certify(prec=30): """the numbers ceil(k(log k + 4.20032)), 1 <= k <= 36, of (1.1), in decimal arithmetic with prec digits (as in A_const_certify): True if for every k the lower and the upper bound for k(log k + 4.20032) have the same integer part, and this integer part plus 1 is A_previous(k)""" from decimal import Decimal as Dm, localcontext, ROUND_FLOOR, ROUND_CEILING ok = True with localcontext() as ctx: ctx.prec = prec; eps = Dm(10)**(3 - prec) for k in range(1, 37): ctx.rounding = ROUND_FLOOR; lo = Dm(k)*(Dm(k).ln() - eps + Dm('4.20032')) ctx.rounding = ROUND_CEILING; hi = Dm(k)*(Dm(k).ln() + eps + Dm('4.20032')) ok &= floor(lo) == floor(hi) and floor(lo) + 1 == A_previous(k) and lo != floor(lo) return ok A_SQ = 4 - 2*log(TAU_INV/2) # c_star = 0.98565..., the coefficient of sqrt(k) in Proposition A.19 def A_table(): """the numbers of Section A.8 (Proposition A.24(i)) for 37 <= k <= KNUM (directed rounding): k -> dict(B, Th, kB, Y, G, l, x, s0, b, E, ref), where ref = 8 when the subdivision has been refined eightfold""" return A_table_rig() def A_exactlog_gain(k, sub=8, nodes=12): """the amount by which Y_k(Theta_k) would decrease if the integrand 2/theta - 1/k were replaced by 2/(-k log(1 - theta/k)), with theta = theta_k(2v) (the remark after the proof of Theorem A.17): the integral of k(2/theta - 1/k + 2/(k log(1 - theta/k))) dv/v over [tau_k, z_k], by Gauss-Legendre quadrature in log v, with theta_k computed directly from Definition A.11 (not rigorous)""" a0, b0 = 2*tau_k(k), 2*A_zk(k) bps = {a0, b0, 1/8, 1/18, 1/20} D = 3 while 1/(D*(D+1)) > a0 and D < k: bps.add(1/(D*(D+1))); D += 1 bps = sorted(x for x in bps if a0 <= x <= b0) x, w = np.polynomial.legendre.leggauss(nodes); S, W = [], [] for a, b in zip(bps[:-1], bps[1:]): e = np.geomspace(a, b, sub+1) for c, d in zip(e[:-1], e[1:]): sa, sb = log(c), log(d) S.append(0.5*(sb-sa)*x + 0.5*(sb+sa)); W.append(0.5*(sb-sa)*w) S = np.concatenate(S); W = np.concatenate(W) th = A_theta_direct(k, np.exp(S))[0] return float(np.dot(W, k*(2/th - 1/k) + 2/np.log1p(-th/k))) A_REC_SELECT = (37, 38, 39, 40, 41, 50, 56, 67, 78, 81, 100, 150, 174, 200, 212, 250, 330, 500, 700, 927, 1000, 2000, 3000, 5000) def A_before(k): """the bound for G(k) that was known before, as it is shown in Table 6: (1.1) for k <= 36, and the number floor(k B(k, Theta^sharp(k))) of Table 3 for k >= 37""" return A_previous(k) if k <= 36 else floor(k*bound(k, Theta_sharp(k))) def print_list_appendix(): """the numbers of Sections A.7 and A.8 of Appendix A, and the tables of [VW2000] as they are used""" rec = A_rec_table() print("# Section A.7, first case (Proposition A.22): k n c_n Y^flat(k) G^flat(k) l^flat(k) the bound for G(k) known before ((1.1) for k <= 36, Table 3 for k >= 37)") for k in range(A_KREC, KNUM+1): r = rec[k] print(f"{k:5d} {r['n']:5d} {r['c']:.6f} {r['Y']:13.6f} {r['G']:6d} {r['l']:6d} {A_before(k):6d}") print("# Section A.7, second case (Proposition A.23): k T_k n c_n Y^VW(k) G^VW(k) the bound for G(k) known before" " l^VW(k) the bound for G^+(k) of [BW2023, Theorems 9.1, 9.2]") for k in sorted(A_VW_T): r = A_rec_vw(k) print(f"{k:5d} {A_VW_T[k]} {r['n']:3d} {r['c']:.6f} {float(r['Y']):11.6f} {r['G']:6d} {A_VW_PREVIOUS[k]:6d}" f" {r['l']:6d} {A_gplus_before(k):6d}") tab = A_table() print("# Section A.8 (Proposition A.24): k Theta^nat(k) B^nat(k) k B^nat(k) 2 s_0^nat(k) b^nat(k) E^nat(k) E^sharp(k) refinement of the subdivision") for k in range(37, KNUM+1): r = tab[k] print(f"{k:5d} {r['Th']:12.6f} {r['B']:.6f} {r['kB']:13.6f} {2*r['s0']:6d} {r['b']:.6f} {r['E']:11.3f} {E_sharp(k):11.3f} {r['ref']}") print("# the tables of [VW2000] for 13 <= k <= 20 as they are used: k s lambda_s phi_1") for k in sorted(A_VW4): for s, (l, p) in enumerate(zip(A_VW4[k].split(), A_VW4_PHI[k].split()), start=3): print(f"{k:5d} {s:3d} {l:>11s} {p}") def part15(rng): print("15. Appendix A") Fr = Fraction # ---- (a) the constants (directed rounding), the library logarithm, and the limit of the saving function (Lemma A.12) lam0, lam1 = A_LAMBDA_R; c00, c01 = A_C0_R check(1.0424535 < lam0 < lam1 < 1.0424536 and lam1 - lam0 < 1e-11 and lam0 <= A_LAMBDA_LO and A_LAMBDA <= lam1 + 1e-15, f"Lambda^star lies in [{lam0:.12f}, {lam1:.12f}] (series up to D = 1e6, tail between 2/(1e6+1.5) and 2/1e6)") check(6.2821316 < c00 < c01 < 6.2821317 and Fraction(c01) < Fraction(62822, 10000) and 0.1073 < C0 - c01 and C0 - c00 < 0.1074, f"C_0^star = 2 + log 9 + 2 Lambda^star lies in [{c00:.12f}, {c01:.12f}]; C_0 - C_0^star = {C0-A_C0:.6f}") check(Fraction(c01) + Fraction(45139505, 10**7) < Fraction(107961, 10000) and c00 + TAU_INV/2 > 10.79608, f"C_0^star + 9.027901/2 = {A_C0+TAU_INV/2:.7f} < 10.7961 (exact comparison of the upper bound)") (dl0, dl1), (dc0, dc1) = A_const_certify() check(Fr(lam0) <= dl0 <= dl1 <= Fr(lam1) and Fr(c00) <= dc0 <= dc1 <= Fr(c01) and dc1 - dc0 < Fr(1, 10**11) and A_previous_certify() and A_previous_plus_certify(), "the same intervals contain the enclosures for Lambda^star and C_0^star that are obtained in decimal arithmetic with 30 digits and a correctly " f"rounded logarithm (C_0^star lies in [{float(dc0):.13f}, {float(dc1):.13f}]); and the numbers ceil(k(log k + 4.20032)) of (1.1) for k <= 36, " "and ceil(k(log k + 4.20032)/2) of [BW2023, Theorem 9.1] for 13 <= k <= 36, are confirmed in the same way") from decimal import Decimal as _Dm, getcontext as _gc _gc().prec = 40; rl = random.Random(3) xs = np.array([2.0**rl.uniform(-2, 21) for _ in range(30000)] + [1 + 2.0**-rl.uniform(1, 40) for _ in range(30000)]) la = np.log(xs) # the array path, as in the computations same = all(float(np.log(float(x))) == float(y) for x, y in zip(xs[::7], la[::7])) ul = max(abs(_Dm(float(y)) - _Dm(float(x)).ln())/_Dm(float(np.spacing(y))) for x, y in zip(xs, la)) Ds = np.concatenate([np.arange(4, 2004), np.array(sorted(rl.sample(range(2004, A_NT + 1), 18000)))]).astype(float) xa = 2*(Ds + 1)/Ds**2; l1 = np.log1p(xa) ul1 = max(abs(_Dm(float(y)) - (1 + _Dm(float(x))).ln())/_Dm(float(np.spacing(y))) for x, y in zip(xa, l1)) check(ul <= 1 and same and ul1 <= 1, f"np.log, applied to arrays, on 60000 sample arguments in [1/4, 2^21] and in (1, 3/2] is accurate to {float(ul):.2f} units in the last place, " f"and agrees with its values for single arguments; np.log1p on 20000 of the arguments that occur in the series for Lambda^star is accurate to " f"{float(ul1):.2f} units (the computations allow {A_LOG_ULP} units for the logarithm)") simple = log(2)/3 + log(17/9)/4 + log(17/9)/4 + A_S4_HI check(abs(A_lam(3) - log(17/9)/4) < 1e-15 and 6.2830 < 2+log(9)+2*simple < 6.2831, f"without Lemma 4.3(ii) for j = 3 the constant would be {2+log(9)+2*simple:.5f}") def closed(U): U = np.asarray(U, float) D = np.floor(U); D = np.where(D == U, D-1, D); D = np.maximum(D, 3) gen = np.maximum((D+1-U)/(D*(D+1)), 1/((D+1)*(D+2))) return np.select([U <= 1.5, U <= 9/4, U <= 17/6, U <= 3, U <= 10/3, U <= 3.5], [(2-U)/2, (3-U)/6, (4-U)/14, 1/12+0*U, (4-U)/12, (5-U)/30], default=gen) Ug = np.arange(1.0, 60.0, 1e-4) sv = A_saving(Ug, 0.0, 10**9, window=70) best = sv.copy() # all estimates of Section 4 as well (Lemma 4.3(ii), j = 4, 5; Lemma 4.4) for j in (4, 5): best = np.maximum(best, np.where(Ug >= j+2.0**-j, (j+2-Ug)/(2**(j+2)-2), 0)) for D in range(4, 80): a0 = Ug/(D+1); best = np.maximum(best, np.where(a0 < 1, np.minimum(a0, D*(1-a0))/(D*(D+1)), 0)) check(np.max(np.abs(sv - closed(Ug))) < 1e-12 and np.max(np.abs(best - sv)) < 1e-12 and bool(np.all(np.diff(sv) <= 1e-15)), "Lemma A.12: varsigma^star(U, 0) equals the function (A.5) on [1, 60], is nonincreasing, and is not improved by Lemma 4.3(ii) with j = 4, 5 or by Lemma 4.4") okf = True for kk in (13, 14, 17, 25): # part (ii) for small k: the inequality for every U, with equality exactly for U <= k - 1 Uk = np.arange(1.0005, kk + 3.0, 1e-3); svk = A_saving(Uk, 0.0, kk, window=70); df = closed(Uk) - svk okf &= df.min() > -1e-13 and np.abs(df[Uk <= kk - 1]).max() < 1e-13 and bool(np.all(df[Uk >= kk - 1 + 2/(kk + 1) + 2e-3] > 1e-9)) okj = all(2**(j+1) >= (j+1)*(j+2) for j in range(4, 200)) check(okf and okj, "Lemma A.12 (ii), (iii): for k = 13, 14, 17, 25 one has varsigma^star(U, 0) <= (A.5) on a grid of U <= k + 3, with equality for U <= k - 1 " "(and up to U = k - 1 + 2/(k+1)), and strict inequality beyond; and 2^(j+1) >= (j+1)(j+2) for j >= 4") old_vs = np.array([varsigma(float(x)) for x in Ug[::25]]); dv = closed(Ug[::25]) - old_vs; Uc = Ug[::25] strict = ((Uc > 17/6+1e-9) & (Uc < 10/3-1e-9)) | (Uc > 3.5+1e-9) check(dv.min() > -1e-15 and bool(np.all(dv[strict] > 0)) and np.abs(dv[~strict]).max() < 1e-15, "varsigma^star(U) >= varsigma(U) on [1, 60], with equality exactly for U <= 17/6 and 10/3 <= U <= 7/2") inc = np.log(sv[:-1]/sv[1:])/Ug[1:] tail = float(np.sum(np.log1p(2*(np.arange(60, 10**7)+1.0)/np.arange(60, 10**7, dtype=float)**2)/(np.arange(60, 10**7)+1.0))) + 2e-7 Iv = tail + np.concatenate([np.cumsum(inc[::-1])[::-1], [0.0]]); z0 = sv/2 msk = (z0 > 0.02) & (z0 < 0.3); Fv = 2 + np.log((1-z0[msk])/z0[msk]) + 2*Iv[msk]; i = int(np.argmin(Fv)) check(abs(Fv[i] - A_C0) < 2e-4 and abs(z0[msk][i] - 0.1) < 1e-4, f"min over z_0 of Psi(z_0) + 2 int_0^z_0 du/(u theta(2u)), from the definition on a grid: {Fv[i]:.5f} at z_0 = {z0[msk][i]:.4f}") # ---- (b) Lemmas A.7-A.9 on small examples: exact counts of pairs (plausibility checks, not needed for the proofs) def classes_mod_p(p, u): mu = [w for w in range(1, p) if pow(w, u*(u+1), p) == 1] label, seen = {}, set() for x in range(1, p): if x in seen: continue t = min(x*w % p for w in mu) for w in mu: y = t*w % p; label[y] = w; seen.add(y) return label def vp(n, p): if n == 0: return 10**9 v = 0 while n % p == 0: n //= p; v += 1 return v def run(k, D, pe, X, a=1, meanvalue=True): primes = [p for p, _ in pe]; J = len(primes); u = k - D m = int(np.prod(primes)); q = 1 for p, ee in pe: q *= p**ee s = {p: max(1, -(-ee//(D+1))) for p, ee in pe} A = 1; qD = 1; qD1 = 1 for p, ee in pe: A *= p**s[p]; qD *= p**max(ee-D*s[p], 0); qD1 *= p**max(ee-(D-1)*s[p], 0) Y = X//A from math import comb aD = a*comb(k, D)*(A**D//(q//qD)); aD1 = a*comb(k, D-1)*(A**(D-1)//(q//qD1)) assert Y >= 1 and gcd(aD, qD) == 1 and gcd(aD1, qD1) == 1 and qD1 % qD == 0 labs = {p: classes_mod_p(p, u) for p in primes} L = X + A*Y; cls = {} for n in range(-A*Y+1, X+1): if gcd(n, m) == 1: cls.setdefault(tuple(labs[p][n % p] for p in primes), []).append(n) d1, d2 = 2/Y**D, 2/Y**(D-1) ndiv = 1 for p, ee in pe: ndiv *= max(ee-(D-1)*s[p], 0) + 1 N1 = lambda dl: 2*L*L/qD1 + 4*k**J*ndiv*dl*(L*qD1/qD**2 + L*L/qD) rnd = random.Random(1); okP = True; okB = True; worst = 0.0; Rmax = 0.0; w = D*(D+1)//2; nb = 0 for lst in cls.values(): for _ in range(200): n1, n2 = rnd.choice(lst), rnd.choice(lst) for p in primes: v = vp(n1-n2, p) okP &= (vp(n1**u-n2**u, p) == v) and (vp(n1**(u+1)-n2**(u+1), p) == v) nn = np.array(lst, dtype=np.int64) x1 = np.array([aD*pow(n, u, qD) % qD for n in lst], dtype=np.int64) x2 = np.array([aD1*pow(n, u+1, qD1) % qD1 for n in lst], dtype=np.int64) N = len(lst); M = Ma = Mb = 0 for i0 in range(0, N, 300): sl = slice(i0, min(N, i0+300)) r1 = (x1[sl, None]-x1[None, :]) % qD; r1 = np.minimum(r1, qD-r1) r2 = (x2[sl, None]-x2[None, :]) % qD1; r2 = np.minimum(r2, qD1-r2) okm = (r1 <= d1*qD) & (r2 <= d2*qD1); dd = nn[sl, None]-nn[None, :] M += int(okm.sum()); Ma += int((okm & (dd % qD == 0) & (dd != 0)).sum()); Mb += int((okm & (dd % qD != 0)).sum()) bA = N1(d2); bB = (2*d1*qD*L + 2*d1*(qD+L)*N1(2*d2)) if d1*qD >= 1 else 0.0 okB &= (Ma <= bA) and (Mb <= bB) and (M == N + Ma + Mb); nb += Mb worst = max(worst, M/(L+bA+bB)) if meanvalue and Y >= 2: g = np.empty(N) for i, n in enumerate(lst): ph = np.array([a*pow(int(n+A*y), k, q) % q for y in range(Y)], dtype=np.float64)/float(q) g[i] = abs(np.exp(2j*pi*ph).sum()) Rmax = max(Rmax, float((g**w).sum())/(Y**w*M**0.5)) return okP, okB, worst, Rmax, nb, len(cls) okP = okB = True; worst = Rmax = 0.0; ncl = 0; nbs = [] for cfg in [(9, 3, [(11, 8)], 600, True), (9, 3, [(11, 8)], 1500, True), (9, 3, [(11, 7)], 700, True), (11, 4, [(13, 10)], 1000, True), (9, 3, [(11, 8), (13, 4)], 20000, False), (9, 3, [(11, 4), (13, 8)], 9000, False), (13, 3, [(17, 8)], 2000, True)]: r = run(cfg[0], cfg[1], cfg[2], cfg[3], meanvalue=cfg[4]) okP &= r[0]; okB &= r[1]; worst = max(worst, r[2]); Rmax = max(Rmax, r[3]); nbs.append(r[4]); ncl += r[5] check(okP, f"Lemma A.7 (valuations) on random pairs of equivalent integers, {ncl} classes in 7 configurations (k, D, q, X)") check(okB and worst < 1 and sum(1 for x in nbs if x > 0) >= 2, f"Lemma A.8 in the situation of Lemma A.9, 7 configurations: exact counts of the pairs of both kinds against the bounds of the proof; largest ratio count/bound = {worst:.3f}") check(0 < Rmax < 1, "a plausibility check for the proof of Lemma A.9 (the lemma does not assert this inequality): the sum of |g_Y|^w over a class, " f"divided by Y^w M^(1/2), is at most {Rmax:.3f}") nV, nbad, wr, n1, neq, n3 = A_pairs_test(seed=1, ntr=200) check(nbad == 0 and nV >= 300 and n1 >= 30 and neq >= 30 and n3 >= 20 and wr < 1, f"Lemma A.8 on 200 random configurations (3 <= k <= 12, up to three primes, moduli r_1 | r_2 including r_1 = 1 and r_1 = r_2, negative integers, " f"delta_1 and delta_2 of several sizes): {nV} sets V, exact counts against the bound of the lemma and the intermediate bounds of its proof; " f"no failure, largest ratio count/bound = {wr:.3f}; r_1 = 1 in {n1} configurations, r_1 = r_2 in {neq}, pairs with r_1 not dividing n - n' in {n3} sets") C1 = lambda kk, JJ: 4*kk**JJ*(kk+1)**JJ okm = True; rm = random.Random(9) for _ in range(2000): # the last step of the proof of Lemma A.9, with random values of the quantities kk = rm.randint(3, 40); JJ = rm.randint(1, 4); X = Fr(rm.randint(50, 10**7)); Y = Fr(rm.randint(1, int(X))); Dd = rm.randint(2, 6) qD = Fr(rm.randint(1, int(X))); qD1 = qD*rm.randint(1, 10**4); dq = rm.randint(1, (kk+1)**JJ); L = 2*X d1 = 2/Y**Dd; d2 = 2/Y**(Dd-1); N1 = lambda dl: 2*L*L/qD1 + 4*kk**JJ*dq*dl*(L*qD1/qD**2 + L*L/qD) Bq = 1/qD1 + qD1/(Y**(Dd-1)*X*qD**2) + 1/(Y**(Dd-1)*qD) Mb = L + N1(d2) + 2*d1*(qD*L + (qD + L)*N1(2*d2)) okm &= N1(d2) <= 8*C1(kk, JJ)*X*X*Bq and N1(2*d2) <= 16*C1(kk, JJ)*X*X*Bq and Mb/X**2 <= 192*C1(kk, JJ)*(1/X + (1 + X/Y**Dd)*Bq + qD/(Y**Dd*X)) check(okm, "the last step of the proof of Lemma A.9 (exact arithmetic, 2000 random values of the quantities with q_D <= X and d(q_(D-1)) <= (k+1)^J): " "N_1(delta_2) <= 8 C X^2 B, N_1(2 delta_2) <= 16 C X^2 B and M/X^2 <= 192 C times the expression in the lemma, where C = 4 k^J (k+1)^J") # ---- (c) falsification test of Corollary A.10 / Lemma A.13(i) on explicit sums def expsum(q, m, k, X, a): r = np.array([(a*pow(n, k, q)) % q for n in range(1, X+1) if gcd(n, m) == 1], dtype=np.float64)/q return abs(np.exp(2j*np.pi*r).sum()) worstS = 0.0; ns = 0; npos = 0; maxsv = 0.0 for (k, m, q, Xs) in [(13, 17, 17**12, [4913, 20000, 83521]), (13, 17, 17**13, [10000, 60000]), (11, 13, 13**11, [3000, 12000, 40000]), (13, 17*19, (17*19)**6, [20000, 104329]), (11, 13*17, 13**10*17**5, [30000, 120000]), (9, 11, 11**9, [900, 2500, 8000])]: for X in Xs: U = log(q)/log(X); vt = log(m)/log(X); sv1 = float(A_saving(np.array([U]), vt, k, window=40)[0]) npos += 4*(sv1 > 0); maxsv = max(maxsv, sv1) for a in (1, 2, 3, 5): worstS = max(worstS, expsum(q, m, k, X, a)/X**(1-sv1)); ns += 1 check(worstS < 1 and npos == 8 and maxsv < 0.01, f"{ns} sums with k in {{9, 11, 13}}, 2.4 < U < 4: largest |S|/X^(1-varsigma^star(U,vartheta)) = {worstS:.3f}; a consistency check only: " f"the saving varsigma^star is positive for {npos} of the sums, and at most {maxsv:.4f}") # ---- (d) the chains of Lemma A.14 against Definition A.11, and the coefficients in the proof of Lemma A.14(iv) rnd = random.Random(7); worstm = 0.0; ntr = 0 for _ in range(160): k = rnd.choice([13, 14, 15, 16, 17, 19, 23, 29, 36, 37, 38, 41, 50, 64, 81, 100, 150, 233, 300, 500, 1000, 2000, 5000]) t = E**rnd.uniform(log(2*tau_k(k)), log(0.19)) if t > 1/12: th = float(A_top_types(k, np.array([t])).max()) else: D = 3 while (D+1)*(D+2)*t <= 1 and D <= A_Dc(k): D += 1 th = float(A_band_values(k, np.array([t]), D).max()) assert th <= k/2 U = np.linspace(1+1e-9, th, int(3000*th)+2) worstm = min(worstm, (float(A_saving(U, th/k, k).min()) - t)/t); ntr += 1 check(worstm > -1e-11, f"{ntr} random pairs (k, t): for the largest theta given by the chains, varsigma^star(U, theta/k) >= t for all U <= theta on a grid (least relative margin {worstm:.1e})") okc = all(r2 >= 3 and r0 <= r2 + 1 for j in (1, 2, 3) for r0, r2 in ((j + 2, 2**(j+1) - 1), (j + 2, 2**(j+2) - 2))) \ and all(j + Fr(1, 2**j) >= Fr(3, 2) for j in (1, 2, 3)) def near_lo(x, ex): return Fr(x) <= ex < Fr(float(_up(x))) # x is the largest floating point number <= ex, or the next one below def near_up(x, ex): return Fr(float(_dn(x))) < ex <= Fr(x) for D in range(3, 3001): d = Fr(D) rho = [((d+1)*(d-1)/(d-2), d*(d+1)**2/(d-2), (d+1)*(2*d-1)/(d-2)), (d+1, d*(d+1)**2/(2*d-2), (d+1)*(3*d-1)/(2*d-2)), (d+1, d*(d+1), d)] # (rho_0, rho_1, rho_2) for the three terms of r_D; the left end point of T_(D+1) is lam1 t + lam2 vartheta lam1 = (d+1)*(d+2)**2/2; lam2 = d*(d+2)/2 okc &= all(r2 >= 3 and r0 <= r2 + 1 for r0, r1, r2 in rho) and (d+1)*(d-1)/2 >= 4 okc &= all(r0 < lam2 + r2 and r0 < D + 1 + r2 for r0, r1, r2 in rho) # a_1 < 1 at the junction T_D, T_(D+1), and at the end (k >= D + 1) if D <= 1600: # the constants that the program uses are these numbers, rounded okc &= all(near_lo(P, r0) and near_up(Q, r1 + lam1) and Fr(Rl) <= lam2 + r2 <= Fr(Ru) and Fr(Ru) - Fr(Rl) <= Fr(float(np.spacing(Ru))) for (P, Q, Rl, Ru), (r0, r1, r2) in zip(_A_jconst(D), (rho[2], rho[1], rho[0]))) okc &= all(near_lo(P, r0) and near_up(Q, r1) and Fr(cl) <= r2 <= Fr(cu) and Fr(cu) - Fr(cl) <= Fr(float(np.spacing(cu))) for (P, Q, cl, cu), (r0, r1, r2) in zip(_A_econst(D), (rho[2], rho[1], rho[0]))) stat = [(3, 7), (3, 10), (0.25, 1), (0.125, 1), (4, 11), (4, 18), (3, 10.5), (3, 13.5), (4, 14.5), (4, 21.5), (0.875, 3), (0.875, 8), (4.875, 20), (5, 22.5), (5, 37.5), (5, 13 + 15), (5, 13 + 30), (3, 13 + 3), (3, 13 + 6), (4, 13 + 7), (4, 13 + 14)] check(okc and all(P < R for P, R in stat), "Lemma A.14 (iv): for the intervals I_1, I_2, I_3 and the tents T_D (3 <= D <= 3000) every term of a right end point has rho_2 >= 3 and " "rho_0 <= rho_2 + 1, and the left end points have lambda_0 >= 3/2 or lambda_2 >= 4; every condition vartheta <= a_1 - a_2 t in the " "program's chains has a_1 < 1; and for D <= 1600 the three constants of each junction and end condition in the program are the exact " "fractions derived from the end points of Lemma A.14, rounded in the safe direction") # ---- (e) the second case of Section A.4: the tables of [VW2000] for 13 <= k <= 20 KV = sorted(A_VW_T); tc = {k: A_vw_tables_check(k) for k in KV} check(KV == list(range(13, 21)) and sorted(A_VW4) == KV and all(tc[k][1] for k in KV) and [len(A_vw_delta(k)) - 1 for k in KV] == [36, 40, 43, 47, 50, 54, 58, 62] and sum(len(A_VW4[k].split()) for k in KV) == 374 and max(tc[k][0] for k in KV) < Fr(4, 10**7), "the tables of [VW2000] for 13 <= k <= 20 (lambda_s and phi_1 for 3 <= s <= 36, 40, 43, 47, 50, 54, 58, 62; 374 rows): the relation " "lambda_s = lambda_(s-1)(1 - phi_1) + 1 + (2s - 2) phi_1 of [VW2000, (4.10)] holds for every row to within " f"{float(max(tc[k][0] for k in KV)):.1e} (the entries have 7 and 8 decimals)") check(all(tc[k][2][1] == A_VW_FOOTER[k][1] == k - 1 and 0 <= tc[k][2][0] - A_VW_FOOTER[k][0] <= 1 for k in KV) and all(Fr(4*(k-1)**2, 1)/(k - 2*A_vw_delta(k)[k-1]) <= Fr(A_VW_T[k]) < Fr(4*(k-1)**2, 1)/(k - 2*A_vw_delta(k)[k-1]) + Fr(1, 100) for k in KV), "the same tables: the largest value of (k - 2 Delta_s)/(4s^2 - 1) is attained at s = k - 1 and agrees with the number tau(k) printed below each table; " "and T_k is 4u^2/(k - 2 Delta_u) with u = k - 1, rounded up to two decimals") rvw = {k: A_rec_vw(k) for k in KV} check(all(all(rvw[k]['checks'].values()) for k in KV) and all(rvw[k]['th'] >= 1.78 for k in KV) and all(set(rvw[k]['checks']) == {'V1', 'V2', 'V3', 'R1', 'R3', 'Y', 'Y419'} for k in KV), "Theorem A.21 in the second case, 13 <= k <= 20 (exact arithmetic): (V1), (V2), (V3); (R1): c_0 < 1/T_k, c_n < 1/10 and theta_k(2 c_n) >= 3/2 " f"(the chains give at least {min(rvw[k]['th'] for k in KV):.4f}); (R3); Y^VW(k) >= 4k + 1; and Y^VW(k) >= 4.19 k, so that part (ii) of the " "theorem applies to Theorem 2.4 as well") # ---- (f) the recursion: Theorem A.21 in the first case, Propositions A.22 and A.23, Tables 6 and 7 ks = list(range(37, KNUM+1)); K = np.array(ks); c0lo, c0hi = Fr(A_C0_R[0]), Fr(A_C0_R[1]) # the step of the recursion against theta_k computed directly from Definition A.11 (not rigorous; an independent test of (R2)) rs = random.Random(11); worst = np.inf; ncross = 0; npts = 0 for k in (13, 17, 21, 37, 100, 577, 1000, 5000): Dlim = min(A_Dc(k) + 1, k - 1); kv = np.array([float(k)]); tau = 1/float(A_VW_T[13]) if k == 13 else tau_k(k) cs = [tau*float(np.exp(rs.uniform(0, log(0.09/tau)))) for _ in range(8)] cs += [0.5*_A_point(D)*(1 - 0.3*rs.random()*(1.5 + D/3)/k) for D in range(3, min(Dlim, 12) + 1) if 0.5*_A_point(D) > tau][:6] # just below a point of division for c in cs: D = int(min(A_band_index(np.array([2*c]), Dlim)[0], Dlim)) cand, th = A_rec_step_lo(kv, np.array([c]), D); cn = float(cand[0]) if cn > A_REC_CMAX: continue v = np.concatenate([np.linspace(c, cn, 201), [0.5/(d*(d+1))*(1 + 1e-12) for d in range(3, 60) if c < 0.5/(d*(d+1)) < cn]]) lo = A_theta_direct(k, 2*v)[0] worst = min(worst, float(np.min(v/(1 - lo/k))/cn)); npts += 1; ncross += int(D >= 3 and 2*cn > _A_point(D)) check(worst > 1 - 1e-9 and npts >= 80 and ncross >= 8, f"(R2) for single steps: at {npts} starting points c ({ncross} of the steps cross a point p_D/2 = 1/(2D(D+1))), the least value of v/(1 - theta_k(2v)/k) " f"over 201 points v in [c, c'] and just beyond each point of division, with theta_k from Definition A.11, is at least c' (least ratio - 1: {worst-1:.1e})") rec = A_rec_table(); kall = list(range(A_KREC, KNUM+1)) Yf = np.array([rec[k]['Y'] for k in ks]); Gf = np.array([rec[k]['G'] for k in ks]); Lf = np.array([rec[k]['l'] for k in ks]) nn = np.array([rec[k]['n'] for k in ks]); cn = np.array([rec[k]['c'] for k in ks]) check(sorted(rec) == kall and A_KREC == 17 and all(rec[k]['G'] == floor(Fr(rec[k]['Y'])) + 1 and rec[k]['l'] == floor(Fr(rec[k]['Y'])/2) + 1 and 2*rec[k]['l'] > Fr(rec[k]['Y']) and rec[k]['c'] <= A_REC_CMAX and Fr(rec[k]['c']) < Fr(1, 10) and rec[k]['n'] >= 1 and Fr(rec[k]['Y']) > 4*k + 1 for k in kall) and 0.99999 < (nn/K).min() and (nn/K).max() < 1.04 and f"{cn.min():.4f}" == "0.0932" and f"{cn.max():.5f}" == "0.09996", f"Theorem A.21 in the first case for 17 <= k <= {KNUM}: sequences with (R1)-(R3) and upper bounds Y^flat(k) for k Psi(c_n) + 2n (directed rounding); " f"for k >= 37 the number n of steps lies between k and {(nn/K).max():.4f} k, and c_n between {cn.min():.4f} and {cn.max():.5f}") okc, m3, mY, xY = A_rec_certify(rec) check(okc and m3 > 0 and 0 <= mY <= xY < 1e-6, f"the same sequences, in decimal arithmetic with 40 digits and a correctly rounded logarithm: (R3) holds (least value of " f"Psi(c_(n-1)) - Psi(c_n) - 2/k: {m3:.1e}), and Y^flat(k) >= k Psi(c_n) + 2n (the difference lies between {mY:.1e} and {xY:.1e})") def thr(holds, fails): """the least k0 such that holds(k) for all k >= k0, and fails(k0 - 1) (both decided exactly)""" bad = [k for k in ks if not holds(k)]; k0 = (max(bad) + 1) if bad else 37 return k0, (k0 == 37 or fails(k0 - 1)) gk = Gf/K; k3, f3 = thr(lambda k: rec[k]['G'] < Fr(63, 10)*k, lambda k: rec[k]['G'] >= Fr(63, 10)*k) check(all(rec[k]['G'] < Fr(634, 100)*k for k in ks) and ks[int(gk.argmax())] == 39 and Fr(rec[39]['G'], 39) == Fr(19, 3) and rec[39]['G'] == 247 and k3 == 47 and f3, f"Proposition A.22 (ii): G^flat(k) < 6.34 k for 37 <= k <= {KNUM} (largest ratio 19/3, at k = 39, where G^flat = 247); G^flat(k) < 6.3 k for k >= {k3}, but not for k = {k3-1}") rG = (Gf - A_C0_LO*K)/np.sqrt(K); rY = (Yf - A_C0_LO*K)/np.sqrt(K) k1G, f1G = thr(lambda k: rec[k]['G'] < c0lo*k, lambda k: rec[k]['G'] >= c0hi*k) k2G, f2G = thr(lambda k: A_lt_sqrt(-(c0lo*k - rec[k]['G']), Fr(-1, 4), k), lambda k: not A_lt_sqrt(-(c0hi*k - rec[k]['G']), Fr(-1, 4), k)) k1Y, f1Y = thr(lambda k: Fr(rec[k]['Y']) < c0lo*k, lambda k: Fr(rec[k]['Y']) >= c0hi*k) k2Y, f2Y = thr(lambda k: A_lt_sqrt(Fr(rec[k]['Y']) - c0lo*k, Fr(-1, 4), k), lambda k: not A_lt_sqrt(Fr(rec[k]['Y']) - c0hi*k, Fr(-1, 4), k)) check(all(A_lt_sqrt(Fr(int(rec[k]['G'])) - c0lo*k, Fr(32, 100), k) for k in ks) and ks[int(rG.argmax())] == 39 and 0.3197 < rG.max() < 0.3198 and all(A_lt_sqrt(Fr(rec[k]['Y']) - c0lo*k, Fr(23, 100), k) for k in ks) and ks[int(rY.argmax())] == 37 and 0.2266 < rY.max() < 0.2267 and (k1G, k2G, k1Y, k2Y) == (56, 78, 50, 69) and f1G and f2G and f1Y and f2Y, f"Proposition A.22 (iii): G^flat(k) < C_0^star k + 0.32 sqrt k (largest ratio {rG.max():.4f}, at k = 39) and Y^flat(k) < C_0^star k + 0.23 sqrt k " f"(largest ratio {rY.max():.4f}, at k = 37) for 37 <= k <= {KNUM}; G^flat(k) < C_0^star k for k >= {k1G}, G^flat(k) < C_0^star k - sqrt(k)/4 for k >= {k2G}, " f"Y^flat(k) < C_0^star k for k >= {k1Y}, Y^flat(k) < C_0^star k - sqrt(k)/4 for k >= {k2Y}; the computed inequalities fail for k = {k1G-1}, {k2G-1}, {k1Y-1}, {k2Y-1}") r2 = (Lf - A_C0_LO*K/2)/np.sqrt(K); k4, f4 = thr(lambda k: rec[k]['l'] < c0lo*k/2, lambda k: rec[k]['l'] >= c0hi*k/2) check(all(A_lt_sqrt(Fr(int(rec[k]['l'])) - c0lo*k/2, Fr(24, 100), k) for k in ks) and 0.2399 < r2.max() < 0.2400 and ks[int(r2.argmax())] == 39 and rec[39]['l'] == 124 and k4 == 64 and f4, f"Proposition A.22 (iv): l^flat(k) < C_0^star k/2 + 0.24 sqrt k for 37 <= k <= {KNUM} (largest ratio {r2.max():.4f}, at k = 39); l^flat(k) < C_0^star k/2 for k >= {k4}, but not for k = {k4-1}") check([rec[k]['l'] for k in (64, 128, 256, 512, 1024, 2048, 4096)] == [201, 399, 798, 1597, 3199, 6407, 12827] and [rec[k]['l'] for k in (37, 50, 100, 1000)] == [117, 157, 312, 3124] and [rec[k]['G'] for k in (21, 37, 50, 100, 1000, 5000)] == [135, 234, 314, 624, 6248, 31323] and rec[64]['l'] < c0lo*32, "l^flat = 201, 399, 798, 1597, 3199, 6407, 12827 for k = 64, ..., 4096, and 117, 157, 312, 3124 for k = 37, 50, 100, 1000; " "G^flat = 135, 234, 314, 624, 6248, 31323 for k = 21, 37, 50, 100, 1000, 5000") old = np.array([floor(k*bound(k, Theta_sharp(k))) for k in ks]); pc = 100*(1 - Gf/old) check((old-Gf).min() == 38 and ks[int((old-Gf).argmin())] == 39 and (old-Gf)[-1] == 793 and 2.46 < pc.min() < 2.47 and 14.28 < pc.max() < 14.29 and ks[int(pc.argmax())] == 37 and [int(old[k-37]) for k in (37, 50, 100, 1000)] == [273, 353, 709, 6492], f"G^flat(k) is smaller than G^sharp(k) by at least {(old-Gf).min()} (k = {ks[int((old-Gf).argmin())]}) and by {(old-Gf)[-1]} for k = {KNUM}; " f"by {pc.min():.2f} per cent (k = {ks[int(pc.argmin())]}) to {pc.max():.2f} per cent (k = {ks[int(pc.argmax())]}); G^sharp = 273, 353, 709, 6492 for k = 37, 50, 100, 1000") fr = Yf - np.floor(Yf); hi = [k for k, f in zip(ks, fr) if f > 0.999]; lo = [k for k, f in zip(ks, fr) if f < 0.001] check(hi == [257, 513, 623, 1656, 1693, 2811, 4481] and 0.99969 < fr.max() < 0.99970 and ks[int(fr.argmax())] == 513 and len(lo) == 10 and ks[int(fr.argmin())] == 2885 and 0.00005 < fr.min() < 0.00006, f"for 37 <= k <= {KNUM} the fractional part of Y^flat(k) exceeds 0.999 for k = {', '.join(map(str, hi))} (largest: {fr.max():.5f}, at k = 513), and it is " f"smaller than 0.001 for {len(lo)} values of k (smallest: {fr.min():.5f}, at k = 2885)") # Table 6: 21 <= k <= 36 and selected k >= 37 TB = {21: (21, '134.0179', 135, 68, 153), 22: (22, '140.2882', 141, 71, 161), 23: (23, '146.6033', 147, 74, 169), 24: (25, '152.8904', 153, 77, 178), 25: (26, '159.2111', 160, 80, 186), 26: (27, '165.5140', 166, 83, 194), 27: (28, '171.7246', 172, 86, 203), 28: (29, '177.8940', 178, 89, 211), 29: (30, '184.1304', 185, 93, 220), 30: (31, '190.3041', 191, 96, 229), 31: (32, '196.5224', 197, 99, 237), 32: (33, '202.7877', 203, 102, 246), 33: (34, '209.0098', 210, 105, 254), 34: (35, '215.2477', 216, 108, 263), 35: (36, '221.4729', 222, 111, 272), 36: (37, '227.6492', 228, 114, 281), 37: (38, '233.8173', 234, 117, 273), 38: (39, '239.9664', 240, 120, 279), 39: (40, '246.0702', 247, 124, 285), 40: (41, '252.3324', 253, 127, 291), 41: (42, '258.4626', 259, 130, 297), 50: (51, '313.9727', 314, 157, 353), 56: (56, '350.9112', 351, 176, 398), 67: (67, '418.7500', 419, 210, 481), 78: (78, '486.9647', 487, 244, 566), 81: (81, '505.5671', 506, 253, 590), 100: (100, '623.5525', 624, 312, 709), 150: (151, '933.7193', 934, 467, 1062), 174: (175, '1083.1920', 1084, 542, 1241), 200: (201, '1245.1492', 1246, 623, 1407), 212: (214, '1320.0039', 1321, 661, 1483), 250: (252, '1556.6930', 1557, 779, 1724), 330: (334, '2056.2439', 2057, 1029, 2231), 500: (509, '3118.9115', 3119, 1560, 3309), 700: (715, '4370.0634', 4371, 2186, 4578), 927: (948, '5790.3206', 5791, 2896, 6025), 1000: (1024, '6247.6151', 6248, 3124, 6492), 2000: (2057, '12511.3081', 12512, 6256, 12902), 3000: (3093, '18780.3738', 18781, 9391, 19309), 5000: (5167, '31322.5609', 31323, 15662, 32116)} okB = list(TB) == list(range(21, 37)) + list(A_REC_SELECT) print(" Table 6: k n Y^flat G^flat l^flat before") for k, want in TB.items(): r = rec[k]; got = (r['n'], f"{r['Y']:.4f}", r['G'], r['l'], A_before(k)); okB &= got == want print(f" {k:8d} {got[0]:5d} {got[1]:>10s} {got[2]:6d} {got[3]:6d} {got[4]:6d}") sm = list(range(21, 37)); dbw = [A_previous(k) - rec[k]['G'] for k in sm]; csm = [rec[k]['c'] for k in sm] msm = min(rec[k]['G'] - rec[k]['Y'] for k in sm) check(okB and min(dbw) == 18 and max(dbw) == 53 and max(Fr(rec[k]['G'], k) for k in sm) == Fr(135, 21) and msm > 0.10 and [A_previous(k) for k in (21, 30, 36)] == [153, 229, 281] and [rec[k]['G'] for k in (30, 36)] == [191, 228], f"the entries of Table 6; for 21 <= k <= 36 the numbers G^flat(k) are smaller than the bounds of (1.1) by {min(dbw)} to {max(dbw)}, G^flat(k)/k <= 135/21, " f"Y^flat(k) lies below the next integer by at least {msm:.3f}, and c_n lies between {min(csm):.4f} and {max(csm):.4f}") check([rec[k]['G'] for k in (17, 18, 19, 20)] == [109, 116, 122, 128] and [rec[k]['l'] for k in (17, 18, 19, 20)] == [55, 58, 61, 64], "without the tables of [VW2000] the recursion gives G^flat = 109, 116, 122, 128 and l^flat = " + ", ".join(str(rec[k]['l']) for k in (17, 18, 19, 20)) + " for k = 17, ..., 20") # Table 7: 13 <= k <= 20 with the tables of [VW2000] TA = {13: ('104.95', 10, '0.0583', '79.0746', 80, 81, 40, 44), 14: ('114.19', 11, '0.0590', '85.7394', 86, 89, 43, 45), 15: ('123.40', 12, '0.0602', '92.2842', 93, 97, 47, 49), 16: ('132.60', 14, '0.0696', '98.7888', 99, 105, 50, 53), 17: ('141.78', 15, '0.0701', '105.2755', 106, 113, 53, 57), 18: ('150.95', 16, '0.0704', '111.8246', 112, 121, 56, 61), 19: ('160.07', 17, '0.0707', '118.3697', 119, 129, 60, 65), 20: ('169.18', 19, '0.0788', '124.7279', 125, 137, 63, 69)} okA = True print(" Table 7: k T_k n c_n Y^VW G^VW before l^VW before") for k in KV: r = rvw[k]; got = (A_VW_T[k], r['n'], f"{r['c']:.4f}", f"{float(r['Y']):.4f}", r['G'], A_VW_PREVIOUS[k], r['l'], A_gplus_before(k)) okA &= got == TA[k] print(f" {k:8d} {got[0]} {got[1]:2d} {got[2]} {got[3]:>8s} {got[4]:5d} {got[5]:5d} {got[6]:5d} {got[7]:5d}") mvw = min(rvw[k]['G'] - rvw[k]['Y'] for k in KV) check(okA and [rvw[k]['G'] for k in KV] == [80, 86, 93, 99, 106, 112, 119, 125] and all(rvw[k]['G'] < A_VW_PREVIOUS[k] for k in KV) and mvw > Fr(17, 100) and Fr(790745, 10**4) < rvw[13]['Y'] < Fr(790746, 10**4) and max(Fr(rvw[k]['G'], k) for k in KV) == Fr(119, 19), "the entries of Table 7: G(k) <= 80, 86, 93, 99, 106, 112, 119, 125 for k = 13, ..., 20 (previously 81, 89, 97, 105, 113, 121, 129, 137); " f"the numbers Y^VW(k) lie below the next integer by at least {float(mvw):.3f}; Y^VW(13) = 79.0745...; G^VW(k)/k <= 119/19") # Theorem A.3(ii) and the comparison with [BW2023, Theorems 9.1 and 9.2] that follows Theorem A.3 lv = {k: rvw[k]['l'] for k in KV}; mid = [k for k in range(21, 37) if k != 32] gs = [A_gplus_before(k) - lv[k] for k in KV]; gm = [A_gplus_before(k) - rec[k]['l'] for k in mid]; bm = [A_gplus_before(k) for k in mid] check([lv[k] for k in KV] == [40, 43, 47, 50, 53, 56, 60, 63] and all(2*lv[k] > rvw[k]['Y'] for k in KV) and [A_gplus_before(k) for k in KV] == [44, 45, 49, 53, 57, 61, 65, 69] and A_gplus_before(13) == A_previous_plus(13) and min(gs) == 2 and max(gs) == 6 and all(rec[k]['l'] - lv[k] in (1, 2) for k in (17, 18, 19, 20)) and min(gm) == 9 and max(gm) == 27 and min(bm) == 77 and max(bm) == 141 and min(rec[k]['l'] for k in mid) == 68 and max(rec[k]['l'] for k in mid) == 114 and rec[32]['l'] == 102 and A_gplus_before(32) == 123 and lv[16] == 50 and A_gplus_before(16) == 53, "Theorem A.3(ii): l^VW(k) = 40, 43, 47, 50, 53, 56, 60, 63 for 13 <= k <= 20; the bounds for G^+(k) of [BW2023, Theorem 9.1] are 44 (k = 13), " f"45, 49, 57, 61, 65, 69 (Table 3 there) and between {min(bm)} and {max(bm)} for 21 <= k <= 36, k != 32; the new bounds are smaller by " f"{min(gs)} to {max(gs)} for k <= 20 and by {min(gm)} to {max(gm)} for 21 <= k <= 36 (l^flat(k) between 68 and 114 there); for 17 <= k <= 20 " "l^VW(k) is smaller than l^flat(k) by 1 or 2; for k = 16 and 32 the admissible numbers 50 and 102 replace 53 and 123 of [BW2023, Theorem 9.2]") # the plain double precision version with theta_k from Definition A.11 (no chains) dd = [] for k in (17, 21, 37, 100, 577, 1000, 5000): p = A_rec_plain(k); dd.append(abs(rec[k]['Y'] - p[0])); _require(p[1] == rec[k]['n']) for k in KV: p = A_rec_plain(k, vw=True); dd.append(abs(float(rvw[k]['Y']) - p[0])); _require(p[1] == rvw[k]['n']) check(max(dd) < 2e-8, f"the recursion in plain double precision, with theta_k computed from Definition A.11 by bisection instead of chains, gives the same n and " f"the same value of k Psi(c_n) + 2n to within {max(dd):.1e} (k = 17, 21, 37, 100, 577, 1000, 5000, and 13 <= k <= 20 with the tables)") pc = [A_rec_potential(k) for k in (21, 37, 100)] check(all(m <= 0 and sl < -0.1 and st > -1e-12 and abs(dv) < 0.005 for m, sl, st, dv in pc), "the potential in the proof of Theorem A.21 for k = 21, 37, 100, on a grid of 40000 points (plain double precision, delta_0 = eps_2 = 0): " "it is nonincreasing, g <= G - 2.1 on (0, 1/5], G(z) - G(z/(1 - theta_k(2z)/k)) >= 2/k below z_0 (least value of the difference minus 2/k: " f"{min(p[2] for p in pc):.1e}), and k G(c_0) agrees with Y^flat(k) to within {max(abs(p[3]) for p in pc):.4f}") # Theorem A.2 lit = {9: 47, 10: 55, 11: 63, 12: 72} # [Woo2016, Theorem 1.1] bw = {k: A_previous(k) for k in range(1, 9)} ratios = [Fr(bw[k], k) for k in range(1, 9)] + [Fr(lit[k], k) for k in lit] + [Fr(rvw[k]['G'], k) for k in KV] + [Fr(rec[k]['G'], k) for k in sm] check(max(ratios) < Fr(643, 100) and max(ratios) == Fr(135, 21) and max(Fr(bw[k], k) for k in range(1, 9)) == Fr(51, 8) and all(Fr(rec[k]['G'], k) < Fr(634, 100) for k in ks), "Theorem A.2: G(k) < 6.43 k for every k: by (1.1) for k <= 8 (largest ratio 51/8 = 6.375), by [Woo2016] for 9 <= k <= 12 (at most 6), " "by Table 7 for 13 <= k <= 20 (at most 119/19 = 6.2632), by Table 6 for 21 <= k <= 36 (at most 135/21 = 6.4286), and by Proposition A.22 for k >= 37") # ---- (g) Section A.8: the numbers B^nat(k), Theta^nat(k) for 37 <= k <= KNUM (directed rounding), and Proposition A.24 (i) tab = A_table() B = np.array([tab[k]['B'] for k in ks]); Th = np.array([tab[k]['Th'] for k in ks]); kB = np.array([tab[k]['kB'] for k in ks]) Ev = np.array([tab[k]['E'] for k in ks]); refd = [k for k in ks if tab[k]['ref'] == 8] check(len(refd) == 45 and all(tab[k]['Th'] >= 2.25 and Fr(tab[k]['Y']) >= Fr(tab[k]['kB']) - 1 and 2*tab[k]['s0'] > Fr(tab[k]['x']) and tab[k]['s0'] >= k for k in ks) and 5.6507 < Th.min() < 5.6508 and int(Th.argmin()) == 0, f"(A.21) for 37 <= k <= {KNUM}: Theta^nat(k) >= 9/4 (least value {floor(1e4*Th.min())/1e4:.4f}..., at k = 37), and upper bounds Y^nat(k) = k B^nat(k) - 1; the subdivision is " f"refined eightfold for the {len(refd)} values of k at which k B^nat(k) has fractional part < {A_REFINE_BELOW}") tenk = Fr(107961, 10000) k5, f5 = thr(lambda k: Fr(tab[k]['E']) < tenk*k - Fr(3, 100), lambda k: Fr(tab[k]['E']) > tenk*k + Fr(1, 10)) m11 = min(11*k - Fr(tab[k]['E']) for k in ks) rE = (Ev - 10.7961*K)/np.sqrt(K) check(all(A_lt_sqrt(Fr(tab[k]['E']) - tenk*k, Fr(1), k) for k in ks) and ks[int(rE.argmax())] == 45 and 0.9909 < rE.max() < 0.9911 and k5 == 135 and f5 and m11 > Fr(19, 10) and all(tab[k]['s0'] > 3*k for k in ks), f"Proposition A.24 (i): E^nat(k) < 10.7961 k + sqrt k (largest ratio {rE.max():.4f}, at k = {ks[int(rE.argmax())]}); 10.7961 k - E^nat(k) > 0.03 for k >= {k5}, " f"and the computed value of this difference is negative for k = {k5-1}; 11 k - E^nat(k) > 1.9 for all k (least value {float(m11):.2f}); s_0^nat(k) > 3k") mlog = min(Fr(k)*(Fr(A_log_lo(k)) + Fr(802113, 100000)) - 25 - Fr(tab[k]['E']) for k in ks) check(mlog > Fr(3, 10) and 10.9473 < (Ev/K).max() < 10.9475 and int((Ev/K).argmax()) == 0, f"Proposition A.24 (i): k(log k + 8.02113) - E^nat(k) > 25.3 for all k (least value {float(mlog)+25:.2f}); E^nat(k) <= {(Ev/K).max():.5f} k, the maximum being at k = 37") # the plain double precision version (A_bound) and the second implementation in 40-digit decimal arithmetic (A_exact) dd = [] for k in (37, 100, 500, 1000, 1201, 5000): Bf, Thf = A_bound(k); Br, Thr = A_bound_rig(k); dd.append(max(k*(Br - Bf), 0.0)); _require(Br >= Bf - 1e-12 and abs(Thr - Thf) < 1e-12*Thf) check(max(dd) < 4e-9, f"plain double precision agrees with directed rounding to within {max(dd):.1e} in k B^nat(k) (k = 37, 100, 500, 1000, 1201, 5000)") for k in (37, 577, 1201): kBe, The = A_exact(k, grid=A_grid_rig(k)); kr = k*A_bound_rig(k)[0] check(abs(float(kBe) - kr) < 1e-9 and float(kBe) <= kr and abs(float(The) - tab[k]['Th']) < 1e-9, f"k = {k}: the second implementation gives k B^nat = {str(kBe)[:len(str(floor(kBe)))+13]} on the same subdivision (directed rounding: {kr:.9f})") for k in (577, 1201): kBe, The = A_exact(k, grid=A_grid_rig(k, refine=8)); kr = tab[k]['kB'] check(abs(float(kBe) - kr) < 5e-9 and float(kBe) <= kr and tab[k]['ref'] == 8, f"k = {k}, refined subdivision: the second implementation gives k B^nat = {str(kBe)[:len(str(floor(kBe)))+13]} (directed rounding: {kr:.9f})") # Table 8 big = {k: A_values_rig(k, *A_bound_rig(k)) for k in (10**4, 10**5, 10**6)} def row(k): r = tab[k] if k in tab else big[k]; return r['Th'], r['B'], r['G'], 2*r['s0'], r['b'], r['E'] T6 = {37: ('5.651', '0.0297', 226, '405.05', '450.43'), 46: ('6.423', '0.0263', 282, '502.88', '544.05'), 50: ('6.788', '0.0250', 306, '545.31', '587.90'), 100: ('10.560', '0.0163', 612, '1081.02', '1174.94'), 200: ('15.614', '0.0112', 1228, '2154.90', '2330.23'), 500: ('26.006', '0.0068', 3092, '5385.65', '5582.86'), 1000: ('37.615', '0.0047', 6210, '10775.05', '11022.78'), 2000: ('54.460', '0.0033', 12458, '21556.89', '21950.76'), 5000: ('87.573', '0.0021', 31236, '53916.71', '54717.71'), 10**4: ('125.397', '0.0014', 62572, '107866.93', '109295.89'), 10**5: ('401.897', '0.0005', 627394, '1079276.33', '1091049.59'), 10**6: ('1276.717', '0.0001', 6279514, '10795000.84', '10905541.15')} ok6 = True print(" Table 8: k Theta^nat b^nat 2 s_0^nat E^nat Table 4") for k, want in T6.items(): Thv, Bv, Gv, s2, b, E1 = row(k); got = (f"{Thv:.3f}", f"{b:.4f}", s2, f"{E1:.2f}", f"{E_sharp(k):.2f}") ok6 &= got == want print(f" {k:8d} {got[0]:>8s} {got[1]} {got[2]:8d} {got[3]:>12s} {got[4]:>12s}") check(ok6 and floor(tab[100]['E']) + 1 == 1082 and floor(tab[1000]['E']) + 1 == 10776, "the entries of Table 8; the exponents 1/1082 and 1/10776 for k = 100 and k = 1000") # ---- (h) large k: Lemma A.18 and Proposition A.19, and Proposition A.24 (ii) L1 = log(TAU_INV/2) def claimed(k, t): n = A_D0(k) if t >= (k-9)/(8*k): return (3-6*t)/(1+3/k) if t > 1/12: return (4-14*t)/(1+7/k) if t >= (k-20)/(6*(3*k+10)): return (4-12*t)/(1+8/k) if t > 1/20: return (5-30*t)/(1+15/k) D = 4 while (D+1)*(D+2)*t <= 1: D += 1 return (D+1)*(1-D*t)/(1+D/k) if D <= n-1 else n/(1+n/k) rnd = random.Random(11); okc = True; nt = 0 for _ in range(3000): k = int(5000*E**rnd.uniform(0, log(2e5))); n = A_D0(k) t = rnd.choice([E**rnd.uniform(log(0.3/(n*(n+1))), log(0.199)), 1/(rnd.randint(4, n)*(rnd.randint(4, n)+1)), (k-9)/(8*k), (k-20)/(6*(3*k+10))]) cl = claimed(k, t) if t > 1/12: got = float(A_top_types(k, np.array([t])).max()) else: D = 3 while (D+1)*(D+2)*t <= 1: D += 1 got = float(A_band_values(k, np.array([t]), min(D, n)).max()) okc &= got >= cl*(1-1e-12); nt += 1 check(okc, f"Lemma A.18 at {nt} random points (k, t) with 5000 <= k <= 1e9: the chains give at least the stated lower bound for theta_k(t)") okJ = all(D*(D*D-3)*(D+8) >= (D+2)*(D**3+4*D*D-1) for D in range(8, 10**5)) and all(D*(D*D-3)/((D+2)*(D**3+4*D*D-1)) > 0.066 for D in range(4, 8)) check(okJ, "the junction condition at the level 1/((D+1)(D+2)) is vt <= D(D^2-3)/((D+2)(D^3+4D^2-1)); this is at least 1/(D+8) for D >= 8 and exceeds 0.066 for 4 <= D <= 7") okD = all(Fr(D*(D*D+4*D-1), D-3) - D == D*D + 6*D + 20 + Fr(60, D-3) and Fr(D*(D*D+3*D-1), D-2) - D == D*D + 4*D + 9 + Fr(18, D-2) and 2*D + 6 - Fr(60, (D-3)**2) > 0 and 2*D + 4 - Fr(18, (D-2)**2) > 0 for D in range(5, 3000)) \ and 16 + 24 + 20 + 60 == 120 and 16 + 16 + 9 + 9 == 50 and all(Fr(60, n-3) <= 12 and Fr(18, n-2) <= 23 + 2*n for n in range(67, 3000)) check(okD, "the proof of Lemma A.18: D(D^2+4D-1)/(D-3) - D = D^2+6D+20+60/(D-3) and D(D^2+3D-1)/(D-2) - D = D^2+4D+9+18/(D-2); these increase for D >= 5, " "they are 120 and 50 for D = 4, and they are at most D^2+6D+32 for D >= 67") kl = sorted(set([5000+i for i in range(0, 20001)] + [int(5000*1.0005**i) for i in range(0, 38300)])); kl = [k for k in kl if k <= 10**12] w1 = w2 = w3 = w4 = w5 = w6 = wE = -1e99; wEk = 0 for k in kl: cC, cT, cR, cW, cS, n, Thr = A_Yreg_terms(k); sq = sqrt(k) w1 = max(w1, cC); w2 = max(w2, cT + cW - ((2*L1-4)*n + 12.28)); w3 = max(w3, (2*L1-4)*n - (-A_SQ*sq + 4.105)) w4 = max(w4, cR - (2*log(k) - 4.394)); w6 = max(w6, cS + log(k)); tot = cC + cT + cR + cW + cS; w5 = max(w5, tot - (-A_SQ*sq + log(k))) Y = A_C0*k + tot; s0, b, E1, x = A_frac(k, Y, Thr) if E1 - (10.7961*k - 0.62*sq + log(k)) > wE: wE = E1 - (10.7961*k - 0.62*sq + log(k)); wEk = k check(w1 < 6.73 and w2 < 0 and w3 < 0 and w4 < 0 and w6 < 0.105 and w5 < 18.84, f"Proposition A.19 at {len(kl)} values of k between 5000 and 1e12: k(C^star(k) - C_0^star) <= {w1:.3f}; the three estimates of the proof hold; " f"-log(z_k/tau_k) <= -log k + {w6:.4f}; Y - C_0^star k + c_star sqrt k - log k <= {w5:.2f}") check(0.98565 < A_SQ < 0.98566 and 0.7356*sqrt(5000) > log(5000) + 20 and 0.4928*sqrt(5000) > 0.5*log(5000) + 11 and A_SQ/2 > 0.4928 and -log(TAU_INV*A_zk(5000)) < 0.105 and A_zk(5000) < A_zk(10**4) < 0.1, f"c_star = 4 - 2 log(9.027901/2) = {A_SQ:.7f}; 0.7356 sqrt k > log k + 20 and 0.4928 sqrt k > (1/2) log k + 11 for k >= 5000; " f"-log(9.027901 z_k) <= {-log(TAU_INV*A_zk(5000)):.4f} for k >= 5000") h5 = h_k(5000); kh = 5000*h5 bds = [kh*(2/15 + 1/(1-h5) + 1/(5-h5)), (2/3)*(12*5003/4991 - 2*7.2 + 2*kh*h5 + 3*log(2)), 0.5*(-16 + 7*log(17/9)), 0.5*(28*(1+28/4980) + 8*log(5/3)), 0.4*(-35 + 15*log(7/6))] check(bds[0] < 9.62 and bds[1] < -0.18 and bds[2] < -5.77 and bds[3] < 16.13 and bds[4] < -13.07 and 9.62-0.18-5.77+16.13-13.07 < 6.7301 and 7.2 < kh < 7.2015 and all((30*x-5*x*x)/(15+12*x-2*x*x) >= 2*x-2*x*x for x in (h5, h5/2, h5/10, 1e-6)), "the five bounds in the proof of Proposition A.19: " + ", ".join(f"{x:.3f}" for x in bds)) n = 67 check(2/n + 1/n**2 - 1/(n-0.5)**2 - (2/3)/(n-0.5)**3 > 2/n - 2/n**3 and 4*(n+8+39/n)/n**2 < 0.07 and (2*n*n+18*n+78)*(7*n+39)/(n*n*(n+1)) < 17.08 and all((2*m*m+18*m+78)*(7*m+39) == 14*m*m*(m+1) + 190*m*m + 1248*m + 3042 for m in range(1, 500)) and 8*A_SQ > 7.885 and 2*L1 < 3.015 and 4.164*A_SQ < 4.105 and 4*log(3) > 4.394 and 6.73+0.07-4.394+12.21+4.105+0.105 < 18.84 and sqrt(5000) - sqrt(5000-23) < 0.164 and A_D0(5000) == 67, "the numerical constants in the proof of Proposition A.19 (n = 67)") check(wE < 24, f"Proposition A.24 (ii) at the same values of k: E - 10.7961 k + 0.62 sqrt k - log k <= {wE:.2f} (k = {wEk})") check(0.62*sqrt(5000) > log(5000) + 24 and 1.0028*2*10.7961/11 - 2*CHI0 < 0.3636 and A_SQ - 0.3636 > 0.62 and 1/(1-2/(11*66.0)) < 1 + 1.0028*2/(11*66.0) and (2/11)*(log(5000)+20.84)/(sqrt(5000)-5.17) < 0.09 and 1.0028*0.09 < 0.10 and 0.3636*6.59 < 2.40 and 18.84 + 2 + 0.10 + 2.40 < 24 and all(10.7961 < log(k) + 8.02113 for k in (5000, 5001)), "0.62 sqrt k > log k + 24 for k >= 5000, and the numerical constants in the proof of Proposition A.24 (ii); 10.7961 < log k + 8.02113 for k >= 5000") okc2 = all(D*D + 9*D + 36 + (114*D-2)/(D*(D-3)) < 2000 for D in range(4, 41)) and all((114*D-2)/(D*(D-3)) < 3 for D in range(41, 10**5)) check(okc2 and 1/(1+2*A_C0/TAU_INV) > 0.4181 and 1/(1+2*A_C0/TAU_INV) < 0.4182, "the two cases at the end of the proof of Lemma A.18; 1/(1 + 2 C_0^star/9.027901) = 0.4181...") yr = sum(A_Yreg_terms(5000)[:5]) + A_C0*5000 g5 = floor(Fr(A_C0_R[1])*5000 - Fr(9856, 10**4)*Fr(float(_dn(np.sqrt(5000.0)))) + Fr(float(_upk(np.log(5000.0), A_LOG_ULP))) + 20) check(tab[5000]['Y'] < yr and all(tab[k]['Y'] < sum(A_Yreg_terms(k)[:5]) + A_C0*k for k in range(4400, 5001, 3)) and g5 == 31369 and rec[5000]['G'] == 31323, f"for k = 5000 the first bound of Proposition A.19 is Y <= {yr:.2f}, against Y^nat = {tab[5000]['Y']:.2f} from the chains; the bound for G(5000) in " f"Theorem A.1 is {g5}, against G^flat(5000) = {rec[5000]['G']}") # ---- (i) the remarks # the remark after the proof of Theorem A.17: the exact logarithm of the step eg = {k: A_exactlog_gain(k) for k in (37, 100, 1000, 5000)}; egs = [A_exactlog_gain(k) for k in range(38, 301, 7)] SUBST = {'EXACTLOG': '$0.057$, $0.045$, $0.018$ and $0.008$ for $k=37$, $100$, $1000$ and $5000$', 'CNRANGE': '$0.0846$ and $0.0938$', 'ELLSMALL': '55$, $58$, $61$ and $64'} check(SUBST['EXACTLOG'] == ", ".join(f"${eg[k]:.3f}$" for k in (37, 100, 1000)) + f" and ${eg[5000]:.3f}$ for $k=37$, $100$, $1000$ and $5000$" and all(0 < x < eg[37] for x in egs) and eg[37] < 0.06, "with the integrand 2/(-k log(1 - theta/k)) in place of 2/theta - 1/k, Y_k(Theta_k) would be smaller by " + ", ".join(f"{eg[k]:.4f}" for k in eg) + " for k = 37, 100, 1000, 5000 (quadrature, theta_k from Definition A.11)") check(SUBST['CNRANGE'] == f"${min(csm):.4f}$ and ${max(csm):.4f}$" and SUBST['ELLSMALL'] == "$, $".join(str(rec[k]['l']) for k in (17, 18, 19)) + f"$ and ${rec[20]['l']}", "the numbers that are inserted in the text of Sections A.5 and A.7 agree with the computation") # Remark A.25: larger steps rr = (kB - A_C0_LO*K)/np.sqrt(K); rt = Th/np.sqrt(K) check(0.6377 < rr[0] < 0.6379 and bool(np.all(np.diff(rr) < 0)) and bool(np.all(np.diff(Th) > 0)) and -1.1784 < rr[-1] < -1.1783, f"Remark A.25: (k B^nat(k) - C_0^star k)/sqrt k decreases from {rr[0]:.4f} at k = 37 to {rr[-1]:.4f} at k = {KNUM}, and Theta^nat(k) increases with k") check(0.9 < rt.min() and rt.max() < 1.25 and [round(float(rt[k-37]), 2) for k in (37, 100, 1000, 5000)] == [0.93, 1.06, 1.19, 1.24], f"Remark A.25: Theta^nat(k)/sqrt k lies between {rt.min():.3f} and {rt.max():.3f}; it is 0.93, 1.06, 1.19, 1.24 for k = 37, 100, 1000, 5000") ratb = {k: (big[k]['kB'] - A_C0*k)/sqrt(k) for k in big} check([big[k]['G'] for k in (10**4, 10**5, 10**6)] == [62698, 627793, 6280769] and [f"{ratb[k]:.2f}" for k in (10**4, 10**5, 10**6)] == ["-1.23", "-1.33", "-1.36"] and all(big[k]['Th'] >= 2.25 for k in big), "Remark A.25: for k = 1e4, 1e5, 1e6 the same computation (directed rounding) gives floor(k B^nat(k)) = 62698, 627793, 6280769, and " "(k B^nat(k) - C_0^star k)/sqrt k = -1.23, -1.33, -1.36") def claimed15(k, t): n = A_D0(k) if t >= (k-9)/(8*k): return (3-6*t)/(1+3/k) if t > 1/12: return (4-14*t)/(1+7/k) if t >= (k-20)/(6*(3*k+10)): return (4-12*t)/(1+8/k) if t > 1/20: return (5-30*t)/(1+15/k) D = 4 while (D+1)*(D+2)*t <= 1: D += 1 return (D+1)*(1-D*t)/(1+D/k) if D <= n-1 else n/(1+n/k) rnd = random.Random(5); worst = 1e9; neq = 0; npt = 0; nab = 0; neqab = 0 for _ in range(150): k = int(5000*E**rnd.uniform(0, log(200))); n = A_D0(k) ts = [E**rnd.uniform(log(2*tau_k(k)), log(2*A_zk(k))) for _ in range(5)] + [1/(rnd.randint(4, n)*(rnd.randint(4, n)+1)), (k-9)/(8*k), (k-20)/(6*(3*k+10))] ts = [t for t in ts if 2*tau_k(k) <= t <= 2*A_zk(k)] lo = A_theta_direct(k, np.array(ts))[0] for t, l in zip(ts, lo): c = claimed15(k, t); worst = min(worst, (l - c)/c); npt += 1; eq = abs(l - c) < 1e-9*c; neq += eq if t > (1 + 1e-9)/(n*(n+1)): nab += 1; neqab += eq check(worst > -1e-12 and nab >= 800 and neqab == nab, f"Lemma A.18 against theta_k computed directly from Definition A.11 at {npt} points (k, t) with 5000 <= k <= 1e6: least relative margin {worst:.1e}; " f"equality (to 1e-9) at all {nab} points with t > 1/(D_star(D_star+1)), and at {neq - neqab} of the others") n5 = A_D0(5000); Th5 = n5/(1+n5/5000) Yq = 5000*A_V_quadrature(5000, cap=Th5) - 1 check(abs(Yq - yr) < 0.01 and 31363.10 < yr < 31363.11 and 31326.33 < tab[5000]['Y'] < 31326.34, f"Remark A.25: for k = 5000 and Theta as in Lemma A.18, Y_k(Theta) = {Yq:.3f} by quadrature, against {yr:.3f} for the first bound of Proposition A.19; Y^nat(5000) = {tab[5000]['Y']:.2f}") xs = np.linspace(2/TAU_INV, 1.0, 200001); f = np.sqrt(1+xs)/xs integ = float(((f[:-1]+f[1:])/2*np.diff(xs)).sum()) check(abs(-4 + sqrt(2)*integ + 1.382) < 5e-4, f"Remark A.25: -4 + sqrt(2) int_(2/9.027901)^1 sqrt(1+x) dx/x = {-4+sqrt(2)*integ:.4f}") # Remark A.26: Definition A.11 formed with the estimates of Section 4 alone (functions A_covered4, A_theta4, # A_Y4_bounds; directed rounding, Riemann sums with theta'_k from the level sets by bisection) got = {37: A_Y4_bounds(37, 4000, 4000), 100: A_Y4_bounds(100, 20000, 30000), 1000: A_Y4_bounds(1000, 40000, 160000, lower=False)} lo1000 = A_Y4_bounds(1000, 10000, 20000)[0] check(floor(got[37][0]) == floor(got[37][1]) == 251 and floor(got[100][0]) == floor(got[100][1]) == 656 and got[1000][1] < 6446 and lo1000 > 6445 and [round(got[k][2], 2) for k in got] == [3.70, 8.03, 27.07], f"Remark A.26, with the estimates of Section 4 alone: Y + 1 lies in [{got[37][0]:.3f}, {got[37][1]:.3f}], [{got[100][0]:.3f}, {got[100][1]:.3f}] and " f"[{lo1000:.3f}, {got[1000][1]:.3f}] for k = 37, 100, 1000, so that G(k) <= 251, 656, 6445; the largest steps are " + ", ".join(f"{got[k][2]:.2f}" for k in got)) # Remark A.27: the two potentials compared Gn = np.array([tab[k]['G'] for k in ks]); Ln = np.array([tab[k]['l'] for k in ks]); Yn = np.array([tab[k]['Y'] for k in ks]) dY = Yn - Yf; dG = Gn - Gf; dL = Ln - Lf cnt = [int((dG == v).sum()) for v in (1, 2, 3, 4, 5)] check(1.49 < dY.min() and dY.max() < 4.50 and ks[int(dY.argmin())] == 42 and ks[int(dY.argmax())] == 4193 and dG.min() == 1 and dG.max() == 5 and cnt == [26, 395, 2432, 2084, 27] and dL.min() == 0 and dL.max() == 3 and all(tab[k]['G'] == v for k, v in [(37, 236), (50, 316), (100, 626), (1000, 6251), (5000, 31327)]) and all(tab[k]['G'] == floor(Fr(tab[k]['kB'])) and tab[k]['l'] == floor(Fr(tab[k]['Y'])/2) + 1 for k in ks), f"Remark A.27: G^nat = 236, 316, 626, 6251, 31327 for k = 37, 50, 100, 1000, 5000; Y^nat(k) - Y^flat(k) lies between {dY.min():.4f} (k = 42) and " f"{dY.max():.4f} (k = 4193); G^nat(k) - G^flat(k) = 1, 2, 3, 4, 5 for {cnt[0]}, {cnt[1]}, {cnt[2]}, {cnt[3]}, {cnt[4]} values of k; " "l^nat(k) - l^flat(k) lies between 0 and 3") rel = [] for k in (37, 100, 1000, 3109, 5000, 10**4, 10**5, 10**6): lo, hi = A_theta_direct(k, np.array([2*tau_k(k)])); Thn = A_bound_rig(k)[1] rel.append(max(abs(lo[0] - Thn), abs(hi[0] - Thn))/Thn) check(max(rel) < 1e-12, f"Remark A.27: Theta_k = theta_k(2 tau_k) computed directly from Definition A.11 equals Theta^nat(k) for k = 37, 100, 1000, 3109, 5000, 1e4, 1e5, 1e6 (largest relative difference {max(rel):.1e})") lows = {} for k, r in ((37, 16), (100, 16), (1000, 128), (5000, 128)): lows[k] = A_Y_lower(k, rtop=2*r, rband=r, rtail=r) check(all(lows[k] >= tab[k]['G'] for k in lows) and [f"{floor(100*lows[k])/100:.2f}" for k in lows] == ["236.30", "626.22", "6251.27", "31327.07"], "Remark A.27: rigorous lower Riemann sums for Y_k(Theta_k) + 1 (theta_k from Definition A.11 by bisection): " + ", ".join(f"{floor(100*lows[k])/100:.2f}" for k in lows) + " for k = 37, 100, 1000, 5000; hence G^nat(k) = floor(Y_k(Theta_k)) + 1 for these k") return tab def main(): if '--list' in sys.argv: print_list(); return if '--list-appendix' in sys.argv: print_list_appendix(); return rng = np.random.default_rng(20261124) if '--appendix' in sys.argv: part15(rng) print("\nALL CHECKS PASSED" if OK else "\nSOME CHECK FAILED"); return Ugrid, vs, sig = part1(rng) part2(rng, Ugrid, vs, sig) part3(rng) part4(rng) part5() part6() part7() part8() part9() part10(rng) part11() part12(rng) part13() part14(rng) part15(rng) print("\nALL CHECKS PASSED" if OK else "\nSOME CHECK FAILED") if __name__ == "__main__": main()