-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwolfram_automaton.c
164 lines (138 loc) · 3.58 KB
/
wolfram_automaton.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <inttypes.h>
#include "wolfram_automaton.h"
static uint32_t ipow(int base, int exp)
{
uint32_t result = 1;
for (;;) {
if (exp & 1)
result *= base;
exp >>= 1;
if (!exp)
break;
base *= base;
}
return result;
}
static int print_bits_spaced(int size, uint8_t a[2 * size], int par,
char buf[size + 1])
{
for (int i = 0; i < size; i++) {
buf[i] = a[2 * i + par] + '0';
}
buf[size] = '\0';
return 0;
}
uint8_t element_at(size_t idx, autom_t autom[static 1])
{
return autom->grid[2 * idx + autom->par];
}
int print_autom(autom_t autom[static 1], char* buf)
{
if (buf)
return print_bits_spaced(autom->size, autom->grid, autom->par, buf);
return 1;
}
int update_step(autom_t autom[static 1], rule_t* rule)
{
size_t size = autom->size;
int radius = rule->radius;
int states = autom->states, mult;
size_t c;
if (!rule)
return 1;
for (size_t r = radius; r < size - radius; r++) {
c = 0;
mult = 1;
for (int w = radius; w >= -radius; w--) {
c += autom->grid[2 * (r + w) + autom->par] * mult;
mult *= states;
}
autom->grid[!autom->par + 2 * r] = rule->lookup[c];
}
for (size_t r = 0; r < (size_t)radius; r++) {
c = 0;
mult = 1;
for (int w = radius; w >= -radius; w--) {
c += autom->grid[(2 * (r + w + size) + autom->par) %
(2 * size)] * mult;
mult *= states;
}
autom->grid[!autom->par + 2 * r] = rule->lookup[c];
}
for (size_t r = size - radius; r < size; r++) {
c = 0;
mult = 1;
for (int w = radius; w >= -radius; w--) {
c += autom->grid[(2 * (r + w) + autom->par) %
(2 * size)] * mult;
mult *= states;
}
autom->grid[!autom->par + 2 * r] = rule->lookup[c];
}
autom->par = 1 - autom->par;
return 0;
}
autom_t* create_automat(size_t size, int states)
{
autom_t* autom = calloc(1, sizeof(autom_t));
autom->states = states;
autom->size = size;
autom->grid = calloc(2 * size, sizeof(uint8_t));
autom->par = 0;
return autom;
}
int init_automat(autom_t autom[static 1], enum InitMode mode)
{
size_t size = autom->size;
int states = autom->states;
if (mode == ONE) {
for (size_t i = 0; i < size; i++) {
autom->grid[2 * i + autom->par] = 0;
}
autom->grid[size + autom->par] = 1;
}
else if (mode == RANDOM) {
for (size_t i = 0; i < size; i++) {
autom->grid[2 * i + autom->par] = rand() % states;
}
} else if (mode == RAND_SMALL) {
for (size_t i = size / 2 - size / 5; i < size / 2 + size / 5; i++) {
autom->grid[2 * i + autom->par] = rand() % states;
}
}
return 0;
}
int init_automat_from(size_t size, uint8_t init[size], autom_t autom[static 1])
{
for (size_t i = 0; i < size; i++) {
autom->grid[2 * i + autom->par] = init[i];
}
return 0;
}
rule_t* rule_from_number(unsigned long rule_number, int states,
int neighbors)
{
int rule_size = ipow(states, neighbors);
rule_t* rule = calloc(1, sizeof(rule_t));
rule->states = states;
rule->rule_size = rule_size;
rule->radius = (neighbors - 1) / 2;
rule->lookup = calloc(rule_size, sizeof(uint8_t));
for (int i = 0; i < rule_size; ++i) {
rule->lookup[i] = (rule_number / (ipow(states, i))) % states;
}
return rule;
}
unsigned long number_from_rule(rule_t rule[static 1])
{
unsigned long count = 0;
unsigned long mult = 1;
for (size_t i = 0; i < rule->rule_size; ++i) {
count += rule->lookup[i] * mult;
mult *= rule->states;
}
return count;
}