Conway's game of life
Martin Uecker
3 years ago
168 | 168 | # |
169 | 169 | TBASE=show slice crop resize join transpose squeeze flatten zeros ones flip circshift extract repmat bitmask reshape version delta copy casorati vec poly index |
170 | 170 | TFLP=scale invert conj fmac saxpy sdot spow cpyphs creal carg normalize cdf97 pattern nrmse mip avg cabs zexp |
171 | TNUM=fft fftmod fftshift noise bench threshold conv rss filter mandelbrot wavelet window var std fftrot roistat pol2mask | |
171 | TNUM=fft fftmod fftshift noise bench threshold conv rss filter mandelbrot wavelet window var std fftrot roistat pol2mask conway | |
172 | 172 | TRECO=pics pocsense sqpics itsense nlinv moba nufft rof tgv sake wave lrmatrix estdims estshift estdelay wavepsf wshfl rtnlinv mobafit |
173 | 173 | TCALIB=ecalib ecaltwo caldir walsh cc ccapply calmat svd estvar whiten rmfreq ssa bin |
174 | 174 | TMRI=homodyne poisson twixread fakeksp looklocker upat |
0 | ||
1 | ||
2 | ||
3 | ||
4 | #bart vec 0 0 1 0 v1 | |
5 | #bart vec 0 0 0 1 v2 | |
6 | #bart vec 0 1 1 1 v3 | |
7 | #bart join 1 v1 v2 v3 v | |
8 | ||
9 | #bart vec 0 0 1 1 v1 | |
10 | #bart vec 0 1 1 0 v2 | |
11 | #bart vec 0 0 1 0 v3 | |
12 | #bart join 1 v1 v2 v3 v | |
13 | ||
14 | #bart vec 0 1 1 1 0 1 v1 | |
15 | #bart vec 0 1 0 0 0 0 v2 | |
16 | #bart vec 0 0 0 0 1 1 v3 | |
17 | #bart vec 0 0 1 1 0 1 v4 | |
18 | #bart vec 0 1 0 1 0 1 v5 | |
19 | #bart join 1 v1 v2 v3 v4 v5 v | |
20 | ||
21 | #bart resize -c 0 300 1 300 v o | |
22 | #bart conway -n3000 o x | |
23 | ||
24 | ||
25 | bart vec 0 0 0 1 1 1 0 0 0 1 1 1 0 0 v0 | |
26 | bart vec 0 0 0 0 0 0 0 0 0 0 0 0 0 0 v1 | |
27 | bart vec 0 1 0 0 0 0 1 0 1 0 0 0 0 1 v2 | |
28 | bart vec 0 1 0 0 0 0 1 0 1 0 0 0 0 1 v3 | |
29 | bart vec 0 1 0 0 0 0 1 0 1 0 0 0 0 1 v4 | |
30 | bart vec 0 0 0 1 1 1 0 0 0 1 1 1 0 0 v5 | |
31 | bart vec 0 0 0 0 0 0 0 0 0 0 0 0 0 0 v6 | |
32 | bart vec 0 0 0 1 1 1 0 0 0 1 1 1 0 0 v7 | |
33 | bart vec 0 1 0 0 0 0 1 0 1 0 0 0 0 1 v8 | |
34 | bart vec 0 1 0 0 0 0 1 0 1 0 0 0 0 1 v9 | |
35 | bart vec 0 1 0 0 0 0 1 0 1 0 0 0 0 1 va | |
36 | bart vec 0 0 0 0 0 0 0 0 0 0 0 0 0 0 vb | |
37 | bart vec 0 0 0 1 1 1 0 0 0 1 1 1 0 0 vc | |
38 | bart join 1 v0 v1 v2 v3 v4 v5 v6 v7 v8 v9 va vb vc v | |
39 | ||
40 | ||
41 | bart resize -c 0 50 1 50 v o | |
42 | ||
43 | bart conway -n3 o x | |
44 |
0 | /* Copyright 2021. Martin Uecker. | |
1 | * All rights reserved. Use of this source code is governed by | |
2 | * a BSD-style license which can be found in the LICENSE file. | |
3 | * | |
4 | * Authors: | |
5 | * 2020 Martin Uecker <martin.uecker@med.uni-goettingen.de> | |
6 | */ | |
7 | ||
8 | #include <complex.h> | |
9 | ||
10 | #include "num/multind.h" | |
11 | #include "num/flpmath.h" | |
12 | #include "num/init.h" | |
13 | #include "num/conv.h" | |
14 | ||
15 | #include "misc/mmio.h" | |
16 | #include "misc/io.h" | |
17 | #include "misc/misc.h" | |
18 | #include "misc/opts.h" | |
19 | ||
20 | ||
21 | static const char usage_str[] = "<input> <output>"; | |
22 | static const char help_str[] = "Conway's game of life.\n"; | |
23 | ||
24 | ||
25 | ||
26 | int main_conway(int argc, char* argv[argc]) | |
27 | { | |
28 | int iter = 20; | |
29 | bool periodic = false; | |
30 | ||
31 | const struct opt_s opts[] = { | |
32 | ||
33 | OPT_SET('P', &periodic, "periodic boundary conditions"), | |
34 | OPT_INT('n', &iter, "#", "nr. of iterations"), | |
35 | }; | |
36 | ||
37 | cmdline(&argc, argv, 2, 2, usage_str, help_str, ARRAY_SIZE(opts), opts); | |
38 | ||
39 | num_init(); | |
40 | ||
41 | long dims[2]; | |
42 | ||
43 | complex float* init = load_cfl(argv[1], 2, dims); | |
44 | ||
45 | complex float* world = md_alloc(2, dims, CFL_SIZE); | |
46 | ||
47 | md_copy(2, dims, world, init, CFL_SIZE); | |
48 | ||
49 | unmap_cfl(2, dims, init); | |
50 | ||
51 | long wdims[3]; | |
52 | md_copy_dims(2, wdims, dims); | |
53 | wdims[2] = 1; | |
54 | ||
55 | long odims[3]; | |
56 | md_copy_dims(2, odims, dims); | |
57 | odims[2] = iter; | |
58 | ||
59 | complex float* out = create_cfl(argv[2], 3, odims); | |
60 | ||
61 | long mdims[2] = { 3, 3 }; | |
62 | ||
63 | complex float mask[3][3] = { | |
64 | { 1., 1., 1., }, | |
65 | { 1., 0., 1., }, | |
66 | { 1., 1., 1., }, | |
67 | }; | |
68 | ||
69 | complex float* buf = md_alloc(2, dims, CFL_SIZE); | |
70 | complex float* tmp = md_alloc(2, dims, CFL_SIZE); | |
71 | ||
72 | struct conv_plan* plan = conv_plan(2, 3UL, periodic ? CONV_CYCLIC : CONV_TRUNCATED, CONV_SYMMETRIC, dims, dims, mdims, &mask[0][0]); | |
73 | ||
74 | for (int i = 0; i < iter; i++) { | |
75 | ||
76 | conv_exec(plan, buf, world); | |
77 | ||
78 | md_zslessequal(2, dims, tmp, buf, 3.1); | |
79 | md_zadd(2, dims, buf, buf, world); | |
80 | md_zsgreatequal(2, dims, world, buf, 2.9); | |
81 | md_zmul(2, dims, world, world, tmp); | |
82 | ||
83 | md_copy_block(3, (long[3]){ [2] = i }, odims, out, wdims, world, CFL_SIZE); | |
84 | } | |
85 | ||
86 | conv_free(plan); | |
87 | ||
88 | md_free(buf); | |
89 | md_free(tmp); | |
90 | ||
91 | unmap_cfl(3, odims, out); | |
92 | return 0; | |
93 | } | |
94 | ||
95 |