(*^
::[ Information =
"This is a Mathematica Notebook file. It contains ASCII text, and can be
transferred by email, ftp, or other text-file transfer utility. It should
be read or edited using a copy of Mathematica or MathReader. If you
received this as email, use your mail application or copy/paste to save
everything from the line containing (*^ down to the line containing ^*)
into a plain text file. On some systems you may have to give the file a
name ending with ".ma" to allow Mathematica to recognize it as a Notebook.
The line below identifies what version of Mathematica created this file,
but it can be opened using any other version as well.";
FrontEndVersion = "Macintosh Mathematica Notebook Front End Version 2.2";
MacintoshStandardFontEncoding;
fontset = title, inactive, noPageBreakBelow, nohscroll, preserveAspect,
groupLikeTitle, center, M7, bold, e8, 24, "Times";
fontset = subtitle, inactive, noPageBreakBelow, nohscroll, preserveAspect,
groupLikeTitle, center, M7, bold, e6, 18, "Times";
fontset = subsubtitle, inactive, noPageBreakBelow, nohscroll,
preserveAspect, groupLikeTitle, center, M7, italic, e6, 14, "Times";
fontset = section, inactive, noPageBreakBelow, nohscroll, preserveAspect,
groupLikeSection, grayBox, M22, bold, a20, 18, "Times";
fontset = subsection, inactive, noPageBreakBelow, nohscroll,
preserveAspect, groupLikeSection, blackBox, M19, bold, a15, 14, "Times";
fontset = subsubsection, inactive, noPageBreakBelow, nohscroll,
preserveAspect, groupLikeSection, whiteBox, M18, bold, a12, 12, "Times";
fontset = text, inactive, nohscroll, noKeepOnOnePage, preserveAspect, M7,
12, "Times";
fontset = smalltext, inactive, nohscroll, noKeepOnOnePage, preserveAspect,
M7, 10, "Times";
fontset = input, noPageBreakInGroup, preserveAspect, groupLikeInput, M42,
N23, bold, B65535, L-5, 12, "Courier";
fontset = output, output, inactive, noPageBreakInGroup, preserveAspect,
groupLikeOutput, M42, N23, L-5, 12, "Courier";
fontset = message, inactive, noPageBreakInGroup, preserveAspect,
groupLikeOutput, M42, N23, R65535, L-5, 12, "Courier";
fontset = print, inactive, noPageBreakInGroup, preserveAspect,
groupLikeOutput, M42, N23, L-5, 12, "Courier";
fontset = info, inactive, noPageBreakInGroup, preserveAspect,
groupLikeOutput, M42, N23, B65535, L-5, 12, "Courier";
fontset = postscript, PostScript, formatAsPostScript, output, inactive,
noPageBreakInGroup, preserveAspect, groupLikeGraphics, M7, l34, w282, h287,
12, "Courier";
fontset = name, inactive, nohscroll, noKeepOnOnePage, preserveAspect, M7,
italic, 10, "Geneva";
fontset = header, inactive, noKeepOnOnePage, preserveAspect, M7, 12, "Times";
fontset = leftheader, inactive, L2, 12, "Times";
fontset = footer, inactive, noKeepOnOnePage, preserveAspect, center, M7,
12, "Times";
fontset = leftfooter, inactive, L2, 12, "Times";
fontset = help, inactive, nohscroll, noKeepOnOnePage, preserveAspect, M7,
10, "Times";
fontset = clipboard, inactive, nohscroll, noKeepOnOnePage, preserveAspect,
M7, 12, "Times";
fontset = completions, inactive, nohscroll, noKeepOnOnePage,
preserveAspect, M7, 12, "Times";
fontset = special1, inactive, nohscroll, noKeepOnOnePage, preserveAspect,
M7, 12, "Times";
fontset = special2, inactive, nohscroll, noKeepOnOnePage, preserveAspect,
M7, 12, "Times";
fontset = special3, inactive, nohscroll, noKeepOnOnePage, preserveAspect,
M7, 12, "Times";
fontset = special4, inactive, nohscroll, noKeepOnOnePage, preserveAspect,
M7, 12, "Times";
fontset = special5, inactive, nohscroll, noKeepOnOnePage, preserveAspect,
M7, 12, "Times";
currentKernel;
]
:[font = smalltext; inactive; preserveAspect]
Copyright (C) 1997 Rich Neidinger, John Swallow, and Todd Will. Free for
distribution to college and university instructors for personal,
non-commercial use. If these notebooks are used in a course, the authors
request $20 per student.
:[font = title; inactive; preserveAspect]
7. Lists III: Repetition with Lists
:[font = smalltext; inactive; preserveAspect]
Last revision: September 12 1996
:[font = text; inactive; preserveAspect]
In the previous section, we learned how to perform operations repeatedly on
separate values. In this section we learn how to perform operations
repeatedly, using a value from the previous operation in the next: this
sort of repetition is called repetition with feedback.
;[s]
3:0,0;247,1;271,0;275,-1;
2:2,13,9,Times,0,12,0,0,0;1,13,9,Times,2,12,0,0,0;
:[font = section; inactive; Cclosed; preserveAspect; startGroup]
Nest, Fold
:[font = subsection; inactive; Cclosed; preserveAspect; startGroup]
Nest, NestList
:[font = text; inactive; preserveAspect]
We have seen several functions which take another function as an argument,
such as "Map[ ]" and "MapThread[ ]". These higher-order functions, or
"adverbs", as well called them in the last section, are some of
Mathematica's most powerful functions. We present two more, "Nest[ ]" and
"Fold[ ]".
;[s]
3:0,0;119,1;141,0;296,-1;
2:2,13,9,Times,0,12,0,0,0;1,13,9,Times,2,12,0,0,0;
:[font = text; inactive; preserveAspect]
"Nest[ ]" is used to apply the same function repeatedly to a value, in the
following way: each time, the function is applied to the result of the
previous application. For instance, applying the squaring function three
times in this way to the value 2, we would find the square of 2, namely, 4;
its square, namely 16; and finally its square, namely 256. The Mathematica
command "Nest[ ]" takes the function to be applied as the first argument,
the initial value as the second, and the number of repetitions of the
function as the third. Hence our squaring of 2 can be accomplished with
the following command:
:[font = input; preserveAspect]
Clear[square];
square[x_] := x^2;
Nest[square,2,3]
:[font = text; inactive; preserveAspect]
"NestList[ ]" is similar to "Nest[ ]" in that it takes as arguments a
function, a starting value, and the number of times to "iterate" or "nest"
the function, but "NestList[ ]" returns a list of all intermediate
calculations, including the initial value. We can see successive square
roots of 10 as follows:
:[font = input; preserveAspect]
NestList[Sqrt,10,4]
:[font = text; inactive; preserveAspect]
If we want to see the decimals we can wrap this command inside "N[ ]", or
we can just plug in a decimal to start with.
:[font = input; preserveAspect]
NestList[Sqrt,10.0,4]
:[font = text; inactive; preserveAspect]
"Nest[ ]" and "NestList[ ]" are more elegant ways to achieve
repetition---and certainly use fewer keystrokes---than performing the same
operation over and over in separate commands:
:[font = input; preserveAspect]
Sqrt[10.0]
:[font = input; preserveAspect]
Sqrt[%]
:[font = input; preserveAspect]
Sqrt[%]
:[font = input; preserveAspect; endGroup]
Sqrt[%]
:[font = subsection; inactive; Cclosed; preserveAspect; startGroup]
Fold, FoldList
:[font = text; inactive; preserveAspect]
The function "Fold[ ]" is similar to "Nest[ ]", except that the function
named in the first argument to "Fold[ ]" must be one that requires two
arguments, such as "Plus[ ]", "Times[ ]", "Subtract[ ]", or "Divide[ ]",
for example. Since the function requires two arguments, we must examine
how "Fold[ ]" will use the result from one application of the named
function in applying the function again. What happens is that "Fold[ ]"
takes the result of the previous application and uses it as the first
argument in the next application, drawing the second argument in the next
application from a list we provide "Fold[ ]". Each time, then, a new
second argument is drawn from the list. The only issue, then, is how
"Fold[ ]" first begins; what will it use for the first argument of the
first application? The answer is that we must provide this first, initial
value, separately. Let's look at an example. We will use "Plus" for the
function, "a" for the initial value, "{b,c,d,e}" for the list.
:[font = input; preserveAspect]
Clear[a,b,c,d,e];
Fold[Plus,a,{b,c,d,e}]
:[font = text; inactive; preserveAspect]
Mathematica first computes "Plus[a,b]", namely "a+b"; then "Plus[a+b,c]",
namely "a+b+c"; then "Plus[a+b+c,d]",namely "a+b+c+d"; and finally
"Plus[a+b+c+d,e]", namely "a+b+c+d+e". Note in particular how all of the
second arguments came from the list passed to "Fold[ ]" as its third
argument. As one might expect, "FoldList[ ]" returns a list of all
intermediate calculations, including the initial value.
:[font = input; preserveAspect]
FoldList[Plus,a,{b,c,d,e}]
:[font = text; inactive; preserveAspect]
Let us examine another example, using "Times[ ]", the familiar
multiplication operator. Take a moment to guess what the output of the
following would be.
:[font = input; preserveAspect]
FoldList[Times,0,Range[10]]
:[font = text; inactive; preserveAspect]
Not too interesting; we'll find all of the intermediate products of
0*1*2*...*10. If we start with 1, however, we can find 10 factorial, or
1*2*...*10:
:[font = input; preserveAspect]
FoldList[Times,1,Range[2,10]]
:[font = input; preserveAspect; endGroup; endGroup]
10!
:[font = section; inactive; Cclosed; preserveAspect; startGroup]
Repetition With and Without Feedback
:[font = text; inactive; preserveAspect]
"Nest[ ]" and "Fold[ ]" are examples of what we call repetition with
feedback. To explain repetition with feedback, let us define instead what
it is not: repetition without feedback. The function "Table[ ]" is an
example of an implementation of repetition without feedback, which means
that no iteration depends on the one before.
;[s]
5:0,0;53,1;77,0;248,1;275,0;334,-1;
2:3,13,9,Times,0,12,0,0,0;2,13,9,Times,2,12,0,0,0;
:[font = input; preserveAspect]
sayHi := "Hello"
:[font = input; preserveAspect]
Table[sayHi,{5}]
:[font = text; inactive; preserveAspect]
However, "Nest[ ]" and "Fold[ ]" each use the result of the previous
iteration in the current one.
"Nest[ ]" applies a function to the result of the previous iteration, while
"Fold[ ]" applies a function to the result of the previous iteration and a
new value taken from a list.
;[s]
3:0,0;46,1;52,0;279,-1;
2:2,13,9,Times,0,12,0,0,0;1,13,9,Times,2,12,0,0,0;
:[font = subsection; inactive; Cclosed; preserveAspect; startGroup]
Example: Powers of 2
:[font = text; inactive; preserveAspect]
Let's examine two ways to compute successive powers of 2, using repetition
with and without feedback. One way is to determine, ahead of time, what
exponents we need, and then use "Map[ ]" to apply the appropriate function
to each of these powers. Suppose we want the first 10 powers of 2. We use
"Range[ ]" to find the first ten integers:
:[font = input; preserveAspect]
Range[10]
:[font = text; inactive; preserveAspect]
Then we can raise 2 "to" the entire list, i.e., we can raise 2 to each
element of the list using a list operation, "^":
:[font = input; preserveAspect]
2 ^ Range[10]
:[font = text; inactive; preserveAspect]
What Mathematica executed, however, was "2^1", "2^2", "2^3", and so on, to
"2^10". We notice that each result is simply 2 times the previous result;
Mathematica does not need to start with 2 and multiply many 2's each time.
A more efficient way to solve the problem, then, is to use repetition with
feedback, as follows:
:[font = input; preserveAspect]
Clear[times2];
times2[x_] := x*2;
NestList[times2,2,9]
:[font = text; inactive; preserveAspect; endGroup]
Note that we used 9 iterations, not 10, since the initial value is placed
in the list.
:[font = subsection; inactive; Cclosed; preserveAspect; startGroup]
Reading Code
:[font = text; inactive; preserveAspect]
To test your comprehension of the material covered so far, try to determine
the output of the following cells without executing them on your machine.
Then execute the cells to check. This exercise is a good skill to develop,
for those who cannot predict the results of their code are performing more
guessing than problem solving.
;[s]
3:0,0;110,1;118,0;334,-1;
2:2,13,9,Times,0,12,0,0,0;1,13,9,Times,2,12,0,0,0;
:[font = input; preserveAspect]
midpoints[list_] :=
(Drop[list,-1] + Drop[list,1]) / 2
;[s]
2:0,0;58,1;59,-1;
2:1,12,10,Courier,1,12,0,0,65535;1,12,9,Times,0,12,0,0,0;
:[font = input; preserveAspect]
xs = Range[0,1,.2]
:[font = input; preserveAspect]
NestList[midpoints,xs,5]
:[font = input; preserveAspect; endGroup; endGroup]
%//TableForm
^*)