With the help of some very competent stackoverflow members in this post , I have the following new definition for NonCommutativeMultiply (**) in Mathematica:
Unprotect[NonCommutativeMultiply];
ClearAll[NonCommutativeMultiply]
NonCommutativeMultiply[] := 1
NonCommutativeMultiply[___, 0, ___] := 0
NonCommutativeMultiply[a___, 1, b___] := a ** b
NonCommutativeMultiply[a___, i_Integer, b___] := i*a ** b
NonCommutativeMultiply[a_] := a
c___ ** Subscript[a_, i_] ** Subscript[b_, j_] ** d___ /; i > j :=
c ** Subscript[b, j] ** Subscript[a, i] ** d
SetAttributes[NonCommutativeMultiply, {OneIdentity, Flat}]
Protect[NonCommutativeMultiply];
This multiplication is great, but it does not concern negative values ββat the beginning of the expression, i.e. a**b**c + (-q)**c**a
should simplify a**b**c - q**c**a
and it will not be.
In my multiplication, the variable q (and any integer scalability) is commutative; I am still trying to write the SetCommutative function without success. I do not need SetCommutative , it would be just nice.
It would also be useful if I could pull all q's to the beginning of each expression, i.e. :: a**b**c + a**b**q**c**a
should simplify:
a**b**c + q**a**b**c**a
and similarly, combining these two questions:
a**b**c + a**c**(-q)**b
should simplify:
a**b**c - q**a**c**b
Currently, I would like to figure out how to deal with these negative variables at the beginning of the expression and how to pull q's and (-q)'s to the front, as mentioned above. I tried to solve the two issues mentioned here using ReplaceRepeated (\\.) , But so far I have not been successful.
Any ideas are welcome, thanks ...
Cantormath
source share