Mathematica Reference Guide
1.2 Building Up Calculations
1.2.1 Using Previous Results
In doing calculations, you will often need to use previous results that you have got. In Mathematica, % always stands for your last result.
% the last result generated
%% the next-to-last result
%% … % H k timesL the k th previous result
% n the result on output line Out@ n D Hto be used with careL
Ways to refer to your previous results.
Here is the first result.
In[1]:= 77 ^ 2 Out[1]= 5929
This adds 1 to the last result.
In[2]:= % + 1 Out[2]= 5930
This uses both the last result, and the result before that.
In[3]:= 3 % + % ^ 2 + %%
Out[3]= 35188619
You will have noticed that all the input and output lines in Mathematica are numbered. You can use these numbers to refer to previous results.
This adds the results on lines 2 and 3 above.
In[4]:= %2 + %3 Out[4]= 35194549
If you use a text-based interface to Mathematica, then successive input and output lines will always appear in order, as they do in the dialogs in this book. However, if you use a notebook interface to Mathematica, as discussed in Section 1.0.1, then successive input and output lines need not appear in order. You can for example “scroll back” and insert your next calculation wherever you want in the notebook. You should realize that % is always defined to be the last result that Mathematica generated. This may or may not be the result that appears immediately above your present position in the notebook. With a notebook interface, the only way to tell when a particular result was generated is to look at the Out[n] label that it has. Because you can insert and delete anywhere in a notebook, the textual ordering of
1.2.2 Defining Variables
When you do long calculations, it is often convenient to give names to your intermediate results. Just as in standard mathematics, or in other computer languages, you can do this by introducing named variables.
This sets the value of the variable x to be 5.
In[1]:= x = 5 Out[1]= 5
Whenever x appears, Mathematica now replaces it with the value 5.
In[2]:= x ^ 2 Out[2]= 25
This assigns a new value to x.
In[3]:= x = 7 + 4 Out[3]= 11
pi is set to be the numerical value of p to 40-digit accuracy.
In[4]:= pi = N[Pi, 40]
Out[4]= 3.141592653589793238462643383279502884197
Here is the value you defined for pi.
In[5]:= pi
Out[5]= 3.141592653589793238462643383279502884197
This gives the numerical value of p2, to the same accuracy as pi.
In[6]:= pi ^ 2
Out[6]= 9.86960440108935861883449099987615113531
x = value assign a value to the variable x x = y = value assign a value to both x and y x =. or Clear@ x D remove any value assigned to x
Assigning values to variables.
It is very important to realize that values you assign to variables are permanent. Once you have assigned a value to a particular variable, the value will be kept until you explicitly remove it. The value will, of course, disappear if you start a whole new Mathematica session.
Forgetting about definitions you made earlier is the single most common cause of mistakes when using Mathematica. If you set x = 5, Mathematica assumes that you always want x to have the value 5, until or unless you explicitly tell it otherwise. To avoid mistakes, you should remove values you have defined as soon as you have finished using them.
† Remove values you assign to variables as soon as you finish using them.
A useful principle in using Mathematica.
The variables you define can have almost any names. There is no limit on the length of their names. One constraint, however, is that variable names can never start with numbers. For example, x2 could be a variable, but 2x means 2*x.
Mathematica uses both upper- and lower-case letters. There is a convention that built-in Mathematica objects always have names starting with upper-case (capital) letters. To avoid confusion, you should always choose names for your own variables that start with lower-case letters.
aaaaa a variable name containing only lower-case letters Aaaaa a built-in object whose name begins with a capital letter
Naming conventions.
You can type formulas involving variables in Mathematica almost exactly as you would in mathematics. There are a few important points to watch, however.
† x y means x times y .
† xy with no space is the variable with name xy .
† 5x means 5 times x .
† x^2y means Hx^2L y , not x^H2yL .
Some points to watch when using variables in Mathematica.
1.2.3 Making Lists of Objects
In doing calculations, it is often convenient to collect together several objects, and treat them as a single entity. Lists give you a way to make collections of objects in Mathematica. As you will see later, lists are very important and general structures in Mathematica.
A list such as {3, 5, 1} is a collection of three objects. But in many ways, you can treat the whole list as a single object. You can, for example, do arithmetic on the whole list at once, or assign the whole list to be the value of a variable.
Here is a list of three numbers.
In[1]:= {3, 5, 1}
Out[1]= 83, 5, 1<
This squares each number in the list, and adds 1 to it.
In[2]:= {3, 5, 1}^2 + 1 Out[2]= 810, 26, 2<
This takes differences between corresponding elements in the two lists. The lists must be the same length.
In[3]:= {6, 7, 8} - {3.5, 4, 2.5}
Out[3]= 82.5, 3, 5.5<
The value of % is the whole list.
In[4]:= %
Out[4]= 82.5, 3, 5.5<
You can apply any of the mathematical functions in Section 1.1.3 to whole lists.
In[5]:= Exp[ % ] // N
Out[5]= 812.1825, 20.0855, 244.692<
Just as you can set variables to be numbers, so also you can set them to be lists.
This assigns v to be a list.
In[6]:= v = {2, 4, 3.1}
Out[6]= 82, 4, 3.1<
Wherever v appears, it is replaced by the list.
In[7]:= v / (v - 1) Out[7]= 92, 43, 1.47619=
1.2.4 Manipulating Elements of Lists
Many of the most powerful list manipulation operations in Mathematica treat whole lists as single objects. Sometimes, however, you need to pick out or set individual elements in a list.
You can refer to an element of a Mathematica list by giving its “index”. The elements are numbered in order, starting at 1.
8 a, b, c < a list
Part@ list, i D or list @@ i DD the i th element of list Hthe first element is list @@1DD L Part@ list, 8 i, j, … <
D or list @@ 8 i, j, … < DD
a list of the i th , j th , … elements of list
Operations on list elements.
This extracts the second element of the list.
In[1]:= {5, 8, 6, 9}[[2]]
Out[1]= 8
This extracts a list of elements.
In[2]:= {5, 8, 6, 9}[[ {3, 1, 3, 2, 4} ]]
Out[2]= 86, 5, 6, 8, 9<
This assigns the value of v to be a list.
In[3]:= v = {2, 4, 7}
Out[3]= 82, 4, 7<
You can extract elements of v.
In[4]:= v[[ 2 ]]
Out[4]= 4
By assigning a variable to be a list, you can use Mathematica lists much like “arrays” in other computer languages.
Thus, for example, you can reset an element of a list by assigning a value to v[[i]].
Part@ v, i D or v @@ i DD extract the i th element of a list Part@ v, i D =
value or v @@ i DD = value
reset the i th element of a list
Array-like operations on lists.
Here is a list.
In[5]:= v = {4, -1, 8, 7}
Out[5]= 84, −1, 8, 7<
This resets the third element of the list.
In[6]:= v[[3]] = 0 Out[6]= 0
Now the list assigned to v has been modified.
In[7]:= v
Out[7]= 84, −1, 0, 7<
1.2.5 The Four Kinds of Bracketing in Mathematica
Over the course of the last few sections, we have introduced each of the four kinds of bracketing used in Mathematica.
Each kind of bracketing has a very different meaning. It is important that you remember all of them.
HtermL parentheses for grouping f @ x D square brackets for functions 8 a, b, c < curly braces for lists
v @@ i DD double brackets for indexing H Part@ v, i D L
The four kinds of bracketing in Mathematica.
When the expressions you type in are complicated, it is often a good idea to put extra space inside each set of brackets.
This makes it somewhat easier for you to see matching pairs of brackets. v[[ 8a, b< ]] is, for example, easier to recognize than v[[8a, b<]].
1.2.6 Sequences of Operations
In doing a calculation with Mathematica, you usually go through a sequence of steps. If you want to, you can do each step on a separate line. Often, however, you will find it convenient to put several steps on the same line. You can do this simply by separating the pieces of input you want to give with semicolons.
expr1; expr2; expr3 do several operations, and give the result of the last one expr1; expr2; do the operations, but print no output
Ways to do sequences of operations in Mathematica.
This does three operations on the same line. The result is the result from the last operation.
In[1]:= x = 4; y = 6; z = y + 6 Out[1]= 12
If you end your input with a semicolon, it is as if you are giving a sequence of operations, with an “empty” one at the end. This has the effect of making Mathematica perform the operations you specify, but display no output.
expr ; do an operation, but display no output
Inhibiting output.
Putting a semicolon at the end of the line tells Mathematica to show no output.
In[2]:= x = 67 - 5 ;
You can still use % to get the output that would have been shown.
In[3]:= % Out[3]= 62