"""Independent numerical Picard-Fuchs route for T731, using mpmath only.

No elliptic integrals or algebraic pullback are used here. Initial F,L are
summed from Hartmann's factorial/harmonic Frobenius series. The scalar ODE is
 z^2(1-z)y''' + (3z-9z^2/2)y'' + (1-51z/16)y' - 3y/32 = 0.
Taylor continuation follows the upper path 1/16 -> 1/2 -> (1+i)/2 ->
(3+i)/2 -> 2, then the positive real axis. Each step is at most one sixth
of the distance to {0,1}; truncation uses 1.8*dps+40 coefficients. This is a
high-precision numerical cross-check, not a rigorous tail certificate.
Repeated at 145 and 180 decimal digits; all inexact selected rows are checked.
The shared assumptions are the published PF germ/branch and rational inputs;
mpmath uses a different arithmetic backend from Arb.
"""
import mpmath as mp
from fractions import Fraction


def germ(z):
    # Derivatives through order 2; each coefficient update is independently
    # derived from (4n)!/(256^n n!^4), not taken from the elliptic generator.
    sums=[mp.mpc(0) for _ in range(6)]
    a=mp.mpf(1); h=mp.mpf(0); hn=mp.mpf(0)
    for n in range(int(mp.mp.dps*2.6)+80):
        if n:
            for k in range(4*n-3,4*n+1):h+=mp.mpf(1)/k
            hn+=mp.mpf(1)/n
        b=4*a*(h-hn)
        for j in range(3):
            if n>=j:
                fac=1 if j==0 else n if j==1 else n*(n-1)
                sums[j]+=fac*a*z**(n-j)
                sums[3+j]+=fac*b*z**(n-j)
        a*=mp.mpf((4*n+1)*(4*n+2)*(4*n+3)*(4*n+4))/(256*(n+1)**4)
    f0,f1,f2,g0,g1,g2=sums; log=mp.log(z/256)
    return [[f0,f1,f2/2],
            [f0*log+g0,f1*log+f0/z+g1,(f2*log+2*f1/z-f0/z**2+g2)/2]]


def step(s,h,state):
    p3=[s*s*(1-s),2*s-3*s*s,1-3*s,-1]
    p2=[3*s-mp.mpf(9)*s*s/2,3-9*s,-mp.mpf(9)/2]
    p1=[1-mp.mpf(51)*s/16,-mp.mpf(51)/16]
    p0=-mp.mpf(3)/32
    result=[]
    for first in state:
        a=list(first)
        for n in range(int(mp.mp.dps*1.8)+40):
            rhs=p0*a[n]
            for j in range(1,4):
                k=n+3-j
                if k>=3:rhs+=p3[j]*k*(k-1)*(k-2)*a[k]
            for j in range(3):
                k=n+2-j
                if k>=2:rhs+=p2[j]*k*(k-1)*a[k]
            for j in range(2):
                k=n+1-j
                if k>=1:rhs+=p1[j]*k*a[k]
            a.append(-rhs/(p3[0]*(n+3)*(n+2)*(n+1)))
        new=[]
        for derivative in range(3):
            terms=[a[k]*(1 if derivative==0 else k if derivative==1 else k*(k-1)/2)
                   for k in range(derivative,len(a))]
            new.append(mp.polyval(list(reversed(terms)),h))
        result.append(new)
    return result


def transport(s,target,state):
    steps=0
    while s!=target:
        distance=min(abs(s),abs(s-1))/6
        h=target-s
        if abs(h)>distance:h*=distance/abs(h)
        state=step(s,h,state);s+=h;steps+=1
        if abs(s-target)<mp.eps*16: s=target
    return s,state,steps


def run(identities,dps=180):
    mp.mp.dps=dps
    out={};upper=[];steps=0
    for identity in identities:
        t=mp.mpf(Fraction(identity).numerator)/Fraction(identity).denominator
        if t==0:out[identity]=mp.mpc(mp.mpf(1)/2,mp.mpf(1)/2)
        elif t>1:
            state=germ(t**-4);out[identity]=state[1][0]/(2*mp.pi*1j*state[0][0])
        else:upper.append((t**-4,identity))
    if upper:
        s=mp.mpf(1)/16;state=germ(s)
        for target in [mp.mpf(1)/2,mp.mpc(mp.mpf(1)/2,mp.mpf(1)/2),mp.mpc(mp.mpf(3)/2,mp.mpf(1)/2),mp.mpf(2)]:
            s,state,n=transport(s,target,state);steps+=n
        for target,identity in sorted(upper):
            s,state,n=transport(s,target,state);steps+=n
            out[identity]=state[1][0]/(2*mp.pi*1j*state[0][0])
    return out,steps
