-
Notifications
You must be signed in to change notification settings - Fork 0
/
symbols.py
87 lines (70 loc) · 2.19 KB
/
symbols.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
from collections import defaultdict
class LinearSymbol:
def __init__(self, name=None):
self.coeffs = defaultdict(int)
if name:
self.coeffs[name] = 1
@staticmethod
def promote(n):
x = LinearSymbol()
x.coeffs[1] = n
return x
def __mul__(self, other):
assert isinstance(other, int)
prod = LinearSymbol()
for i in self.coeffs:
prod.coeffs[i] = self.coeffs[i] * other
return prod
def __add__(self, other):
if isinstance(other, int):
return self + LinearSymbol.promote(other)
assert isinstance(other, LinearSymbol)
sum_ = LinearSymbol()
for i in self.coeffs:
sum_.coeffs[i] += self.coeffs[i]
for i in other.coeffs:
sum_.coeffs[i] += other.coeffs[i]
return sum_
def __sub__(self, other):
return self + other * -1
def __div__(self, other):
return self * (1 / other)
class NonDetSymbol:
def __init__(self, value=None):
self.values = set()
if value:
self.values.add(value)
@staticmethod
def promote(n):
return NonDetSymbol(n)
def __add__(self, other):
if isinstance(other, int):
return self + NonDetSymbol.promote(other)
assert isinstance(other, NonDetSymbol)
sum_ = NonDetSymbol()
for x in self.values:
for y in other.values:
sum_.values.add(x + y)
return sum_
def __mul__(self, other):
if isinstance(other, int):
return self * NonDetSymbol.promote(other)
assert isinstance(other, NonDetSymbol)
prod = NonDetSymbol()
for x in self.values:
for y in other.values:
prod.values.add(x * y)
return prod
def __sub__(self, other):
return self + other * -1
def __div__(self, other):
return self * (1 / other)
if __name__ == "__main__":
from functions import *
symbols = [NonDetSymbol(f'x{i+1}') for i in range(12)]
x = if_function(symbols)
for symbol in x:
print(symbol.coeffs)
import solver
n = solver.System(x)
n.solve([51, 54, 194, 206])