Building Up Calculations

Using Previous Results
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 nexttolast result
%%% (k times)
the k th previous result
%n
the result on output line Out[n] (to be used with care)
Ways to refer to your previous results.
Here is the first result:
This adds 1 to the last result:
This uses both the last result, and the result before that:
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.
This adds the results on lines 2 and 3 above:
If you use a textbased 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.
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:
Whenever x appears, the Wolfram Language now replaces it with the value 5:
This assigns a new value to x:
pi is set to be the numerical value of to 40digit accuracy:
Here is the value you defined for pi:
This gives the numerical value of , to the same accuracy as pi:
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
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 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.
Remove values you assign to variables as soon as you finish using them.
A useful principle in using the Wolfram Language.
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 builtin 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 builtin object whose name begins with a capital letter
Naming conventions.
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).
Some points to watch when using variables in the Wolfram Language.
Values for Symbols
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.
This uses the transformation rule x->3 in the expression 1+2x:
You can replace x with any expression. Here every occurrence of x is replaced by 2-y:
Here is a transformation rule. The Wolfram Language treats it like any other symbolic expression:
This applies the transformation rule on the previous line to the expression x^2-9:
expr/.x->value
replace x by value in the expression expr
expr/.{x->xval,y->yval}
perform several replacements
Replacing symbols by values in expressions.
You can apply rules together by putting the rules in a list:
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.
This assigns the value 3 to x:
Now x will automatically be replaced by 3 wherever it appears:
This assigns the expression 1+a to be the value of x:
Now x is replaced by 1+a:
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.
x=value
define a value for x that will always be used
x=.
remove any value defined for x
Assigning values to symbols.
The symbol x still has the value you assigned to it:
This removes the value you assigned to x:
Now x has no value defined, so it can be used as a purely symbolic variable:
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.
This assigns a value to the symbol t:
This finds the value of t, and then replaces x by 2 in it:
This finds the value of t for a different value of x:
This finds the value of t when x is replaced by Pi, and then evaluates the result numerically:
The Four Kinds of Bracketing in the Wolfram Language
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]]
double brackets for indexing ( Part[v,i] )
The four kinds of bracketing in the Wolfram Language.
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}]].
Sequences of Operations
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
Ways to do sequences of operations in the Wolfram Language.
This does three operations on the same line. The result is the result from the last operation:
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 the Wolfram Language 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 the Wolfram Language to show no output:
You can still use % to get the output that would have been shown: