I have three expressions a[x, y], b[x, y], c[x, y]
that act as placeholders for functions of two variables x,y
. Consider the following substitution:
a[x, y]/(b[x, y] c[x, y]) /. f_[x1_, y1_] :> f[2 x1, 3 y1]
a[2 x, 3 y]/(64 b[x, y]^3 c[x, y]^3)
In the output we see that the numerator expression was substituted properly, but in the denominator the pattern f_
registered for the head Power
instead of looking for my own expressions. Of course I can fix this by:
a[x, y]/(b[x, y] c[x, y]) /. a[x1_, y1_] :> a[2 x1, 3 y1] /.b[x1_, y1_] :> b[2 x1, 3 y1] /. c[x1_, y1_] :> c[2 x1, 3 y1]
a[2 x, 3 y]/(b[2 x, 3 y] c[2 x, 3 y])
which gives the desired output. But this amounts to writing three times as many substitution directives and is therefore inconvenient. To fix the first example, I tried using /. f_Symbol[x1_, y1_] :> f[2 x1, 3 y1]
or /. f_[x1_, y1_]/;Head[f]===Symbol :> f[2 x1, 3 y1]
, but this does not correct it. Is there a way to write a proper substitution that works with headers and does not act on built in functions? Thanks for any suggestions.
EDIT:
Just noticed that Head[Power]
actually returns Symbol
, which is kind of weird. I would have expected it to return e.g. Function
, or Directive
, or something along the lines. (If one unprotects and clears the Power
function, then I would again expect Head[Power]
to return Symbol
of course. But maybe that's just me...)
f_
by(f:(a|b|c))
$\endgroup$Product
is not a head that ever appears in these expressions. This can be seen by usingFullForm
orFreeQ[a[x, y]/(b[x, y] c[x, y]), Product]
. It is a little bit tricky to see what headsf_
will be matched with, as things depend on evaluation, but the heads area
,Power
andPower
. This can be seen fromReap[a[x, y]/(b[x, y] c[x, y]) /. f_[x1_, y1_] :> Sow[f]][[2, 1]]
. $\endgroup$Power
head instead ofProduct
. (The problem stays the same though.) $\endgroup$