diff --git a/.editorconfig b/.editorconfig index 4ea5e2ea9..a3fbd6e02 100644 --- a/.editorconfig +++ b/.editorconfig @@ -172,3 +172,6 @@ indent_size = 4 indent_style = space indent_size = 2 +[*.coco] +indent_style = space +indent_size = 4 diff --git a/contents/barnsley/barnsley.md b/contents/barnsley/barnsley.md index 0c0cf682b..e3f01f96f 100644 --- a/contents/barnsley/barnsley.md +++ b/contents/barnsley/barnsley.md @@ -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 | @@ -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 diff --git a/contents/barnsley/code/coconut/barnsley.coco b/contents/barnsley/code/coconut/barnsley.coco new file mode 100644 index 000000000..a27fc7c5c --- /dev/null +++ b/contents/barnsley/code/coconut/barnsley.coco @@ -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)