1.2. BASICS OF SYMBOLIC COMPUTING 13
be numbers or they may be represented by symbols that have been predefined by
mathematica and cannot be changed (e.g., Pi, which is 3.14159 ···).
The arithmetic operations are illustrated by a+b, a-b, abor a*b (multiplication),
a/b, a^b (a
b
). The for tran notation for exponentiation, a**b,isnot recognized by
mathematica. Multiplication and division are left-associative, so a/b*c stands for
(a/b)c and a/b/c is (a/b)/c = a/bc. When no operator symbol is used to indicate
multiplication, symbolic operands (e.g., a and b) must be separated by at least one
space; otherwise they would be interpreted as a single symbol (e.g., ab). A better
practice, followed in most of the coding examples in this book, is to always use the
* symbol for multiplication. Parentheses can be used to control grouping, and must
be used when the grouping is not that obtained in mathematica by default. For
example, a^b^c is interpreted by mathematica as a
b
c
; if the user wants (a
b
)
c
=
a
bc
, he/she must write (a^b)^c. Argument lists for functions or commands are not
surrounded by parentheses, but by square brackets, []. Other grouping symbols or
combinations thereof, such as {}, have special meanings and cannot be used in place
of ()or [].
Commands are ended with Shift/Enter (keep the shift key depressed while
pressing Enter). Enter (by itself) simply permits a continuation of the input on the
next line of the notebook. If ; is the last character before Shift/Enter, printing
of the output (but not its generation) is suppressed. A command can consist of
just an expression, or it may be an assignment, in which case a variable is as-
signed a value (which may itself be symbolic, depending on other variables). The
assignment operator is = and once a variable has been assigned, its occurrence in all
subsequently processed statements (no matter where they physically occur in the
notebook) will be replaced by the assigned value. All output that has been generated
in a mathematica session remains available for the duration of the session, and may
be referred to as % (most recently produced output), %% (next most recent output),
%%...% (k times) (the kth previous output), or %n (the output line Out[n]). Using %n
may cause problems if a notebook is saved and reused, because the output numbering
depends on the session history. Here are a few lines of mathematica input and
output. The user does not type In[..]:=; that is supplied by mathematica when
the statement is processed.
In[1]:= x1 = z + 5
Out[1]= z +5
In[2]:= x1
Out[2]= z +5
In[3]:= % + ymax + 3
Out[3]= ymax + z +8
In[4]:= Sin[Pi/2]
Out[4]= 1
Assigns z + 5 as value of x1
Checks that assignment was
done
% denotes Out[2];
addends are combined
Note style: S and []
mathematica
knows the value
Notice that if a symbol has already been defined when a statement is executed, the
value is used when the result is evaluated. Undefined quantities remain as symbols.
To undefine a symbol x to which something has been assigned, write Clear[x].
A need that often arises is to evaluate an expression for some value of a variable
contained in the expression. For example, we may need to evaluate x1=z+5 for z =1.