mathai 0.8.0__py3-none-any.whl → 0.8.2__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
mathai/__init__.py CHANGED
@@ -35,7 +35,8 @@ from .inverse import inverse
35
35
 
36
36
  from .trig import trig0, trig1, trig2, trig3, trig4
37
37
 
38
- from .logic import logic0, logic1, logic2, logic3, logic_n
38
+ from .logic import logic0, logic1, logic2, logic3, logic_n, set_sub
39
+ from .logic import auto_apply as logic_x
39
40
 
40
41
  from .apart import apart, apart2
41
42
 
mathai/apart.py CHANGED
@@ -107,13 +107,14 @@ def _apart(eq, v=None):
107
107
 
108
108
  lst = poly(s.children[0], v)
109
109
 
110
- lst = [TreeNode("f_eq", [item, tree_form("d_0")]) for item in lst if "v_" in str_form(item)]
110
+ lst = [simplify(TreeNode("f_eq", [item, tree_form("d_0")])) for item in lst if "v_" in str_form(item)]
111
111
  lst2 = []
112
112
  for item in lst:
113
113
  lst2+=vlist(item)
114
114
  origv = list(set(lst2)-set(origv))
115
115
 
116
116
  out = linear_solve(TreeNode("f_and", lst), [tree_form(item) for item in origv])
117
+
117
118
  for item in out.children:
118
119
 
119
120
  final3 = replace(final3, tree_form(list(set(vlist(item))&set(origv))[0]), inverse(item.children[0], list(set(vlist(item))&set(origv))[0]))
@@ -139,4 +140,6 @@ def apart(eq):
139
140
  return eq2
140
141
 
141
142
  return TreeNode(eq.name, [helper(child) for child in eq.children])
142
- return fx(helper(eq))
143
+ eq = helper(eq)
144
+ eq = fx(eq)
145
+ return eq
mathai/base.py CHANGED
@@ -1,3 +1,4 @@
1
+ import time
1
2
  import copy
2
3
  from fractions import Fraction
3
4
  def use(eq):
@@ -334,7 +335,7 @@ def vlist(eq):
334
335
  out.append(eq.name)
335
336
  for child in eq.children:
336
337
  out += vlist(child)
337
- return sorted(list(set(out)), key=lambda x: int(x[2:]))
338
+ return list(sorted(list(set(out)), key=lambda x: int(x[2:])))
338
339
  def product(lst):
339
340
  if lst == []:
340
341
  return tree_form("d_1")
@@ -352,6 +353,8 @@ def flatten_tree(node):
352
353
  merged_children = []
353
354
  for child in node.children:
354
355
  flattened_child = flatten_tree(child)
356
+ if flattened_child is None:
357
+ return None
355
358
  if flattened_child.name == node.name:
356
359
  merged_children.extend(flattened_child.children)
357
360
  else:
@@ -360,7 +363,7 @@ def flatten_tree(node):
360
363
  else:
361
364
  node.children = [flatten_tree(child) for child in node.children]
362
365
  return node
363
- def dowhile(eq, fx):
366
+ def dowhile(eq, fx, start_time=None, budget=None):
364
367
  if eq is None:
365
368
  return None
366
369
  while True:
@@ -368,6 +371,10 @@ def dowhile(eq, fx):
368
371
  eq2 = fx(eq)
369
372
  if eq2 is None:
370
373
  return None
374
+ if start_time is not None:
375
+ if -start_time + time.monotonic() > budget:
376
+ print("timeout")
377
+ return None
371
378
  eq = copy.deepcopy(eq2)
372
379
  if eq == orig:
373
380
  return orig
mathai/linear.py CHANGED
@@ -8,7 +8,7 @@ from .base import *
8
8
  from .factor import factorconst
9
9
  from .tool import poly
10
10
  def ss(eq):
11
- return dowhile(eq, lambda x: fraction(expand(simplify(x))))
11
+ return dowhile(eq, lambda x: fraction(simplify(x)))
12
12
  def rref(matrix):
13
13
  rows, cols = len(matrix), len(matrix[0])
14
14
  lead = 0
@@ -34,12 +34,14 @@ def rref(matrix):
34
34
  return matrix
35
35
  def islinear(eq, fxconst):
36
36
  eq =simplify(eq)
37
- if all(fxconst(tree_form(item)) and poly(eq, item) is not None and len(poly(eq, item)) <= 2 for item in vlist(eq)):
37
+ if all(not fxconst(tree_form(item)) or (fxconst(tree_form(item)) and poly(eq, item) is not None and len(poly(eq, item)) <= 2)for item in vlist(eq)):
38
38
  return True
39
+ else:
40
+ pass
39
41
  return False
40
42
  def linear(eqlist, fxconst):
41
43
  orig = [item.copy_tree() for item in eqlist]
42
-
44
+
43
45
  if eqlist == [] or not all(islinear(eq, fxconst) for eq in eqlist):
44
46
  return None
45
47
 
@@ -53,10 +55,11 @@ def linear(eqlist, fxconst):
53
55
  for eq in eqlist:
54
56
  varlist(eq, fxconst)
55
57
  vl = list(set(vl))
56
-
58
+
57
59
  if len(vl) > len(eqlist):
58
60
  return TreeNode("f_and", [TreeNode("f_eq", [x, tree_form("d_0")]) for x in eqlist])
59
61
  m = []
62
+
60
63
  for eq in eqlist:
61
64
  s = copy.deepcopy(eq)
62
65
  row = []
@@ -149,6 +152,7 @@ def linear_solve(eq, lst=None):
149
152
  if lst is None:
150
153
  out = linear(copy.deepcopy(eqlist), lambda x: "v_" in str_form(x))
151
154
  else:
155
+
152
156
  out = linear(copy.deepcopy(eqlist), lambda x: any(contain(x, item) for item in lst))
153
157
  if out is None:
154
158
  return None
mathai/logic.py CHANGED
@@ -1,3 +1,4 @@
1
+ import time
1
2
  import itertools
2
3
  from .base import *
3
4
  def c(eq):
@@ -5,6 +6,35 @@ def c(eq):
5
6
  eq = dowhile(eq, logic0)
6
7
  eq = dowhile(eq, logic2)
7
8
  return eq
9
+ def set_sub(eq):
10
+ if eq.name == "f_sub":
11
+ return eq.children[0] & eq.children[1].fx("not")
12
+ return TreeNode(eq.name, [set_sub(child) for child in eq.children])
13
+
14
+ start_time = None
15
+ budget = 2.0
16
+ def auto_apply(eq):
17
+ global start_time
18
+ fs = [logic1, logic2]
19
+ results = []
20
+ pair = 0
21
+ for g in fs:
22
+ for f in fs:
23
+ start_time = time.monotonic()
24
+ r = dowhile(g(f(eq)), logic0)
25
+ if r is None:
26
+ continue
27
+ if r != eq:
28
+ results.append(r)
29
+ start_time = None
30
+ pair += 1
31
+ #print(f"pair {pair}")
32
+ if not results:
33
+ return eq
34
+
35
+ return min(results, key=lambda x: len(str(x)))
36
+
37
+
8
38
  def logic_n(eq):
9
39
  return dowhile(eq, c)
10
40
  def logic0(eq):
@@ -40,6 +70,13 @@ def logic3(eq):
40
70
  return TreeNode("f_forall", [eq.children[0], eq.children[1].fx("not")]).fx("not")
41
71
  return TreeNode(eq.name, [logic3(child) for child in eq.children])
42
72
  def logic2(eq):
73
+ if eq is None:
74
+ return None
75
+ global start_time, budget
76
+ if start_time is not None:
77
+ if -start_time + time.monotonic() > budget:
78
+ #print("timeout")
79
+ return None
43
80
  if eq.name in ["f_exist", "f_forall"]:
44
81
  return TreeNode(eq.name, [eq.children[0], logic2(eq.children[1])])
45
82
  if eq.name not in ["f_and", "f_or", "f_not", "f_imply", "f_equiv"]:
@@ -111,6 +148,10 @@ def logic2(eq):
111
148
 
112
149
  if eq.name in ["f_and", "f_or"] and any(child.children is not None and len(child.children)!=0 for child in eq.children):
113
150
  for i in range(len(eq.children),1,-1):
151
+ if start_time is not None:
152
+ if time.monotonic() - start_time > budget:
153
+
154
+ return None
114
155
  for item in itertools.combinations(enumerate(eq.children), i):
115
156
  op = "f_and"
116
157
  if eq.name == "f_and":
@@ -148,7 +189,20 @@ def logic2(eq):
148
189
  return output
149
190
  return TreeNode(eq.name, [flatten_tree(logic2(child)) for child in eq.children])
150
191
  def logic1(eq):
192
+ if eq is None:
193
+ return None
194
+ global start_time, budget
195
+ if start_time is not None:
196
+ if time.monotonic() - start_time > budget:
197
+ #print("timeout")
198
+ return None
199
+ else:
200
+ pass
151
201
  def helper(eq):
202
+ if start_time is not None:
203
+ if -start_time + time.monotonic() > budget:
204
+ #print("timeout")
205
+ return None
152
206
  if eq.name in ["f_exist", "f_forall"]:
153
207
  return TreeNode(eq.name, [eq.children[0], logic1(eq.children[1])])
154
208
  if eq.name not in ["f_and", "f_or", "f_not", "f_imply", "f_equiv"]:
@@ -156,13 +210,17 @@ def logic1(eq):
156
210
  if eq.name == "f_equiv":
157
211
  A, B = eq.children
158
212
  A, B = logic1(A), logic1(B)
159
- A, B = dowhile(A, logic2), dowhile(B, logic2)
213
+ A, B = dowhile(A, logic2, start_time, budget), dowhile(B, logic2, start_time, budget)
214
+ if A is None or B is None:
215
+ return None
160
216
  return flatten_tree((A & B) | (A.fx("not") & B.fx("not")))
161
217
  if eq.name == "f_imply":
162
218
 
163
219
  A, B = eq.children
164
220
  A, B = logic1(A), logic1(B)
165
- A, B = dowhile(A, logic2), dowhile(B, logic2)
221
+ A, B = dowhile(A, logic2, start_time, budget), dowhile(B, logic2, start_time, budget)
222
+ if A is None or B is None:
223
+ return None
166
224
  return flatten_tree(A.fx("not") | B)
167
225
  return TreeNode(eq.name, [helper(child) for child in eq.children])
168
226
  if eq.name in ["f_exist", "f_forall"]:
@@ -171,30 +229,27 @@ def logic1(eq):
171
229
  return eq
172
230
  eq = helper(eq)
173
231
  eq = flatten_tree(eq)
174
-
175
232
  if len(eq.children) > 2:
176
233
  lst = []
177
234
  l = len(eq.children)
178
-
179
235
  if l % 2 == 1:
180
236
  last_child = eq.children[-1]
181
237
 
182
238
  if isinstance(last_child, TreeNode):
183
- last_child = dowhile(last_child, logic2)
239
+ last_child = dowhile(last_child, logic2, start_time, budget)
184
240
  lst.append(last_child)
185
241
  l -= 1
186
242
 
187
243
  for i in range(0, l, 2):
188
244
  left, right = eq.children[i], eq.children[i+1]
189
245
  pair = TreeNode(eq.name, [left, right])
190
- simplified = dowhile(logic1(pair), logic2)
246
+ simplified = dowhile(logic1(pair), logic2, start_time, budget)
191
247
  lst.append(simplified)
192
248
 
193
249
  if len(lst) == 1:
194
250
  return flatten_tree(lst[0])
195
251
 
196
252
  return flatten_tree(TreeNode(eq.name, lst))
197
-
198
253
  if eq.name == "f_and":
199
254
  lst= []
200
255
  for child in eq.children:
@@ -224,4 +279,3 @@ def logic1(eq):
224
279
  out = out.children[0]
225
280
  return flatten_tree(out)
226
281
  return TreeNode(eq.name, [logic1(child) for child in eq.children])
227
-
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: mathai
3
- Version: 0.8.0
3
+ Version: 0.8.2
4
4
  Summary: Mathematics solving Ai tailored to NCERT
5
5
  Home-page: https://github.com/infinity390/mathai4
6
6
  Requires-Python: >=3.7
@@ -1,6 +1,6 @@
1
- mathai/__init__.py,sha256=JdRPSes54qhzvi6f8Rni2sXljzzirrWZsaCmu9W9rws,1525
2
- mathai/apart.py,sha256=VSS3khE9PNuxiRvdU5JDl4IN-KJBSIFjwR17pkhviXI,4197
3
- mathai/base.py,sha256=zha7eOnfLjJuQcwi29rE0588U1R8v-0zulVPatr2aQo,14843
1
+ mathai/__init__.py,sha256=MX8bkJQ_MOk0fkoCo3r3g5Kwt4zMx9g5M6i64p5kH18,1576
2
+ mathai/apart.py,sha256=CbCX4pAWIaJgu21wZcW9XdEf82oXgxmtTyD3tUI31Ls,4235
3
+ mathai/base.py,sha256=W_Lk9hQp4wTzF4H5DQLsb8O_HAE6bfqgjqHvWJOMJb8,15114
4
4
  mathai/bivariate_inequality.py,sha256=Da-A1kqVynR0tNOlEI7GSTf5T2vNkcF4etL9-EoyPJg,11415
5
5
  mathai/diff.py,sha256=GEiAGMCTT0dO-ULTUGvTlBKy-Io-r2xcgvFJfmqGurY,6879
6
6
  mathai/expand.py,sha256=yioVkQrtCz1fC8npcnokN1WwvJlGxrO1D4aKn8F8ky0,1757
@@ -9,8 +9,8 @@ mathai/fraction.py,sha256=44RuEPQ8z2CGhnxFXrHIy4MkgaE4QDtu7-sEV12GWjc,2597
9
9
  mathai/integrate.py,sha256=6Ik_uAzkLyOWCE9M7mqwH_SVTvEdiG0-UYS2KsDgjLI,17454
10
10
  mathai/inverse.py,sha256=ya7P8WjzfaAL3UXL7xqOh5GaIsXLDZ-F6lZFy3IEgaQ,2931
11
11
  mathai/limit.py,sha256=9F8i9UZh2xb-V8A5Sd1gdhDf9c2RFgpE1GdNn9MvbWI,5703
12
- mathai/linear.py,sha256=36mcYq2mzSidxC2F9gmaMHrXO9vNHYH9kX8CNsopyW8,5145
13
- mathai/logic.py,sha256=wUenUEcIazQH2jnka43QQ9XpLQgY7x3Rq2QUxzFzf-U,9569
12
+ mathai/linear.py,sha256=pTo4gXyosUnwG6h5G1zZOMCJPwQaBZyuaeH8XlrBy9o,5219
13
+ mathai/logic.py,sha256=-riPN3SI0gv53u2jlHY81onlN0NfPXDOpyJszATLZOQ,11353
14
14
  mathai/matrix.py,sha256=k8gIasxtEF1t_B0mJSbDvUJ3FDddvYI8hhy0FMMQ54k,5416
15
15
  mathai/ode.py,sha256=F0Z_Zv2YsnVf4HwIJmqvIenjONaItzy5H739THkLvcU,11616
16
16
  mathai/parser.py,sha256=vXpDobXd6de3-1zdeXVDcZ817oiGM_o2RoVwgUc4U9s,6785
@@ -22,7 +22,7 @@ mathai/structure.py,sha256=u4usB0yPnem2VscgEBqKWxRN9Y4tTjymEaQh1de5Bsk,4121
22
22
  mathai/tool.py,sha256=Wjp8pcBu7MkJr-oAa_bFzKr9mNAB9WrpPISSJd4p-x0,5407
23
23
  mathai/trig.py,sha256=tkn6NgfXj5XzjVB261SuX_mx2g3stqNksoco4O6so90,10713
24
24
  mathai/univariate_inequality.py,sha256=QoE6lZW_cUXa5sbbvwz88DjAVSY--RZt03w02edCVtA,16375
25
- mathai-0.8.0.dist-info/METADATA,sha256=RaL-uVubiBKVs7rz5DdcPKe9ri9Ihe0llTv6bA-7ElY,7735
26
- mathai-0.8.0.dist-info/WHEEL,sha256=wUyA8OaulRlbfwMtmQsvNngGrxQHAvkKcvRmdizlJi0,92
27
- mathai-0.8.0.dist-info/top_level.txt,sha256=ROP4l3OhGYw3ihkQGASr18xM9GsK4z3_6whV5AyXLwE,7
28
- mathai-0.8.0.dist-info/RECORD,,
25
+ mathai-0.8.2.dist-info/METADATA,sha256=7pNC30dw3ZaFDHm2q_CNxEZ5E5CUtUPF7T9YYJahyqY,7735
26
+ mathai-0.8.2.dist-info/WHEEL,sha256=wUyA8OaulRlbfwMtmQsvNngGrxQHAvkKcvRmdizlJi0,92
27
+ mathai-0.8.2.dist-info/top_level.txt,sha256=ROP4l3OhGYw3ihkQGASr18xM9GsK4z3_6whV5AyXLwE,7
28
+ mathai-0.8.2.dist-info/RECORD,,
File without changes