可換でないはずの記号がいくつかありますが、方程式を作成するときにどの式がこの動作をするかを覚えておく必要はありません。
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"}]
私と一緒に経験した人からの提案に感謝します。
デフォルトの動作と乗算の順序を変更せずにこれを行う方法を見つけようとしています。
ありがとう!:)
ジョー