2
$\begingroup$

Suppose my expressions are given as a combinations of non-commutative products, i.e., $2 X**Y**Z + 3 Y**Z**X$, I want to make this as commutative product and get $5 X**Y**Z$. And want similar things for higher length operations, i.e., $X**Y**Z**W**K$.

I have a long list of inputs with $X**Y**Z**\cdots$ and my first trial was just replacing $X**Y \mapsto X*Y$ or $Y**X \mapsto X**Y$ but i realized it did not worked well and i need a lots of expressions for practical use.

$\endgroup$
1
  • 2
    $\begingroup$ It is not clear what you want. For a non commutative multiply xy is not equal to yx. If you want to set them equal you may simply replace non-commutative-multiply by the ordinary multiply. $\endgroup$ Commented Jul 31, 2022 at 8:56

1 Answer 1

5
$\begingroup$

You can simply replace NonCommutativeMultiply by Times as follows:

2X**Y**Z+3Y**Z**X/.NonCommutativeMultiply->Times (* 5X Y Z *) 

Alternatively, you can use your approach with patterns:

2X**Y**Z+3Y**Z**X//.x_**y_:> x y (* 5X Y Z *) 

(Note the use of ReplaceRepeated (//.) to repeatedly apply the replacement until the result doesn't change anymore)

Finally, you could use BlankSequence (__) in the pattern to replace arbitrarily long products in one step:

2X**Y**Z+3Y**Z**X/.x_**y__:> x y (* 5X Y Z *) 
$\endgroup$

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.