Building Up Calculations
Building Up Calculations
| Using Previous Results | The Four Kinds of Bracketing inthe 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) | thekth previous result |
| %n | the result on output lineOut[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 theOut[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 givenames to your intermediate results. Just as in standard mathematics, or in other computer languages, you can do this by introducing namedvariables.
| x=value | assign a value to the variablex |
| x=y=value | assign a value to bothx andy |
| x=. or Clear[x] | remove any value assigned tox |
It is very important to realize that values you assign to variables arepermanent. 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 setx=5, the Wolfram Language assumes that youalways wantx to have the value5, 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 neverstart with numbers. For example,x2 could be a variable, but2x means2*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 meansx timesy. |
■xy with no space is the variable with namexy. |
■5x means5 timesx. |
■x^2y means(x^2) y, notx^(2y). |
When the Wolfram Language transforms an expression such asx+x into2x, it is treating the variablex 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 likex with a definite "value". Sometimes this value will be a number; often it will be another expression.
To take an expression such as1+2x and replace the symbolx 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 replacex with the value3, you would create the transformation rulex->3. You must type-> as a pair of characters, with no space in between. You can think ofx->3 as being a rule in which "x goes to3".
To apply a transformation rule to a particular Wolfram Language expression, you typeexpr/.rule. The "replacement operator"/. is typed as a pair of characters, with no space in between.
| expr/.x->value | replacex byvalue in the expressionexpr |
| 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 shouldalways be applied. For example, you might want to replacex with3 wheneverx occurs.
As discussed in"Defining Variables", you can do this byassigning the value3 tox usingx=3. Once you have made the assignmentx=3,x will always be replaced by3, 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 asx 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 ofx straight in order to avoid making mistakes. The most important distinction is between the use ofx 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 asx=3, thenx will always be replaced by3, and can no longer serve as a formal variable.
You should understand that explicit definitions such asx=3 have a global effect. On the other hand, a replacement such asexpr/.x->3 affects only the specific expressionexpr. 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.
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 thanv[[{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 |
Related Guides
[8]ページ先頭
©2009-2025 Movatter.jp

