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/factor.py
CHANGED
@@ -1,125 +1,125 @@
|
|
1
|
-
import itertools
|
2
|
-
from .parser import parse
|
3
|
-
from .structure import transform_formula
|
4
|
-
from .base import *
|
5
|
-
from .simplify import simplify,solve
|
6
|
-
from .expand import expand
|
7
|
-
import math
|
8
|
-
|
9
|
-
from collections import Counter
|
10
|
-
def multiset_intersection(*lists):
|
11
|
-
counters = list(map(Counter, lists))
|
12
|
-
common = counters[0]
|
13
|
-
for c in counters[1:]:
|
14
|
-
common = common & c
|
15
|
-
return list(common.elements())
|
16
|
-
def subtract_sublist(full_list, sublist):
|
17
|
-
c_full = Counter(full_list)
|
18
|
-
c_sub = Counter(sublist)
|
19
|
-
result = c_full - c_sub
|
20
|
-
tmp = list(result.elements())
|
21
|
-
if tmp == []:
|
22
|
-
return [tree_form("d_1")]
|
23
|
-
return tmp
|
24
|
-
def term_common2(eq):
|
25
|
-
if eq.name != "f_add":
|
26
|
-
return eq
|
27
|
-
s = []
|
28
|
-
arr = [factor_generation(child) for child in eq.children]
|
29
|
-
s = multiset_intersection(*arr)
|
30
|
-
return product(s)*summation([product(subtract_sublist(factor_generation(child), s)) for child in eq.children])
|
31
|
-
def term_common(eq):
|
32
|
-
if eq.name == "f_add":
|
33
|
-
return solve(term_common2(eq))
|
34
|
-
return solve(product([term_common2(item) for item in factor_generation(eq)]))
|
35
|
-
def take_common(eq):
|
36
|
-
if eq.name == "f_add":
|
37
|
-
eq = term_common(eq)
|
38
|
-
if eq.name == "f_add":
|
39
|
-
for i in range(len(eq.children)-1,1,-1):
|
40
|
-
for item in itertools.combinations(range(len(eq.children)), i):
|
41
|
-
eq2 = summation([item2 for index, item2 in enumerate(eq.children) if index in item])
|
42
|
-
eq2 = term_common(eq2)
|
43
|
-
if eq2.name == "f_mul":
|
44
|
-
return take_common(solve(summation([item2 for index, item2 in enumerate(eq.children) if index not in item]) + eq2))
|
45
|
-
return eq
|
46
|
-
return term_common(eq)
|
47
|
-
def take_common2(eq):
|
48
|
-
eq = take_common(eq)
|
49
|
-
return TreeNode(eq.name, [take_common2(child) for child in eq.children])
|
50
|
-
|
51
|
-
def _factorconst(eq):
|
52
|
-
def hcf_list(numbers):
|
53
|
-
if not numbers:
|
54
|
-
return None # empty list
|
55
|
-
hcf = numbers[0]
|
56
|
-
for num in numbers[1:]:
|
57
|
-
hcf = math.gcd(hcf, num)
|
58
|
-
return hcf
|
59
|
-
def extractnum(eq):
|
60
|
-
lst = factor_generation(eq)
|
61
|
-
for item in lst:
|
62
|
-
if item.name[:2] == "d_":
|
63
|
-
return int(item.name[2:])
|
64
|
-
return 1
|
65
|
-
n = 1
|
66
|
-
if eq.name == "f_add":
|
67
|
-
n = hcf_list([extractnum(child) for child in eq.children])
|
68
|
-
eq = TreeNode(eq.name, [child/tree_form("d_"+str(n)) for child in eq.children])
|
69
|
-
if n != 1:
|
70
|
-
return tree_form("d_"+str(n))*eq
|
71
|
-
return TreeNode(eq.name, [factorconst(child) for child in eq.children])
|
72
|
-
def factorconst(eq):
|
73
|
-
return simplify(_factorconst(eq))
|
74
|
-
def factor_quad_formula_init():
|
75
|
-
var = ""
|
76
|
-
formula_list = [(f"(A*D^2+B*D+C)", f"A*(D-(-B+(B^2-4*A*C)^(1/2))/(2*A))*(D-(-B-(B^2-4*A*C)^(1/2))/(2*A))")]
|
77
|
-
formula_list = [[simplify(parse(y)) for y in x] for x in formula_list]
|
78
|
-
expr = [[parse("A"), parse("1")], [parse("B"), parse("0"), parse("1")], [parse("C"), parse("0")]]
|
79
|
-
return [formula_list, var, expr]
|
80
|
-
|
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))"),\
|
84
|
-
(f"-D^3+E", f"(-D+E^(1/3))*(D^2+D*E^(1/3)+E^(2/3))")]
|
85
|
-
formula_list = [[simplify(parse(y)) for y in x] for x in formula_list]
|
86
|
-
expr = [[parse("A")], [parse("B")]]
|
87
|
-
return [formula_list, var, expr]
|
88
|
-
formula_gen2 = factor_quad_formula_init()
|
89
|
-
formula_gen3 = factor_cube_formula_init()
|
90
|
-
def factor_helper(equation, complexnum, power=2):
|
91
|
-
global formula_gen2, formula_gen3
|
92
|
-
maxnum = 1
|
93
|
-
def high(eq):
|
94
|
-
nonlocal maxnum
|
95
|
-
if eq.name == "f_pow" and eq.children[1].name[:2] == "d_":
|
96
|
-
n = int(eq.children[1].name[2:])
|
97
|
-
if n>power and n % power == 0:
|
98
|
-
maxnum = max(maxnum, n)
|
99
|
-
for child in eq.children:
|
100
|
-
high(child)
|
101
|
-
def helper(eq):
|
102
|
-
nonlocal maxnum
|
103
|
-
if eq.name == "f_pow" and eq.children[1].name[:2] == "d_":
|
104
|
-
n = int(eq.children[1].name[2:])
|
105
|
-
sgn = round(abs(n)/n)
|
106
|
-
n = abs(n)
|
107
|
-
if n>power and n % power == 0 and maxnum==n:
|
108
|
-
out= (eq.children[0]**tree_form("d_"+str(sgn*int(n/power))))**power
|
109
|
-
return out
|
110
|
-
return TreeNode(eq.name, [helper(child) for child in eq.children])
|
111
|
-
high(equation)
|
112
|
-
out = None
|
113
|
-
if power == 2:
|
114
|
-
out = transform_formula(helper(equation), "v_0", formula_gen2[0], formula_gen2[1], formula_gen2[2])
|
115
|
-
elif power == 3:
|
116
|
-
out = transform_formula(helper(equation), "v_0", formula_gen3[0], formula_gen3[1], formula_gen3[2])
|
117
|
-
if out is not None:
|
118
|
-
out = simplify(solve(out))
|
119
|
-
if out is not None and (complexnum or (not complexnum and not contain(out, tree_form("s_i")))):
|
120
|
-
return out
|
121
|
-
return TreeNode(equation.name, [factor_helper(child, complexnum, power) for child in equation.children])
|
122
|
-
def factor(equation, complexnum=False):
|
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))
|
1
|
+
import itertools
|
2
|
+
from .parser import parse
|
3
|
+
from .structure import transform_formula
|
4
|
+
from .base import *
|
5
|
+
from .simplify import simplify,solve
|
6
|
+
from .expand import expand
|
7
|
+
import math
|
8
|
+
|
9
|
+
from collections import Counter
|
10
|
+
def multiset_intersection(*lists):
|
11
|
+
counters = list(map(Counter, lists))
|
12
|
+
common = counters[0]
|
13
|
+
for c in counters[1:]:
|
14
|
+
common = common & c
|
15
|
+
return list(common.elements())
|
16
|
+
def subtract_sublist(full_list, sublist):
|
17
|
+
c_full = Counter(full_list)
|
18
|
+
c_sub = Counter(sublist)
|
19
|
+
result = c_full - c_sub
|
20
|
+
tmp = list(result.elements())
|
21
|
+
if tmp == []:
|
22
|
+
return [tree_form("d_1")]
|
23
|
+
return tmp
|
24
|
+
def term_common2(eq):
|
25
|
+
if eq.name != "f_add":
|
26
|
+
return eq
|
27
|
+
s = []
|
28
|
+
arr = [factor_generation(child) for child in eq.children]
|
29
|
+
s = multiset_intersection(*arr)
|
30
|
+
return product(s)*summation([product(subtract_sublist(factor_generation(child), s)) for child in eq.children])
|
31
|
+
def term_common(eq):
|
32
|
+
if eq.name == "f_add":
|
33
|
+
return solve(term_common2(eq))
|
34
|
+
return solve(product([term_common2(item) for item in factor_generation(eq)]))
|
35
|
+
def take_common(eq):
|
36
|
+
if eq.name == "f_add":
|
37
|
+
eq = term_common(eq)
|
38
|
+
if eq.name == "f_add":
|
39
|
+
for i in range(len(eq.children)-1,1,-1):
|
40
|
+
for item in itertools.combinations(range(len(eq.children)), i):
|
41
|
+
eq2 = summation([item2 for index, item2 in enumerate(eq.children) if index in item])
|
42
|
+
eq2 = term_common(eq2)
|
43
|
+
if eq2.name == "f_mul":
|
44
|
+
return take_common(solve(summation([item2 for index, item2 in enumerate(eq.children) if index not in item]) + eq2))
|
45
|
+
return eq
|
46
|
+
return term_common(eq)
|
47
|
+
def take_common2(eq):
|
48
|
+
eq = take_common(eq)
|
49
|
+
return TreeNode(eq.name, [take_common2(child) for child in eq.children])
|
50
|
+
|
51
|
+
def _factorconst(eq):
|
52
|
+
def hcf_list(numbers):
|
53
|
+
if not numbers:
|
54
|
+
return None # empty list
|
55
|
+
hcf = numbers[0]
|
56
|
+
for num in numbers[1:]:
|
57
|
+
hcf = math.gcd(hcf, num)
|
58
|
+
return hcf
|
59
|
+
def extractnum(eq):
|
60
|
+
lst = factor_generation(eq)
|
61
|
+
for item in lst:
|
62
|
+
if item.name[:2] == "d_":
|
63
|
+
return int(item.name[2:])
|
64
|
+
return 1
|
65
|
+
n = 1
|
66
|
+
if eq.name == "f_add":
|
67
|
+
n = hcf_list([extractnum(child) for child in eq.children])
|
68
|
+
eq = TreeNode(eq.name, [child/tree_form("d_"+str(n)) for child in eq.children])
|
69
|
+
if n != 1:
|
70
|
+
return tree_form("d_"+str(n))*eq
|
71
|
+
return TreeNode(eq.name, [factorconst(child) for child in eq.children])
|
72
|
+
def factorconst(eq):
|
73
|
+
return simplify(_factorconst(eq))
|
74
|
+
def factor_quad_formula_init():
|
75
|
+
var = ""
|
76
|
+
formula_list = [(f"(A*D^2+B*D+C)", f"A*(D-(-B+(B^2-4*A*C)^(1/2))/(2*A))*(D-(-B-(B^2-4*A*C)^(1/2))/(2*A))")]
|
77
|
+
formula_list = [[simplify(parse(y)) for y in x] for x in formula_list]
|
78
|
+
expr = [[parse("A"), parse("1")], [parse("B"), parse("0"), parse("1")], [parse("C"), parse("0")]]
|
79
|
+
return [formula_list, var, expr]
|
80
|
+
|
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))"),\
|
84
|
+
(f"-D^3+E", f"(-D+E^(1/3))*(D^2+D*E^(1/3)+E^(2/3))")]
|
85
|
+
formula_list = [[simplify(parse(y)) for y in x] for x in formula_list]
|
86
|
+
expr = [[parse("A")], [parse("B")]]
|
87
|
+
return [formula_list, var, expr]
|
88
|
+
formula_gen2 = factor_quad_formula_init()
|
89
|
+
formula_gen3 = factor_cube_formula_init()
|
90
|
+
def factor_helper(equation, complexnum, power=2):
|
91
|
+
global formula_gen2, formula_gen3
|
92
|
+
maxnum = 1
|
93
|
+
def high(eq):
|
94
|
+
nonlocal maxnum
|
95
|
+
if eq.name == "f_pow" and eq.children[1].name[:2] == "d_":
|
96
|
+
n = int(eq.children[1].name[2:])
|
97
|
+
if n>power and n % power == 0:
|
98
|
+
maxnum = max(maxnum, n)
|
99
|
+
for child in eq.children:
|
100
|
+
high(child)
|
101
|
+
def helper(eq):
|
102
|
+
nonlocal maxnum
|
103
|
+
if eq.name == "f_pow" and eq.children[1].name[:2] == "d_":
|
104
|
+
n = int(eq.children[1].name[2:])
|
105
|
+
sgn = round(abs(n)/n)
|
106
|
+
n = abs(n)
|
107
|
+
if n>power and n % power == 0 and maxnum==n:
|
108
|
+
out= (eq.children[0]**tree_form("d_"+str(sgn*int(n/power))))**power
|
109
|
+
return out
|
110
|
+
return TreeNode(eq.name, [helper(child) for child in eq.children])
|
111
|
+
high(equation)
|
112
|
+
out = None
|
113
|
+
if power == 2:
|
114
|
+
out = transform_formula(helper(equation), "v_0", formula_gen2[0], formula_gen2[1], formula_gen2[2])
|
115
|
+
elif power == 3:
|
116
|
+
out = transform_formula(helper(equation), "v_0", formula_gen3[0], formula_gen3[1], formula_gen3[2])
|
117
|
+
if out is not None:
|
118
|
+
out = simplify(solve(out))
|
119
|
+
if out is not None and (complexnum or (not complexnum and not contain(out, tree_form("s_i")))):
|
120
|
+
return out
|
121
|
+
return TreeNode(equation.name, [factor_helper(child, complexnum, power) for child in equation.children])
|
122
|
+
def factor(equation, complexnum=False):
|
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/fraction.py
CHANGED
@@ -1,59 +1,59 @@
|
|
1
|
-
from .base import *
|
2
|
-
from .simplify import solve
|
3
|
-
from .expand import expand
|
4
|
-
def fraction(eq):
|
5
|
-
if eq.name == "f_eq":
|
6
|
-
return TreeNode(eq.name, [fraction(eq.children[0]), fraction(eq.children[1])])
|
7
|
-
if eq.name == "f_add":
|
8
|
-
con = []
|
9
|
-
for child in eq.children:
|
10
|
-
if child.name == "f_pow" and child.children[1].name[:2] == "d_" and int(child.children[1].name[2:])<0:
|
11
|
-
den = []
|
12
|
-
n = int(child.children[1].name[2:])
|
13
|
-
if n == -1:
|
14
|
-
den.append(child.children[0])
|
15
|
-
else:
|
16
|
-
den.append(TreeNode("f_pow", [child.children[0], tree_form("d_"+str(-n))]))
|
17
|
-
con.append([[], den])
|
18
|
-
elif child.name == "f_mul":
|
19
|
-
num = []
|
20
|
-
den = []
|
21
|
-
for child2 in child.children:
|
22
|
-
if child2.name == "f_pow" and child2.children[1].name[:2] == "d_" and int(child2.children[1].name[2:])<0:
|
23
|
-
n = int(child2.children[1].name[2:])
|
24
|
-
if n == -1:
|
25
|
-
den.append(child2.children[0])
|
26
|
-
else:
|
27
|
-
den.append(TreeNode("f_pow", [child2.children[0], tree_form("d_"+str(-n))]))
|
28
|
-
else:
|
29
|
-
num.append(child2)
|
30
|
-
con.append([num, den])
|
31
|
-
else:
|
32
|
-
con.append([[child], []])
|
33
|
-
if len(con)>1 and any(x[1] != [] for x in con):
|
34
|
-
a = TreeNode("f_add", [])
|
35
|
-
for i in range(len(con)):
|
36
|
-
b = TreeNode("f_mul", [])
|
37
|
-
if con[i][0] != []:
|
38
|
-
b.children += con[i][0]
|
39
|
-
for j in range(len(con)):
|
40
|
-
if i ==j:
|
41
|
-
continue
|
42
|
-
b.children += con[j][1]
|
43
|
-
if len(b.children) == 1:
|
44
|
-
a.children.append(b.children[0])
|
45
|
-
elif len(b.children) > 1:
|
46
|
-
a.children.append(b)
|
47
|
-
else:
|
48
|
-
a.children.append(tree_form("d_1"))
|
49
|
-
c = TreeNode("f_mul", [])
|
50
|
-
for i in range(len(con)):
|
51
|
-
c.children += con[i][1]
|
52
|
-
if len(c.children)==1:
|
53
|
-
c = c.children[0]
|
54
|
-
c = TreeNode("f_pow", [c, tree_form("d_-1")])
|
55
|
-
return TreeNode("f_mul", [expand(a),c])
|
56
|
-
arr = TreeNode(eq.name, [])
|
57
|
-
for child in eq.children:
|
58
|
-
arr.children.append(fraction(child))
|
59
|
-
return solve(arr)
|
1
|
+
from .base import *
|
2
|
+
from .simplify import solve
|
3
|
+
from .expand import expand
|
4
|
+
def fraction(eq):
|
5
|
+
if eq.name == "f_eq":
|
6
|
+
return TreeNode(eq.name, [fraction(eq.children[0]), fraction(eq.children[1])])
|
7
|
+
if eq.name == "f_add":
|
8
|
+
con = []
|
9
|
+
for child in eq.children:
|
10
|
+
if child.name == "f_pow" and child.children[1].name[:2] == "d_" and int(child.children[1].name[2:])<0:
|
11
|
+
den = []
|
12
|
+
n = int(child.children[1].name[2:])
|
13
|
+
if n == -1:
|
14
|
+
den.append(child.children[0])
|
15
|
+
else:
|
16
|
+
den.append(TreeNode("f_pow", [child.children[0], tree_form("d_"+str(-n))]))
|
17
|
+
con.append([[], den])
|
18
|
+
elif child.name == "f_mul":
|
19
|
+
num = []
|
20
|
+
den = []
|
21
|
+
for child2 in child.children:
|
22
|
+
if child2.name == "f_pow" and child2.children[1].name[:2] == "d_" and int(child2.children[1].name[2:])<0:
|
23
|
+
n = int(child2.children[1].name[2:])
|
24
|
+
if n == -1:
|
25
|
+
den.append(child2.children[0])
|
26
|
+
else:
|
27
|
+
den.append(TreeNode("f_pow", [child2.children[0], tree_form("d_"+str(-n))]))
|
28
|
+
else:
|
29
|
+
num.append(child2)
|
30
|
+
con.append([num, den])
|
31
|
+
else:
|
32
|
+
con.append([[child], []])
|
33
|
+
if len(con)>1 and any(x[1] != [] for x in con):
|
34
|
+
a = TreeNode("f_add", [])
|
35
|
+
for i in range(len(con)):
|
36
|
+
b = TreeNode("f_mul", [])
|
37
|
+
if con[i][0] != []:
|
38
|
+
b.children += con[i][0]
|
39
|
+
for j in range(len(con)):
|
40
|
+
if i ==j:
|
41
|
+
continue
|
42
|
+
b.children += con[j][1]
|
43
|
+
if len(b.children) == 1:
|
44
|
+
a.children.append(b.children[0])
|
45
|
+
elif len(b.children) > 1:
|
46
|
+
a.children.append(b)
|
47
|
+
else:
|
48
|
+
a.children.append(tree_form("d_1"))
|
49
|
+
c = TreeNode("f_mul", [])
|
50
|
+
for i in range(len(con)):
|
51
|
+
c.children += con[i][1]
|
52
|
+
if len(c.children)==1:
|
53
|
+
c = c.children[0]
|
54
|
+
c = TreeNode("f_pow", [c, tree_form("d_-1")])
|
55
|
+
return TreeNode("f_mul", [expand(a),c])
|
56
|
+
arr = TreeNode(eq.name, [])
|
57
|
+
for child in eq.children:
|
58
|
+
arr.children.append(fraction(child))
|
59
|
+
return solve(arr)
|