'Internal implementation of :func:`subs`.'

def _eval_subs(f, old, new):

    if (old in f.gens):
        if new.is_number:
            return f.eval(old, new)
        else:
            try:
                return f.replace(old, new)
            except PolynomialError:
                pass
    return f.as_expr().subs(old, new)
