

def continuants(contfrac):
    'Continuants of a continued fraction.\n\n    contfrac must yield an infinite sequence (n0, d0), (n1, d1), (n2,\n    d2), ..., representing the continued fraction\n\n                n0\n        ------------------\n                  n1\n        d0 + -------------\n                     n2\n             d1 + --------\n                  d2 + ...\n\n    The kth continuant is the numerator and denominator of the\n    continued fraction truncated at the kth term, i.e. with zero\n    instead of the rest in the ellipsis.\n\n    If the numerator or denominator grows large in magnitude, both are\n    multiplied by the machine epsilon, leaving their quotient\n    unchanged.\n    '
    p0 = 1.0
    q0 = 0.0
    p = 0.0
    q = 1.0
    for (n, d) in contfrac:
        (p0, q0, p, q) = (p, q, ((d * p) + (n * p0)), ((d * q) + (n * q0)))
        if ((p >= EPSINV) or (q >= EPSINV)):
            p0 *= EPSILON
            q0 *= EPSILON
            p *= EPSILON
            q *= EPSILON
        assert (q != 0)
        (yield (p, q))

dataset/ETHPy150Open probcomp/bayeslite/src/math_util.py continuants/original