import sys, json
sys.path.insert(0, '.')
import batch
from sympy import symbols, solve, series, sqrt, simplify, Rational
x, g = symbols('x g')
def cf_periodic(coeffs):
    """g = 1/(1 - c1 x/(1 - c2 x/(... /(1 - ck x * g)))) with signs embedded in coeffs."""
    t = g
    for c in reversed(coeffs):
        t = 1/(1 - c*x*t)
    sols = solve(t - g, g)
    for s in sols:
        try:
            if series(s, x, 0, 2).removeO().subs(x, 0) == 1:
                return simplify(s)
        except Exception:
            pass
    return None
batch.GF['A168503'] = cf_periodic([1, 2, 3])
batch.GF['A168505'] = cf_periodic([1, -1, 1])
batch.GF['A186338'] = cf_periodic([2, 2, 1])
for k in ['A168503', 'A168505', 'A186338']:
    print(k, batch.GF[k])
    print(json.dumps(batch.run(k)))
