# Symbolic nest expressions

I'm trying to understand how to use symbolic expressions as arguments. The following example represents my problem:

Q[f_, s0_] := Q[f, s0] = D[f[s0], s0]
Q[Q[f, s0], s0]


where Q is simply first derivative of function f. As a result of the second line, I expect to see second derivative of f. But I'm getting

f'[s0]'[s0] + f''[s0][s0]


Could you help me please find a way to obtain a correct answer?

• Try using Q[f_, s0_] := Q[f, s0] = D[f, s0] in a fresh kernel, and then evaluate Q[Q[f, s0], s0] again. – J. M.'s technical difficulties May 26 at 2:17

You are effectively calling Q with two different argument patterns (Q[foo, t] in the first call in the nested calls and Q[bar[t],t] in the outer call) but your Q is defined only for the first argument pattern. If you add the definition for the second signature, you get the desired result:

ClearAll[Q]
Q[f_, s0_] := Q[f, s0] = D[f[s0], s0]
Q[f_[s0_], s0_] := Q[f, s0]

Q[Q[f, s0], s0]

 (f^′′)[s0]

Q[Q[f[s0], s0], s0]

  (f^′′)[s0]

• That solves my problem! Thank you so much! – Svetlana May 27 at 0:10

I suggest you using Derivative:

Q[f_] := Derivative[1][f]
Q[f_, s0_] := Derivative[1][f][s0]

Q[f, s0]

f'[s0]

Q[Q[f], s0]

f''[s0]


BTW, the number 1 in the first [...] after Derivative can be other integers, so that the nested application can actually be lifted.

• That is a good solution! Thank you very much! – Svetlana May 27 at 0:11