2
$\begingroup$

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?

Thank you in advance!

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

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]
| improve this answer | |
$\endgroup$
  • $\begingroup$ That solves my problem! Thank you so much! $\endgroup$ – Svetlana May 27 at 0:10
2
$\begingroup$

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.

| improve this answer | |
$\endgroup$
  • $\begingroup$ That is a good solution! Thank you very much! $\endgroup$ – Svetlana May 27 at 0:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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