mathai 0.4.0__py3-none-any.whl → 0.4.1__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 CHANGED
@@ -36,8 +36,6 @@ from .console import console
36
36
 
37
37
  from .limit import limit
38
38
 
39
- from .search import dfs_simplify as search0
40
-
41
39
  from .univariate_inequality import wavycurvy, absolute, domain, handle_sqrt
42
40
 
43
41
  from .base import *
mathai/apart.py CHANGED
@@ -1,14 +1,18 @@
1
1
  from .linear import linear_solve
2
2
  from .expand import expand
3
3
  from .simplify import simplify
4
+
4
5
  from .diff import diff
5
6
  from .inverse import inverse
6
7
  from .base import *
7
8
  import math
8
9
  from .tool import poly, enclose_const
9
10
 
10
- def _apart(eq, v="v_0"):
11
-
11
+ def _apart(eq, v=None):
12
+ if v is None:
13
+ if len(vlist(eq)) == 0:
14
+ return eq
15
+ v = vlist(eq)[0]
12
16
  origv = vlist(eq)
13
17
  eq = simplify(eq)
14
18
  if eq.name != "f_mul":
@@ -42,8 +46,9 @@ def _apart(eq, v="v_0"):
42
46
  s = []
43
47
  facd = [simplify(x) for x in factor_generation(simplify(d))]
44
48
 
45
-
46
- facd2 = remove_duplicates_custom(facd, lambda m, n: simplify(m-n) == tree_form("d_0"))
49
+
50
+ facd2 = remove_duplicates_custom(facd, lambda m, n: simplify(expand(simplify(m-n))) == tree_form("d_0"))
51
+
47
52
  if len(facd2) == 1:
48
53
  return eq
49
54
  x = tree_form(v)
@@ -58,6 +63,7 @@ def _apart(eq, v="v_0"):
58
63
  if n > 2:
59
64
  return eq
60
65
  n = tree_form("d_"+str(n))
66
+
61
67
  l = len(poly(item, v))
62
68
  if l == 3:
63
69
  a = alloclst.pop(0)
@@ -93,14 +99,15 @@ def _apart(eq, v="v_0"):
93
99
  final = summation(final2)
94
100
 
95
101
  s = simplify(TreeNode("f_eq", [final-eq2, tree_form("d_0")]))
96
-
102
+
97
103
  lst = poly(s.children[0], v)
98
-
104
+
99
105
  lst = [TreeNode("f_eq", [item, tree_form("d_0")]) for item in lst if "v_" in str_form(item)]
100
106
  lst2 = []
101
107
  for item in lst:
102
108
  lst2+=vlist(item)
103
109
  origv = list(set(lst2)-set(origv))
110
+
104
111
  out = linear_solve(TreeNode("f_and", lst), [tree_form(item) for item in origv])
105
112
  for item in out.children:
106
113
 
@@ -108,6 +115,9 @@ def _apart(eq, v="v_0"):
108
115
  return simplify(final3)
109
116
  def apart(eq):
110
117
  eq, fx = enclose_const(eq)
111
-
112
- eq = _apart(eq)
113
- return fx(eq)
118
+ def helper(eq):
119
+ eq2 = _apart(eq)
120
+ if eq != eq2:
121
+ return eq2
122
+ return TreeNode(eq.name, [helper(child) for child in eq.children])
123
+ return fx(helper(eq))
mathai/base.py CHANGED
@@ -131,6 +131,8 @@ def frac(eq):
131
131
  return Fraction(int(eq.name[2:]))
132
132
  if eq.name == "f_add":
133
133
  p = frac(eq.children[0])
134
+ if p is None:
135
+ return None
134
136
  for child in eq.children[1:]:
135
137
  tmp = frac(child)
136
138
  if isinstance(tmp, Fraction):
@@ -140,6 +142,8 @@ def frac(eq):
140
142
  return p
141
143
  if eq.name == "f_mul":
142
144
  p = frac(eq.children[0])
145
+ if p is None:
146
+ return None
143
147
  for child in eq.children[1:]:
144
148
  tmp = frac(child)
145
149
  if isinstance(tmp, Fraction):
@@ -286,6 +290,8 @@ def flatten_tree(node):
286
290
  node.children = [flatten_tree(child) for child in node.children]
287
291
  return node
288
292
  def dowhile(eq, fx):
293
+ if eq is None:
294
+ return None
289
295
  while True:
290
296
  orig = eq.copy_tree()
291
297
  eq2 = fx(eq)
mathai/integrate.py CHANGED
@@ -104,8 +104,6 @@ def handle_try(eq):
104
104
  else:
105
105
  return TreeNode(eq.name, [handle_try(child) for child in eq.children])
106
106
  def inteq(eq):
107
- if "f_ref" not in str_form(eq):
108
- return eq
109
107
  if eq.name == "f_try":
110
108
  eq2 = None
111
109
  output = []
@@ -113,13 +111,19 @@ def inteq(eq):
113
111
  if child.name == "f_ref":
114
112
  eq2 = child.children[0]
115
113
  break
114
+ if eq2 is None:
115
+ return eq
116
116
  for child in eq.children:
117
117
  if child.name == "f_ref":
118
118
  output.append(child)
119
119
  else:
120
120
  eq3 = simplify(expand(simplify(eq2 - child)))
121
121
  if contain(eq3, eq2):
122
- output.append(inverse(eq3, str_form(eq2)))
122
+ out = inverse(eq3, str_form(eq2))
123
+ if out is None:
124
+ output.append(child)
125
+ else:
126
+ output.append(out)
123
127
  else:
124
128
  output.append(child)
125
129
  return TreeNode("f_try", output)
@@ -128,13 +132,14 @@ def inteq(eq):
128
132
  def rm(eq):
129
133
  if eq.name == "f_try":
130
134
  eq = TreeNode(eq.name, list(set(eq.children)))
131
- return TreeNode(eq.name, [rm(child) for child in eq.children])
135
+ return TreeNode(eq.name, [rm(child) for child in eq.children if child is not None])
132
136
  def solve_integrate(eq):
133
137
 
134
138
  eq2 = dowhile(eq, _solve_integrate)
135
139
  eq2 = dowhile(eq2, handle_try)
136
140
  eq2 = rm(eq2)
137
- eq2.children = list(set(eq2.children))
141
+ if eq2.name == "f_try":
142
+ eq2.children = list(set(eq2.children))
138
143
  return eq2
139
144
  def integrate_subs(equation, term, v1, v2):
140
145
  output = []
@@ -165,7 +170,7 @@ def integrate_subs(equation, term, v1, v2):
165
170
 
166
171
  return none
167
172
 
168
- return TreeNode("f_subs", [TreeNode("f_integrate", [simplify(expand(simplify(equation))), tree_form(origv2)]),tree_form(origv2) ,g])
173
+ return TreeNode("f_subs", [TreeNode("f_integrate", [simplify(fraction(expand(simplify(equation)))), tree_form(origv2)]),tree_form(origv2) ,g])
169
174
 
170
175
  def integrate_subs_main(equation):
171
176
  if equation.name == "f_ref":
@@ -291,7 +296,7 @@ def _sqint(equation):
291
296
  return coll
292
297
 
293
298
  def sqint(eq):
294
- out = _sqint(eq)
299
+ out = simplify(_sqint(eq))
295
300
  if out is None:
296
301
  return eq
297
302
  return out
@@ -376,6 +381,7 @@ def rm_const(equation):
376
381
  return rm_const(TreeNode("f_integrate",[equation, wrt])) *const
377
382
  equation = eq2
378
383
  return TreeNode(equation.name, [rm_const(child) for child in equation.children])
384
+
379
385
  def integrate_formula(equation):
380
386
  if equation.name == "f_ref":
381
387
  return equation.copy_tree()
@@ -384,17 +390,18 @@ def integrate_formula(equation):
384
390
  integrand = eq2.children[0]
385
391
  wrt = eq2.children[1]
386
392
  if integrand == wrt:
387
- return TreeNode("f_add", [TreeNode("f_power", [wrt.copy_tree(), TreeNode("2")]), TreeNode("f_div", [TreeNode("1"), TreeNode("2")])]) # x^2/2
393
+ return wrt**2/2 # x^2/2
388
394
  if not contain(integrand, wrt):
389
- return TreeNode("f_mul", [wrt.copy_tree(), integrand.copy_tree()]) # constant * dx
395
+ return integrand*wrt
390
396
  out = transform_formula(simplify(trig0(integrand)), wrt.name, formula_gen[0], formula_gen[1], formula_gen[2])
391
397
  if out is not None:
398
+
392
399
  return out
393
400
  expr_str = str_form(integrand)
394
401
  if expr_str.count("f_sin") + expr_str.count("f_cos") > 2:
395
402
  out = transform_formula(integrand, wrt.name, formula_gen4[0], formula_gen4[1], formula_gen4[2])
396
403
  if out is not None:
397
- print(out, 111)
404
+
398
405
  return out
399
406
  return TreeNode(eq2.name, [integrate_formula(child) for child in eq2.children])
400
407
 
mathai/linear.py CHANGED
@@ -3,6 +3,9 @@ from .simplify import simplify, solve
3
3
  from .fraction import fraction
4
4
  from .expand import expand
5
5
  from .base import *
6
+ from .factor import factorconst
7
+ def ss(eq):
8
+ return dowhile(eq, lambda x: fraction(expand(simplify(x))))
6
9
  def rref(matrix):
7
10
  rows, cols = len(matrix), len(matrix[0])
8
11
  lead = 0
@@ -10,7 +13,7 @@ def rref(matrix):
10
13
  if lead >= cols:
11
14
  return matrix
12
15
  i = r
13
- while fraction(simplify(matrix[i][lead])) == tree_form("d_0"):
16
+ while ss(matrix[i][lead]) == tree_form("d_0"):
14
17
  i += 1
15
18
  if i == rows:
16
19
  i = r
@@ -19,11 +22,11 @@ def rref(matrix):
19
22
  return matrix
20
23
  matrix[i], matrix[r] = matrix[r], matrix[i]
21
24
  lv = matrix[r][lead]
22
- matrix[r] = [fraction(simplify(m / lv)) for m in matrix[r]]
25
+ matrix[r] = [ss(m / lv) for m in matrix[r]]
23
26
  for i in range(rows):
24
27
  if i != r:
25
28
  lv = matrix[i][lead]
26
- matrix[i] = [fraction(simplify(m - lv * n)) for m, n in zip(matrix[i], matrix[r])]
29
+ matrix[i] = [ss(m - lv * n) for m, n in zip(matrix[i], matrix[r])]
27
30
  lead += 1
28
31
  return matrix
29
32
  def islinear(eq, fxconst):
@@ -91,7 +94,7 @@ def linear(eqlist, fxconst):
91
94
  for i in range(len(m)):
92
95
  for j in range(len(m[i])):
93
96
  m[i][j] = fraction(m[i][j])
94
- #print(m)
97
+
95
98
  for item in m:
96
99
  if all(item2==tree_form("d_0") for item2 in item[:-1]) and item[-1] != tree_form("d_0"):
97
100
  return tree_form("s_false")
mathai/simplify.py CHANGED
@@ -286,6 +286,23 @@ def simplify(eq):
286
286
  error = True
287
287
  else:
288
288
  eq = tree_form("d_0")
289
+
290
+ if eq.name == "f_mul":
291
+ dic = {}
292
+ for child in eq.children:
293
+ head = child
294
+ tail = None
295
+ if child.name == "f_pow":
296
+ head = child.children[0]
297
+ tail = child.children[1]
298
+ if tail is None:
299
+ tail = tree_form("d_1")
300
+ if head not in dic.keys():
301
+ dic[head] = tail
302
+ else:
303
+ dic[head] += tail
304
+ if len(eq.children) != len(dic.keys()):
305
+ eq = product([key if dic[key] == 1 else key**dic[key] for key in dic.keys()])
289
306
  if eq.name == "f_pow" and eq.children[0].name == "f_pow" and eq.children[0].children[1] == tree_form("d_2")**-1 and eq.children[1] == tree_form("d_2"):
290
307
  eq = eq.children[0].children[0]
291
308
  if (eq.name == "f_sin" and eq.children[0].name == "f_arcsin") or (eq.name == "f_cos" and eq.children[0].name == "f_arccos") or (eq.name == "f_tan" and eq.children[0].name == "f_arctan"):
mathai/trig.py CHANGED
@@ -199,16 +199,37 @@ def trig4(eq, numer=True):
199
199
  return tree_form("d_1")/(1+a**2)**(tree_form("d_2")**-1)
200
200
 
201
201
  return TreeNode(eq.name, [trig4(child, False) if not numer or (eq.name == "f_pow" and frac(eq.children[1]) is not None and frac(eq.children[1]) < 0) else trig4(child, True) for child in eq.children])
202
+
202
203
  def trig2(eq):
203
204
  if eq.name == "f_add":
204
205
  for item in itertools.combinations(range(len(eq.children)), 2):
205
- if all(eq.children[item2].name == "f_sin" for item2 in item):
206
- a, b = eq.children[item[0]].children[0], eq.children[item[1]].children[0]
207
- rest = [item2 for index, item2 in enumerate(eq.children) if index not in item]
208
- if len(rest)==0:
209
- rest = tree_form("d_0")
206
+ child1, child2 = eq.children[item[0]], eq.children[item[1]]
207
+
208
+ # Check if both are sin or cos
209
+ if child1.name in ["f_sin", "f_cos"] and child2.name in ["f_sin", "f_cos"]:
210
+ a, b = child1.children[0], child2.children[0]
211
+
212
+ # Compute the rest of the sum
213
+ rest = [eq.children[i] for i in range(len(eq.children)) if i not in item]
214
+ if len(rest) == 0:
215
+ rest_tree = tree_form("d_0")
216
+ else:
217
+ rest_tree = summation(rest)
218
+
219
+ # Now handle the sin/cos combination formula
220
+ if child1.name == "f_sin" and child2.name == "f_sin":
221
+ # sin A + sin B = 2 sin((A+B)/2) cos((A-B)/2)
222
+ two = tree_form("d_2")
223
+ combined = two * ((a + b) / two).fx("sin") * ((a - b) / two).fx("cos")
224
+ elif child1.name == "f_cos" and child2.name == "f_cos":
225
+ # cos A + cos B = 2 cos((A+B)/2) cos((A-B)/2)
226
+ two = tree_form("d_2")
227
+ combined = two * ((a + b) / two).fx("cos") * ((a - b) / two).fx("cos")
210
228
  else:
211
- rest = summation(rest)
212
- two = tree_form("d_2")
213
- return rest + two*((a+b)/two).fx("sin")*((a-b)/two).fx("cos")
229
+ # sin A + cos B = sin A + cos B (leave unchanged, or implement formula if desired)
230
+ continue # skip for now, keep original
231
+
232
+ return rest_tree + combined
233
+
234
+ # Recurse for other nodes
214
235
  return TreeNode(eq.name, [trig2(child) for child in eq.children])
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: mathai
3
- Version: 0.4.0
3
+ Version: 0.4.1
4
4
  Summary: Mathematics solving Ai tailored to NCERT
5
5
  Home-page: https://github.com/infinity390/mathai4
6
6
  Author: educated indians are having a low iq and are good for nothing
@@ -1,25 +1,25 @@
1
- mathai/__init__.py,sha256=GsDxDy6iWjoJ7CJty2nzE4Jx03mZkHFQW6hkmPJHkPE,1217
2
- mathai/apart.py,sha256=Fhl-OHZzPGIOj8YFyGQddjuh-2HV7uVDhkIzQ-YoMO0,3492
3
- mathai/base.py,sha256=x4Rjgz5mVblclsycKtDJjSudFpyIBWSEUX83_120BZY,12587
1
+ mathai/__init__.py,sha256=hxMCrvwj98yE5ChWSzvkn0gMxqAQnh8oze3ZlImWyD0,1170
2
+ mathai/apart.py,sha256=8IlJ8X6SKAjPunUPHVLmgNB5GuEi4_XpY0oLkQqrHKc,3770
3
+ mathai/base.py,sha256=DBFHDBRSPeb8O2cH_YzTNG3h7vxrQzrEPSUpDIPCtPI,12724
4
4
  mathai/console.py,sha256=Sn58iwYE79MLEh67s8X3q6vZjw6g7f9XM1T8_dBBR2o,3048
5
5
  mathai/diff.py,sha256=YUBpRsz0qmBkq5vGxeGnvR4nMKjdOQiIXlNMxpij2ns,3051
6
6
  mathai/expand.py,sha256=SnBltkpIENMGkP0AYmbMlSc4H-CF5RslO2PcBEkn1BQ,3359
7
7
  mathai/factor.py,sha256=NPXxET52TacNExuvw6p1jbC6g3wY6_VOCdlGlexXZio,5916
8
8
  mathai/fraction.py,sha256=Q2ztsh5Bpz6YhML2QU0tfufbAs0Q6J319AhlzKephIY,4396
9
- mathai/integrate.py,sha256=UKR_GH-ifplq_VyJKZukWjVDsQKkXXIuKNhpZ4w1Mhk,15033
9
+ mathai/integrate.py,sha256=mKw_L8zccFifJINKLCFiYBrkgQM2qO_Pk8b2ShgxTa8,15065
10
10
  mathai/inverse.py,sha256=QCvDrzKquWsZv-BDAzZd9HnU0c3gZvcc44UztHVO5LQ,2919
11
11
  mathai/limit.py,sha256=RA8YAehgYCGVWv9qBc8uQ34BQ9mFthWl2OrVTwcHl2g,4920
12
- mathai/linear.py,sha256=wyiLpIxRDmD96xXktkgvc5gegIB3zblLB1EuV3TFdmU,5474
12
+ mathai/linear.py,sha256=BzSnm941Zlod_l6hON4Rs6J4pdAA3MGpRVqr6-66ZBk,5524
13
13
  mathai/logic.py,sha256=UvHzRmKcO9AD51tRzHmpNSEhgW5gmaf4XPaQKFjGfC4,9653
14
14
  mathai/parser.py,sha256=f7bemieFmp0sbup1NlraMLvZDVFvqKGFknEVtlFRMVk,6979
15
15
  mathai/printeq.py,sha256=gIes-pstFOa6FcnpVIVvkjVKuWdsVdo11LlEnmHhakU,1303
16
16
  mathai/search.py,sha256=6S_V0PrC2ZnbhkWyT2ZTtExRD_mDlo-C3p0TDn6Dk3w,4743
17
- mathai/simplify.py,sha256=F37h-Z_rW35uVgx0G86vQErU2Ac6ZiTBN9KcC3RzDkg,15156
17
+ mathai/simplify.py,sha256=qG23MRyVLjJL_8Xk5n4Z596bjihFvZg_zKtIvOqrLIk,15824
18
18
  mathai/structure.py,sha256=4Ww2IAx62RcQSO7_17TZES-DjMWBpcFQtL939FBIHwY,4103
19
19
  mathai/tool.py,sha256=UyccamiJy_CkFPakfufyPzdhtlEO6v2D7qwbXQ9V7Rg,2000
20
- mathai/trig.py,sha256=5P0RNS4eetNds2l-wyA4BAKqJdFIUws_8RGS_dH7gp8,9348
20
+ mathai/trig.py,sha256=Zadw-uTJaMfoXwxuiio7QYHWgPxezEP2xZ2fr__PFM0,10290
21
21
  mathai/univariate_inequality.py,sha256=_r-kkiS4Hr-jRN7f-EL_E4svAMFWJP1Ea50HJKKbjfk,14778
22
- mathai-0.4.0.dist-info/METADATA,sha256=SB2hfiau23qNhjHplLEs5NzT7g3vBlW63YLJnh96Iq0,7021
23
- mathai-0.4.0.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
24
- mathai-0.4.0.dist-info/top_level.txt,sha256=ROP4l3OhGYw3ihkQGASr18xM9GsK4z3_6whV5AyXLwE,7
25
- mathai-0.4.0.dist-info/RECORD,,
22
+ mathai-0.4.1.dist-info/METADATA,sha256=m5N3sDp8R2_3ffySHyzvmo4YDpxYq6dT3FXUfkerCco,7021
23
+ mathai-0.4.1.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
24
+ mathai-0.4.1.dist-info/top_level.txt,sha256=ROP4l3OhGYw3ihkQGASr18xM9GsK4z3_6whV5AyXLwE,7
25
+ mathai-0.4.1.dist-info/RECORD,,
File without changes