# Second order differential operator in Mathematica

I am trying to write the code for the following expression in Mathematica: $$\partial_{x} e^{-g x}=-g e^{-g x}+e^{-g x} \partial_{x}\,.$$ I was able to do so with the help of the following links with the help of answer of Carl Woll of the following question Having the derivative be an operator and from Symbolic FAQ.

What I have done is

<< DifferentialOperator

Clear[differentialOperate]
differentialOperate[a_, expr_] /; FreeQ[a, D] := a*expr

differentialOperate[L1_ + L2_, expr_] := differentialOperate[L1, expr] + differentialOperate[L2, expr]

differentialOperate[a_*L_, expr_] /; FreeQ[a, D] := a*differentialOperate[L, expr]

differentialOperate[a : HoldPattern[D[__] &], expr_] := a[expr]

differentialOperate[L1__ ** L2_, expr_] := Expand[differentialOperate[L1, differentialOperate[L2, expr]]]

commutator[L1_, L2_] := L1 ** L2 - L2 ** L1

differentialOperate[L1_^n_Integer, expr_] /; n > 1 := Nest[Expand[differentialOperate[L1, #]] &, expr, n]

ddx = (D[#, x] &);

diffop = (ddx + DifferentialOperator[x])


Got the desired results here

differentialOperate[diffop, Exp[-g x]]


However, I also want to calculate $$\partial_x^2 e^{-gx}\,.$$ What necessary changes are needed in the code? I have tried differentialOperate[ diffop,%]. However could not get the desired results.

For completeness I'll recopy the differentialOperate code. I have no idea what is being loaded with the <<DifferentialOperator so I omit that.

differentialOperate[a_, expr_] /; FreeQ[a, D] := a*expr
differentialOperate[L1_ + L2_, expr_] :=
differentialOperate[L1, expr] + differentialOperate[L2, expr]
differentialOperate[a_*L_, expr_] /; FreeQ[a, D] :=
a*differentialOperate[L, expr]
differentialOperate[a : HoldPattern[D[__] &], expr_] := a[expr]
differentialOperate[L1__ ** L2_, expr_] :=
Expand[differentialOperate[L1, differentialOperate[L2, expr]]]
commutator[L1_, L2_] := L1 ** L2 - L2 ** L1
differentialOperate[L1_^n_Integer, expr_] /; n > 1 :=
Nest[Expand[differentialOperate[L1, #]] &, expr, n]


Where your code goes astray is in treating the exponential as the thing to operate on rather than as part of the operator itself. From context this latter is what you seem to want. The way to do that is to use NonCommutativeMultiply so as to make sure the derivative part gets applied appropriately to both exponential and operatee (this is The Chain Rule at Work for You).

All this is simpler than it might look. Just define the second derivative operator, noncommutatively multiply by the exponential, and apply to an "arbitrary" function of the variable in question. I show the result in InputForm.

d2dx = (D[#, {x, 2}] &);

differentialOperate[d2dx ** Exp[-g*x], f[x]]

(* Out[11]= (g^2*f[x])/E^(g*x) - (2*g*Derivative[1][f][x])/E^(g*x) + Derivative[2][f][x]/E^(g*x) *)

• I grabbed the idea that we have to re-define the second derivative function. Will this work equally well for $\partial_x=f(x)$, where f(x) is any function of x? I think we need to do some more work to generalize the code.
– Sam
Mar 31, 2022 at 5:55