Attempting to write a rule that will substitute an undetermined function of several variables by some other undeterminate functions (also, of the same several variables). I can do it in an inelegant way that is not very reusable (requires the specification of all the arguments individually). I'd like to be able to do it for any number of arguments. Unfortunately, I stumble into the following problem:
The following two rules return the same output when applied to a function
f @@ var /. f -> ( h[#1, #2, #3, #4] + g[#1, #2, #3, #4] &)
(* g(t,x,y,z)+h(t,x,y,z) *)
f @@ var /. f -> ( h[##] + g[##] &)
(* g(t,x,y,z)+h(t,x,y,z) *)
Here var = {t,x,y,z}
. Unfortunately, they return different results when applied to the derivative of that function.
D[f @@ var, x] /. f -> ( h[#1, #2, #3, #4] + g[#1, #2, #3, #4] &)
(* g^(0,1,0,0)(t,x,y,z)+h^(0,1,0,0)(t,x,y,z) *)
D[f @@ var, x] /. f -> ( h[##] + g[##] &)
(* 0 *)
I'd like to understand both why the second rule doesn't work and why the first one does.
To clarify, my question is how to write a rule that does what the first of the above rules performs on both function and derivatives that can easily be applied to functions with many arguments.
FullForm[f @@ var]
withFullForm[D[f @@ var, x]]
, and then taking a look at what you're actually trying to replace inD[f @@ var, x] /. f -> ( h[##] + g[##] &)
should explain this behavior. $\endgroup$D[(f @@ var) /. f -> (h[##] + g[##] &), x]
$\endgroup$