"""Reproduce the Weierstrass constant for T910.

Install numberdb in Sage, then run:
    sage -pip install numberdb
    sage -python generate.py             # verify the stored singleton
    sage -python generate.py --publish   # authorized entries upload only

The lattice has full periods 1,i. Waldschmidt, Transcendence of Periods
(2011), PDF page 36, gives sigma(1/2)=2^(5/4)*sqrt(pi)*exp(pi/8)/Gamma(1/4)^2.
All fractional exponents and arguments are exact rationals. Arb real balls
propagate rounding error from pi, Gamma, exp and the positive real power.
At 100 digits, 512 working bits gave radius below 3e-153. The builder also
compared the result with FLINT's direct canonical sigma evaluation.
Credentials and upload provenance belong to the invoking environment.
"""
import argparse
import numberdb.sage as numberdb
from sage.rings.rational_field import QQ
from sage.rings.real_arb import RealBallField


class WeierstrassConstant(numberdb.Generator):
    table = 'T910'
    parameters = ()
    type = 'R'
    digits = 100
    rigour = 'proven'

    def enumerate(self):
        yield {}

    def value(self, params, digits):
        if params:
            raise ValueError('The Weierstrass constant has no parameters.')
        R = RealBallField(max(512, numberdb.bits(digits, losing=128)))
        pi = R.pi()
        result = R(2)**(QQ(5)/4) * pi.sqrt() * (pi/8).exp() / R(QQ(1)/4).gamma()**2
        if not result.is_finite() or not result > 0:
            raise ArithmeticError('Expected a finite positive real ball.')
        return result


if __name__ == '__main__':
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument('--publish', action='store_true')
    args = parser.parse_args()
    generator = WeierstrassConstant()
    print(generator.publish() if args.publish else generator.verify(sample=None))
