-
Notifications
You must be signed in to change notification settings - Fork 0
/
theorypots.py
333 lines (285 loc) · 12.6 KB
/
theorypots.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
__author__ = 'ak'
from copy import deepcopy
from interval import intervals
from assumption import result, assumption
from theorypots_linear_assumptions_for_pot import assumption_ratio_to_linear
from theorypots_numerical import decompose # TODO: names refactoring
from theorypots_symbol_assumptions import pot_symbol_variants
from theorypots_intervals import test_linear_assumption
from sympy import Number
class theorypot:
def __init__(self):
self.symbol_intervals = dict()
self.assumptions = list()
self.new_symbol_intervals = dict()
self.new_assumptions = list()
self.symbol_variants = pot_symbol_variants()
def __str__(self):
ret = ""
for s in self.symbol_intervals:
ret += 'symbol ' + str(s) + ':' + str(self.symbol_intervals[s]) + "\n"
for s in self.assumptions:
ret += "\t" + str(s) + "\n"
return ret
def clean_new_lists(self):
self.new_symbol_intervals = dict()
self.new_assumptions = list()
def linear_assumption_basic_test_on_possibility(self, linear_assumption):
assumption_deps = linear_assumption.depends()
deps_len = len(assumption_deps)
if deps_len == 0:
if linear_assumption.test() == result.not_possible: return False
return True
if deps_len == 1:
symbol = assumption_deps[0]
if symbol not in self.symbol_intervals: return True
lim = decompose(linear_assumption)
if not lim:
print("[warn] decompose failed:", linear_assumption)
return True
res = deepcopy(self.symbol_intervals[symbol])
res *= lim
if res.is_zero():
return False
return True
some_res = self.basic_test_linear_assumption(linear_assumption)
if some_res == result.correct:
return True
elif some_res == result.not_possible:
return False
if not test_linear_assumption(self.symbol_intervals, linear_assumption):
return False
return True
def has_already(self, linear_assumption):
for assumption in self.assumptions:
if assumption == linear_assumption:
return True
return False
def basic_test_linear_assumption(self, linear_assumption):
lexp, lsign = linear_assumption.exp, linear_assumption.sign
for assumption in self.assumptions:
exp, sign = assumption.exp, assumption.sign
if exp == lexp:
if sign == '>' and lsign == '>' : return result.correct
elif sign == '>' and lsign == '>=': return result.correct
elif sign == '>' and lsign == '==': return result.not_possible
elif sign == '>' and lsign == '!=': return result.correct
elif sign == '>=' and lsign == '>' : return result.possible
elif sign == '>=' and lsign == '>=': return result.correct
elif sign == '>=' and lsign == '==': return result.possible
elif sign == '>=' and lsign == '!=': return result.possible
elif sign == '==' and lsign == '>' : return result.not_possible
elif sign == '==' and lsign == '>=': return result.correct
elif sign == '==' and lsign == '==': return result.correct
elif sign == '==' and lsign == '!=': return result.not_possible
elif sign == '!=' and lsign == '>' : return result.possible
elif sign == '!=' and lsign == '>=': return result.possible
elif sign == '!=' and lsign == '==': return result.not_possible
elif sign == '!=' and lsign == '!=': return result.correct
elif exp == lexp*Number(-1):
if sign == '>' and lsign == '>' : return result.not_possible
elif sign == '>' and lsign == '>=': return result.not_possible
elif sign == '>' and lsign == '==': return result.not_possible
elif sign == '>' and lsign == '!=': return result.correct
elif sign == '>=' and lsign == '>' : return result.not_possible
elif sign == '>=' and lsign == '>=': return result.possible
elif sign == '>=' and lsign == '==': return result.possible
elif sign == '>=' and lsign == '!=': return result.possible
elif sign == '==' and lsign == '>' : return result.not_possible
elif sign == '==' and lsign == '>=': return result.correct
elif sign == '==' and lsign == '==': return result.correct
elif sign == '==' and lsign == '!=': return result.not_possible
elif sign == '!=' and lsign == '>' : return result.possible
elif sign == '!=' and lsign == '>=': return result.possible
elif sign == '!=' and lsign == '==': return result.not_possible
elif sign == '!=' and lsign == '!=': return result.correct
return result.possible
def _add_linear_assumption(self, linear_assumption):
assumption_deps = linear_assumption.depends()
deps_len = len(assumption_deps)
if deps_len == 0:
if linear_assumption.test() == result.not_possible: return False
return True
if deps_len == 1:
symbol = assumption_deps[0]
if symbol not in self.symbol_intervals:
self.symbol_intervals[symbol] = intervals()
lim = decompose(linear_assumption)
if not lim:
print("[warn] decompose failed:", linear_assumption)
return False
res = deepcopy(self.symbol_intervals[symbol])
res *= lim
if res.is_zero(): return False
if self.symbol_intervals[symbol] != res:
self.new_symbol_intervals[symbol] = res
self.symbol_intervals[symbol] = res
if not self.symbol_variants.test_by_symbol_intervals(self.symbol_intervals):
#print("[info] filtered by test_by_symbol_intervals()")
return False
return True
some_res = self.basic_test_linear_assumption(linear_assumption)
if some_res == result.correct:
return True
elif some_res == result.not_possible:
#print("[info] filtered by basic_test_linear_assumption")
return False
if not test_linear_assumption(self.symbol_intervals, linear_assumption):
#print("[info] filtered by test_linear_assumption")
return False
self.assumptions.append(linear_assumption)
self.new_assumptions.append(linear_assumption)
if not self.symbol_variants.linear_assumption_decompose(self, linear_assumption, assumption_deps):
#print("[info] filtered by linear_assumption_decompose")
return False
return True
def add_linear_assumptions(self, linear_assumptions):
for linear_assumption in linear_assumptions:
if not self._add_linear_assumption(linear_assumption):
return False
return True
def apply_assumption_to_pot(pot, assumption):
result_pots = list()
linear_variations_variations = assumption_ratio_to_linear(assumption, pot)
for linear_assumptions in linear_variations_variations:
temp_pot = deepcopy(pot)
if temp_pot.add_linear_assumptions(linear_assumptions):
result_pots.append(temp_pot)
return result_pots
def apply_assumptions_to_pot(pot, assumptions):
first_pot = deepcopy(pot)
first_pot.clean_new_lists()
current_pots = [ first_pot , ]
for assumption in assumptions:
new_current_pots = list()
for cp in current_pots:
new_current_pots += apply_assumption_to_pot(cp, assumption)
current_pots = new_current_pots
return current_pots
def apply_or_assumptions_to_pot(pot, or_assumptions):
new_pots = list()
for assumptions in or_assumptions:
temp_pot = deepcopy(pot)
new_pots += apply_assumptions_to_pot(temp_pot, assumptions)
return new_pots
class theorypots:
def __init__(self):
self.pots = list()
self.pots.append(theorypot())
def is_valid(self):
if len(self.pots) == 0: return False
else: return True
def add_or_assumptions(self, or_assumptions):
new_pots = list()
for pot in self.pots:
new_pots += apply_or_assumptions_to_pot(pot, or_assumptions)
self.pots = new_pots
return self.is_valid()
def add_assumptions(self, assumpts):
new_pots = list()
for pot in self.pots:
new_pots += apply_assumptions_to_pot(pot, assumpts)
self.pots = new_pots
return self.is_valid()
def __str__(self):
ret = ""
if len(self.pots) == 0: return "no pots\n"
for i, pot in enumerate(self.pots):
ret += 'pot #' + str(i) + ':' + '\n'
ret += str(pot) + '\n'
return ret
def get_common_and_not_assumptions(self):
if not self.is_valid(): return ('', [])
assumps_by_pot = list()
all_assumps = list()
for pot in self.pots:
assumps = list()
for s in pot.new_symbol_intervals:
assumps += pot.new_symbol_intervals[s].get_assumptions(s)
assumps += pot.new_assumptions
assumps_by_pot.append(assumps)
all_assumps += assumps
n = len(assumps_by_pot)
common_assumps = list()
while len(all_assumps) > 0:
current_assumpt = all_assumps[0]
new_all_assumps = list()
i = 0
for assumpt in all_assumps:
if assumpt == current_assumpt:
i += 1
else:
new_all_assumps.append(assumpt)
all_assumps = new_all_assumps
if i == n:
common_assumps.append(current_assumpt)
new_assumps_by_pot = list()
for pot in assumps_by_pot:
new_pot = list()
for a in pot:
common = False
for ca in common_assumps:
if a == ca:
common = True
break
if not common:
new_pot.append(a)
if len(new_pot) == 0: continue
bad = False
for pot in new_assumps_by_pot:
diff = False
for a1 in new_pot:
found = False
for a2 in pot:
if a1 == a2:
found = True
break
if not found:
diff = True
break
for a1 in pot:
found = False
for a2 in new_pot:
if a1 == a2:
found = True
break
if not found:
diff = True
break
if not diff:
bad = True
break
if not bad:
new_assumps_by_pot.append(new_pot)
return (common_assumps, new_assumps_by_pot)
import time
if __name__ == "__main__":
from assumption import assumption
#from expression import expr
from sympy import Symbol, Number
t = theorypots()
b1 = Symbol('b1')
b2 = Symbol('b2')
a11 = Symbol('a11')
a12 = Symbol('a12')
a21 = Symbol('a21')
a22 = Symbol('a22')
c1 = Symbol('c1')
c2 = Symbol('c2')
t1 = time.clock()
print('ret:', t.add_assumptions([assumption(b2, '>=', Number(0)), assumption(b1, '>=', Number(0)), assumption(c1, '<', Number(0)), assumption(a11, '>', Number(0))] ))
#print(t)
print('ret:', t.add_or_assumptions([ [assumption(a21, '<=', Number(0))], [assumption(a21, '>', Number(0)), assumption(b2*a11-b1*a21, '>=', Number(0))] ]))
#print('+' * 60)
#print(t)
print('ret:', t.add_assumptions([assumption( (c1*a12-c2*a11)/a11 , '>', Number(0)) , assumption( (a11*a22-a12*a21)/a11 , '>', Number(0)) ] ))
#print(t)
print('ret:', t.add_or_assumptions([ [assumption( a12/a11, '<=', Number(0))],
[
assumption(a12/a11, '>', Number(0)),
assumption( (b1*a11*a22 - a12*b2*a11)/(a12*a11*a22-a12*a12*a21), '>=', Number(0))]
]))
print('ret:', t.add_assumptions([assumption( a22 , '>', Number(0)) , assumption( (c1*a22-c2*a21) , '>', Number(0)) ] ))
print('pots:', t, sep='\n')
t2 = time.clock()
print('calculation time: ', t2 - t1)