In my recent study, I encounter the following problem:

{A,B,C,D}::NonCommuting.
ex:=A B (a-b)+B A (b-a)+A C (a-c)+C A (c-a)+B C (b-c)+C B (c-b);


How to simplify the expression above? i.e. get the result
$$(AB-BA)(a-b)+(AC-CA)(a-c)+(BC-CB)(b-c)$$

edited

Remember that you need to write either a space or a * character to indicate products. Also, NonCommuting is spelled with a single t.

What do you want to do with this? You can distribute(_) this, but there isn't much else you can simplify given that the capital symbols do not commute with each other.

Sorry for my typo. I want to extract the scalars in the following expression (the matrices outside the brackets are noncommuting)

I have written following code

substitute(_,$A?? (B??)+ C?? (D??)|B??+D??=0->(A??-C??) B??$);


but it's useless.

I don't understand what you want to do. Can you write down the expression that you would like to end up with?

I have known how to solve it. Only need to combine multiplythrough ,zoom and factorout.

For example:

{a,b}::SortOrder.
{A,B}::NonCommuting.
ex:=A B (a-b)+B A (b-a);
import cdb.core.manip as manip
zoom(_,$B A A??$);
distribute(_);
manip.multiply_through(_,$-1$);
factor_out(_,$A,B$);
manip.multiply_through(_,$-1$);
sort_sum(_);
unzoom(_);
factor_out(_,$a-b$);


But I have no ideas how to solve my practical problem simply. .