# Commutator of differential operators

Let $$P_x = \frac{\hbar}{i}\frac{d}{dx}$$, after specifying the commutator relation symbolically $$[X, P_x] = i\hbar$$, I can ask Mathematica to calculate commutator algebra. My question: is there a way for Mathematica to directly calculate this commutator without me specifying it? I've tried after searching and reading pure functions

x*(D[#, x] &) - (D[#, x] &)*x

but it doesn't quite compute correctly.

• Please make use of the search bar on top of this site; this question has been asked at least a dozen times before. – Henrik Schumacher Jan 11 '19 at 17:17
• For computing it directly, maybe the following will help: In[420]:= Together[ Through[(Composition[x*# &, D[#, x] &] + Composition[-D[#, x] &, x*# &])[f[x]]]/f[x]] Out[420]= -1 – Daniel Lichtblau Jan 11 '19 at 18:38
• Thanks for the quick line Daniel! After more searching, I found a nice answer by Jens here – Histoscienology Jan 11 '19 at 21:50