From c15b2a9e51616ba789abba5b96e972f4ffc3263f Mon Sep 17 00:00:00 2001 From: Jonathan D B Van Schenck Date: Wed, 20 May 2020 20:35:24 -0700 Subject: [PATCH 1/6] Add IFS in python --- contents/IFS/IFS.md | 4 ++++ contents/IFS/code/python/IFS.py | 24 ++++++++++++++++++++++++ 2 files changed, 28 insertions(+) create mode 100644 contents/IFS/code/python/IFS.py diff --git a/contents/IFS/IFS.md b/contents/IFS/IFS.md index 87b1790d5..3995a1d89 100644 --- a/contents/IFS/IFS.md +++ b/contents/IFS/IFS.md @@ -128,6 +128,8 @@ Here, instead of tracking children of children, we track a single individual tha {% method %} {% sample lang="jl" %} [import:4-17, lang:"julia"](code/julia/IFS.jl) +{% sample lang="py" %} +[import:5-13, lang:"python"](code/python/IFS.py) {% endmethod %} 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: @@ -180,6 +182,8 @@ In addition, we have written the chaos game code to take in a set of points so t {% method %} {% sample lang="jl" %} [import, lang:"julia"](code/julia/IFS.jl) +{% sample lang="py" %} +[import, lang:"python"](code/python/IFS.py) {% endmethod %} ### Bibliography diff --git a/contents/IFS/code/python/IFS.py b/contents/IFS/code/python/IFS.py new file mode 100644 index 000000000..0c9369c07 --- /dev/null +++ b/contents/IFS/code/python/IFS.py @@ -0,0 +1,24 @@ +from random import random, choice +from math import sqrt + +# This is a function to simulate a "chaos game" +def chaos_game(n, shape_points, output_file = "out.dat"): + # Initialize the starting point + point = [random(), random()] + + with open(output_file, "w") as f: + for _ in range(n): + # Update the point position and write it to the file + point = [_p + _s for _p, _s in zip(point, choice(shape_points))] + f.write(",".join([str(_p) for _p in p]) + '\n') + +# This will generate a Sierpinski triangle with a chaos game of n points for an +# initial triangle with three points on the vertices of an equilateral triangle: +# A = (0.0, 0.0) +# B = (0.5, sqrt(0.75)) +# C = (1.0, 0.0) +# It will output the file sierpinski.dat, which can be plotted after +shape_points = [[0.0, 0.0], + [0.5, sqrt(0.75)], + [1.0, 0.0]] +chaos_game(10000, shape_points, 'sierpinski.dat') From 98488d848fa9868e4d83a5d3de5118e631723bfc Mon Sep 17 00:00:00 2001 From: Jonathan D B Van Schenck Date: Wed, 20 May 2020 20:51:16 -0700 Subject: [PATCH 2/6] fix typos --- contents/IFS/code/python/IFS.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/contents/IFS/code/python/IFS.py b/contents/IFS/code/python/IFS.py index 0c9369c07..64fd03ec3 100644 --- a/contents/IFS/code/python/IFS.py +++ b/contents/IFS/code/python/IFS.py @@ -9,8 +9,8 @@ def chaos_game(n, shape_points, output_file = "out.dat"): with open(output_file, "w") as f: for _ in range(n): # Update the point position and write it to the file - point = [_p + _s for _p, _s in zip(point, choice(shape_points))] - f.write(",".join([str(_p) for _p in p]) + '\n') + point = [(_p + _s) / 2 for _p, _s in zip(point, choice(shape_points))] + f.write(",".join([str(_p) for _p in point]) + '\n') # This will generate a Sierpinski triangle with a chaos game of n points for an # initial triangle with three points on the vertices of an equilateral triangle: From 4cdf194fb69cc23abce88b65eeb31e0b2a5f52ce Mon Sep 17 00:00:00 2001 From: Jonathan D B Van Schenck Date: Thu, 21 May 2020 08:56:33 -0700 Subject: [PATCH 3/6] Added name to contributors list --- CONTRIBUTORS.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/CONTRIBUTORS.md b/CONTRIBUTORS.md index a9d53d836..7c9dd7d88 100644 --- a/CONTRIBUTORS.md +++ b/CONTRIBUTORS.md @@ -42,9 +42,10 @@ This file lists everyone, who contributed to this repo and wanted to show up her - Christopher Milan - Vexatos - Raven-Blue Dragon -- Björn Heinrichs +- Björn Heinrichs - Olav Sundfør - Ben Chislett - dovisutu - Antetokounpo - Akash Dhiman +- Jonathan D. B. Van Schenck From ae230f7b233a9dfec5b306719a236b56db27df21 Mon Sep 17 00:00:00 2001 From: Jonathan D B Van Schenck Date: Thu, 21 May 2020 22:47:09 -0700 Subject: [PATCH 4/6] Add yield statement and ngon generator --- contents/IFS/IFS.md | 2 +- contents/IFS/code/python/IFS.py | 42 ++++++++++++++++++++------------- 2 files changed, 27 insertions(+), 17 deletions(-) diff --git a/contents/IFS/IFS.md b/contents/IFS/IFS.md index 3995a1d89..a61fd8af5 100644 --- a/contents/IFS/IFS.md +++ b/contents/IFS/IFS.md @@ -129,7 +129,7 @@ Here, instead of tracking children of children, we track a single individual tha {% sample lang="jl" %} [import:4-17, lang:"julia"](code/julia/IFS.jl) {% sample lang="py" %} -[import:5-13, lang:"python"](code/python/IFS.py) +[import:5-12, lang:"python"](code/python/IFS.py) {% endmethod %} 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: diff --git a/contents/IFS/code/python/IFS.py b/contents/IFS/code/python/IFS.py index 64fd03ec3..55bd3620d 100644 --- a/contents/IFS/code/python/IFS.py +++ b/contents/IFS/code/python/IFS.py @@ -1,24 +1,34 @@ from random import random, choice -from math import sqrt +from math import sqrt, sin, cos, pi -# This is a function to simulate a "chaos game" -def chaos_game(n, shape_points, output_file = "out.dat"): +# This generator simulates a "chaos game" +def chaos_game(n, shape_points): # Initialize the starting point point = [random(), random()] - with open(output_file, "w") as f: - for _ in range(n): - # Update the point position and write it to the file - point = [(_p + _s) / 2 for _p, _s in zip(point, choice(shape_points))] - f.write(",".join([str(_p) for _p in point]) + '\n') + for _ in range(n): + # Update the point position and yield the result + point = [(_p + _s) / 2 for _p, _s in zip(point, choice(shape_points))] + yield point + +# This function generates the shape points for an N-gon inscribed inside a +# unit circle, with one of the edges oriented along the bottom +def ngon_shape_points(N): + # Exterior N-gon angle + phi = 2 * pi / N + + # Offset to put an edge at the bottom + delta = (pi + phi) / 2 + + return [[cos(- delta - m * phi), sin(- delta - m * phi)] for m in range(N)] # This will generate a Sierpinski triangle with a chaos game of n points for an -# initial triangle with three points on the vertices of an equilateral triangle: -# A = (0.0, 0.0) -# B = (0.5, sqrt(0.75)) -# C = (1.0, 0.0) +# initial equilateral triangle inscribed on a unit circle. The shape points are: +# A = (-sqrt(3)/2, -0.5) +# B = ( 0.0, 1.0) +# C = ( sqrt(3)/2, -0.5) # It will output the file sierpinski.dat, which can be plotted after -shape_points = [[0.0, 0.0], - [0.5, sqrt(0.75)], - [1.0, 0.0]] -chaos_game(10000, shape_points, 'sierpinski.dat') +shape_points = ngon_shape_points(3) +with open("sierpinski.dat", "w") as f: + for point in chaos_game(10000, shape_points): + f.write("{0},{1}\n".format(*point)) From c7e84e7dc74828228ae0abd31ee389927e4d7642 Mon Sep 17 00:00:00 2001 From: Jonathan D B Van Schenck Date: Fri, 22 May 2020 21:11:10 -0700 Subject: [PATCH 5/6] included import section in IFS.md --- contents/IFS/IFS.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contents/IFS/IFS.md b/contents/IFS/IFS.md index 4b6adb3cd..fcf62ec49 100644 --- a/contents/IFS/IFS.md +++ b/contents/IFS/IFS.md @@ -131,7 +131,7 @@ Here, instead of tracking children of children, we track a single individual tha {% sample lang="cpp" %} [import:39-52, lang:"cpp"](code/c++/IFS.cpp) {% sample lang="py" %} -[import:5-12, lang:"python"](code/python/IFS.py) +[import:1-12, lang:"python"](code/python/IFS.py) {% endmethod %} 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: From 62a6174ee7a4d484a70d97ea0978a84c34108eb5 Mon Sep 17 00:00:00 2001 From: Jonathan D B Van Schenck Date: Mon, 25 May 2020 21:36:06 -0700 Subject: [PATCH 6/6] fix imports --- contents/IFS/IFS.md | 2 +- contents/IFS/code/python/IFS.py | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/contents/IFS/IFS.md b/contents/IFS/IFS.md index fcf62ec49..4b6adb3cd 100644 --- a/contents/IFS/IFS.md +++ b/contents/IFS/IFS.md @@ -131,7 +131,7 @@ Here, instead of tracking children of children, we track a single individual tha {% sample lang="cpp" %} [import:39-52, lang:"cpp"](code/c++/IFS.cpp) {% sample lang="py" %} -[import:1-12, lang:"python"](code/python/IFS.py) +[import:5-12, lang:"python"](code/python/IFS.py) {% endmethod %} 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: diff --git a/contents/IFS/code/python/IFS.py b/contents/IFS/code/python/IFS.py index b2e69dfe9..a7808fb5c 100644 --- a/contents/IFS/code/python/IFS.py +++ b/contents/IFS/code/python/IFS.py @@ -1,5 +1,5 @@ from random import random, choice -from math import sqrt, sin, cos, pi +from math import sqrt # This generator simulates a "chaos game" def chaos_game(n, shape_points): @@ -8,7 +8,7 @@ def chaos_game(n, shape_points): for _ in range(n): # Update the point position and yield the result - point = [(_p + _s) / 2 for _p, _s in zip(point, choice(shape_points))] + point = [(p + s) / 2 for p, s in zip(point, choice(shape_points))] yield point # This will generate a Sierpinski triangle with a chaos game of n points for an @@ -22,4 +22,4 @@ def chaos_game(n, shape_points): [1.0, 0.0]] with open("sierpinski.dat", "w") as f: for point in chaos_game(10000, shape_points): - f.write("{0},{1}\n".format(*point)) + f.write("{0}\t{1}\n".format(*point))