Skip to content

Commit 92e7ae5

Browse files
authored
Iterated Function System chapter (algorithm-archivists#692)
* adding text for IFS chapter, smallscale modifications left for images * adding 'completed' initial IFS triangle section * rough draft for IFS chapter * fixing typo in licensing
1 parent 94b0ae2 commit 92e7ae5

19 files changed

+358
-0
lines changed

SUMMARY.md

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
* [Introduction](contents/introduction/introduction.md)
55
* [How To Contribute](contents/how_to_contribute/how_to_contribute.md)
66
* [Plotting](contents/plotting/plotting.md)
7+
* [Iterated Function Systems](contents/IFS/IFS.md)
78
* [Data Structures](contents/data_structures/data_structures.md)
89
* [Stacks and Queues](contents/stacks_and_queues/stacks_and_queues.md)
910
* [Mathematical Background](contents/mathematical_background/mathematical_background.md)

contents/IFS/IFS.md

+220
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,220 @@
1+
# Iterated Function Systems
2+
3+
A few quick notes before we start:
4+
5+
1. For this chapter, we will be following the methodology set by the [plotting chapter](../plotting/plotting.md).
6+
That is to say that the code presented in this chapter will output another file that can be easily plotted by an external plotter.
7+
If you like to use a plotter provided by your language of choice, please modify the code provided to do so.
8+
9+
2. This chapter is currently a subsection to the plotting chapter, but we may extend the algorithm archive in the future with other fractal generation methods, which would require created a new section on fractals, in particular.
10+
This would include a chapter with more rigorous definitions on fractals, which is largely missing from the following discussion.
11+
Please let us know if you are interested!
12+
13+
In this chapter, we will show you how to make one of the most famous fractals via Iterated Function Systems (IFSs): the Sierpinski triangle.
14+
We will also introduce a number of interesting concepts for further exploration, such as chaos games, Hutchinson operators, and attractors.
15+
16+
## The Sierpinski Triangle
17+
18+
To begin the discussion of Iterated Function Systems (IFSs), we will first discuss what might be one of the most famous fractals currently known, the Sierpinski triangle, shown below:
19+
20+
<img class="center" src="res/IFS_triangle_1.png" alt="Sierpinsky Triangle Chaos Game" style="width:100%">
21+
22+
This image is clearly a set of triangles embedded in a larger triangle in such a way that it can be continually cut into three identical pieces and still retain its internal structure.
23+
This idea is known as self-similarity {{"self-similar" | cite }}, and it is usually the first aspect of fractals that will catch an audience's attention.
24+
In fact, there are plenty of uses of fractals and their mathematical underpinnings, such as estimating the coastline of Britain {{ "mandelbrot1967long" | cite}}, identifying fingerprints {{ "jampour2010new" | cite }}, and image compression {{ "fractal-compression" | cite }}{{ "saupe1994review" | cite }}.
25+
In many more rigorous definitions, a fractal can be described as any system that has a non-integer Hausdorff dimension {{ "3b1bfractal" | cite }}{{ "hausdorff" | cite }}{{ "gneiting2012estimators" | cite }}.
26+
Though this is an incredibly interesting concept, the discussion of this chapter will focus primarily on methods to generate fractal patterns and will refrain from delving into this discussion for now and instead focus on the methods by which fractals can be generated through IFSs.
27+
28+
To start, imagine creating a triangle from three points, $$A$$, $$B$$, and $$C$$.
29+
These points can be arbitrarily chosen, but for this conversation, we will constrict them to the vertices of an equilateral triangle, as shown below:
30+
31+
<img class="center" src="res/IFS_triangle_2.png" alt="Triangle Vertices" style="width:100%">
32+
33+
Now let's create three separate functions that can act on a 2-dimensional space:
34+
35+
$$
36+
\begin{align}
37+
f_1(P) &= \frac{P + A}{2}\\
38+
f_2(P) &= \frac{P + B}{2}\\
39+
f_3(P) &= \frac{P + C}{2}\\
40+
\end{align}
41+
$$
42+
43+
Each function will input a particular location in space (here, $$P \in \mathbb{R}^2$$) and output a new location that is the midpoint between the input location and $$A$$, $$B$$, or $$C$$ for functions 1, 2, and 3 respectively.
44+
The union of all of these functions (the set of all possible functions available for use) is often notated as the _Hutchinson operator_ {{ "hutchinson-operator" | cite }}{{ "hutchinson1981fractals" | cite}}, and for this case it would look like this:
45+
46+
$$
47+
H(P) = \bigcup_{i=1}^3f_i(P)
48+
$$
49+
50+
By iteratively using this operator, we can traverse through all possible movements in the set.
51+
For example, let's generate 3 new points that are halfway between $$A$$ and $$B$$, $$B$$ and $$C$$, and $$A$$ and $$C$$, which will be called $$D$$, $$E$$, and $$F$$ respectively.
52+
This is shown below:
53+
54+
<img class="center" src="res/IFS_triangle_3.png" alt="Triangle Midpoints" style="width:100%">
55+
56+
From here, each new point ($$D$$, $$E$$, and $$F$$) will spawn 3 children, and each child will move according to one of the three possible functions in the Hutchinson operator, as shown below:
57+
58+
<div style="text-align:center">
59+
<video style="width:100%" controls>
60+
<source src="res/IFS_triangle_vid_1.mp4" type="video/mp4">
61+
<img class="center" src="res/IFS_triangle_4.png" alt="First Children" style="width:100%">
62+
</video>
63+
</div>
64+
65+
Here, all red children come from $$D$$, green children come from $$E$$ and blue children come from $$F$$.
66+
From here, the children will then spawn 3 more children, each of which will move according to a different function.
67+
Those children will then spawn more children, who act accordingly.
68+
As this process continues on and on, we begin to see an interesting pattern form:
69+
70+
<div style="text-align:center">
71+
<video style="width:100%" controls>
72+
<source src="res/IFS_triangle_vid_2.mp4" type="video/mp4">
73+
<img class="center" src="res/IFS_triangle_5.png" alt="Last children" style="width:100%">
74+
</video>
75+
</div>
76+
77+
This is the Sierpinski triangle.
78+
At first, it might seem like mathematical magic that a simple set of 3 functions can create such a pattern.
79+
After all, why aren't any of the children migrating to the empty spaces in the structure?
80+
This will require some thought, but the simplest answer is that no function within the Hutchinson operator allows for children to enter those spaces.
81+
82+
## What about a square?
83+
84+
When I learned about this for the first time, I began to wonder about other shapes.
85+
Could we create fractal squares? Hexagons? Circles?
86+
Such shapes _seem_ like natural extensions to the triangular Hutchinson operator provided above, but there's a bit of a hitch...
87+
88+
First, let's take 4 points, $$A$$, $$B$$, $$C$$, and $$D$$, this time located at the four vertices of a square, like so:
89+
90+
<img class="center" src="res/IFS_square_1.png" alt="Sierpinsky Triangle Chaos Game" style="width:100%">
91+
92+
In a similar fashion, we'll create 4 functions with $$H(P) = \bigcup_{i=1}^4f_i(P)$$, and $$P \in \mathbb{R}^2$$ such that:
93+
94+
$$
95+
\begin{align}
96+
f_1(P) &= \frac{P + A}{2}\\
97+
f_2(P) &= \frac{P + B}{2}\\
98+
f_3(P) &= \frac{P + C}{2}\\
99+
f_4(P) &= \frac{P + D}{2}\\
100+
\end{align}
101+
$$
102+
103+
If we then create 5 initial points located between all the vertices and allow these points to continually spawn children like before, something peculiar happens:
104+
105+
<div style="text-align:center">
106+
<video style="width:100%" controls>
107+
<source src="res/IFS_square_vid_1.mp4" type="video/mp4">
108+
<img class="center" src="res/IFS_square_2.png" alt="Hutchinson square" style="width:100%">
109+
</video>
110+
</div>
111+
112+
Well, this is essentially a square of squares.
113+
What happened to the self-similar structure we were getting before?
114+
Why isn't this more interesting?
115+
116+
The best answer I have for now is that some Hutchinson operators are interesting and some are not.
117+
On the other hand, this square is a bit more interesting than it first appears, but I think this is easiest to understand when we use the Hutchinson operator in a slightly different way.
118+
119+
## Chaos games and attractors
120+
121+
Until now, we have been using the Hutchinson operator in a computationally costly way.
122+
Every iteration, we generate 3 or 4 new children per child per step of the simulation.
123+
This scales exponentially and means that we will quickly have millions of children to keep track of!
124+
125+
A much more computationally feasible method to use the Hutchinson operator comes in the form of the _chaos game_ {{ "chaos-game" | cite }}{{ "chaos-game-wolf" | cite }}.
126+
Here, instead of tracking children of children, we track a single individual that chooses randomly between the Hutchinson functions, as shown here:
127+
128+
{% method %}
129+
{% sample lang="jl" %}
130+
[import:4-17, lang:"julia"](code/julia/IFS.jl)
131+
{% endmethod %}
132+
133+
If we set the initial points to the on the equilateral triangle we saw before, we can see the Sierpinski triangle again after a few thousand iterations, as shown below:
134+
135+
<div style="text-align:center">
136+
<video style="width:100%" controls>
137+
<source src="res/chaos_vid_1.mp4" type="video/mp4">
138+
<img class="center" src="res/chaos_1.png" alt="Chaos game" style="width:100%">
139+
</video>
140+
</div>
141+
142+
That said, there is something peculiar about the way the chaos game starts.
143+
Essentially, our lone child begins their journey at a random location.
144+
What if that location is off the triangle?
145+
Well, let's test that!
146+
147+
If we start the point at the center of the triangle, it will eventually find its way onto the triangle, and then all subsequent iterations will be on the fractal.
148+
Here, I have plotted the first 20 steps where the wanderer is still looking for the correct shape:
149+
150+
<div style="text-align:center">
151+
<video style="width:100%" controls>
152+
<source src="res/chaos_vid_2.mp4" type="video/mp4">
153+
<img class="center" src="reschaos_2.png" alt="Chaos game with initial points" style="width:100%">
154+
</video>
155+
</div>
156+
157+
It would seem that the wanderer is _attracted_ to the Sierpinski triangle with this set of functions.
158+
That is actually the case.
159+
The truth is that the word _attractor_ is a very loaded term in the literature, but for the purposes of our discussion here, the _attractor_ is any shape defined by the iteration through Hutchinson operator functions.
160+
161+
So let's go back to the square, which seemed like a somewhat random distribution of points:
162+
163+
<img class="center" src="res/IFS_square_2.png" alt="Hutchinson square" style="width:100%">
164+
165+
Even with the chaos game, this will not change; however, we now know that the random distribution isn't truly random.
166+
Rather, it's an attractive plane where our lone wanderer can exist happily within.
167+
That is to say, the 2-dimensional square is, itself, the attractor for that Hutchinson operator, and if we start our journey person off of the square, they will eventually find themselves within it, similar to the triangle before.
168+
169+
The next obvious question is whether a square can create any more interesting fractally patterns, and the answer is "yes, but only if we restrict the movement a bit."
170+
Which brings us to another topic entirely: restricted chaos games.
171+
Discussing restricted chaos games in more detail is a chapter in its own right, so I will forego the discussion here.
172+
If you are interested, please let me know and I will be more than willing to add the chapter in the future!
173+
174+
## Example Code
175+
176+
For the code in this chapter, we have decided to write it specifically for the Chaos game, not the hutchinson animations shown at the start of the chapter.
177+
This is because that animation is slightly tricky to create and distracts from the overall purpose of this chapter.
178+
In addition, we have written the chaos game code to take in a set of points so that it is not hard-coded for the Sierpinski triangle and can be easily extended to other shapes like the square or restricted chaos games, as we mentioned before!
179+
180+
{% method %}
181+
{% sample lang="jl" %}
182+
[import:4-17, lang:"julia"](code/julia/IFS.jl)
183+
{% endmethod %}
184+
185+
### Bibliography
186+
187+
{% references %} {% endreferences %}
188+
189+
<script>
190+
MathJax.Hub.Queue(["Typeset",MathJax.Hub]);
191+
</script>
192+
193+
## License
194+
195+
##### Code Examples
196+
197+
The code examples are licensed under the MIT license (found in [LICENSE.md](https://github.com/algorithm-archivists/algorithm-archive/blob/master/LICENSE.md)).
198+
199+
##### Text
200+
201+
The text of this chapter was written by [James Schloss](https://github.com/leio) and is licensed under the [Creative Commons Attribution-ShareAlike 4.0 International License](https://creativecommons.org/licenses/by-sa/4.0/legalcode).
202+
203+
[<p><img class="center" src="../cc/CC-BY-SA_icon.svg" /></p>](https://creativecommons.org/licenses/by-sa/4.0/)
204+
205+
#### Images/Graphics
206+
207+
- The image "[IFS triangle 1](res/IFS_triangle_1.png)" was created by [James Schloss](https://github.com/leios) and is licensed under the [Creative Commons Attribution-ShareAlike 4.0 International License](https://creativecommons.org/licenses/by-sa/4.0/legalcode).
208+
- The image "[IFS triangle 2](res/IFS_triangle_2.png)" was created by [James Schloss](https://github.com/leios) and is licensed under the [Creative Commons Attribution-ShareAlike 4.0 International License](https://creativecommons.org/licenses/by-sa/4.0/legalcode).
209+
- The image "[IFS triangle 3](res/IFS_triangle_3.png)" was created by [James Schloss](https://github.com/leios) and is licensed under the [Creative Commons Attribution-ShareAlike 4.0 International License](https://creativecommons.org/licenses/by-sa/4.0/legalcode).
210+
- The image "[IFS triangle 4](res/IFS_triangle_4.png)" was created by [James Schloss](https://github.com/leios) and is licensed under the [Creative Commons Attribution-ShareAlike 4.0 International License](https://creativecommons.org/licenses/by-sa/4.0/legalcode).
211+
- The image "[IFS triangle 5](res/IFS_triangle_5.png)" was created by [James Schloss](https://github.com/leios) and is licensed under the [Creative Commons Attribution-ShareAlike 4.0 International License](https://creativecommons.org/licenses/by-sa/4.0/legalcode).
212+
- The image "[IFS square 1](res/IFS_square_1.png)" was created by [James Schloss](https://github.com/leios) and is licensed under the [Creative Commons Attribution-ShareAlike 4.0 International License](https://creativecommons.org/licenses/by-sa/4.0/legalcode).
213+
- The image "[IFS square 2](res/IFS_square_2.png)" was created by [James Schloss](https://github.com/leios) and is licensed under the [Creative Commons Attribution-ShareAlike 4.0 International License](https://creativecommons.org/licenses/by-sa/4.0/legalcode).
214+
- The image "[Chaos 1](res/chaos_1.png)" was created by [James Schloss](https://github.com/leios) and is licensed under the [Creative Commons Attribution-ShareAlike 4.0 International License](https://creativecommons.org/licenses/by-sa/4.0/legalcode).
215+
- The image "[Chaos 2](res/chaos_2.png)" was created by [James Schloss](https://github.com/leios) and is licensed under the [Creative Commons Attribution-ShareAlike 4.0 International License](https://creativecommons.org/licenses/by-sa/4.0/legalcode).
216+
- The video "[IFS triangle video 1](res/IFS_triangle_vid_1.mp4)" was created by [James Schloss](https://github.com/leios) and is licensed under the [Creative Commons Attribution-ShareAlike 4.0 International License](https://creativecommons.org/licenses/by-sa/4.0/legalcode).
217+
- The video "[IFS triangle video 2](res/IFS_triangle_vid_2.mp4)" was created by [James Schloss](https://github.com/leios) and is licensed under the [Creative Commons Attribution-ShareAlike 4.0 International License](https://creativecommons.org/licenses/by-sa/4.0/legalcode).
218+
- The video "[IFS square video 1](res/IFS_square_vid_1.mp4)" was created by [James Schloss](https://github.com/leios) and is licensed under the [Creative Commons Attribution-ShareAlike 4.0 International License](https://creativecommons.org/licenses/by-sa/4.0/legalcode).
219+
- The video "[Chaos video 1](res/chaos_vid_1.mp4)" was created by [James Schloss](https://github.com/leios) and is licensed under the [Creative Commons Attribution-ShareAlike 4.0 International License](https://creativecommons.org/licenses/by-sa/4.0/legalcode).
220+
- The video "[Chaos video 2](res/chaos_vid_2.mp4)" was created by [James Schloss](https://github.com/leios) and is licensed under the [Creative Commons Attribution-ShareAlike 4.0 International License](https://creativecommons.org/licenses/by-sa/4.0/legalcode).

contents/IFS/code/julia/IFS.jl

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using DelimitedFiles
2+
3+
# This is a function to simulate a "chaos game"
4+
function chaos_game(n::Int, shape_points; output_file="out.dat")
5+
6+
# Initializing the output array and the initial point
7+
out = zeros(n,2)
8+
point = [rand(), rand()]
9+
10+
for i = 1:n
11+
out[i,:] .= point
12+
point = 0.5*(rand(shape_points) .+ point)
13+
end
14+
15+
writedlm(output_file, out)
16+
17+
end
18+
19+
# This will generate a Sierpinski triangle with a chaos game of n points for an
20+
# initial triangle with three points on the vertices of an equilateral triangle:
21+
# A = (0.0, 0.0)
22+
# B = (0.5, sqrt(0.75))
23+
# C = (1.0, 0.0)
24+
# It will output the file sierpinski.dat, which can be plotted after
25+
shape_points = [[0.0, 0.0],
26+
[0.5, sqrt(0.75)],
27+
[1.0, 0.0]]
28+
chaos_game(10000, shape_points)

contents/IFS/res/IFS_square_1.png

6.84 KB
Loading

contents/IFS/res/IFS_square_2.png

93.2 KB
Loading

contents/IFS/res/IFS_square_vid_1.mp4

5.16 MB
Binary file not shown.

contents/IFS/res/IFS_triangle_1.png

372 KB
Loading

contents/IFS/res/IFS_triangle_2.png

6.32 KB
Loading

contents/IFS/res/IFS_triangle_3.png

7.48 KB
Loading

contents/IFS/res/IFS_triangle_4.png

14.8 KB
Loading

contents/IFS/res/IFS_triangle_5.png

219 KB
Loading
21.3 KB
Binary file not shown.
1.99 MB
Binary file not shown.

contents/IFS/res/chaos_1.png

375 KB
Loading

contents/IFS/res/chaos_2.png

413 KB
Loading

contents/IFS/res/chaos_vid_1.mp4

1.21 MB
Binary file not shown.

contents/IFS/res/chaos_vid_2.mp4

637 KB
Binary file not shown.

contents/plotting/plotting.md

+2
Original file line numberDiff line numberDiff line change
@@ -561,6 +561,8 @@ Here, we have chosen `pointtype 7`, simply because it is easier to see when comp
561561

562562
{% endmethod %}
563563

564+
If you are interested in seeing this type of plot generate fractal patterns, please look at the chapter on [iterated function systems](../IFS/IFS.md).
565+
564566
## Conclusions
565567

566568
Plotting is a powerful tool that is essential for most of computational science.

literature.bib

+107
Original file line numberDiff line numberDiff line change
@@ -110,3 +110,110 @@ @misc{lunar_month_wiki
110110
url={https://en.wikipedia.org/wiki/Lunar_month},
111111
year={2020}
112112
}
113+
114+
#------------------------------------------------------------------------------#
115+
# IFS
116+
#------------------------------------------------------------------------------#
117+
@misc{self-similar,
118+
title={Wikipedia: Self-similarity},
119+
url={https://en.wikipedia.org/wiki/Self-similarity},
120+
year={2019}
121+
}
122+
123+
@misc{hausdorff,
124+
title={Wikipedia: Hausdorff dimension},
125+
url={https://en.wikipedia.org/wiki/Hausdorff_dimension},
126+
year={2019}
127+
}
128+
129+
@misc{3b1bfractal,
130+
author={Sanderson, G},
131+
title={3blue1brown: Fractals are typically not self-similar},
132+
url={https://www.youtube.com/watch?v=gB9n2gHsHN4},
133+
year={2017}
134+
}
135+
136+
@book{mandelbrot1983fractal,
137+
title={The fractal geometry of nature},
138+
author={Mandelbrot, Benoit B},
139+
volume={173},
140+
year={1983},
141+
publisher={WH freeman New York}
142+
}
143+
144+
@article{mandelbrot1967long,
145+
title={How long is the coast of Britain? Statistical self-similarity and fractional dimension},
146+
author={Mandelbrot, Benoit},
147+
journal={science},
148+
volume={156},
149+
number={3775},
150+
pages={636--638},
151+
year={1967},
152+
publisher={American Association for the Advancement of Science}
153+
}
154+
155+
@article{jampour2010new,
156+
title={A new fast technique for fingerprint identification with fractal and chaos game theory},
157+
author={Jampour, Mahdi and Yaghoobi, Mahdi and Ashourzadeh, Maryam and Soleimani, Adel},
158+
journal={Fractals},
159+
volume={18},
160+
number={03},
161+
pages={293--300},
162+
year={2010},
163+
publisher={World Scientific}
164+
}
165+
166+
@misc{fractal-compression,
167+
title={Wikipedia: Fractal Compression},
168+
url={https://en.wikipedia.org/wiki/Fractal_compression},
169+
year={2019}
170+
}
171+
172+
@article{saupe1994review,
173+
title={A review of the fractal image compression literature},
174+
author={Saupe, Dietmar and Hamzaoui, Raouf},
175+
journal={ACM SIGGRAPH Computer Graphics},
176+
volume={28},
177+
number={4},
178+
pages={268--276},
179+
year={1994},
180+
publisher={ACM}
181+
}
182+
183+
@article{gneiting2012estimators,
184+
title={Estimators of fractal dimension: Assessing the roughness of time series and spatial data},
185+
author={Gneiting, Tilmann and Ševčíková, Hana and Percival, Donald B},
186+
journal={Statistical Science},
187+
pages={247--277},
188+
year={2012},
189+
publisher={JSTOR}
190+
}
191+
192+
@article{hutchinson1981fractals,
193+
title={Fractals and self similarity},
194+
author={Hutchinson, John E},
195+
journal={Indiana University Mathematics Journal},
196+
volume={30},
197+
number={5},
198+
pages={713--747},
199+
year={1981},
200+
publisher={JSTOR}
201+
}
202+
203+
@misc{hutchinson-operator,
204+
title={Wikipedia: Hutchinson Operator},
205+
url={https://en.wikipedia.org/wiki/Hutchinson_operator},
206+
year={2019}
207+
}
208+
209+
@misc{chaos-game,
210+
title={Wikipedia: Chaos Game},
211+
url={https://en.wikipedia.org/wiki/Chaos_game},
212+
year={2019}
213+
}
214+
215+
@misc{chaos-game-wolf,
216+
title={Wolfram: Chaos Game},
217+
url={http://mathworld.wolfram.com/ChaosGame.html},
218+
year={2019}
219+
}

0 commit comments

Comments
 (0)