Notations and Objects in WL
Notations and Objects in WL
Bignya Ranjan Pathi *
Jayanta Padhikar *
* Wolfram Technical Consulting
Jayanta Padhikar *
* Wolfram Technical Consulting
#WolframTechConf
Outline
Outline
Evaluation Model
Box Language
Notations
Objects
Evaluation Model
Evaluation Model
In[]:=
20
30
Rendering/FrontEnd Typesetting
The rendering of the box expressions in the FE, and modification of the box expressions in response to the user actions.
Formatting/Kernel Typesetting
The conversion of Wolfram Language expressions to the box language for transmission to the FE.
Parsing/Kernel Untypesetting
The conversion of box expressions sent by the FE into normal expressions to be evaluated by the kernel.
Boxes in Mathematica
Boxes in Mathematica
Boxes are 2D typesetting structures that represents all WL expressions including graphics objects, dynamic objects, control objects, etc.
To see the underlying box structure of any expression, select the cell and choose Cell ▶ Show Expression (Ctrl+Shift+E):
Simple Expressions
Simple Expressions
2+2(*InputCell*)
Cell[RowBox[{"2","+","2"}], "Input", ...] (* Corrsponding Cell Expression *)
1+x+x^2(*InputCell*)
(* Corrsponding Cell Expression *)Cell[RowBox[{"1","+","x","+",SuperscriptBox["x","2"]}], "Input", ...]
Graphics Objects
Graphics Objects
Plot[Sin[x],{x,0,10}]
In[]:=
Graphics[Disk[]]
Dynamic Objects
Dynamic Objects
In[]:=
Slider[]
In[]:=
Manipulate[Plot[Sin[x(1+ax)],{x,0,6}],{a,0,2}]
Box Generator
Box Generator
Even though the underlying structures in WL are boxes, we do not have to interact with them directly.
There is a high level interface called Box Generator that generates different types of boxes and allow us to use these powerful low-level boxes.
Styling Outputs
Styling Outputs
In[]:=
Style["See the Box structure",Bold,10,FontFamily"Helvetica"]
Mathematical Typesetting
Mathematical Typesetting
In[]:=
{Subscript[a,b],Superscript[a,b],Underscript[a,b],Overscript[a,b],Subsuperscript[a,b,c],Underoverscript[a,b,c]}
Grid Layout
Grid Layout
Grid[{{1,2},{3,4}}]
And many more
Some Notable Boxes
Some Notable Boxes
There are about 100 boxes in total including , , DynamicBox etc.
Examples of Notable Boxes
Examples of Notable Boxes
SubscriptBox
SuperscriptBox
FractionBox
RowBox
GridBox
StyleBox
Examples of Notable Boxes (Cont....)
Examples of Notable Boxes (Cont....)
InterpretationBox
InterpretationBox
Helps in creating specially formatted output for any expressions.
If we copy the above output “myVar” and evaluate, it will be interpreted as 3.
The top level function which uses InterpretationBox is Interpretation
TagBox
TagBox
Used to assign tags to box structures to aid interpretation. For Example:
Without the TagBox, this notation would be interpreted as 1/f:
Another use case of TagBox
Examples of Notable Boxes(Cont....)
Examples of Notable Boxes(Cont....)
TemplateBox
TemplateBox
We can copy the above output and evaluate it to get back the original form.
Two major components of TemplateBox are DisplayFunction and InterpretationFunction
DisplayFunction specifies the function used to construct boxes.
InterpretationFunction specifies the function used for evaluation.
If the InterpretationFunction is set to Automatic, the tag will be used as the function head of the box structure
Tags can be used to create a pre-defined template box structure
TemplateBoxes can be nested as well
Dynamic TemplateBox
Create a box that displays as a popup menu and evaluates as the selected value:
TemplateBox along with MakeExpression and MakeBoxes can be used to define new notations.
Conversion between Expression and Box
Conversion between Expression and Box
Construct boxes to represent expression in the specified form.
Generates boxes without evaluating its input.
Construct an expression corresponding to boxes.
Generates boxes corresponding to the output in the specified form.
Generates boxes after evaluating its input.
Gives the expression obtained by interpreting strings or boxes as Wolfram Language input.
Make a cell from these:
Forms of Input and Output
Forms of Input and Output
In Wolfram System notebooks, expressions are by default output
Two dimensional notation
This typically requires the WL frontend.
Yields a form that can be typed directly on a keyboard
One-dimensional notation
Useful for test based interfaces (no WL frontend)
Shows the internal form of an expression in explicit functional notation
Uses a large collection of ad hoc rules to produce an approximation to traditional mathematical notation
TraditionalForm Vs StandardForm
TraditionalForm does not have the precision of StandardForm.
Hence, there is in general no unambiguous way to go back from a TraditionalForm representation.
Custom Notation
Custom Notation
For any type of custom notations, we would like to have the following properties:
It should be evaluatable as other WL expressions
It should be nicely represented in its output format
The next few sections explain how to construct custom notations
1. Notations
1. Notations
2+2 -> Plus[2, 2] -> 4
Here, the operator “+” is associated with the built-in function Plus.
However, the above is not true for all operators recognized by the Wolfram Language.
Custom Notation (Cont.....)
Custom Notation (Cont.....)
2. Output Formats
2. Output Formats
WL also allows us to define how expressions should be formatted for output.
Format
Format
MakeBoxes and MakeExpression
MakeBoxes and MakeExpression
However, this can not be interpreted as input
An important difference between MakeBoxes and Format is that the former does not evaluate its argument, so you can define rules for formatting expressions without being concerned about how these expressions would evaluate.
MakeBoxes and Interpretation
MakeBoxes and Interpretation
Notation for Special Functions
Notation for Special Functions
Special functions do not format in any specific way in the StandardForm. Consider the Gamma function:
It does have the specific format in the TraditionalForm:
You can make your custom notations for such special functions.
Check the box structure:
Notation for Special Functions
Notation for Special Functions
Create your own boxes for representing gamma functions by following the structure above:
Assign the appearance function to Gamma:
Now the Gamma function formats in StandardForm:
It continues to work as expected with numerical values:
We can copy and paste the notation from the cell above and insert our own value to get the result:
Note that if we directly typeset the form using CapitalGamma, it won’t work. Since the special box structure with TemplateBox is required for the interpretation.
Add an input alias:
Notation Package
Notation Package
The Notation Package provides functionality for introducing new notations easily, intuitively, and graphically.
Set boxes to be parsed as expressions
Set expressions to be formatted as boxes
Remove the notations
Both formatting and parsing
Notation Package (Cont..)
Notation Package (Cont..)
Notation Package (Cont..)
Notation Package (Cont..)
Symbolize
Composite boxes can be treated as a symbol.
InfixNotation
Composite box structure can be treated as Infix Operator.
It parses the Input and formats the output
Objects
Objects
Many of the built-in functions in WL generate objects as output. These are called elided forms.
A few examples are:
The advantages are:
Information/Properties are presented in a nice and compact form.
It can contain large amount of data.
It can be used as input to other function like any other WL expression.
If it is saved in a notebook, all the information and data will be saved along with the object.
Objects (Cont...)
Objects (Cont...)
We can access the information stored in the elided forms as well. Let us take an example of a TimeSeries object:
TimeSeries
TimeSeries
LinearModelFit
LinearModelFit
Objects (Cont..)
Objects (Cont..)
For some of the objects, we can copy the box structure and use it as an input to another function:
DateObject, TimeObject, ..
TimeSeries, TemporalData, ..
For some of the objects, we can not copy the box structure to use it as an input to another function:
Interpolation, LisInterpolation ..
SparseArray, ..
However, we can assign it a variable and then use it to access it’s properties or pass it as an input to other functions.
Custom Objects
Custom Objects
myPower
myPower
Code
Code
Usage
Usage
Limitations
1
.The output object itself is not nice looking. Also exposes internal information unnecessarily.
2
.Doesn’t allow for meaningful operations on the object:
Compare with Around:
Custom Object (Cont..)
Custom Object (Cont..)
myPower1
myPower1
Code
Code
Usage
Usage
Properly formatted display:
Works like the previous version:
Note z above doesn’t give 10^k + 20^k
Custom Object (Cont..)
Custom Object (Cont..)
Formatted grid object
Formatted grid object
Code
Code
Usage
Usage
Summary Box
Summary Box
Summary Box are another type of elided form that summarize the available results and corresponding properties as a result of some evaluation.
Built-in Examples
Built-in Examples
Extract properties/attributes
Extract properties/attributes
Use it in other function
Use it in other function
Construct Summary Box
Construct Summary Box
Add UpValue
Add UpValue
Add DownValue
Add DownValue
Add FormatValue
Add FormatValue
Custom Summary Box
Custom Summary Box
Object Oriented Programming
Object Oriented Programming
Object oriented programming is another programming paradigm that can be beneficial in certain cases.
Collections of data elements treated as one unit and functions are considered part of it. The combination of both is called an Object.
The functions associated with each data objects are called methods. Usually, these methods are not defined for each object separately. Instead, they are put together in a class.
It is possible that different data types have the same characteristics or methods. These can be bundled into an abstract data type and then made available for others. This is called inheritance.
Another important feature of the OOPs is abstraction which enables to hide the actual implementation from users and expose the essential functionalities only.
Sometimes, it is desirable to have one method behave differently for different data types. This feature is called polymorphism.
Object Oriented Programming
Object Oriented Programming
Object Oriented Programming
Object Oriented Programming
Mutability
Mutability
Most data structures in WL are immutable.
Constructing Mutable Structures
New DataStructure objects
Options in Options Inspector
Some of the Graph Functionalities
Reference
Reference