3
$\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$
1
  • $\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$ Commented May 26, 2020 at 2:17

3 Answers 3

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]
$\endgroup$
1
  • $\begingroup$ That solves my problem! Thank you so much! $\endgroup$
    – Svetlana
    Commented May 27, 2020 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.

$\endgroup$
1
  • $\begingroup$ That is a good solution! Thank you very much! $\endgroup$
    – Svetlana
    Commented May 27, 2020 at 0:11
1
$\begingroup$

Try this:

Q[function_,variable_,n_? Positive]/;Element[n,Integers]:=Nest[D[#,variable]&,function[variable],n]

Example:

Q[f,s0,1]
Q[f,s0,2]
(*f'[s0]*)
(*f''[s0]*)
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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