Skip to content

Commit bd58862

Browse files
authored
Added Barnsley fern in Coconut (+ .editorconfig for Coconut) (#814)
1 parent 09168eb commit bd58862

File tree

3 files changed

+51
-1
lines changed

3 files changed

+51
-1
lines changed

.editorconfig

+3
Original file line numberDiff line numberDiff line change
@@ -172,3 +172,6 @@ indent_size = 4
172172
indent_style = space
173173
indent_size = 2
174174

175+
[*.coco]
176+
indent_style = space
177+
indent_size = 4

contents/barnsley/barnsley.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ To account for this, each function is also given a probability of being chosen:
8585
| Function | Probability |
8686
| -------- | ----------- |
8787
| $$f_1(P) = \begin{bmatrix} 0 &0 \\ 0 &0.16 \end{bmatrix}P + \begin{bmatrix} 0 \\ 0 \end{bmatrix}$$ | 0.01 |
88-
| $$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 |
88+
| $$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 |
8989
| $$f_3(P) = \begin{bmatrix} 0.2 &-0.26 \\ 0.23 &022 \end{bmatrix}P + \begin{bmatrix} 0 \\ 1.6 \end{bmatrix}$$ | 0.07 |
9090
| $$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 |
9191

@@ -131,6 +131,9 @@ The biggest differences between the two code implementations is that the Barnsle
131131
[import, lang:"c"](code/c/barnsley.c)
132132
{% sample lang="java" %}
133133
[import, lang:"java"](code/java/Barnsley.java)
134+
{% sample lang="coco" %}
135+
[import, lang:"coconut"](code/julia/barnsley.coco)
136+
134137
{% endmethod %}
135138

136139
### Bibliography
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
from random import choices
2+
import numpy as np
3+
4+
data Point(x=0, y=0):
5+
def __rmatmul__(self, mat: np.array):
6+
point_array = np.array([self.x, self.y, 1])
7+
x, y, *_ = tuple(*(mat @ point_array))
8+
return Point(x, y)
9+
10+
11+
def chaos_game(initial_location is Point, hutchinson_op, probabilities):
12+
point = initial_location
13+
while True:
14+
yield (point := choices(hutchinson_op, probabilities) @ point)
15+
16+
barnsley_hutchinson = [
17+
np.array([
18+
[0., 0., 0.],
19+
[0., 0.16, 0.],
20+
[0., 0., 1.],
21+
]),
22+
np.array([
23+
[0.85, 0.04, 0.],
24+
[-0.04, 0.85, 1.6],
25+
[0., 0., 1.],
26+
]),
27+
np.array([
28+
[0.2, -0.26, 0.],
29+
[0.23, 0.22, 1.6],
30+
[0., 0., 1.],
31+
]),
32+
np.array([
33+
[-0.15, 0.28, 0.],
34+
[0.26, 0.24, 0.44],
35+
[0., 0., 1.],
36+
]),
37+
]
38+
39+
barnsley_probabilities = [0.01, 0.85, 0.07, 0.07]
40+
41+
if __name__ == '__main__':
42+
output_gen = chaos_game(Point(0, 0), barnsley_hutchinson, barnsley_probabilities)
43+
output_points = np.array([*output_gen$[:10000]])
44+
np.savetxt("out.dat", output_points)

0 commit comments

Comments
 (0)