'Replace ``x`` with ``y`` in generators list.
Examples
>>> from sympy import Poly
>>> from sympy.abc import x, y
>>> Poly(x**2 + 1, x).replace(x, y)
Poly(y**2 + 1, y, domain=\'ZZ\')'

def replace(f, x, y=None):

    if (y is None):
        if f.is_univariate:
            (x, y) = (f.gen, x)
        else:
            raise PolynomialError('syntax    supported    only    in    univariate    case')
    if (x == y):
        return f
    if ((x in f.gens) and (y not in f.gens)):
        dom = f.get_domain()
        if ((not dom.is_Composite) or (y not in dom.symbols)):
            gens = list(f.gens)
            gens[gens.index(x)] = y
            return f.per(f.rep, gens=gens)
    raise PolynomialError(("can't    replace    %s    with    %s    in    %s" % (x, y, f)))
