mathai 0.3.3__tar.gz → 0.3.5__tar.gz
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-0.3.3 → mathai-0.3.5}/PKG-INFO +1 -1
- {mathai-0.3.3 → mathai-0.3.5}/mathai/integrate.py +15 -14
- mathai-0.3.5/mathai/search.py +117 -0
- {mathai-0.3.3 → mathai-0.3.5}/mathai.egg-info/PKG-INFO +1 -1
- {mathai-0.3.3 → mathai-0.3.5}/setup.py +1 -1
- mathai-0.3.3/mathai/search.py +0 -108
- {mathai-0.3.3 → mathai-0.3.5}/README.md +0 -0
- {mathai-0.3.3 → mathai-0.3.5}/mathai/__init__.py +0 -0
- {mathai-0.3.3 → mathai-0.3.5}/mathai/apart.py +0 -0
- {mathai-0.3.3 → mathai-0.3.5}/mathai/base.py +0 -0
- {mathai-0.3.3 → mathai-0.3.5}/mathai/console.py +0 -0
- {mathai-0.3.3 → mathai-0.3.5}/mathai/diff.py +0 -0
- {mathai-0.3.3 → mathai-0.3.5}/mathai/expand.py +0 -0
- {mathai-0.3.3 → mathai-0.3.5}/mathai/factor.py +0 -0
- {mathai-0.3.3 → mathai-0.3.5}/mathai/fraction.py +0 -0
- {mathai-0.3.3 → mathai-0.3.5}/mathai/inverse.py +0 -0
- {mathai-0.3.3 → mathai-0.3.5}/mathai/limit.py +0 -0
- {mathai-0.3.3 → mathai-0.3.5}/mathai/linear.py +0 -0
- {mathai-0.3.3 → mathai-0.3.5}/mathai/logic.py +0 -0
- {mathai-0.3.3 → mathai-0.3.5}/mathai/parser.py +0 -0
- {mathai-0.3.3 → mathai-0.3.5}/mathai/printeq.py +0 -0
- {mathai-0.3.3 → mathai-0.3.5}/mathai/simplify.py +0 -0
- {mathai-0.3.3 → mathai-0.3.5}/mathai/structure.py +0 -0
- {mathai-0.3.3 → mathai-0.3.5}/mathai/tool.py +0 -0
- {mathai-0.3.3 → mathai-0.3.5}/mathai/trig.py +0 -0
- {mathai-0.3.3 → mathai-0.3.5}/mathai/univariate_inequality.py +0 -0
- {mathai-0.3.3 → mathai-0.3.5}/mathai.egg-info/SOURCES.txt +0 -0
- {mathai-0.3.3 → mathai-0.3.5}/mathai.egg-info/dependency_links.txt +0 -0
- {mathai-0.3.3 → mathai-0.3.5}/mathai.egg-info/requires.txt +0 -0
- {mathai-0.3.3 → mathai-0.3.5}/mathai.egg-info/top_level.txt +0 -0
- {mathai-0.3.3 → mathai-0.3.5}/setup.cfg +0 -0
@@ -376,24 +376,25 @@ def rm_const(equation):
|
|
376
376
|
return rm_const(TreeNode("f_integrate",[equation, wrt])) *const
|
377
377
|
equation = eq2
|
378
378
|
return TreeNode(equation.name, [rm_const(child) for child in equation.children])
|
379
|
+
|
379
380
|
def integrate_formula(equation):
|
380
381
|
if equation.name == "f_ref":
|
381
|
-
return equation
|
382
|
+
return equation.copy_tree()
|
382
383
|
eq2 = equation.copy_tree()
|
383
384
|
if eq2.name == "f_integrate":
|
384
|
-
|
385
|
+
integrand = eq2.children[0]
|
385
386
|
wrt = eq2.children[1]
|
386
|
-
if
|
387
|
-
return
|
388
|
-
if not contain(
|
389
|
-
return wrt*
|
390
|
-
out = transform_formula(simplify(trig0(
|
387
|
+
if integrand == wrt:
|
388
|
+
return TreeNode("f_add", [TreeNode("f_power", [wrt.copy_tree(), TreeNode("2")]), TreeNode("f_div", [TreeNode("1"), TreeNode("2")])]) # x^2/2
|
389
|
+
if not contain(integrand, wrt):
|
390
|
+
return TreeNode("f_mul", [wrt.copy_tree(), integrand.copy_tree()]) # constant * dx
|
391
|
+
out = transform_formula(simplify(trig0(integrand)), wrt.name, formula_gen[0], formula_gen[1], formula_gen[2])
|
391
392
|
if out is not None:
|
392
393
|
return out
|
393
|
-
|
394
|
-
if
|
395
|
-
|
396
|
-
|
397
|
-
|
398
|
-
|
399
|
-
|
394
|
+
expr_str = str_form(integrand)
|
395
|
+
if expr_str.count("f_sin") + expr_str.count("f_cos") > 2:
|
396
|
+
out = transform_formula(integrand, wrt.name, formula_gen4[0], formula_gen4[1], formula_gen4[2])
|
397
|
+
if out is not None:
|
398
|
+
return out
|
399
|
+
return TreeNode(eq2.name, [integrate_formula(child) for child in eq2.children])
|
400
|
+
|
@@ -0,0 +1,117 @@
|
|
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 using predicate functions
|
50
|
+
if true_expr(current_eq):
|
51
|
+
found_boolean = True
|
52
|
+
boolean_path = path
|
53
|
+
boolean_expr = current_eq
|
54
|
+
break
|
55
|
+
if false_expr(current_eq):
|
56
|
+
found_boolean = True
|
57
|
+
boolean_path = path
|
58
|
+
boolean_expr = current_eq
|
59
|
+
break
|
60
|
+
|
61
|
+
|
62
|
+
# Insert into smallest_four if qualifies
|
63
|
+
inserted = False
|
64
|
+
for j in range(len(smallest_four)):
|
65
|
+
if len(expr_str) < len(str(smallest_four[j][0])):
|
66
|
+
smallest_four.insert(j, (copy.deepcopy(current_eq), copy.deepcopy(path)))
|
67
|
+
inserted = True
|
68
|
+
break
|
69
|
+
if not inserted and len(smallest_four) < max_small:
|
70
|
+
smallest_four.append((copy.deepcopy(current_eq), copy.deepcopy(path)))
|
71
|
+
if len(smallest_four) > max_small:
|
72
|
+
smallest_four = smallest_four[:max_small]
|
73
|
+
|
74
|
+
# Calculate adaptive timeout with cap
|
75
|
+
timeout = (base_timeout + time_per_char * len(expr_str)) * (1 + timeout_increase * consecutive_timeouts)
|
76
|
+
if timeout > max_timeout:
|
77
|
+
timeout = max_timeout
|
78
|
+
|
79
|
+
# Try functions that reduce length first
|
80
|
+
reduced_any = False
|
81
|
+
for fx in functions:
|
82
|
+
print(f"[Thinking] Executing {fx.__name__} on current expression (timeout={timeout:.2f}s):")
|
83
|
+
printeq(current_eq)
|
84
|
+
future = executor.submit(fx, current_eq)
|
85
|
+
try:
|
86
|
+
new_expr = future.result(timeout=timeout)
|
87
|
+
new_expr_str = str(new_expr)
|
88
|
+
if len(new_expr_str) <= len(expr_str) and new_expr_str != expr_str:
|
89
|
+
reduced_any = True
|
90
|
+
stack.append((new_expr, path + [copy.deepcopy(new_expr)]))
|
91
|
+
consecutive_timeouts = 0 # reset after success
|
92
|
+
except TimeoutError:
|
93
|
+
print(f"[Thinking] {fx.__name__} timed out, skipping.")
|
94
|
+
consecutive_timeouts += 1
|
95
|
+
continue
|
96
|
+
|
97
|
+
# If no reducing function worked, try growing functions
|
98
|
+
if not reduced_any:
|
99
|
+
for fx in functions:
|
100
|
+
print(f"[Thinking] Trying growing {fx.__name__} on current expression (timeout={timeout:.2f}s):")
|
101
|
+
printeq(current_eq)
|
102
|
+
future = executor.submit(fx, current_eq)
|
103
|
+
try:
|
104
|
+
new_expr = future.result(timeout=timeout)
|
105
|
+
new_expr_str = str(new_expr)
|
106
|
+
if new_expr_str != expr_str:
|
107
|
+
stack.append((new_expr, path + [copy.deepcopy(new_expr)]))
|
108
|
+
consecutive_timeouts = 0
|
109
|
+
break # only take one growing function
|
110
|
+
except TimeoutError:
|
111
|
+
print(f"[Thinking] {fx.__name__} (growing) timed out, skipping.")
|
112
|
+
consecutive_timeouts += 1
|
113
|
+
continue
|
114
|
+
|
115
|
+
executor.shutdown(wait=True)
|
116
|
+
|
117
|
+
return found_boolean, boolean_path, smallest_four
|
mathai-0.3.3/mathai/search.py
DELETED
@@ -1,108 +0,0 @@
|
|
1
|
-
from mathai import *
|
2
|
-
import copy
|
3
|
-
from concurrent.futures import ThreadPoolExecutor, TimeoutError
|
4
|
-
|
5
|
-
# Original expression
|
6
|
-
original_eq = simplify(parse("((P|~Q)&(P&~Q|P&R)&(~P&~R|~Q))<->P&~Q"))
|
7
|
-
|
8
|
-
# Simplification functions
|
9
|
-
lst = [logic1, logic3, logic2]
|
10
|
-
|
11
|
-
MAX_SMALL = 2
|
12
|
-
smallest_four = []
|
13
|
-
|
14
|
-
# Stack element: (current_expr, path_list)
|
15
|
-
stack = [(copy.deepcopy(original_eq), [copy.deepcopy(original_eq)])]
|
16
|
-
|
17
|
-
# Keep track of visited expressions to prevent cycles
|
18
|
-
visited = set()
|
19
|
-
|
20
|
-
# Boolean constants for immediate termination
|
21
|
-
TRUE_EXPR = tree_form("s_true")
|
22
|
-
FALSE_EXPR = tree_form("s_false")
|
23
|
-
|
24
|
-
found_boolean = False
|
25
|
-
boolean_path = None
|
26
|
-
boolean_expr = None
|
27
|
-
|
28
|
-
# Thread pool executor
|
29
|
-
executor = ThreadPoolExecutor(max_workers=3)
|
30
|
-
|
31
|
-
while stack and not found_boolean:
|
32
|
-
current_eq, path = stack.pop()
|
33
|
-
expr_str = str(current_eq)
|
34
|
-
|
35
|
-
if expr_str in visited:
|
36
|
-
continue
|
37
|
-
visited.add(expr_str)
|
38
|
-
|
39
|
-
# Thinking message
|
40
|
-
printeq(current_eq)
|
41
|
-
|
42
|
-
# Immediate termination for boolean constants
|
43
|
-
if current_eq == TRUE_EXPR or current_eq == FALSE_EXPR:
|
44
|
-
found_boolean = True
|
45
|
-
boolean_path = path
|
46
|
-
boolean_expr = current_eq
|
47
|
-
break
|
48
|
-
|
49
|
-
# Insert into smallest_four if qualifies
|
50
|
-
inserted = False
|
51
|
-
for j in range(len(smallest_four)):
|
52
|
-
if len(expr_str) < len(str(smallest_four[j][0])):
|
53
|
-
smallest_four.insert(j, (copy.deepcopy(current_eq), copy.deepcopy(path)))
|
54
|
-
inserted = True
|
55
|
-
break
|
56
|
-
if not inserted and len(smallest_four) < MAX_SMALL:
|
57
|
-
smallest_four.append((copy.deepcopy(current_eq), copy.deepcopy(path)))
|
58
|
-
if len(smallest_four) > MAX_SMALL:
|
59
|
-
smallest_four = smallest_four[:MAX_SMALL]
|
60
|
-
|
61
|
-
# First, try functions that reduce length
|
62
|
-
reduced_any = False
|
63
|
-
for fx in lst:
|
64
|
-
print(f"[Thinking] Executing {fx.__name__} on current expression:")
|
65
|
-
printeq(current_eq)
|
66
|
-
future = executor.submit(fx, current_eq)
|
67
|
-
try:
|
68
|
-
new_expr = future.result(timeout=5)
|
69
|
-
new_expr_str = str(new_expr)
|
70
|
-
# Only accept if shorter or equal
|
71
|
-
if len(new_expr_str) <= len(expr_str) and new_expr_str != expr_str:
|
72
|
-
reduced_any = True
|
73
|
-
stack.append((new_expr, path + [copy.deepcopy(new_expr)]))
|
74
|
-
except TimeoutError:
|
75
|
-
print(f"[Thinking] {fx.__name__} timed out, skipping.")
|
76
|
-
continue
|
77
|
-
|
78
|
-
# If no reducing function produced a shorter or equal expression, try a “growing” function
|
79
|
-
if not reduced_any:
|
80
|
-
for fx in lst:
|
81
|
-
print(f"[Thinking] Trying growing {fx.__name__} on current expression:")
|
82
|
-
printeq(current_eq)
|
83
|
-
future = executor.submit(fx, current_eq)
|
84
|
-
try:
|
85
|
-
new_expr = future.result(timeout=5)
|
86
|
-
new_expr_str = str(new_expr)
|
87
|
-
if new_expr_str != expr_str:
|
88
|
-
stack.append((new_expr, path + [copy.deepcopy(new_expr)]))
|
89
|
-
break # only take one growing function
|
90
|
-
except TimeoutError:
|
91
|
-
print(f"[Thinking] {fx.__name__} (growing) timed out, skipping.")
|
92
|
-
continue
|
93
|
-
|
94
|
-
# Shutdown executor
|
95
|
-
executor.shutdown(wait=True)
|
96
|
-
|
97
|
-
# Display final results
|
98
|
-
if found_boolean:
|
99
|
-
print("\nBoolean constant found! Full path to solution:\n")
|
100
|
-
for step in boolean_path:
|
101
|
-
printeq(step)
|
102
|
-
else:
|
103
|
-
print("\nDFS completed. Two smallest expressions with steps:\n")
|
104
|
-
for expr, path in smallest_four:
|
105
|
-
print("Path to final expression:")
|
106
|
-
for step in path:
|
107
|
-
printeq(step)
|
108
|
-
print("-"*50)
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|