mathai 0.1.4__py3-none-any.whl → 0.1.5__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 +3 -3
- mathai/apart.py +1 -1
- mathai/console.py +6 -2
- mathai/factor.py +11 -6
- mathai/integrate.py +75 -20
- mathai/simplify.py +13 -2
- {mathai-0.1.4.dist-info → mathai-0.1.5.dist-info}/METADATA +1 -1
- {mathai-0.1.4.dist-info → mathai-0.1.5.dist-info}/RECORD +10 -10
- {mathai-0.1.4.dist-info → mathai-0.1.5.dist-info}/WHEEL +0 -0
- {mathai-0.1.4.dist-info → mathai-0.1.5.dist-info}/top_level.txt +0 -0
mathai/__init__.py
CHANGED
@@ -1,10 +1,10 @@
|
|
1
1
|
from .expand import expand
|
2
2
|
from .parser import parse
|
3
|
-
from .printeq import printeq, printeq_log
|
3
|
+
from .printeq import printeq, printeq_log, printeq_str
|
4
4
|
from .simplify import solve, simplify
|
5
|
-
from .integrate import integrate,
|
5
|
+
from .integrate import integrate, sqint, byparts
|
6
6
|
from .diff import diff
|
7
|
-
from .factor import factor
|
7
|
+
from .factor import factor, factor2
|
8
8
|
from .fraction import fraction
|
9
9
|
from .inverse import inverse
|
10
10
|
from .trig import trig0, trig1, trig2, trig3, trig4
|
mathai/apart.py
CHANGED
mathai/console.py
CHANGED
@@ -3,7 +3,7 @@ from .expand import expand
|
|
3
3
|
from .parser import parse
|
4
4
|
from .printeq import printeq, printeq_log
|
5
5
|
from .simplify import solve, simplify
|
6
|
-
from .integrate import integrate, typesqint
|
6
|
+
from .integrate import integrate, typesqint, typebyparts, typeintegrate
|
7
7
|
from .diff import diff
|
8
8
|
from .base import *
|
9
9
|
from .factor import _factorconst, factor
|
@@ -58,9 +58,13 @@ def console():
|
|
58
58
|
eq = simplify(eq)
|
59
59
|
elif command == "fraction":
|
60
60
|
eq = fraction(eq)
|
61
|
-
elif command.split(" ")[0] in ["integrate", "sqint"]:
|
61
|
+
elif command.split(" ")[0] in ["integrate", "sqint", "byparts"]:
|
62
62
|
if command.split(" ")[0] == "sqint":
|
63
63
|
typesqint()
|
64
|
+
elif command.split(" ")[0] == "byparts":
|
65
|
+
typebyparts()
|
66
|
+
elif command.split(" ")[0] == "integrate":
|
67
|
+
typeintegrate()
|
64
68
|
out = integrate(eq, parse(command.split(" ")[1]).name)
|
65
69
|
if out is None:
|
66
70
|
print("failed to integrate")
|
mathai/factor.py
CHANGED
@@ -79,8 +79,9 @@ def factor_quad_formula_init():
|
|
79
79
|
return [formula_list, var, expr]
|
80
80
|
|
81
81
|
def factor_cube_formula_init():
|
82
|
-
var = "
|
83
|
-
formula_list = [(f"D^3+E", f"(D+E^(1/3))*(D^2-D*E^(1/3)+E^(2/3))"), (f"D^3-E", f"(D-E^(1/3))*(D^2+D*E^(1/3)+E^(2/3))")
|
82
|
+
var = ""
|
83
|
+
formula_list = [(f"D^3+E", f"(D+E^(1/3))*(D^2-D*E^(1/3)+E^(2/3))"), (f"D^3-E", f"(D-E^(1/3))*(D^2+D*E^(1/3)+E^(2/3))"),\
|
84
|
+
(f"-D^3+E", f"(-D+E^(1/3))*(D^2+D*E^(1/3)+E^(2/3))")]
|
84
85
|
formula_list = [[simplify(parse(y)) for y in x] for x in formula_list]
|
85
86
|
expr = [[parse("A")], [parse("B")]]
|
86
87
|
return [formula_list, var, expr]
|
@@ -101,8 +102,11 @@ def factor_helper(equation, complexnum, power=2):
|
|
101
102
|
nonlocal maxnum
|
102
103
|
if eq.name == "f_pow" and eq.children[1].name[:2] == "d_":
|
103
104
|
n = int(eq.children[1].name[2:])
|
105
|
+
sgn = round(abs(n)/n)
|
106
|
+
n = abs(n)
|
104
107
|
if n>power and n % power == 0 and maxnum==n:
|
105
|
-
|
108
|
+
out= (eq.children[0]**tree_form("d_"+str(sgn*int(n/power))))**power
|
109
|
+
return out
|
106
110
|
return TreeNode(eq.name, [helper(child) for child in eq.children])
|
107
111
|
high(equation)
|
108
112
|
out = None
|
@@ -114,7 +118,8 @@ def factor_helper(equation, complexnum, power=2):
|
|
114
118
|
out = simplify(solve(out))
|
115
119
|
if out is not None and (complexnum or (not complexnum and not contain(out, tree_form("s_i")))):
|
116
120
|
return out
|
117
|
-
|
118
|
-
return TreeNode(equation.name, [factor_helper(child, complexnum, power) for child in equation.children])
|
121
|
+
return TreeNode(equation.name, [factor_helper(child, complexnum, power) for child in equation.children])
|
119
122
|
def factor(equation, complexnum=False):
|
120
|
-
return solve(take_common2(
|
123
|
+
return solve(take_common2(simplify(factor_helper(simplify(equation), complexnum, 2))))
|
124
|
+
def factor2(equation, complexnum=False):
|
125
|
+
return solve(factor_helper(simplify(factor_helper(simplify(equation), complexnum, 2)), complexnum, 3))
|
mathai/integrate.py
CHANGED
@@ -9,8 +9,11 @@ from .printeq import printeq_str
|
|
9
9
|
from .structure import transform_formula
|
10
10
|
from .inverse import inverse
|
11
11
|
from .tool import poly
|
12
|
+
from fractions import Fraction
|
13
|
+
from .printeq import printeq
|
12
14
|
def integrate_summation(equation, wrt, tab, inf):
|
13
15
|
logs= []
|
16
|
+
orig = copy.deepcopy(equation)
|
14
17
|
for i in range(2):
|
15
18
|
|
16
19
|
if equation.name == "f_add":
|
@@ -25,17 +28,23 @@ def integrate_summation(equation, wrt, tab, inf):
|
|
25
28
|
return summation(answer), logs
|
26
29
|
if i == 0:
|
27
30
|
|
28
|
-
tmp = expand(equation)
|
31
|
+
tmp = expand(simplify(fraction(simplify(equation))))
|
32
|
+
|
29
33
|
logs += [(tab, f"integrating {printeq_str(simplify(equation))} will be the same thing as integrating {printeq_str(simplify(tmp))}")]
|
34
|
+
|
30
35
|
equation = tmp
|
36
|
+
if equation.name != "f_add" and orig != equation:
|
37
|
+
out = integrate(equation, wrt, tab+1, inf)
|
38
|
+
if out is None:
|
39
|
+
return None
|
40
|
+
return out[0], logs+out[1]
|
31
41
|
return None
|
32
42
|
|
33
43
|
def subs_heuristic(eq, var):
|
34
44
|
output = []
|
35
45
|
def collect2(eq):
|
36
|
-
if eq.name == "f_pow" and eq.children[
|
37
|
-
|
38
|
-
output.append(str_form( tree_form(var)**tree_form("d_3") ))
|
46
|
+
if eq.name == "f_pow" and frac(eq.children[1]) is not None and abs(frac(eq.children[1])) == Fraction(1,2):
|
47
|
+
output.append(str_form(eq.children[0].fx("sqrt")))
|
39
48
|
if eq.name in ["f_pow", "f_sin", "f_cos", "f_arcsin"] and eq.children[0].name[:2] != "v_" and var in str_form(eq.children[0]):
|
40
49
|
output.append(str_form(eq.children[0]))
|
41
50
|
if eq.name == "f_pow" and eq.children[0].name == "s_e" and "v_" in str_form(eq):
|
@@ -54,10 +63,12 @@ def subs_heuristic(eq, var):
|
|
54
63
|
if output == []:
|
55
64
|
collect3(eq)
|
56
65
|
tmp = sorted(output, key=lambda x: len(x))
|
57
|
-
tmp = [
|
66
|
+
tmp = [simplify(tree_form(x)) for x in tmp]
|
67
|
+
|
58
68
|
return tmp
|
59
69
|
|
60
70
|
def integrate_subs(equation, term, v1, v2, tab, inf):
|
71
|
+
|
61
72
|
origv2 = copy.deepcopy(v2)
|
62
73
|
equation = solve(equation)
|
63
74
|
eq = equation
|
@@ -83,6 +94,7 @@ def integrate_subs(equation, term, v1, v2, tab, inf):
|
|
83
94
|
continue
|
84
95
|
|
85
96
|
eq = expand(simplify(eq))
|
97
|
+
|
86
98
|
out = integrate(eq, origv2, tab+1, inf)
|
87
99
|
|
88
100
|
if out is None:
|
@@ -103,7 +115,7 @@ def integrate_subs_main(equation, wrt, tab, inf):
|
|
103
115
|
if tmp3 is not None:
|
104
116
|
return tmp3[0], tmp3[1]
|
105
117
|
return None
|
106
|
-
def sqint(equation, var, depth, inf):
|
118
|
+
def sqint(equation, var="v_0", depth=0, inf=0):
|
107
119
|
|
108
120
|
logs = []
|
109
121
|
def sgn(eq):
|
@@ -148,6 +160,7 @@ def sqint(equation, var, depth, inf):
|
|
148
160
|
t1, t2 = sgn(const)
|
149
161
|
la = s2**root
|
150
162
|
lb = b*s2**root/(two*a)
|
163
|
+
|
151
164
|
if mode:
|
152
165
|
if s1 == one:
|
153
166
|
if t1 == one:
|
@@ -162,9 +175,10 @@ def sqint(equation, var, depth, inf):
|
|
162
175
|
return -k*((la*x+lb)/t2**root).fx("arctan")/(la * t2**root), logs
|
163
176
|
if s1 == one:
|
164
177
|
if t1 == one:
|
165
|
-
return k*(la*x + lb + ((la*x + lb)**two + t2)**root).fx("abs").fx("log")/la, logs
|
178
|
+
return simplify(k*(la*x + lb + ((la*x + lb)**two + t2)**root).fx("abs").fx("log")/la), logs
|
166
179
|
else:
|
167
|
-
return k*(la*x + lb + ((la*x + lb)**two - t2)**root).fx("abs").fx("log")/la, logs
|
180
|
+
return simplify(k*(la*x + lb + ((la*x + lb)**two - t2)**root).fx("abs").fx("log")/la), logs
|
181
|
+
|
168
182
|
else:
|
169
183
|
if t1 == one:
|
170
184
|
return k*((la*x + lb)/t2**root).fx("arcsin")/la, logs
|
@@ -191,7 +205,6 @@ def sqint(equation, var, depth, inf):
|
|
191
205
|
else:
|
192
206
|
log2 = tmp[1]
|
193
207
|
tmp = tmp[0]
|
194
|
-
|
195
208
|
return A*two*t**root + tmp*B, logs+log2
|
196
209
|
else:
|
197
210
|
tmp = sqint(simplify(one/t), var, depth, inf)
|
@@ -206,28 +219,65 @@ def sqint(equation, var, depth, inf):
|
|
206
219
|
else:
|
207
220
|
log2 = tmp[1]
|
208
221
|
tmp = tmp[0]
|
209
|
-
|
210
222
|
return A*t.fx("abs").fx("log") + tmp*B, logs+log2
|
211
223
|
return None
|
224
|
+
typeint = "integrate"
|
225
|
+
def typeintegrate():
|
226
|
+
global typeint
|
227
|
+
typeint= "integrate"
|
228
|
+
def typesqint():
|
229
|
+
global typeint
|
230
|
+
typeint= "sqint"
|
231
|
+
def typebyparts():
|
232
|
+
global typeint
|
233
|
+
typeint= "byparts"
|
234
|
+
def byparts(eq, wrt="v_0", tab=0, inf=0):
|
235
|
+
|
236
|
+
lst = factor_generation(eq)
|
237
|
+
if len(lst) == 1:
|
238
|
+
lst += [tree_form("d_1")]
|
239
|
+
if len(lst) == 2:
|
240
|
+
for i in range(2):
|
241
|
+
|
242
|
+
f, g = copy.deepcopy([lst[i], lst[1-i]])
|
243
|
+
|
244
|
+
logs = [(tab, f"trying integration by parts, f = {printeq_str(simplify(f))} and g = {printeq_str(simplify(g))}")]
|
245
|
+
typeintegrate()
|
246
|
+
out1 = integrate(copy.deepcopy(g), wrt, tab+1, inf)
|
247
|
+
typebyparts()
|
248
|
+
|
249
|
+
if out1 is None:
|
250
|
+
continue
|
251
|
+
|
252
|
+
typeintegrate()
|
253
|
+
out2 = integrate(simplify(diff(copy.deepcopy(f), wrt)*out1[0]), wrt, tab+1, inf)
|
254
|
+
|
255
|
+
typebyparts()
|
256
|
+
if out2 is None:
|
257
|
+
continue
|
258
|
+
|
259
|
+
return copy.deepcopy([simplify(copy.deepcopy(f) * out1[0] - out2[0]), logs+out1[1]+out2[1]])
|
260
|
+
return None
|
212
261
|
def integration_formula_init():
|
213
262
|
var = "x"
|
214
263
|
formula_list = [(f"(A*{var}+B)^C", f"(A*{var}+B)^(C+1)/(A*(C+1))"),\
|
215
264
|
(f"sin(A*{var}+B)", f"-cos(A*{var}+B)/A"),\
|
216
265
|
(f"cos(A*{var}+B)", f"sin(A*{var}+B)/A"),\
|
217
|
-
(f"1/(A*{var}+B)", f"log(abs(A*{var}+B))/A")
|
266
|
+
(f"1/(A*{var}+B)", f"log(abs(A*{var}+B))/A"),\
|
267
|
+
(f"e^(A*{var}+B)", f"e^(A*{var}+B)/A"),\
|
268
|
+
(f"abs(A*{var}+B)", f"(A*{var}+B)*abs(A*{var}+B)/(2*A)")]
|
218
269
|
formula_list = [[simplify(parse(y)) for y in x] for x in formula_list]
|
219
270
|
expr = [[parse("A"), parse("1")], [parse("B"), parse("0")]]
|
220
271
|
return [formula_list, var, expr]
|
221
272
|
formula_gen = integration_formula_init()
|
222
|
-
|
223
|
-
|
224
|
-
global typeint
|
225
|
-
typeint= "sqint"
|
273
|
+
|
274
|
+
|
226
275
|
def integrate(equation, wrt="v_0", tab=0, inf=0):
|
227
276
|
global formula_list, var, expr
|
228
277
|
global typeint
|
229
278
|
|
230
279
|
equation = simplify(equation)
|
280
|
+
|
231
281
|
logs = []
|
232
282
|
if tab == 0:
|
233
283
|
logs += [(tab, f"the given question is to integrate {printeq_str(simplify(equation))} wrt to {str(tree_form(wrt))}")]
|
@@ -245,9 +295,10 @@ def integrate(equation, wrt="v_0", tab=0, inf=0):
|
|
245
295
|
if lst_const != []:
|
246
296
|
equation = product([item for item in lst if contain(item, tree_form(wrt))])
|
247
297
|
const = product(lst_const)
|
248
|
-
|
298
|
+
const = simplify(const)
|
299
|
+
if const != 1 and not contain(const, tree_form("s_i")):
|
249
300
|
|
250
|
-
equation =
|
301
|
+
equation = simplify(equation)
|
251
302
|
out = integrate(equation, wrt, tab+1, inf)
|
252
303
|
|
253
304
|
if out is None:
|
@@ -261,14 +312,18 @@ def integrate(equation, wrt="v_0", tab=0, inf=0):
|
|
261
312
|
if out is not None:
|
262
313
|
return out[0], logs+out[1]+[(tab, f"result is {printeq_str(simplify(out[0]))}")]
|
263
314
|
out = None
|
264
|
-
if typeint
|
315
|
+
if typeint in ["integrate", "sqint"]:
|
265
316
|
out = sqint(equation, wrt, tab+1, inf)
|
266
317
|
if out is not None:
|
267
318
|
return out[0], logs+out[1]+[(tab, f"result is {printeq_str(simplify(out[0]))}")]
|
268
|
-
|
319
|
+
if typeint == "integrate":
|
269
320
|
if inf==0:
|
270
321
|
out = integrate_subs_main(equation, wrt, tab, inf+1)
|
271
322
|
if out is not None:
|
272
323
|
return out[0], logs+out[1]+[(tab, f"result is {printeq_str(simplify(out[0]))}")]
|
273
|
-
|
324
|
+
if typeint == "byparts":
|
325
|
+
|
326
|
+
out = byparts(copy.deepcopy(equation), wrt, tab, inf)
|
327
|
+
if out is not None:
|
328
|
+
return out[0], logs+out[1]+[(tab, f"result is {printeq_str(simplify(out[0]))}")]
|
274
329
|
return None
|
mathai/simplify.py
CHANGED
@@ -244,6 +244,12 @@ def simplify(eq):
|
|
244
244
|
out = perfect_nth_root_value(n, r)
|
245
245
|
if out is not None:
|
246
246
|
return pp( tree_form("d_"+str(out)), f)
|
247
|
+
if eq.name == "f_mul" and len(eq.children)== 2:
|
248
|
+
for i in range(2):
|
249
|
+
if eq.children[i].name[:2] == "d_" and eq.children[1-i].name == "f_log":
|
250
|
+
return (eq.children[1-i].children[0]**eq.children[i]).fx("log")
|
251
|
+
if eq.name == "f_pow" and eq.children[0] == tree_form("s_e") and eq.children[1].name == "f_log":
|
252
|
+
return eq.children[1].children[0]
|
247
253
|
if eq.name == "f_pow" and eq.children[0] == tree_form("d_1"):
|
248
254
|
eq = tree_form("d_1")
|
249
255
|
if eq.name == "f_pow" and eq.children[0] == tree_form("d_0"):
|
@@ -297,7 +303,12 @@ def simplify(eq):
|
|
297
303
|
if eq.name == "f_pow" and eq.children[1].name[:2] == "d_" and abs(int(eq.children[1].name[2:]))%2==0 and eq.children[0].name == "f_abs":
|
298
304
|
return eq.children[0].children[0]**eq.children[1]
|
299
305
|
return TreeNode(eq.name, [helper5(child) for child in eq.children])
|
300
|
-
|
306
|
+
def helper8(eq):
|
307
|
+
if eq.name == "f_abs" and eq.children[0].name == "f_abs":
|
308
|
+
return eq.children[0]
|
309
|
+
if eq.name == "f_cos" and eq.children[0].name == "f_abs":
|
310
|
+
return eq.children[0].children[0].fx("cos")
|
311
|
+
return TreeNode(eq.name, [helper8(child) for child in eq.children])
|
301
312
|
def fx1(eq):
|
302
313
|
for item in [helper, helper3, helper4, helper6,solve,helper2,helper5]:
|
303
314
|
eq = dowhile(eq, item)
|
@@ -311,5 +322,5 @@ def simplify(eq):
|
|
311
322
|
eq = dowhile(eq, item)
|
312
323
|
return eq
|
313
324
|
eq = dowhile(eq, fx3)
|
314
|
-
|
325
|
+
eq = dowhile(eq, helper8)
|
315
326
|
return solve(eq)
|
@@ -1,22 +1,22 @@
|
|
1
|
-
mathai/__init__.py,sha256=
|
2
|
-
mathai/apart.py,sha256=
|
1
|
+
mathai/__init__.py,sha256=CIxRQncAAbHhI6YtrOI2HAiTyJX8reAoqnu7qfonVDc,504
|
2
|
+
mathai/apart.py,sha256=P0B21wHUFitc5RGbNJlwSeOdseOMcav4I7teFNcZmFA,3130
|
3
3
|
mathai/base.py,sha256=dnOCgCquNeSZY5tnd0aeXwOf1VxFrVMipdF4M92Bd-M,12223
|
4
|
-
mathai/console.py,sha256=
|
4
|
+
mathai/console.py,sha256=FsEMqDsgFeBSUiPCm9sfReZp56NSyoFVldqmVwvW4lg,3119
|
5
5
|
mathai/diff.py,sha256=Ia3ndCOa0c8qqcMEWBIrsXco942dcNfFLpu_T6TeEZA,2788
|
6
6
|
mathai/expand.py,sha256=zI2QDjjPSP5dqzgTVhuAMnBlohApL1MRjPZe0v-ccTw,1753
|
7
|
-
mathai/factor.py,sha256=
|
7
|
+
mathai/factor.py,sha256=1-Bxl3MX6P1mYM-CX0hRskCrTZWHFdaUL-V1OqX48hY,5316
|
8
8
|
mathai/fraction.py,sha256=KGldpi8mndp5cvp6qbN7ZdMZyVmK6popa3AwkWmHXl4,2573
|
9
|
-
mathai/integrate.py,sha256=
|
9
|
+
mathai/integrate.py,sha256=bt03xpe5dTGJiIIB--lvKZluLy1NAwBQNfTIDfjRxo4,11984
|
10
10
|
mathai/inverse.py,sha256=jd3YMqFgeHBUh9JoZa-1Wqh5lXZNB_UcrSD1paZYn-Q,2578
|
11
11
|
mathai/linear.py,sha256=eVnDbJYC1TWwg4J7ovBKsaHYlSoDmXk5jQpsqwtVPyI,5481
|
12
12
|
mathai/logic.py,sha256=UvHzRmKcO9AD51tRzHmpNSEhgW5gmaf4XPaQKFjGfC4,9653
|
13
13
|
mathai/parser.py,sha256=ENtMMm7Q_5QZL6cjbwsXGVJv-H53ueCPGEosJy4LcjY,6809
|
14
14
|
mathai/printeq.py,sha256=JTB0_RBcgt1yFviqlHtXGXuSXcpKrxzSxj5WHkOHon4,1318
|
15
|
-
mathai/simplify.py,sha256=
|
15
|
+
mathai/simplify.py,sha256=niSfqqzg44YGVuRRrQ9XG_K7Ak0Wj0WrRxQjkM0KBrw,13721
|
16
16
|
mathai/structure.py,sha256=4lKbRNRAkLTZjvnWEeKLXg_tTeBPgc0VIz0evQq4-K8,4069
|
17
17
|
mathai/tool.py,sha256=0WLsZR-LsXaPzrSt49ijvJImnqqW1_ArY4s1-V5o_74,1041
|
18
18
|
mathai/trig.py,sha256=gvwDjid8_O6XNnWkXXf0oGvkkqtbKg7_XMmwHwK9sjc,6681
|
19
|
-
mathai-0.1.
|
20
|
-
mathai-0.1.
|
21
|
-
mathai-0.1.
|
22
|
-
mathai-0.1.
|
19
|
+
mathai-0.1.5.dist-info/METADATA,sha256=c9zZ2vTB5uPeEZ3XSZnbH4ui69XnimwtJCPlitvbXXM,741
|
20
|
+
mathai-0.1.5.dist-info/WHEEL,sha256=jB7zZ3N9hIM9adW7qlTAyycLYW9npaWKLRzaoVcLKcM,91
|
21
|
+
mathai-0.1.5.dist-info/top_level.txt,sha256=ROP4l3OhGYw3ihkQGASr18xM9GsK4z3_6whV5AyXLwE,7
|
22
|
+
mathai-0.1.5.dist-info/RECORD,,
|
File without changes
|
File without changes
|