import mpmath as mp
from fractions import Fraction
mp.mp.dps = 130
def mean_crossing_clusters(r):
r = Fraction(r) # exact width/height ratio
if r <= 0:
raise ValueError("The aspect ratio must be positive")
s = max(r, 1/r)
ss = mp.mpf(s.numerator)/s.denominator
q = mp.exp(-mp.pi*ss)
y = (mp.jtheta(2, 0, q)/mp.jtheta(3, 0, q))**4
a = mp.mpf(1)/3
d = mp.sqrt(3)/(2*mp.pi)*y*mp.hyper([1, 1, 4*a], [2, 5*a], y)
k = mp.sqrt(3)/(4*mp.pi)
if r < 1:
return mp.mpf(1)/2 - d/2 - k*mp.log(y)
c = 3*mp.gamma(2*a)/mp.gamma(a)**2*y**a*mp.hyp2f1(a, 2*a, 4*a, y)
return c - d/2 - k*mp.log1p(-y)
mean_crossing_clusters(Fraction(2, 1)) # numerical example, not an enclosure
# Stored default: all 43 reduced positive a/b with max(a,b) <= 8.
# Square first, then s,1/s in increasing s>1; nonsquares are exploratory.
Sage 10.9 with FLINT/Arb ball arithmetic encloses exact rational aspect ratios, modular lambda, gamma factors and ordinary hypergeometric evaluations [4]. Set $s=\max(r,1/r)$ and $y=\lambda(i s)\leq1/2$. The computation uses the first formula at $y$ for $r\geq1$, and Maier's direct formula with $1-x=y$ for $r<1$, propagating input and arithmetic uncertainty. The square has exactly $x=C(1)=1/2$, but its mean remains an inexact enclosure. Working at 461 bits supports 100 significant decimal digits; all 43 outputs are finite, have relative radius below $10^{-115}$, and exceed the corresponding horizontal crossing probability. The minimum measured relative accuracy is 448 bits, with at most one cancellation bit.
An alternate mpmath implementation checks all 43 decimal intervals using theta constants at the original ratio and Maier's direct formula, with its correction evaluated by the Kleban–Zagier double integral after cubic substitutions at both endpoints. It also checks the elliptic aspect relation, the separate square closed form and $\lambda(i\cdot2)=17-12\sqrt2$, and rejects shifted values, a two-color factor, reversed orientation and nonfinite input. This numerical quadrature comparison is independent implementation evidence, not a rigorous error certificate. A second complete check uses explicit theta and positive hypergeometric series with geometric tail bounds at 512 bits. For $r\geq1$, it evaluates the positive residual series for $N(r)-C(r)$; for $r<1$, it uses the direct complementary formula. Every alternate ball overlaps the production ball and lies within the recorded decimal interval, with at least 500 bits of relative accuracy. Narrowness and finiteness controls reject vacuous agreement. NumberDB decimals denote uncertainty of one unit in the last place.