generate.py

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

5626 bytes, as of the version from 2026-11-28 03:37 (current). Recorded here, not run.

"""T841: p-adic quadratic Dirichlet L-values at one.

Requires SageMath 10.9 with p-adics. Install the client:
    sage -pip install numberdb
Reproduce without writing:
    sage -python generate.py
Opt in to filling the authorized draft:
    sage -python generate.py --publish

The conductor sum is Fieker--Zhang (2016), Theorem 2.3, m=pD.
B_1=-1/2. Exact rational coefficients and modular Horner evaluation sum all
Bernoulli terms below J; j-2-floor(log_p(j)) >= A bounds the omitted tail.
Integer logarithms are evaluated with Sage's precision-tracked log, to A+1
absolute digits before division by m. No units or class numbers enter this
analytic algorithm. N is least with p^N >= 10^digits. A=N+32 is increased if
cancellation prevents N relative digits. Results carry O(p^(v+N)).

Default coverage is every positive fundamental discriminant 5 <= D <= 100
and every odd split prime 3 <= p <= 31, in (D,p) order. These noncanonical
arithmetic reference bounds sample small field complexity and completions.
They are not the natural domain. Credentials and agent attribution belong in
the calling environment. Work is checkpointed when NUMBERDB_WORKDIR is set.
"""
import numberdb.sage as numberdb
from sage.rings.integer_ring import ZZ
from sage.rings.rational_field import QQ
from sage.rings.padics.factory import Qp
from sage.arith.misc import bernoulli, kronecker_symbol, prime_range
import os,json,sys,hashlib
from functools import lru_cache
from pathlib import Path


def fundamental(D):
    return (D % 4 == 1 and ZZ(D).is_squarefree()) or (D % 4 == 0 and (D//4) % 4 in (2,3) and ZZ(D//4).is_squarefree())


def parameters():
    for D in range(5,101):
        if fundamental(D):
            for p in prime_range(3,32):
                if kronecker_symbol(D,p)==1:
                    yield {'D':D,'p':int(p)}


def digits_for(p,digits):
    N=0;power=1
    while power < 10**digits: N+=1;power*=p
    return N


def floor_log(j,p):
    k=0
    while j>=p: j//=p;k+=1
    return k


def cutoff(p,A):
    J=2
    while J-2-floor_log(J,p)<A: J+=1
    return J


@lru_cache(None)
def B(j): return bernoulli(j)



@lru_cache(None)
def integer_log(p,A,a):
    F=Qp(p,A+5,print_mode='val-unit');x=F(a).log()
    assert x.precision_absolute()>=A+1 and x.valuation()>=1
    return int(x.lift())%(p**(A+1))


def analytic(D,p,digits=100):
    assert fundamental(D) and p>2 and ZZ(p).is_prime() and kronecker_symbol(D,p)==1
    N=digits_for(p,digits);A=N+32
    while True:
        mod=p**A;m=p*D;J=cutoff(p,A)
        cs=[]
        for j in range(1,J):
            b=B(j)
            if not b: continue
            c=(-1)**j * QQ(m)**(j-1)*b/ZZ(j)
            assert c.denominator()%p!=0
            cs.append((j,int(c.numerator())*pow(int(c.denominator()),-1,mod)%mod))
        coeff=dict(cs);logsum=0;total=0;terms=0
        top=max(coeff)
        # All j>1 odd Bernoulli numbers vanish. Evaluate the even polynomial
        # in a^-2, then add the B_1 term explicitly.
        for a in range(1,m):
            if a%p==0: continue
            chi=int(kronecker_symbol(D,a))
            if not chi: continue
            terms+=1;inva=pow(a,-1,mod);x=inva*inva%mod;poly=0
            for j in range(top if top%2==0 else top-1,1,-2):
                poly=(poly*x+coeff.get(j,0))%mod
            val=(poly*x+coeff[1]*inva)%mod
            total=(total+chi*val)%mod
            logsum=(logsum+chi*integer_log(p,A,a))%(p**(A+1))
        assert logsum%p==0
        total=(total-(logsum//p)*pow(D,-1,mod))%mod
        v=0;part=total
        while part and part%p==0: v+=1;part//=p
        if total and A-v>=N:
            outA=v+N;residue=total%(p**outA)
            result=Qp(p,outA+2,print_mode='val-unit')(residue).add_bigoh(outA)
            assert result.precision_absolute()==outA and result.precision_relative()==N
            return result,{'D':D,'p':p,'N':N,'A':outA,'v':v,'residue':str(residue),'working_A':A,'first_omitted_J':J,'tail_bound':J-2-floor_log(J,p),'nonzero_residue_terms':terms}
        A+=32


class QuadraticPadicL(numberdb.Generator):
    table=os.environ.get('NUMBERDB_TABLE','T841')
    parameters=('D','p')
    type='Qp'
    digits=100
    rigour='proven'

    def enumerate(self): return parameters()

    def value(self,params,digits):
        D=int(params['D']);p=int(params['p'])
        work=os.environ.get('NUMBERDB_WORKDIR')
        fingerprint=hashlib.sha256(Path(__file__).read_bytes()).hexdigest()
        cache=Path(work)/f'analytic-{D}-{p}-{digits}.json' if work else None
        if cache and cache.exists():
            data=json.loads(cache.read_text())
            if data.get('generator_sha256')==fingerprint:
                return Qp(p,data['A']+2,print_mode='val-unit')(ZZ(data['residue'])).add_bigoh(data['A'])
        result,data=analytic(D,p,digits)
        if cache:
            data['generator_sha256']=fingerprint
            temp=cache.with_suffix('.tmp');temp.write_text(json.dumps(data,indent=2));temp.replace(cache)
        return result


if __name__=='__main__':
    if os.environ.get('NUMBERDB_KEY_FROM_STDIN')=='1': numberdb.configure(api_key=sys.stdin.read().strip())
    generator=QuadraticPadicL()
    if '--publish' in sys.argv or os.environ.get('NUMBERDB_PUBLISH')=='1':
        print(generator.publish(message=os.environ.get('NUMBERDB_EDIT_MESSAGE','Fill analytic conductor sum')),flush=True)
    elif generator.table!='TBD': print(generator.verify(sample=None),flush=True)
    else:
        for par in generator.enumerate():
            value=generator.value(par,100)
            print(par,'v',value.valuation(),'relative',value.precision_relative(),flush=True)