How is it possible to define the operator $(x+\frac{d}{dx})^n$ as a function of $n$? I use

 op[x_] = (x + D[#, x]) &;

with the action on, for example, $\cos(x)$


for $n=1$. How is it possible to extend the definition for an arbitrary $n$?

  • $\begingroup$ What is the expected output of your example ? $\endgroup$ – b.gates.you.know.what Nov 30 '18 at 10:44
  • $\begingroup$ I think your initial function should be defined as op[x_] = (x*# + D[#, x]) &; This is what you want right? $\endgroup$ – user59583 Nov 30 '18 at 10:55
  • $\begingroup$ @ b.gatessucks, the desired output is $(x+d/dx)^n f(x)$ for any user-defined function $f(x)$. $\endgroup$ – Chipa-Chipa Nov 30 '18 at 12:28
  • $\begingroup$ @Buddha u r right $\endgroup$ – Chipa-Chipa Nov 30 '18 at 12:31

I believe that you want to apply the following operator $n$ times:

 op[x_] = (x*# + D[#, x]) &

Otherwise you add the $x$ rather than apply it as an operator.

I do these in a recursive function way:

 op[x_, n_] := op[x, n] = (x*# + D[#, x]) &[op[x, n - 1]];
 op[x, 0] = Cos[x];

Well, apparently using the Nest command you can do it even more clearly as I have seen in "another post". Here is the more simple way:

  op[x_,n_,input_]:=Expand@Nest[(x*# + D[#, x]) &,input,n]

I have added the Expand to simplify the final expression. You can just replace the input with the desired function when calling the function or I believe with your syntax you can use:


I have added this because with recursive functions you have to remember to clear the definition.


What about

op[f_, i_] := Nest[x # + D[#, x] &, f[x], i]

What about

op[x_, n_] = (x + D[#, {x, n}]) &;


For example,

op[x, 3][x^3]

(*  6 + x  *)

Have fun!

  • $\begingroup$ your suggestion is $x+\frac{d^n}{dx^n}$ which is not the operator needed $\endgroup$ – Chipa-Chipa Nov 30 '18 at 12:25

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.