'Unify representations of two multivariate polynomials.'

def unify(f, g):

    if ((not isinstance(g, DMP)) or (f.lev != g.lev)):
        raise UnificationFailed(("can't    unify    %s    with    %s" % (f, g)))
    if ((f.dom == g.dom) and (f.ring == g.ring)):
        return (f.lev, f.dom, f.per, f.rep, g.rep)
    else:
        (lev, dom) = (f.lev, f.dom.unify(g.dom))
        ring = f.ring
        if (g.ring is not None):
            if (ring is not None):
                ring = ring.unify(g.ring)
            else:
                ring = g.ring
        F = dmp_convert(f.rep, lev, f.dom, dom)
        G = dmp_convert(g.rep, lev, g.dom, dom)
        def per(rep, dom=dom, lev=lev, kill=False):
            if kill:
                if (not lev):
                    return rep
                else:
                    lev -= 1
            return DMP(rep, dom, lev, ring)
        return (lev, dom, per, F, G)
