Mathematica

From Example Problems
Jump to navigation Jump to search

Template:Infobox Software2

This article is about computer software. For the policy research organization, please see Mathematica Policy Research, Inc.

Mathematica is a widely-used computer algebra system originally developed by Stephen Wolfram and sold by his company Wolfram Research. Mathematica is also a powerful programming language emulating multiple paradigms on top of term-rewriting.

Overview

Wolfram started to work on the program in 1986 and released the first version in 1988. The current version is 5.2 (released July 12, 2005). It is available on a wide variety of computer systems.

The Mathematica programming language is based on term-rewriting and supports both functional and procedural programming (though functional code is much more efficient in general). It is implemented in an object-oriented variant of C, but the bulk of the extensive code library is actually written in the Mathematica language that can be used to extend the system. Typically, new code is added in the form of Mathematica "packages", i.e., text files written in the Mathematica language.

In the Mathematica system, the core language is interpreted by a kernel that performs the actual computations. The results are usually communicated to one of several frontends. Communication between the kernel and the frontend (or any other client, like user-written programs) uses the MathLink protocol, often over a network. It is possible for several frontend processes to connect to the same kernel, and for one frontend to be connected to several kernels.

Unlike some other computer algebra systems, for example Maxima or Maple, Mathematica tries to apply the currently stored transformation rules as long as possible, looking for a fixed point. For this to be meaningful, absence of side-effects is beneficial (though not enforced), hence the similarity to functional programming. Functions and code are first-class and not opaque. Scoping is dynamic, but there are also some constructs that try to simulate lexical scope (all of these can easily be broken).

Examples

The following Mathematica sequence will find the determinant of the 6×6 matrix whose i, j'th entry contains ij with all zero entries replaced as 1.

 In[1]:= Det@ReplaceAll[Table[i j, {i,0,5}, {j,0,5}],{0->1}]
 Out[1]= 0

So the determinant of such a matrix is 0.

The following numerically calculates the root of the equation ex = x2 + 2, starting at the point x = -1.

 In[2]:= FindRoot[Exp[x] == x^2 + 2, {x, -1}]
 Out[2]= {x -> 1.3190736768573652}

See Hello World for the prototypical Hello World program.

Multiple paradigms, one language

Mathematica permits multiple programming paradigmatic approaches to programming. Consider a simple example: we want a table of values of gcd(x, y) for 1 ≤ x ≤ 5, 1 ≤ y ≤ 5.

There are at least three approaches to this:

In[3]:= Table[GCD[x, y], {x, 1, 5}, {y, 1, 5}]
Out[3]=  {{1, 1, 1, 1, 1}, {1, 2, 1, 2, 1}, {1, 1, 3, 1, 1}, {1, 2, 1, 4, 1}, {1, 1, 1, 1, 5}}

An APL-style approach:

In[4]:= Outer[GCD, Range[5], Range[5]]
Out[4]=  {{1, 1, 1, 1, 1}, {1, 2, 1, 2, 1}, {1, 1, 3, 1, 1}, {1, 2, 1, 4, 1}, {1, 1, 1, 1, 5}}

Outer corresponds to the outer product operator, Range corresponds to the iota operator.

An iterative approach:

In[5]:= l1 = {}; (* initialize as empty list, since we want a list in the end *)
       For[i = 1, i <= 5, i++,
       l2 = {}; 
          For[j = 1, j <= 5, j++,
             l2 = Append[l2, GCD[i, j]] 
             ];                                      
          l1 = Append[l1, l2]; (* append the sublist, that is, the row *)
       ]; l1
Out[5]=  {{1, 1, 1, 1, 1}, {1, 2, 1, 2, 1}, {1, 1, 3, 1, 1}, {1, 2, 1, 4, 1}, {1, 1, 1, 1, 5}}

Observe that this solution is considerably larger than the previous two.

Common structures, common manipulations

One guiding principle in Mathematica is a unified structure behind almost all objects representable in Mathematica. For example, the expression Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle x^4+1} if entered will be represented as if it were written:

In[6]:= x^4 + 1
Out[6]= 1+x4

If the FullForm command is used on this expression however:

In[7]:= FullForm[x^4 + 1]
Out[7]= Plus[1, Power[x, 4]]

Nearly all objects in Mathematica have the basic form head[e1, e2, ...] (which may be displayed or entered in some other fashion). For example, the head of the above example is Plus, and symbols such as x have form Symbol["x"]. Lists have this structure too, where the head is List.

The principle permits regular expressions unrelated to lists to be operated on with list operators:

In[8]:= Expand[(Cos[x] + 2 Log[x^11])/13][[2, 1]]
Out[8]= 2/13

The reverse can also occur -- lists can be modified to behave like regular expressions:

In[9]:= Map[Apply[Log, #] &, {{2, x}, {3, x}, {4, x}}]
Out[9]= {Log[x]/Log[2], Log[x]/Log[3], Log[x]/Log[4]}

where the Apply function changes the head of its second argument to that of the first.

Front ends

The default Mathematica frontend features extensive layout and graphical capabilities, performs prettyprinting and offers a notebook metaphor - user input (both text and Mathematica input) as well as results sent by the kernel (including graphics and sound) are placed in a hierarchy of cells (as is the case for Maple), which also allows for outlining and sectioning of a document. Starting with version 3.0 of the software, notebooks are represented as expressions that can be manipulated by the kernel, and the typesetting features of the frontend were deemed sufficiently important to warrant the availability of a dedicated reader software for displaying Mathematica notebooks, the MathReader software that is not tied to a commercial license.

Several other frontends are also available, e.g., JMath or mash, but the standard Mathematica frontend is the most popular.

Connections with other applications

The MathLink protocol allows not only communication between the Mathematica kernel and front-end, but also between the kernel and arbitrary applications. Wolfram Research distributes freely a developer kit for linking applications written in the C programming language to the Mathematica kernel through MathLink, as well as J/Link, a similar, easier to use system for the Java programming language. Using J/Link, a Java program can ask Mathematica to perform computations; also, a Mathematica program can load any Java class, manipulate Java objects and perform method calls, making it possible, for instance, to build Java graphical user interfaces from Mathematica.

Mathematica on the Web

Wolfram Research also makes a program called webMathematica with which Web servers can add "interactive calculations and visualization to a website."

On Sloane's Online Encyclopedia of Integer Sequences, Mathematica and MAPLE are the two most often used CASs for which commands are provided with which to calculate the sequences; both of them have their own database fields on the OEIS.

See also

External links

de:Mathematica fr:Mathematica lt:Mathematica pl:Mathematica pt:Mathematica sv:Mathematica tr:Mathematica zh:Mathematica