-
Notifications
You must be signed in to change notification settings - Fork 0
/
common_functions.c
executable file
·294 lines (269 loc) · 7.77 KB
/
common_functions.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
287
288
289
290
291
292
293
294
/** COMMON FUNCTIONS - common_functions.c
Generic functions for defining EGGP function sets.
*/
#include "common_functions.h"
/**print_fset. Prints a function set.
Args:
*Function_Set* fsetV - The function set to print.
*/
void print_fset(Function_Set* fsetV){
printf("Function set with %d members:\n", fsetV->functionCount);
Function* f = fsetV->first;
//Loop through function set printinf each function.
while(f != NULL){
printf("%s (%d)\n", f->name, f->arity);
f = f->next;
}
}
/**get_max_arity. Finds the maximum arity of a function set.
Args:
*Function_Set* fsetV - The function set to find the maximum arity of.
Returns:
*int max_arity. The maximum arity of fsetV.
*/
int get_max_arity(Function_Set* fset){
Function* f = fset->first;
int max_arity = 0;
//Loop through function set finding the function with highest arity.
while(f != NULL){
if(f->arity > max_arity){
max_arity = f->arity;
}
f = f->next;
}
//Return highest arity.
return max_arity;
}
/**add_function. Adds a new function to a function set.
Args:
*Function_Set* fset - The function set to append a function to.
*string name - The unique name to be associated with the appended function.
*int arity. The arity of the function to append.
*double (*func)(double* inputs). A function which takes an array of doubles and returns a double.
*/
void add_function(Function_Set* fset, string name, int arity, double (*func)(double* inputs)){
Function* f = malloc(sizeof(Function));
f->name = strdup(name);
f->arity = arity;
f->func = func;
f->next = NULL;
if(fset->functionCount > 0){
fset->last->next = f;
fset->last = f;
}
else{
fset->first = f;
fset->last = f;
}
fset->functionCount = fset->functionCount + 1;
}
/**get_common_fset. Generates a function set based on a string that indicates which common functions should be included.
Args:
*string fsetV. The string representation of the function set to generate. It should be of the form "f1,f2,f3,f4" where each fi is the relevant
name of a function intended to be included (see below for individual names).
Returns:
Function_Set* fset. The generated function set.
*/
Function_Set* get_common_fset(string fsetV){
Function_Set* fset = malloc(sizeof(Function_Set));
fset->functionCount = 0;
int f = 0;
int i = 0;
int o = 0;
//Iterate through the string; every time we see a ',', the preceeding characters are the associated name of a function to be included.
while(o < (int)strlen(fsetV)){
if(fsetV[o] == ',' || o == (int)strlen(fsetV) - 1){
char* name;
if(o == (int)strlen(fsetV) - 1){
name = malloc((o + 2 - i) * sizeof(char));
for(int j = i; j <= o; j++){
name[j - i] = fsetV[j];
}
name[o + 1 - i] = '\0';
}
else{
name = malloc((1 + o - i) * sizeof(char));
for(int j = i; j < o; j++){
name[j - i] = fsetV[j];
}
name[o - i] = '\0';
}
if(strcmp(name, "and") == 0){
add_function(fset, "AND", 2, common_and);
}
else if(strcmp(name, "or") == 0){
add_function(fset, "OR", 2, common_or);
}
else if(strcmp(name, "nand") == 0){
add_function(fset, "NAND", 2, common_nand);
}
else if(strcmp(name, "nor") == 0){
add_function(fset, "NOR", 2, common_nor);
}
else if(strcmp(name, "xor") == 0){
add_function(fset, "XOR", 2, common_xor);
}
else if(strcmp(name, "not") == 0){
add_function(fset, "NOT", 1, common_not);
}
else if(strcmp(name, "not2") == 0){
add_function(fset, "NOT", 2, common_not);
}
else if(strcmp(name, "id") == 0){
add_function(fset, "ID", 1, common_id);
}
else if(strcmp(name, "add") == 0){
add_function(fset, "ADD", 2, common_add);
}
else if(strcmp(name, "sub") == 0){
add_function(fset, "SUB", 2, common_sub);
}
else if(strcmp(name, "div") == 0){
add_function(fset, "DIV", 2, common_div);
}
else if(strcmp(name, "mul") == 0){
add_function(fset, "MUL", 2, common_mul);
}
else if(strcmp(name, "sin") == 0){
add_function(fset, "SIN", 1, common_sin);
}
else if(strcmp(name, "cos") == 0){
add_function(fset, "COS", 1, common_cos);
}
else if(strcmp(name, "exp") == 0){
add_function(fset, "EXP", 1, common_exp);
}
else if(strcmp(name, "log") == 0){
add_function(fset, "LOG", 1, common_log);
}
else{
printf("Unknown function %s\n", name);
}
i = o + 1;
f++;
free(name);
}
o++;
}
return fset;
}
/**get_function. Gets a function from a function set by its name.
Args:
*Function_Set* fset. The function set to search.
*string name. The name of the function to search for.
Returns:
*Function* f, the found function, if it exists. Otherwise NULL.
*/
Function* get_function(Function_Set* fset, string name){
Function* f = fset->first;
//Loop through the function set, checking for the given function name.
for(int i = 0; i < fset->functionCount; i++){
if(strcmp(f->name, name) == 0){
return f;
}
else{
f = f->next;
}
}
return NULL;
}
/**boolcheck. Checks if a set of inputs are valid for a boolean function.
Args:
*double* inputs. The set of inputs to check.
*int count. The number of inputs to count.
Crashes if fails*/
void boolcheck(double* inputs, int count){
for(int i = 0; i < count; i++){
if(inputs[i] != 0.0 && inputs[i] != 1.0){
printf("Broken logic gate!\n\n");
exit(0);
}
}
}
/** VARIOUS COMMON FUNCTIONS */
//Each function declaration is self explanatory.
double common_and(double* inputs){
boolcheck(inputs, 2);
bool i0 = (inputs[0] == 1.0);
bool i1 = (inputs[1] == 1.0);
return i0 && i1;
}
double common_or(double* inputs){
boolcheck(inputs, 2);
bool i0 = (inputs[0] == 1.0);
bool i1 = (inputs[1] == 1.0);
return i0 || i1;
}
double common_nand(double* inputs){
boolcheck(inputs, 2);
bool i0 = (inputs[0] == 1.0);
bool i1 = (inputs[1] == 1.0);
return !(i0 && i1);
}
double common_nor(double* inputs){
boolcheck(inputs, 2);
bool i0 = (inputs[0] == 1.0);
bool i1 = (inputs[1] == 1.0);
return !(i0 || i1);
}
double common_xor(double* inputs){
boolcheck(inputs, 2);
bool i0 = (inputs[0] == 1.0);
bool i1 = (inputs[1] == 1.0);
return ((i0 && !i1) || (!i0 && i1));
}
double common_not(double* inputs){
boolcheck(inputs, 1);
bool i0 = (inputs[0] == 1.0);
return !i0;
}
double common_id(double* inputs){
return inputs[0];
}
double common_add(double* inputs){
return inputs[0] + inputs[1];
}
double common_sub(double* inputs){
return inputs[0] - inputs[1];
}
double common_div(double* inputs){
if(inputs[1] == 0.0){
return 1.0;
}
return inputs[0] / inputs[1];
}
double common_mul(double* inputs){
return inputs[0] * inputs[1];
}
double common_sin(double* inputs){
return sin(inputs[0]);
}
double common_cos(double* inputs){
return cos(inputs[0]);
}
double common_log(double* inputs){
if(inputs[0] <= 0.0){
return 1.0;
}
return log(inputs[0]);
}
double common_exp(double* inputs){
return exp(inputs[0]);
}
/**freeFset. Frees a given Function_Set.
Args:
*Function_Set* fset. The function set to free.
*/
void freeFset(Function_Set* fset){
Function* f = fset->first;
while(f != NULL){
Function* f2 = f->next;
free(f->name);
f->next = NULL;
free(f);
f = f2;
}
fset->first = NULL;
fset->last = NULL;
free(fset);
}