我有一些应该是不可交换的符号,但我不想在构造方程时记住哪些表达式具有这种行为。
我曾想过使用 MakeExpression 作用于原始框,并在适当的时候自动将乘法提升为非交换乘法(例如,当某些符号是非交换对象时)。
我想知道是否有人对这种配置有任何经验。
这是我到目前为止所得到的:
(* 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[a b]
F[a*b]
F[a - b]
F[3 x]
F[x^2]
F[e f*g ** h*i j]
Clear[MakeExpression]
这似乎可以正确识别作为乘法语句的表达式:
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
因此,看起来我可能能够有条件地重写底层表达式的框,这并不是没有问题的;但如何可靠地做到这一点?
以表达式为例RowBox[{"e","f","*",RowBox[{"g","**","h"}],"*","i","j"}]
,这将需要重写,因为RowBox[{"e","**","f","**",RowBox[{"g","**","h"}],"**","i","**","j"}]
这似乎是与模式匹配器和规则集有关的非平凡操作。
我将不胜感激那些对我更有经验的人的任何建议。
我试图找到一种在不改变默认行为和乘法顺序的情况下做到这一点的方法。
谢谢!:)
乔