mathai 0.2.7__py3-none-any.whl → 0.2.9__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 +16 -16
- mathai/apart.py +103 -103
- mathai/base.py +355 -354
- mathai/console.py +84 -84
- mathai/diff.py +65 -65
- mathai/expand.py +58 -58
- mathai/factor.py +125 -125
- mathai/fraction.py +59 -59
- mathai/integrate.py +346 -346
- mathai/inverse.py +65 -65
- mathai/limit.py +130 -130
- mathai/linear.py +152 -152
- mathai/logic.py +224 -224
- mathai/parser.py +154 -154
- mathai/printeq.py +34 -34
- mathai/simplify.py +358 -358
- mathai/structure.py +103 -103
- mathai/tool.py +35 -35
- mathai/trig.py +169 -169
- mathai/univariate_inequality.py +410 -414
- {mathai-0.2.7.dist-info → mathai-0.2.9.dist-info}/METADATA +231 -231
- mathai-0.2.9.dist-info/RECORD +24 -0
- {mathai-0.2.7.dist-info → mathai-0.2.9.dist-info}/WHEEL +1 -1
- mathai-0.2.7.dist-info/RECORD +0 -24
- {mathai-0.2.7.dist-info → mathai-0.2.9.dist-info}/top_level.txt +0 -0
mathai/inverse.py
CHANGED
@@ -1,65 +1,65 @@
|
|
1
|
-
from .base import *
|
2
|
-
from .simplify import solve
|
3
|
-
from .expand import expand
|
4
|
-
def inverse(rhs,term, sign=None):
|
5
|
-
term = tree_form(term)
|
6
|
-
lhs = tree_form("d_0")
|
7
|
-
count = 15
|
8
|
-
|
9
|
-
while not rhs==term:
|
10
|
-
if rhs.name == "f_add":
|
11
|
-
if all(term in factor_generation(child) for child in rhs.children):
|
12
|
-
newrhs = solve(expand(rhs*term**-1))
|
13
|
-
if not contain(newrhs, term):
|
14
|
-
rhs = term * newrhs
|
15
|
-
else:
|
16
|
-
for i in range(len(rhs.children)-1,-1,-1):
|
17
|
-
if not contain(rhs.children[i], term):
|
18
|
-
lhs = lhs - rhs.children[i]
|
19
|
-
rhs.children.pop(i)
|
20
|
-
elif rhs.name == "f_mul":
|
21
|
-
for i in range(len(rhs.children)-1,-1,-1):
|
22
|
-
if not contain(rhs.children[i], term):
|
23
|
-
lhs = lhs * rhs.children[i]**-1
|
24
|
-
if sign is not None:
|
25
|
-
if "v_" in str_form(rhs.children[i]):
|
26
|
-
return None
|
27
|
-
if compute(rhs.children[i]**-1) < 0:
|
28
|
-
sign = not sign
|
29
|
-
|
30
|
-
rhs.children.pop(i)
|
31
|
-
elif rhs.name == "f_pow" and contain(rhs.children[0], term):
|
32
|
-
lhs = lhs ** (tree_form("d_1")/rhs.children[1])
|
33
|
-
rhs = copy.deepcopy(rhs.children[0])
|
34
|
-
elif rhs.name == "f_sin" and contain(rhs.children[0], term):
|
35
|
-
lhs = lhs.fx("arcsin")
|
36
|
-
rhs = copy.deepcopy(rhs.children[0])
|
37
|
-
elif rhs.name == "f_arcsin" and contain(rhs.children[0], term):
|
38
|
-
lhs = lhs.fx("sin")
|
39
|
-
rhs = copy.deepcopy(rhs.children[0])
|
40
|
-
elif rhs.name == "f_arccos" and contain(rhs.children[0], term):
|
41
|
-
lhs = lhs.fx("cos")
|
42
|
-
rhs = copy.deepcopy(rhs.children[0])
|
43
|
-
elif rhs.name == "f_cos" and contain(rhs.children[0], term):
|
44
|
-
lhs = lhs.fx("arccos")
|
45
|
-
rhs = copy.deepcopy(rhs.children[0])
|
46
|
-
elif rhs.name == "f_log" and contain(rhs.children[0], term):
|
47
|
-
lhs = tree_form("s_e")**lhs
|
48
|
-
rhs = copy.deepcopy(rhs.children[0])
|
49
|
-
elif rhs.name == "f_pow" and rhs.children[0].name == "s_e" and contain(rhs.children[1], term):
|
50
|
-
lhs = lhs.fx("log")
|
51
|
-
rhs = copy.deepcopy(rhs.children[1].fx("log"))
|
52
|
-
elif rhs.name == "f_tan" and contain(rhs.children[0], term):
|
53
|
-
lhs = lhs.fx("arctan")
|
54
|
-
rhs = copy.deepcopy(rhs.children[0])
|
55
|
-
elif rhs.name == "f_arctan" and contain(rhs.children[0], term):
|
56
|
-
lhs = lhs.fx("tan")
|
57
|
-
rhs = copy.deepcopy(rhs.children[0])
|
58
|
-
if len(rhs.children) == 1:
|
59
|
-
rhs = rhs.children[0]
|
60
|
-
count -= 1
|
61
|
-
if count == 0:
|
62
|
-
return None
|
63
|
-
if sign is None:
|
64
|
-
return solve(lhs)
|
65
|
-
return solve(lhs), sign
|
1
|
+
from .base import *
|
2
|
+
from .simplify import solve
|
3
|
+
from .expand import expand
|
4
|
+
def inverse(rhs,term, sign=None):
|
5
|
+
term = tree_form(term)
|
6
|
+
lhs = tree_form("d_0")
|
7
|
+
count = 15
|
8
|
+
|
9
|
+
while not rhs==term:
|
10
|
+
if rhs.name == "f_add":
|
11
|
+
if all(term in factor_generation(child) for child in rhs.children):
|
12
|
+
newrhs = solve(expand(rhs*term**-1))
|
13
|
+
if not contain(newrhs, term):
|
14
|
+
rhs = term * newrhs
|
15
|
+
else:
|
16
|
+
for i in range(len(rhs.children)-1,-1,-1):
|
17
|
+
if not contain(rhs.children[i], term):
|
18
|
+
lhs = lhs - rhs.children[i]
|
19
|
+
rhs.children.pop(i)
|
20
|
+
elif rhs.name == "f_mul":
|
21
|
+
for i in range(len(rhs.children)-1,-1,-1):
|
22
|
+
if not contain(rhs.children[i], term):
|
23
|
+
lhs = lhs * rhs.children[i]**-1
|
24
|
+
if sign is not None:
|
25
|
+
if "v_" in str_form(rhs.children[i]):
|
26
|
+
return None
|
27
|
+
if compute(rhs.children[i]**-1) < 0:
|
28
|
+
sign = not sign
|
29
|
+
|
30
|
+
rhs.children.pop(i)
|
31
|
+
elif rhs.name == "f_pow" and contain(rhs.children[0], term):
|
32
|
+
lhs = lhs ** (tree_form("d_1")/rhs.children[1])
|
33
|
+
rhs = copy.deepcopy(rhs.children[0])
|
34
|
+
elif rhs.name == "f_sin" and contain(rhs.children[0], term):
|
35
|
+
lhs = lhs.fx("arcsin")
|
36
|
+
rhs = copy.deepcopy(rhs.children[0])
|
37
|
+
elif rhs.name == "f_arcsin" and contain(rhs.children[0], term):
|
38
|
+
lhs = lhs.fx("sin")
|
39
|
+
rhs = copy.deepcopy(rhs.children[0])
|
40
|
+
elif rhs.name == "f_arccos" and contain(rhs.children[0], term):
|
41
|
+
lhs = lhs.fx("cos")
|
42
|
+
rhs = copy.deepcopy(rhs.children[0])
|
43
|
+
elif rhs.name == "f_cos" and contain(rhs.children[0], term):
|
44
|
+
lhs = lhs.fx("arccos")
|
45
|
+
rhs = copy.deepcopy(rhs.children[0])
|
46
|
+
elif rhs.name == "f_log" and contain(rhs.children[0], term):
|
47
|
+
lhs = tree_form("s_e")**lhs
|
48
|
+
rhs = copy.deepcopy(rhs.children[0])
|
49
|
+
elif rhs.name == "f_pow" and rhs.children[0].name == "s_e" and contain(rhs.children[1], term):
|
50
|
+
lhs = lhs.fx("log")
|
51
|
+
rhs = copy.deepcopy(rhs.children[1].fx("log"))
|
52
|
+
elif rhs.name == "f_tan" and contain(rhs.children[0], term):
|
53
|
+
lhs = lhs.fx("arctan")
|
54
|
+
rhs = copy.deepcopy(rhs.children[0])
|
55
|
+
elif rhs.name == "f_arctan" and contain(rhs.children[0], term):
|
56
|
+
lhs = lhs.fx("tan")
|
57
|
+
rhs = copy.deepcopy(rhs.children[0])
|
58
|
+
if len(rhs.children) == 1:
|
59
|
+
rhs = rhs.children[0]
|
60
|
+
count -= 1
|
61
|
+
if count == 0:
|
62
|
+
return None
|
63
|
+
if sign is None:
|
64
|
+
return solve(lhs)
|
65
|
+
return solve(lhs), sign
|
mathai/limit.py
CHANGED
@@ -1,130 +1,130 @@
|
|
1
|
-
from .structure import structure
|
2
|
-
from .base import *
|
3
|
-
from .parser import parse
|
4
|
-
from .simplify import simplify, solve
|
5
|
-
from .expand import expand
|
6
|
-
from .diff import diff
|
7
|
-
from .trig import trig0
|
8
|
-
from .fraction import fraction
|
9
|
-
from .printeq import printeq_str
|
10
|
-
tab=0
|
11
|
-
def substitute_val(eq, val, var="v_0"):
|
12
|
-
eq = replace(eq, tree_form(var), tree_form("d_"+str(val)))
|
13
|
-
return eq
|
14
|
-
|
15
|
-
def subslimit(equation, var):
|
16
|
-
equation2 = trig0(replace(equation, var, tree_form("d_0")))
|
17
|
-
try:
|
18
|
-
tmp = simplify(equation2)
|
19
|
-
return simplify(expand(tmp))
|
20
|
-
except:
|
21
|
-
return None
|
22
|
-
|
23
|
-
def check(num, den, var):
|
24
|
-
n, d = None, None
|
25
|
-
|
26
|
-
n, d = (dowhile(replace(e, tree_form(var), tree_form("d_0")), lambda x: trig0(simplify(x))) for e in (num, den))
|
27
|
-
|
28
|
-
if n is None or d is None:
|
29
|
-
return False
|
30
|
-
if n == 0 and d == 0: return True
|
31
|
-
if d != 0:
|
32
|
-
return simplify(n/d)
|
33
|
-
return False
|
34
|
-
def lhospital(num, den, steps,var):
|
35
|
-
logs = []
|
36
|
-
|
37
|
-
out = check(num, den, var)
|
38
|
-
if isinstance(out, TreeNode):
|
39
|
-
return out,[]
|
40
|
-
for _ in range(steps):
|
41
|
-
num2, den2 = map(lambda e: simplify(diff(e, var)), (num, den))
|
42
|
-
out = check(num2, den2, var)
|
43
|
-
if out is True:
|
44
|
-
num, den = num2, den2
|
45
|
-
logs += [(0,"lim x->0 "+printeq_str(simplify(num/den)))]
|
46
|
-
continue
|
47
|
-
if out is False:
|
48
|
-
eq2 = simplify(fraction(simplify(num/den)))
|
49
|
-
return eq2,logs
|
50
|
-
return out,logs
|
51
|
-
def lhospital2(eq, var):
|
52
|
-
eq= simplify(eq)
|
53
|
-
if eq is None:
|
54
|
-
return None
|
55
|
-
if not contain(eq, tree_form(var)):
|
56
|
-
return eq,[]
|
57
|
-
num, dem = [simplify(item) for item in num_dem(eq)]
|
58
|
-
if num is None or dem is None:
|
59
|
-
return eq,[]
|
60
|
-
|
61
|
-
return lhospital(num, dem, 10,var)
|
62
|
-
ls = [parse("sin(A)"), parse("A^B-1"),parse("log(1+A)"), parse("cos(A)")]
|
63
|
-
ls= [simplify(item) for item in ls]
|
64
|
-
|
65
|
-
def approx(eq, var):
|
66
|
-
n, d= num_dem(eq)
|
67
|
-
n, d = solve(n), solve(d)
|
68
|
-
n, d = expand(n), expand(d)
|
69
|
-
out = []
|
70
|
-
for equation in [n, d]:
|
71
|
-
for item in factor_generation(equation):
|
72
|
-
tmp = structure(item, ls[0])
|
73
|
-
if tmp is not None and contain(tmp["v_-1"], var):
|
74
|
-
item2 = substitute_val(tmp["v_-1"], 0, var.name)
|
75
|
-
if tree_form("d_0") == expand(simplify(item2)):
|
76
|
-
equation = equation/item
|
77
|
-
equation = equation*tmp["v_-1"]
|
78
|
-
break
|
79
|
-
elif tree_form("d_0") == expand(simplify(tree_form("s_pi") - item2)):
|
80
|
-
equation = equation/item
|
81
|
-
equation = equation*(tree_form("s_pi") - tmp["v_-1"])
|
82
|
-
break
|
83
|
-
tmp = structure(item, ls[1])
|
84
|
-
if tmp is not None and contain(tmp["v_-1"], var) and not contain(tmp["v_-2"], var):
|
85
|
-
item2 = substitute_val(tmp["v_-1"], 0, var.name)
|
86
|
-
item2 = expand(solve(item2))
|
87
|
-
if tree_form("d_0") == item2:
|
88
|
-
equation = equation/item
|
89
|
-
equation = solve(equation*tmp["v_-1"]*tmp["v_-2"].fx("log"))
|
90
|
-
break
|
91
|
-
tmp = structure(item, ls[2])
|
92
|
-
if tmp is not None and contain(tmp["v_-1"], var):
|
93
|
-
|
94
|
-
item2 = substitute_val(tmp["v_-1"], 0, var.name)
|
95
|
-
item2 = expand(solve(item2))
|
96
|
-
if tree_form("d_0") == item2:
|
97
|
-
equation = equation/item
|
98
|
-
equation = solve(equation*tmp["v_-1"])
|
99
|
-
break
|
100
|
-
tmp = structure(item, ls[3])
|
101
|
-
if tmp is not None and contain(tmp["v_-1"], var):
|
102
|
-
item2 = substitute_val(item, 0, var.name)
|
103
|
-
|
104
|
-
if tree_form("d_0") == expand(solve(item2)):
|
105
|
-
|
106
|
-
equation = equation/item
|
107
|
-
equation = equation*(tree_form("d_1") - tmp["v_-1"]**tree_form("d_2"))
|
108
|
-
break
|
109
|
-
|
110
|
-
equation = solve(equation)
|
111
|
-
out.append(equation)
|
112
|
-
return simplify(out[0]/out[1])
|
113
|
-
def approx_limit(equation, var):
|
114
|
-
return dowhile(equation, lambda x: approx(x, var))
|
115
|
-
|
116
|
-
def limit(equation, var="v_0"):
|
117
|
-
logs = [(0,"lim x->0 "+printeq_str(simplify(equation)))]
|
118
|
-
eq2 = dowhile(replace(equation, tree_form(var), tree_form("d_0")), lambda x: trig0(simplify(x)))
|
119
|
-
if eq2 is not None and not contain(equation, tree_form(var)):
|
120
|
-
return eq2,logs
|
121
|
-
|
122
|
-
equation, tmp = lhospital2(equation, var)
|
123
|
-
equation = simplify(expand(simplify(equation)))
|
124
|
-
if not contain(equation, tree_form(var)):
|
125
|
-
return equation,logs+tmp
|
126
|
-
'''
|
127
|
-
if equation.name == "f_add":
|
128
|
-
return simplify(summation([limit(child, var) for child in equation.children]))
|
129
|
-
'''
|
130
|
-
return equation,logs+tmp
|
1
|
+
from .structure import structure
|
2
|
+
from .base import *
|
3
|
+
from .parser import parse
|
4
|
+
from .simplify import simplify, solve
|
5
|
+
from .expand import expand
|
6
|
+
from .diff import diff
|
7
|
+
from .trig import trig0
|
8
|
+
from .fraction import fraction
|
9
|
+
from .printeq import printeq_str
|
10
|
+
tab=0
|
11
|
+
def substitute_val(eq, val, var="v_0"):
|
12
|
+
eq = replace(eq, tree_form(var), tree_form("d_"+str(val)))
|
13
|
+
return eq
|
14
|
+
|
15
|
+
def subslimit(equation, var):
|
16
|
+
equation2 = trig0(replace(equation, var, tree_form("d_0")))
|
17
|
+
try:
|
18
|
+
tmp = simplify(equation2)
|
19
|
+
return simplify(expand(tmp))
|
20
|
+
except:
|
21
|
+
return None
|
22
|
+
|
23
|
+
def check(num, den, var):
|
24
|
+
n, d = None, None
|
25
|
+
|
26
|
+
n, d = (dowhile(replace(e, tree_form(var), tree_form("d_0")), lambda x: trig0(simplify(x))) for e in (num, den))
|
27
|
+
|
28
|
+
if n is None or d is None:
|
29
|
+
return False
|
30
|
+
if n == 0 and d == 0: return True
|
31
|
+
if d != 0:
|
32
|
+
return simplify(n/d)
|
33
|
+
return False
|
34
|
+
def lhospital(num, den, steps,var):
|
35
|
+
logs = []
|
36
|
+
|
37
|
+
out = check(num, den, var)
|
38
|
+
if isinstance(out, TreeNode):
|
39
|
+
return out,[]
|
40
|
+
for _ in range(steps):
|
41
|
+
num2, den2 = map(lambda e: simplify(diff(e, var)), (num, den))
|
42
|
+
out = check(num2, den2, var)
|
43
|
+
if out is True:
|
44
|
+
num, den = num2, den2
|
45
|
+
logs += [(0,"lim x->0 "+printeq_str(simplify(num/den)))]
|
46
|
+
continue
|
47
|
+
if out is False:
|
48
|
+
eq2 = simplify(fraction(simplify(num/den)))
|
49
|
+
return eq2,logs
|
50
|
+
return out,logs
|
51
|
+
def lhospital2(eq, var):
|
52
|
+
eq= simplify(eq)
|
53
|
+
if eq is None:
|
54
|
+
return None
|
55
|
+
if not contain(eq, tree_form(var)):
|
56
|
+
return eq,[]
|
57
|
+
num, dem = [simplify(item) for item in num_dem(eq)]
|
58
|
+
if num is None or dem is None:
|
59
|
+
return eq,[]
|
60
|
+
|
61
|
+
return lhospital(num, dem, 10,var)
|
62
|
+
ls = [parse("sin(A)"), parse("A^B-1"),parse("log(1+A)"), parse("cos(A)")]
|
63
|
+
ls= [simplify(item) for item in ls]
|
64
|
+
|
65
|
+
def approx(eq, var):
|
66
|
+
n, d= num_dem(eq)
|
67
|
+
n, d = solve(n), solve(d)
|
68
|
+
n, d = expand(n), expand(d)
|
69
|
+
out = []
|
70
|
+
for equation in [n, d]:
|
71
|
+
for item in factor_generation(equation):
|
72
|
+
tmp = structure(item, ls[0])
|
73
|
+
if tmp is not None and contain(tmp["v_-1"], var):
|
74
|
+
item2 = substitute_val(tmp["v_-1"], 0, var.name)
|
75
|
+
if tree_form("d_0") == expand(simplify(item2)):
|
76
|
+
equation = equation/item
|
77
|
+
equation = equation*tmp["v_-1"]
|
78
|
+
break
|
79
|
+
elif tree_form("d_0") == expand(simplify(tree_form("s_pi") - item2)):
|
80
|
+
equation = equation/item
|
81
|
+
equation = equation*(tree_form("s_pi") - tmp["v_-1"])
|
82
|
+
break
|
83
|
+
tmp = structure(item, ls[1])
|
84
|
+
if tmp is not None and contain(tmp["v_-1"], var) and not contain(tmp["v_-2"], var):
|
85
|
+
item2 = substitute_val(tmp["v_-1"], 0, var.name)
|
86
|
+
item2 = expand(solve(item2))
|
87
|
+
if tree_form("d_0") == item2:
|
88
|
+
equation = equation/item
|
89
|
+
equation = solve(equation*tmp["v_-1"]*tmp["v_-2"].fx("log"))
|
90
|
+
break
|
91
|
+
tmp = structure(item, ls[2])
|
92
|
+
if tmp is not None and contain(tmp["v_-1"], var):
|
93
|
+
|
94
|
+
item2 = substitute_val(tmp["v_-1"], 0, var.name)
|
95
|
+
item2 = expand(solve(item2))
|
96
|
+
if tree_form("d_0") == item2:
|
97
|
+
equation = equation/item
|
98
|
+
equation = solve(equation*tmp["v_-1"])
|
99
|
+
break
|
100
|
+
tmp = structure(item, ls[3])
|
101
|
+
if tmp is not None and contain(tmp["v_-1"], var):
|
102
|
+
item2 = substitute_val(item, 0, var.name)
|
103
|
+
|
104
|
+
if tree_form("d_0") == expand(solve(item2)):
|
105
|
+
|
106
|
+
equation = equation/item
|
107
|
+
equation = equation*(tree_form("d_1") - tmp["v_-1"]**tree_form("d_2"))
|
108
|
+
break
|
109
|
+
|
110
|
+
equation = solve(equation)
|
111
|
+
out.append(equation)
|
112
|
+
return simplify(out[0]/out[1])
|
113
|
+
def approx_limit(equation, var):
|
114
|
+
return dowhile(equation, lambda x: approx(x, var))
|
115
|
+
|
116
|
+
def limit(equation, var="v_0"):
|
117
|
+
logs = [(0,"lim x->0 "+printeq_str(simplify(equation)))]
|
118
|
+
eq2 = dowhile(replace(equation, tree_form(var), tree_form("d_0")), lambda x: trig0(simplify(x)))
|
119
|
+
if eq2 is not None and not contain(equation, tree_form(var)):
|
120
|
+
return eq2,logs
|
121
|
+
|
122
|
+
equation, tmp = lhospital2(equation, var)
|
123
|
+
equation = simplify(expand(simplify(equation)))
|
124
|
+
if not contain(equation, tree_form(var)):
|
125
|
+
return equation,logs+tmp
|
126
|
+
'''
|
127
|
+
if equation.name == "f_add":
|
128
|
+
return simplify(summation([limit(child, var) for child in equation.children]))
|
129
|
+
'''
|
130
|
+
return equation,logs+tmp
|