Language Basics

1. Overview of Mathematica Features. Mathematica as a Calculator.

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.


2. Language Basics_1.gif

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

2. Language Basics_2.gif

2. Language Basics_3.gif

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].

2. Language Basics_4.gif

2. Language Basics_5.gif

2. Language Basics_6.gif

2. Language Basics_7.gif

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

2. Language Basics_8.gif
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:

2. Language Basics_9.gif

2. Language Basics_10.gif

2. Language Basics_11.gif

2. Language Basics_12.gif

2. Language Basics_13.gif

2. Language Basics_14.gif

2. Language Basics_15.gif

2. Language Basics_16.gif

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

Mathematica notebooks

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.

The Unifying Idea of Mathematica

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

Main Features of Mathematica

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:

2. Language Basics_17.gif

2. Language Basics_18.gif

2. Language Basics_19.gif

2. Language Basics_20.gif

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 :

2. Language Basics_21.gif

2. Language Basics_22.gif

2. Language Basics_23.gif

2. Language Basics_24.gif

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.

2. Language Basics_25.gif

2. Language Basics_26.gif

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 :

2. Language Basics_27.gif

The result is :

2. Language Basics_28.gif

The same matrix can be entered like this :

2. Language Basics_29.gif

2. Language Basics_30.gif

2. Language Basics_31.gif

2. Language Basics_32.gif

Here % means the last expression.

2. Language Basics_33.gif

2. Language Basics_34.gif

2. Language Basics_35.gif

2. Language Basics_36.gif

2. Language Basics_37.gif

2. Language Basics_38.gif

Here %% means the last but one expression.


2. Language Basics_39.gif

2. Language Basics_40.gif

2. Language Basics_41.gif

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.   


The use of Ctrl+6:

2. Language Basics_42.gif


2. Language Basics_43.gif


2. Language Basics_44.gif

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:

2. Language Basics_45.gif

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).



All objects in Mathematica programming language are expressions. For example

2. Language Basics_46.gif

2. Language Basics_47.gif

2. Language Basics_48.gif

2. Language Basics_49.gif

2. Language Basics_50.gif

2. Language Basics_51.gif

2. Language Basics_52.gif

2. Language Basics_53.gif

2. Language Basics_54.gif

2. Language Basics_55.gif

2. Language Basics_56.gif

2. Language Basics_57.gif

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

2. Language Basics_58.gif

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:

2. Language Basics_59.gif

2. Language Basics_60.gif

2. Language Basics_61.gif

2. Language Basics_62.gif

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

2. Language Basics_63.gif

2. Language Basics_64.gif

2. Language Basics_65.gif

2. Language Basics_66.gif

2. Language Basics_67.gif

2. Language Basics_68.gif

2. Language Basics_69.gif

2. Language Basics_70.gif

2. Language Basics_71.gif

2. Language Basics_72.gif

2. Language Basics_73.gif

2. Language Basics_74.gif

Note that atoms also have Head:

2. Language Basics_75.gif

2. Language Basics_76.gif

2. Language Basics_77.gif

2. Language Basics_78.gif

2. Language Basics_79.gif

2. Language Basics_80.gif

Note also that :

2. Language Basics_81.gif

2. Language Basics_82.gif

2. Language Basics_83.gif

2. Language Basics_84.gif

2. Language Basics_85.gif

2. Language Basics_86.gif

2. Language Basics_87.gif

2. Language Basics_88.gif

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

2. Language Basics_89.gif

2. Language Basics_90.gif

2. Language Basics_91.gif

2. Language Basics_92.gif

2. Language Basics_93.gif

2. Language Basics_94.gif

2. Language Basics_95.gif

2. Language Basics_96.gif

The full form of  

2. Language Basics_97.gif


2. Language Basics_98.gif

2. Language Basics_99.gif

2. Language Basics_100.gif

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

2. Language Basics_101.gif

2. Language Basics_102.gif

2. Language Basics_103.gif

2. Language Basics_104.gif

2. Language Basics_105.gif

2. Language Basics_106.gif

2. Language Basics_107.gif

2. Language Basics_108.gif

2. Language Basics_109.gif

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:

2. Language Basics_110.gif

2. Language Basics_111.gif

2. Language Basics_112.gif

2. Language Basics_113.gif

2. Language Basics_114.gif

2. Language Basics_115.gif

2. Language Basics_116.gif

2. Language Basics_117.gif

2. Language Basics_118.gif

2. Language Basics_119.gif

2. Language Basics_120.gif

2. Language Basics_121.gif

2. Language Basics_122.gif

2. Language Basics_123.gif

2. Language Basics_124.gif

2. Language Basics_125.gif

2. Language Basics_126.gif

2. Language Basics_127.gif

2. Language Basics_128.gif

2. Language Basics_129.gif

2. Language Basics_130.gif

2. Language Basics_131.gif

2. Language Basics_132.gif

2. Language Basics_133.gif

2. Language Basics_134.gif

2. Language Basics_135.gif

2. Language Basics_136.gif

and so on.

You can also do this from the back :

2. Language Basics_137.gif

2. Language Basics_138.gif

2. Language Basics_139.gif

2. Language Basics_140.gif

2. Language Basics_141.gif

2. Language Basics_142.gif

2. Language Basics_143.gif

2. Language Basics_144.gif

2. Language Basics_145.gif

2. Language Basics_146.gif

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

2. Language Basics_147.gif

2. Language Basics_148.gif

2. Language Basics_149.gif

2. Language Basics_150.gif

2. Language Basics_151.gif


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

2. Language Basics_152.gif

2. Language Basics_153.gif

2. Language Basics_154.gif

2. Language Basics_155.gif

2. Language Basics_156.gif

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

2. Language Basics_157.gif

2. Language Basics_158.gif

2. Language Basics_159.gif

2. Language Basics_160.gif

2. Language Basics_161.gif

2. Language Basics_162.gif

2. Language Basics_163.gif

2. Language Basics_164.gif

2. Language Basics_165.gif

2. Language Basics_166.gif

2. Language Basics_167.gif

2. Language Basics_168.gif

2. Language Basics_169.gif

2. Language Basics_170.gif

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

2. Language Basics_171.gif

2. Language Basics_172.gif

2. Language Basics_173.gif

2. Language Basics_174.gif

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.






2. Working with Lists

One of the most common expressions in Mathematica are lists.

2. Language Basics_175.gif

2. Language Basics_176.gif

2. Language Basics_177.gif

2. Language Basics_178.gif

2. Language Basics_179.gif

2. Language Basics_180.gif

2. Language Basics_181.gif

2. Language Basics_182.gif

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

2. Language Basics_183.gif

2. Language Basics_184.gif

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

2. Language Basics_185.gif

2. Language Basics_186.gif

2. Language Basics_187.gif


One can generate not only numbers but also other expressions :

2. Language Basics_188.gif

2. Language Basics_189.gif

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

2. Language Basics_190.gif

2. Language Basics_191.gif

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

2. Language Basics_192.gif

2. Language Basics_193.gif

2. Language Basics_194.gif

2. Language Basics_195.gif

Basic operations for the lists include the following :

2. Language Basics_196.gif

2. Language Basics_197.gif

2. Language Basics_198.gif

2. Language Basics_199.gif

A scalar product is given by a dot

2. Language Basics_200.gif

2. Language Basics_201.gif

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

2. Language Basics_202.gif

2. Language Basics_203.gif

2. Language Basics_204.gif

Other useful operations include

2. Language Basics_205.gif

2. Language Basics_206.gif

2. Language Basics_207.gif

2. Language Basics_208.gif

2. Language Basics_209.gif

2. Language Basics_210.gif

2. Language Basics_211.gif

2. Language Basics_212.gif

2. Language Basics_213.gif

2. Language Basics_214.gif

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.

2. Language Basics_215.gif

2. Language Basics_216.gif

2. Language Basics_217.gif

2. Language Basics_218.gif

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

2. Language Basics_219.gif

2. Language Basics_220.gif

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

2. Language Basics_221.gif

2. Language Basics_222.gif

2. Language Basics_223.gif

2. Language Basics_224.gif

A similar command for the dimension of the list is Dimensions

2. Language Basics_225.gif

2. Language Basics_226.gif

2. Language Basics_227.gif

2. Language Basics_228.gif

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.

2. Language Basics_229.gif

2. Language Basics_230.gif

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.

2. Language Basics_231.gif

2. Language Basics_232.gif

2. Language Basics_233.gif

2. Language Basics_234.gif

To get rid of repeated elements one uses Union

2. Language Basics_235.gif

2. Language Basics_236.gif

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

2. Language Basics_237.gif

2. Language Basics_238.gif

2. Language Basics_239.gif

2. Language Basics_240.gif

2. Language Basics_241.gif

2. Language Basics_242.gif

Apply and Map

2. Language Basics_243.gif

2. Language Basics_244.gif

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

2. Language Basics_245.gif

2. Language Basics_246.gif

2. Language Basics_247.gif

2. Language Basics_248.gif

2. Language Basics_249.gif

2. Language Basics_250.gif

2. Language Basics_251.gif

2. Language Basics_252.gif

Another example is

2. Language Basics_253.gif

2. Language Basics_254.gif

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

2. Language Basics_255.gif

2. Language Basics_256.gif

Here /@ means Map.  

2. Language Basics_257.gif

2. Language Basics_258.gif

Here there is a trivial example of forming a list.

2. Language Basics_259.gif

2. Language Basics_260.gif

2. Language Basics_261.gif

2. Language Basics_262.gif

Therefore, the command /@ works as follows. It applies the function 2. Language Basics_263.gif 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:

2. Language Basics_264.gif

2. Language Basics_265.gif

2. Language Basics_266.gif

2. Language Basics_267.gif

2. Language Basics_268.gif

2. Language Basics_269.gif

2. Language Basics_270.gif

2. Language Basics_271.gif

2. Language Basics_272.gif

FullForm[Hold[2 + 3*I]]

2. Language Basics_273.gif

2. Language Basics_274.gif

2. Language Basics_275.gif

2. Language Basics_276.gif

2. Language Basics_277.gif

2. Language Basics_278.gif

2. Language Basics_279.gif

2. Language Basics_280.gif

An interesting special case are graphics.


2. Language Basics_281.gif

2. Language Basics_282.gif

2. Language Basics_283.gif

2. Language Basics_284.gif

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:

2. Language Basics_285.gif

2. Language Basics_286.gif

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:

2. Language Basics_287.gif

2. Language Basics_288.gif

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

2. Language Basics_289.gif

2. Language Basics_290.gif

2. Language Basics_291.gif

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

2. Language Basics_292.gif

2. Language Basics_293.gif

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).

Programming using Patterns and Rules

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

2. Language Basics_294.gif

2. Language Basics_295.gif


2. Language Basics_296.gif

2. Language Basics_297.gif

Note that:

2. Language Basics_298.gif

2. Language Basics_299.gif

2. Language Basics_300.gif

2. Language Basics_301.gif

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

2. Language Basics_302.gif

2. Language Basics_303.gif

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

2. Language Basics_304.gif

2. Language Basics_305.gif

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

2. Language Basics_306.gif

2. Language Basics_307.gif

2. Language Basics_308.gif

2. Language Basics_309.gif

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 ?.

2. Language Basics_310.gif

2. Language Basics_311.gif

2. Language Basics_312.gif

2. Language Basics_313.gif

2. Language Basics_314.gif

2. Language Basics_315.gif

2. Language Basics_316.gif

2. Language Basics_317.gif

2. Language Basics_318.gif

2. Language Basics_319.gif

2. Language Basics_320.gif

2. Language Basics_321.gif

2. Language Basics_322.gif

2. Language Basics_323.gif

2. Language Basics_324.gif

2. Language Basics_325.gif

2. Language Basics_326.gif

2. Language Basics_327.gif

2. Language Basics_328.gif

2. Language Basics_329.gif

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:

2. Language Basics_330.gif

2. Language Basics_331.gif

2. Language Basics_332.gif

2. Language Basics_333.gif

2. Language Basics_334.gif

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

2. Language Basics_335.gif

2. Language Basics_336.gif

2. Language Basics_337.gif

2. Language Basics_338.gif

2. Language Basics_339.gif

2. Language Basics_340.gif

2. Language Basics_341.gif

2. Language Basics_342.gif

2. Language Basics_343.gif

2. Language Basics_344.gif

2. Language Basics_345.gif

Rule based programming is very convenient when dealing with graphics.

2. Language Basics_346.gif

2. Language Basics_347.gif

2. Language Basics_348.gif

2. Language Basics_349.gif

2. Language Basics_350.gif

2. Language Basics_351.gif

2. Language Basics_352.gif

2. Language Basics_353.gif

2. Language Basics_354.gif

2. Language Basics_355.gif

2. Language Basics_356.gif

2. Language Basics_357.gif

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

2. Language Basics_358.gif

2. Language Basics_359.gif

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:

2. Language Basics_360.gif

2. Language Basics_361.gif

2. Language Basics_362.gif

2. Language Basics_363.gif

2. Language Basics_364.gif

2. Language Basics_365.gif

Here is a similar example with FindRoot instead of Solve

2. Language Basics_366.gif

2. Language Basics_367.gif

2. Language Basics_368.gif

2. Language Basics_369.gif


http : //

Global Rules ("Functions")

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

2. Language Basics_370.gif

2. Language Basics_371.gif

2. Language Basics_372.gif

2. Language Basics_373.gif

2. Language Basics_374.gif

2. Language Basics_375.gif

2. Language Basics_376.gif

2. Language Basics_377.gif

2. Language Basics_378.gif

2. Language Basics_379.gif


2. Language Basics_380.gif

2. Language Basics_381.gif

2. Language Basics_382.gif

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:

2. Language Basics_383.gif

2. Language Basics_384.gif

2. Language Basics_385.gif

Here x_ is a "pattern", which stands for "anything", with a temporary name "x". The rule says "change f(anything) to 2. Language Basics_386.gif". HoldPattern prevents evaluation of f(x_) (which would otherwise be replaced by 2. Language Basics_387.gif 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:

2. Language Basics_388.gif

2. Language Basics_389.gif

2. Language Basics_390.gif

2. Language Basics_391.gif

2. Language Basics_392.gif

2. Language Basics_393.gif

2. Language Basics_394.gif

2. Language Basics_395.gif

2. Language Basics_396.gif

2. Language Basics_397.gif

2. Language Basics_398.gif

2. Language Basics_399.gif

2. Language Basics_400.gif

2. Language Basics_401.gif

2. Language Basics_402.gif

2. Language Basics_403.gif

2. Language Basics_404.gif

2. Language Basics_405.gif

2. Language Basics_406.gif

2. Language Basics_407.gif

2. Language Basics_408.gif

2. Language Basics_409.gif

2. Language Basics_410.gif

2. Language Basics_411.gif

2. Language Basics_412.gif

2. Language Basics_413.gif

2. Language Basics_414.gif

2. Language Basics_415.gif

2. Language Basics_416.gif

2. Language Basics_417.gif

2. Language Basics_418.gif

2. Language Basics_419.gif

2. Language Basics_420.gif

2. Language Basics_421.gif

2. Language Basics_422.gif

2. Language Basics_423.gif

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:

2. Language Basics_424.gif

2. Language Basics_425.gif

2. Language Basics_426.gif

2. Language Basics_427.gif

2. Language Basics_428.gif

2. Language Basics_429.gif

2. Language Basics_430.gif

2. Language Basics_431.gif

2. Language Basics_432.gif

2. Language Basics_433.gif

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

2. Language Basics_434.gif

2. Language Basics_435.gif

2. Language Basics_436.gif


2. Language Basics_437.gif

2. Language Basics_438.gif

does not automatically simplify to 1 but

2. Language Basics_439.gif

2. Language Basics_440.gif

Some simplifications only work with specific assumptions:

2. Language Basics_441.gif

2. Language Basics_442.gif

2. Language Basics_443.gif

2. Language Basics_444.gif

2. Language Basics_445.gif

2. Language Basics_446.gif

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:

2. Language Basics_447.gif

2. Language Basics_448.gif

2. Language Basics_449.gif

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

2. Language Basics_450.gif

2. Language Basics_451.gif

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

2. Language Basics_452.gif

2. Language Basics_453.gif

2. Language Basics_454.gif

2. Language Basics_455.gif

2. Language Basics_456.gif

2. Language Basics_457.gif

The difference between := and =

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

2. Language Basics_458.gif

2. Language Basics_459.gif

2. Language Basics_460.gif

2. Language Basics_461.gif

Consider the following two definitions:

2. Language Basics_462.gif

2. Language Basics_463.gif

2. Language Basics_464.gif

2. Language Basics_465.gif

2. Language Basics_466.gif

2. Language Basics_467.gif

If we apply them to an expression like 2. Language Basics_468.gif we will get quite different results:

2. Language Basics_469.gif

2. Language Basics_470.gif

2. Language Basics_471.gif

2. Language Basics_472.gif

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.


http : //

http : //

Functions and Functional Programming

Pure Functions

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

2. Language Basics_473.gif

2. Language Basics_474.gif

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:

2. Language Basics_475.gif

2. Language Basics_476.gif

2. Language Basics_477.gif

2. Language Basics_478.gif

2. Language Basics_479.gif

2. Language Basics_480.gif

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

2. Language Basics_481.gif

2. Language Basics_482.gif

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:

2. Language Basics_483.gif

2. Language Basics_484.gif

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

2. Language Basics_485.gif

2. Language Basics_486.gif

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:

2. Language Basics_487.gif

2. Language Basics_488.gif

2. Language Basics_489.gif

2. Language Basics_490.gif

2. Language Basics_491.gif

2. Language Basics_492.gif

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

2. Language Basics_493.gif

2. Language Basics_494.gif

2. Language Basics_495.gif

2. Language Basics_496.gif

2. Language Basics_497.gif

Here is the same thing done using a pure function

2. Language Basics_498.gif

2. Language Basics_499.gif

Such pure functions can be used in patterns:

2. Language Basics_500.gif

2. Language Basics_501.gif

2. Language Basics_502.gif

2. Language Basics_503.gif

2. Language Basics_504.gif

2. Language Basics_505.gif

2. Language Basics_506.gif

Functions that take Functions as arguments

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:

2. Language Basics_507.gif

2. Language Basics_508.gif

2. Language Basics_509.gif

2. Language Basics_510.gif

2. Language Basics_511.gif

2. Language Basics_512.gif

2. Language Basics_513.gif

2. Language Basics_514.gif

2. Language Basics_515.gif

2. Language Basics_516.gif

2. Language Basics_517.gif

2. Language Basics_518.gif

2. Language Basics_519.gif

2. Language Basics_520.gif

2. Language Basics_521.gif

2. Language Basics_522.gif

2. Language Basics_523.gif

2. Language Basics_524.gif

2. Language Basics_525.gif

Short notation:

2. Language Basics_526.gif

2. Language Basics_527.gif

2. Language Basics_528.gif

2. Language Basics_529.gif

Attributes and Listability

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

2. Language Basics_530.gif

2. Language Basics_531.gif

2. Language Basics_532.gif

2. Language Basics_533.gif

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

2. Language Basics_534.gif

2. Language Basics_535.gif

2. Language Basics_536.gif

2. Language Basics_537.gif

2. Language Basics_538.gif

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

2. Language Basics_539.gif

2. Language Basics_540.gif

2. Language Basics_541.gif

In addition

2. Language Basics_542.gif

2. Language Basics_543.gif

2. Language Basics_544.gif

2. Language Basics_545.gif

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

2. Language Basics_546.gif

2. Language Basics_547.gif

2. Language Basics_548.gif

2. Language Basics_549.gif

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

2. Language Basics_550.gif

2. Language Basics_551.gif

2. Language Basics_552.gif

2. Language Basics_553.gif

2. Language Basics_554.gif

2. Language Basics_555.gif

2. Language Basics_556.gif

2. Language Basics_557.gif

2. Language Basics_558.gif

2. Language Basics_559.gif

2. Language Basics_560.gif

2. Language Basics_561.gif

Another group of important attributes are HoldFirst, HoldAll, HoldRest

2. Language Basics_562.gif

2. Language Basics_563.gif

2. Language Basics_564.gif

2. Language Basics_565.gif

2. Language Basics_566.gif

2. Language Basics_567.gif

2. Language Basics_568.gif

2. Language Basics_569.gif

2. Language Basics_570.gif

2. Language Basics_571.gif

2. Language Basics_572.gif


http : //

http : //

http : //

http : //