# Calculating bracket operations [closed]

Is there a way to calculate commutation relations in Mathematica? For example, let's say I want to compute ; how can this be done?

• I am just curious if your goal is to compute this given commutator, or you also want to try other commutators (if yes which ones), or this problem is a part of a bigger problem which you try to solve with mathematica? – yarchik Dec 21 '17 at 20:39
• @yarchik I don't want to calculate commutators by hand as sometimes they could be too long and chances of making a mistake is not too low. something like $[(\partial_{x}x\partial_{x})^2,\partial_{x}]$ is 2 A4 papers in calculations. I want to know how to calculate commutators in general using mathematica – yasiren Dec 21 '17 at 21:05
• Possible duplicate of Boson commutation relations – yasiren Dec 22 '17 at 17:48

Assuming you are doing quantum mechanics, your $\partial_x$ is really $\hat{p}_x=\hbar/i \partial/\partial x$. Using the example, $[(\hat{p}_{x} x \hat{p}_{x})^2, \hat{p}_{x}]$, we may wish to move all of the $x$'s to the left and all of the $\hat{p}_{x}$'s to the right. Wherever we see $\hat{p}_{x} x$ in our expression, we want to replace it with $x\hat{p}_{x}-i\hbar$. And if you're not doing QM, just set $\hbar$ equal to $i$, where $i^2=-1$, in the following.

To avoid using symbols and operations with special meaning to Mathematica, let's use the symbols $p$ and $x$ and use non-commutative multiplication, symbolized with **. So we want to replace $p**x$ with $x**p - i\hbar$ inside operator expressions. Here's how we do it

rule = NonCommutativeMultiply[y___, p, x, z___] :>
NonCommutativeMultiply[y, x, p, z] -
I ℏ  NonCommutativeMultiply[y, z];


The rule uses triple blanks (____) to mean the p**x can be at the beginning, in the middle, or at the end of the non-commutative product. The example commutator can be written as

comm =  (p ** x ** p)^2 ** p - p ** (p ** x ** p)^2 ;


First, we expand the exponentials into non-commutative multiplications

comm = comm /. z_^2 :> z ** z ;


Then we apply our commutator rule using ReplaceRepeated,

comm //. rule // Expand

(*  4 ℏ^2 p ** p ** p + 2 I ℏ x ** p ** p ** p ** p  *)


We interpret the above expression to be $4\hbar^2 \, \hat{p}_{x}^3 +2i \hbar \,x\, \hat{p}_{x}^4$. So, it's a little rough, but it does put the commutator expression into a more manageable form.

There's a theorem that can be used to simplify commutator expressions by hand. The theorem says that $[x,\hat{p}_{x}^n] = i\hbar \, n \,\hat{p}_{x}^{n-1}$. Using this theorem we can verify the Mathematica results shown above in about 6 lines of calculations.

The problem is with the Times that gets introduced as the head of "f[x] x". It is difficult to get the "x out of the Derivative function".

You probably want to apply your commutator onto a function. The simplest way is to enter:

commutator[fctofx_, xx_] := -I (D[xx fctofx, xx] - xx D[fctofx, xx])


and call the function as the function and it's variable separately:

commutator[f[x],x]


which gives -I f[x] (I removed the hbar).

For this particular example you asked:

Commutator[op1_, op2_, fun_] := op1[op2[fun]] - op2[op1[fun]];
X[f_] := x f;
dx[f_] := D[f, x];
Commutator[dx, X, g[x]]


you just need to know the expansion of one operator in the other operator basis as well as the function expansion. It can be easily generalized.

Edited: as I said, my code is editable:

Commutator[op1_, op2_, fun_] := op1[op2[fun]] - op2[op1[fun]];
X[f_] := x f;
dx[f_] := D[f, x];
Commutator[dx, X, g[x]]
dxXdx[f_] := dx[X[dx[f]]];

dxXdx[g[x]];
dxXdx[dxXdx[g[x]]];

(*[(dx.x.dx)^2,dx] = dx.x.dx[dx.x.dx,dx]+[dx.x.dx,dx]dx.x.dx*)

dxXdx[Commutator[dxXdx, dx, g[x]] + Commutator[dxXdx, dx, dxXdx[g[x]]]]


the final results are:

g[x]
-g'''[x] -4 g''''[x] - x g'''''[x] + x (-g''''[x] - 5 g'''''[x] - x g''''''[x])