Noncommutative multiplications and negative coefficients at the beginning of an expression in mathematics

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 ...

+3
source share
2 answers

The key to this is the realization that Mathematica represents ab as a+((-1)*b) , as you can see from

 In[1]= FullForm[ab] Out[2]= Plus[a,Times[-1,b]] 

In the first part of your question, all you have to do is add this rule:

 NonCommutativeMultiply[Times[-1, a_], b__] := - a ** b 

or you can even catch a sign from any position:

 NonCommutativeMultiply[a___, Times[-1, b_], c___] := - a ** b ** c 

Update - Part 2. A common problem with getting scalars in front of the front is that the _Integer pattern in your current rule will only determine what is explicitly integer. This does not even mean that q is an integer in a construct of type Assuming[{Element[q, Integers]}, a**q**b] .
To achieve this, you need to study assumptions, a process that is probably expensive to be put in the global transformation table. Instead, I would write a conversion function that I could apply manually (and possibly remove the current rule from the global table). Maybe something like this:

 NCMScalarReduce[e_] := e //. { NonCommutativeMultiply[a___, i_ /; Simplify@Element [i, Reals],b___] :> ia ** b } 

The above rule uses Simplify to explicitly request assumptions that you can set globally by assigning $Assumptions or locally using Assuming :

 Assuming[{q \[Element] Reals}, NCMScalarReduce[c ** (-q) ** c]] 

returns -qc**c .

NTN

+3
source

Just a quick answer repeating some comments from the previous question. You can remove a couple of definitions and solve all parts of this question using the rule on Times[i,c] , where i is commutative and c is the default value of Sequence[]

 Unprotect[NonCommutativeMultiply]; ClearAll[NonCommutativeMultiply] NonCommutativeMultiply[] := 1 NonCommutativeMultiply[a___, (i:(_Integer|q))(c_:Sequence[]), b___] := ia**Switch[c, 1, Unevaluated[Sequence[]], _, c]**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]; 

It works as expected.

 In[]:= a**b**q**(-c)**3**(2 a)**q Out[]= -6 q^2 a**b**c**a 

Note that you can generalize (_Integer|q) to work with more general commutative objects.

+2
source

All Articles