Introduction to Mathematica

Tom Wickham-Jones
Director of Kernel Technology
Wolfram Research

Please note that this notebook is optimized for display by Mathematica 4.2. Also that it may refer to hyperlinks that are not included in the file archive.

Initialization

Off[General::spell];
Off[General::spell1];

SetDirectory[
        DirectoryName[
          ToFileName["FileName" /. 
              NotebookInformation[ SelectedNotebook[]]]]];

What is Mathematica?

Integrated technical computing system.

Combines many different areas of computatation.

Used for small scale calculator type operations,

Used for large scale computations.

General Features of Mathematica

Differential equations, special functions, linear algebra, optimization, statistics, data analysis etc..

Validated extended precision arithmetic.

Algebra (solving equations), integration, differentiation, inequality processing, differential equations, etc..

Symbolics fully integrated with numerics.

Simple calculator operations.

Prototyping and large-scale programming.

Powerful visualization tools, again this is full integrated.

Document centric notebook interface, fully programmable.

Interactive mathematical typesetting system, fully integrated with MathML.

Powerful connection technology, via C with MathLink and Java with J/Link.

Mathematica as a Calculator

Interactive input/output technique very convenient.

6^20

Numbers of any size.

6^200

Standard mathematical notation, using palettes or from the keyboard.

(6^200)

Calculate the eigenvalues of a matrix.

Eigenvalues[(({{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}))]

Formulae as well as numbers

Solve an equation.

Solve[(Sqrt[x]) + a == 2  x, x]

Compute an integral.

∫(Sqrt[x]) (Sqrt[a + x]) \[DifferentialD]x

2D and 3D graphics

Mathematica can generate many different types of plot.

A 2D plot of a simple function.

Plot[Sin[x] + Sin[1.6  x], {x, 0, 40}];

A 3D plot.

Plot3D[Sin[x ] Sin[y], {x, 0, 2  π}, {y, 0, 2  π}, 
        PlotPoints -> 30];

A closely related syntax will generate a contour plot.

ContourPlot[Sin[x ] Sin[y], {x, 0, 2  π}, {y, 0, 2  π}, 
        PlotPoints -> 30];

Similar commands in Mathematica have a closely related syntax.

Many special plots, bar charts, log plots, data plots etc...

Integrated Computation

There are many functions in Mathematica.

They are all designed carefully to work together.

Mathematica is much more than a collection of different functions.

Integrated Computation: Data Analysis

With Mathematica least squares fitting of some data is very straight-forward.   First some data is generated.

data = 
        Table[{i, Random[] + 4  Sin[(Sqrt[i])]}, {i, 0. , 
            20,  .1}];

A plot of the data can be made.

(Subscript[p, 1]) = ListPlot[data];

Now we make a fit of the data.

Fit[data, {1, x, (x^2), (x^3), (x^4), (x^5)}, x]

The fitted function can be plotted.

(Subscript[p, 2]) = Plot[%, {x, 0, 20}];

Finally the fit and the data can be plotted together.

Show[(Subscript[p, 1]), (Subscript[p, 2])];

After carrying out simple data fitting one might wish to do more sophisticated analysis using the statistics functionality that Mathematica provides.

Also use some of the data import features.

Fourier

This is another example which involves a Fourier transform.

We can generate a collection of data.

data = 
        Table[Random[] + 50 Sin[2 x], {x, 0. , 300}];

It is easy to make a plot of this data.

ListPlot[data];

Now we can take the fourier transform of the data and plot the absolute value.

ListPlot[Abs[Fourier[data]], 
        PlotJoined -> True];

Differential Equations

Solve a nonlinear differential equation  => interpolating function which implicitly represents the whole solution.

NDSolve[{x''[t] + x[t]^3 == Sin[t], 
        x[0] == x'[0] == 0}, x, {t, 0, 50}]

A phase space plot of the solution.

ParametricPlot[
        Evaluate[{x[t], x'[t]} /. %], {t, 0, 50}];

Note how easy it is to take the result of the differential equation, differentiate it and plot the result.   This is an example of good design.

Visualization

Powerful graphics system: combines with programming language to allow many different types of plot to be generated.

Table is used to construct a collection of graphics primitives.

{Line[Table[t {Sin[t], Cos[t]}, {t,0,6Pi,0.2}]],
 Table[Line[{ t {Sin[t],Cos[t]},
  (t-Pi/2){-Cos[t],Sin[t]}}], {t,Pi/2,6Pi,0.2}]};

Display the result.

Show[Graphics[%, AspectRatio -> Automatic]];

This generates a list of Circle primitives with centers that lie on a hyperbola and which  pass through the origin.

Table[ Circle[ {x,Sqrt[1+x^2]}, Sqrt[1+2x^2]],{x,-1,1,0.2}];

The envelope that results generates part of the Lemniscate of Bernoulli.

Show[Graphics[ %, 
	AspectRatio -> Automatic]];

Graphics Programming Applications

An optical system designed with Optica,  the Application Pack for optical analysis.

Mathematica as a Document System

The Mathematica notebook interface (notebook front end) provides an interactive document centric interface.

Can be used as a presentation tool, for live interactive computations.

Provides many features, active elements, programming interface, and style mechanism.

Notebooks as Objects

Mathematica provides a system of active elements, called palettes that can be used to perform specific tasks.

A large collection of palettes ships with Mathematica, and new palettes can be written by users, see later.

Notebook documents have a basic representation as Mathematica expressions, and a large collection of functions are provided to make this work.

nb = NotebookCreate[];

This can be very useful in the context of webMathematica, it means that new documents can be created dynamically.

NotebookWrite[ ]

Very relevant to webMathematica, can build web sites that automatically generate documents containing text and graphics in formats such as Mathematica NB or PDF.

XML Support

XML becoming very popular, widely supported in browsers, other applications, and tools such as XSLT, and DTDs.

Strong support in Mathematica:  XML and Mathematica share the basic principle of structured data.

Here is a basic XML file.

!! coffee.xml

Import into Mathematica

xml=Import["coffee.xml"]

Symbolic representation in Mathematica, can be worked on with regular Mathematica syntax.

Cases[xml, XMLElement["coffee", _, _], 
      Infinity]

Restructure the symbolic XML to get into a form for working with.

% //. {XMLElement[n_, _, {cont_}] -> (n -> cont),  
        XMLElement[n_, _, {cont__}] -> n[cont]}

Re-export as XML.

ExportString[xml, "XML"]

Value of XML

Datafiles

No need to invent a special data format, use XML.

Parsers, formatters, transformation, validators etc... all readily available in many places including Mathematica.

Standard XML Formats

XML is useful for math with MathML, graphics with SVG, and web publishing with XHTML.

All immediately work well with Mathematica.

Mathematica Support

Mathematica commands, input, output many controls.
Mathematica XML dialects: MathML, SVG, also notebooks with NBML, expressions with ExpressionML, text with XHTML etc...

Many uses for webMathematica.

More information in the XML docs.

MathML

Short word on MathML, really quite relevant to webMathematica.

Mathematica typesetting language, based on Mathematica expressions. We can see this by printing the result with BoxForm. Closely related to MathML.

Simple to generate MathML for a given expression.

Sin[(x^2)]//MathMLForm

Many features that come from the XML support. For example, generate presentation MathML using a namespace prefix.

\(\(\(XML`MathML`ExpressionToMathML[
      Sin[x^2], "Formats" -> {"PresentationMathML"}, "NamespacePrefixes" -> {"http://www.w3.org/1998/Math/MathML" -> "mml"}]\)\(\ \)\)\)

The MathML functionality that is coming on-line can make ready use of such features.

Mathematica as a Component

Mathematica has long provided a connection technology called MathLink, works for C and C++, various linkage technology built on this, to connect Mathematica to Excel, Word, etc...

Particularly useful connection is provided by J/Link, the connection to Java.

Soon connection to .NET.

J/Link

J/Link is a general connection between Java and Mathematica.

Java programs can call Mathematica and Mathematica programs can call Java.

It provides many many more features than MathLink, and is much easier to use.

It comes with extensive documentation and the complete source code.

It ships with Mathematica 4.2, with its own Java Runtime Environment ready to run. To use, just load and install.

<< JLink`;

InstallJava[];

Formatting Numbers

Java has some nice classes for formatting numbers. JavaNew creates a new Java object and returns a reference to it back to Mathematica.

\(\(fmt\)\(\ \)\(=\)\(\ \)\(JavaNew["java.text.DecimalFormat", \ "#.0000"]\)\(\ \)\)

The @ symbol is used like the Java . (dot). This means "call the fmt object's format method."

fmt@format[12.34]

fmt  =  
          JavaNew["java.text.DecimalFormat",  "#,###.00"];

fmt@format[123456.789]

J/Link Examples

Collection of examples, open with a palette.

For example the Spirograph example.

PlotOrbits.nb

Summary

Integrated system for technical computation.

Provides careful integration of many different areas of functionality.

Maximum ease of use with consistent design principles.

Unified way to communicate technically and write up discoveries using familar and specialized notation.

Viable solution for electronic publishing and courseware.

Strong communication features that integrate the system with other languages.

The link with Java is particularly strong and forms the basis for webMathematica.