Skip to content
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 .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 |
Comment on lines -88 to 89
Copy link
Member

Choose a reason for hiding this comment

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

whitespace change here, probably fine?

| $$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)