"""Look-and-say growth rates (T894).

Install SageMath, then run:
    sage -pip install numberdb
    sage -python generate.py             # preview, without writing
    sage -python generate.py --publish   # fill the assigned private draft

The intrinsic three-regime family is enumerated in binary, ternary, Conway
order. Exact integer polynomials and exact rational isolating brackets select
the roots in AA; conversion into a real interval at digits*log2(10)+96 bits
rigorously encloses each algebraic number. Decimal serialization keeps 100
significant digits with last-place uncertainty, never an exact decimal.

The degree-71 polynomial is transcribed from Ekhad and Zeilberger's PTlam
output, https://sites.math.rutgers.edu/~zeilberg/tokhniot/oPTlam. checks.py
independently reconstructs its 92-atom matrix and characteristic polynomial.
The binary and ternary cubics and three radix regimes are stated in Dresden
and Siehler, arXiv:2405.11103v1, introduction and Growth Theorem.
No credentials or agent attribution are embedded here.
"""
import os
import numberdb.sage as numberdb
from sage.rings.integer_ring import ZZ
from sage.rings.rational_field import QQ
from sage.rings.polynomial.polynomial_ring_constructor import PolynomialRing
from sage.rings.real_mpfi import RealIntervalField
from sage.rings.qqbar import AA

R=PolynomialRing(ZZ,'x')
x=R.gen()
CONWAY_COEFFICIENTS={71:1,69:-1,68:-2,67:-1,66:2,65:2,64:1,63:-1,62:-1,61:-1,60:-1,59:-1,58:2,57:5,56:3,55:-2,54:-10,53:-3,52:-2,51:6,50:6,49:1,48:9,47:-3,46:-7,45:-8,44:-8,43:10,42:6,41:8,40:-5,39:-12,38:7,37:-7,36:7,35:1,34:-3,33:10,32:1,31:-6,30:-2,29:-10,28:-3,27:2,26:9,25:-3,24:14,23:-8,21:-7,20:9,19:3,18:-4,17:-10,16:-7,15:12,14:7,13:2,12:-12,11:-4,10:-2,9:5,7:1,6:-7,5:7,4:-4,3:12,2:-6,1:3,0:-6}
POLYNOMIALS={'binary':x**3-x**2-1,'ternary':x**3-x-1,
             'conway':sum(ZZ(c)*x**k for k,c in CONWAY_COEFFICIENTS.items())}
BRACKETS={'binary':(QQ(146)/100,QQ(147)/100),
          'ternary':(QQ(132)/100,QQ(133)/100),
          'conway':(QQ(130)/100,QQ(131)/100)}
_ROOTS={}
def root(regime):
    if regime not in _ROOTS:
        p=POLYNOMIALS[regime];lo,hi=BRACKETS[regime]
        candidates=[a for a in p.roots(AA,multiplicities=False) if lo<a<hi]
        assert len(candidates)==1
        _ROOTS[regime]=candidates[0]
    return _ROOTS[regime]

class LookAndSayGrowth(numberdb.Generator):
    table=os.environ.get('NUMBERDB_TABLE','T894')
    parameters=('regime',)
    type='R'
    digits=100
    rigour='proven'
    def enumerate(self):
        for regime in ('binary','ternary','conway'):
            yield {'regime':regime}
    def value(self,params,digits):
        return RealIntervalField(numberdb.bits(digits,losing=96))(root(params['regime']))

if __name__=='__main__':
    import argparse
    parser=argparse.ArgumentParser(description=__doc__)
    parser.add_argument('--publish',action='store_true')
    options=parser.parse_args()
    generator=LookAndSayGrowth()
    if options.publish:
        generator.publish()
    else:
        generator.preview()
