

def dmp_zz_heu_gcd(f, g, u, K):
    '\n    Heuristic polynomial GCD in `Z[X]`.\n\n    Given univariate polynomials `f` and `g` in `Z[X]`, returns\n    their GCD and cofactors, i.e. polynomials ``h``, ``cff`` and ``cfg``\n    such that::\n\n          h = gcd(f, g), cff = quo(f, h) and cfg = quo(g, h)\n\n    The algorithm is purely heuristic which means it may fail to compute\n    the GCD. This will be signaled by raising an exception. In this case\n    you will need to switch to another GCD method.\n\n    The algorithm computes the polynomial GCD by evaluating polynomials\n    f and g at certain points and computing (fast) integer GCD of those\n    evaluations. The polynomial GCD is recovered from the integer image\n    by interpolation. The evaluation proces reduces f and g variable by\n    variable into a large integer.  The final step is to verify if the\n    interpolated polynomial is the correct GCD. This gives cofactors of\n    the input polynomials as a side effect.\n\n    Examples\n    ========\n\n    >>> from sympy.polys import ring, ZZ\n    >>> R, x,y, = ring("x,y", ZZ)\n\n    >>> f = x**2 + 2*x*y + y**2\n    >>> g = x**2 + x*y\n\n    >>> R.dmp_zz_heu_gcd(f, g)\n    (x + y, x + y, x)\n\n    References\n    ==========\n\n    1. [Liao95]_\n\n    '
    if (not u):
        return dup_zz_heu_gcd(f, g, K)
    result = _dmp_rr_trivial_gcd(f, g, u, K)
    if (result is not None):
        return result
    (gcd, f, g) = dmp_ground_extract(f, g, u, K)
    f_norm = dmp_max_norm(f, u, K)
    g_norm = dmp_max_norm(g, u, K)
    B = K(((2 * min(f_norm, g_norm)) + 29))
    x = max(min(B, (99 * K.sqrt(B))), ((2 * min((f_norm // abs(dmp_ground_LC(f, u, K))), (g_norm // abs(dmp_ground_LC(g, u, K))))) + 2))
    for i in range(0, HEU_GCD_MAX):
        ff = dmp_eval(f, x, u, K)
        gg = dmp_eval(g, x, u, K)
        v = (u - 1)
        if (not (dmp_zero_p(ff, v) or dmp_zero_p(r, v))):
            (h, cff, cfg) = dmp_zz_heu_gcd(ff, gg, v, K)
            h = _dmp_zz_gcd_interpolate(h, x, v, K)
            h = dmp_ground_primitive(h, u, K)[1]
            (cff_, r) = dmp_div(f, h, u, K)
            if dmp_zero_p(r, u):
                (cfg_, r) = dmp_div(g, h, u, K)
                if dmp_zero_p(r, u):
                    h = dmp_mul_ground(h, gcd, u, K)
                    return (h, cff_, cfg_)
            cff = _dmp_zz_gcd_interpolate(cff, x, v, K)
            (h, r) = dmp_div(f, cff, u, K)
            if dmp_zero_p(r, u):
                (cfg_, r) = dmp_div(g, h, u, K)
                if dmp_zero_p(r, u):
                    h = dmp_mul_ground(h, gcd, u, K)
                    return (h, cff, cfg_)
            cfg = _dmp_zz_gcd_interpolate(cfg, x, v, K)
            (h, r) = dmp_div(g, cfg, u, K)
            if dmp_zero_p(r, u):
                (cff_, r) = dmp_div(f, h, u, K)
                if dmp_zero_p(r, u):
                    h = dmp_mul_ground(h, gcd, u, K)
                    return (h, cff_, cfg)
        x = (((73794 * x) * K.sqrt(K.sqrt(x))) // 27011)
    raise HeuristicGCDFailed('no luck')

dataset/ETHPy150Open sympy/sympy/sympy/polys/euclidtools.py dmp_zz_heu_gcd/VarMisuse@248/890 `gg`->`r`