# Non-commutative algebra (NCAlgebra): How to properly SetCommutingOperators

I am starting to work with non-commutative algebra in Mathematica and had a look at the NCAlgebra package. I installed it and can use its functions. However, what I am struggling with is the SetCommutingOperators command, described in I.4.6.4 of the documentation. Honestly, LeftQ, SetCommutingFunctions and SetCommutingOperators just reference themselves in a cycle if I am not mistaken - which disallows me to fully understand the important note about using LeftQ.

How do I properly define that two symbols a and b commute, such that a**b-b**a==0. Some code with (as it appears) no effect of SetCommutingOperators:

ClearAll["Global*"];
<< NC
<< NCAlgebra
SetCommutingOperators[a, b]
a ** b == b ** a
(* a ** b == b ** a *)
NCE[b ** a - a ** b]
(* -a ** b + b ** a *)


It is important, that a and b are not commutative in general.

Clarification It was suggested in a comment to use SetCommutative[a,b] which achieves the desired result in this case. However, this is the wrong approach as you can see if there is a second operator c with that a and b should not commute:

SetCommutative[a, b]
a**c-c**a
(* 0 *)


This is not desired; it should be -c**a+a**c. SetCommutative sets a and b commutative in general, but they should only commute with each other.

• Why can't you just use SetCommutative[a, b]. It works. Dec 18, 2015 at 13:27
• @Hubble07 In some sense it does, but actually it lets a and b commute with everything. See update Dec 18, 2015 at 13:35
• Check my answer Dec 18, 2015 at 13:54

Setting LeftQ[a, b] = True together with SetCommutingOperators seems to work.

ClearAll["Global*"];
<< NC
<< NCAlgebra

NCE[b ** a - a ** b]

(*-a ** b + b ** a*)

SetCommutingOperators[a, b];
LeftQ[a, b] = True;

NCE[b ** a - a ** b]

(*0*)

NCE[a ** c - c ** a]

(*a ** c - c ** a*)


Update providing explanation for the usage of SetCommutingOperators

Firstly the docs (see pg 74) says

SetCommutingOperators takes exactly two parameters. SetCommutingOperators[b, c] will implement the definitions which follow ...

This means that SetCommutingOperators is not supposed to be used as a standalone command instead it should always be followed by setting LeftQ

Secondly LeftQ determines which of the two operators should be equated to the other.

For e.g.

 SetCommutingOperators[a, b];
LeftQ[a, b] = True;

a ** b
(*a ** b*)

b ** a
(*a ** b*)

SetCommutingOperators[a, b];
LeftQ[a, b] = False;

a ** b
(*b ** a*)

b ** a
(*b ** a*)


So LeftQ is required to be set in order to avoid any ambiguity about which operator is actually equated to the other. This is mentioned clearly in the NOTE section in that same page.

Also the order of operators in LeftQ should match the order in SetCommutingOperators. Read the WARNING section in that page.

Lastly for SetCommutingOperators it doesn't matter if you set LeftQ to either True or False with the correct ordering. I think the True/False matters for SetCommutingFunctions as seen here (scroll to the bottom).

• Thanks alot! Can you explain what the LeftQ actually does? I'd like to understand what it is needed for Dec 18, 2015 at 17:11
• @Lukas see my updated answer Dec 18, 2015 at 18:30
• many thanks! Of course I read the description of LeftQ and also the referenced note. But I wasn't able to make the right out of it. Now it's clear :) Dec 18, 2015 at 19:14

In the newest version of NCAlgebra the command SetCommutingOperators has been much improved. It is implemented using UpValues for efficiency and it no longer uses LeftQ. Instead, it will honor whatever order you give to SetCommutingOperators. For example, after:

<< NC
<< NCAlgebra
SetCommutingOperators[a,b]


the following will have the results:

a ** b == b ** a


True

b ** a - a ** b


0

a ** b


a ** b

b ** a


a ** b

SetCommutingOperators[b,a]


then

a ** b


b ** a

b ** a


b ** a