Sie sind auf Seite 1von 3

2.

4 Transformation Rules and Definitions 291

2.4.4 Special Forms of Assignment


Particularly when you write procedural programs in Mathematica, you will often need to modify the
value of a particular variable repeatedly. You can always do this by constructing the new value and ex-
plicitly performing an assignment such as x = value. Mathematica, however, provides special notations
for incrementing the values of variables, and for some other common cases.

i++ increment the value of i by 1


i-- decrement i
++i pre-increment i
--i pre-decrement i
i += di add di to the value of i
i -= di subtract di from i
x *= c multiply x by c
x /= c divide x by c

Modifying values of variables.

This assigns the value 7x to the variable In 1]:= t = 7x


t. Out 1]= 7 x

This increments the value of t by 18x. In 2]:= t += 18x


Out 2]= 25 x

The value of t has been modified. In 3]:= t


Out 3]= 25 x

This sets t to 8, multiplies its value by In 4]:= t = 8 t *= 7 t


7, then gives the final value of t. Out 4]= 56

The value of i++ is the value of i before In 5]:= i=5 Printi++] Printi]
the increment is done. 5
6

The value of ++i is the value of i after In 6]:= i=5 Print++i] Printi]
the increment. 6
6

Web sample page from The Mathematica Book, Second Edition, by Stephen Wolfram, published by Addison-Wesley Publishing Company (hard-
cover ISBN 0-201-51502-4; softcover ISBN 0-201-51507-5). To order Mathematica or this book contact Wolfram Research: info@wolfram.com;
http://www.wolfram.com/; 1-800-441-6284.

1991 Wolfram Research, Inc. Permission is hereby granted for web users to make one paper copy of this page for their personal use. Further re-
production, or any copying of machine-readable files (including this one) to any server computer, is strictly prohibited.
292 2. Principles of Mathematica

x = y = value assign the same value to both x and y


{x, y} = {value1 , value2 } assign different values to x and y
{x, y} = {y, x} interchange the values of x and y

Assigning values to several variables at a time.

This assigns the value 5 to x and 8 to y. In 7]:= {x, y} = {5, 8}


Out 7]= {5, 8}

This interchanges the values of x and y. In 8]:= {x, y} = {y, x}


Out 8]= {8, 5}

Now x has value 8. In 9]:= x


Out 9]= 8

And y has value 5. In 10]:= y


Out 10]= 5

You can use assignments to lists to In 11]:= {a, b, c} = {1, 2, 3} {b, a, c} = {a, c, b} {a, b, c}
permute values of variables in any way. Out 11]= {3, 1, 2}

When you write programs in Mathematica, you will sometimes find it convenient to take a list, and
successively add elements to it. You can do this using the functions PrependTo and AppendTo.

PrependTo v, elem] prepend elem to the value of v


AppendTo v, elem] append elem
v = {v, elem} make a nested list containing elem

Assignments for modifying lists.

This assigns the value of v to be the list In 12]:= v = {5, 7, 9}


{5, 7, 9}. Out 12]= {5, 7, 9}

This appends the element 11 to the In 13]:= AppendTov, 11]


value of v. Out 13]= {5, 7, 9, 11}

Now the value of v has been modified. In 14]:= v


Out 14]= {5, 7, 9, 11}

Although AppendTo v, elem] is always equivalent to v = Append v, elem], it is often a convenient


notation. However, you should realize that because of the way Mathematica stores lists, it is usually less
efficient to add a sequence of elements to a particular list than to create a nested structure that consists,

Web sample page from The Mathematica Book, Second Edition, by Stephen Wolfram, published by Addison-Wesley Publishing Company (hard-
cover ISBN 0-201-51502-4; softcover ISBN 0-201-51507-5). To order Mathematica or this book contact Wolfram Research: info@wolfram.com;
http://www.wolfram.com/; 1-800-441-6284.

1991 Wolfram Research, Inc. Permission is hereby granted for web users to make one paper copy of this page for their personal use. Further re-
production, or any copying of machine-readable files (including this one) to any server computer, is strictly prohibited.
2.4 Transformation Rules and Definitions 293

for example, of lists of length 2 at each level. When you have built up such a structure, you can always
reduce it to a single list using Flatten.
This sets up a nested list structure for w. In 15]:= w = {1} Do w = {w, k^2}, {k, 1, 4} ] w
Out 15]= {{{{{1}, 1}, 4}, 9}, 16}

You can use Flatten to unravel the In 16]:= Flattenw]


structure. Out 16]= {1, 1, 4, 9, 16}

Web sample page from The Mathematica Book, Second Edition, by Stephen Wolfram, published by Addison-Wesley Publishing Company (hard-
cover ISBN 0-201-51502-4; softcover ISBN 0-201-51507-5). To order Mathematica or this book contact Wolfram Research: info@wolfram.com;
http://www.wolfram.com/; 1-800-441-6284.

1991 Wolfram Research, Inc. Permission is hereby granted for web users to make one paper copy of this page for their personal use. Further re-
production, or any copying of machine-readable files (including this one) to any server computer, is strictly prohibited.

Das könnte Ihnen auch gefallen