generate.py

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

3905 bytes, as of the version from 2026-10-24 22:26 (current). Recorded here, not run.

"""Expected convex hull volume ratios -- numberdb.org/T470.

Run with SageMath or passagemath:
    sage -pip install numberdb
    sage -python generate.py             # verify all stored entries
    sage -python checks.py               # independent geometric checks
    sage -python generate.py --fill      # fill the draft using NUMBERDB_API_KEY

The body argument represents an affine class. The triangle and parallelogram
formulas are those of Buchta (1984), displayed on slide 5 of his Rouen 2012
lecture, combined with Efron's identity. Their values are exact rationals.
The disk uses Efron's edge-count integral, evaluated with Arb's certified
complex integration. All integrands are entire functions, including at the
endpoints. Uniform sampling is from the interior, by normalized area.

The complete rectangle 3 <= n <= 20 covers small finite-sample hulls from
triangles through twenty-point clouds for the three classical planar classes.
80 guard bits give a measured relative radius below 1e-120 throughout.
No Monte Carlo or unbounded quadrature result supplies stored digits.
"""
import os
import sys
import numberdb.sage as numberdb
from sage.rings.rational_field import QQ
from sage.rings.complex_arb import ComplexBallField

GUARD_BITS = 80
MAX_N = 20
REGIONS = ('equilateral-triangle', 'unit-square', 'unit-disk')


def expected_ratio(region, n, digits=100):
    n = int(n)
    if n < 3:
        raise ValueError('This planar table requires n >= 3.')
    if region == 'equilateral-triangle':
        return 1 - QQ(2) / (n + 1) * sum(QQ(1) / k for k in range(1, n + 1))
    if region == 'unit-square':
        s = sum((1 - QQ(1) / 2**k) / k for k in range(1, n + 2))
        return 1 - QQ(8) / (3 * (n + 1)) * (s - QQ(1) / ((n + 1) * 2**(n + 1)))
    if region != 'unit-disk':
        raise ValueError('Unknown affine class: ' + region)
    C = ComplexBallField(numberdb.bits(digits, losing=GUARD_BITS))
    pi = C.pi()

    def integrand(u, analytic):
        sine = (pi * u).sin()
        cosine = (pi * u).cos()
        cap_fraction = u - sine * cosine / pi
        return sine**4 * cap_fraction**(n - 1)

    result = 1 - C(8 * n) / 3 * C.integral(integrand, 0, 1)
    assert (result.real().is_finite() and result.imag().is_finite()) and result.imag().contains_zero()
    value = result.real()
    assert value > 0 and value < 1
    assert value.parent()(value.rad()) < abs(value) * value.parent()(10)**(-digits - 10)
    return value


class ExpectedHullVolume(numberdb.Generator):
    table = 'T470'
    parameters = ('region', 'n')
    type = 'R'
    digits = 100
    rigour = 'proven'
    files = ('generate.py', 'checks.py', 'check.py', 'derivation.txt', 'validation.json')

    def enumerate(self):
        for region in REGIONS:
            for n in range(3, MAX_N + 1):
                yield {'region': region, 'n': n}

    def value(self, params, digits):
        region, n = params['region'], int(params['n'])
        value = expected_ratio(region, n, digits)
        comments = {
            3: r'The mean area of a random triangle, divided by the ellipse area, is $35/(48\pi^2)$.',
            4: r'The exact ratio is $35/(24\pi^2)$.',
            5: r'The exact ratio is $175/(72\pi^2)-23023/(6912\pi^4)$ CITE{Marckert}.',
        }
        if region == 'unit-disk' and n in comments:
            return {'number': value, 'comment': comments[n]}
        return value


if __name__ == '__main__':
    if os.environ.get('NUMBERDB_KEY_FROM_STDIN') == '1':
        os.environ['NUMBERDB_API_KEY'] = sys.stdin.read().strip()
    generator = ExpectedHullVolume()
    if '--fill' in sys.argv or os.environ.get('NUMBERDB_PUBLISH') == '1':
        print(generator.publish(message='Fill 54 certified hull-volume ratios and attach independent geometric checks.'))
    else:
        report = generator.verify(sample=None)
        print(report)
        sys.exit(0 if report.ok else 1)