# How can I take derivatives recursively in Mathematica? [closed]

Let's say I have the function

f[x_]:=x^50


I want to take one derivative and multiply the result by x recursively. That is, I want to

x D[x D[f[x],x],x]


but with an arbitrary amount of iterations. How can I implement this more elegantly?

• Look at Nest. – MarcoB Mar 10 '17 at 22:38

The function you are looking for is Nest.

Nest[x D[#, x] &, f[x], 2]


gives the expression you want. Change the last argument to control the number of recursions.

Your process is equivalent to differentiating with respect to Log[x]. I gave a function, chainD for performing these kinds of derivatives in my answer to Higher-order partial derivatives w.r.t. variable raised to some power. If you use this function:

chainD[x^50, Log[x]]
chainD[x^50, {Log[x], 20}]


50 x^50

9536743164062500000000000000000000 x^50

I should mention that you can manually apply the idea behind chainD with:

D[Exp[50 y], {y, 20}] /. y->Log[x]


9536743164062500000000000000000000 x^50

dOp1[f_, n_Integer?NonNegative] :=
Nest[x*D[#, x] &, f[x], n]


For an undefined function f

Table[dOp1[f, n], {n, 0, 5}] // Expand // Column


Compare the coefficients with

Table[StirlingS2[n, m], {n, 0, 5}, {m, 0, n}] // Grid


Consequently, an alternative definition is

dOp2[f_, n_Integer?NonNegative] :=
Sum[StirlingS2[n, m] D[f[x], {x, m}] x^m, {m, 0, n}]


Testing equivalence

And @@ Table[dOp1[f, n] == dOp2[f, n] // Simplify, {n, 0, 15}]

(*  True  *)


You can use a proof by induction to verify this.

For your specific f

f[x_] = x^50;

dOp2[f, 20]

(*  9536743164062500000000000000000000 x^50 *)