-
Notifications
You must be signed in to change notification settings - Fork 0
/
BoolFunc.py
81 lines (71 loc) · 2.7 KB
/
BoolFunc.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
import FuncElement
import math
import itertools
class BoolFunc:
def __init__(self):
self.vector = None
self.elements = None
def __str__(self):
return str(tuple(str(element) for element in self.elements))
def setFromVector(self, vector):
self.vector = vector
self.buildPDNF()
def buildPDNF(self):
self.elements = list()
n = int(math.log2(len(self.vector)))
combinations = itertools.product([False, True], repeat = n)
for v, item in zip(self.vector, combinations):
if v: self.elements.append(FuncElement.FuncElement(item))
def setFromElements(self, elementList):
self.elements = elementList
self.vector = list()
n = len(elementList[0].values)
combinations = itertools.product([False, True], repeat = n)
for item in combinations:
v = False
for element in elementList:
temp = True
for i in range(n):
if element.values[i] == True: temp = temp and item[i]
elif element.values[i] == False: temp = temp and not item[i]
if not temp: break
if temp:
v = True
break
self.vector.append(v)
def equalElements(self, elementList):
n = len(elementList[0].values)
combinations = itertools.product([False, True], repeat = n)
for i, item in zip(range(pow(2, n)), combinations):
v1 = self.vector[i]
v2 = False
for element in elementList:
temp = True
for j in range(n):
if element.values[j] == True: temp = temp and item[j]
elif element.values[j] == False: temp = temp and not item[j]
if not temp: break
if temp:
v2 = True
break
if v1 != v2: return False
return True
def equalVectors(self, vectorList):
n = len(self.vector)
for i in range(n):
v1 = self.vector[i]
v2 = False
for vector in vectorList:
if vector[i] == True:
v2 = True
break
if v1 != v2: return False
return True
def main():
bf1 = BoolFunc()
bf1.setFromVector([False, True, True, True])
bf2 = BoolFunc()
bf2.setFromElements([FuncElement.FuncElement([True, None]), FuncElement.FuncElement([None, True])])
print(bf1.equalVectors([element.getVector() for element in bf2.elements]))
if __name__ == '__main__':
main()