Skip to content

Add IFS in python #700

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CONTRIBUTORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 4 additions & 0 deletions contents/IFS/IFS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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" %}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would fix the range so that the imports in the source code file aren't included.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

okay!

[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:
Expand Down Expand Up @@ -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
Expand Down
34 changes: 34 additions & 0 deletions contents/IFS/code/python/IFS.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
from random import random, choice
from math import sqrt, sin, cos, pi
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you remove your unused imports?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh! yup, my bad


# This generator simulates a "chaos game"
def chaos_game(n, shape_points):
# Initialize the starting point
point = [random(), random()]

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))]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't need the underscores here, since _p and _s can't escape the scope of the list comprehension. Up to you whether or not to keep them.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll change them

yield point
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:)


# 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 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 = 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))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For consistency with the Julia implementation, the plot output will be easier to read if it's tab-separated:

Suggested change
f.write("{0},{1}\n".format(*point))
f.write("{0}\t{1}\n".format(*point))

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done