From d88ad50377376e7ac2722a68097eeeeaf898301a Mon Sep 17 00:00:00 2001 From: Amaras Date: Sat, 4 Jul 2020 23:41:43 +0200 Subject: [PATCH 1/6] first draft on IFS Coconut version --- contents/IFS/code/coconut/IFS.coco | 33 ++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 contents/IFS/code/coconut/IFS.coco diff --git a/contents/IFS/code/coconut/IFS.coco b/contents/IFS/code/coconut/IFS.coco new file mode 100644 index 000000000..37b22d11f --- /dev/null +++ b/contents/IFS/code/coconut/IFS.coco @@ -0,0 +1,33 @@ +from math import sqrt +from random import random, choice + +data point(x=0, y=0): + def __add__(self, other): + match point(x1, y1) in other: + return point(self.x + x1, self.y + y1) + else: + return self + + 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 point in chaos_game(10000, shape_points): + f.write("{0}\t{1}\n".format(*point)) From 2bdcf7843ad83657e3282bfbf469659f75605cc4 Mon Sep 17 00:00:00 2001 From: Amaras Date: Sun, 5 Jul 2020 21:55:30 +0200 Subject: [PATCH 2/6] No idea why it doesn't work. Stopping for now. --- contents/IFS/code/coconut/IFS.coco | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/contents/IFS/code/coconut/IFS.coco b/contents/IFS/code/coconut/IFS.coco index 37b22d11f..3e93f673b 100644 --- a/contents/IFS/code/coconut/IFS.coco +++ b/contents/IFS/code/coconut/IFS.coco @@ -3,10 +3,7 @@ from random import random, choice data point(x=0, y=0): def __add__(self, other): - match point(x1, y1) in other: - return point(self.x + x1, self.y + y1) - else: - return self + return point(self.x + other.x, self.y + other.y) def __rmul__(self, other): return point(self.x * other, self.y * other) From b07cf624bfed7d21543b47deb53df40e7caf27ff Mon Sep 17 00:00:00 2001 From: Amaras Date: Mon, 6 Jul 2020 01:20:37 +0200 Subject: [PATCH 3/6] added .gitignore clause for Coconut compilation + try for IFS code --- .gitignore | 3 +++ contents/IFS/code/coconut/IFS.coco | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 225dcc094..932dd5038 100644 --- a/.gitignore +++ b/.gitignore @@ -511,3 +511,6 @@ vscode/ # Data file extension for Algorithm Archive *.dat + +# Coconut compilation files +**/coconut/*.py \ No newline at end of file diff --git a/contents/IFS/code/coconut/IFS.coco b/contents/IFS/code/coconut/IFS.coco index 3e93f673b..255587944 100644 --- a/contents/IFS/code/coconut/IFS.coco +++ b/contents/IFS/code/coconut/IFS.coco @@ -12,7 +12,7 @@ def chaos_game(n, shape_points): p = point(random(), random()) for _ in range(n): - p = 1/2 * (p + choice(shape_points)) + p = (1/2) * (p + choice(shape_points)) yield p From bdac498e185c8598a0ed9516d1732098c90ee98b Mon Sep 17 00:00:00 2001 From: Amaras Date: Mon, 6 Jul 2020 01:25:41 +0200 Subject: [PATCH 4/6] Overly minor revision because GH didn't like the lack of newline --- .gitignore | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 932dd5038..7208f6513 100644 --- a/.gitignore +++ b/.gitignore @@ -513,4 +513,4 @@ vscode/ *.dat # Coconut compilation files -**/coconut/*.py \ No newline at end of file +**/coconut/*.py From 3f1bca2fa0f0736c5ff830630059a7b356b6efb6 Mon Sep 17 00:00:00 2001 From: Amaras Date: Tue, 7 Jul 2020 21:44:55 +0200 Subject: [PATCH 5/6] Corrected the bug. Should be good to go. --- contents/IFS/code/coconut/IFS.coco | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/contents/IFS/code/coconut/IFS.coco b/contents/IFS/code/coconut/IFS.coco index 255587944..2fce567d6 100644 --- a/contents/IFS/code/coconut/IFS.coco +++ b/contents/IFS/code/coconut/IFS.coco @@ -26,5 +26,5 @@ 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 point in chaos_game(10000, shape_points): - f.write("{0}\t{1}\n".format(*point)) + for p in chaos_game(10000, shape_points): + f.write("{0}\t{1}\n".format(p.x, p.y)) From 98c55be0764ef3ec23e2e15d6456024cc51855a5 Mon Sep 17 00:00:00 2001 From: Amaras Date: Wed, 8 Jul 2020 12:39:44 +0200 Subject: [PATCH 6/6] Added coconut in the md file --- contents/IFS/IFS.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/contents/IFS/IFS.md b/contents/IFS/IFS.md index 93c2d241f..8b7805ff3 100644 --- a/contents/IFS/IFS.md +++ b/contents/IFS/IFS.md @@ -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: @@ -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