generate.py

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

6289 bytes, as of the version from 2026-11-26 14:09 (current). Recorded here, not run.

"""T820: Colored Jones polynomials of twist knots.

Install SageMath 10.9 (or a compatible Sage), then install the client:
    sage -pip install numberdb
Run with:
    sage -python generate.py                 # offline controls and 60 entries
    sage -python generate.py --verify        # read-only, all stored entries
    sage -python generate.py --publish       # explicitly fill the target
For a private draft, configure NUMBERDB_API_KEY for verification or filling.
NUMBERDB_KEY_FROM_STDIN=1 reads that key from standard input instead.
NUMBERDB_PUBLISH=1 is equivalent to --publish for the campaign runner.
Engine/model/run provenance is supplied by the invoking session, never here.

Exact division-free multisums from Nawata, Ramadevi and Zodinmawia,
arXiv:1209.1409v2, section 2.1, positive/negative-twist formulas following (24).
They are the colored-Jones formulas, not the conjectural superpolynomials.
Masbaum's Figure 3 fixes K_p, section 5 fixes reduction and zero framing,
and Remark 5.2 fixes q=A^4. N is representation dimension. Reflection is
q -> q^(-1); changing p's sign is not reflection. The searchable polynomial
is q^(-m)J with m=min exponent, retained in each comment, without sign changes.

All operations are exact in ZZ[q,q^-1]. Gaussian binomials use their polynomial
recurrence, avoiding division. There is no numerical precision or rounding.
The default selection has 1<=|p|<=4, source and distinct mirror, N=2..5;
the amphichiral p=-1 has source only. Omitted p=0 and N=1 are controls.
The bounds on p and N may be extended explicitly with enumerate(p_limit=...,
color_max=...), subject to polynomial size/readability reassessment.
"""
import argparse
from functools import lru_cache
import json
import os
import sys

import numberdb.sage as numberdb
from sage.rings.integer_ring import ZZ
from sage.rings.polynomial.polynomial_ring_constructor import PolynomialRing
from sage.rings.polynomial.laurent_polynomial_ring import LaurentPolynomialRing

POLYNOMIALS = PolynomialRing(ZZ, 'q')
LAURENT = LaurentPolynomialRing(ZZ, 'q')
q = LAURENT.gen()
x = POLYNOMIALS.gen()


@lru_cache(maxsize=None)
def gaussian(n, k):
    """[n choose k]_q as an exact ordinary polynomial."""
    if k < 0 or k > n:
        return POLYNOMIALS.zero()
    if k == 0 or k == n:
        return POLYNOMIALS.one()
    return gaussian(n - 1, k - 1) + x**k * gaussian(n - 1, k)


@lru_cache(maxsize=None)
def twisting(depth, k, positive):
    """Nested chain with outermost index k and depth=|p|."""
    if depth == 1:
        return LAURENT.one()
    total = LAURENT.zero()
    for s in range(k + 1):
        exponent = s * (s + 1) if positive else -s * (k + 1)
        total += q**exponent * LAURENT(gaussian(k, s)) * twisting(depth - 1, s, positive)
    return total


@lru_cache(maxsize=None)
def jones_source(p, N):
    """Unshifted Laurent polynomial; accepts p=0 and N=1 for controls."""
    p, N = int(p), int(N)
    if N < 1:
        raise ValueError('N must be a positive representation dimension')
    if p == 0:
        return LAURENT.one()
    product = LAURENT.one()
    result = LAURENT.zero()
    for k in range(N):
        if p > 0:
            outer = q**k
        else:
            outer = ZZ(-1)**k * q**(-(k * (k + 1) // 2))
        result += outer * product * twisting(abs(p), k, p > 0)
        product *= (1 - q**(1 - N + k)) * (1 - q**(1 + N + k))
    return result


def jones(p, hand, N):
    if hand not in ('source', 'mirror'):
        raise ValueError('hand must be source or mirror')
    value = jones_source(int(p), int(N))
    return value if hand == 'source' else value(q**(-1))


def shifted(p, hand, N):
    value = jones(p, hand, N)
    m = int(value.valuation())
    polynomial = POLYNOMIALS(value * q**(-m))
    assert polynomial[0] != 0
    return polynomial, m


class ColoredJones(numberdb.Generator):
    table = 'T820'
    parameters = ('p', 'hand', 'N')
    type = 'Z[]'
    digits = 100  # no rounding or digit truncation for exact polynomials
    rigour = 'exact'
    files = ('generate.py',)

    def enumerate(self, p_limit=4, color_max=5):
        for d in range(1, int(p_limit) + 1):
            for p in (d, -d):
                for hand in (('source',) if p == -1 else ('source', 'mirror')):
                    for N in range(2, int(color_max) + 1):
                        yield {'p': p, 'hand': hand, 'N': N}

    def value(self, params, digits):
        p, N, hand = int(params['p']), int(params['N']), str(params['hand'])
        if p == 0 or N < 2:
            raise ValueError('stored domain requires p nonzero and N>=2')
        polynomial, m = shifted(p, hand, N)
        return {'number': polynomial, 'comment': '$m=%d$' % m}


def offline_controls():
    assert jones_source(1, 2) == q + q**3 - q**4
    assert jones_source(-1, 2) == q**(-2) - q**(-1) + 1 - q + q**2
    for N in range(1, 6):
        assert jones_source(0, N) == 1
    for p in (1, -1, 2, -2, 3, -3, 4, -4):
        assert jones_source(p, 1) == 1
    rows = []
    for params in ColoredJones().enumerate():
        polynomial, m = shifted(**params)
        assert all(type(c).__module__.startswith('sage.rings.integer') for c in polynomial.list())
        assert polynomial(1) == 1
        rows.append({'params': params, 'm': m, 'polynomial': str(polynomial),
                     'laurent': str(jones(**params)), 'degree': int(polynomial.degree()),
                     'coefficients': [int(c) for c in polynomial.list()]})
    return rows


if __name__ == '__main__':
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument('--publish', action='store_true')
    parser.add_argument('--verify', action='store_true')
    args = parser.parse_args()
    if os.environ.get('NUMBERDB_KEY_FROM_STDIN') == '1':
        key = sys.stdin.read().strip()
        if '=' in key and key.split('=', 1)[0].isupper():
            key = key.split('=', 1)[1].strip().strip('\"\'')
        numberdb.configure(api_key=key)
    generator = ColoredJones()
    if args.publish or os.environ.get('NUMBERDB_PUBLISH') == '1':
        print(generator.publish(message=os.environ.get('NUMBERDB_EDIT_MESSAGE', '')))
    elif args.verify:
        print(generator.verify(sample=None))
    else:
        print(json.dumps({'rows': offline_controls()}, indent=2))