mathai 0.3.1__py3-none-any.whl → 0.3.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 +32 -3
- mathai/apart.py +17 -7
- mathai/base.py +13 -6
- mathai/console.py +1 -1
- mathai/diff.py +7 -4
- mathai/expand.py +86 -48
- mathai/factor.py +11 -3
- mathai/fraction.py +97 -53
- mathai/integrate.py +304 -251
- mathai/linear.py +3 -2
- mathai/parser.py +2 -2
- mathai/printeq.py +1 -1
- mathai/simplify.py +11 -7
- mathai/structure.py +1 -1
- mathai/tool.py +28 -1
- mathai/trig.py +55 -10
- {mathai-0.3.1.dist-info → mathai-0.3.2.dist-info}/METADATA +13 -13
- mathai-0.3.2.dist-info/RECORD +25 -0
- {mathai-0.3.1.dist-info → mathai-0.3.2.dist-info}/WHEEL +1 -1
- mathai-0.3.1.dist-info/RECORD +0 -25
- {mathai-0.3.1.dist-info → mathai-0.3.2.dist-info}/top_level.txt +0 -0
mathai/integrate.py
CHANGED
@@ -1,3 +1,4 @@
|
|
1
|
+
from .factor import factor2
|
1
2
|
from .parser import parse
|
2
3
|
import itertools
|
3
4
|
from .diff import diff
|
@@ -11,47 +12,36 @@ from .inverse import inverse
|
|
11
12
|
from .tool import poly
|
12
13
|
from fractions import Fraction
|
13
14
|
from .printeq import printeq
|
14
|
-
|
15
|
-
|
16
|
-
orig = copy.deepcopy(equation)
|
17
|
-
for i in range(2):
|
18
|
-
|
19
|
-
if equation.name == "f_add":
|
20
|
-
logs += [(tab, f"by integration over sums {', '.join([printeq_str(simplify(child)) for child in equation.children])}")]
|
21
|
-
answer = []
|
22
|
-
for child in equation.children:
|
23
|
-
out = integrate(child, wrt, tab+1, inf)
|
24
|
-
if out is None:
|
25
|
-
return None
|
26
|
-
logs += out[1]
|
27
|
-
answer.append(out[0])
|
28
|
-
return summation(answer), logs
|
29
|
-
if i == 0:
|
30
|
-
|
31
|
-
tmp = expand(simplify(fraction(simplify(equation))))
|
32
|
-
|
33
|
-
logs += [(tab, f"integrating {printeq_str(simplify(equation))} will be the same thing as integrating {printeq_str(simplify(tmp))}")]
|
34
|
-
|
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]
|
41
|
-
return None
|
15
|
+
from .trig import trig0, trig2, trig3, trig4
|
16
|
+
from .apart import apart
|
42
17
|
|
18
|
+
def integrate_summation(equation):
|
19
|
+
if equation.name == "f_ref":
|
20
|
+
return equation
|
21
|
+
|
22
|
+
eq2 = equation
|
23
|
+
if eq2.name == "f_integrate":
|
24
|
+
equation = eq2.children[0]
|
25
|
+
wrt = eq2.children[1]
|
26
|
+
if equation.name == "f_add":
|
27
|
+
return summation([TreeNode("f_integrate", [child, wrt]) for child in equation.children])
|
28
|
+
equation = eq2
|
29
|
+
|
30
|
+
return TreeNode(equation.name, [integrate_summation(child) for child in equation.children])
|
43
31
|
def subs_heuristic(eq, var):
|
44
32
|
output = []
|
45
33
|
def collect2(eq):
|
46
|
-
if eq.name == "f_pow" and frac(eq.children[1]) is not None and abs(frac(eq.children[1])) ==
|
47
|
-
output.append(str_form(eq.children[0]
|
48
|
-
if eq.name in ["f_pow", "f_sin", "f_cos", "f_arcsin"] and
|
49
|
-
|
34
|
+
if eq.name == "f_pow" and frac(eq.children[1]) is not None and frac(eq.children[1]).denominator == 1 and abs(frac(eq.children[1]).numerator) % 2 == 0:
|
35
|
+
output.append(str_form(eq.children[0]**2))
|
36
|
+
if eq.name in ["f_pow", "f_sin", "f_cos", "f_arcsin"] and var.name in str_form(eq.children[0]):
|
37
|
+
if eq.children[0].name[:2] != "v_":
|
38
|
+
output.append(str_form(eq.children[0]))
|
39
|
+
if eq.name in ["f_sin", "f_cos"]:
|
40
|
+
output.append(str_form(eq))
|
50
41
|
if eq.name == "f_pow" and eq.children[0].name == "s_e" and "v_" in str_form(eq):
|
51
42
|
if eq.children[1].name[:2] != "v_":
|
52
43
|
output.append(str_form(eq.children[1]))
|
53
44
|
output.append(str_form(eq))
|
54
|
-
|
55
45
|
for child in eq.children:
|
56
46
|
collect2(child)
|
57
47
|
def collect3(eq):
|
@@ -60,27 +50,110 @@ def subs_heuristic(eq, var):
|
|
60
50
|
for child in eq.children:
|
61
51
|
collect3(child)
|
62
52
|
collect2(eq)
|
53
|
+
|
63
54
|
if output == []:
|
64
55
|
collect3(eq)
|
65
|
-
tmp = sorted(output, key=lambda x: len(x))
|
66
|
-
tmp = [simplify(tree_form(x)) for x in tmp]
|
67
56
|
|
57
|
+
|
58
|
+
tmp = list(set([simplify(tree_form(x)) for x in output]))
|
59
|
+
tmp = sorted(tmp, key=lambda x: len(str(x)))
|
68
60
|
return tmp
|
69
|
-
|
70
|
-
|
61
|
+
try_index = []
|
62
|
+
try_lst = []
|
63
|
+
def ref(eq):
|
64
|
+
if eq.name in ["f_try", "f_ref"]:
|
65
|
+
return eq
|
66
|
+
if eq.name == "f_integrate":
|
67
|
+
return TreeNode("f_try", [eq.fx("ref"), eq])
|
68
|
+
return TreeNode(eq.name, [ref(child) for child in eq.children])
|
69
|
+
def place_try(eq):
|
70
|
+
global try_index
|
71
|
+
if eq.name == "f_try":
|
72
|
+
try_index.append(list(range(len(eq.children))))
|
73
|
+
return TreeNode(eq.name, [place_try(child) for child in eq.children])
|
74
|
+
def place_try2(eq):
|
75
|
+
global try_lst
|
76
|
+
if eq.name == "f_try":
|
77
|
+
return eq.children[try_lst.pop(0)]
|
78
|
+
return TreeNode(eq.name, [place_try2(child) for child in eq.children])
|
79
|
+
def _solve_integrate(eq):
|
80
|
+
if eq.name == "f_ref":
|
81
|
+
return eq
|
82
|
+
if eq.name == "f_subs":
|
83
|
+
if "f_integrate" not in str_form(eq.children[0]):
|
84
|
+
return replace(eq.children[0], eq.children[1], eq.children[2])
|
85
|
+
|
86
|
+
if eq.name == "f_try":
|
87
|
+
for child in eq.children:
|
88
|
+
if "f_integrate" not in str_form(child):
|
89
|
+
return child
|
90
|
+
return TreeNode(eq.name, [_solve_integrate(child) for child in eq.children])
|
91
|
+
def handle_try(eq):
|
92
|
+
global try_lst, try_index
|
93
|
+
if eq.name == "f_try":
|
94
|
+
try_lst = []
|
95
|
+
try_index = []
|
96
|
+
for child in eq.children:
|
97
|
+
place_try(child)
|
98
|
+
output = []
|
99
|
+
for item in itertools.product(*try_index):
|
100
|
+
try_lst = list(item)
|
101
|
+
output += [place_try2(child) for child in eq.children]
|
102
|
+
|
103
|
+
return TreeNode("f_try", output)
|
104
|
+
else:
|
105
|
+
return TreeNode(eq.name, [handle_try(child) for child in eq.children])
|
106
|
+
def inteq(eq):
|
107
|
+
if "f_ref" not in str_form(eq):
|
108
|
+
return eq
|
109
|
+
if eq.name == "f_try":
|
110
|
+
eq2 = None
|
111
|
+
output = []
|
112
|
+
for child in eq.children:
|
113
|
+
if child.name == "f_ref":
|
114
|
+
eq2 = child.children[0]
|
115
|
+
break
|
116
|
+
for child in eq.children:
|
117
|
+
if child.name == "f_ref":
|
118
|
+
output.append(child)
|
119
|
+
else:
|
120
|
+
eq3 = simplify(expand(simplify(eq2 - child)))
|
121
|
+
if contain(eq3, eq2):
|
122
|
+
output.append(inverse(eq3, str_form(eq2)))
|
123
|
+
else:
|
124
|
+
output.append(child)
|
125
|
+
return TreeNode("f_try", output)
|
126
|
+
else:
|
127
|
+
return TreeNode(eq.name, [inteq(child) for child in eq.children])
|
128
|
+
def rm(eq):
|
129
|
+
if eq.name == "f_try":
|
130
|
+
eq = TreeNode(eq.name, list(set(eq.children)))
|
131
|
+
return TreeNode(eq.name, [rm(child) for child in eq.children])
|
132
|
+
def solve_integrate(eq):
|
71
133
|
|
134
|
+
eq2 = dowhile(eq, _solve_integrate)
|
135
|
+
eq2 = dowhile(eq2, handle_try)
|
136
|
+
eq2 = rm(eq2)
|
137
|
+
eq2.children = list(set(eq2.children))
|
138
|
+
return eq2
|
139
|
+
def integrate_subs(equation, term, v1, v2):
|
140
|
+
output = []
|
141
|
+
orig = equation.copy_tree()
|
142
|
+
none = TreeNode("f_integrate",[orig, tree_form(v1)])
|
72
143
|
origv2 = copy.deepcopy(v2)
|
73
144
|
equation = solve(equation)
|
74
145
|
eq = equation
|
75
146
|
termeq = term
|
76
147
|
t = inverse(copy.deepcopy(termeq), v1)
|
148
|
+
|
77
149
|
g = inverse(termeq, v2)
|
78
150
|
|
79
151
|
if g is None:
|
80
|
-
return
|
152
|
+
return none
|
81
153
|
if t is None:
|
82
|
-
return
|
154
|
+
return none
|
83
155
|
else:
|
156
|
+
|
84
157
|
t = expand(t)
|
85
158
|
eq = replace(eq, tree_form(v1), t)
|
86
159
|
|
@@ -88,185 +161,174 @@ def integrate_subs(equation, term, v1, v2, tab, inf):
|
|
88
161
|
equation = eq/eq2
|
89
162
|
equation = solve(equation)
|
90
163
|
|
91
|
-
|
92
|
-
for eq in lst:
|
93
|
-
if v1 in str_form(eq):
|
94
|
-
continue
|
164
|
+
if v1 in str_form(equation):
|
95
165
|
|
96
|
-
|
97
|
-
|
98
|
-
out = integrate(eq, origv2, tab+1, inf)
|
99
|
-
|
100
|
-
if out is None:
|
101
|
-
continue
|
102
|
-
tmp, logs = out
|
103
|
-
tmp = replace(tmp, tree_form(v2), g)
|
104
|
-
return tmp, [(tab, f"substituted {str(tree_form(origv2))}={printeq_str(simplify(g))}, integrating {printeq_str(simplify(eq))} wrt {str(tree_form(origv2))}")]+logs+\
|
105
|
-
[(tab, f"substituting back to {printeq_str(simplify(out[0]))} which is the result after integration")]
|
106
|
-
return None
|
166
|
+
return none
|
107
167
|
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
168
|
+
return TreeNode("f_subs", [TreeNode("f_integrate", [simplify(expand(simplify(equation))), tree_form(origv2)]),tree_form(origv2) ,g])
|
169
|
+
|
170
|
+
def integrate_subs_main(equation):
|
171
|
+
if equation.name == "f_ref":
|
172
|
+
return equation
|
173
|
+
eq2 = equation
|
174
|
+
if eq2.name == "f_integrate":
|
175
|
+
output = [eq2]
|
176
|
+
wrt = eq2.children[1]
|
177
|
+
eq = equation.children[0]
|
178
|
+
v2 = "v_"+str(int(wrt.name[2:])+1)
|
179
|
+
for item in subs_heuristic(eq, wrt):
|
180
|
+
x = tree_form(v2)-item
|
181
|
+
output.append(integrate_subs(eq, x, wrt.name, v2))
|
182
|
+
output = list(set(output))
|
183
|
+
if len(output) == 1:
|
184
|
+
return output[0]
|
114
185
|
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
logs = []
|
186
|
+
return TreeNode("f_try", [item.copy_tree() for item in output])
|
187
|
+
else:
|
188
|
+
return TreeNode(equation.name, [integrate_subs_main(child) for child in equation.children])
|
189
|
+
|
190
|
+
def _sqint(equation):
|
121
191
|
def sgn(eq):
|
122
192
|
if compute(eq) <0:
|
123
193
|
return tree_form("d_-1"), tree_form("d_-1")*eq
|
124
194
|
return tree_form("d_1"), eq
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
root = tree_form("d_2")**-1
|
130
|
-
zero = tree_form("d_0")
|
195
|
+
eq2 = equation
|
196
|
+
if eq2.name == "f_integrate":
|
197
|
+
equation = eq2.children[0]
|
198
|
+
var = eq2.children[1]
|
131
199
|
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
d =
|
140
|
-
|
141
|
-
|
142
|
-
|
200
|
+
one = tree_form("d_1")
|
201
|
+
two = tree_form("d_2")
|
202
|
+
four = tree_form("d_4")
|
203
|
+
three = tree_form("d_3")
|
204
|
+
root = tree_form("d_2")**-1
|
205
|
+
zero = tree_form("d_0")
|
206
|
+
|
207
|
+
n, d = num_dem(equation)
|
208
|
+
n, d = simplify(n), simplify(d)
|
209
|
+
term = [simplify(x) for x in factor_generation(d)]
|
210
|
+
const = product([item for item in term if "v_" not in str_form(item)])
|
211
|
+
term = [item for item in term if "v_" in str_form(item)]
|
212
|
+
mode = False
|
213
|
+
if all(item.name == "f_pow" and simplify(item.children[1]-root) == zero for item in term):
|
214
|
+
d = simplify(expand(const**two*product([item.children[0] for item in term])))
|
215
|
+
else:
|
216
|
+
mode = True
|
217
|
+
if any(item.name == "f_pow" and simplify(item.children[1]-root) == zero for item in term):
|
218
|
+
return None
|
219
|
+
if vlist(equation) == []:
|
143
220
|
return None
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
if np is None or dp is None:
|
154
|
-
return None
|
155
|
-
|
156
|
-
if len(np) == 1 and len(dp) == 3:
|
157
|
-
k, a, b, c = np+dp
|
158
|
-
if a == zero:
|
221
|
+
v = vlist(equation)[0]
|
222
|
+
x = tree_form(v)
|
223
|
+
|
224
|
+
np = poly(n, v)
|
225
|
+
|
226
|
+
dp = poly(d, v)
|
227
|
+
|
228
|
+
if np is None or dp is None:
|
159
229
|
return None
|
160
|
-
s1, s2 = sgn(a)
|
161
|
-
const = (four*a*c - b**two)/(four*a)
|
162
|
-
t1, t2 = sgn(const)
|
163
|
-
la = s2**root
|
164
|
-
lb = b*s2**root/(two*a)
|
165
230
|
|
166
|
-
if
|
231
|
+
if len(np) == 1 and len(dp) == 3:
|
232
|
+
k, a, b, c = np+dp
|
233
|
+
if a == zero:
|
234
|
+
return None
|
235
|
+
s1, s2 = sgn(a)
|
236
|
+
const = (four*a*c - b**two)/(four*a)
|
237
|
+
t1, t2 = sgn(const)
|
238
|
+
la = s2**root
|
239
|
+
lb = b*s2**root/(two*a)
|
240
|
+
|
241
|
+
if mode:
|
242
|
+
if s1 == one:
|
243
|
+
if t1 == one:
|
244
|
+
return k*((la*x+lb)/t2**root).fx("arctan")/(la * t2**root)
|
245
|
+
else:
|
246
|
+
return None
|
247
|
+
else:
|
248
|
+
if t1 == one:
|
249
|
+
return None
|
250
|
+
else:
|
251
|
+
_, t2 = sgn(-const)
|
252
|
+
return -k*((la*x+lb)/t2**root).fx("arctan")/(la * t2**root)
|
167
253
|
if s1 == one:
|
168
254
|
if t1 == one:
|
169
|
-
return k*((la*x+lb)
|
255
|
+
return simplify(k*(la*x + lb + ((la*x + lb)**two + t2)**root).fx("abs").fx("log")/la)
|
170
256
|
else:
|
171
|
-
return
|
257
|
+
return simplify(k*(la*x + lb + ((la*x + lb)**two - t2)**root).fx("abs").fx("log")/la)
|
258
|
+
|
172
259
|
else:
|
173
260
|
if t1 == one:
|
174
|
-
return
|
261
|
+
return k*((la*x + lb)/t2**root).fx("arcsin")/la
|
175
262
|
else:
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
else:
|
182
|
-
return simplify(k*(la*x + lb + ((la*x + lb)**two - t2)**root).fx("abs").fx("log")/la), logs
|
183
|
-
|
184
|
-
else:
|
185
|
-
if t1 == one:
|
186
|
-
return k*((la*x + lb)/t2**root).fx("arcsin")/la, logs
|
187
|
-
else:
|
263
|
+
return None
|
264
|
+
if len(np) == 2 and len(dp) == 3:
|
265
|
+
|
266
|
+
p, q, a, b, c = np+dp
|
267
|
+
if a == zero:
|
188
268
|
return None
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
B = q - A*b
|
196
|
-
t = a*x**two + b*x + c
|
197
|
-
|
198
|
-
if not mode:
|
199
|
-
tmp = sqint(simplify(one/t**root), var, depth, inf)
|
200
|
-
if tmp is None:
|
201
|
-
tmp = integrate(simplify(one/t**root), var, depth, inf)
|
269
|
+
A = p/(two*a)
|
270
|
+
B = q - A*b
|
271
|
+
t = a*x**two + b*x + c
|
272
|
+
|
273
|
+
if not mode:
|
274
|
+
tmp = _sqint(TreeNode("f_integrate", [simplify(one/t**root), var]))
|
202
275
|
if tmp is None:
|
203
|
-
|
204
|
-
|
205
|
-
tmp = tmp[0]
|
206
|
-
|
276
|
+
tmp = TreeNode("f_integrate", [simplify(one/t**root), var])
|
277
|
+
return A*two*t**root + tmp*B
|
207
278
|
else:
|
208
|
-
|
209
|
-
tmp = tmp[0]
|
210
|
-
return A*two*t**root + tmp*B, logs+log2
|
211
|
-
else:
|
212
|
-
tmp = sqint(simplify(one/t), var, depth, inf)
|
213
|
-
if tmp is None:
|
214
|
-
tmp = integrate(simplify(one/t), var, depth, inf)
|
215
|
-
|
279
|
+
tmp = _sqint(TreeNode("f_integrate", [simplify(one/t), var]))
|
216
280
|
if tmp is None:
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
if
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
281
|
+
tmp = TreeNode("f_integrate", [simplify(one/t), var])
|
282
|
+
return A*t.fx("abs").fx("log") + tmp*B
|
283
|
+
equation = eq2
|
284
|
+
coll = TreeNode(equation.name, [])
|
285
|
+
for child in equation.children:
|
286
|
+
out = _sqint(child)
|
287
|
+
if out is None:
|
288
|
+
coll.children.append(child)
|
289
|
+
else:
|
290
|
+
coll.children.append(out)
|
291
|
+
return coll
|
292
|
+
|
293
|
+
def sqint(eq):
|
294
|
+
out = _sqint(eq)
|
295
|
+
if out is None:
|
296
|
+
return eq
|
297
|
+
return out
|
298
|
+
|
299
|
+
def byparts(eq):
|
300
|
+
if eq.name == "f_ref":
|
301
|
+
return eq
|
302
|
+
eq2 = eq
|
303
|
+
if eq2.name == "f_integrate":
|
304
|
+
output = []
|
305
|
+
eq = eq2.children[0]
|
306
|
+
wrt = eq2.children[1]
|
307
|
+
lst = factor_generation(eq)
|
308
|
+
if len(lst) == 3 and len(list(set(lst))) == 1:
|
309
|
+
lst = [(lst[0]**2).copy_tree(), lst[0].copy_tree()]
|
244
310
|
if len(lst) == 1:
|
245
|
-
lst += [
|
246
|
-
elif len(lst)==2 and item != 1:
|
247
|
-
break
|
311
|
+
lst += [tree_form("d_1")]
|
248
312
|
if len(lst) == 2:
|
249
313
|
for i in range(2):
|
250
314
|
|
251
|
-
f, g =
|
315
|
+
f, g = [lst[i], lst[1-i]]
|
252
316
|
|
253
|
-
|
254
|
-
|
255
|
-
out1 = integrate(copy.deepcopy(g), wrt, tab+1, inf)
|
256
|
-
typebyparts()
|
257
|
-
|
258
|
-
if out1 is None:
|
259
|
-
continue
|
260
|
-
|
261
|
-
typeintegrate()
|
262
|
-
out2 = integrate(simplify(diff(copy.deepcopy(f), wrt)*out1[0]), wrt, tab+1, inf)
|
317
|
+
out1 = TreeNode("f_integrate", [g.copy_tree(), wrt])
|
318
|
+
|
263
319
|
|
264
|
-
|
265
|
-
if out2 is None:
|
266
|
-
continue
|
320
|
+
out2 = TreeNode("f_integrate", [simplify(diff(f.copy_tree(), wrt.name)*out1), wrt])
|
267
321
|
|
268
|
-
|
269
|
-
|
322
|
+
output.append(simplify(f.copy_tree() * out1 - out2))
|
323
|
+
if len(output) == 0:
|
324
|
+
pass
|
325
|
+
elif len(output) == 1:
|
326
|
+
return output[0]
|
327
|
+
else:
|
328
|
+
return TreeNode("f_try", output)
|
329
|
+
eq = eq2
|
330
|
+
return TreeNode(eq.name, [byparts(child) for child in eq.children])
|
331
|
+
|
270
332
|
def integration_formula_init():
|
271
333
|
var = "x"
|
272
334
|
formula_list = [(f"(A*{var}+B)^C", f"(A*{var}+B)^(C+1)/(A*(C+1))"),\
|
@@ -274,73 +336,64 @@ def integration_formula_init():
|
|
274
336
|
(f"cos(A*{var}+B)", f"sin(A*{var}+B)/A"),\
|
275
337
|
(f"1/(A*{var}+B)", f"log(abs(A*{var}+B))/A"),\
|
276
338
|
(f"e^(A*{var}+B)", f"e^(A*{var}+B)/A"),\
|
339
|
+
(f"1/cos({var})", f"log(abs((1+sin({var}))/cos({var})))"),\
|
340
|
+
(f"1/cos({var})^2", f"sin({var})/cos({var})"),\
|
277
341
|
(f"abs(A*{var}+B)", f"(A*{var}+B)*abs(A*{var}+B)/(2*A)")]
|
278
342
|
formula_list = [[simplify(parse(y)) for y in x] for x in formula_list]
|
279
343
|
expr = [[parse("A"), parse("1")], [parse("B"), parse("0")]]
|
280
344
|
return [formula_list, var, expr]
|
281
345
|
formula_gen = integration_formula_init()
|
346
|
+
def integration_formula_trig():
|
347
|
+
var = "x"
|
348
|
+
formula_list = [(f"(A+B*sin({var})+C*cos({var}))/(D+E*sin({var})+F*cos({var}))", f"((B*E+C*F)/(E^2+F^2))*{var}+((C*E-B*F)/(E^2+F^2))*log(D+E*sin({var})+F*cos({var}))")]
|
349
|
+
formula_list = [[simplify(parse(y)) for y in x] for x in formula_list]
|
350
|
+
expr = [[parse("A"), parse("0"), parse("1")], [parse("B"), parse("0"), parse("1")],\
|
351
|
+
[parse("C"), parse("0"), parse("1")], [parse("D"), parse("0"), parse("1")],\
|
352
|
+
[parse("E"), parse("0"), parse("1")], [parse("F"), parse("0"), parse("1")]]
|
353
|
+
return [formula_list, var, expr]
|
282
354
|
|
283
|
-
def rm_const(equation, wrt="v_0", tab=0, inf=0, logs=[]):
|
284
|
-
lst = factor_generation(equation)
|
285
|
-
|
286
|
-
lst_const = [item for item in lst if not contain(item, tree_form(wrt))]
|
287
|
-
if lst_const != []:
|
288
|
-
equation = product([item for item in lst if contain(item, tree_form(wrt))])
|
289
|
-
const = product(lst_const)
|
290
|
-
const = simplify(const)
|
291
|
-
if const != 1 and not contain(const, tree_form("s_i")):
|
292
|
-
|
293
|
-
equation = simplify(equation)
|
294
|
-
out = None
|
295
|
-
if typeint == "byparts":
|
296
|
-
out = byparts(equation, wrt, tab+1, inf)
|
297
|
-
else:
|
298
|
-
out = integrate(equation, wrt, tab+1, inf)
|
299
|
-
if out is None:
|
300
|
-
return None
|
301
|
-
out = (out[0]*const, out[1])
|
302
|
-
return out[0], logs+\
|
303
|
-
[(tab, f"extracted the constant {printeq_str(simplify(const))}, now integrating the equation {printeq_str(simplify(equation))} only")]+out[1]+\
|
304
|
-
[(tab, f"result is {printeq_str(simplify(out[0]))}")]
|
305
|
-
return None
|
306
|
-
def integrate(equation, wrt="v_0", tab=0, inf=0):
|
307
|
-
|
308
|
-
global formula_list, var, expr
|
309
|
-
global typeint
|
310
|
-
|
311
|
-
equation = simplify(equation)
|
312
|
-
|
313
|
-
logs = []
|
314
|
-
if tab == 0:
|
315
|
-
logs += [(tab, f"the given question is to integrate {printeq_str(simplify(equation))} wrt to {str(tree_form(wrt))}")]
|
316
|
-
|
317
|
-
if equation == tree_form(wrt):
|
318
|
-
return equation**2/2,[]
|
319
|
-
if not contain(equation,tree_form(wrt)):
|
320
|
-
return tree_form(wrt)*equation,logs
|
321
|
-
out = transform_formula(equation, wrt, formula_gen[0], formula_gen[1], formula_gen[2])
|
322
|
-
if out is not None:
|
323
|
-
return out, logs
|
324
355
|
|
325
|
-
|
326
|
-
|
327
|
-
|
328
|
-
|
329
|
-
|
330
|
-
|
331
|
-
|
332
|
-
|
333
|
-
|
334
|
-
|
335
|
-
return out[0], logs+out[1]+[(tab, f"result is {printeq_str(simplify(out[0]))}")]
|
336
|
-
if typeint in ["byparts", "integrate"]:
|
337
|
-
if inf==0:
|
338
|
-
out = integrate_subs_main(equation, wrt, tab, inf+1)
|
339
|
-
if out is not None:
|
340
|
-
return out[0], logs+out[1]+[(tab, f"result is {printeq_str(simplify(out[0]))}")]
|
341
|
-
if typeint == "byparts":
|
356
|
+
formula_gen4 = integration_formula_trig()
|
357
|
+
|
358
|
+
def rm_const(equation):
|
359
|
+
if equation.name == "f_ref":
|
360
|
+
return equation
|
361
|
+
eq2 = equation
|
362
|
+
if eq2.name == "f_integrate":
|
363
|
+
equation = eq2.children[0]
|
364
|
+
wrt = eq2.children[1]
|
365
|
+
lst = factor_generation(equation)
|
342
366
|
|
343
|
-
|
367
|
+
lst_const = [item for item in lst if not contain(item, wrt)]
|
368
|
+
if lst_const != []:
|
369
|
+
|
370
|
+
equation = product([item for item in lst if contain(item, wrt)]).copy_tree()
|
371
|
+
const = product(lst_const)
|
372
|
+
const = simplify(const)
|
373
|
+
|
374
|
+
if not contain(const, tree_form("s_i")):
|
375
|
+
|
376
|
+
return rm_const(TreeNode("f_integrate",[equation, wrt])) *const
|
377
|
+
equation = eq2
|
378
|
+
return TreeNode(equation.name, [rm_const(child) for child in equation.children])
|
379
|
+
def integrate_formula(equation):
|
380
|
+
if equation.name == "f_ref":
|
381
|
+
return equation
|
382
|
+
eq2 = equation.copy_tree()
|
383
|
+
if eq2.name == "f_integrate":
|
384
|
+
equation = eq2.children[0]
|
385
|
+
wrt = eq2.children[1]
|
386
|
+
if equation == wrt:
|
387
|
+
return equation**2/2
|
388
|
+
if not contain(equation,wrt):
|
389
|
+
return wrt*equation
|
390
|
+
out = transform_formula(simplify(trig0(equation)), wrt.name, formula_gen[0], formula_gen[1], formula_gen[2])
|
344
391
|
if out is not None:
|
345
|
-
return out
|
346
|
-
|
392
|
+
return out
|
393
|
+
|
394
|
+
if str_form(equation).count("f_sin")+str_form(equation).count("f_cos")>2:
|
395
|
+
out = transform_formula(equation, wrt.name, formula_gen4[0], formula_gen4[1], formula_gen4[2])
|
396
|
+
if out is not None:
|
397
|
+
return out
|
398
|
+
equation = eq2
|
399
|
+
return TreeNode(equation.name, [integrate_formula(child) for child in equation.children])
|
mathai/linear.py
CHANGED
@@ -70,6 +70,7 @@ def linear(eqlist, fxconst):
|
|
70
70
|
for eq in eqlist:
|
71
71
|
varlist(eq, fxconst)
|
72
72
|
vl = list(set(vl))
|
73
|
+
|
73
74
|
if len(vl) > len(eqlist):
|
74
75
|
return TreeNode("f_and", final+[TreeNode("f_eq", [x, tree_form("d_0")]) for x in eqlist])
|
75
76
|
m = []
|
@@ -84,7 +85,7 @@ def linear(eqlist, fxconst):
|
|
84
85
|
for i in range(len(m)):
|
85
86
|
for j in range(len(m[i])):
|
86
87
|
m[i][j] = simplify(expand(m[i][j]))
|
87
|
-
|
88
|
+
|
88
89
|
m = rref(m)
|
89
90
|
|
90
91
|
for i in range(len(m)):
|
@@ -93,7 +94,7 @@ def linear(eqlist, fxconst):
|
|
93
94
|
#print(m)
|
94
95
|
for item in m:
|
95
96
|
if all(item2==tree_form("d_0") for item2 in item[:-1]) and item[-1] != tree_form("d_0"):
|
96
|
-
return tree_form("
|
97
|
+
return tree_form("s_false")
|
97
98
|
|
98
99
|
output = []
|
99
100
|
for index, row in enumerate(m):
|