'Apply a function to all terms of ``f``.
Examples
>>> from sympy import Poly
>>> from sympy.abc import x
>>> def func(k, coeff):
...     k = k[0]
...     return coeff//10**(2-k)
>>> Poly(x**2 + 20*x + 400).termwise(func)
Poly(x**2 + 2*x + 4, x, domain=\'ZZ\')'

def termwise(f, func, *gens, **args):

    terms = {}
    for (monom, coeff) in f.terms():
        result = func(monom, coeff)
        if isinstance(result, tuple):
            (monom, coeff) = result
        else:
            coeff = result
        if coeff:
            if (monom not in terms):
                terms[monom] = coeff
            else:
                raise PolynomialError(('%s    monomial    was    generated    twice' % monom))
    return f.from_dict(terms, *(gens or f.gens), **args)
