Building Up Calculations
Using Previous Results | The Four Kinds of Bracketing in the Wolfram Language |
Defining Variables | Sequences of Operations |
Values for Symbols |
In doing calculations, you will often need to use previous results that you have got. In the Wolfram Language, % always stands for your last result.
% | the last result generated |
%% | the next‐to‐last result |
%%…% (k times) | the k th previous result |
%n | the result on output line Out[n] (to be used with care) |
You will have noticed that all the input and output lines in the Wolfram Language are numbered. You can use these numbers to refer to previous results.
If you use a text‐based interface to the Wolfram System, then successive input and output lines will always appear in order. However, if you use a notebook interface to the Wolfram System, as discussed in "Notebook Interfaces", 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 the Wolfram Language 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 results in a notebook need have no relation to the order in which the results were generated.
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.
x=value | assign a value to the variable x |
x=y=value | assign a value to both x and y |
x=. or Clear[x] | remove any value assigned to x |
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 Wolfram Language session.
Forgetting about definitions you made earlier is the single most common cause of mistakes when using the Wolfram Language. If you set x=5, the Wolfram Language 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.
The variables you define can have almost any name. 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.
The Wolfram Language uses both uppercase and lowercase letters. There is a convention that built‐in Wolfram Language objects always have names starting with uppercase (capital) letters. To avoid confusion, you should always choose names for your own variables that start with lowercase letters.
aaaaa | a variable name containing only lowercase letters |
Aaaaa | a built‐in object whose name begins with a capital letter |
You can type formulas involving variables in the Wolfram Language 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 (x^2) y, not x^(2y). |
When the Wolfram Language transforms an expression such as x+x into 2x, it is treating the variable x in a purely symbolic or formal fashion. In such cases, x is a symbol that can stand for any expression.
Often, however, you need to replace a symbol like x with a definite "value". Sometimes this value will be a number; often it will be another expression.
To take an expression such as 1+2x and replace the symbol x that appears in it with a definite value, you can create a Wolfram Language transformation rule, and then apply this rule to the expression. To replace x with the value 3, you would create the transformation rule x->3. You must type -> as a pair of characters, with no space in between. You can think of x->3 as being a rule in which "x goes to 3".
To apply a transformation rule to a particular Wolfram Language expression, you type expr/.rule. The "replacement operator" /. is typed as a pair of characters, with no space in between.
expr/.x->value | replace x by value in the expression expr |
expr/.{x->xval,y->yval} | perform several replacements |
The replacement operator /. allows you to apply transformation rules to a particular expression. Sometimes, however, you will want to define transformation rules that should always be applied. For example, you might want to replace x with 3 whenever x occurs.
As discussed in "Defining Variables", you can do this by assigning the value 3 to x using x=3. Once you have made the assignment x=3, x will always be replaced by 3, whenever it appears.
You can define the value of a symbol to be any expression, not just a number. You should realize that once you have given such a definition, the definition will continue to be used whenever the symbol appears, until you explicitly change or remove the definition. For most people, forgetting to remove values you have assigned to symbols is the single most common source of mistakes in using the Wolfram Language.
A symbol such as x can serve many different purposes in the Wolfram Language, and in fact, much of the flexibility of the Wolfram Language comes from being able to mix these purposes at will. However, you need to keep some of the different uses of x straight in order to avoid making mistakes. The most important distinction is between the use of x as a name for another expression, and as a symbolic variable that stands only for itself.
Traditional programming languages that do not support symbolic computation allow variables to be used only as names for objects, typically numbers, that have been assigned as values for them. In Wolfram Language, however, x can also be treated as a purely formal variable, to which various transformation rules can be applied. Of course, if you explicitly give a definition, such as x=3, then x will always be replaced by 3, and can no longer serve as a formal variable.
You should understand that explicit definitions such as x=3 have a global effect. On the other hand, a replacement such as expr/.x->3 affects only the specific expression expr. It is usually much easier to keep things straight if you avoid using explicit definitions except when absolutely necessary.
You can always mix replacements with assignments. With assignments, you can give names to expressions in which you want to do replacements, or to rules that you want to use to do the replacements.
There are four kinds of bracketing used in the Wolfram Language. Each kind of bracketing has a very different meaning. It is important that you remember all of them.
(term) | parentheses for grouping |
f[x] | square brackets for functions |
{a,b,c} | curly braces for lists |
v[[i]] |
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[[ {a,b} ]] is, for example, easier to recognize than v[[{a,b}]].
In doing a calculation with the Wolfram Language, 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
|