# NCAlgebra: is it wrong or do I not understand commuting?

Let me try and do a simple non commutative expansion in NCAlgebra:

SetNonCommutative[xpt, rp, xq, s]
ExpandNonCommutativeMultiply[xpt*rp*rp*xpt*xq*xpt*rp*s*xq]


I get the surprising answer in which some of the symbols do in fact commute

s xq ** xq rp ** rp ** rp xpt ** xpt ** xpt


I can force these symbols to actually not commute by using **:

ExpandNonCommutativeMultiply[
xpt ** rp ** rp ** xpt ** xq ** xpt ** rp ** s ** xq]


giving

xpt ** rp ** rp ** xpt ** xq ** xpt ** rp ** s ** xq


But then what did the SetNonCommutative call even do for me? What is its function? I thought it would cause multiplication to be interpreted non-commutatively but perhaps I am missing something and everything happens via **?

EDIT: @DanielHuber in a comment suggests, I think that my confusion here is thinking that * is noncommutative multiplication rather than **, whcih suggests perhaps that this question is insufficiently clearly stated. ** is AFAICT non-commutative, non-distributive multiplication even without mathematica. With NCAlgebra it become distributive, non-commutative multiplication. However the manual seems to suggest that one can also get non-commutative multiplication by defining symbols to be non-commutative, which does not work as far as I can see. so *What does SetNonCommmutative do? Why do we have it?

• Non commutative multiply is written "*" not "". Read the manual of NC Apr 8, 2021 at 11:03
• Is your comment formatting as intended, @DanielHuber? It sounds like you want to contrast multiplication using *, a single asterisk, and multiplication using an empty string. However my question only uses ** a double asterisk and *, a single asterisk. Apr 9, 2021 at 13:29
• Sorry, I meant, as you rightly guessed, "*" (two!) is non commutative and "" is commutative. Apr 9, 2021 at 16:19
• I see that, much like the NCAlgebra manual, sometimes it can take some helpful community feedback to understand the stackexchange manual. If you need to markup code such as asterisks, I recommend wrapping it in backticks. Apr 11, 2021 at 0:36

Let me try to digest what is going on here.

First, * or blank is the built in commutative product that you should not mess up with.

So if you want to work with NCAlgebra you need to use ** for all non-commutative products.

SetNonCommutative and SetCommutative will then trigger the following behavior:

SetNonCommutative[X,Y]
SetCommutative[Z]
X ** Y ** Z


will evaluate to

Z X ** Y

The symbol Z is commutative and is pulled out of the noncommutative product. If you dig deeper:

FullForm[Z X ** Y]


will give you

Times[Z, NonCommutativeMultiply[X,Y]]

What happened in your example above is the result of a convenience method that is built in NCAlgebra:

X^2


evaluates to

X ** X

This way you can work with noncommutative polynomials using powers. The idea is to allow one to type

X^8 ** Y^2 ** X^16


X ** X ** X ** X ** X ** X ** X ** X ** Y ** Y ** X ** X ** X ** X ** X ** X ** X ** X ** X ** X ** X ** X ** X ** X ** X ** X


Note however that you still should use ** and not *! A side effect of this convenience method is that if you type

X * X * Y


Mathematica will normalize X * X into X^2 which will trigger the NCAlgebra rule to convert the result into

Y X ** X

While this is a valid Mathematica object, it is not a valid NonCommutative object and some but not all NCAlgebra functions will even warn you about it. For example:

NCCollect[X ** X Y, X]


will bark that NCCollect::NotPolynomial: Could not transform expression into nc polynomial.

I hope this helps.