'Invert ``f`` modulo ``g`` when possible.
Examples
>>> from sympy import Poly
>>> from sympy.abc import x
>>> Poly(x**2 - 1, x).invert(Poly(2*x - 1, x))
Poly(-4/3, x, domain=\'QQ\')
>>> Poly(x**2 - 1, x).invert(Poly(x - 1, x))
Traceback (most recent call last):
NotInvertible: zero divisor'

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

    (dom, per, F, G) = f._unify(g)
    if (auto and dom.is_Ring):
        (F, G) = (F.to_field(), G.to_field())
    if hasattr(f.rep, 'invert'):
        result = F.invert(G)
    else:
        raise OperationNotSupported(f, 'invert')
    return per(result)
