-
Notifications
You must be signed in to change notification settings - Fork 0
/
families.c
286 lines (264 loc) · 7.44 KB
/
families.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
/*
family3.c
Lee A. Christie
Reads a 3-bit fuction class from STDIN space-separated e.g.
7 4 4 1 4 1 1 0
Computes and outputs the class with its Walsh family to STDOUT e.g.
[7, 4, 4, 1, 4, 1, 1, 0] {0B, 3F, 4B, 7F}
Terminates on non-numberical input marker e.g.
END
Suggested usage:
./family3 <all.txt >out.txt
*/
#include <stdio.h>
/* Returns the structure of the given specific function by appplying
the Fast Walsh-Hadamard Transform (FWHT) and converting the
resulting non-zero structure to a numerical code. */
int find_structure(int* f) {
int rv = 0;
int ff[16];
ff[0] = f[0] + f[4];
ff[1] = f[1] + f[5];
ff[2] = f[2] + f[6];
ff[3] = f[3] + f[7];
ff[4] = f[0] - f[4];
ff[5] = f[1] - f[5];
ff[6] = f[2] - f[6];
ff[7] = f[3] - f[7];
ff[8] = ff[0] + ff[2];
ff[9] = ff[1] + ff[3];
ff[10] = ff[0] - ff[2];
ff[11] = ff[1] - ff[3];
ff[12] = ff[4] + ff[6];
ff[13] = ff[5] + ff[7];
ff[14] = ff[4] - ff[6];
ff[15] = ff[5] - ff[7];
if (ff[8] - ff[9]) rv += 1;
if (ff[10] + ff[11]) rv += 2;
if (ff[10] - ff[11]) rv += 4;
if (ff[12] + ff[13]) rv += 8;
if (ff[12] - ff[13]) rv += 16;
if (ff[14] + ff[15]) rv += 32;
if (ff[14] - ff[15]) rv += 64;
return rv;
}
/* Returns the number of distinct values for a specified class. */
int calc_num_ranks(int* clazz) {
int rv = 0;
int i, j, is_new;
for (i = 0; i < 8; i++) {
is_new = 1;
for (j = 0; j < i-1; j++) {
if (clazz[i] == clazz[j]) {
is_new = 0;
}
}
if (is_new) {
rv++;
}
}
return rv;
}
/* Finds the class for a specified funtion. */
void function_to_class(int* function, int* clazz) {
int i, j, num_lower;
for (i = 0; i < 8; i++) {
num_lower = 0;
for (j = 0; j < 8; j++) {
if (function[j] < function[i]) {
num_lower++;
}
}
clazz[i] = num_lower;
}
}
/* Converts a clazz in-place into tokens for replacement. */
void tokenise(int* clazz) {
int used;
int rank, i;
int token = -1;
for (rank = 0; rank < 8; rank++) {
used = 0;
for (i = 0; i < 8; i++) {
if (clazz[i] == rank) {
clazz[i] = token;
used = 1;
}
}
if (used) {
token--;
}
}
}
/* Creates an instance specified tokeneized class as fitnesses
with the given values. */
void detokenise(int* t_clazz, int* fitnesses, int* vals) {
int i;
for (i = 0; i < 8; i++) {
fitnesses[i] = vals[-t_clazz[i] - 1];
}
}
/* Computes the Walsh family for the specified tokenised
class with 3 distinct ranks. */
void find_family_3(int* t_clazz, int* family) {
int v[3], f[8];
for (v[0] = 0; v[0] <= 2; v[0]++) {
for (v[1] = v[0] + 1; v[1] <= 3; v[1]++) {
for (v[2] = v[1] + 1; v[2] <= 4; v[2]++) {
detokenise(t_clazz, f, v);
family[find_structure(f)] = 1;
}}}}
/* Computes the Walsh family for the specified tokenised
class with 4 distinct ranks. */
void find_family_4(int* t_clazz, int* family) {
int v[4], f[8];
for (v[0] = 0; v[0] <= 4; v[0]++) {
for (v[1] = v[0] + 1; v[1] <= 5; v[1]++) {
for (v[2] = v[1] + 1; v[2] <= 6; v[2]++) {
for (v[3] = v[2] + 1; v[3] <= 7; v[3]++) {
detokenise(t_clazz, f, v);
family[find_structure(f)] = 1;
}}}}}
/* Computes the Walsh family for the specified tokenised
class with 5 distinct ranks. */
void find_family_5(int* t_clazz, int* family) {
int v[5], f[8];
for (v[0] = 0; v[0] <= 6; v[0]++) {
for (v[1] = v[0] + 1; v[1] <= 7; v[1]++) {
for (v[2] = v[1] + 1; v[2] <= 8; v[2]++) {
for (v[3] = v[2] + 1; v[3] <= 9; v[3]++) {
for (v[4] = v[3] + 1; v[4] <= 10; v[4]++) {
detokenise(t_clazz, f, v);
family[find_structure(f)] = 1;
}}}}}}
/* Computes the Walsh family for the specified tokenised
class with 6 distinct ranks. */
void find_family_6(int* t_clazz, int* family) {
int v[6], f[8];
for (v[0] = 0; v[0] <= 7; v[0]++) {
for (v[1] = v[0] + 1; v[1] <= 8; v[1]++) {
for (v[2] = v[1] + 1; v[2] <= 9; v[2]++) {
for (v[3] = v[2] + 1; v[3] <= 10; v[3]++) {
for (v[4] = v[3] + 1; v[4] <= 11; v[4]++) {
for (v[5] = v[4] + 1; v[5] <= 12; v[5]++) {
detokenise(t_clazz, f, v);
family[find_structure(f)] = 1;
}}}}}}}
/* Computes the Walsh family for the specified tokenised
class with 7 distinct ranks. */
void find_family_7(int* t_clazz, int* family) {
int v[7], f[8];
for (v[0] = 0; v[0] <= 8; v[0]++) {
for (v[1] = v[0] + 1; v[1] <= 9; v[1]++) {
for (v[2] = v[1] + 1; v[2] <= 10; v[2]++) {
for (v[3] = v[2] + 1; v[3] <= 11; v[3]++) {
for (v[4] = v[3] + 1; v[4] <= 12; v[4]++) {
for (v[5] = v[4] + 1; v[5] <= 13; v[5]++) {
for (v[6] = v[5] + 1; v[6] <= 14; v[6]++) {
detokenise(t_clazz, f, v);
family[find_structure(f)] = 1;
}}}}}}}}
/* Computes the Walsh family for the specified tokenised
class with 8 distinct ranks. */
void find_family_8(int* t_clazz, int* family) {
int v[8], f[8];
for (v[0] = 0; v[0] <= 8; v[0]++) {
for (v[1] = v[0] + 1; v[1] <= 9; v[1]++) {
for (v[2] = v[1] + 1; v[2] <= 10; v[2]++) {
for (v[3] = v[2] + 1; v[3] <= 11; v[3]++) {
for (v[4] = v[3] + 1; v[4] <= 12; v[4]++) {
for (v[5] = v[4] + 1; v[5] <= 13; v[5]++) {
for (v[6] = v[5] + 1; v[6] <= 14; v[6]++) {
for (v[7] = v[6] + 1; v[7] <= 15; v[7]++) {
detokenise(t_clazz, f, v);
family[find_structure(f)] = 1;
}}}}}}}}}
/* Computes the Walsh fmaily for the specified class. */
void find_family(int* clazz, int* family) {
int i, num_ranks;
/* Clears the array to store the Walsh family. */
for (i = 0; i < 128; i++) {
family[i] = 0;
}
num_ranks = calc_num_ranks(clazz);
if (num_ranks == 1) {
/* Family is always {00}. */
family[0] = 1;
} else if (num_ranks == 2) {
/* Family contains only one element. */
family[find_structure(clazz)] = 1;
} else {
tokenise(clazz);
switch (num_ranks) {
case 3:
find_family_3(clazz, family);
break;
case 4:
find_family_4(clazz, family);
break;
case 5:
find_family_5(clazz, family);
break;
case 6:
find_family_6(clazz, family);
break;
case 7:
find_family_7(clazz, family);
break;
case 8:
find_family_8(clazz, family);
break;
}
}
}
/* Prints the specified funciton to STDOUT in decimal. */
void print_function(int* function) {
int i;
printf("[");
for (i = 0; i < 8; i++) {
if (i != 0) {
printf(", ");
}
printf("%d", function[i]);
}
printf("]\t");
}
/* Prints the specified Walsh family to STDOUT in hex. */
void print_family(int* family) {
int i, first = 1;
printf("{");
for (i = 0; i < 128; i++) {
if (family[i]) {
if (!first) {
printf(", ");
}
printf("%02X", i);
first = 0;
}
}
printf("}\n");
}
/* Main method. */
int main(void) {
int done = 0;
int f[8];
int clazz[8];
int family[128];
while(!done) {
/* Reads a function. */
if (scanf("%d %d %d %d %d %d %d %d",
f, f+1, f+2, f+3, f+4, f+5, f+6, f+7)) {
/* Gets the class. */
function_to_class(f, clazz);
/* Outputs the class. */
print_function(clazz);
/* Calculates the Walsh family. */
find_family(clazz, family);
/* Outputs the Walsh family */
print_family(family);
} else {
done = 1;
}
}
return 0;
}