mathai 0.6.4__py3-none-any.whl → 0.6.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/base.py CHANGED
@@ -1,11 +1,12 @@
1
1
  import copy
2
2
  from fractions import Fraction
3
3
  def contains_list_or_neg(node):
4
- if node.name == "f_list" or node.name.startswith("v_-"):
5
- return True
6
- for child in node.children:
7
- if contains_list_or_neg(child):
4
+ stack = [node]
5
+ while stack:
6
+ n = stack.pop()
7
+ if n.name == "f_list" or n.name.startswith("v_-"):
8
8
  return True
9
+ stack.extend(n.children)
9
10
  return False
10
11
  class TreeNode:
11
12
  matmul = None
@@ -30,31 +31,32 @@ class TreeNode:
30
31
  sortable.append(c)
31
32
  else:
32
33
  fixed.append(c)
34
+
35
+ if len(sortable) > 1:
36
+ sortable = TreeNode("f_dmul", list(sorted(sortable, key=lambda x: str_form(x))))
37
+ sortable.name = "f_mul"
38
+
39
+ elif len(sortable) == 1:
40
+ sortable = sortable[0]
41
+
42
+ if isinstance(sortable, TreeNode):
43
+ fixed.append(sortable)
33
44
  if len(fixed) > 1:
34
- fixed = TreeNode("f_wmul", fixed)
45
+ self.name = "f_wmul"
35
46
  elif len(fixed) == 1:
36
- fixed = fixed[0]
37
- if isinstance(fixed, TreeNode):
38
- sortable.append(fixed)
39
- if len(sortable)==1 and name == "f_mul":
40
- self.name = sortable[0].name
41
- if self.name in ["f_add", "f_mul"]:
42
- self.children = list(sorted(sortable[0].children, key=lambda x: str_form(x)))
43
- else:
44
- self.children = sortable[0].children
47
+ self.name = fixed[0].name
48
+ fixed = fixed[0].children
45
49
 
46
- else:
47
- self.children = list(sorted(sortable, key=lambda x: str_form(x)))
48
- elif name == "f_mul" and TreeNode.matmul == 0:
49
-
50
- self.children = children
50
+
51
+ self.children = fixed
51
52
  else:
52
53
  self.children = children
53
54
 
54
55
 
55
56
  def fx(self, fxname):
56
57
  return TreeNode("f_" + fxname, [self])
57
-
58
+ def copy_tree(self):
59
+ return copy.deepcopy(self)
58
60
  def __repr__(self):
59
61
  return string_equation(str_form(self))
60
62
 
mathai/matrix.py CHANGED
@@ -32,16 +32,26 @@ def dot(u,v):
32
32
  s = TreeNode("f_add",[s,TreeNode("f_mul",[a,b])])
33
33
  return s
34
34
 
35
- def matmul(A,B):
36
- n,m,p = len(A), len(A[0]), len(B[0])
37
- if m!=len(B):
35
+ def matmul(A, B):
36
+ # A: n × m
37
+ # B: m × p
38
+
39
+ n = len(A)
40
+ m = len(A[0])
41
+ p = len(B[0])
42
+
43
+ if m != len(B):
38
44
  raise ValueError("Matrix dimension mismatch")
39
- z = tree_form("d_0")
40
- C = [[z for _ in range(p)] for _ in range(n)]
45
+
46
+ C = [[tree_form("d_0") for _ in range(p)] for _ in range(n)]
47
+
41
48
  for i in range(n):
42
49
  for j in range(p):
43
50
  for k in range(m):
44
- C[i][j] = TreeNode("f_add",[C[i][j], TreeNode("f_mul",[A[i][k], B[k][j]])])
51
+ C[i][j] = TreeNode(
52
+ "f_add",
53
+ [C[i][j], TreeNode("f_mul", [A[i][k], B[k][j]])]
54
+ )
45
55
  return C
46
56
 
47
57
  # ---------- promotion ----------
@@ -84,7 +94,38 @@ def multiply(left,right):
84
94
  return py_to_tree([[TreeNode("f_mul",[A,x]) for x in row] for row in B])
85
95
  A, B = B, A
86
96
  return None
97
+ def add_vec(A, B):
98
+ if len(A) != len(B):
99
+ raise ValueError("Vector dimension mismatch")
100
+
101
+ return [
102
+ TreeNode("f_add", [A[i], B[i]])
103
+ for i in range(len(A))
104
+ ]
105
+ def matadd(A, B):
106
+ if len(A) != len(B) or len(A[0]) != len(B[0]):
107
+ raise ValueError("Matrix dimension mismatch")
108
+
109
+ n = len(A)
110
+ m = len(A[0])
87
111
 
112
+ return [
113
+ [
114
+ TreeNode("f_add", [A[i][j], B[i][j]])
115
+ for j in range(m)
116
+ ]
117
+ for i in range(n)
118
+ ]
119
+ def addition(left,right):
120
+ A,B = promote(left), promote(right)
121
+ # vector + vector
122
+ if is_vector(A) and is_vector(B):
123
+ return add_vec(A,B)
124
+ # matrix + matrix
125
+ if is_matrix(A) and is_matrix(B):
126
+ return py_to_tree(matadd(A,B))
127
+ return None
128
+ '''
88
129
  def fold_wmul(eq):
89
130
  if eq.name == "f_pow" and eq.children[1].name.startswith("d_"):
90
131
  n = int(eq.children[1].name[2:])
@@ -97,22 +138,87 @@ def fold_wmul(eq):
97
138
  for i in range(n-1):
98
139
  tmp = matmul(orig, tmp)
99
140
  eq = py_to_tree(tmp)
100
- elif eq.name=="f_wmul":
101
-
102
- i = len(eq.children)-1
103
- while i>0:
104
- out = multiply(eq.children[i], eq.children[i-1])
105
- if out is not None:
106
- eq.children.pop(i)
107
- eq.children.pop(i-1)
108
- eq.children.insert(i-1,out)
109
- i = i-1
141
+ elif eq.name in ["f_wmul", "f_add"]:
142
+ if len(eq.children) == 1:
143
+ eq = eq.children[0]
144
+ else:
145
+ i = len(eq.children)-1
146
+ while i>0:
147
+ if eq.name == "f_wmul":
148
+ out = multiply(eq.children[i-1], eq.children[i])
149
+ else:
150
+ out = addition(eq.children[i-1], eq.children[i])
151
+ if out is not None:
152
+ eq.children.pop(i)
153
+ eq.children.pop(i-1)
154
+ eq.children.insert(i-1,out)
155
+ i = i-1
110
156
  return TreeNode(eq.name, [fold_wmul(child) for child in eq.children])
157
+ '''
158
+ def fold_wmul(root):
159
+ # Post-order traversal using explicit stack
160
+ stack = [(root, False)]
161
+ newnode = {}
162
+
163
+ while stack:
164
+ node, visited = stack.pop()
165
+
166
+ if not visited:
167
+ # First time: push back as visited, then children
168
+ stack.append((node, True))
169
+ for child in node.children:
170
+ stack.append((child, False))
171
+ else:
172
+ # All children already processed
173
+ children = [newnode[c] for c in node.children]
174
+ eq = TreeNode(node.name, children)
175
+
176
+ # ---- original rewrite logic ----
177
+
178
+ if eq.name == "f_pow" and eq.children[1].name.startswith("d_"):
179
+ n = int(eq.children[1].name[2:])
180
+ if n == 1:
181
+ eq = eq.children[0]
182
+ elif n > 1:
183
+ tmp = promote(eq.children[0])
184
+ if is_matrix(tmp):
185
+ orig = tmp
186
+ for _ in range(n - 1):
187
+ tmp = matmul(orig, tmp)
188
+ eq = py_to_tree(tmp)
189
+
190
+ elif eq.name in ["f_wmul", "f_add"]:
191
+ if len(eq.children) == 1:
192
+ eq = eq.children[0]
193
+ else:
194
+ i = len(eq.children) - 1
195
+ while i > 0:
196
+ if eq.name == "f_wmul":
197
+ out = multiply(eq.children[i - 1], eq.children[i])
198
+ else:
199
+ out = addition(eq.children[i - 1], eq.children[i])
200
+
201
+ if out is not None:
202
+ eq.children.pop(i)
203
+ eq.children.pop(i - 1)
204
+ eq.children.insert(i - 1, out)
205
+ i -= 1
206
+
207
+ # --------------------------------
208
+
209
+ newnode[node] = eq
210
+
211
+ return newnode[root]
212
+
111
213
  def flat(eq):
112
214
  return flatten_tree(eq, ["f_wmul"])
113
- def matrix_solve(eq):
215
+ def use(eq):
216
+ return TreeNode(eq.name, [use(child) for child in eq.children])
217
+ def _matrix_solve(eq):
114
218
  if TreeNode.matmul == True:
115
219
  TreeNode.matmul = False
116
220
  eq = flat(dowhile(eq, lambda x: fold_wmul(flat(x))))
117
221
  TreeNode.matmul = True
118
222
  return eq
223
+ def matrix_solve(eq):
224
+ return _matrix_solve(eq)
mathai/parser.py CHANGED
@@ -158,4 +158,6 @@ def parse(equation, funclist=None):
158
158
  TreeNode.matmul = False
159
159
  tree_node = tree_form(str_form(tree_node).replace("f_w","f_"))
160
160
  TreeNode.matmul = True
161
+ else:
162
+ tree_node = tree_form(str_form(tree_node).replace("f_w","f_"))
161
163
  return tree_node
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: mathai
3
- Version: 0.6.4
3
+ Version: 0.6.5
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,6 +1,6 @@
1
1
  mathai/__init__.py,sha256=gzddzgNCG3Bg5BnqrDYvO71SzK4hu9fbo4nGEaDaG5Q,1554
2
2
  mathai/apart.py,sha256=VSS3khE9PNuxiRvdU5JDl4IN-KJBSIFjwR17pkhviXI,4197
3
- mathai/base.py,sha256=HSmUAUAVlKhjn-i3_IDoduyRzNrhw9kTHDgpUpahdK8,15660
3
+ mathai/base.py,sha256=aTyWgaQUmJ31OVEkm9K71JN02Pz0zk4rSta7xkdQK8g,15582
4
4
  mathai/bivariate_inequality.py,sha256=Da-A1kqVynR0tNOlEI7GSTf5T2vNkcF4etL9-EoyPJg,11415
5
5
  mathai/console.py,sha256=Sn58iwYE79MLEh67s8X3q6vZjw6g7f9XM1T8_dBBR2o,3048
6
6
  mathai/diff.py,sha256=RSTwlfeddvYXUDShCeRdcPjsmAS8Vf5OkYJAaUBPaiM,3060
@@ -12,9 +12,9 @@ mathai/inverse.py,sha256=ya7P8WjzfaAL3UXL7xqOh5GaIsXLDZ-F6lZFy3IEgaQ,2931
12
12
  mathai/limit.py,sha256=9F8i9UZh2xb-V8A5Sd1gdhDf9c2RFgpE1GdNn9MvbWI,5703
13
13
  mathai/linear.py,sha256=viGlPU8BPrjLWHlyNUvnfPHNH5d4ZBImiQMdyXaKGg0,5702
14
14
  mathai/logic.py,sha256=Ndz4Fd6aNCmzFlqoPyyIpSmV_BXmYHsurePjLyZJoNc,9809
15
- mathai/matrix.py,sha256=gExDBHre3f2JTkj63-58b2YKnlLhqVYszOaNTvG9maE,3927
15
+ mathai/matrix.py,sha256=a3SeAjrsg2wVKy-uETOGTCEClL3NMBxGsLOM15sHEwY,7177
16
16
  mathai/ode.py,sha256=zxxTXAOpt7oSsfpgI4vHsCWKXevmM96ZOBZWWs-vj8Y,4801
17
- mathai/parser.py,sha256=dhI4hRiYDdmSAOrhIWkBs_Boo2BFzC1m5cbAOOfHUas,7220
17
+ mathai/parser.py,sha256=YzXCW5MmwL3J0BhR0ZrPS2wnfz77uB_enBXPjITc-a0,7303
18
18
  mathai/parsetab.py,sha256=TL-4jvRM_Tx6ipwet8CFJc2DkjR4tGsbrGF_r4IC8xI,9651
19
19
  mathai/printeq.py,sha256=MKsR6-qXig80R07vLnFPYHQMeS41FrMVj3n3arrhJpQ,1329
20
20
  mathai/simplify.py,sha256=nvpwTLEgnGeXgc9NGGmQLAjKZ9c4XTdEVqAuo04oSrw,19588
@@ -22,7 +22,7 @@ mathai/structure.py,sha256=wrU7kqphSN7CqaVffyHHXD2-3t5My_Z_TtYFoUe_lTU,4099
22
22
  mathai/tool.py,sha256=ozcXTXLbKUnyPM9r9kz9M43YA2CBcWezcqLZfEs8rpc,6051
23
23
  mathai/trig.py,sha256=fnBbfiopcQzFg4ya1BoO5M0X_aCBnse2bjnKh1juw4I,11223
24
24
  mathai/univariate_inequality.py,sha256=LPFdWgC1y5zBwnsy1wwZxj-yP_SbqFDhCmTTzhuwoiY,16469
25
- mathai-0.6.4.dist-info/METADATA,sha256=vj85R6gzXafAT_M4LhpjwqOp_Ru32chy8M2SMiZGzaU,7103
26
- mathai-0.6.4.dist-info/WHEEL,sha256=qELbo2s1Yzl39ZmrAibXA2jjPLUYfnVhUNTlyF1rq0Y,92
27
- mathai-0.6.4.dist-info/top_level.txt,sha256=ROP4l3OhGYw3ihkQGASr18xM9GsK4z3_6whV5AyXLwE,7
28
- mathai-0.6.4.dist-info/RECORD,,
25
+ mathai-0.6.5.dist-info/METADATA,sha256=jLOXmfTrc1arF3RvTQdTST_-xoMdZJuakP21omUBwcs,7103
26
+ mathai-0.6.5.dist-info/WHEEL,sha256=qELbo2s1Yzl39ZmrAibXA2jjPLUYfnVhUNTlyF1rq0Y,92
27
+ mathai-0.6.5.dist-info/top_level.txt,sha256=ROP4l3OhGYw3ihkQGASr18xM9GsK4z3_6whV5AyXLwE,7
28
+ mathai-0.6.5.dist-info/RECORD,,
File without changes