I would like to implement the following formula, which goes under the name of Hadamard Lemma:

$ e^A \, B \, e^{-A} = \sum_{k=0}^{+\infty} \frac{1}{k!} [A,B]_k $


$ [A,B]_0 = B , \quad [A,B]_1=[A,B], \quad [A,B]_2=[A,[A,B]], \quad \dots $

Of course $[A,B]$ is the standard commutator: $[A,B]=AB-BA$.

The first big problem is how to define my own operator algebra $\mathcal{A}= \{A,B,C\}$. In other words, I would like to be able to define the constitutive commutators of my own algebra. For example:

$ [A,B]=C, \quad [B,C]=A, \quad [C,A]=B $

And then of course, I would like to implement Hadamard Lemma. To be more precise, I would like to write a function, Rotation[X_,Y_] with X,Y $\in \mathcal{A}$ which outputs:

$ \sum_{k=0}^{+\infty} \frac{1}{k!} [X,Y]_k $


Let's consider angular momentum algebra, whose defining commutators are:

$ [L_1,L_2]=i L_3, \quad [L_2,L_3]=i L_1, \quad [L_3,L_1]=iL_2 \quad $

It's well known that

$e^{i\varphi L_2}L_1e^{-i\varphi L_2} = L_1\cos \varphi +L_3 \sin \varphi $

The way one can manually prove this relation is straightforward:

$\sum_{k=0}^{+\infty} \frac{(i\varphi)^k}{k!} [L_2,L_1]_k = L_1 + i\varphi[L_2,L_1]+\frac{(i\varphi)^2}{2}[L_2,[L_2,L_1]] + \dots =$

$ = L_1 + i\varphi(-iL_3) +\frac{(i\varphi)^2}{2} [L_2, (-i L_3)]+\dots = $

$= L_1 +\varphi L_3 -\frac{\varphi^2}{2} L_1 + \dots =$

And than one recognizes the Taylor series of $\sin$ and $\cos$:

$L_1 \cos \varphi+ L_3 \sin \varphi$

Well, I would like to implement this computation on Mathematica, starting from an user-defined commutator algebra.

  • $\begingroup$ It's not clear what you mean by "I would like to implement Hadamard Lemma". Try phrasing your question like this: "How can I write a function that returns X when given Y?" Preferably show examples in terms of code. How familiar are you with Mathematica? It doesn't do noncommutative algebra out of the box. There are packages for specific use cases, google e.g. NCAlgebra. Otherwise you have to implement things from scratch. To ask for help with that, it will be necessary to explain what you want to achieve in much clearer terms. $\endgroup$
    – Szabolcs
    Nov 16, 2016 at 18:29
  • $\begingroup$ Sorry for my imprecision. My question is: given a set of user-defined commutators, how can i write a function, Rotation[A_,B_] that outputs the sum of Hadamard's summation ? I've been using Mathematica for a couple of years, mainly for my Physics studies. I've already worked on some related topics following Feagin's book "Quantum Methods with Mathematica", but I don't know any specific package, like NCAlgebra you mentioned. $\endgroup$
    – AndreaPaco
    Nov 16, 2016 at 18:39
  • $\begingroup$ Please edit the text of the question, and explain this within the question, preferably through an example (this is my input to the function, this is the output I want). $\endgroup$
    – Szabolcs
    Nov 16, 2016 at 18:40
  • 1
    $\begingroup$ I am afraid most people will still not understand what you want to do. As I said before, the easiest way to remedy this is to add a clear example, where both the input and output is shown as Mathematica code. Also clarify how you want to deal with the non-commutative aspect since this isn't built into the system. Without this the question will get closed, as it doesn't fit the model of this site (it's more of a discussion starter). I am writing this to try to help you formulate a clear question and avoid closure. Please also see the site tour. $\endgroup$
    – Szabolcs
    Nov 16, 2016 at 18:56

1 Answer 1


One should really use commutator rather than Commutator for future-proofing concerns, but I have some bad habits...


(* Manually implement the algebra of interest: *)

Commutator[L1, L2] = I L3; Commutator[L2, L1] = -I L3;
Commutator[L2, L3] = I L1; Commutator[L3, L2] = -I L1;
Commutator[L3, L1] = I L2; Commutator[L1, L3] = -I L2;

(* Implement linearity, so that numbers can be pulled out. *)

Commutator[Times[c_, A__], B_] := c Commutator[Times[A], B] /; NumericQ[c]
Commutator[A_, Times[c_, B__]] := c Commutator[A, Times[B]] /; NumericQ[c]

(* Linearity
[A+B+...,C] = [A,C]+[B,C]+... and [A,B+C+...] = [A,B]+[A,C]+...

Commutator[Plus[A_, Addends__], B_]:=Commutator[A, B] + Commutator[Plus[Addends], B]
Commutator[A_, Plus[B_, Bddends__]]:=Commutator[A, B] + Commutator[A, Plus[Bddends]]

(* Note the reversed ordering! *)
hadamard[order_] := Sum[1/k! Fold[Commutator[#2, #1] &, B, Table[A, {k}]], {k, 0, order}]

NumericQ[\[Phi]] = True;
hadamard[10] /. {B -> L1, A -> I \[Phi] L2}

It is possible to make Commutator naturally antisymmetric in its arguments, but it takes more thinking. If you've got an algebra already it's probably worth writing out explicitly.

We can implement additional rules, but it requires a general implementation of noncommutative multiply. Mathematica has a built-in NonCommutativeMultiply which we can write as an infix operator **. But it requires some additional structure:


(* Flatness causes infinite loop problems for the pattern matcher *)
ClearAttributes[NonCommutativeMultiply, Flat];

(* But one-way flatness is fine. *)
NonCommutativeMultiply[A___, NonCommutativeMultiply[B__], C___] := NonCommutativeMultiply[A, B, C]
(* And if there's only one argument, you can get rid of the NCM wrapper *)
NonCommutativeMultiply[A_] := A
(* And if NCM has nothing in it, evaluate to the empty product. *)
NonCommutativeMultiply[] := 1

(* Distribution of ** over + *)
NonCommutativeMultiply[A___, Plus[B_, Bddends__], C___] := NonCommutativeMultiply[A, B, C] + NonCommutativeMultiply[A, Plus[Bddends], C]

(* Numeric things commute. *)
NonCommutativeMultiply[A___, c_, B___] := c NonCommutativeMultiply[A, B] /; NumericQ[c]
NonCommutativeMultiply[A___, Times[b_, B___], C___] := b NonCommutativeMultiply[A, Times[B], C]/; NumericQ[b]

(* Leibniz rule [AB,C] = A[B,C] + [A,C]B and [A,BC]=B[A,C]+[A,B]C *)
Commutator[NonCommutativeMultiply[A_, B__], C_] := Commutator[A, C] ** NonCommutativeMultiply[B] + A ** Commutator[NonCommutativeMultiply[B], C]
Commutator[A_, NonCommutativeMultiply[B__, C_]] := Commutator[A, NonCommutativeMultiply[B]] ** C + NonCommutativeMultiply[B] ** Commutator[A, C]

You further can add additional rules too, like

Commutator[A_, A_] := 0
Commutator[A_, B_] := A**B - B**A

which allows you to arrive at conclusions like

Commutator[L1, L2 ** L3]
(* I L1 ** L1 - I L2 ** L2 *)

using the above definitions for the specific Commutators given by the specified algebra. Note that you can't derive that it's zero without an additional explicit assumption. This also could let you define products L1**L2 rather than Commutator[L1,L2] if you so desired.

At this point I should point out that this answer is converging towards my answer to https://mathematica.stackexchange.com/a/63880/7936 but also that there's a bug there that I never tracked down.

  • $\begingroup$ The code you've written is already a great help! By the way, it includes a finite summation. Can you please modify it in such a way that it sums all the infinite terms? In such a way it should automatically recognize the Taylor series of many interesting functions, like cos(phi) and sin(phi) in my example. $\endgroup$
    – AndreaPaco
    Nov 17, 2016 at 9:11
  • $\begingroup$ An other, much simpler question: can yuo please write the command for the linearity of the sum? [A+B,C]=[A,C]+[B,C] and [A,B+C]=[A,B]+[A,C] $\endgroup$
    – AndreaPaco
    Nov 17, 2016 at 12:16
  • $\begingroup$ I added the linearity over +. $\endgroup$
    – evanb
    Nov 17, 2016 at 17:23
  • $\begingroup$ As for your other request, I think the answer is: no, I cannot do this, because it amounts to recognizing any arbitrary function based on its taylor expansion. What you can do is to compute hadamard to a given order, and then compute your guess to the same given order, and check that the difference is 0. $\endgroup$
    – evanb
    Nov 17, 2016 at 17:28
  • $\begingroup$ Indeed, that's what I've tried even myself: I've made use of the function FindSequenceFunction to simplify the procedure. Great help, thanks! $\endgroup$
    – AndreaPaco
    Nov 17, 2016 at 17:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.