mathai 0.6.9__py3-none-any.whl → 0.7.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/base.py CHANGED
@@ -370,21 +370,23 @@ def product(lst):
370
370
  for item in lst[1:]:
371
371
  s *= item
372
372
  return s
373
- def flatten_tree(node, add=[]):
373
+ def flatten_tree(node):
374
+ if node is None:
375
+ return None
374
376
  if not node.children:
375
377
  return node
376
378
  ad = []
377
379
  if node.name in ["f_add", "f_mul", "f_and", "f_or", "f_wmul"]:
378
380
  merged_children = []
379
381
  for child in node.children:
380
- flattened_child = flatten_tree(child, add)
382
+ flattened_child = flatten_tree(child)
381
383
  if flattened_child.name == node.name:
382
384
  merged_children.extend(flattened_child.children)
383
385
  else:
384
386
  merged_children.append(flattened_child)
385
387
  return TreeNode(node.name, merged_children)
386
388
  else:
387
- node.children = [flatten_tree(child, add) for child in node.children]
389
+ node.children = [flatten_tree(child) for child in node.children]
388
390
  return node
389
391
  def dowhile(eq, fx):
390
392
  if eq is None:
mathai/expand.py CHANGED
@@ -72,6 +72,84 @@ def expand_once(node):
72
72
 
73
73
  return node, False
74
74
 
75
+ def _expand2(equation):
76
+ """Iterative version of _expand without recursion."""
77
+ # Stack: (node, child_index, partially_processed_children)
78
+ stack = [(equation, 0, [])]
79
+
80
+ while stack:
81
+ node, child_index, processed_children = stack.pop()
82
+
83
+ # If all children are processed
84
+ if child_index >= len(node.children):
85
+ # Replace children with processed versions
86
+ node.children = processed_children
87
+
88
+ # === Handle f_pow ===
89
+ if node.name == "f_pow":
90
+ n = frac(node.children[1])
91
+ if n is not None and n.denominator == 1 and n.numerator > 1:
92
+ # Convert power to repeated multiplication
93
+ power_children = [node.children[0] for _ in range(n.numerator)]
94
+ new_node = TreeNode("f_mul", power_children)
95
+ # Flatten tree
96
+ node = flatten_tree(new_node)
97
+ # Push it back for further processing
98
+ stack.append((node, 0, []))
99
+ continue
100
+
101
+ # === Handle f_mul ===
102
+ elif node.name == "f_mul":
103
+ # Separate lone children and bracket children
104
+ lone_children = tree_form("d_1")
105
+ bracket_children = []
106
+
107
+ # Iterate in reverse (like original)
108
+ for child in reversed(node.children):
109
+ if child.name == "f_add":
110
+ bracket_children.append(child)
111
+ elif child.name == "f_pow" and child.children[0].name == "f_add":
112
+ n = frac(child.children[1])
113
+ if n is not None and n.denominator == 1 and n.numerator > 1:
114
+ for _ in range(n.numerator):
115
+ bracket_children.append(child.children[0])
116
+ else:
117
+ lone_children = lone_children * child
118
+ else:
119
+ lone_children = lone_children * child
120
+
121
+ lone_children = simplify(lone_children)
122
+
123
+ # Distribute bracket children over lone children iteratively
124
+ while bracket_children:
125
+ tmp = tree_form("d_0")
126
+ bracket = bracket_children.pop(0)
127
+ for bc in bracket.children:
128
+ if lone_children.name == "f_add":
129
+ for lc in lone_children.children:
130
+ tmp = tmp + bc * lc
131
+ else:
132
+ tmp = tmp + bc * lone_children
133
+ # Simplify after each distribution
134
+ lone_children = flatten_tree(simplify(tmp))
135
+
136
+ node = lone_children
137
+
138
+ # === Return node to parent ===
139
+ if stack:
140
+ parent, idx, parent_children = stack.pop()
141
+ parent_children.append(node)
142
+ stack.append((parent, idx + 1, parent_children))
143
+ else:
144
+ # Root node fully expanded
145
+ return node
146
+
147
+ else:
148
+ # Push current node back for next child
149
+ stack.append((node, child_index, processed_children))
150
+ # Push the child to process next
151
+ child = flatten_tree(node.children[child_index])
152
+ stack.append((child, 0, []))
75
153
 
76
154
  # =====================================================
77
155
  # Phase 3: Global fixed-point driver
@@ -79,6 +157,8 @@ def expand_once(node):
79
157
 
80
158
  def expand(eq):
81
159
  orig = TreeNode.matmul
160
+ if TreeNode.matmul is None:
161
+ return _expand2(eq)
82
162
  eq = simplify(eq)
83
163
  if TreeNode.matmul is not None:
84
164
  TreeNode.matmul = True
mathai/matrix.py CHANGED
@@ -215,7 +215,7 @@ def fold_wmul(root):
215
215
  return newnode[root]
216
216
 
217
217
  def flat(eq):
218
- return flatten_tree(eq, ["f_wmul"])
218
+ return flatten_tree(eq)
219
219
  def use(eq):
220
220
  return TreeNode(eq.name, [use(child) for child in eq.children])
221
221
  def _matrix_solve(eq):
mathai/parser.py CHANGED
@@ -154,7 +154,7 @@ def parse(equation, funclist=None):
154
154
  return tree_node
155
155
 
156
156
  tree_node = rfx(tree_node)
157
- tree_node = flatten_tree(tree_node, ["f_wmul"])
157
+ tree_node = flatten_tree(tree_node)
158
158
  if TreeNode.matmul == True:
159
159
  TreeNode.matmul = False
160
160
  tree_node = use(tree_form(str_form(tree_node).replace("f_w","f_")))
@@ -0,0 +1,293 @@
1
+ Metadata-Version: 2.4
2
+ Name: mathai
3
+ Version: 0.7.2
4
+ Summary: Mathematics solving Ai tailored to NCERT
5
+ Home-page: https://github.com/infinity390/mathai4
6
+ Requires-Python: >=3.7
7
+ Description-Content-Type: text/markdown
8
+ Requires-Dist: lark-parser
9
+ Dynamic: description
10
+ Dynamic: description-content-type
11
+ Dynamic: home-page
12
+ Dynamic: requires-dist
13
+ Dynamic: requires-python
14
+ Dynamic: summary
15
+
16
+ # Math AI Documentation
17
+ ## Source
18
+ Github repository of the code
19
+ https://github.com/infinity390/mathai4
20
+
21
+ ## Philosophy
22
+ I think it is a big realization in computer science and programming to realize that computers can solve mathematics.
23
+ This understanding should be made mainstream. It can help transform education, mathematical research, and computation of mathematical equations for work.
24
+
25
+ ## Societal Implications Of Such A Computer Program And The Author's Comment On Universities Of India
26
+ I think mathematics is valued by society because of education. Schools and universities teach them.
27
+ So this kind of software, if made mainstream, could bring real change.
28
+
29
+ ## The Summary Of How Computer "Solves" Math
30
+ Math equations are a tree data structure (`TreeNode` class).
31
+ We can manipulate the math equations using various algorithms (functions provided by the `mathai` library).
32
+ We first parse the math equation strings to get the tree data structure (`parse` function in `mathai`).
33
+
34
+ ## The Library
35
+ Import the library by doing:
36
+
37
+ ```python
38
+ from mathai import *
39
+ ```
40
+
41
+ ### str_form
42
+ It is the string representation of a `TreeNode` math equation.
43
+
44
+ #### Example
45
+ ```text
46
+ (cos(x)^2)+(sin(x)^2)
47
+ ```
48
+
49
+ Is represented internally as:
50
+
51
+ ```text
52
+ f_add
53
+ f_pow
54
+ f_cos
55
+ v_0
56
+ d_2
57
+ f_pow
58
+ f_sin
59
+ v_0
60
+ d_2
61
+ ```
62
+
63
+ #### Leaf Nodes
64
+
65
+ **Variables** (start with a `v_` prefix):
66
+
67
+ - `v_0` -> x
68
+ - `v_1` -> y
69
+ - `v_2` -> z
70
+ - `v_3` -> a
71
+
72
+ **Numbers** (start with `d_` prefix; only integers):
73
+
74
+ - `d_-1` -> -1
75
+ - `d_0` -> 0
76
+ - `d_1` -> 1
77
+ - `d_2` -> 2
78
+
79
+ #### Branch Nodes
80
+ - `f_add` -> addition
81
+ - `f_mul` -> multiplication
82
+ - `f_pow` -> power
83
+
84
+ ### parse
85
+ Takes a math equation string and outputs a `TreeNode` object.
86
+
87
+ ```python
88
+ from mathai import *
89
+
90
+ equation = parse("sin(x)^2+cos(x)^2")
91
+ print(equation)
92
+ ```
93
+
94
+ #### Output
95
+ ```text
96
+ (cos(x)^2)+(sin(x)^2)
97
+ ```
98
+
99
+ ### solve, simplify
100
+ It simplifies and cleans up a given math equation.
101
+
102
+ ```python
103
+ from mathai import *
104
+
105
+ equation = simplify(parse("(x+x+x+x-1-1-1-1)*(4*x-4)*sin(sin(x+x+x)*sin(3*x))"))
106
+ printeq(equation)
107
+ ```
108
+
109
+ #### Output
110
+ ```text
111
+ ((-4+(4*x))^2)*sin((sin((3*x))^2))
112
+ ```
113
+
114
+ ### Incomplete Documentation, Will be updated and completed later on
115
+
116
+ ### Demonstrations
117
+
118
+ #### Example Demonstration 1 (absolute value inequalities)
119
+ ```python
120
+ from mathai import *
121
+ question_list_from_lecture = [
122
+ "2*x/(2*x^2 + 5*x + 2) > 1/(x + 1)",
123
+ "(x + 2)*(x + 3)/((x - 2)*(x - 3)) <= 1",
124
+ "(5*x - 1) < (x + 1)^2 & (x + 1)^2 < 7*x - 3",
125
+ "(2*x - 1)/(2*x^3 + 3*x^2 + x) > 0",
126
+ "abs(x + 5)*x + 2*abs(x + 7) - 2 = 0",
127
+ "x*abs(x) - 5*abs(x + 2) + 6 = 0",
128
+ "x^2 - abs(x + 2) + x > 0",
129
+ "abs(abs(x - 2) - 3) <= 2",
130
+ "abs(3*x - 5) + abs(8 - x) = abs(3 + 2*x)",
131
+ "abs(x^2 + 5*x + 9) < abs(x^2 + 2*x + 2) + abs(3*x + 7)"
132
+ ]
133
+
134
+ for item in question_list_from_lecture:
135
+ eq = simplify(parse(item))
136
+ eq = dowhile(eq, absolute)
137
+ eq = simplify(factor1(fraction(eq)))
138
+ eq = prepare(eq)
139
+ eq = factor2(eq)
140
+ c = wavycurvy(eq & domain(eq)).fix()
141
+ print(c)
142
+ ```
143
+ #### Output
144
+
145
+ ```
146
+ (-2,-1)U(-(2/3),-(1/2))
147
+ (-inf,0)U(2,3)U{0}
148
+ (2,4)
149
+ (-inf,-1)U(-(1/2),0)U(1/2,+inf)
150
+ {-4,-3,-(3/2)-(sqrt(57)/2)}
151
+ {-1,(5/2)-(sqrt(89)/2),(5/2)+(sqrt(41)/2)}
152
+ (-inf,-sqrt(2))U((2*sqrt(2))/2,+inf)
153
+ (-3,1)U(3,7)U{1,-3,7,3}
154
+ (5/3,8)U{5/3,8}
155
+ (-inf,-(7/3))
156
+ ```
157
+
158
+ #### Example Demonstration 2 (trigonometry)
159
+ ```python
160
+ from mathai import *
161
+ def nested_func(eq_node):
162
+ eq_node = fraction(eq_node)
163
+ eq_node = simplify(eq_node)
164
+ eq_node = trig1(eq_node)
165
+ eq_node = trig0(eq_node)
166
+ return eq_node
167
+ for item in ["(cosec(x)-cot(x))^2=(1-cos(x))/(1+cos(x))", "cos(x)/(1+sin(x)) + (1+sin(x))/cos(x) = 2*sec(x)",\
168
+ "tan(x)/(1-cot(x)) + cot(x)/(1-tan(x)) = 1 + sec(x)*cosec(x)", "(1+sec(x))/sec(x) = sin(x)^2/(1-cos(x))",\
169
+ "(cos(x)-sin(x)+1)/(cos(x)+sin(x)-1) = cosec(x)+cot(x)"]:
170
+ eq = logic0(dowhile(parse(item), nested_func))
171
+ print(eq)
172
+ ```
173
+ #### Output
174
+
175
+ ```
176
+ true
177
+ true
178
+ true
179
+ true
180
+ true
181
+ ```
182
+
183
+ #### Example Demonstration 3 (integration)
184
+ ```python
185
+ from mathai import *
186
+
187
+ eq = simplify(parse("integrate(2*x/(x^2+1),x)"))
188
+ eq = integrate_const(eq)
189
+ eq = integrate_fraction(eq)
190
+ print(simplify(fraction(simplify(eq))))
191
+
192
+ eq = simplify(parse("integrate(sin(cos(x))*sin(x),x)"))
193
+ eq = integrate_subs(eq)
194
+ eq = integrate_const(eq)
195
+ eq = integrate_formula(eq)
196
+ eq = integrate_clean(eq)
197
+ print(simplify(eq))
198
+
199
+ eq = simplify(parse("integrate(x*sqrt(x+2),x)"))
200
+ eq = integrate_subs(eq)
201
+ eq = integrate_const(eq)
202
+ eq = integrate_formula(eq)
203
+ eq = expand(eq)
204
+ eq = integrate_const(eq)
205
+ eq = integrate_summation(eq)
206
+ eq = simplify(eq)
207
+ eq = integrate_const(eq)
208
+ eq = integrate_formula(eq)
209
+ eq = integrate_clean(eq)
210
+ print(simplify(fraction(simplify(eq))))
211
+
212
+ eq = simplify(parse("integrate(x/(e^(x^2)),x)"))
213
+ eq = integrate_subs(eq)
214
+ eq = integrate_const(eq)
215
+ eq = integrate_formula(eq)
216
+ eq = simplify(eq)
217
+ eq = integrate_formula(eq)
218
+ eq = integrate_clean(eq)
219
+ print(simplify(eq))
220
+
221
+ eq = fraction(trig0(trig1(simplify(parse("integrate(sin(x)^4,x)")))))
222
+ eq = integrate_const(eq)
223
+ eq = integrate_summation(eq)
224
+ eq = integrate_formula(eq)
225
+ eq = integrate_const(eq)
226
+ eq = integrate_formula(eq)
227
+ print(factor0(simplify(fraction(simplify(eq)))))
228
+ ```
229
+ #### Output
230
+
231
+ ```
232
+ log(abs((1+(x^2))))
233
+ cos(cos(x))
234
+ ((6*((2+x)^(5/2)))-(20*((2+x)^(3/2))))/15
235
+ -((e^-(x^2))/2)
236
+ -(((8*sin((2*x)))-(12*x)-sin((4*x)))/32)
237
+ ```
238
+
239
+ #### Example Demonstration 4 (derivation of hydrogen atom's ground state energy in electron volts using the variational principle in quantum physics)
240
+ ```python
241
+ from mathai import *;
242
+ def auto_integration(eq):
243
+ for _ in range(3):
244
+ eq=dowhile(integrate_subs(eq),lambda x:integrate_summation(integrate_const(integrate_formula(simplify(expand(x))))));
245
+ out=integrate_clean(copy.deepcopy(eq));
246
+ if "f_integrate" not in str_form(out):return dowhile(out,lambda x:simplify(fraction(x)));
247
+ eq=integrate_byparts(eq);
248
+ return eq;
249
+ z,k,m,e1,hbar=map(lambda s:simplify(parse(s)),["1","8987551787","9109383701*10^(-40)","1602176634*10^(-28)","1054571817*10^(-43)"]);
250
+ pi,euler,r=tree_form("s_pi"),tree_form("s_e"),parse("r");a0=hbar**2/(k*e1**2*m);psi=((z**3/(pi*a0**3)).fx("sqrt"))*euler**(-(z/a0)*r);
251
+ laplace_psi=diff(r**2*diff(psi,r.name),r.name)/r**2;V=-(k*z*e1**2)/r;Hpsi=-hbar**2/(2*m)*laplace_psi+V*psi;
252
+ norm=lambda f:simplify(
253
+ limit3(limit2(expand(TreeNode("f_limitpinf",[auto_integration(TreeNode("f_integrate",[f*parse("4")*pi*r**2,r])),r]))))
254
+ -limit1(TreeNode("f_limit",[auto_integration(TreeNode("f_integrate",[f*parse("4")*pi*r**2,r])),r]))
255
+ );
256
+ print(compute(norm(psi*Hpsi)/(norm(psi**2)*e1)));
257
+ ```
258
+ #### Output
259
+
260
+ ```
261
+ -13.605693122882867
262
+ ```
263
+
264
+ #### Example Demonstration 5 (boolean algebra)
265
+ ```python
266
+ from mathai import *
267
+ print(logic_n(simplify(parse("~(p<->q)<->(~p<->q)"))))
268
+ print(logic_n(simplify(parse("(p->q)<->(~q->~p)"))))
269
+ ```
270
+ #### Output
271
+
272
+ ```
273
+ true
274
+ true
275
+ ```
276
+
277
+ #### Example Demonstration 6 (limits)
278
+ ```python
279
+ from mathai import *
280
+ limits = ["(e^(tan(x)) - 1 - tan(x)) / x^2", "sin(x)/x", "(1-cos(x))/x^2", "(sin(x)-x)/sin(x)^3"]
281
+ for q in limits:
282
+ q = fraction(simplify(TreeNode("f_limit",[parse(q),parse("x")])))
283
+ q = limit1(q)
284
+ print(q)
285
+ ```
286
+ #### Output
287
+
288
+ ```
289
+ 1/2
290
+ 1
291
+ 1/2
292
+ -(1/6)
293
+ ```
@@ -1,10 +1,10 @@
1
1
  mathai/__init__.py,sha256=Mv0vNdV-FHKS6MzAsnHsE0eBtSkCUq0nc9tUgCWxbFo,1541
2
2
  mathai/apart.py,sha256=VSS3khE9PNuxiRvdU5JDl4IN-KJBSIFjwR17pkhviXI,4197
3
- mathai/base.py,sha256=mk5LTVcgOpd-Zh0VBGQmZQOBlH5G3XFQI_TzwXYaRss,15794
3
+ mathai/base.py,sha256=hRiJWS94asWl-t11eGrVCEhZqIub0T-8S_Eqmlun6mI,15819
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
7
- mathai/expand.py,sha256=NCe4LfM3-ndW78Zg8ZjlEVujb3-lG7_0H81ncRQizvw,2760
7
+ mathai/expand.py,sha256=r2Ha6graDLgLOlx3ox4egUvJD5L_-4_n-FQs2wyWVKg,6284
8
8
  mathai/factor.py,sha256=3wcmZOGUqMlLj4v2DA14ZLqEQ7khavOi7PjZJU6VX40,12494
9
9
  mathai/fraction.py,sha256=88xvRpDGfFi8tbe1QIyejdSP91HcErrN4VS2MxzbhrY,4392
10
10
  mathai/integrate.py,sha256=C_lqYgQN4UiriCb_LDkpwtKx7XJhp_K8T9skCkxWqas,17208
@@ -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=Owv7Xbn2emrSWqrvaqSHeWX5xPuQF5tkZICCuTbQAyQ,7288
15
+ mathai/matrix.py,sha256=sIusSmZN0Y6k4TovI5L6Hepls9i7FPRjA3CZIU4X4_w,7276
16
16
  mathai/ode.py,sha256=zxxTXAOpt7oSsfpgI4vHsCWKXevmM96ZOBZWWs-vj8Y,4801
17
- mathai/parser.py,sha256=nEAdSSJeapBOhvjbkXJFtCEBU8izajXZAITV8rbslic,7314
17
+ mathai/parser.py,sha256=3cBl7DhqL275gqXszET5zC85O9v3XKxRPc2EaOigAns,7302
18
18
  mathai/parsetab.py,sha256=TL-4jvRM_Tx6ipwet8CFJc2DkjR4tGsbrGF_r4IC8xI,9651
19
19
  mathai/printeq.py,sha256=4UgLJo-vV_YlVw_3QUQY_jQMHrFnG-ZKAyVZsd7yD6o,1450
20
20
  mathai/simplify.py,sha256=VsUw89U3FqSpD3mMUGM0pr0dZ__MDQOBD8maaB7MDBY,19973
@@ -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.9.dist-info/METADATA,sha256=6Q_imbUXwELM1E1VZap8NoO2fm9SFAhalIoi0nWsULg,7103
26
- mathai-0.6.9.dist-info/WHEEL,sha256=qELbo2s1Yzl39ZmrAibXA2jjPLUYfnVhUNTlyF1rq0Y,92
27
- mathai-0.6.9.dist-info/top_level.txt,sha256=ROP4l3OhGYw3ihkQGASr18xM9GsK4z3_6whV5AyXLwE,7
28
- mathai-0.6.9.dist-info/RECORD,,
25
+ mathai-0.7.2.dist-info/METADATA,sha256=e4tsi5hAzJaj4cqRDXewQl3SueJlmBt8ePY3Jvxbwj4,7742
26
+ mathai-0.7.2.dist-info/WHEEL,sha256=wUyA8OaulRlbfwMtmQsvNngGrxQHAvkKcvRmdizlJi0,92
27
+ mathai-0.7.2.dist-info/top_level.txt,sha256=ROP4l3OhGYw3ihkQGASr18xM9GsK4z3_6whV5AyXLwE,7
28
+ mathai-0.7.2.dist-info/RECORD,,
@@ -1,5 +1,5 @@
1
1
  Wheel-Version: 1.0
2
- Generator: setuptools (80.10.1)
2
+ Generator: setuptools (80.10.2)
3
3
  Root-Is-Purelib: true
4
4
  Tag: py3-none-any
5
5
 
@@ -1,234 +0,0 @@
1
- Metadata-Version: 2.4
2
- Name: mathai
3
- Version: 0.6.9
4
- Summary: Mathematics solving Ai tailored to NCERT
5
- Home-page: https://github.com/infinity390/mathai4
6
- Author: educated indians are having a low iq and are good for nothing
7
- Requires-Python: >=3.7
8
- Description-Content-Type: text/markdown
9
- Requires-Dist: lark-parser
10
- Dynamic: author
11
- Dynamic: description
12
- Dynamic: description-content-type
13
- Dynamic: home-page
14
- Dynamic: requires-dist
15
- Dynamic: requires-python
16
- Dynamic: summary
17
-
18
- # Math AI Documentation
19
- ## Source
20
- Github repository of the code
21
- https://github.com/infinity390/mathai4
22
-
23
- ## Philosophy
24
- I think it is a big realization in computer science and programming to realize that computers can solve mathematics.
25
- This understanding should be made mainstream. It can help transform education, mathematical research, and computation of mathematical equations for work.
26
-
27
- ## Societal Implications Of Such A Computer Program And The Author's Comment On Universities Of India
28
- I think mathematics is valued by society because of education. Schools and universities teach them.
29
- So this kind of software, if made mainstream, could bring real change.
30
-
31
- ### The Author's Comments On The Universities In His Country
32
- > Educated Indians are having a low IQ and are good for nothing.
33
- > The Indian Institute of Technology (IITs) graduates are the leader of the fools.
34
- > Every educated Indian is beneath me.
35
- > Now learn how this Python library can solve the math questions of your exams.
36
-
37
- ## The Summary Of How Computer "Solves" Math
38
- Math equations are a tree data structure (`TreeNode` class).
39
- We can manipulate the math equations using various algorithms (functions provided by the `mathai` library).
40
- We first parse the math equation strings to get the tree data structure (`parse` function in `mathai`).
41
-
42
- ## The Library
43
- Import the library by doing:
44
-
45
- ```python
46
- from mathai import *
47
- ```
48
-
49
- ### str_form
50
- It is the string representation of a `TreeNode` math equation.
51
-
52
- #### Example
53
- ```text
54
- (cos(x)^2)+(sin(x)^2)
55
- ```
56
-
57
- Is represented internally as:
58
-
59
- ```text
60
- f_add
61
- f_pow
62
- f_cos
63
- v_0
64
- d_2
65
- f_pow
66
- f_sin
67
- v_0
68
- d_2
69
- ```
70
-
71
- #### Leaf Nodes
72
-
73
- **Variables** (start with a `v_` prefix):
74
-
75
- - `v_0` -> x
76
- - `v_1` -> y
77
- - `v_2` -> z
78
- - `v_3` -> a
79
-
80
- **Numbers** (start with `d_` prefix; only integers):
81
-
82
- - `d_-1` -> -1
83
- - `d_0` -> 0
84
- - `d_1` -> 1
85
- - `d_2` -> 2
86
-
87
- #### Branch Nodes
88
- - `f_add` -> addition
89
- - `f_mul` -> multiplication
90
- - `f_pow` -> power
91
-
92
- ### parse
93
- Takes a math equation string and outputs a `TreeNode` object.
94
-
95
- ```python
96
- from mathai import *
97
-
98
- equation = parse("sin(x)^2+cos(x)^2")
99
- print(equation)
100
- ```
101
-
102
- #### Output
103
- ```text
104
- (cos(x)^2)+(sin(x)^2)
105
- ```
106
-
107
- ### printeq, printeq_str, printeq_log
108
- Prints math equations in a more readable form than usual `print`.
109
-
110
- ```python
111
- from mathai import *
112
-
113
- equation = simplify(parse("(x+1)/x"))
114
- print(equation)
115
- printeq(equation)
116
- ```
117
-
118
- #### Output
119
- ```text
120
- (1+x)*(x^-1)
121
- (1+x)/x
122
- ```
123
-
124
- ### solve, simplify
125
- `simplify` performs what `solve` does and more.
126
- It simplifies and cleans up a given math equation.
127
-
128
- ```python
129
- from mathai import *
130
-
131
- equation = simplify(parse("(x+x+x+x-1-1-1-1)*(4*x-4)*sin(sin(x+x+x)*sin(3*x))"))
132
- printeq(equation)
133
- ```
134
-
135
- #### Output
136
- ```text
137
- ((-4+(4*x))^2)*sin((sin((3*x))^2))
138
- ```
139
-
140
- ### Incomplete Documentation, Will be updated and completed later on
141
-
142
- ### Example Demonstration [limits questions can also be solved other than this these, try limit()]
143
- ![pip-install-mathai-mathematics-solving-ai-system-in-python-v0-xcg3c22k51sf1](https://github.com/user-attachments/assets/799f576f-27d0-4d7c-86e9-ad55ff221bcc)
144
- ```python
145
- import sys, os, time
146
- from mathai import *
147
-
148
- sys.setrecursionlimit(10000)
149
-
150
- def integration_byparts(item): return simplify(fraction(simplify(byparts(simplify(parse(item)))[0])))
151
- def integration_apart(item): return simplify(fraction(integrate(apart(factor2(simplify(parse(item)))))[0]))
152
- def integration_direct(item): return simplify(fraction(simplify(integrate(simplify(parse(item)))[0])))
153
- def integration_trig(item): return simplify(trig0(integrate(trig1(simplify(parse(item))))[0]))
154
- def algebra(item): return logic0(simplify(expand(simplify(parse(item)))))
155
- def trig_basic(item): return logic0(simplify(expand(trig3(simplify(parse(item))))))
156
- def trig_advanced(item): return logic0(simplify(trig0(trig1(trig4(simplify(fraction(trig0(simplify(parse(item))))))))))
157
-
158
- all_tasks = [
159
- *[(item, trig_advanced) for item in [
160
- "cos(x)/(1+sin(x)) + (1+sin(x))/cos(x) = 2*sec(x)",
161
- "(1+sec(x))/sec(x) = sin(x)^2/(1-cos(x))"]],
162
- *[(item, integration_byparts) for item in ["sin(x)*x","x*sin(3*x)","x*log(abs(x))","arctan(x)"]],
163
- *[(item, integration_apart) for item in ["x/((x+1)*(x+2))","1/(x^2-9)"]],
164
- *[(item, integration_direct) for item in [
165
- "x*sqrt(x+2)","sin(cos(x))*sin(x)","2*x/(1+x^2)","sqrt(a*x+b)","cos(sqrt(x))/sqrt(x)","e^(arctan(x))/(1+x^2)","sqrt(sin(2*x))*cos(2*x"]],
166
- *[(item, integration_trig) for item in ["sin(2*x+5)^2","sin(x)^4","cos(2*x)^4"]],
167
- *[(item, algebra) for item in ["(x+1)^2 = x^2+2*x+1","(x+1)*(x-1) = x^2-1"]],
168
- *[(item, trig_basic) for item in ["2*sin(x)*cos(x)=sin(2*x)"]],
169
- ]
170
-
171
- def run_task(task):
172
- item, func = task
173
- try: result = func(item)
174
- except Exception as e: result = str(e)
175
- return item, result
176
-
177
- if __name__=="__main__":
178
- print(f"Solving {len(all_tasks)} math questions...\n")
179
- start_time = time.time()
180
- for task in all_tasks:
181
- item, result = run_task(task)
182
- print(f"{item} => {result}\n")
183
- print(f"All tasks completed in {time.time()-start_time:.2f} seconds")
184
- ```
185
- ### Output
186
-
187
- ```
188
- Running 21 tasks asynchronously on 8 cores...
189
-
190
- x*log(abs(x)) => ((-2*(x^2))+(4*log(abs(x))*(x^2)))*(8^-1)
191
-
192
- arctan(x) => (log((abs((1+(x^2)))^-1))+(2*arctan(x)*x))*(2^-1)
193
-
194
- sin(cos(x))*sin(x) => cos(cos(x))
195
-
196
- 1/(x^2-9) => (log(abs((-3+x)))+log((abs((3+x))^-1)))*(6^-1)
197
-
198
- x/((x+1)*(x+2)) => log((abs((1+x))^-1))+log(((2+x)^2))
199
-
200
- x*sin(3*x) => ((-9*cos((3*x))*x)+(3*sin((3*x))))*(27^-1)
201
-
202
- (1+sec(x))/sec(x) = sin(x)^2/(1-cos(x)) => true
203
-
204
- e^(arctan(x))/(1+x^2) => e^arctan(x)
205
-
206
- cos(sqrt(x))/sqrt(x) => 2*sin((x^(2^-1)))
207
-
208
- sqrt(a*x+b) => 2*(3^-1)*(((x*a)+b)^(3*(2^-1)))*(a^-1)
209
-
210
- sin(x)*x => (-1*cos(x)*x)+sin(x)
211
-
212
- (x+1)^2 = x^2+2*x+1 => true
213
-
214
- (x+1)*(x-1) = x^2-1 => true
215
-
216
- cos(x)/(1+sin(x)) + (1+sin(x))/cos(x) = 2*sec(x) => true
217
-
218
- 2*sin(x)*cos(x)=sin(2*x) => true
219
-
220
- sqrt(sin(2*x))*cos(2*x) => (3^-1)*(sin((2*x))^(3*(2^-1)))
221
-
222
- 2*x/(1+x^2) => log(abs((1+(x^2))))
223
-
224
- sin(2*x+5)^2 => ((-1*(4^-1)*sin((10+(4*x))))+x)*(2^-1)
225
-
226
- cos(2*x)^4 => ((4^-1)*x)+((64^-1)*sin((8*x)))+((8^-1)*sin((4*x)))+((8^-1)*x)
227
-
228
- x*sqrt(x+2) => ((-1*(4^-1)*((2+x)^(2+(2^-1))))+(-2*((2+x)^(2+(2^-1))))+(5*((2+x)^(1+(2^-1)))*x)+((2^-1)*((2+x)^(1+(2^-1)))*x)+((8^-1)*((2+x)^(1+(2^-1)))*x))*((1+(2^-1))^-3)*((2+(2^-1))^-1)
229
-
230
- sin(x)^4 => (-1*(4^-1)*sin((2*x)))+((32^-1)*sin((4*x)))+((4^-1)*x)+((8^-1)*x)
231
-
232
- All tasks completed in 129.78 seconds
233
- ```
234
-