Suppose I have an expression of the form
a b c d + x y z
The FullForm
of this is
Plus[Times[a,b,c,d], Times[x,y,z]]
Now suppose I want to set all expressions containing the product b*c to zero. The naive way of doing this is
a b c d + x y z /. Times[___, b, c, ___] -> 0
However, this doesn't work - nothing is replaced.
(Background info: I'm expanding some term using Series
, and would like to cancel out all products dx*dy, so that only first-order terms remain.)
Even more confusing: if I don't use Times
but a generic function f
it works - even when I assign all the attributes of Times
to f
!
f;
SetAttributes[f, #]& /@ Attributes[Times];
Attributes[f]
f[a, b, c, d] /. f[___, b, c, ___] -> 0
==>
{Flat, Listable, NumericFunction, OneIdentity, Orderless, Protected}
0
(If you evaluate this use a new kernel, as f
will be Protected
.)
So: how do I replace expressions containing some product*?
*: Assuming the order of the arguments is known. Don't worry about the fact that the expression for b might be on the left of the one for a; solving this would be another question.