# define an operator

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)$$

 op[x][Cos[x]]


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

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

 Clear[op]
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:

  op[x,3,#]&[Cos[x]]


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

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


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


??

For example,

op[x, 3][x^3]

(*  6 + x  *)


Have fun!

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