1

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?

m_goldberg
  • 107,779
  • 16
  • 103
  • 257
bnado
  • 413
  • 2
  • 8

2 Answers2

5

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.

Nasser
  • 143,286
  • 11
  • 154
  • 359
2

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.

Edmund
  • 42,267
  • 3
  • 51
  • 143