Skip to content

IFS in coconut #727

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
merged 6 commits into from
Jul 11, 2020
Merged
Show file tree
Hide file tree
Changes from all 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: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -511,3 +511,6 @@ vscode/

# Data file extension for Algorithm Archive
*.dat

# Coconut compilation files
**/coconut/*.py
4 changes: 4 additions & 0 deletions contents/IFS/IFS.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,8 @@ Here, instead of tracking children of children, we track a single individual tha
[import:5-12, lang:"python"](code/python/IFS.py)
{% sample lang="c" %}
[import:18-29, lang:"c"](code/c/IFS.c)
{% sample lang="coco" %}
[import:4-16, lang:"coconut"](code/coconut/IFS.coco)
{% 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 @@ -203,6 +205,8 @@ In addition, we have written the chaos game code to take in a set of points so t
[import, lang:"python"](code/python/IFS.py)
{% sample lang="c" %}
[import, lang:"c"](code/c/IFS.c)
{%sample lang="coco" %}
[import, lang:"coconut"](code/coconut/IFS.coco)
{% endmethod %}

### Bibliography
Expand Down
30 changes: 30 additions & 0 deletions contents/IFS/code/coconut/IFS.coco
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
from math import sqrt
from random import random, choice

data point(x=0, y=0):
Copy link
Member

Choose a reason for hiding this comment

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

Hint: it has to do with point here...

def __add__(self, other):
return point(self.x + other.x, self.y + other.y)

def __rmul__(self, other):
return point(self.x * other, self.y * other)

def chaos_game(n, shape_points):
p = point(random(), random())

for _ in range(n):
p = (1/2) * (p + choice(shape_points))
yield p


# 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 = [point(0.0, 0.0),
point(0.5, sqrt(0.75)),
point(1.0, 0.0)]
with open("sierpinski.dat", "w") as f:
for p in chaos_game(10000, shape_points):
f.write("{0}\t{1}\n".format(p.x, p.y))