generate.py

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

4090 bytes, as of the version from 2026-11-15 23:03 (current). Recorded here, not run.

"""Li coefficients for quadratic Dirichlet L-functions; table T718.

Install numberdb and python-flint in the Sage Python environment:
    sage -pip install numberdb python-flint
    sage -python generate.py              # verify all stored entries
    sage -python generate.py --publish    # additive submission to this draft

D is a nontrivial fundamental discriminant, not a squarefree radicand.
The completion is (|D|/pi)^(s/2) Gamma((s+a)/2) L(s,chi_D),
a=0 for D>0 and a=1 for D<0. No pole-removing factor is used.
Lambda_D(1+x) is evaluated through order 20 from the finite Hurwitz-zeta
identity, with the common pole removed from each summand before summing.
FLINT's zeta, log-Gamma and power-series operations enclose all rounding and
Euler-Maclaurin remainders. At least 512 bits are used, with 180 guard bits
beyond the requested decimal precision. The log jet is converted using
lambda_n = n sum_{k=1}^n binomial(n-1,k-1) [x^k] log Lambda_D(1+x).
Only positive-order log coefficients matter, so the logarithm's additive
constant and any nonzero constant multiplier of Lambda have no effect.
The attached theta_check.py implements a separate theta-Mellin check.
"""
import os
import sys
from functools import lru_cache
from math import comb
import numberdb.sage as numberdb
from sage.rings.integer_ring import ZZ
from sage.rings.real_arb import RealBallField
from flint import acb, arb, acb_series, ctx

MAX_CONDUCTOR = 40
MAX_ORDER = 20

def discriminants(bound=MAX_CONDUCTOR):
    answer = []
    for q in range(2, bound + 1):
        for D in (q, -q):
            d = ZZ(D)
            if (D % 4 == 1 and d.is_squarefree()) or (
                    D % 4 == 0 and (D // 4) % 4 in (2, 3)
                    and ZZ(D // 4).is_squarefree()):
                answer.append(D)
    return answer


def sage_ball(x, bits):
    """Copy exact binary midpoint and outward radius, without decimal loss."""
    R = RealBallField(bits)
    m, e = x.mid().man_exp()
    r, f = x.rad().man_exp()
    return R(ZZ(int(m)) * ZZ(2)**int(e),
             ZZ(int(r)) * ZZ(2)**int(f))


@lru_cache(maxsize=None)
def li_balls(D, digits=100):
    D = int(D)
    assert D != 1 and D in discriminants(abs(D))
    bits = max(512, numberdb.bits(digits, losing=180))
    with ctx.workprec(bits):
        old_cap = ctx.cap
        ctx.cap = MAX_ORDER + 1
        try:
            q = abs(D)
            a = int(D < 0)
            s = acb_series([1, 1], prec=MAX_ORDER + 1)
            L = acb_series([], prec=MAX_ORDER + 1)
            weights = [int(ZZ(D).kronecker(r)) for r in range(1, q + 1)]
            assert sum(weights) == 0
            for r, chi in enumerate(weights, 1):
                if chi:
                    L += chi * s.zeta(acb(r) / q, deflate=True)
            L *= (-s * arb(q).log()).exp()
            log_completed = L.log() + ((s + a) / 2).lgamma() + s * (arb(q) / arb.pi()).log() / 2
            values = []
            for n in range(1, MAX_ORDER + 1):
                b = n * sum((comb(n - 1, k - 1) * log_completed[k]
                              for k in range(1, n + 1)), acb(0))
                assert b.is_finite() and b.imag.contains(0)
                assert b.real.rel_accuracy_bits() >= 380, (D, n, b)
                values.append(sage_ball(b.real, bits))
            return tuple(values)
        finally:
            ctx.cap = old_cap


class QuadraticLi(numberdb.Generator):
    table = os.environ.get('NUMBERDB_TABLE', 'T718')
    parameters = ('D', 'n')
    type = 'R'
    digits = 100
    rigour = 'proven'

    def enumerate(self):
        for D in discriminants():
            for n in range(1, MAX_ORDER + 1):
                yield {'D': D, 'n': n}

    def value(self, params, digits):
        D, n = int(params['D']), int(params['n'])
        if not 1 <= n <= MAX_ORDER:
            raise ValueError('This generator implements orders 1 through 20')
        return li_balls(D, digits)[n - 1]


if __name__ == '__main__':
    g = QuadraticLi()
    if '--publish' in sys.argv:
        g.publish(removing=False)
    else:
        print(g.verify(sample=None))