Suppose I have some rule, e.g. Re[x_] :> (x + Conjugate[x])/2
. I want to apply it to all matching sub-expressions in some expression, excluding any sub-expressions that are located somewhere under heads Hold[…]
or Function[…]
(at any depth). What is the simplest way to do that? Can it be done without explicit filtering of positions?
For example, the result of replacement in the expression:
{z + Re[a], Hold[1 + Re[b]], Function[z, Abs[Re[c]]]}
should be
{z + (a + Conjugate[a])/2, Hold[1 + Re[b]], Function[z, Abs[Re[c]]]}
where Re[b]
and Re[c]
are left unmodified, because they are located under Hold
and Function
.