"""Check the rational witness in Appendix A using only the standard library.""" from dataclasses import dataclass from fractions import Fraction as Q @dataclass(frozen=True) class Bounds: lo: Q hi: Q @staticmethod def of(value): if isinstance(value, Bounds): return value return Bounds(Q(value), Q(value)) def __add__(self, other): other = self.of(other) return Bounds(self.lo + other.lo, self.hi + other.hi) __radd__ = __add__ def __neg__(self): return Bounds(-self.hi, -self.lo) def __sub__(self, other): return self + (-self.of(other)) def __rsub__(self, other): return self.of(other) + (-self) def __mul__(self, other): other = self.of(other) products = [a * b for a in (self.lo, self.hi) for b in (other.lo, other.hi)] return Bounds(min(products), max(products)) __rmul__ = __mul__ def __truediv__(self, other): other = self.of(other) if other.lo <= 0 <= other.hi: raise ZeroDivisionError("denominator interval contains zero") return self * Bounds(1 / other.hi, 1 / other.lo) def __rtruediv__(self, other): return self.of(other) / self def log_unit(y): """Enclose log(y) for 1 <= y <= 2 by a partial sum and a tail bound.""" z = (y - 1) / (y + 1) terms = 16 lower = 2 * sum(z**(2*j+1) / (2*j+1) for j in range(terms)) tail = 2 * z**(2*terms+1) / ((2*terms+1) * (1-z*z)) return Bounds(lower, lower + tail) LOG2 = log_unit(Q(2)) LOG4 = 2 * LOG2 def log(y): y = Q(y) if y <= 0: raise ValueError("logarithm argument must be positive") power = 0 while y < 1: y *= 2 power -= 1 while y > 2: y /= 2 power += 1 return log_unit(y) + power * LOG2 def entropy(y): return -y * log(y) - (1-y) * log(1-y) def omega(*shape): a, b, c = sorted(shape, reverse=True) if c <= a*b/(a+b): return a+b return (a*b+a*c+b*c)**2/(4*a*b*c) def require_greater(values, threshold): for name, value in values.items(): if Bounds.of(value).lo <= threshold: raise ArithmeticError(f"Could not certify {name} > {threshold}.") def verify(): e, c, v, r, u, zeta, w = map( Q, "0.084796 10.57065 0.077072 0.0155776 0.021436 1/5 0.110788".split() ) a_l, k_l, a_h, k_h = map(Q, "11/10 11 0.95014 13.11475".split()) x, s = e/2, 1-r eta = 1-x half = Q(1, 2) C0 = c * entropy(1/c) / LOG4 Ts = c-1+C0 T = u+s*Ts psi = entropy(v) / LOG2 def gamma(a): return entropy((half-a)/(c-1)) / LOG4 G = ((c-1+v)*log(c-1+v) - (c-1)*log(c-1) + (1-v)*log(1-v) - v*LOG4) / LOG2 z = s*(c-1+v+w)*entropy(w/(c-1+v+w)) / LOG2 qo = s*(psi+2*v-(v+w)*entropy(w/(v+w))/LOG2) # The limiting costs are evaluated at zero exponent loss. Wl, Wh, Wbase = omega(a_l, 1, k_l), omega(a_h, 1, k_h), omega(u, r, u) alpha_l, beta_l = Wl-k_l-1, Wl-k_l-a_l alpha_h, beta_h = Wh-k_h-1, Wh-k_h-a_h A0 = r+s*(2*C0+(2*c-1)*log(3)/LOG4+psi-Ts) A1 = r+s*(Ts+psi+zeta*(1+alpha_l)-zeta/(2*gamma(v))) A2 = r+s*(Ts-G) Vp, Vc = s*G-(Wbase-2*u), s*G-z*alpha_h ql, qh = r+s*(psi+zeta*beta_l), qo+z*beta_h gamma_prime = -log((c-1)/(half-v)-1)/((c-1)*LOG4) require_greater({ "parameter domain": min(c-5, v, half-2*v, r, 1-r, u-r, zeta, w, 1-v-w, a_l, a_h, k_l-1, k_h-1, x, Q(1,10)-x), "band fit": 1-e*T, "low columns": eta+e*r-e*s*zeta*k_l, "high columns": eta-e*z*k_h, "window capacity": (c-1)*gamma(2*v)-zeta, "window derivative": 2*log((1-v)/v)/LOG4 + zeta*gamma_prime/(2*gamma(v)*gamma(v)), }, 0) require_greater({ "root encoder": 1-e*A0, "residual encoder": 1-e*A1, "high encoder": 1-e*A2, "high base products": e*Vp, "high group startup": e*Vc, "low reads": x-e*ql, "high reads": x-e*qh, "dense job construction": 1-e, "common sample construction": Q(1,10)-x, "incumbent formation": 1-x, "measurement decoding": x, }, Q("0.008373")) if __name__ == "__main__": verify() print("Verified dimension conditions and strict savings for O(n^2.991627).")