7
$\begingroup$

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.

$\endgroup$
3
  • 2
    $\begingroup$ Carefully comparing FullForm[f @@ var] with FullForm[D[f @@ var, x]], and then taking a look at what you're actually trying to replace in D[f @@ var, x] /. f -> ( h[##] + g[##] &) should explain this behavior. $\endgroup$
    – ktm
    Mar 3, 2019 at 22:02
  • $\begingroup$ Are you at all able to formulate your replacement this way? D[(f @@ var) /. f -> (h[##] + g[##] &), x] $\endgroup$
    – ktm
    Mar 3, 2019 at 22:05
  • 1
    $\begingroup$ Here is the answer to why it does not work: Derivative of a pure function with SlotSequence $\endgroup$
    – Kuba
    Mar 4, 2019 at 20:58

2 Answers 2

7
$\begingroup$

I think you have found one of the quirks of the convoluted Mathematica evaluation rules. I suggest you try instead the alternative code:

D[f @@ {t. x, y, z}, x] /. {dd_[nn__][f][xx__] -> dd[nn][g][xx] + (dd[nn][h])[xx]} // InputForm

which returns

Derivative[0, 1, 0, 0][g][t, x, y, z] + Derivative[0, 1, 0, 0][h][t, x, y, z]

The reason for this is that the Derivative[] function returns an unusual expression form:

D[ f[x], x] == Derivative[1][f][x]

which is more convoluted than expected and why the replacement rule has to accept this form.

$\endgroup$
4
$\begingroup$

If you write g[##] + h[##] & in alternative form Total @* (Through @ {g, h} @ ## &) you get the desired result:

D[f @@ var, x] /. f -> (Total @* (Through @ {g, h} @ ## &))

enter image description here

Alternatively, you can inject g[##] + h[##] & for f in D[...] using With:

With[{f = (g[##] + h[##] &)}, D[f @@ var, x]]
% == %%

True

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.