'Computes the polynomial remainder of ``f`` by ``g``.
Examples
>>> from sympy import Poly
>>> from sympy.abc import x
>>> Poly(x**2 + 1, x).rem(Poly(2*x - 4, x))
Poly(5, x, domain=\'ZZ\')
>>> Poly(x**2 + 1, x).rem(Poly(2*x - 4, x), auto=False)
Poly(x**2 + 1, x, domain=\'ZZ\')'

def rem(f, g, auto=True):

    (dom, per, F, G) = f._unify(g)
    retract = False
    if (auto and dom.is_Ring and (not dom.is_Field)):
        (F, G) = (F.to_field(), G.to_field())
        retract = True
    if hasattr(f.rep, 'rem'):
        r = F.rem(G)
    else:
        raise OperationNotSupported(f, 'rem')
    if retract:
        try:
            r = r.to_ring()
        except CoercionFailed:
            pass
    return per(r)
