# Define a function whose variable is differential operator

I would like to act with a differential operator $$D_x = x \partial_x^2+3\partial_x$$ on a function $$g(x)$$ to compute something like $$((D_x)^2 +D_x) g(x)$$ Clearly, in this simple example, this does the trick

dop[t_] := t  D[#, {t, 2}] + 3  D[#, t] &
dop[x][dop[x][g[x]]] + dop[x][g[x]]


However, in my case I have more derivatives and I would like to do things differently. I would like to more generally define a function $$f(x) = x^2+x$$ and then consider $$f(D_x)g(x) = ((D_x)^2 +D_x) g(x)$$

How can I define this function $$f(D_x)$$ in Mathematica?

• Feb 20 at 11:35

We define an operator, that takes a polynomial in x and replaces every x by a derivative operator that acts on the variable x. To prevent that the operator is evaluated too early, we need "Hold":

op[der_] :=
Evaluate[der /. x^(n_ : 1) :> Hold[D[#, {x, n}]]] & // ReleaseHold


Here is an example:

op[x + 2  x^2 + 3  x^3][x^3]

18 + 12 x + 3 x^2


Of course you may also define the operator that it take an arbitrary variable as argument to act on, but I want to keep it simple.

If you want differential operators of the for of a polynomial like e.g. x ∂^2x, we may extend the above.

Define the operator as a function of d with coefficients depending of x:

op[der_] :=  Evaluate[der /. d^(n_ : 1) :> Hold[D[#, {x, n}]]] & // ReleaseHold;


Now if we write e.g.:

op[x d^2][x^2]

2 x


Or

op[a  d^2 + b  x  d][x^2]

2 a + 2 b x^2


Or even:

op[Sin[x]  d][x^2]

2 x Sin[x]

• The question needs an operator like $x ∂_x^2$; how would you enter that using your answer? Feb 20 at 14:20
• I'd be also curious to know Feb 22 at 10:42
• Look at the addendum. Feb 22 at 11:03