Mathematica tutorial
A. Di Bucchianico September 2005

The goal of this notebook is to provide a short overview of the possibilities that Mathematica offers through its notebook concept. The emphasis of this tutorial is on the programming concepts of Mathematica, in conjunction with the notebook concept. As such, this tutorial complements the excellent built-in Mathematica tutorial (choose Help -> Tutorial in the menu) and the Mathematica Tour (choose Help -> Help Browser -> Tour) in the menu.

Getting started

Cells are evaluated by pressing shift-enter. 
Sections can be closed and opened by double-clicking the one but right-most brackets.

It is recommended to change some global preferences by choosing Edit -> Preferences . My choice is to go Notebook Options -> File Options and then check the options NotebookAutoSave and ClosingAutoSave (for some reasons, the defaults are false rather than true).

Basic functions

Mathematica provides a huge number of predefined functions, that can be perform numerical and symbolical computations. Note that Mathematica writes functions and constants with capitals

Sin[2 Pi]

D[x^2 + Sin[x y], {x, 2}]

The above example computes the second derivative with respect to x.

Series[1/(1 - x), {x, 0, 10}]

Limit[Sin[a x]/x, {x -> 0}]

DSolve[y''[x] - 2 y'[x] == Sin[x], y, x]

Assuming[Im[a] == 0 && a > -1 , 
      Integrate[x^a, {x, 0, 1}]]

Integrate[x^a, {x, 0, 1}]

Assuming[0 < x < 1, Sum[x^n, {n, 0, Infinity}]]

Moreover,it offers extensive plotting possibilities,

Plot3D[x^2 y Sin[x],  {x, 0, 1}, {y, 0, 1}]

Packages

In order to prevent unnecessary delays in starting up Mathematica, a number of specialized functions is not immediately available. Mathematica has a large number of packages, that contain specialized functions on several topics (see the Help Browser -> Add-ons & Links for a complete overview). We mention a few packages that are useful for statistics. Of course, Mathematica is not a substitute for statistics software, but many statistical analyses can be performed within Mathematica. Packages are loaded with the following command:

<< Statistics`ContinuousDistributions`

PDF[NormalDistribution[μ, σ], x]

Note the strange convention regarding the variance of the normal distribution!

RandomArray[ExponentialDistribution[2], {10}]

<< Statistics`DiscreteDistributions`

Variance[BinomialDistribution[n, p]]

<< Statistics`DescriptiveStatistics`

LocationReport[{1, 2, 4, 5, 3, 4}]

<< Statistics`HypothesisTests`

<< Statistics`StatisticsPlots`

BoxWhiskerPlot[Table[Random[], {20}]]

Histogram[{1, 2, 2, 2, 3, 4, 5}]

Initialization

One of the great advantages of a notebook is that it allows to automatically load packages and user-defined functions. The way to do this is to select each cell that needs to be automatically loaded at start-up and then choose Cell ->  Cell Properties -> Initialization Cell

Mathematica will create an additional .m file that contains the initializations (notebooks have the extension .nb)

List Operations

The basic data object in Mathematica is a list. Lists are created by using the Table construct.

Table[x^i, {i, 1, 10}]

There are several list operations (see List-based and Functional Programming for more details).

Prepend[{a, b}, c]

Append[{a, b}, {c, d}]

Flatten[Append[{a, b}, {c, d}]]

There are more refined forms of flattening lists like FlattenAt.

User-defined Functions

User-defined functions can be constructed as follows (simple examples to present the basic ideas).

MyFunction[x_, y_] := Sin[x] y

MyFunction[a, b]

If a user-defined function is more complex, it may useful to use local variables. By using the module construct, one can protect these local variables. This is useful to prevent that a local variable gets the value of an already defined variable in the notebook with the same value.

AnotherFunction[x_, n_] := 
      Module[{i}, Sum[i x^i , {i, 1, n}]]

AnotherFunction[x, 3]

AnotherFunction[x, m]

It is often convenient to use default values for functions.

DefaultIllustration[x_, α_:  3] :=  x α

DefaultIllustration[x]

DefaultIllustration[y, 4]

Pattern Recognition

Mathematica has a powerful pattern matching system.

Cases[{3, 4, x, x^2, x^3}, x^n_]

Cases[{3, 4, x, x^2, x^3}, x^n_ -> n]

Select[{2, E, 3, 5, 1, 1/2, π}, IntegerQ]

ListFunction[x_List] := Max[x]

ListFunction[{3, 4}]

ListFunction[{3}]

ListFunction[3]

Replacement Rules

Mathematica can apply replacement rules based on pattern matching (/. is a convenient abbreviation for Replace).

{a, b, 1, 2}  /. x_Integer :> x^2

List-based and Functional Programming

Select[{2, E, 3, 5, 1, 1/2, π}, # > 2 &]

Apply is a useful functions that applies a so-called pure function to a list. Here is an example to compute the sum of a list without using a dummy variable.

Apply[Plus, {1, 2, 3}]

Map maps a function to each element of a list. This can be refined using the levels of list through MapAt.

Map[Abs[#] &, {-2, -1, 0, 1, 2}]

FoldList can be useful to perform sequential operations on a list like computing cumulative sums.

FoldList[Plus, 0, {a, b, c, d, e, f}]

Don'ts

Do loops are relatively slow in Mathematica . They can almost always be avoided by using functional programming.