Movatterモバイル変換


[0]ホーム

URL:


WOLFRAM

Wolfram Language & System Documentation Center
Building Up Calculations

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)
thekth previous result
%n
the result on output lineOut[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 lines2 and3 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 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.
Defining Variables
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.
This sets the value of thevariablex to be5:
Wheneverx appears, the Wolfram Language now replaces it with the value5:
This assigns a new value tox:
pi is set to be the numerical value of to 40digit accuracy:
Here is the value you defined forpi:
This gives the numerical value of, to the same accuracy aspi:
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
Assigning values to variables.
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.
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 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 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 meansx timesy.
xy with no space is the variable with namexy.
5x means5 timesx.
x^2y means(x^2) y, notx^(2y).
Some points to watch when using variables in the Wolfram Language.
Values for Symbols
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.
This uses the transformation rulex->3 in the expression1+2x:
You can replacex with any expression. Here every occurrence ofx is replaced by2-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 expressionx^2-9:
expr/.x->value
replacex byvalue in the expressionexpr
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 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.
This assigns the value3 tox:
Nowx will automatically be replaced by3 wherever it appears:
This assigns the expression1+a to be the value ofx:
Nowx is replaced by1+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 forx that will always be used
x=.
remove any value defined forx
Assigning values to symbols.
The symbolx still has the value you assigned to it:
This removes the value you assigned to x:
Nowx has no value defined, so it can be used as a purely symbolic variable:
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.
This assigns a value to the symbolt:
This finds the value oft, and then replacesx by2 in it:
This finds the value oft for a different value ofx:
This finds the value oft whenx is replaced byPi, 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 thanv[[{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:

Related Guides

Top

[8]ページ先頭

©2009-2025 Movatter.jp