# Writing (differential) operators [duplicate]

I would like to apply the differential operator

$$- (\partial_\theta -\frac{i}{\sin (\theta)} \partial_\phi + s \cot (\theta))$$

to a function $$F$$ (Spherical Harmonics), which is dependent on $$\theta$$ and $$\phi$$ like SphericalHarmonicY[l, m, θ, ϕ]. How do I write this differential operator in Mathematica? The implementation should not depend on the particular function.

A non-working attempt I made is

 (-Derivative[0, 0, 1, 0] + I/Sin[θ] Derivative[0, 0, 0, 1] + s Cot[θ])
SphericalHarmonicY[l, m, θ, ϕ]


But Mathematica didn't resolve the derivatives as stated in Derivative. Is there an easy solution to this, that works like multiplication from the left?

This question seems to solve this by repeating f after every differential. Is this really necessary? Mathematically, differential operators should not care about what comes after them, should they?

### Edit

Just found this question about writing differential operators, but don't really understand it.

• You need to write the operator as a pure function and enclose its argument in square brackets: (-Derivative[0, 0, 1, 0][#] + I/Sin[\[Theta]] Derivative[0, 0, 0, 1][#] + s Cot[\[Theta]]) & [ SphericalHarmonicY[l, m, \[Theta], \[Phi]]] – Bob Hanlon May 3 '18 at 15:09
• @BobHanlon it appears that you missed 's Cot[[Theta]] #', which seems to throw a monkey wrench into your solution since it somehow cant evaluate this. (it outputs (0&) at the end of each result). Full line: Y[s_, l_, m_, [Theta]_, [Phi]_] := Simplify[(-1)^ s Sqrt[(l + s)!/(l - s)!] (-Derivative[0, 0, 1, 0][#] + I/Sin[[Theta]] Derivative[0, 0, 0, 1][#] + s Cot[[Theta]] Derivative[0, 0, 0, 0][#]) & [ SphericalHarmonicY[l, m, [Theta], [Phi]]], Assumptions -> {-l <= s <= 0, -[Pi] <= [Phi] <= [Pi], 0 < [Theta] < [Pi]}]; – Gladaed May 3 '18 at 15:35
• I did not understand that the s Cot[\[Theta]] was supposed to be multiplied by the argument. Just change to (-Derivative[0, 0, 1, 0][#] + I/Sin[\[Theta]] Derivative[0, 0, 0, 1][#] + s Cot[\[Theta]] #) &[ SphericalHarmonicY[l, m, \[Theta], \[Phi]]] There is no 0& – Bob Hanlon May 3 '18 at 15:42

You can use my DifferentialOperator package to do what you want. Install with:

PacletInstall[
]


<<DifferentialOperator


Here's a short animation of the package in action for your question: • Thanks, but now op[s_, [Theta]_, [Phi]_] := -(Subscript[ DifferentialOperatorPrivateoperator[ DifferentialOperator[]], [Theta]] - I/Sin[[Theta]] Subscript[ DifferentialOperatorPrivateoperator[ DifferentialOperator[]], [Phi]] + s Cot[[Theta]]); Y[s_, l_, m_, [Theta]_, [Phi]_] := Simplify[(-1)^ s ((l - s)!/(l + s)!) (op[s, [Theta], [Phi]] @ SphericalHarmonicY[l, m, [Theta], [Phi]]), Assumptions -> {-l <= s <= 0, -[Pi] <= [Phi] <= [Pi], 0 < [Theta] < [Pi]}] – Gladaed May 7 '18 at 9:19
opL[f_]:=-(D[f,θ]-(I/Sin[θ])*D[f,φ]+s*Cot[θ]*f)
`