Programming
in
 Mathematica

Dipl.-Ing. Dr. Wolfgang Windsteiger

RISC-Linz
A-4232 Hagenberg

Introduction

In this course, we present the Computer Algebra System Mathematica with an emphasis on its programming language. The features of the system are investigated and we try to show how the potential of the system can be used for modelling and solving mathematical problems and for visualizing eventual results.
In practice, it has often been observed that the real power of Mathematica can only be exploited if the user is able to use the system's programming language in order to adapt available algorithms to one's personal needs in a certain context. Therefore, after introducing basic system features, we will concentrate on the following aspects of Mathematica as a programming environment:

Datastructures in Mathematica

Pattern-matching

Recursive and procedural programming

Rule-based programming

Graphics

Communication with other programs: MathLink

GUI programming: Active elements, notebooks, buttons, palettes, etc.

etc. etc.

©    Wolfgang Windsteiger 2007

User Interface: The Mathematica Notebook

Mathematica consists of two separate units, the Kernel and the FrontEnd. The kernel is responsible for all sorts of computation, the frontend is responsible for communication with the user, i.e. input and output. Kernel and frontend are separate entities and they communicate bi-directionally. A Mathematica session permanently loops through the following cycle:

User input in the frontend

Frontend sends data to kernel

Kernel evaluates request and generates a result

Kernel sends result to frontend

Frontend displays result.

Independently from the concrete operating system Mathematica is running (MS-Windows, Apple, Unix/X-Windows), the user frontend is the so-called Mathematica Notebook. When launching Mathematica, what one typically gets is a big empty "sheet" and some menues. The "sheet" is called "Notebook". A notebook is a document that may contain Mathematica input, output, and graphics, it can be saved in a file, it can be printed, sent be email, etc.
A Mathematica notebook is built up from cells that can be nested, a notebook's cell structure is indicated by the blue "brackets" on the right margin of the ducument. The key feature of nested cells is that they can be opened and closed by mouse-click, where closing a nested group of cells has the effect of hiding the contents of all cells but the first in the group. Opening a closed cell group will eventually recover the content of previously hidden cells. Menues allow further operations on the cell structure, like e.g. merging, dividing, grouping, etc. Cells can have different types and the type of a cell determines it's behaviour and its appearance. Most importantly there are input cells and output cells. Input cells may contain Mathematica input (what a surprise!) that can be sent to the kernel for evaluation by a certain keystroke, on most platforms the combination "Shift-Return" (⇧+↵) works, sometimes also ⌤ is supported. The kernel will then compute the result and send it back to the frontend, which will then display the result in an output cell. Input and corresponding output are automatically grouped.
Frontend menues allow various operations and configurations, notably there is the HelpBrowser and the OptionInspector that can be accessed via frontend menues.

Exercise

Launch
1) the Mathematica kernel (in Unix this is usually done by calling "math" in a shell, in other systems there should be an application called "MathKernel" or similar),
2) the Mathematica frontend.

When a notebook is stored it is stored in ASCII-format and it is platform-independent. This means that a notebook produced e.g. on a Macintosh can easily opened on Unix or Windows and it preserves full functionality also on the "foreign" platform.

The Help Browser

As already mentioned above, the frontend provides a Help facility, the so-called Help Browser.
The Help browser not only describes functions from standard Mathematica, but also functions in third-party add-on packages. The description contains call syntax, textual description, many examples (actively evaluateable), and hyperlinks to related topics.
Open the help browser and experiment …

Inputting Special (Mathematical) Symbols

The Mathematica notebook allows special symbols and two-dimensional structures in input and output. Entering special input can usually be established in various ways.

Palettes

Palettes contain predefined input patterns grouped into various categories. Predefined palettes are available in the menue

File -> Palettes

there is even a possibility for user-defined palettes. Clicking a button in a palette usually pastes the displayed pattern into the notebook at the current cursor position. If a pattern contains a ■, then the currently selected expression is filled into this position. Other positions can be indicated with □, and one can easily navigate between input positions using the ⇥ key.

Keyboard

Most of the special symbols (e.g. greek letters, ∑, ∫, etc.) can be entered via escape-sequences on the keyboard. Each symbol has an abbreviation, enclosing its abbreviation in Esc produces the symbol directly.
Example: the abbreviation for "α" is simply "a", hence EscaEsc will produce α. (The abbreviations for symbols can be found in some of the palettes. Practice makes perfect …)

Menu

Last but not least, there is the menu

Edit -> Expression Input

which also provides help for inputting special syntax.