此为 Mathematica 7 文档,内容基于更早版本的 Wolfram 语言
查看最新文档(版本11.1)

Notation, Symbolize and InfixNotation

Notation

Notation[boxes DoubleLongLeftRightArrow expr]set boxes to be parsed to expr and expr to be formatted as boxes
Notation[boxes DoubleLongRightArrow expr]set boxes to be parsed to expr
Notation[boxes DoubleLongLeftArrow expr]set expr to be formatted as boxes
Notation[notation, opts]define a notation notation with the options opts
RemoveNotation[notation]remove the notation notation

Syntax of notation declarations.

Notation takes both an external representation and an internal representation as arguments. Mathematica translates any input matching the external representation into the corresponding internal representation and, reciprocally, formats any expression matching the internal representation into the corresponding external representation. In this context, representation means a composite structure made up of boxes representing some "notation".
This loads the package.
In[5]:=
Click for copyable input
The following declares a new notation for gplus.
In[6]:=
Click for copyable input
Any input matching x_ CirclePlusn_ y_ is now interpreted as gplus[x, y, n].
In[7]:=
Click for copyable input
Out[7]//FullForm=
Any gplus expression is now formatted in the new notation.
In[8]:=
Click for copyable input
Out[8]=
Notations defined using DoubleLongLeftRightArrow in their definition both parse and format expressions according to the given notation. However, you can restrict the notation to only parsing or only formatting by using DoubleLongRightArrow or DoubleLongLeftArrow respectively, instead of DoubleLongLeftRightArrow in your notation statements.
This defines a notation for the parsing of a hypothetical DomainIntegral.
In[9]:=
Click for copyable input
DomainIntegrals are now parsable.
In[10]:=
Click for copyable input
Out[10]=
The following defines an output format for Derivative objects that looks more like that of traditional mathematics.
In[7]:=
Click for copyable input
Derivatives are now formatted according to the new derivative notation.
In[9]:=
Click for copyable input
Out[9]=
To allow the previous output to be used as input you can define an interpretation of partial derivatives.
In[10]:=
Click for copyable input
You can now use these new notations for derivatives.
In[12]:=
Click for copyable input
Out[12]=
You should define your notations in such a way that they both parse and format, since users will generally expect this functionality.
The following notation both formats and parses arrows overscripted by Apply.
In[13]:=
Click for copyable input
Simple rules like linearity can now be entered in a visually intuitive way.
In[14]:=
Click for copyable input
In[15]:=
Click for copyable input
Out[15]=
You may at first feel that having underscores on both sides of a Notation statement is somewhat unsettling. However, notational transformations usually work in both directions, therefore having underscores on both sides of a notation statement is natural, and soon becomes intuitive.

Symbolize

Symbolize[boxes]treat boxes as a symbol
Symbolize[boxes, opts]treat boxes as a symbol with the options opts
RemoveSymbolize[boxes]remove the treatment of boxes as a symbol

Syntax of symbolization declarations.

Symbolize is used to treat a class of composite boxes internally as symbols. It is sometimes necessary and desirable to be able to symbolize composite objects; for example, to be able to attach rules to a composite object, it must be treated as a symbol.
This declares that the composite object ScriptCapitalDScriptOScriptMScriptAScriptIScriptNPartialD should be treated as a symbol.
In[22]:=
Click for copyable input
The package has created a FullForm name for the composite symbol ScriptCapitalDScriptOScriptMScriptAScriptIScriptNPartialD.
In[23]:=
Click for copyable input
Out[23]//FullForm=
You can now attach rules to the composite symbol ScriptCapitalDScriptOScriptMScriptAScriptIScriptNPartialD.
In[15]:=
Click for copyable input
The above rule for ScriptCapitalDScriptOScriptMScriptAScriptIScriptNPartialD is now tried before the rules of DomainIntegrals.
In[16]:=
Click for copyable input
Out[16]=
Symbolize also symbolizes classes of expressions, for example, to symbolize all objects subscripted by Rule.
In[20]:=
Click for copyable input
Anything subscripted by Rule is now treated as a symbol.
In[21]:=
Click for copyable input
Out[21]//FullForm=
You can use symbolized box structures as pattern variables in definitions. This lets you create formulas in a more familiar notation. However, you must use pattern expressions of the form CompositePatternVariable : PatternContent otherwise Mathematica may interpret the pattern expression as CompositePatternVariable Star PatternContent.
This defines the transmission coefficient for an incident wave packet on a potential step under certain conditions.
In[22]:=
Click for copyable input
In[23]:=
Click for copyable input

InfixNotation

InfixNotation[op, func]treat op as an infix operator representing the function func
InfixNotation[op, func, opts]treat op as an infix operator representing the function func with the options opts
RemoveInfixNotation[op, func]remove the composite infix operator op

Syntax of infix notation declarations.

InfixNotation is used to treat a composite box structure as an infix operator. InfixNotation requires both a composite box object which will represent the infix operator and a symbol which will be the full form head of the expression. A simple parallel of this duality in Mathematica is that the infix notation + has the full form head Plus.
This declares that the composite object +{} should act as the infix form of Join.
In[23]:=
Click for copyable input
In[24]:=
Click for copyable input
Out[24]=
The infix notation +{} both parses input and formats output.
In[25]:=
Click for copyable input
Out[25]=
An advantage of using InfixNotation over that of using Notation to define an infix operator is that InfixNotation parses an expression into a flat internal form without evaluation. Although this is a subtle difference, it is an important one (see ring operations and parsing without evaluation.)
InfixNotation parses input to a flat expression.
In[26]:=
Click for copyable input
Out[26]//FullForm=
The function InfixNotation can be compared to using the infix form of functions ~ScriptF~ (cf. "Special Ways to Input Expressions")

Entering Notations

When defining your own notations it is critically important to use the notation templates. The notation templates are contained in the notation palette shown below.
1.gif
The reason for this restriction is that the templates pasted into a notebook contain essential tag boxes embedded in the correct way. These embedded tag boxes allow Notation , Symbolize and InfixNotation to properly group and parse the new notation being defined. Further, they allow these functions to get the styling information of the new notation so that the output is formatted with the same spacing, sizes, adjustments, etc. as the input.
A blank notation template looks like the following.
Click for copyable input

Advanced note: You can circumvent using the notation palette by wrapping the corresponding literal box structures in a ParsedBoxWrapper.

In Mathematica it is possible to create input aliases for any notation or expression you want. The normal interface to these input aliases is rather cumbersome and so the notation package contains the function AddInputAlias to facilitate adding input aliases to the current notebook. Each of the common templates has an alias associated with it for easy entry. For instance typing EscapeKeynotationEscapeKey in an input cell will enter the notation template Notation[ DoubleLongLeftRightArrow ].
Add an input alias for apply statements to the current notebook.
In[27]:=
Click for copyable input
Now that we have created this alias we can enter "apply" templates by simply typing EscapeKeyapplyEscapeKey.