Mathematica®:
Summary of the Front End





This chapter describes how to start up Mathematica on the NeXT Computer and briefly introduces you to preparing input, getting on-line help, doing calculations, and using a remote computer.

The Mathematica system has two parts: a Kernel, which does computations, and a Front End, which handles interaction with the user.  This booklet describes some of the features of the NeXT Front End for Mathematica.

The main documentation for the Mathematica Kernel is the book Mathematica: A System for Doing Mathematics by Computer, written by Stephen Wolfram, and published by Addison-Wesley.  This book is hereafter referred to as The Mathematica Book.  You cannot expect to be able to use Mathematica without a copy of this book.  The book is available in most technical bookstores, and can be ordered directly from Addison-Wesley by calling 800-447-2226.




Starting Mathematica

Begin by installing Mathematica, opening it, and trying a simple calculation.

1. To start up Mathematica with the NeXT Front End, double click the Mathematica icon in
the dock.  If Mathematica isn't in your dock, double-click Mathematica.app in /NextApps
to start it.
2. Once Mathematica is running, type

2+2

Then press Enter (or Shift-Return, or Command-Return).

If in a few seconds you get the answer 4, you can assume that things are working.




Things to Try

To do calculations with Mathematica you'll need to learn about the language used by the Mathematica kernel.  Refer to the book Mathematica:  A System for Doing Mathematics by Computer.  In the meantime, here are a few things you can try:

Enter the following expression:

100!

Then press Shift-Return.  Your result will be the value of 100    99    . . .    1.

Enter the following expression (note capitalization):

Expand[(1 + x)^10]

Then press Shift-Return. This expands the algebraic expression (1 + x)10.

Enter the following expression (note capitalization):

Plot[Sin[x^2], {x, 0, 5}]

Then press Shift-Return.  This will plot a curve.

Enter the following expression:

Plot3D[Sin[x y], {x,0,3}, {y,0,3}]

Then press Shift-Return.  (Don't forget the space between x and y in
Sin[x y].  The space denotes multiplication.)  After Mathematica runs this calculation, you should see something like this:




Producing this picture will take about 13 seconds.  If you have a color monitor, or if you are preparing graphics output for rendering on a color device, add a Lighting option to the expression (use a hyphen or minus sign followed by a "greater than" sign to make the arrow):

Plot3D[Sin[x y], {x, 0, 3}, {y, 0, 3}, Lighting->True]

This will make a three-dimensional color picture.

Note that you can continue typing while Mathematica is working on an evaluation.  The only things you can't do in Mathematica while an evaluation is running are invoke another Kernel command, close the Notebook in which a cell is being evaluated, and delete the cell currently being evaluated.  (Mathematica documents are called Notebooks, and their contents are arranged in units called cells.)

To interrupt a Mathematica calculation, type Command-Period, or choose Interrupt from the Action menu.

To see more examples of Mathematica, open the Tour of Mathematica Notebook in /NextLibrary/Mathematica/Notebooks.




Getting Help

An extensive on-line information system is built into Mathematica.  If you are an experienced NeXT user, you can probably learn most of what you need to know about the NeXT Front End for Mathematica by exploring Mathematica's on-line help.



General Information

Choose the Info command from the main menu, and click the Help button in the panel that appears.  The information you get provides an overview of the entire system.  This is a good place to begin learning about the NeXT Front End for Mathematica.  This is also the place to bring up the Mathematica Registration Form, which you can fill out online, then print and mail to Wolfram Research.  By registering your copy of Mathematica you can receive information about products, services, publications, user conferences, and new developments in Mathematica itself.





Menu Information

To get information about any menu command, hold down the Command key and press the Question Mark key.  This turns the cursor into a question mark.  Then choose the menu command you want to know about.  The panel that appears gives you information about the command you have chosen, and also provides buttons that you can click to read about all the commands in each menu.  This panel can give you a pretty good idea of the capabilities of the Front End.





The menu help message for the Info command.



Panel Information

There are two ways to get help about panels.  Most panels have a Help button, which displays a help message when you click it.



Screen Information

You can get information about anything you see on the screen by typing Command-? and then clicking the object you want to know about.  You can explore the structure of Mathematica Notebooks using this feature.



Explain Selection

When you select text, cells, or graphics in a Notebook and then choose Explain Selection from the Help menu, a panel appears giving you information about whatever you have selected.  When you select a Mathematica function or symbol name (built-in or user- defined), Explain Selection gives an explanation of its use in Mathematica.

If you have the Long Form box checked in the panel, Explain Selection will also give you general information about the current cell, window, Notebook, and Kernel.  Note that to get information from the Mathematica Kernel, you must have a Kernel running.



Why the Beep?

If anything goes wrong, Mathematica beeps.  Usually you will know what the problem is--perhaps a missing bracket in an expression you tried to evaluate, or a mistakenly-chosen menu command--but if you are not sure, you can find out why Mathematica beeped by choosing the Why the Beep? command in the Apple menu.  (Mathematica tries to save you time by not putting up a panel automatically when something goes wrong.  But if you want panels automatically, choose Display in the Preferences menu, and check the "Panel for all errors" option.)

.

Mathematica gives this message if you try to evaluate an expression with a syntax error.



Other Messages

The Mathematica Kernel often prints error and warning messages directly in your Notebook while an evaluation progresses.  These messages are often warnings that you should simply be aware of, and do not necessarily indicate that an evaluation has failed.




Notebooks and Cells

Mathematica documents are called Notebooks.  Notebooks can contain ordinary text and graphics as well as Mathematica input and output.

Notebook data is contained in cells, whose characteristics vary with each cell's function and the kind of data it holds.  The brackets on the right side of each Notebook window show the extent of each cell.  Mathematica allows you to organize Notebook cells hierarchically, corresponding, for example, to sections and subsections of an outline.  The hierarchical structure of a cell group is indicated by the layers of brackets on the right side of a window.




Cells can contain text or graphics.  The picture above shows one way to group a combination of Mathematica input and output along with textual commentary.



Grouping and Moving Cells

You can group cells by using the commands in the Cell menu.  To create a group of cells:

1. Select the range of cells you wish to group together by dragging the cursor across their
cell brackets.  (Or hold down the Shift key as you click the first and last cells individually.)
2. Choose the Group Cells command from the Cell menu.

An outer grouping bracket will appear, enclosing the selected cells.  Grouping is especially useful when you want to organize cells into logical units, like chapters.




Cells can be grouped hierarchically.

To format and manipulate individual cells or groups of cells, select the cell or group brackets by clicking, and choose commands from the menus.  You can use standard editing commands (such as Copy and Paste) not only on text inside cells, but also on whole cells and on groups of cells.

It is easy, for instance, to reorganize a Notebook by selecting and moving a grouped section of cells.  All subgroups of the selected cells are included automatically in the selection.





The selection marked by the highlighted bracket contains all the cells and groups to its left.



To move a group of cells:

1. Select the whole group by clicking the bracket that encloses it.
2. Choose Cut from the Edit menu to delete the group and put it on the pasteboard.
3. Click when the cursor is a horizontal I-beam to place the cell insertion point (represented
by a horizontal line across the window) anywhere in a Notebook.
4. Choose Paste from the Edit menu.

The selected group of cells and their contents will be transferred to the new location.



You can hide the contents of a grouped range of cells by clicking the grouping bracket and choosing Closed Group from the Cell menu.  (You can also just double-click the grouping bracket.)  This collapses the group so that only the contents of the first cell, or head cell, are visible.  Closed groups are marked by an arrow at the bottom of the grouping bracket, and by a box at the bottom of the head cell.  (The size of the box indicates the number of cells in the closed group.)



When you first open a prepared Notebook, often only the head cells of the outermost groups may be visible.  The head cells frequently contain titles or headings for the group of cells.  To open the closed group, double-click the outermost grouping bracket or the closed group box at the bottom of the head cell.




Attributes of Cells

The Cell menu contains a number of commands that specify attributes of cells.  When you activate one of these attributes by choosing it from the menu, the attribute name becomes bold. Choosing the attribute again deactivates it.

One of the most important attributes of a cell is whether it is formatted or unformatted. Unformatted cells contain text that you can edit.  Formatted cells contain more elaborate text or graphics, and cannot be edited directly.  For example, cells in which you have entered text yourself are unformatted, and output cells created by Mathematica are usually formatted. Choosing the Formatted command from the Cell menu will unformat selected cells that are already formatted, and will format ones that aren't.

Cells containing PostScript graphics are formatted.  However, you can edit the PostScript page description just like ordinary text simply by unformatting the cell.  (When you reformat the cell, the Front End will re-render the PostScript text based on your changes.)






A cell can also be active or inactive.  An active cell is one that can be evaluated by the Mathematica Kernel, and usually contains mathematical expressions.  An inactive cell contains material that you would ordinarily not want to evaluate, such as explanatory text or titles, as well as Mathematica output.  Inactive cells are marked with a horizontal tick mark near the top of their cell brackets.

A cell can also be marked as an initialization cell.  Assuming you have checked the appropriate setting in the Startup Preferences panel, you will be asked when you open the Notebook whether you want to evaluate automatically the initialization cells in the Notebook.  Initialization cells are marked by a vertical tick mark near the top of their cell brackets.  Cells can have several other attributes, described in online help for the various commands in the Cell menu.



Styles of Cells

Each cell in a Mathematica Notebook has a particular style--a combination of text and paragraph formatting attributes.  These attributes include the text's font family (Times, for example), typeface (bold or italic, for example), size, color, and format (left justified or centered, for example).

The Cell Style submenu of the Font menu contains a list of styles already defined for cells.  You can see the current style of a selected cell by choosing Cell Style from the Font menu: the selected cell's style name is bold.  You can change the style of any cell simply by clicking its bracket and choosing a new style from the Cell Style submenu.

Each Notebook that you open or create has its own "style sheet"--a collection of styles corresponding to each of the names in the Cell Style submenu.  You can see a Notebook's "style sheet" by choosing Styles Window from the Activate menu.  The window that opens in front of your Notebook will have the title "Styles for..." followed by your Notebook's name.  Note that this window does not represent a Notebook itself; it only shows the styles of the Notebook immediately behind it.




You can change the style definitions for a Notebook by opening the Styles Window in front of the Notebook, selecting cells in the Styles Window, and then choosing new attributes for them from the submenus of the Font menu.  Note, however, that you cannot change the names of the styles in the Cell Style submenu.

Note also that output cells (Output Style) should use a monospaced font family (such as Courier) so that superscripts and subscripts appear properly spaced in mathematical notation.

When you create a new Notebook, its style sheet is copied from the Defaults Notebook, which you can see by choosing Defaults Window from the Activate menu.  (You cannot open this Notebook with standard file opening techniques.)




You can change the styles in the Defaults Notebook by opening the Styles Window in front of it and choosing new attributes just as you would for any other Notebook.  Changes you make to the Defaults Notebook will affect all new Notebooks you create afterwards, but not Notebooks you created previously.





Graphics

All graphical output comes from the Mathematica Kernel in the form of a PostScript page description.  This PostScript text is sent to the Mathematica Front End, which creates a screen image from the PostScript output.

You can read and edit this PostScript text by switching off the Formatted attribute for the cell containing the graphic.  After editing the PostScript text you can have the Front End re-render the image simply by switching on the Formatted attribute.  You can transfer PostScript graphics from a Mathematica Notebook to other NeXT applications in the form of Encapsulated PostScript (EPS).  To transfer a PostScript graphic to another application, just copy it to the pasteboard, activate the other application, and paste.

You can resize graphics in the Mathematica Front End in essentially the same way that you would resize objects in a typical graphics application.

To resize a graphic:

1. Click anywhere inside the cell except on the bracket.  A bounding box with small handles appears around the graphic image.  (See illustration below.)
2. Drag these handles to adjust the size and shape of the bounding box.

If the Preserve Aspect Ratio command in the Graph menu is bold (the default), the aspect ratio of the box will be held constant.  If this command is not bold, you can preserve the aspect ratio manually by holding down the Shift key while resizing.  While you are resizing, the height and width of the bounding box (in points) will be displayed in the status bar at the bottom left of the window.




To reposition an image within its cell without resizing it, click anywhere inside the bounding box and then drag.  While you are moving the image, the offset from the top and left of the cell will be displayed in the status bar at the bottom of the window.

You can change the way the Mathematica Front End renders PostScript graphics by using the six menu commands at the top of the Graph menu.  These commands are described in online help.



Graphical Input

You can use the mouse to find 2D coordinates in a graphic in a Mathematica Notebook.  First click inside the body of the cell, then move the cursor around inside the bounding box.  When you hold down the Command key, the status bar at the bottom of the Notebook window displays the coordinates of the cursor within the bounding box.  If you click while holding down the Command key, a dot is drawn on top of the image.  If you drag while holding down the Command key, a sequence of dots is drawn.   You can then use the Copy command to copy the coordinates of all the dots that have been drawn.  The coordinates are copied as a list that you can paste back into a Notebook as input to Mathematica.




Animated Graphics

You can use sequences of cells containing graphics images as frames in a "movie".  To show the movie, select the sequence of cells, then choose the Animate Selected Graphics command from the Graph menu.  The movie appears in the most visible cell containing a graphic image.  The movie is produced by showing in rapid succession the cells you have selected.  You can set the speed and direction of the movie using the Animation command in the Preferences menu, or by clicking the buttons that appear at the bottom of the window during the animation.  (For some examples of animation, see the sample Notebooks in /NextLibrary/Mathematica/Notebooks.)




Printing
When you are ready to print a Notebook, choose the Print or Print Selection commands from the main menu, and make any necessary settings in the Print panel that appears.

You can also modify the appearance of your printed Notebook by using the Page Setup and Printing Options commands in the Window menu.




Interacting with the Kernel

When you do a calculation in Mathematica, you use the Front End to prepare your input for the Mathematica Kernel and to tell the Kernel to evaluate it.  The NeXT Front End provides a number of features that simplify the preparation and modification of Mathematica input.  Many of these features are mentioned in this section.  These features interact with the Kernel in exactly the same way whether you are running the Front End and the Kernel on the same machine, or running just the Front End on your NeXT Computer with a Kernel on a remote computer.  For a full discussion of preparing input and interacting with the Mathematica Kernel, refer to The Mathematica Book.



Evaluating Input

To evaluate a cell containing Mathematica input, select the cell (or just leave the text insertion point anywhere in the cell) and press Enter (or Shift-Return, or Command- Return).  Mathematica creates one or more new cells immediately after the input cell, and places the results of the evaluation in the new cells.

You can evaluate many cells at once by selecting all of them and pressing Enter, Shift-Return, or Command-Return.

You can continue editing your Notebook and using any other features of the Front End while the Kernel is running.  The only things you can't do are invoke another Kernel command, close the Notebook in which a cell is being evaluated, or delete the cell currently being evaluated. You are free to do anything else, including opening other Notebooks or switching to other programs.

Mathematica assigns numbers in sequence to each of the cells it evaluates.  The "flow" of a Mathematica calculation is represented by these numbers, and not necessarily by the actual positions of cells in a Notebook.  This means, for example, that if you use the percent sign (%) to stand for the last expression generated by the Mathematica Kernel, this will represent the expression with the most recent cell number, not necessarily the expression in the cell immediately above.



Command Completion

If you type the beginning of the name of a Mathematica symbol (built-in or user- defined), then choose the Complete Selection command from the Prepare Input submenu of the Action menu, the Front End will help you complete the symbol's name.

For example, to complete the name Int:

1. Type the first few letters:

Int

2. Choose Complete Selection from the Prepare Input submenu of the Action menu.

If there is only one possible completion, Mathematica automatically pastes it at your insertion point; otherwise it lists the possible completions.  In this example, a panel appears, listing the various possible completions as Integer, IntegerQ, Integrate, InterpolatingPolynomial, Interrupt, and Intersection.  Click the desired function name to insert it, or type additional letters to narrow the choices.  Press Delete to cancel.





Function Templates

Another feature that helps you compose Mathematica input is the Make Template command, also in the Prepare Input submenu of the Action menu.  When you choose this command with a Mathematica function selected or immediately before the text insertion point, Mathematica shows you an example of the function's use.

For example, to get a template for the Plot3D function:

1. Type the name of the Mathematica function:

Plot3D

2. Choose Make Template from the Prepare Input submenu of the Action menu.
Mathematica types an example of the Plot3D function in your Notebook:

Plot3D[f, {x, xmin, xmax}, {y, ymin, ymax}]

You can now select each of the arguments and replace them with the values you want to use. (Make Template automatically uses Complete Selection when you type an incomplete or ambiguous function name.)

The templates you get for built-in Mathematica functions typically illustrate only the simplest of several forms.  Choose Explain Selection to get more information, or see the Mathematica book for a complete description.  The Mathematica Kernel needs to be started (and unoccupied) for function templates to work.




3D ViewPoint Selector

You can use the 3D ViewPoint Selector command in the Prepare Input submenu of the Action menu to bring up a panel that lets you choose a particular orientation for 3D graphics.




You can move the cube in the panel by dragging either the cube itself or the scroll bars.  Once you have selected the viewpoint you want, you can click the Paste button to paste the viewpoint specification at your current insertion point.  By default, the viewpoint is pasted in a form suitable for use as an option to the Plot3D, ListPlot3D, Show, Graphics3D, or SurfaceGraphics commands.

If you click the Make Default button below the Paste button, whatever viewpoint you choose in the panel is made the default for all subsequent Plot3D, ListPlot3D, Graphics3D, and SurfaceGraphics commands.  This way you can simply move the box, then click Make Default, and reevaluate your input without having to paste a new ViewPoint string into each expression.



Copy Input/Output from Above

Often when you are doing a series of calculations, you will find yourself evaluating similar expressions one after another.  The Copy Input from Above command (in the Prepare Input submenu of the Action menu) copies the text of the cell immediately above into your current insertion point.  You can then use standard editing techniques to modify the copy and reevaluate it.

The Copy Output from Above command does the same thing, except that it copies the contents of the nearest output cell.




Loading Packages

The following paragraphs explain the simplest methods for loading packages into Mathematica. These are sufficient for loading any of the packages that are provided with Mathematica.

[Appendix A. "Information about Contexts and Packages," provides further information about writing and loading packages.  The information in this article is technically detailed, and you should probably become familiar with the basic techniques of Mathematica before you read it. ]



Get

You can use the kernel command <<filename (or Get["filename]") to instruct Mathematica to load a package.  If the package is in one of the directories on your current search path, then Mathematica will find the package and load it.  If Mathematica cannot find the package in one of these places, it will fail to load the file.

You can specify either absolute pathnames, which begin with a "/", or relative pathnames, which do not.  The general form of a relative pathname is directory/directory/.../directory/filename. This indicates that the package is in a subdirectory of one of the directories on your current search path (Mathematica $Path variable).

The packages distributed with Mathematica are contained in subdirectories of the Packages directory.  The file Polyhedra.m, for example, is in the Graphics subdirectory, and can be loaded with the command <<Graphics/Polyhedra.m.

When you are using a remote Kernel remember that the commands Get and << use filenames for the machine the Kernel is running on.  The file names you must use may differ from one machine to another.



Needs

Needs is an alternative to Get that has the advantage that its syntax is the same on all machines. By using Needs you can make interdependent packages work the same in NeXT, Macintosh, UNIX, DOS and other environments.  And Needs will not load a package that has already been loaded in the Kernel you are using.

Needs takes a specification not of a file name, but rather of the Mathematica context to be defined in a file.  As a typical case, you can use Needs["Graphics`Polyhedra`"] to load the package Polyhedra.m that is in the Graphics directory.  As described in The Mathematica Book, Needs works by first working out an appropriate file name, then using Get.  As a result, all the features of Get associated with the current search path $Path also work in Needs.




Using a Remote Computer

You need not run the Mathematica Kernel on the same computer on which you are running the Front End.  You can use the Front End to prepare your input, and then send the input for processing by a Kernel on a computer that runs Mathematica in remote mode (typically a larger workstation, mainframe, or supercomputer).

To connect the Mathematica Front End to a Kernel on a remote computer, do the following:

1. Start up the Mathematica Front End.  (If you already have the Front End running and
connected to a local Kernel, choose the Quit Kernel command from the Action menu to
disconnect yourself from this kernel.)

2. Open the Startup Preferences panel by choosing the Startup command from the
Preferences menu.
3. Click the "Use the following command..." setting in the panel.
4. Enter a Mach shell command which will start a Kernel on the desired remote computer
(see below).
5. Click OK.
6. Choose the Kernel command from the Kernels submenu of the Action menu.

The Mach shell command used to start the remote kernel will typically have the following form:

rsh hostname mathremote

The hostname is the network name of the machine on which you want to run the Kernel.  You can enter any shell command (including the name of a shell script you have prepared ahead of time), as long as it ultimately starts up a Mathematica Kernel.

Warning: If you don't feel comfortable with the Mach pipe mechanism, you should consult a guru
before attempting any use of a remote kernel.




Final Note

For further information on any of these operations and features, refer to Mathematica's on-line Help file.  The entire Help file (about 130 pages) can be opened as a Notebook, and can also be printed.  Help messages are designed to assist beginners as well as experts.