# Symbolic derivatives and substitution [duplicate]

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?

• 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]] Commented Jan 31, 2016 at 11:20
• yes sorry, I was actually thinking about $x^2 h(x,y)$ but I mistyped the first formula! Commented Jan 31, 2016 at 11:24
• @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? Commented Jan 31, 2016 at 11:30
• take a look here: change of variables in differential expressions and check the section "Functions replacement" in my answer.
– Kuba
Commented Jan 31, 2016 at 11:51
• And a quick fix: % /. f -> (#^2 h[#, #2] &)
– Kuba
Commented Jan 31, 2016 at 11:58

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

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


Release[r]


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


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

it is the same.

Starting in version 10 you can use Inactivate and Activate.

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


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


Activate@wh


Hope this helps.