There are different ways to organize this, but here is a compact way -- not necessarily the best. I don't know what your 'func's look like so I am just showing the transformation of nc by multiplying or dividing by 2:
def mul_xform(func): def product_bases(nc): if len(nc) == 2 or len(nc) == 1 and nc[0].is_Pow and nc[0].exp == 2: return Mul._from_args(nc) assert not nc def updater(p, func, m): if p.is_Mul: c, nc = p.args_cnc(split_1=False) if nc: nc = product_bases(nc) if nc not in m: m[nc] = func(nc) c = Mul._from_args(c) return c*m[nc] return p return Transform(lambda k: updater(k, func, {}), lambda w: w.is_Mul) T2 = mul_xform(lambda x: 2*x) Thalf = mul_xform(lambda x: x/2) >>> var('ex ey', commutative=False) (ex, ey) >>> (15*ex**2 + 6*ey*ex + ex).xreplace(T2) ex + 30*ex**2 + 12*ey*ex >>> _.xreplace(Thalf) ex + 15*ex**2 + 6*ey*ex /c -- You received this message because you are subscribed to the Google Groups "sympy" group. To unsubscribe from this group and stop receiving emails from it, send an email to sympy+unsubscr...@googlegroups.com. To post to this group, send email to sympy@googlegroups.com. Visit this group at http://groups.google.com/group/sympy?hl=en-US. For more options, visit https://groups.google.com/groups/opt_out.