Mathematicas NonCommutativeMultiply (**) does not simplify terms like
a**0=0**a=0
a**1=1**a=a
or
a**a=a^2.
I would like to redefine **
to do this. I was using NCAlgebra to do this but I need ReplaceRepeated (//.) and NCAlgebra, as their documentation says, specifically breaks this functionality in mathematica.
Can some show me how to Clear the attributes of **
and redefine this multiplication do the same things it would normal do plus dealing with 1 and 0. I really do not need the multiplication to deal with a**a
, but It would be nice if it is simple enough. The main thing I need **
to deal with 1 and 0.
The below only works if you remove the Flat attribute of NonCommutativeMultiply (Which is something I did by mistake during testing... a rookie mistake!)
The simplest thing to do is
Unprotect[NonCommutativeMultiply];
NonCommutativeMultiply[a___, 1, b___] := a ** b
NonCommutativeMultiply[___, 0, ___] := 0
NonCommutativeMultiply[a_] := a
Protect[NonCommutativeMultiply];
The final expression is needed so that a**1
simplifies to a
instead of NonCommutativeMultiply[a]
. You might also need NonCommutativeMultiply[]:=1
so that expressions like 1**1
simplify properly (*).
The only problem with all of this, is for large expressions, the pattern is checked against everything and this gets really slow.
The above two definitions for 0 and 1 can be combined and generalized to
NonCommutativeMultiply[a___, n_?NumericQ, b___] := n a ** b
which factors out any numerical terms inside the expression. But this slows down things even more in large expressions, since each term is checked to see if its numerical.
To simplify your a**a
to a^2
, you need something like
NonCommutativeMultiply[a___, b_, b_, c___] := a ** b^2 ** c
or more generally
NonCommutativeMultiply[a___, b_^n_., b_^m_., c___] := a ** b^(n + m) ** c
(*) Note that this is only because the default order that Mathematica puts its DownValues
in is not necessarily the best in this case. Change the order so that NonCommutativeMultiply[a_]
comes before a___ ** n_?NumericQ ** b___
then NonCommutativeMultiply[]
won't be generated by the rules, and you won't need that last pattern (unless you produce NonCommutativeMultiply[]
some other way).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With