27 | Applying Functions Repeatedly |
f[x] applies f to x. f[f[x]] applies f to f[x], or effectively nests the application of f. It’s common to want to repeat or nest a function.
This makes a list of the results of nesting f up to 4 times:
NestList[f, x, 4]
Using Framed as the function makes it a little more obvious what’s going on:
NestList[Framed, x, 5]
If you want to see a list of the results of successive nestings, use NestList. If you only want to see the final result, use Nest.
This gives the final result of 5 levels of nesting:
Nest[Framed, x, 5]
Nestedly apply EdgeDetect to an image, first finding edges, then edges of edges and so on.
Nestedly do edge detection on an image:
NestList[EdgeDetect, \!\(\*
GraphicsBox[RasterBox[CompressedData["
1:eJztmUFuwjAQRSP1JL1Ce5MegQv0CpEQEtyADUvEgiU3gAuwZ8meCyCYvmgk
y2oIDYkT/0W/flAUnPFXbI9nxu+T76/JW1EUn1wfXNW9/SML7vfMAm43O5/t
cLDt1pZLWyxsOrWyrH655wnP+Zc2tBwBdLTb2XxeaWhDWtKetwbC6WSrVVsx
dfIuFhLicrH1uruemNjBWn8cjzabpZHkxBo2O4OJypRIqCcmljsshOvVNpuh
JDmxTy8vfaWhJQVh7b/YcAP3cCjbgKk4miTnn5OfZZt2xbUhPT53F6n80quk
3ybge7NIcjZ5/j4bSn/Sex1soBklOeub+JjeoIm/vASurH1wMhzREDtVheEr
a4NI3JhdjxMlAQS02fU4URJApJ1djxMlAaQA2fU4URLgGYoCUeIgj8suJmbI
KwW/ley80lyDmv5qv8+vxxn7ds19UDNm0IyvRAbxYUFJMG431RzHJPNBU82d
TbLO4BCsyZhq/coka33hi6nVRQPUasgBgvX2ALWziRhq5zgx1M68niDd+eAP
mjcM6Q==
"], {{0, 0}, {80., 81.60000000000001}}, {0, 255},
ColorFunction->RGBColor],
ImageSize->{40.58203125, Automatic},
PlotRange->{{0, 80.}, {0, 81.60000000000001}}]\), 6]
Use a pure function to both edge-detect and color-negate at each step:
NestList[ColorNegate[EdgeDetect[#]] &, \!\(\*
GraphicsBox[RasterBox[CompressedData["
1:eJztmUFuwjAQRSP1JL1Ce5MegQv0CpEQEtyADUvEgiU3gAuwZ8meCyCYvmgk
y2oIDYkT/0W/flAUnPFXbI9nxu+T76/JW1EUn1wfXNW9/SML7vfMAm43O5/t
cLDt1pZLWyxsOrWyrH655wnP+Zc2tBwBdLTb2XxeaWhDWtKetwbC6WSrVVsx
dfIuFhLicrH1uruemNjBWn8cjzabpZHkxBo2O4OJypRIqCcmljsshOvVNpuh
JDmxTy8vfaWhJQVh7b/YcAP3cCjbgKk4miTnn5OfZZt2xbUhPT53F6n80quk
3ybge7NIcjZ5/j4bSn/Sex1soBklOeub+JjeoIm/vASurH1wMhzREDtVheEr
a4NI3JhdjxMlAQS02fU4URJApJ1djxMlAaQA2fU4URLgGYoCUeIgj8suJmbI
KwW/ley80lyDmv5qv8+vxxn7ds19UDNm0IyvRAbxYUFJMG431RzHJPNBU82d
TbLO4BCsyZhq/coka33hi6nVRQPUasgBgvX2ALWziRhq5zgx1M68niDd+eAP
mjcM6Q==
"], {{0, 0}, {80., 81.60000000000001}}, {0, 255},
ColorFunction->RGBColor],
ImageSize->{44.296875, Automatic},
PlotRange->{{0, 80.}, {0, 81.60000000000001}}]\), 6]
Add another yellow into the blend at each step:
NestList[Blend[{#, Yellow}] &, Red, 20]
If you successively apply a function that adds 1, you just get successive integers.
Nestedly add 1, getting successive numbers:
NestList[# + 1 &, 1, 15]
Nestedly multiply by 2, getting powers of 2.
The result doubles each time, giving a list of powers of 2:
NestList[2*# &, 1, 15]
Nested squaring very soon leads to big numbers:
NestList[#^2 &, 2, 6]
You can make nested square roots too.
Nestedly apply square root:
NestList[Sqrt[1 + #] &, 1, 5]
The decimal version of the result converges quickly (to the golden ratio):
NestList[Sqrt[1 + #] &, 1, 10] // N
RandomChoice randomly chooses from a list. You can use it to create a pure function that, say, randomly either adds +1 or −1.
Randomly add or subtract 1 at each step, starting from 0:
NestList[# + RandomChoice[{+1, -1}] &, 0, 20]
ListLinePlot[NestList[# + RandomChoice[{+1, -1}] &, 0, 500]]
So far, we’ve used NestList iteratively—effectively to perform a chain of applications of a particular function. But you can also use it for recursion, in which the very pattern of applications of the function is itself nested.
This does a chain of applications of the function f:
NestList[f[#] &, x, 3]
The pattern of applications of f is more complicated here:
NestList[f[#, #] &, x, 3]
Adding frames makes it a little easier to see what’s going on:
NestList[Framed[f[#, #]] &, x, 3]
Putting everything in columns shows the nested pattern of function applications.
The nested boxes are recursively combined in twos at each level:
NestList[Framed[Column[{#, #}]] &, x, 3]
NestList[Framed[Grid[{{#, #}, {#, #}}]] &, x, 3]
This forms the beginning of a fractal structure:
NestList[Framed[Grid[{{0, #}, {#, #}}]] &, x, 3]
It’s easy to get some pretty ornate recursive structures:
NestList[Flatten[{#, Rotate[#, 90 °], Rotate[#, 270 °]}] &, "R", 4]
Not all results from recursion are so complicated. Here’s an example that successively adds two shifted copies of a list together, as in {0,1,2,1}+{1,2,1,0}.
Prepend and append 0 to a list, then add together:
NestList[Join[{0}, #] + Join[#, {0}] &, {1}, 5]
If you put the result in a grid, it forms Pascal’s triangle of binomial coefficients:
NestList[Join[{0}, #] + Join[#, {0}] &, {1}, 8] // Grid
NestList is set up to recursively generate a list by repeatedly applying a function that gives a single result. But what if one has a function that gives multiple results? A list is no longer sufficient to represent what happens; instead one has to use a graph. And that’s where NestGraph comes in.
If one actually only has one result at each step, NestGraph produces a simple chain of results:
NestGraph[{f[#]}&, x, 5, VertexLabels->Automatic]
With two results at each step, however, it gives a tree:
NestGraph[{f[#],g[#]}&, x, 3, VertexLabels->Automatic]
There are two edges that come out of each node here, one representing the result of applying f, the other representing the result of applying g.
With the functions 2# and 2#+1, you still get a tree:
NestGraph[{2 #, 2 # + 1} &, 1, 3, VertexLabels -> All]
But with 2# and #+1, you don’t:
NestGraph[{2 #, # + 1} &, 1, 4, VertexLabels -> All]
With {2#, 2#+1}&, it works out that you’re always generating different numbers. But with {2#,#+1}&, you sometimes end up generating the same number in different ways. For example, 2#& applied to 2 gives 4. #+1& applied to 2 gives 3. But applying #+1& to 3 again gives 4. So this means there are two ways to get to 4, as represented by two “branches” merging at 4 in the graph generated by NestGraph.
Let’s say our function takes any country and gives a list of countries that border it. Starting from Switzerland, here’s the graph we get after one step, in effect showing the immediate neighbors of Switzerland.
One step in generating a graph of bordering countries around Switzerland:
NestGraph[#["BorderingCountries"] &,
Entity["Country", "Switzerland"], 1, VertexLabels -> All]
NestGraph[#["BorderingCountries"] &,
Entity["Country", "Switzerland"], 2, VertexLabels -> All]
The result of NestGraph is here showing us a network that includes not just immediate neighbors, but also neighbors of neighbors.
As another example, start from the word “hello” and successively connect every word to 3 words in the list of common words that Nearest considers nearest to it.
Create a network of nearby words with respect to 1-letter changes:
NestGraph[Nearest[WordList[], #, 3] &, "hello", 4,
VertexLabels -> All]
Within this graph, we can now find the shortest path from “hello” to “fallow”:
FindShortestPath[\!\(\*
GraphicsBox[
NamespaceBox["NetworkGraphics",
DynamicModuleBox[{Typeset`graph = HoldComplete[
Graph[{"hello", "cello", "hallo", "cell", "hall", "hallow", "bell",
"call", "all", "ball", "allow", "callow", "ail", "alb",
"aglow", "allot", "bail", "belle", "fallow"}, {{{1, 2}, {1,
3}, {1, 1}, {2, 4}, {2, 2}, {2, 1}, {3, 5}, {3, 3}, {3, 6}, {
4, 7}, {4, 8}, {4, 4}, {5, 9}, {5, 10}, {5, 5}, {6, 11}, {6,
12}, {6, 6}, {9, 13}, {9, 14}, {9, 9}, {11, 15}, {11, 16}, {
11, 11}, {10, 9}, {10, 17}, {10, 10}, {7, 10}, {7, 7}, {7,
18}, {8, 9}, {8, 10}, {8, 8}, {12, 11}, {12, 12}, {12, 19}},
Null}]]},
TagBox[GraphicsGroupBox[GraphicsComplexBox[CompressedData["
1:eJw1V3k0FVrUv5cyz/e69xqLUNKopDTsLTJElMrcIA1EZEj0yhBFj2SIh0xR
FOmJSMm7EhnKPM9zdM3zdPHdb63vO/+ctdfZ56y9fmfv/fttmSuORtfYCASC
NpFA+N89K06nWN9fCNW4dBs7J3ogLc6wkLxIwpaan//2NhXCrX9Cfcyr+NCz
3NEI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8XqFFxOxQ
6njitUUI734vkzZIwHId9XsiOYugkbTIU3yXgPa8XrmmlYsgMWfg9Dc7AQeu
CGzjzFgEPQ3F37Gb1kFq4oYIu/EimBIvvMzWYtVHFDvxRuUC/Og0Jog8WQXx
R2HyByQWgIfSdLZ5ignDRYbcA7qsOfBF2em+J0xwm6lN+MTSIYq0xF49EyaY
3oqfELk/C2v626N5LzDhfwAopY8S
"], {
{Hue[0.6, 0.7, 0.5], Opacity[0.7], Arrowheads[Medium],
ArrowBox[BezierCurveBox[{1, 23, 26, 28, 34, 36, 39, 1},
SplineDegree->7], 0.055905301441446106`],
ArrowBox[
BezierCurveBox[{1, {4.62759957316787,
0.47186084003209017`}, 2}], 0.055905301441446106`],
ArrowBox[{1, 3}, 0.055905301441446106`],
ArrowBox[
BezierCurveBox[{2, {4.496795865781245,
0.18897740861252663`}, 1}], 0.055905301441446106`],
ArrowBox[BezierCurveBox[{2, 78, 81, 83, 89, 91, 94, 2},
SplineDegree->7], 0.055905301441446106`],
ArrowBox[{2, 4}, 0.055905301441446106`],
ArrowBox[
BezierCurveBox[{3, 101, 104, 106, 112, 114, 117, 3},
SplineDegree->7], 0.055905301441446106`],
ArrowBox[{3, 5}, 0.055905301441446106`],
ArrowBox[{3, 6}, 0.055905301441446106`],
ArrowBox[
BezierCurveBox[{4, 124, 127, 129, 135, 137, 140, 4},
SplineDegree->7], 0.055905301441446106`],
ArrowBox[{4, 7}, 0.055905301441446106`],
ArrowBox[{4, 8}, 0.055905301441446106`],
ArrowBox[
BezierCurveBox[{5, 147, 150, 152, 158, 160, 163, 5},
SplineDegree->7], 0.055905301441446106`],
ArrowBox[{5, 9}, 0.055905301441446106`],
ArrowBox[{5, 10}, 0.055905301441446106`],
ArrowBox[
BezierCurveBox[{6, 170, 173, 175, 181, 183, 186, 6},
SplineDegree->7], 0.055905301441446106`],
ArrowBox[{6, 11}, 0.055905301441446106`],
ArrowBox[{6, 12}, 0.055905301441446106`],
ArrowBox[
BezierCurveBox[{7, 193, 196, 198, 204, 206, 209, 7},
SplineDegree->7], 0.055905301441446106`],
ArrowBox[{7, 10}, 0.055905301441446106`],
ArrowBox[{7, 18}, 0.055905301441446106`],
ArrowBox[
BezierCurveBox[{8, 216, 219, 221, 227, 229, 232, 8},
SplineDegree->7], 0.055905301441446106`],
ArrowBox[{8, 9}, 0.055905301441446106`],
ArrowBox[{8, 10}, 0.055905301441446106`],
ArrowBox[
BezierCurveBox[{9, 239, 242, 244, 250, 252, 255, 9},
SplineDegree->7], 0.055905301441446106`],
ArrowBox[{9, 13}, 0.055905301441446106`],
ArrowBox[{9, 14}, 0.055905301441446106`],
ArrowBox[{10, 9}, 0.055905301441446106`],
ArrowBox[
BezierCurveBox[{10, 262, 265, 267, 273, 275, 278, 10},
SplineDegree->7], 0.055905301441446106`],
ArrowBox[{10, 17}, 0.055905301441446106`],
ArrowBox[
BezierCurveBox[{11, 285, 288, 290, 296, 298, 301, 11},
SplineDegree->7], 0.055905301441446106`],
ArrowBox[{11, 15}, 0.055905301441446106`],
ArrowBox[{11, 16}, 0.055905301441446106`],
ArrowBox[{12, 11}, 0.055905301441446106`],
ArrowBox[
BezierCurveBox[{12, 308, 311, 313, 319, 321, 324, 12},
SplineDegree->7], 0.055905301441446106`],
ArrowBox[{12, 19}, 0.055905301441446106`]},
{Hue[0.6, 0.2, 0.8], EdgeForm[{GrayLevel[0], Opacity[0.7]}],
DiskBox[1, 0.055905301441446106],
DiskBox[2, 0.055905301441446106],
DiskBox[3, 0.055905301441446106],
DiskBox[4, 0.055905301441446106],
DiskBox[5, 0.055905301441446106],
DiskBox[6, 0.055905301441446106],
DiskBox[7, 0.055905301441446106],
DiskBox[8, 0.055905301441446106],
DiskBox[9, 0.055905301441446106],
DiskBox[10, 0.055905301441446106],
DiskBox[11, 0.055905301441446106],
DiskBox[12, 0.055905301441446106],
DiskBox[13, 0.055905301441446106],
DiskBox[14, 0.055905301441446106],
DiskBox[15, 0.055905301441446106],
DiskBox[16, 0.055905301441446106],
DiskBox[17, 0.055905301441446106],
DiskBox[18, 0.055905301441446106],
DiskBox[19, 0.055905301441446106]}}]],
MouseAppearanceTag["NetworkGraphics"]],
AllowKernelInitialization->False]],
DefaultBaseStyle->{"NetworkGraphics",
FrontEnd`GraphicsHighlightColor -> Hue[0.8, 1., 0.6]},
FormatType->TraditionalForm,
FrameTicks->None,
ImageSize->{232.7659912109375, Automatic}]\), "hello", "fallow"]
NestList[ f,x,n] | make a list of applying f to x up to n times | |
Nest [f,x,n] | give the result of applying f to x exactly n times | |
NestGraph[ f,x,n] | make a graph by nestedly applying f starting with x |
27.1Make a list of the results of nesting Blur up to 10 times, starting with a rasterized size-30 “X”. »
27.2Start with x, then make a list by nestedly applying Framed up to 10 times, using a random background color each time. »
27.3Start with a size-50 “A”, then make a list of nestedly applying a frame and a random rotation 5 times. »
27.4Make a line plot of 100 iterations of the logistic map iteration 4 #(1-#)&, starting from 0.2. »
27.6Create a list of the first 10 powers of 3 (starting at 0) by nested multiplication. »
27.7Make a list of the result of nesting the (Newton’s method) function (#+2/#)/2& up to 5 times starting from 1.0, and then subtract from all the results. »
27.8Make graphics of a 1000-step 2D random walk which starts at {0, 0}, and in which at each step a pair of random numbers between −1 and +1 are added to the coordinates. »
27.9Make an array plot of 50 steps of Pascal’s triangle modulo 2 by starting from {1} and nestedly joining {0} at the beginning and at the end, and adding these results together modulo 2. »
27.10Generate a graph by starting from 0, then nestedly 10 times connecting each node with value n to ones with values n+1 and 2n. »
27.11Generate a graph obtained by nestedly finding bordering countries starting from the United States, and going 4 iterations. »
+27.1Make a line plot of 100 iterations of linear congruential function Mod[59#, 101]&, starting from 1. »
+27.4Generate a list of the numerical values obtained by up to 10 nestings of the function Sqrt[1+#]&. »
+27.5Make graphics of a 1000-step 3D random walk which starts at {0, 0, 0}, and in which at each step a triple of random numbers between −1 and +1 are added to the coordinates. »
What’s the difference between iteration and recursion?
If one does one thing repeatedly, it’s iteration. When one takes the result of an operation and applies the same operation to it wherever it is, that’s recursion. It’s slightly confusing, because simple cases of recursion are just iteration. NestList always does recursion, but if only one slot appears in the function, the recursion can be “unrolled” into iteration.
What is the relation between nesting, recursion and fractals?
They are very closely related. The definitions aren’t precise, but fractals are basically geometrical forms that exhibit some type of nested or recursive structure.
What is Pascal’s triangle?
It’s a very common structure discussed in elementary mathematics. Its definition is very close to the Wolfram Language code here: at each row each number is computed as the sum of the numbers directly above it and above it to its right. Each row gives the coefficients in the expansion of (1+x)^n.
Is NestGraph like a web crawler?
Conceptually yes. One can think of it as doing the analog of starting from a webpage and then visiting links from that page, and continuing recursively with that process. We’ll see an example of this in Section 44.
Why do some but not all countries have arrows both ways in the bordering countries graph?
If NestGraph was run for enough steps, all countries would have arrows both ways, since if A borders B, then B borders A. But here we’re stopping after just 2 steps, so many of the reverse connections haven’t been reached.
You don’t need to. You can just use Power, as in Table[2^n, {n, 0, 15}]. But it’s nice to see the sequence Plus, Times, Power arise from successive nesting (e.g. NestList[2+#&, 0, 15] is Table[2*n, {n, 0, 15}]).
Is there a way to keep applying a function until nothing is changing?
- NestList effectively traces out a “single path of computation”. NestGraph can be thought of as tracing out multiple “entangled” paths, to yield a multiway graph.
- NestGraph can be used, for example, to generate game graphs by having the results at each step correspond to possible moves in a game.
- Many examples of “nondeterministic computation” can be thought of as finding paths in graphs generated by NestGraph. Examples include theorem proving and parsing.
- The nearest words example can be made much more efficient by first computing a NearestFunction, then using this repeatedly, rather than computing Nearest from scratch for every word. This example is also closely related to NearestNeighborGraph, discussed in Section 22.