2011-07-19 22 views
4

如何在Mathematica中定义特殊的算子,例如特殊类型的加法或乘法算子?我过去做过,但我不记得我在哪里放置代码。 我试了两点矩阵定义这个充满小圈子操作:Mathematica中的特殊算符定义

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

但它不工作。我究竟做错了什么?

+0

左手边被解释(如果你把A_'和'\ [FilledSmallCircle]''之间有一个空格)作为'时报[\ [FilledSmallCircle],模式[A,空白[]],模式[B,空白[]]]'。你需要创建一个中缀符号。请参阅下面的各种答案。 – Simon

回答

5

所以你正在试图让一个操作员进行中缀操作。如果你把它比作内置缀运营商+***\[CircleTimes],等等......你“”看到他们都翻译成他们的FullFormPlusTimesNonCommutativeMultiplyCircleTimes,分别。

您应该尝试创建类似的东西。所以与我说的最后一行作为一个包罗了所有当A和B都不是一个矩阵或矢量

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

注启动。

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

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

好多比我 – acl

+0

@acl:谢谢:) – Simon

+0

看来我提交以上......一个逗号一个不完整的注释TRUE'在代码中失踪前'(剪切和粘贴可能的问题) – acl

2

Mathematica有一些没有内建定义的运算符,如CirclePlus和CircleTimes,您可以定义它们。我现在是iPhoning所以我不能检查,但我认为FilledSmallCircle只是一个字符而不是操作符。将它定义为一个操作符并不重要,但您可能需要检查Notation包。

+0

是的:检查Notations软件包 – acl

+0

@sjoerd:我知道FilledSmallCircle只是一个字符而不是操作符。我在这里我想给这个角色提供一个定义.Mathematica有一些没有内建定义的操作符,比如CirclePlus和CircleTimes,你可以定义它们。我的问题是如何在数学 – Phil

+0

实现特定的DEF @Phil也许http://reference.wolfram.com/mathematica/Notation/guide/NotationPackage.html – acl

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位之后,只需剪切并粘贴上面给出的内容即可。

+0

谢谢。我试过了,它给出了错误消息:Notation :: notapatu:Warning:模式A_。被解释为符号而不是模式。如果您希望将此模式视为真正的模式,请使用内嵌的NotationPatternTag TemplateBox包装器。 >> – Phil

+0

@Phil对不起,再试一次(再次剪切并粘贴最后一段代码);我在'A_'和'.'之间添加了一个空格,但不知何故失踪了。 – acl

+0

再试一次:同样的错误信息 – Phil