I have an expression of the type
expr = k*a + j*a + a*f + a*g + h
And I would like to set a=0
but ONLY if it's NOT multiplied by f
or g
.
The result I'd want for the example would look like
expr = a*f + a*g + h
Is there a simple way to accomplish that?
In general k
and j
may be unkown expressions, but I do know the form of f
and g
.
expr /. Times[a, b__] /; FreeQ[{b}, f | g] -> 0
? $\endgroup$expr /. a Except[f | g] :> 0
? But won'r work if there are other factors in each term. $\endgroup$