mathai 0.3.0__py3-none-any.whl → 0.3.1__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
@@ -12,5 +12,6 @@ from .logic import logic0, logic1, logic2, logic3
12
12
  from .apart import apart
13
13
  from .console import console
14
14
  from .limit import limit
15
+ from .search import dfs_simplify
15
16
  from .univariate_inequality import wavycurvy, absolute, domain, handle_sqrt
16
17
  from .base import *
mathai/search.py ADDED
@@ -0,0 +1,111 @@
1
+ from mathai import *
2
+ import copy
3
+ from concurrent.futures import ThreadPoolExecutor, TimeoutError
4
+
5
+ def dfs_simplify(equation, functions, true_expr, false_expr,
6
+ max_timeout=25, max_small=4,
7
+ base_timeout=1, time_per_char=0.1, timeout_increase=0.5):
8
+ """
9
+ Perform DFS simplification on a given equation using provided functions.
10
+
11
+ Args:
12
+ equation: The starting expression (TreeNode or parsed equation)
13
+ functions: List of simplification functions
14
+ true_expr: Expression representing True (immediate termination)
15
+ false_expr: Expression representing False (immediate termination)
16
+ max_timeout: Maximum timeout allowed for any function
17
+ max_small: Number of smallest expressions to track
18
+ base_timeout: Base timeout in seconds
19
+ time_per_char: Additional timeout per character of expression
20
+ timeout_increase: Factor to increase timeout for consecutive timeouts
21
+
22
+ Returns:
23
+ tuple(found_boolean, boolean_path, smallest_expressions)
24
+ """
25
+ original_eq = simplify(equation)
26
+ smallest_four = []
27
+
28
+ stack = [(copy.deepcopy(original_eq), [copy.deepcopy(original_eq)])]
29
+ visited = set()
30
+
31
+ found_boolean = False
32
+ boolean_path = None
33
+ boolean_expr = None
34
+
35
+ executor = ThreadPoolExecutor(max_workers=3)
36
+ consecutive_timeouts = 0
37
+
38
+ while stack and not found_boolean:
39
+ current_eq, path = stack.pop()
40
+ expr_str = str(current_eq)
41
+
42
+ if expr_str in visited:
43
+ continue
44
+ visited.add(expr_str)
45
+
46
+ # Thinking message
47
+ printeq(current_eq)
48
+
49
+ # Immediate termination for provided boolean expressions
50
+ if current_eq == true_expr or current_eq == false_expr:
51
+ found_boolean = True
52
+ boolean_path = path
53
+ boolean_expr = current_eq
54
+ break
55
+
56
+ # Insert into smallest_four if qualifies
57
+ inserted = False
58
+ for j in range(len(smallest_four)):
59
+ if len(expr_str) < len(str(smallest_four[j][0])):
60
+ smallest_four.insert(j, (copy.deepcopy(current_eq), copy.deepcopy(path)))
61
+ inserted = True
62
+ break
63
+ if not inserted and len(smallest_four) < max_small:
64
+ smallest_four.append((copy.deepcopy(current_eq), copy.deepcopy(path)))
65
+ if len(smallest_four) > max_small:
66
+ smallest_four = smallest_four[:max_small]
67
+
68
+ # Calculate adaptive timeout with cap
69
+ timeout = (base_timeout + time_per_char * len(expr_str)) * (1 + timeout_increase * consecutive_timeouts)
70
+ if timeout > max_timeout:
71
+ timeout = max_timeout
72
+
73
+ # Try functions that reduce length first
74
+ reduced_any = False
75
+ for fx in functions:
76
+ print(f"[Thinking] Executing {fx.__name__} on current expression (timeout={timeout:.2f}s):")
77
+ printeq(current_eq)
78
+ future = executor.submit(fx, current_eq)
79
+ try:
80
+ new_expr = future.result(timeout=timeout)
81
+ new_expr_str = str(new_expr)
82
+ if len(new_expr_str) <= len(expr_str) and new_expr_str != expr_str:
83
+ reduced_any = True
84
+ stack.append((new_expr, path + [copy.deepcopy(new_expr)]))
85
+ consecutive_timeouts = 0 # reset after success
86
+ except TimeoutError:
87
+ print(f"[Thinking] {fx.__name__} timed out, skipping.")
88
+ consecutive_timeouts += 1
89
+ continue
90
+
91
+ # If no reducing function worked, try growing functions
92
+ if not reduced_any:
93
+ for fx in functions:
94
+ print(f"[Thinking] Trying growing {fx.__name__} on current expression (timeout={timeout:.2f}s):")
95
+ printeq(current_eq)
96
+ future = executor.submit(fx, current_eq)
97
+ try:
98
+ new_expr = future.result(timeout=timeout)
99
+ new_expr_str = str(new_expr)
100
+ if new_expr_str != expr_str:
101
+ stack.append((new_expr, path + [copy.deepcopy(new_expr)]))
102
+ consecutive_timeouts = 0
103
+ break # only take one growing function
104
+ except TimeoutError:
105
+ print(f"[Thinking] {fx.__name__} (growing) timed out, skipping.")
106
+ consecutive_timeouts += 1
107
+ continue
108
+
109
+ executor.shutdown(wait=True)
110
+
111
+ return found_boolean, boolean_path, smallest_four
mathai/simplify.py CHANGED
@@ -149,7 +149,7 @@ def solve2(eq):
149
149
  def clear_div(eq, denom=False):
150
150
  lst = factor_generation(eq)
151
151
  if tree_form("d_0") in lst:
152
- return tree_form("d_0")
152
+ return tree_form("d_0"), True
153
153
  lst3 = [item for item in lst if "v_" not in str_form(item) and compute(item) < 0]
154
154
 
155
155
  sign = True
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.2
2
2
  Name: mathai
3
- Version: 0.3.0
3
+ Version: 0.3.1
4
4
  Summary: Mathematics solving Ai tailored to NCERT
5
5
  Home-page: https://github.com/infinity390/mathai4
6
6
  Author: educated indians are having a low iq and are good for nothing
@@ -1,4 +1,4 @@
1
- mathai/__init__.py,sha256=1e8IUnfQv45U_EIp4GS_ugwVQlqQPWoWn3td5pTF5Lk,615
1
+ mathai/__init__.py,sha256=OrSjLAAeRQrr4eHcOZFtU56gRIjukNKnsEaDnYiZjrk,649
2
2
  mathai/apart.py,sha256=P0B21wHUFitc5RGbNJlwSeOdseOMcav4I7teFNcZmFA,3130
3
3
  mathai/base.py,sha256=l8p8nkCuBDUISI5whtxIuZnOHyFn7F29mLXeKtWWWFQ,12392
4
4
  mathai/console.py,sha256=FsEMqDsgFeBSUiPCm9sfReZp56NSyoFVldqmVwvW4lg,3119
@@ -13,12 +13,13 @@ mathai/linear.py,sha256=eVnDbJYC1TWwg4J7ovBKsaHYlSoDmXk5jQpsqwtVPyI,5481
13
13
  mathai/logic.py,sha256=UvHzRmKcO9AD51tRzHmpNSEhgW5gmaf4XPaQKFjGfC4,9653
14
14
  mathai/parser.py,sha256=aDaF1-tqJHZI4znAcHUSEbz7pPVnvHItOMZ2lp7Vxww,6940
15
15
  mathai/printeq.py,sha256=JTB0_RBcgt1yFviqlHtXGXuSXcpKrxzSxj5WHkOHon4,1318
16
- mathai/simplify.py,sha256=FZrQkG-dhUJqD-V856139Ogj2SAQQ8pNkvUCyfTwZr0,15034
16
+ mathai/search.py,sha256=7YH39WN3pZsGdMfA-dXUSZalc0Orz3kI9fDLaegvV10,4617
17
+ mathai/simplify.py,sha256=NymYVFocnpY8T-LJkyvQPddoF5avUgm3T-JzzRC1R3w,15040
17
18
  mathai/structure.py,sha256=Hgw2y43dwasa6G8z6OS2lmReh7JHwlChGn-FUdEaWY8,4106
18
19
  mathai/tool.py,sha256=87N5Ya7DmHdFOobTYDAjPHNWZuMzMIDjYN0ZtlHjxqE,1099
19
20
  mathai/trig.py,sha256=ywu89MJfAB81JCzsVPBLpGGv8od0LhLn5cgDtLtYwmw,6897
20
21
  mathai/univariate_inequality.py,sha256=_r-kkiS4Hr-jRN7f-EL_E4svAMFWJP1Ea50HJKKbjfk,14778
21
- mathai-0.3.0.dist-info/METADATA,sha256=SvdOmm8n5BQHeXmZeyCBbSBYrJ_MfFLuw4YS1Ljc2jg,7087
22
- mathai-0.3.0.dist-info/WHEEL,sha256=jB7zZ3N9hIM9adW7qlTAyycLYW9npaWKLRzaoVcLKcM,91
23
- mathai-0.3.0.dist-info/top_level.txt,sha256=ROP4l3OhGYw3ihkQGASr18xM9GsK4z3_6whV5AyXLwE,7
24
- mathai-0.3.0.dist-info/RECORD,,
22
+ mathai-0.3.1.dist-info/METADATA,sha256=hsbiZmYg6R_9C2NVmr7rh0g03RuOArr-CZ9r0WJQB80,7087
23
+ mathai-0.3.1.dist-info/WHEEL,sha256=jB7zZ3N9hIM9adW7qlTAyycLYW9npaWKLRzaoVcLKcM,91
24
+ mathai-0.3.1.dist-info/top_level.txt,sha256=ROP4l3OhGYw3ihkQGASr18xM9GsK4z3_6whV5AyXLwE,7
25
+ mathai-0.3.1.dist-info/RECORD,,
File without changes