I have some characters that should be non-commutative, but I do not want to remember which expressions have this behavior when constructing equations.
I had to use MakeExpression to work with raw boxes and automatically increase multiplication by non-commutative multiplication when necessary (for example, when some of the characters are non-commutative objects).
I was wondering if anyone had any experience with this configuration.
Here is what I have so far:
(* Detect whether a set of row boxes represents a multiplication *) Clear[isRowBoxMultiply]; isRowBoxMultiply[x_RowBox] := (Print["rowbox: ", x]; Head[ToExpression[x]] === Times) isRowBoxMultiply[x___] := (Print["non-rowbox: ", x]; False) (* Hook into the expression maker, so that we can capture any \ expression of the form F[x___], to see how it is composed of boxes, \ and return true or false on that basis *) MakeExpression[ RowBox[List["F", "[", x___, "]"]], _] := (HoldComplete[ isRowBoxMultiply[x]]) (* Test a number of expressions to see whether they are automatically \ detected as multiplies or not. *) F[a] F[ab] F[a*b] F[a - b] F[3 x] F[x^2] F[ef*g ** h*ij] Clear[MakeExpression]
This seems to correctly identify expressions that are multiplication operators:
During evaluation of In[561]:= non-rowbox: a Out[565]= False During evaluation of In[561]:= rowbox: RowBox[{a,b}] Out[566]= True During evaluation of In[561]:= rowbox: RowBox[{a,*,b}] Out[567]= True During evaluation of In[561]:= rowbox: RowBox[{a,-,b}] Out[568]= False During evaluation of In[561]:= rowbox: RowBox[{3,x}] Out[569]= True During evaluation of In[561]:= non-rowbox: SuperscriptBox[x,2] Out[570]= False During evaluation of In[561]:= rowbox: RowBox[{e,f,*,RowBox[{g,**,h}],*,i,j}] Out[571]= True
So, it seems that this is not one of the questions that I could conditionally rewrite the boxes of the main expression; but how to do it reliably?
Take the expression RowBox[{"e","f","*",RowBox[{"g","**","h"}],"*","i","j"}]
, this will need to be rewritten as RowBox[{"e","**","f","**",RowBox[{"g","**","h"}],"**","i","**","j"}]
, which seems to be a non-zero operation associated with pattern matching and a set of rules.
I would be grateful for any suggestions from more experienced with me.
I am trying to find a way to do this without changing the default behavior and multiplication order.
Thanks!:)
Joe