1
$\begingroup$

I have troubles substituting functions when I have symbolic derivatives and I need to substitute more symbolic derivatives in my expression. Take for example

D[f[x, y], {x, 2}];
% /. f[x, y] -> x^2 h[x, y]

The output is

(f^(2,0))[x,y]

and not $x^2\, \partial_x^{\,2}h(x,y)+4\,\partial_x h(x,y) x + 2\, h(x,y)$.

While I've read that this happens because

D[f[x, y], {x, 2}] // FullForm

gives

Derivative[2, 0][f][x, y]

and f[x, y] is not present here anymore, I couldn't find a solution for this.

Any ideas?

$\endgroup$
  • $\begingroup$ What you say should be the result is actually wrong. Try D[x h[x, y], {x, 2}] to see. btw, it might be easier just to make a function. r[expr_] := D[expr, {x, 2}]; then you can do r[f[x, y]] and r[x h[x, y]] $\endgroup$ – Nasser Jan 31 '16 at 11:20
  • $\begingroup$ yes sorry, I was actually thinking about $x^2 h(x,y)$ but I mistyped the first formula! $\endgroup$ – bnado Jan 31 '16 at 11:24
  • $\begingroup$ @Nasser your solution is a bit unconvenient for me, because I would have to change a big chunk of code. do you know of another way? $\endgroup$ – bnado Jan 31 '16 at 11:30
  • 5
    $\begingroup$ take a look here: change of variables in differential expressions and check the section "Functions replacement" in my answer. $\endgroup$ – Kuba Jan 31 '16 at 11:51
  • 2
    $\begingroup$ And a quick fix: % /. f -> (#^2 h[#, #2] &) $\endgroup$ – Kuba Jan 31 '16 at 11:58
5
$\begingroup$

A standard way to do this is to Hold it, and Release when needed:

r = Hold[D[f[x, y], {x, 2}]]

Mathematica graphics

Release[r]

Mathematica graphics

Release[r /. f[x, y] -> x h[x, y]]

Mathematica graphics

Compare the above to D[x h[x, y], {x, 2}]

Mathematica graphics

it is the same.

| improve this answer | |
$\endgroup$
2
$\begingroup$

Starting in version 10 you can use Inactivate and Activate.

w = Inactivate[D[f[x, y], {x, 2}], D]

enter image description here

wh = w /. f[x, y] -> x^2 h[x, y]

enter image description here

Activate@wh

enter image description here

Hope this helps.

| improve this answer | |
$\endgroup$

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