generate.py

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

5518 bytes, as of the version from 2026-12-02 03:41 (current). Recorded here, not run.

"""Reproduce NumberDB T856: Watts's horizontal-vertical crossing probability.

Install numberdb in SageMath, then run:
    sage -pip install numberdb
    sage -python generate.py             # read-only verification of all entries
    sage -python generate.py --offline   # print the default selection locally
    sage -python generate.py --publish   # explicitly fill the existing target

For a private draft configure your own NUMBERDB_API_KEY. Session attribution
belongs in the calling environment. publish() writes entries additively with
removing=False; it neither offers nor publishes a private draft.

r is the exact width/height ratio of a free-boundary rectangle for the scaling
limit of critical triangular-lattice site percolation, one open color, p=1/2.
W(r) is the probability of an open cluster touching all four sides. Rotation
identifies r and 1/r, so this table and generator use representatives r>=1.
The natural parameter is real; this implementation accepts exact rationals.
The default reference selection is every reduced a/b with 1<=b<=a<=8, sorted
in increasing order (22 ratios). The square is intrinsic; the other ratios
are exploratory reference choices with a noncanonical complexity bound.

For x=lambda(i*r),
 C=Gamma(2/3)/(Gamma(4/3)*Gamma(1/3))*x^(1/3)*2F1(1/3,2/3;4/3;x),
 W=C-sqrt(3)/(2*pi)*x*3F2(1,1,4/3;2,5/3;x).
All powers are positive real. K in r=K(1-x)/K(x) uses elliptic parameter x.
The theta nome is exp(-pi*r). Source: Sheffield--Wilson (2011), section 1
and Theorem 7.1, arXiv:1003.3271, DOI 10.1214/11-AOP652; Kleban--Zagier
(2003), sections 2--3, equations (2)--(9),(12)--(13),
DOI 10.1023/A:1026012600583. Maier arXiv:math-ph/0210013,
Corollary 4.2.1 gives the independent square closed form.

Sage/FLINT ball arithmetic encloses modular lambda, gamma and the ordinary
hypergeometric functions at numberdb.bits(digits, losing=128) bits (461 bits
for 100 decimal digits). Rational inputs are enclosed, never copied decimals.
On r>=1 the series arguments satisfy 0<x<=1/2; no cut is crossed. For r=1,
x=1/2 and C=1/2 are mathematical identities. The final W is inexact, including
the square. Extracting real components is justified by real positive series;
finite components and imaginary zero containment are checked too. The result
must have radius less than abs(W)*10^(-digits-15), with probability/event
bounds checked before returning. Offline evidence measures actual cancellation,
retained accuracy, and independent integral and explicitly bounded series checks.
"""
import argparse
from math import gcd

import numberdb.sage as numberdb
from sage.rings.rational_field import QQ
from sage.rings.real_arb import RealBallField
from sage.rings.complex_arb import ComplexBallField

GUARD_BITS = 128


def real_enclosure(value):
    real, imag = value.real(), value.imag()
    if not real.is_finite() or not imag.is_finite() or not imag.contains_zero():
        raise ArithmeticError('Expected a finite enclosure of a real value')
    return real


def watts_components(r, digits=100):
    r = QQ(r)
    if r < 1:
        raise ValueError('Use the rotation representative r>=1')
    precision = numberdb.bits(digits, losing=GUARD_BITS)
    R = RealBallField(precision)
    C = ComplexBallField(precision)
    a, b, c = QQ(1)/QQ(3), QQ(2)/QQ(3), QQ(4)/QQ(3)
    x = R(QQ(1)/QQ(2)) if r == 1 else real_enclosure(C(0, R(r)).modular_lambda())
    if not (x > 0 and x < 1):
        raise ArithmeticError('Cross-ratio must be inside (0,1)')
    h2 = real_enclosure(C(x).hypergeometric([a, b], [c]))
    cardy = R(QQ(1)/QQ(2)) if r == 1 else R(b).gamma()/(R(c).gamma()*R(a).gamma())*x**a*h2
    h3 = real_enclosure(C(x).hypergeometric([1, 1, c], [2, QQ(5)/QQ(3)]))
    difference = R(3).sqrt()/(2*R.pi())*x*h3
    result = cardy - difference
    if not all(v.is_finite() for v in (x, cardy, difference, result)):
        raise ArithmeticError('Nonfinite value')
    if not (result > 0 and result < cardy and cardy < 1 and difference > 0):
        raise ArithmeticError('Probability/event bounds failed')
    if not R(result.rad().exact_rational()) < abs(result)*R(10)**(-digits-15):
        raise ArithmeticError('Insufficient relative ball accuracy')
    return result, x, cardy, difference


class WattsCrossing(numberdb.Generator):
    table = 'T856'
    parameters = ('r',)
    type = 'R'
    digits = 100
    rigour = 'proven'

    def enumerate(self, height=8):
        if type(height) is not int or height < 1:
            raise ValueError('height must be a positive integer')
        ratios = sorted(QQ(a)/QQ(b) for a in range(1, height+1)
                        for b in range(1, a+1) if gcd(a, b) == 1)
        for r in ratios:
            yield {'r': str(r)}

    def value(self, params, digits):
        return watts_components(params['r'], digits)[0]


def main():
    p = argparse.ArgumentParser(description=__doc__)
    flags = p.add_mutually_exclusive_group()
    flags.add_argument('--publish', action='store_true')
    flags.add_argument('--offline', action='store_true')
    p.add_argument('--height', type=int, default=8)
    options = p.parse_args()
    g = WattsCrossing()
    if options.publish:
        print(g.publish(height=options.height, removing=False))
    elif options.offline:
        from numberdb._write import to_text
        for params in g.enumerate(height=options.height):
            print(params['r'], to_text(g.value(params, g.digits), g.digits))
    else:
        print(g.verify(sample=None))


if __name__ == '__main__':
    main()