2
$\begingroup$

I am working on some operator calculation; I do not want the commutation relation.

so xy ≠ yx.

I found the mathematica code from

https://reference.wolfram.com/language/ref/NonCommutativeMultiply.html

and from the example code,

ExpandNCM[(h : NonCommutativeMultiply)[a___, b_Plus, c___]] := Distribute[h[a, b, c], Plus, h, Plus, ExpandNCM[h[##]] &] ExpandNCM[(h : NonCommutativeMultiply)[a___, b_Times, c___]] := Most[b] ExpandNCM[h[a, Last[b], c]] ExpandNCM[a_] := ExpandAll[a] 

so that

ExpandNCM[(a + b) ** (c + z)] 

I got

a ** c + a ** z + b ** c + b ** z 

But the problem is, it does not recognize the real number such that

ExpandNCM[2 (a + b) ** (c + z)] 2 (a + b) ** (c + z) 

It will work if I make it like (Obviously)

2 * ExpandNCM[(a + b) ** (c + z)] 2 (a ** c + a ** z + b ** c + b ** z) 

Would you help me to add some condition that if the variable is Real number, then take it out front?

$\endgroup$

1 Answer 1

5
$\begingroup$

I've seen this done in the NCAlgebra package. The relevant pieces of code can be found in their NCMultiplication.m and are (roughly) these:

Literal[NonCommutativeMultiply[a___, NonCommutativeMultiply[b__], c___]] := NonCommutativeMultiply[a, b, c]; Literal[NonCommutativeMultiply[a___, b_ c_, d___]]:= b NonCommutativeMultiply[a, c, d] /; CommutativeAllQ[b] Literal[NonCommutativeMultiply[a___, b_, c___]] := b NonCommutativeMultiply[a, c] /; CommutativeAllQ[b] Literal[NonCommutativeMultiply[a_]] := a; NonCommutativeMultiply[] := 1; 

where

CommutativeAllQ[x_Symbol] := CommutativeQ[x]; CommutativeAllQ[x_Integer] := True; CommutativeAllQ[x_Real] := True; CommutativeAllQ[x_String] := True; CommutativeAllQ[c_?NumberQ] := True; CommutativeAllQ[f_[x___]] := If[CommutativeQ[f], Apply[And,Map[CommutativeAllQ,{x}]] , False ]; 

where

CommutativeQ[x_Symbol] := True; CommutativeQ[x_Integer] := True; CommutativeQ[x_Real] := True; CommutativeQ[x_String] := True; If[Global`$NC$ForceCommutativeAllQ===True , CommutativeQ[x_] := CommutativeAllQ[x]; , CommutativeQ[x_] := True; ]; 
$\endgroup$
0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.