-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgen_gates.py
executable file
·76 lines (65 loc) · 2.1 KB
/
gen_gates.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
#!/usr/bin/env python
from sympy import *
alphabet = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']
# Compute the number of symbols for the specified y-cardinality
def nsymbols(cardinality):
return int(((cardinality + 2)*(cardinality + 1)) / 2)
def get_nsymbols(n):
return symbols(' '.join(alphabet[0:n]))
def symbols_for_yn(n):
return list(get_nsymbols(nsymbols(n)))
def drop_bottom(n, symbols):
res = []
symbols_on_level = 1
symbols_appended = 0
for i in range(n):
add = symbols[symbols_appended:symbols_appended+symbols_on_level]
symbols_appended += symbols_on_level
res = res + add
symbols_on_level += 1
return res
def drop_right(n, symbols):
res = []
add_on_level = 0
skip = 0
symbols_on_level = 1
for i in range(n+1):
add = symbols[skip:skip+add_on_level]
res = res + add
skip += symbols_on_level
symbols_on_level += 1
add_on_level += 1
return res
def drop_left(n, symbols):
res = []
add_on_level = 0
skip = 0
symbols_on_level = 1
symbols_encountered = 0
for i in range(n+1):
add = symbols[skip:skip+add_on_level]
res = res + add
symbols_encountered += symbols_on_level
skip = symbols_encountered + 1
symbols_on_level += 1
add_on_level += 1
return res
def rec_formula_for_yn(n, symbols):
if n == 0:
return symbols[0]
bottom_formula = rec_formula_for_yn(n-1, drop_bottom(n, symbols))
right_formula = rec_formula_for_yn(n-1, drop_right(n, symbols))
left_formula = rec_formula_for_yn(n-1, drop_left(n, symbols))
return (bottom_formula & right_formula) | (bottom_formula & left_formula) | (left_formula & right_formula)
def formula_for_yn(n):
symbols = symbols_for_yn(n)
bottom_formula = rec_formula_for_yn(n-1, drop_bottom(n, symbols))
right_formula = rec_formula_for_yn(n-1, drop_right(n, symbols))
left_formula = rec_formula_for_yn(n-1, drop_left(n, symbols))
f = (bottom_formula & right_formula) | (bottom_formula & left_formula) | (left_formula & right_formula)
return to_dnf(f, simplify=True)
n = 3
y_formula = formula_for_yn(n)
init_printing()
#print('Y{}_formula: {}'.format(n, y_formula))
print(y_formula)