# How to find nth log derivative using mathematica?

Now I have a function, say $f(k,z)=e^{-kz}(1+kz)$

I want to find the $n$th $\log$ derivative with respect to z. like $(z\partial_z)^{(n)}f(k,z)$ (or $(\partial_{\ln z})^{(n)}f(k,z)$ if you like), where the $(n)$ denotes that we take the derivative $n$ times.

I found the answer in this question quite helpful for me to find a general expression for $\partial_z^{(n)}f(k,z)$; however, I don't know how to generalize it to $\log$ derivative case using Mathematica. Any suggestions?

Consider

logDiv[f_, n_] := Nest[z D[#, z] &, f, n]


which applies z D[#, z] & n times to f. Then, for instance,

logDiv[k z, 2]
(* k z *)


or

logDiv[Exp[-kz] (1 + k z), 2]
(* E^-kz k z *)


There is a nice closed form in terms of the Stirling subset numbers for these:

lDerivative[n_Integer?Positive][f_] := Composition[Function, Evaluate] @
(StirlingS2[n, Range[n]].Table[#^\[FormalK] Derivative[\[FormalK]][f][#],
{\[FormalK], 1, n}])


For instance,

lDerivative[f][x] == Nest[x D[#, x] &, f[x], 8] // Expand
True

lDerivative[Function[z, Exp[-k z] (1 + k z)]][z] // Simplify
E^(-k z) k^2 z^2 (-2 + k z)

f[k_, z_] = E^(-k*z)*(1 + k*z);


Direct calculation using Nest

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


Simplify at each step to reduce number of terms to be differentiated

dOp2[f_, n_Integer?NonNegative] :=
Nest[Simplify[z*D[#, z]] &, f, n]


Transform for straightforward differentiation rather than differential operator

dOp3[f_, n_Integer?NonNegative] :=
D[f /. z -> E^z, {z, n}] /. z -> Log[z]


Equivalent series expansion

dOp4[f_, n_Integer?NonNegative] :=
Sum[StirlingS2[n, i]*z^i*D[f, {z, i}], {i, n}]


Comparing performance of the different approaches

m = 15;

g1 = dOp1[f[k, z], m]; // AbsoluteTiming

(*  {0.153352, Null}  *)

g2 = dOp2[f[k, z], m]; // AbsoluteTiming

(*  {0.11716, Null}  *)

g3 = dOp3[f[k, z], m]; // AbsoluteTiming

(*  {0.102199, Null}  *)

g4 = dOp4[f[k, z], m]; // AbsoluteTiming

(*  {0.000697, Null}  *)


Verifying that all results are the same

g1 == g2 == g3 == g4 // Simplify

(*  True  *)


Relative efficiency varies depending on order (n) and specific function (f).