Consider the following symbolic expression:
expr=mon[m,b]*kd[m,d]+band[a,c]alc[b,c]kd[a,d]+func[a,c]dum[a,c]kd[b,d] + f1[b,c]f2[d,c] It has three types of summands: the ones where arguments of kd are included in the pre-factor, the ones where it is not, and those do not containing kd. I want to obtain
expr1=mon[d,b] + band[d,c]alc[b,c] + func[a,c]dum[a,c]kd[b,d] + f1[b,c]f2[d,c] i.e. "contract" kd with the pre-factors. I may do this for the first two summands using the replacement
e_*kd[x_, y_] :> (e /. x -> y) However, if applying to the third summand, it obviously removes kd and returns a wrong expression. In reality, I have a complicated expression generated in-flight, including the two types of summands, so I need a universal rule that does not touch the terms of the second type and replaces the terms of the first type.
What can be such a replacement rule?