You can get a lot of information from the Help Browser (to access it press F1 or use the Help menu).

One can use Mathematica just like a calculator: one types in formulas and Mathematica returns back their values. Just press SHIFT + ENTER (RETURN) to tell Mathematica to evaluate the input you have given it.

Example.

( press SHIFT + ENTER after putting the cursor after 2 + 2 to see the output)

With a text - based interface, you interact with Mathematica just by typing successive lines of input, and getting back successive lines of output on your screen.

At each stage, Mathematica prints a prompt of the form In[n] := to tell you that it is ready to receive input. When you have entered your input, Mathematica processes it, and then displays the result with a label of the form Out[n] =.

Different text - based interfaces use slightly different schemes for letting Mathematica know when you have finished typing your input.With some interfaces you press Shift - Return, while in others Return alone is sufficient.

An important feature of Mathematica is its ability to handle formulas as well as numbers. Whenever you use Mathematica, you are accessing the world' s largest collection of computational algorithms. Mathematica knows about all the hundreds of special functions in pure and applied mathematics (e.g., Chebyshev polynomials, Bessel functions).

Example. The following function computes the 10th degree Chebyshev polynomial and the next one draws the function on the interval [-1, 1].

Let's see what happens when we use the same input in WolframAlpha:

Note the two links at the lower right hand corner: Download as PDF and Live Mathematica. The first one is obvious. The second one needs the CDF Player plug-in to be installed.

In general, Mathematica notebooks allow importing and exporting of many formats. One can prepare even a slide show in Mathematica.

Kernel and FrontEnd

Mathematica consists of two independent computational environments called the FrontEnd and the Kernel, which communicate by means of a protocol called MathLink. The Kernel does all the computations. The FrontEnd is what you see in front of you, including the window, menu, etc. You can use many FrontEnds with one Kernel but the usual FrontEnd is what is know a notebook FrontEnd (there are also ASCII front ends you can run using a terminal interface).

The Kernel is the basic programing environment and in fact it can be used to completely control the FrontEnd. We will give a few examples, but we will not use much of this. For example:

The FrontEnd itself can also be “programmed” independently of the Kernel. This will be more important for us later, in building interfaces.

Mathematica is one of the largest single application programs ever developed, and it contains a vast array of algorithms and important technical innovations. Among these innovations is the concept of platform - independent interactive documents known as notebooks.

Every Mathematica notebook is a complete interactive document combining text, tables, graphics, calculations, and other elements. A Mathematica notebook consists of a list of cells, which you can group (sections etc).

Exercise. Click on different brackets on the right in this notebook with a right mouse button to find out the style being used.

Palettes and buttons provide a simple but fully customizable point - and - click interface to Mathematica (for Greek symbols, signs of integral, simple build - in functions, etc).

Recently Wolfram Research has expanded the concept of a notebook by introducing a new document format called CDF (“Computable Document Format”) which unlike traditional notebooks allows interactive “dynamic” content.

Mathematica is built on the powerful unifying idea that everything can be represented as a symbolic expression.

Once one starts experimenting in Mathematica, one immediately notices some of its main features.

1. One important feature of Mathematica that differs from other computer languages, and from conventional mathematical notation, is that function arguments are enclosed in square brackets, not parentheses. Parentheses in Mathematica are reserved specifically for indicating the grouping of terms. There is obviously a need to distinguish giving arguments to a function from grouping terms together.

2. Names of built-in functions start with a capital letter.

3. Multiplication is represented either by * or by a space.

4. Powers are denoted by ^.

5. Numbers in scientific notation are entered, for example, as 2.5*^-4 or 2.5 10^-4.

6. There is a general convention in Mathematica that all function names are spelled out as full English words, unless there is a standard mathematical abbreviation for them. The great advantage of this scheme is that it is predictable. Once you know what a function does, you will usually be able to guess exactly what its name is. If the names were abbreviated, you would always have to remember which shortening of the standard English words was used.

7. Another feature of built - in Mathematica names is that they all start with capital letters. The capital letter convention makes it easy to distinguish built - in objects. If Mathematica used max instead of Max to represent the operation of finding a maximum, then you would never be able to use max as the name of one of your variables. In addition, when you read programs written in Mathematica, the capitalization of built - in names makes them easier to pick out.

8. N is a function that turns exact numbers and certain symbols into approximate numbers. For example:

Hence N cannot be used for a function or a variable name. The same is true of some other symbols written with a capital letter (e.g. E,C). For that reason it is important to follow the convention that user defined symbols begin with a small letter.

A quick access to help information is achieved by typing the question mark :

The quick access to help is also by highlighting the word and then pressing F1.

To get help for the command/operator you know you need to type ? and the command/operator. If you do not know the operator, search the Help Browser.

Mathematica understands lists as {a, b, c} (in the full form it is List[a, b, c]). One can learn later on that many objects in Mathematica are written by using lists. For instance, a matrix can be inserted in the following way: go to the main menu; insert; tables/matrices:

? | ? | ? |

? | ? | ? |

? | ? | ? |

Next one just needs to put the brackets and fill in the matrix elements by clicking on each empty square :

The result is :

The same matrix can be entered like this :

Here % means the last expression.

Here %% means the last but one expression.

Example.

One can type many symbols without using palettes. For instance, to type in ?, one needs to press esc then type in pi then press esc once again.

Example.

The use of Ctrl+6:

esc+i+i+esc

esc+pi+esc

Alt+7 (applying to the blue bracket on the right): gives text in the notebook.

A very useful trick is the formula completion feature. Suppose, for example, you wish to use a function whose name begins with Plot but you can’t quite remember the rest of it. Just type in the beginning of the name and press the Control key (Command key on the Macintosh) and the letter K. You will see a pop up menu of all functions whose name begins with Plot. If you decide you want to use the function Plot3D you can type the name Plot3D and press Control and Shift keys together with the K key. You will see a template:

An overview of programming techniques

For most of the more complex problems that one wants to solve with Mathematica, one has to create Mathematica programs oneself. Mathematica supports several styles of programming, and one is free to choose the one, one is most comfortable with. However, it turns out that no single type of programming suits all cases equally. As a result, it is useful to learn several different types of programming.

Traditional programming language such as C or Fortran use procedural programming (assignments and loops such as Do, For, While and so on). They also exist in Mathematica. But while any Mathematica program can, in principle, be written in a procedural way, this is rarely the best approach. In a symbolic system like Mathematica, functional and rule - based programming typically yields programs that are more efficient, and easier to understand.

Some types of programming :

Procedural Programming

List - based Programming (Many operations are automatically threaded over lists, a starting point to learn).

Functional Programming

Rule - Based Programming

Mixed Programming Paradigms

There are typically many different ways to formulate a given problem in Mathematica. In almost all cases, however, the most direct, precise and simple formulations will be best.

There are dozen of definitions of the factorial function (see later on).

Expressions

All objects in Mathematica programming language are expressions. For example

are all different kinds of expressions. These expressions often look like mathematical formulas (more about that later on), which makes them more understandable and memorable to humans, but actually that have an internal form that is very simple and very consistent. It is called the “Full Form” of an expression and can be seen by applying the function FullForm to it (but there is a caveat, see below).

FullForm of expressions

Each expression is either an Atom or has the form

where F is called the Head of the expression and a1, a2, are expressions. Examples of atoms are 2,a,3/4,3.2, "cat". Whether something is an atom can be tested with the function AtomQ:

Expressions often do not look like their FullForms, for example a+b has FullForm:

Note that atoms also have Head:

Note also that :

Evaluation of x to 1 caused this to happen. You can see the original Head by preventing evaluating e.g.

The full form of

is

It is important to distinguish the assignment Set from Equal, which is usually written as == and has

Parts of Expressions

A very important skill is extracting parts of expressions. An expression is really a tree-like object, as can be seen using the function TreeForm:

and so on.

You can also do this from the back :

Now, here comes a very nice and important fact: you can change an expression by an assignment to a part of it. For example;

A very important thing to notice that in Mathematica lists are just expressions with Head List:

A matrix is simply a list of lists of the same length:

We will later see how to easily create arbitrarily large matrices using the functions Table and Array.

A note on forms of expressions.

We already know that a Mathematica expression often looks different to human eyes than its internal form (FullForm). However, the situation is made more complicated, by the fact that traditional mathematical notation is not unambiguous. Because of this and for reasons of history Mathematica has several “forms” of input and output. The first versions of Mathematica has only two forms: InputForm, which looked like a standard programming language (e.g. Fortran) way of writing mathematical formulas and OutputForm, which is a little more like usual mathematics and has become completely obsolete (it retained only for reasons of compatibility with very early Mathematica notebooks). Since then they have both been replaced by StandardForm and TraditionalForm. StandardForm retains the basic principles of InputForm but allows more usual mathematical expressions. TraditionalForm looks almost like the usual mathematical notation. One can convert between these forms using the Convert To sub menu in the Cell menu. One can also set the default forms for the Input and Output in the Preferences menu.

Basic principles of InputForm (and StandardForm)

1. All built in functions start with a capital letter.

2. Square brackets [] are used as function brackets.

3. (InputForm) The basic arithmetical operations are denoted by + (addition),* or space (multiplication) / (division), ^ (power).

4. There are the following inclusions InputForm ? StandardForm ? TraditionalForm but not in the opposite direction.

Links

http://reference.wolfram.com/Mathematica/guide/Expressions.html

http://reference.wolfram.com/mathematica/tutorial/FormsOfInputAndOutput.html

2. Working with Lists

One of the most common expressions in Mathematica are lists.

Let us also recall that the matrix is entered by using lists :

Let us learn how to generate lists and what basic operations one can perform with them. Another useful command is Table

3 |

2 |

3 |

6 |

One can generate not only numbers but also other expressions :

Some commonly used objects are already defined in Mathematica. For instance, the identity matrix :

For the matrices Mathematica has a lot of build - in operations

Basic operations for the lists include the following :

A scalar product is given by a dot

However, one needs to be careful with length of the objects.

Other useful operations include

Also have a look at commands Insert, Delete and many others in the help. The name of the command suggests unambiguously what it performs with a given list.

To get an element of the list one indicates its position.

Here - 1 means the first element counted from the end.

If you do not know how many elements are in the list, you can always verify this by using Length

A similar command for the dimension of the list is Dimensions

This counts the elements of the first level in the list.

In applications one often encounters the problem to verify whether a given element is in the list and if so, one might require further its position.

Here Position takes account of the nesting of lists.

Since the lists can be nested, it is useful to know that they can always be flattened.

To get rid of repeated elements one uses Union

From a given list one can get a list of permutations and other lists of a given length with all elements of the original list

Apply and Map

Let us form a new expression from the list and the other way round.

Another example is

A more complicated example is to generate a list of coefficients (maybe useful for polynomial expressions)

Here /@ means Map.

Here there is a trivial example of forming a list.

Therefore, the command /@ works as follows. It applies the function to every element of the list k. (Here we meet an example of a pure function, the concept which will be discused below.)

A very important concept in Mathematica is that of evaluation. In Mathematica evaluation always takes place after you write some input and press Shift + Enter. The process of evaluation is quite complicated, and follows a definite sequence of steps. Understanding this process is important in advanced Mathematica programming and we will return to this in the future. Often the evaluation process takes place even if nothing seems to happen. For example:

FullForm[Hold[2 + 3*I]]

An interesting special case are graphics.

We see that a plot of a function is also a Mathematica Graphics object. One can therefore use the Mathematica programing language to control every detail of a graphic. Graphic programming in Mathematica is a whole big subject, but we will see a few examples later on.

It is possible to think of Mathematica as an algebraic object, somewhat like a ring, with partial addition and multiplication. This means that you can perform algebraic operations which are purely formal, for example, you can raise a number to the power of a graphic:

In some situations arithmetical operations on objecs of different kind are defined, for example, it is possible to add a number (or a symbol) to a list:

However, in certain cases, trying to perform such an operation on objects of different kind will cause a error message:

Here is an example of abstract algebraic manipulation performed on strings:

The evaluation loop.

When you enter an input expression Mathematica’s Kernel evaluates in a very definite order. Understanding this order is important for Mathematica programming. The evaluation order will be described carefully later once we learn about rules and patterns. However, the basic idea is this: Mathematica evaluates each part of the expression by turn, starting with the Head. It applies all rules it knows for the expression, first user defined then built in ones, until it can no longer find a rule. Then it stops and “returns” the result. (Sometimes this evaluation will not stop and we get into an infinite loop. Actually Mathematica will almost always detect such situations and will stop, unless we change the defaults to make it run for ever).

Mathematica allows many different styles of programming. There is one style that, although not unique to Mathematica, distinguishes it from most other similar systems. This is the possibility of using "patterns" and "re-write rules" or just "rules".

The basic concepts in this kind of programming are "rule" and "pattern". Rules can be local and global.

Local Rules

A local rule always has the form

or

Note that:

The difference between Rule and RuleDelayed will be explained below. Most often Rule is used together with the function ReplaceAll (see also Replace):

Here are some examples of using rules (in some of these examples the output appears in TraditionalForm).

Now we use a more general rule. This time we use a "pattern"

The reason for the above result is that ReplaceAll starts looking for a match starting at the top level of the expression and when it finds a match it stops looking for more. If we want to find a match at a different level we can use the function Replace with a level specification. For example, here we replace everything on level 3 of the expression with ?.

These examples illustrate some of the very many ways of forming patterns in Mathematica. The most basic pattern is x_ which stands for anything that is (locally) assigned the name x.

Here is an example where Rule and RuleDelayed give different answers:

Before using a rule it is a good idea to look at the FullForm of an expression. Here are some possible “traps”:

Rule based programming is very convenient when dealing with graphics.

Many Mathematica functions return a list of rules as the output.

Note that this is actually a list of lists, each containing one rule.

This is very convenient, because we can use ReplaceAll to substitute these rules into other formulas. For example:

Here is a similar example with FindRoot instead of Solve

Links

http : // reference.wolfram.com/mathematica/tutorial/PatternsAndTransformationRules.html

Global Rules ("Functions")

Here is one way to define a "function" in Mathematica:

Although people often call *f* defined in this way a function, actually it is only a "global rule". More precisely, when a definition of this kind is evaluated, Mathematica creates a rule for the symbol *f*, which it uses every time when *f* is used. The rule is stored as a DownValue of *f*:

Here x_ is a "pattern", which stands for "anything", with a temporary name "x". The rule says "change f(anything) to ". HoldPattern prevents evaluation of f(x_) (which would otherwise be replaced by but f[x_] is treated as a pattern for matching purposes.

So what happens when we evaluate definitions of this kind is this: Mathematica makes certain rules, stores them, and then applies them in a certain order. Here is an example:

The order in which rules are applied by Mathematica is roughly determined by two facts; more specific rules are applied before more general rules, and rules of equal generality are applied in the order they are entered.

In addition to DownValues there are also OwnValues and UpValues (and some other Values) created as follows:

When an expression is evaluated, Mathematica applies the rules contained in UpValues, DownValues, and so on in a certain order, after which it applies the built-in rules. It keeps evalutating the resulting expression until it stops changing. Note also that certain built in rules are applied by Mathematica automatically on evaluation but others require using a special function such as Simplify or FullSimplify. For example the transformation

while

does not automatically simplify to 1 but

Some simplifications only work with specific assumptions:

Mathematica generally tries to apply any transformations it knows to an expression until it no longer changes. However, this is not the case when we use ReplaceAll. ReplaceAll looks for patterns in all the parts of an expression, but only looks for one match in each part. So, if we have only more than one rule, we may not obtain all the transformations we wish to get:

In order to obtain all transformations we should use ReplaceRepeated (//.) instead of ReplaceAll (/.).

Another important thing: Options of Mathematica's functions are given as Rules.

The difference between :=and= is exactly the same as that between :› and ›. Note these FullForms:

Consider the following two definitions:

If we apply them to an expression like we will get quite different results:

The reason is that = evaluates the right hand side before assigning the evaluated value to the left hand side, while := assigns the unevaluated right hand side to the left hand side.

Links

http : // reference.wolfram.com/mathematica/tutorial/ManipulatingValueLists.html

http : // reference.wolfram.com/mathematica/tutorial/ManipulatingOptions.html

Pure Functions

In addition to functions defined by means of global rules Mathematica also has "genuine functions", defined as follows:

Note that such a function does not need to have a name (so it is called an anonymous function), although we can of course give it a name:

We can also, of course, in the same way, construct functions of several variables.

There are two problems with this approach. First, it is inconvenient to use letters for variable names. This problem is solved by using the notation #1, #2 ,... for the first, second, third etc., arguments. Thus:

Lastly, the word Function can be replaced by the shorthand & after the end of the function, as in

Predicates (Boolean Functions)

A common class of functions are functions whose value are the Boolean constants True and False. Such functions are called predicates. Most built in Mathematica predicates have names that end in Q:

Here are two ways of defining a predicate that test is a number is larger than 5:

Here is the same thing done using a pure function

Such pure functions can be used in patterns:

In functional programming a very important role is played by functions that take functions as arguments. The most important of these are Map and Apply:

Short notation:

The behavior of Mathematica functions and global rules is affected by so called Attributes. Each built-in function has some attributes, for example

The most important attribute of functions is the attribute Listable. Let's explain briefly what it does.

If we give *f* the Attribute Listable we will not need to use Map.

In addition

The attribute Listable of Plus is the reason for the following behavior:

The attributes Orderless, Flat and OneIdentity are interesting, but complicated. Let’s see an illustration

Another group of important attributes are HoldFirst, HoldAll, HoldRest

Links

http : // reference.wolfram.com/mathematica/tutorial/PureFunctions.html

http : // reference.wolfram.com/mathematica/tutorial/Attributes.html

http : // reference.wolfram.com/mathematica/tutorial/SelectingPartsOfExpressionsWithFunctions.html