mathai 0.2.8__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/console.py CHANGED
@@ -1,84 +1,84 @@
1
- import copy
2
- from .expand import expand
3
- from .parser import parse
4
- from .printeq import printeq, printeq_log
5
- from .simplify import solve, simplify
6
- from .integrate import integrate, typesqint, typebyparts, typeintegrate
7
- from .diff import diff
8
- from .base import *
9
- from .factor import _factorconst, factor
10
- from .fraction import fraction
11
- from .inverse import inverse
12
- from .trig import trig0, trig1, trig2, trig3, trig4
13
- from .logic import logic0, logic1, logic2, logic3
14
- from .apart import apart
15
-
16
- def console():
17
- eq = None
18
- orig = None
19
- while True:
20
- command = input(">>> ")
21
- try:
22
- orig = copy.deepcopy(eq)
23
- if command == "expand":
24
- eq = expand(eq)
25
- elif command.split(" ")[0] == "inverse":
26
- eq=simplify(eq)
27
- if eq.name == "f_eq":
28
- eq3 = eq.children[0]-eq.children[1]
29
- eq2 = parse(command.split(" ")[1])
30
- out = inverse(eq3, str_form(eq2))
31
- eq = TreeNode(eq.name, [eq2,out])
32
- elif command == "apart":
33
- eq = apart(eq, vlist(eq)[0])
34
- elif command == "rawprint":
35
- print(eq)
36
- elif command == "logic0":
37
- eq = logic0(eq)
38
- elif command == "logic1":
39
- eq = logic1(eq)
40
- elif command == "logic2":
41
- eq = logic2(eq)
42
- elif command == "logic3":
43
- eq = logic3(eq)
44
- elif command == "trig0":
45
- eq = trig0(eq)
46
- elif command == "trig1":
47
- eq = trig1(eq)
48
- elif command == "factor":
49
- eq = factor(eq)
50
- elif command == "trig2":
51
- eq = trig2(eq)
52
- elif command == "trig3":
53
- eq = trig3(eq)
54
- elif command == "trig4":
55
- eq = trig4(eq)
56
- elif command == "simplify":
57
- eq = _factorconst(eq)
58
- eq = simplify(eq)
59
- elif command == "fraction":
60
- eq = fraction(eq)
61
- elif command.split(" ")[0] in ["integrate", "sqint", "byparts"]:
62
- if command.split(" ")[0] == "sqint":
63
- typesqint()
64
- elif command.split(" ")[0] == "byparts":
65
- typebyparts()
66
- elif command.split(" ")[0] == "integrate":
67
- typeintegrate()
68
- out = integrate(eq, parse(command.split(" ")[1]).name)
69
- if out is None:
70
- print("failed to integrate")
71
- else:
72
- eq, logs = out
73
- eq = simplify(eq)
74
- printeq_log(logs)
75
- print()
76
- elif command.split(" ")[0] == "diff":
77
- eq = diff(eq, parse(command.split(" ")[1]).name)
78
- else:
79
- eq = parse(command)
80
- eq = copy.deepcopy(eq)
81
- printeq(eq)
82
- except:
83
- eq = copy.deepcopy(orig)
84
- print("error")
1
+ import copy
2
+ from .expand import expand
3
+ from .parser import parse
4
+ from .printeq import printeq, printeq_log
5
+ from .simplify import solve, simplify
6
+ from .integrate import integrate, typesqint, typebyparts, typeintegrate
7
+ from .diff import diff
8
+ from .base import *
9
+ from .factor import _factorconst, factor
10
+ from .fraction import fraction
11
+ from .inverse import inverse
12
+ from .trig import trig0, trig1, trig2, trig3, trig4
13
+ from .logic import logic0, logic1, logic2, logic3
14
+ from .apart import apart
15
+
16
+ def console():
17
+ eq = None
18
+ orig = None
19
+ while True:
20
+ command = input(">>> ")
21
+ try:
22
+ orig = copy.deepcopy(eq)
23
+ if command == "expand":
24
+ eq = expand(eq)
25
+ elif command.split(" ")[0] == "inverse":
26
+ eq=simplify(eq)
27
+ if eq.name == "f_eq":
28
+ eq3 = eq.children[0]-eq.children[1]
29
+ eq2 = parse(command.split(" ")[1])
30
+ out = inverse(eq3, str_form(eq2))
31
+ eq = TreeNode(eq.name, [eq2,out])
32
+ elif command == "apart":
33
+ eq = apart(eq, vlist(eq)[0])
34
+ elif command == "rawprint":
35
+ print(eq)
36
+ elif command == "logic0":
37
+ eq = logic0(eq)
38
+ elif command == "logic1":
39
+ eq = logic1(eq)
40
+ elif command == "logic2":
41
+ eq = logic2(eq)
42
+ elif command == "logic3":
43
+ eq = logic3(eq)
44
+ elif command == "trig0":
45
+ eq = trig0(eq)
46
+ elif command == "trig1":
47
+ eq = trig1(eq)
48
+ elif command == "factor":
49
+ eq = factor(eq)
50
+ elif command == "trig2":
51
+ eq = trig2(eq)
52
+ elif command == "trig3":
53
+ eq = trig3(eq)
54
+ elif command == "trig4":
55
+ eq = trig4(eq)
56
+ elif command == "simplify":
57
+ eq = _factorconst(eq)
58
+ eq = simplify(eq)
59
+ elif command == "fraction":
60
+ eq = fraction(eq)
61
+ elif command.split(" ")[0] in ["integrate", "sqint", "byparts"]:
62
+ if command.split(" ")[0] == "sqint":
63
+ typesqint()
64
+ elif command.split(" ")[0] == "byparts":
65
+ typebyparts()
66
+ elif command.split(" ")[0] == "integrate":
67
+ typeintegrate()
68
+ out = integrate(eq, parse(command.split(" ")[1]).name)
69
+ if out is None:
70
+ print("failed to integrate")
71
+ else:
72
+ eq, logs = out
73
+ eq = simplify(eq)
74
+ printeq_log(logs)
75
+ print()
76
+ elif command.split(" ")[0] == "diff":
77
+ eq = diff(eq, parse(command.split(" ")[1]).name)
78
+ else:
79
+ eq = parse(command)
80
+ eq = copy.deepcopy(eq)
81
+ printeq(eq)
82
+ except:
83
+ eq = copy.deepcopy(orig)
84
+ print("error")
mathai/diff.py CHANGED
@@ -1,65 +1,65 @@
1
- from .simplify import solve
2
- from .base import *
3
-
4
- def diff(equation, var="v_0"):
5
- def diffeq(eq):
6
- eq = solve(eq)
7
- if "v_" not in str_form(eq):
8
- return tree_form("d_0")
9
- if eq.name == "f_add":
10
- add = tree_form("d_0")
11
- for child in eq.children:
12
- add += diffeq(child)
13
- return add
14
- elif eq.name == "f_abs":
15
- return diffeq(eq.children[0])*eq.children[0]/eq
16
- elif eq.name == "f_pow" and eq.children[0].name == "s_e":
17
- return diffeq(eq.children[1])*eq
18
- elif eq.name == "f_tan":
19
- return diffeq(eq.children[0])/(eq.children[0].fx("cos")*eq.children[0].fx("cos"))
20
- elif eq.name == "f_log":
21
- return diffeq(eq.children[0])*(tree_form("d_1")/eq.children[0])
22
- elif eq.name == "f_arcsin":
23
- return diffeq(eq.children[0])/(tree_form("d_1")-eq.children[0]*eq.children[0])**(tree_form("d_2")**-1)
24
- elif eq.name == "f_arccos":
25
- return tree_form("d_-1")*diffeq(eq.children[0])/(tree_form("d_1")-eq.children[0]*eq.children[0])**(tree_form("d_2")**-1)
26
- elif eq.name == "f_arctan":
27
- return diffeq(eq.children[0])/(tree_form("d_1")+eq.children[0]*eq.children[0])
28
- elif eq.name == "f_pow" and "v_" in str_form(eq.children[1]):
29
- a, b = eq.children
30
- return a**b * ((b/a) * diffeq(a) + a.fx("log") * diffeq(b))
31
- elif eq.name == "f_mul":
32
- add = tree_form("d_0")
33
- for i in range(len(eq.children)):
34
- tmp = eq.children.pop(i)
35
- if len(eq.children)==1:
36
- eq2 = eq.children[0]
37
- else:
38
- eq2 = eq
39
- add += diffeq(tmp)*eq2
40
- eq.children.insert(i, tmp)
41
- return add
42
- elif eq.name == "f_sin":
43
- eq.name = "f_cos"
44
- return diffeq(eq.children[0])*eq
45
- elif eq.name == "f_cos":
46
- eq.name = "f_sin"
47
- return tree_form("d_-1")*diffeq(eq.children[0])*eq
48
- elif eq.name[:2] == "v_":
49
- return TreeNode("f_dif", [eq])
50
- elif eq.name == "f_pow" and "v_" not in str_form(eq.children[1]):
51
- base, power = eq.children
52
- dbase = diffeq(base)
53
- b1 = power - tree_form("d_1")
54
- bab1 = TreeNode("f_pow", [base, b1])
55
- return power * bab1 * dbase
56
- return eq.fx("dif")
57
- def helper(equation, var="v_0"):
58
- if equation.name == "f_dif":
59
- if equation.children[0].name == var:
60
- return tree_form("d_1")
61
- return tree_form("d_0")
62
- return TreeNode(equation.name, [helper(child, var) for child in equation.children])
63
- equation = diffeq(equation)
64
- equation = helper(equation, var)
65
- return solve(equation)
1
+ from .simplify import solve
2
+ from .base import *
3
+
4
+ def diff(equation, var="v_0"):
5
+ def diffeq(eq):
6
+ eq = solve(eq)
7
+ if "v_" not in str_form(eq):
8
+ return tree_form("d_0")
9
+ if eq.name == "f_add":
10
+ add = tree_form("d_0")
11
+ for child in eq.children:
12
+ add += diffeq(child)
13
+ return add
14
+ elif eq.name == "f_abs":
15
+ return diffeq(eq.children[0])*eq.children[0]/eq
16
+ elif eq.name == "f_pow" and eq.children[0].name == "s_e":
17
+ return diffeq(eq.children[1])*eq
18
+ elif eq.name == "f_tan":
19
+ return diffeq(eq.children[0])/(eq.children[0].fx("cos")*eq.children[0].fx("cos"))
20
+ elif eq.name == "f_log":
21
+ return diffeq(eq.children[0])*(tree_form("d_1")/eq.children[0])
22
+ elif eq.name == "f_arcsin":
23
+ return diffeq(eq.children[0])/(tree_form("d_1")-eq.children[0]*eq.children[0])**(tree_form("d_2")**-1)
24
+ elif eq.name == "f_arccos":
25
+ return tree_form("d_-1")*diffeq(eq.children[0])/(tree_form("d_1")-eq.children[0]*eq.children[0])**(tree_form("d_2")**-1)
26
+ elif eq.name == "f_arctan":
27
+ return diffeq(eq.children[0])/(tree_form("d_1")+eq.children[0]*eq.children[0])
28
+ elif eq.name == "f_pow" and "v_" in str_form(eq.children[1]):
29
+ a, b = eq.children
30
+ return a**b * ((b/a) * diffeq(a) + a.fx("log") * diffeq(b))
31
+ elif eq.name == "f_mul":
32
+ add = tree_form("d_0")
33
+ for i in range(len(eq.children)):
34
+ tmp = eq.children.pop(i)
35
+ if len(eq.children)==1:
36
+ eq2 = eq.children[0]
37
+ else:
38
+ eq2 = eq
39
+ add += diffeq(tmp)*eq2
40
+ eq.children.insert(i, tmp)
41
+ return add
42
+ elif eq.name == "f_sin":
43
+ eq.name = "f_cos"
44
+ return diffeq(eq.children[0])*eq
45
+ elif eq.name == "f_cos":
46
+ eq.name = "f_sin"
47
+ return tree_form("d_-1")*diffeq(eq.children[0])*eq
48
+ elif eq.name[:2] == "v_":
49
+ return TreeNode("f_dif", [eq])
50
+ elif eq.name == "f_pow" and "v_" not in str_form(eq.children[1]):
51
+ base, power = eq.children
52
+ dbase = diffeq(base)
53
+ b1 = power - tree_form("d_1")
54
+ bab1 = TreeNode("f_pow", [base, b1])
55
+ return power * bab1 * dbase
56
+ return eq.fx("dif")
57
+ def helper(equation, var="v_0"):
58
+ if equation.name == "f_dif":
59
+ if equation.children[0].name == var:
60
+ return tree_form("d_1")
61
+ return tree_form("d_0")
62
+ return TreeNode(equation.name, [helper(child, var) for child in equation.children])
63
+ equation = diffeq(equation)
64
+ equation = helper(equation, var)
65
+ return solve(equation)
mathai/expand.py CHANGED
@@ -1,58 +1,58 @@
1
-
2
- import itertools
3
- from .base import *
4
- from .simplify import solve, simplify
5
-
6
- def expand(eq):
7
- if eq is None:
8
- return None
9
- if eq.name == "f_mul" or eq.name == "f_pow":
10
- if eq.name == "f_pow":
11
- eq = TreeNode("f_pow", [eq])
12
- ac = []
13
- addchild = []
14
- for child in eq.children:
15
- tmp5 = [solve(x) for x in factor_generation(child)]
16
- ac += tmp5
17
- tmp3 = []
18
- for child in ac:
19
- tmp2 = []
20
- if child.name == "f_add":
21
- if child.children != []:
22
- for child2 in child.children:
23
- tmp2.append(child2)
24
- else:
25
- tmp2 = [child]
26
- else:
27
- tmp3.append(child)
28
- if tmp2 != []:
29
- addchild.append(tmp2)
30
- tmp4 = 1
31
- for item in tmp3:
32
- tmp4 = tmp4 * item
33
- addchild.append([tmp4])
34
- def flatten(lst):
35
- flat_list = []
36
- for item in lst:
37
- if isinstance(item, list) and item == []:
38
- continue
39
- if isinstance(item, list):
40
- flat_list.extend(flatten(item))
41
- else:
42
- flat_list.append(item)
43
- return flat_list
44
-
45
- if isinstance(addchild, list) and len(flatten(addchild))>0:
46
- add= 0
47
- for item in itertools.product(*addchild):
48
- mul = 1
49
- for item2 in item:
50
- mul = mul * item2
51
- mul = simplify(mul)
52
- add = add + mul
53
- add = simplify(add)
54
- eq = add
55
- eq = simplify(eq)
56
-
57
- return TreeNode(eq.name, [expand(child) for child in eq.children])
58
-
1
+
2
+ import itertools
3
+ from .base import *
4
+ from .simplify import solve, simplify
5
+
6
+ def expand(eq):
7
+ if eq is None:
8
+ return None
9
+ if eq.name == "f_mul" or eq.name == "f_pow":
10
+ if eq.name == "f_pow":
11
+ eq = TreeNode("f_pow", [eq])
12
+ ac = []
13
+ addchild = []
14
+ for child in eq.children:
15
+ tmp5 = [solve(x) for x in factor_generation(child)]
16
+ ac += tmp5
17
+ tmp3 = []
18
+ for child in ac:
19
+ tmp2 = []
20
+ if child.name == "f_add":
21
+ if child.children != []:
22
+ for child2 in child.children:
23
+ tmp2.append(child2)
24
+ else:
25
+ tmp2 = [child]
26
+ else:
27
+ tmp3.append(child)
28
+ if tmp2 != []:
29
+ addchild.append(tmp2)
30
+ tmp4 = 1
31
+ for item in tmp3:
32
+ tmp4 = tmp4 * item
33
+ addchild.append([tmp4])
34
+ def flatten(lst):
35
+ flat_list = []
36
+ for item in lst:
37
+ if isinstance(item, list) and item == []:
38
+ continue
39
+ if isinstance(item, list):
40
+ flat_list.extend(flatten(item))
41
+ else:
42
+ flat_list.append(item)
43
+ return flat_list
44
+
45
+ if isinstance(addchild, list) and len(flatten(addchild))>0:
46
+ add= 0
47
+ for item in itertools.product(*addchild):
48
+ mul = 1
49
+ for item2 in item:
50
+ mul = mul * item2
51
+ mul = simplify(mul)
52
+ add = add + mul
53
+ add = simplify(add)
54
+ eq = add
55
+ eq = simplify(eq)
56
+
57
+ return TreeNode(eq.name, [expand(child) for child in eq.children])
58
+