-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathboolean_table_utils.py
398 lines (294 loc) · 13.5 KB
/
boolean_table_utils.py
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
"""Helper Functions For Blast Module Boolean Functions Jupyter Notebook
C Matthew Digman 2024"""
import inspect
import string
from itertools import product
import numpy as np
import matplotlib.pyplot as plt
def test_and(op_in):
return test_match_2arg(lambda x, y: x and y, op_in, true_label = 'A And B', test_label='Your Function', correct_label='Correct?')
def test_or(op_in):
return test_match_2arg(lambda x, y: x or y, op_in, true_label = 'A Or B', test_label='Your Function', correct_label='Correct?')
def test_nor(op_in):
return test_match_2arg(lambda x, y: not (x or y), op_in, true_label = 'A Nor B', test_label='Your Function', correct_label='Correct?')
def test_xor(op_in):
return test_match_2arg(lambda x, y: x != y, op_in, true_label = 'A Xor B', test_label='Your Function', correct_label='Correct?')
def test_nand(op_in):
return test_match_2arg(lambda x, y: not (x and y), op_in, true_label = 'A Nand B', test_label='Your Function', correct_label='Correct?')
def test_iff(op_in):
return test_match_2arg(lambda x, y: (x == y), op_in, true_label = 'A IFF B', test_label='Your Function', correct_label='Correct?')
def test_impliesRight(op_in):
return test_match_2arg(lambda x, y: (not x or y), op_in, true_label = 'A -> B', test_label='Your Function', correct_label='Correct?')
def test_impliesLeft(op_in):
return test_match_2arg(lambda x, y: (x or not y), op_in, true_label = 'A <- B', test_label='Your Function', correct_label='Correct?')
def test_AAndNotB(op_in):
return test_match_2arg(lambda x, y: not (not x or y), op_in, true_label = 'A And (Not B)', test_label='Your Function', correct_label='Correct?')
def test_NotAAndB(op_in):
return test_match_2arg(lambda x, y: not (x or not y), op_in, true_label = '(Not A) and B', test_label='Your Function', correct_label='Correct?')
def test_true(op_in):
return test_match_2arg(lambda x, y: True, op_in, true_label = 'True', test_label='Your Function', correct_label='Correct?')
def test_false(op_in):
return test_match_2arg(lambda x, y: False, op_in, true_label = 'False', test_label='Your Function', correct_label='Correct?')
def test_not(op_in):
return test_match_2arg(lambda x: not x, op_in, true_label = 'A', test_label='Your Function', correct_label='Correct?')
def test_A(op_in):
return test_match_2arg(lambda x, y: x, op_in, true_label = 'A', test_label='Your Function', correct_label='Correct?')
def test_B(op_in):
return test_match_2arg(lambda x, y: y, op_in, true_label = 'B', test_label='Your Function', correct_label='Correct?')
def test_notA(op_in):
return test_match_2arg(lambda x, y: not x, op_in, true_label = 'not A', test_label='Your Function', correct_label='Correct?')
def test_notB(op_in):
return test_match_2arg(lambda x, y: not y, op_in, true_label = 'not B', test_label='Your Function', correct_label='Correct?')
def half_adder(x, y):
return x and y, x != y
def full_adder(z, x, y):
return (x and y) or ((x!=y) and z), (x != y)!=z
all_labels = [\
'A',\
'B',\
'Not A',\
'Not B',\
'A And B',\
'A Or B',\
'A Xor B',\
'A Nor B',\
'A Nand B',\
'A -> B',\
'A <- B',\
'A IFF B',\
'A And (Not B)',\
'(Not B) And A',\
'True',\
'False',\
]
all_ops = [\
lambda x, y: x,\
lambda x, y: y,\
lambda x, y: not x,\
lambda x, y: not y,\
lambda x, y: x and y,\
lambda x, y: x or y,\
lambda x, y: x != y,\
lambda x, y: not (x or y),\
lambda x, y: not (x and y),\
lambda x, y: not x or y,\
lambda x, y: x or not y,\
lambda x, y: x == y,\
lambda x, y: x and not y,\
lambda x, y: y and not x,\
lambda x, y: True,\
lambda x, y: False,\
]
def gen_2_argument():
A_text = np.array([True, True, False, False])
B_text = np.array([True, False, True, False])
loc_text = []
for itrc in range(len(all_ops)):
op_text = np.zeros(A_text.size,dtype=np.bool_)
for itrx in range(A_text.size):
op_text[itrx] = all_ops[itrc](A_text[itrx], B_text[itrx])
loc_text.append(op_text)
return loc_text
# generate all of the two argument maps
all_text = gen_2_argument()
def test_match_2arg(op_true, op_test, true_label = 'True', test_label='Test', correct_label='Correct?',input_label=None):
if not callable(op_true) or not callable(op_test):
raise ValueError('Inputs must be functions')
n_par = len(inspect.getfullargspec(op_true).args)
if n_par != len(inspect.getfullargspec(op_test).args):
raise ValueError('Input functions must have same number of parameters')
inputs_text = np.array(list(product([True,False],repeat=n_par)))
n_inputs = inputs_text.shape[0]
# check the number of return elements
res0 = op_true(*inputs_text[0])
res1 = op_test(*inputs_text[0])
if isinstance(res0,np.bool_) or isinstance(res0,bool):
n_res0 = 1
if not (isinstance(res1,np.bool_) or isinstance(res1,bool)):
raise ValueError('Input Functions must have same number of return elements')
else:
if not hasattr(res0,'__len__') or not hasattr(res1,'__len__') or len(res1) != len(res0):
raise ValueError('Input Functions must have same number of return elements')
else:
n_res0 = len(res0)
true_text = np.zeros((n_inputs,n_res0),dtype=np.bool_)
test_text = np.zeros((n_inputs,n_res0),dtype=np.bool_)
for itrx in range(n_inputs):
true_text[itrx] = op_true(*inputs_text[itrx])
test_text[itrx] = op_test(*inputs_text[itrx])
correct_text = true_text == test_text
cell_labels = []
if input_label is None:
for itr in range(n_par):
cell_labels.append(string.ascii_uppercase[itr])
elif isinstance(input_label,str):
for itr in range(n_par):
cell_labels.append(input_label+' '+str(itr))
else:
for itr in range(n_par):
if len(input_label) <= itr:
cell_labels.append('')
else:
cell_labels.append(input_label[itr])
if n_res0 == 1:
cell_labels.append(true_label)
cell_labels.append(test_label)
cell_labels.append(correct_label)
else:
for itr in range(n_res0):
if isinstance(true_label,str):
cell_labels.append(true_label+' '+str(itr))
elif len(true_label) <= itr:
cell_labels.append('')
else:
cell_labels.append(true_label[itr])
for itr in range(n_res0):
if isinstance(test_label,str):
cell_labels.append(test_label+' '+str(itr))
elif len(true_label) <= itr:
cell_labels.append('')
else:
cell_labels.append(test_label[itr])
for itr in range(n_res0):
if isinstance(correct_label,str):
cell_labels.append(correct_label+' '+str(itr))
elif len(true_label) <= itr:
cell_labels.append('')
else:
cell_labels.append(correct_label[itr])
figsize = (6*len(cell_labels)/5,1.5*(n_inputs+1)/5)
cell_text = np.vstack([inputs_text.T, true_text.T, test_text.T, correct_text.T])
fig = gen_figure(cell_text, cell_labels, figsize=figsize)
correct = np.all(correct_text)
if correct:
fig.set_facecolor('honeydew')
fig.suptitle('Thats Right!',verticalalignment='top')
else:
if n_res0 == 1 and n_par == 2:
res = np.all(test_text[:,0] == np.array(all_text), axis=1)
if np.any(res):
op_idx = np.argmax(res)
label_loc = all_labels[op_idx]
else:
label_loc = "Huh???"
title_label = 'Try Again! The operation you gave is called: ' + label_loc
else:
title_label = 'Try Again!'
fig.set_facecolor('mistyrose')
fig.suptitle(title_label, verticalalignment='top')
return correct
def plot_2arg_table(op_in,op_label='Test',figsize=None):
A_text = np.array([True, True, False, False])
B_text = np.array([True, False, True, False])
op_text = np.zeros(A_text.size,dtype=np.bool_)
for itrx in range(A_text.size):
op_text[itrx] = op_in(A_text[itrx], B_text[itrx])
cell_labels = ['A', 'B', op_label]
gen_figure([A_text, B_text, op_text], cell_labels, figsize=figsize)
def plot_1arg_table(op_in,op_label='Test',figsize=None):
A_text = np.array([True, False])
op_text = np.zeros(A_text.size,dtype=np.bool_)
for itrx in range(A_text.size):
op_text[itrx] = op_in(A_text[itrx])
cell_labels = ['A', op_label]
gen_figure([A_text, op_text], cell_labels, figsize=figsize)
def all_2_argument():
figsize = (19.2,1.5)
#figsize = None
fig = gen_figure(all_text, all_labels, figsize=figsize)
def gen_figure(cell_text, cell_labels, figsize=None):
cell_text = np.array(cell_text).T
cell_color = np.zeros(cell_text.shape, dtype='object')
cell_color[:] = 'lightcoral'
cell_color[cell_text] = 'lightgreen'
fig, ax = plt.subplots(1, 1, figsize=figsize)
ax.axis('tight')
ax.axis('off')
ax.table(cellText=cell_text, cellColours=cell_color, colLabels=cell_labels, loc='center', cellLoc='center', rowLoc='center')
fig.tight_layout()
return fig
def ripple_adder(num1bool, num2bool):
if not isinstance(num1bool,np.ndarray) or not isinstance(num2bool,np.ndarray):
raise ValueError('Inputs must be array of booleans')
if num1bool.size != num2bool.size:
raise ValueError('Inputs must be the same size')
n_b = num1bool.size
res = np.zeros(num1bool.size,dtype=np.bool_)
carry = False
for itrb in range(n_b-1,-1,-1):
carry, res[itrb] = full_adder(carry, num1bool[itrb], num2bool[itrb])
return res
def unsigned2boolarray(number,n_bits=64):
if not isinstance(n_bits,int) or n_bits <= 0:
raise ValueError('Number of bits must be a positive integer')
if not isinstance(number,int) or number < 0:
raise ValueError('This function only takes non-negative integers')
return (np.array([*format(number,'0'+str(n_bits)+'b')])=='1')[-n_bits:]
def boolarray2unsigned(bools):
return int(''.join([format(bools[itr],'b') for itr in range(0,len(bools))]),2)
def ripple_adder_combos(adder_in, n_bits):
if not isinstance(n_bits,int) or n_bits <= 0:
raise ValueError('Number of bits must be a positive integer')
reps = np.zeros((2**n_bits,n_bits),dtype=np.bool_)
for itrx in range(0,2**n_bits):
reps[itrx] = unsigned2boolarray(itrx,n_bits)
n_tests = 2**n_bits*2**n_bits
As = np.zeros(n_tests,dtype=np.int64)
Bs = np.zeros(n_tests,dtype=np.int64)
expecteds = np.zeros(n_tests,dtype=np.int64)
results = np.zeros(n_tests,dtype=np.int64)
itrt = 0
for itrx in range(0,2**n_bits):
bool1 = reps[itrx]
for itry in range(0,2**n_bits):
bool2 = reps[itry]
As[itrt] = itrx
Bs[itrt] = itry
expecteds[itrt] = (itrx+itry)%(2**n_bits)
results[itrt] = boolarray2unsigned(adder_in(bool1,bool2))
itrt = itrt+1
return As,Bs,expecteds,results
def test_ripple_adder(adder_in,bits_max=4):
if not isinstance(bits_max,int) or bits_max <= 0:
raise ValueError('Number of bits must be a positive integer')
pass_bits = np.zeros(bits_max,dtype=np.bool_)
for itrb in range(1,bits_max+1):
As, Bs, expecteds, results = ripple_adder_combos(adder_in, itrb)
pass_bits[itrb-1] = np.all(expecteds == results)
return np.all(pass_bits), pass_bits
def gen_adder_figure(adder_in, n_bits=3):
As, Bs, expecteds, results = ripple_adder_combos(adder_in,n_bits)
correct = expecteds == results
figsize = (8,3.5*(n_bits**2+1)/5)
cell_text = np.array([As,Bs,expecteds,results]).T
cell_color = np.zeros(cell_text.shape, dtype='object')
cell_color[:] = 'white'
cell_color[:,3] = 'lightcoral'
cell_color[correct,3] = 'lightgreen'
cell_labels = ['A (base 10)','B (base 10)','Expected Sum (base 10)','Test Sum (base 10)']
fig, ax = plt.subplots(1, 1, figsize=figsize)
ax.axis('tight')
ax.axis('off')
ax.table(cellText=cell_text, cellColours=cell_color, colLabels=cell_labels, loc='center', cellLoc='center', rowLoc='center')
all_correct = np.all(correct)
if all_correct:
fig.set_facecolor('honeydew')
title_label = 'Thats Right!'
else:
fig.set_facecolor('mistyrose')
title_label = 'Try Again!'
fig.suptitle(title_label, verticalalignment='top')
#fig.tight_layout()
fig.subplots_adjust()
return fig
def plot_and_table():
return plot_2arg_table(lambda x, y: x and y,op_label='And',figsize=(3.2,1.5))
def plot_andor_table():
fig1 = plot_2arg_table(lambda x, y: x and y,op_label='And',figsize=(3.2,1.5))
fig2 = plot_2arg_table(lambda x, y: x or y,op_label='Or',figsize=(3.2,1.5))
def plot_not_table():
return plot_1arg_table(lambda x: not x,op_label='Not A',figsize=(6*3/5+0.2,1.5*3/5+0.5))
def test_half_adder(half_adder_in):
return test_match_2arg(half_adder, half_adder_in, [r"$C_{out}$ Intended",'S Intended'], [r"$C_{out}$ Test",'S Test'],[r"$C_{out}$ Correct?",'S Correct?'],['A','B'])
def test_full_adder(full_adder_in):
test_match_2arg(full_adder, full_adder_in, [r"$C_{out}$ Intended",'S Intended'], [r"$C_{out}$ Test",'S Test'],[r"$C_{out}$ Correct?",'S Correct?'],['A','B',r"$C_{in}$"])