1
$\begingroup$

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?

$\endgroup$
1
  • $\begingroup$ What comprises a prefactor here? $\endgroup$ Commented Feb 4, 2024 at 23:12

1 Answer 1

1
$\begingroup$

You can explicitly check for the presence of arguments with ContainsAny.

expr /. e_[args__]*kd[x_, y_] /; ContainsAny[{args}, {x, y}] :> (e[args] /. x -> y) (* alc[b, c] band[d, c] + f1[b, c] f2[d, c] + dum[a, c] func[a, c] kd[b, d] + mon[d, b] *) 
$\endgroup$

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.