Anda di halaman 1dari 3

Building Up Calculations

Using Previous Results


In doing calculations, you will often need to use previous results that you have got. In Mathemat ica, % always stands for your last result.

the last result generated

%%

the next-to-last result

%% % Hk timesL

the kth previous result

%n

the result on output line Out@nD (to be used with care)

Ways to refer to your previous results.


Here is the first result.
In[1]:=

77 ^ 2

Out[1]= 5929

This adds 1 to the last result.


In[2]:=
Out[2]=

%+1
5930

This uses both the last result, and the result before that.
In[3]:=
Out[3]=

3 % + % ^ 2 + %%
35 188 619

You will have noticed that all the input and output lines in Mathematica are numbered. You can
use these numbers to refer to previous results.
This adds the results on lines 2 and 3 above.
In[4]:=
Out[4]=

%2 + %3
35 194 549

If you use a text-based interface to Mathematica, then successive input and output lines will
always appear in order. However, if you use a notebook interface to Mathematica, 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 note-

Core Language

You can replace x with any expression. Here every occurrence of x is replaced by 2 - y.
In[2]:=
Out[2]=

1 + x + x ^ 2 . x -> 2 - y
3 + H2 - yL2 - y

Here is a transformation rule. Mathematica treats it like any other symbolic expression.
In[3]:=
Out[3]=

x -> 3 + y
x3+y

This applies the transformation rule on the previous line to the expression x ^ 2 - 9.
In[4]:=
Out[4]=

x ^ 2 - 9 . %
-9 + H3 + yL2

expr.x->value

replace x by value in the expression expr

expr.8x->xval,y->yval<

perform several replacements

Replacing symbols by values in expressions.


You can apply rules together by putting the rules in a list.
In[5]:=
Out[5]=

Hx + yL Hx - yL ^ 2 . 8x -> 3, y -> 1 - a<


H4 - aL H2 + aL2

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.
In[6]:=
Out[6]=

x=3
3

Now x will automatically be replaced by 3 wherever it appears.


In[7]:=
Out[7]=

x^2 - 1
8

This assigns the expression 1 + a to be the value of x.

Core Language

335

Input of Boxes
Use a palette
Use control keys
Ways to input boxes.

Control Keys
Ctrl+2 or Ctrl+@

square root

Ctrl+5 or Ctrl+%

switch to alternate position (e.g. subscript to superscript)

Ctrl+6 or Ctrl+^

superscript

Ctrl+7 or Ctrl+&

overscript

Ctrl+9 or Ctrl+(

begin a new cell within an existing cell

Ctrl+0 or Ctrl+)

end a new cell within an existing cell

Ctrl+- or Ctrl+_

subscript

Ctrl+= or Ctrl+Plus

underscript

Ctrl+Enter

create a new row in a table

Ctrl+,

create a new column in a table

Ctrl+.

expand current selection

Ctrl+/

fraction

Ctrl+Space

return from current position or state

Ctrl+, Ctrl+, Ctrl+, Ctrl+


move an object by minimal increments on the screen
Standard control keys.

On English-language keyboards both forms will work where alternates are given. On other
keyboards the first form should work but the second may not.

Boxes Constructed from Text


When textual input that you give is used to construct boxes, as in StandardForm or
TraditionalForm cells in a notebook, the input is handled slightly differently from when it is
fed directly to the kernel.
The input is broken into tokens, and then each token is included in the box structure as a
separate character string. Thus, for example, xx + yyy is broken into the tokens "xx", "+",
"yyy".

Anda mungkin juga menyukai