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

SetNonCommutative[xpt, rp, xq, s]

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 **:

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


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?

  • $\begingroup$ Non commutative multiply is written "*" not "". Read the manual of NC $\endgroup$ Apr 8, 2021 at 11:03
  • $\begingroup$ 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. $\endgroup$ Apr 9, 2021 at 13:29
  • $\begingroup$ Sorry, I meant, as you rightly guessed, "*" (two!) is non commutative and "" is commutative. $\endgroup$ Apr 9, 2021 at 16:19
  • $\begingroup$ 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. $\endgroup$ Apr 11, 2021 at 0:36

1 Answer 1


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:

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:


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

instead of

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.


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.