Skip to content

Commit

Permalink
Added Barnsley fern in Coconut (+ .editorconfig for Coconut) (#814)
Browse files Browse the repository at this point in the history
  • Loading branch information
Amaras authored Sep 6, 2021
1 parent 09168eb commit bd58862
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 1 deletion.
3 changes: 3 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -172,3 +172,6 @@ indent_size = 4
indent_style = space
indent_size = 2

[*.coco]
indent_style = space
indent_size = 4
5 changes: 4 additions & 1 deletion contents/barnsley/barnsley.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ To account for this, each function is also given a probability of being chosen:
| Function | Probability |
| -------- | ----------- |
| $$f_1(P) = \begin{bmatrix} 0 &0 \\ 0 &0.16 \end{bmatrix}P + \begin{bmatrix} 0 \\ 0 \end{bmatrix}$$ | 0.01 |
| $$f_2(P) = \begin{bmatrix} 0.85 &0.04 \\ -0.04 &0.85 \end{bmatrix}P + \begin{bmatrix} 0 \\ 1.6 \end{bmatrix}$$ | 0.85 |
| $$f_2(P) = \begin{bmatrix} 0.85 &0.04 \\ -0.04 &0.85 \end{bmatrix}P + \begin{bmatrix} 0 \\ 1.6 \end{bmatrix}$$ | 0.85 |
| $$f_3(P) = \begin{bmatrix} 0.2 &-0.26 \\ 0.23 &022 \end{bmatrix}P + \begin{bmatrix} 0 \\ 1.6 \end{bmatrix}$$ | 0.07 |
| $$f_4(P) = \begin{bmatrix} -0.15 &0.28 \\ 0.26 &0.24 \end{bmatrix}P + \begin{bmatrix} 0 \\ 0.44 \end{bmatrix}$$ | 0.07 |

Expand Down Expand Up @@ -131,6 +131,9 @@ The biggest differences between the two code implementations is that the Barnsle
[import, lang:"c"](code/c/barnsley.c)
{% sample lang="java" %}
[import, lang:"java"](code/java/Barnsley.java)
{% sample lang="coco" %}
[import, lang:"coconut"](code/julia/barnsley.coco)

{% endmethod %}

### Bibliography
Expand Down
44 changes: 44 additions & 0 deletions contents/barnsley/code/coconut/barnsley.coco
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
from random import choices
import numpy as np

data Point(x=0, y=0):
def __rmatmul__(self, mat: np.array):
point_array = np.array([self.x, self.y, 1])
x, y, *_ = tuple(*(mat @ point_array))
return Point(x, y)


def chaos_game(initial_location is Point, hutchinson_op, probabilities):
point = initial_location
while True:
yield (point := choices(hutchinson_op, probabilities) @ point)

barnsley_hutchinson = [
np.array([
[0., 0., 0.],
[0., 0.16, 0.],
[0., 0., 1.],
]),
np.array([
[0.85, 0.04, 0.],
[-0.04, 0.85, 1.6],
[0., 0., 1.],
]),
np.array([
[0.2, -0.26, 0.],
[0.23, 0.22, 1.6],
[0., 0., 1.],
]),
np.array([
[-0.15, 0.28, 0.],
[0.26, 0.24, 0.44],
[0., 0., 1.],
]),
]

barnsley_probabilities = [0.01, 0.85, 0.07, 0.07]

if __name__ == '__main__':
output_gen = chaos_game(Point(0, 0), barnsley_hutchinson, barnsley_probabilities)
output_points = np.array([*output_gen$[:10000]])
np.savetxt("out.dat", output_points)

0 comments on commit bd58862

Please sign in to comment.