特殊运算符在Mathematica中的定义

时间:2011-07-19 19:50:25

标签: wolfram-mathematica operators definition

如何在Mathematica中定义一个特殊运算符,例如一种特殊类型的加法或乘法运算符?我过去做过,但我不记得我把代码放在哪里了。 我尝试在两个矩阵上定义这个填充的小圆算子:

A_\[FilledSmallCircle] B_ := 
  Which[(MatrixQ[A] || VectorQ[A]) && (MatrixQ[B] || VectorQ[B]), 
   A.B, ! (MatrixQ[A] || VectorQ[A]) && (MatrixQ[B] || VectorQ[B]), 
   A@B, (MatrixQ[A] || VectorQ[A]) && ! (MatrixQ[B] || VectorQ[B]), 
   Transpose[B@Transpose[A]]];

但它不起作用。我做错了什么?

3 个答案:

答案 0 :(得分:5)

因此,您尝试使用中缀操作创建运算符。如果您将其与内置中缀运算符+***\[CircleTimes]等进行比较......您会发现它们都被解释为FullFormPlusTimesNonCommutativeMultiplyCircleTimes

您应该尝试创建类似的东西。所以从

开始
BigDot[A_, B_] := Which[
          (MatrixQ[A] || VectorQ[A]) &&  (MatrixQ[B] || VectorQ[B]), A.B, 
         !(MatrixQ[A] || VectorQ[A]) &&  (MatrixQ[B] || VectorQ[B]), A@B, 
          (MatrixQ[A] || VectorQ[A]) && !(MatrixQ[B] || VectorQ[B]), Transpose[B@Transpose[A]],
          True, HoldForm[BigDot[A, B]]];

请注意,当A和B都不是矩阵或向量时,我将最后一行添加为全能。

然后创建中缀符号部分。困难的方法是制定一些MakeExpressionMakeBoxes定义。简单的方法是使用NotationPackage

Needs["Notation`"]
InfixNotation[ParsedBoxWrapper["\[FilledSmallCircle]"], BigDot]

答案 1 :(得分:4)

尝试(只需剪切并粘贴):

Needs["Notation`"]

Notation[ParsedBoxWrapper[
RowBox[{"A_", " ", "\[FilledSmallCircle]", " ", 
     "B_"}]] \[DoubleLongLeftRightArrow] ParsedBoxWrapper[
RowBox[{"Which", "[", 
RowBox[{
RowBox[{
RowBox[{"(", 
RowBox[{
RowBox[{"MatrixQ", "[", "A_", "]"}], "||", 
RowBox[{"VectorQ", "[", "A_", "]"}]}], ")"}], "&&", 
RowBox[{"(", 
RowBox[{
RowBox[{"MatrixQ", "[", "B_", "]"}], "||", 
RowBox[{"VectorQ", "[", "B_", "]"}]}], ")"}]}], ",", 
RowBox[{"A_", " ", ".", "B_"}], ",", 
RowBox[{
RowBox[{"!", 
RowBox[{"(", 
RowBox[{
RowBox[{"MatrixQ", "[", "A_", "]"}], "||", 
RowBox[{"VectorQ", "[", "A_", "]"}]}], ")"}]}], "&&", 
RowBox[{"(", 
RowBox[{
RowBox[{"MatrixQ", "[", "B_", "]"}], "||", 
RowBox[{"VectorQ", "[", "B_", "]"}]}], ")"}]}], ",", 
RowBox[{"A_", "[", "B_", "]"}], ",", 
RowBox[{
RowBox[{"(", 
RowBox[{
RowBox[{"MatrixQ", "[", "A_", "]"}], "||", 
RowBox[{"VectorQ", "[", "A_", "]"}]}], ")"}], "&&", 
RowBox[{"!", 
RowBox[{"(", 
RowBox[{
RowBox[{"MatrixQ", "[", "B_", "]"}], "||", 
RowBox[{"VectorQ", "[", "B_", "]"}]}], ")"}]}]}], ",", 
RowBox[{"Transpose", "[", 
RowBox[{"B_", "[", 
RowBox[{"Transpose", "[", "A_", "]"}], "]"}], "]"}]}], "]"}]]]

现在我已经使用Notation调色板输入了这个,所以实际上它在屏幕上看起来像这样: enter image description here (调色板在必要时插入各种框)。由于所有内容的显式字符串表示,我剪切和粘贴时看起来很糟糕。

编辑:那就是:输入"Needs["Notation“]`,导致出现调色板。点击第一个按钮,然后这个

enter image description here

出现。在第一个黄色框中输入A_ \[FilledSmallCircle] B_,在第二个黄色框中输入

Which[(MatrixQ[A_]||VectorQ[A_])&&(MatrixQ[B_]||VectorQ[B_]),A_ .B_,!(MatrixQ[A_]||VectorQ[A_])&&(MatrixQ[B_]||VectorQ[B_]),A_[B_],(MatrixQ[A_]||VectorQ[A_])&&!(MatrixQ[B_]||VectorQ[B_]),Transpose[B_[Transpose[A_]]]]

结果如下所示 enter image description here

并且在评估时定义您想要的内容。或者,在Needs位之后,只需剪切并粘贴上面给出的内容。

答案 2 :(得分:2)

Mathematica有一些运营商没有您可以定义的CirclePlus和CircleTimes等内置定义。我现在是iPhoning所以我无法检查,但我认为FilledSmallCircle只是一个角色而不是操作员。将它定义为运算符并不简单,但您可能需要检查Notation包。