# Overview of Mathematica Syntax

This page contains a very brief overview of the syntax used throughout the rest of the article. Further information can be found at Wolfram Research's website. Their Documentation Center currently offers free access to *The Mathematica Book* (Wolfram, 2003) which describes all aspects of the program. The Document Center also has a searchable index of Mathematica functions with examples.

If you wish, you may jump directly to one of the following topics by clicking on the link.

## Comments

Any expression delineated by (* and *), such as

(* Sample Comment *)

is essentially invisible to either Mathematica or LiveGraphics3D.

## Mathematical Constants

Mathematica and LiveGraphics3D recognize a number of mathematical constants. The most recognizable of these are Pi and E. (The capitalization is important.) The constant Degree might also be useful to convert from degrees to radians. It is defined to be Pi/180 so that, for example, 270*Degree evaluates to 3*Pi/2.

## Assignments and Rules

As with most languages, you can assign a value to a variable using an equal sign, as in a = 2. It is not necessary to declare the data type of a variable before assigning it a value, and Mathematica does automatic conversion between the various types of numbers. In LiveGraphics3D, these are all treated as (double precision) floating point numbers.

LiveGraphics3D's parameters use a slightly different construction known as a rule, written a -> 2. Within Mathematica there are significant differences between = and ->, but these are not important for the scope of this article.

## If Statement

The If statement in Mathematica has the following syntax.

If[ test, trueResult, falseResult ]

Here test is a condition such as x => 0, x < 0 or x == 0. If test is true, the If statement returns the expression trueResult; otherwise the If statement evaluates to falseResult. It is common in LiveGraphics3D to use nested If statments, such as:

If[ x < 0, 0, If[ x > 1, 1, x] ]

This statement returns 0 if x < 0, 1 if x > 1, and x otherwise.

## Functions

Mathematica's functions are always capitalized and use square brackets around their arguments. For example, the following input would compute the square root of 5. (Mathematica would do this symbolically, but LiveGraphics3D converts all numbers to a decimal format.)

Sqrt[5]

Common functions such as Sin[x], Cos[x], Exp[x] and Abs[x] should all be recognizable in context.

## User Defined Functions

User defined functions can be given names. For example,

f[x_]=(1-x)^2

(Here the use of the underscore in x_ is due to the way Mathematica uses its pattern-matching abilities when defining functions.) With the given definition, f[2] would return a value of 1, whereas f[π] would return a value of (1-π)^{2}.

## Lists and Tables

A list is one of the basic objects in Mathematica. Elements of a list are separated by commas and are surrounded by curly brackets:

{1,2,3}

In this article we will use two different types of lists. The first is a list of three numbers, like the example above; this is how Mathematica represents coordinates in three-dimensions. The second is a list of graphical objects to be plotted on the screen; more on that below.

Mathematica's implementation of vectors is based on lists, so we can use scalar multiplication, component-wise addition, the dot product and (in three-dimensions) the cross product:

- a*{1, 2, 3} evaluates to {a, 2a, 3a}
- {a, b, c} + {d, e, f} evaluates to {a+d, b+e, c+f}
- {a, b, c} . {d, e, f} evaluates to a*d + b*e + c*f
- Cross[{a, b, c}, {d, e, f}] evaluates to {b*f - c*e, c*d - a*f, a*e - b*d}

Lists of objects can be easily constructed using Mathematica's Table function. The function

Table[ expression, {var, min, max(, optional stepsize)} ]

creates a list of the values of expression as the variable var runs from the given minimum value to the stated maximum. (If the minimum and maximum values are integers, the default stepsize is 1; this can be adjusted using the optional stepsize in the command.) For example, the command

Table[ i, {i, 0, 5} ]

generates the list {0, 1, 2, 3, 4, 5}, whereas

Table[ i, {i, 0, 5, 2.5} ]

results in {0, 2.5, 5}.

Table can generate multi-dimensional lists as well. Consider the following commands:

f[x_,y_]=x^2-y^2

Table[ {i, j, f[i,j]}, {i,-1,1},{j,-1,1}]

This generates the following list; ignoring the extra curly brackets for now, it is nothing more than a list of points on the graph of *z=x ^{2}-y^{2}*.

{{{-1, -1, 0}, {-1, 0, 1}, {-1, 1, 0}},

{{0, -1, -1}, {0, 0, 0}, {0, 1, -1}},

{{1, -1, 0}, {1, 0, 1}, {1, 1, 0}}}

## Graphics3D Objects

A three-dimensional picture is represented with the following syntax.

Graphics3D[ *list of graphics primitives*, *list of graphics options* ]

*Graphics primitive* is a name given to objects such as points and polygons. These are all defined in terms of coordinates which, as mentioned above, are represented by a list of three numbers. Hence the origin in three-dimensional space is written:

{0, 0, 0}

The rest of this overview covers the most common graphics primitives and options.

### Graphics Primitives

This article uses four basic graphics primitives: points, lines, polygons and text. Each is described below.#### Points

To draw a three-dimensional point, use the Point primitive. The only argument is the list of coordinates.

Point[{1,2,3}]

#### Lines

A line segment between two points is created using the Line primitive, together with a list of the two points.

Line[{{0,0,0}, {1,2,3}}]

In fact, Line can accept an arbitrarily long list of points; line segments are drawn from one point to the next in the list. This allows us to use Line to create curves. For example,

f[t_]={Cos[t], Sin[t], t}

Line[ Table[ f[t], {t, 0, 2Pi, 2Pi/30.} ] ]

represents 30 tiny line segments which approximate the graph of the helix parametrized by f[t]. (The Table function generates the list of points on the curve.) These segments are shown in the following LiveGraphics3D applet, whose input was generated by the command Graphics3D[ Line[ Table[ f[t], {t, 0, 2Pi, 2Pi/30.} ] ] ]

#### Polygons

A filled-in polygon is described by the Polygon primitive, together with an ordered list of its vertices. The following primitive represents the unit square in the plane *z=0*.

Polygon[{{0,0,0}, {1,0,0}, {1,1,0}, {0,1,0}}]

When creating surfaces, we generally want a long list of polygons which approximate the surface. This can be created with the Table function.

f[x_,y_]=x^2-y^2

dx = 0.1

dy = 0.1

Table[ Polygon[{{i, j, f[i,j]}, {i+dx, j, f[i+dx,j]},

{i+dx, j+dy, f[i+dx,j+dy]}, {i, j+dy, f[i,j+dy]}}],

{i,-1,1-dx/2,dx},
{j,-1,1-dy/2,dy}]

The commands above generate a list of polygons which approximate the graph of *z=x ^{2}-y^{2}*. (For the curious reader, the use of 1-dx/2 and 1-dy/2 instead of 1 is to prevent Mathematica from generating extra polygons, where

*x*and

*y*would range from 1 to 1.1.) Here are those polygons, as displayed by LiveGraphics3D;

#### Text

You can insert text into a picture using the Text[ "string", coordinates ] primitive. When using LiveGraphics3D's INPUT parameter (as opposed to INPUT_FILE -- see the main article), you should replace " with a pair of single quotes '' in your definition of the string. (Replacing " with " may also work with some browsers.) The following LiveGraphics3D applet was given the input Graphics3D[ Text[ ''Hello World!'', {0,0,0} ]. Try rotating the box; note that LiveGraphics3D does not rotate the text, but instead always leaves it facing the reader.

### Graphics Directives

The appearance of a graphics primitive can be altered using special functions known as*graphics directives*. The most common of these include:

- RGBColor[r,g,b]: represents a color using the standard Red-Green-Blue color model. The arguments represent the percentage of red, green and blue light, respectively, and are given as a decimal between 0 and 1.
- SurfaceColor[RGBColor[r,g,b]]: specifies the surface color of polygons for diffuse lighting. By default lighting is enabled. Note, however, that the surface color is not used if lighting is disabled with the option Lighting -> False (see below). With disabled lighting, the color specified by RGBColor[r,g,b] (without SurfaceColor) is employed for polygons.
- PointSize[value]: adjusts the size of a Point. The default value is 0.01, and 0.05 is already quite large.
- Thickness[value]: adjusts the thickness of a Line. The default value is 0.01.
- EdgeForm[]: A directive for polygons which describes how their edges should be drawn. As given here, with no argument, the edges are omitted from the picture. This is a standard way of removing the "mesh" from a surface.
- FaceForm[RGBColor[fr,fg,fb], RGBColor[br,bg,bb]]: A directive for unlit polygons which specifies separate colors for the front and back faces. This directive is only effective if lighting is disabled with the Lighting -> False option described below.
- FaceForm[SurfaceColor[RGBColor[fr,fg,fb]], SurfaceColor[RGBColor[br,bg,bb]]]: specifies separate surface colors for the lighting of back and front faces of polygons. Note that this directive is only effective if lighting is enabled with the (default) Lighting -> True option described below.

There are complicated issues involving lists and the "scope" of a directive, but at the most basic level you use them by replacing a primitive (such as Point[{1,2,3}] with a list. The last element of the list should be the primitive; the preceding elements are directives. For example,

{RGBColor[1,0,0], PointSize[0.05], Point[{1,2,3}]}.

This new object is displayed in the following applet. The point is now red, and five times larger than normal.

### Graphics3D Options

Options for Graphics3D objects are invoked using the format OptionName -> Setting. While there are too many options to mention here, we can describe a few of the more commonly used ones.

- PlotRange: specifies the bounding box of the graphics. The minimum and maximum values for the x, y, and z axes are specified in the form {{xmin, xmax}, {ymin, ymax}, {zmin, zmax}}. While Mathematica clips all objects at this bounding box, LiveGraphics3D performs no clipping. In both cases, the bounding box (in particular its center) is crucial for projecting, rotating, and zooming the graphics. The default value Automatic determines the bounding box based on the primitives of the graphics.
- BoxRatios: specifies the ratios of the edge lengths of the diplayed bounding box. A value of {sx, sy, sz} will scale the edges in x, y, and z direction such that the ratios between displayed edge lengths are sx : sy : sz. For example, a value of {1, 1, 1} will display any bounding box as a cube. The default value Automatic determines these ratios according to the coordinate ranges covered by the bounding box as specified by the PlotRange option.
- Boxed: specifies whether or not the bounding box should be drawn. Can be set to True or False.
- Axes: specifies whether or not the coordinate axes should be drawn. Can be set to True or False.
- AxesLabel: if axes are drawn, they will not be labeled "x," "y" and "z" by default. If you wish to have these labels, you should specificy AxesLabel -> {X,Y,Z}. (In fact, the labels can be any strings, but note the comment about '' and " in the description of the Text[] directive above.)
- Lighting: specifies whether or not to use the default lighting for polygons. (See the saddle-shaped surface in the Polygon section to see an example with this lighting model.) By default this option is set to True. Enabled lighting will use colors specified with SurfaceColor and light sources specified with LightSources to light polygons.
- LightSources: specifies a list of directional light sources. Each directional light source is specified by a vector in display coordinates for the initial view point (positive x axis to the right, positive y axis to the top, and positive z axis towards the viewer) and a color of the emitted light. For example, {{{x1, y1, z1}, RGBColor[r1,g1,b1]}, {{x2, y2, z2}, RGBColor[r2,g2,b2]}} specifies two light sources. The first light source emits light from the direction {x1, y1, z1} and is colored by the red, green, and blue components r1, g1, and b1. The light sources are only effective if lighting is enabled with Lighting -> True.
- ViewPoint specifies the view point {x, y, z} at which the viewer is located. The magnitude of this vector determines the strength of perspective warping, i.e., a large magnitude will reduce the perspective warping. The default magnitude is about 3.4.
- ViewVertical specifies the "up direction" of the viewer. The default {0, 0, 1} describes viewers whose head is pointing into z direction.

### Putting Everything Together

Compare the input for LiveGraphics3D shown here to the picture you see in the applet. If you can see how the primitives, directives and options lead the displayed picture, you know enough about Mathematica's syntax to create your own material with LiveGraphics3D. Note that the Lighting is set to False; because the square in the plane *x = 1* has no RBGColor[] directive, it is black on both sides.

Graphics3D[{

Point[{0,0,0}],

{RGBColor[0,1,0], PointSize[0.05], Point[{1,2,3}]},

Polygon[{{1,0,0},{1,1,0},{1,1,1},{1,0,1}}],

{EdgeForm[], FaceForm[ RGBColor[1,0,0], RGBColor[0,0,1] ],

Polygon[{{0,1,1},{-1,0,1},{-1,1,1}}]},

{RGBColor[0,1,1], Thickness[0.05], Line[{{-1,1,2},{1,1,2}}]}

},

Lighting->False, Boxed->False, Axes->True, AxesLabel->{X,Y,Z}]