mathai 0.2.0__tar.gz → 0.2.2__tar.gz

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.
Files changed (31) hide show
  1. mathai-0.2.2/PKG-INFO +231 -0
  2. mathai-0.2.2/README.md +214 -0
  3. {mathai-0.2.0 → mathai-0.2.2}/mathai/__init__.py +1 -0
  4. {mathai-0.2.0 → mathai-0.2.2}/mathai/base.py +2 -0
  5. {mathai-0.2.0 → mathai-0.2.2}/mathai/expand.py +2 -0
  6. mathai-0.2.2/mathai/limit.py +126 -0
  7. {mathai-0.2.0 → mathai-0.2.2}/mathai/simplify.py +16 -2
  8. {mathai-0.2.0 → mathai-0.2.2}/mathai/structure.py +2 -1
  9. {mathai-0.2.0 → mathai-0.2.2}/mathai/trig.py +2 -0
  10. mathai-0.2.2/mathai.egg-info/PKG-INFO +231 -0
  11. {mathai-0.2.0 → mathai-0.2.2}/mathai.egg-info/SOURCES.txt +1 -0
  12. {mathai-0.2.0 → mathai-0.2.2}/setup.py +1 -1
  13. mathai-0.2.0/PKG-INFO +0 -24
  14. mathai-0.2.0/README.md +0 -7
  15. mathai-0.2.0/mathai.egg-info/PKG-INFO +0 -24
  16. {mathai-0.2.0 → mathai-0.2.2}/mathai/apart.py +0 -0
  17. {mathai-0.2.0 → mathai-0.2.2}/mathai/console.py +0 -0
  18. {mathai-0.2.0 → mathai-0.2.2}/mathai/diff.py +0 -0
  19. {mathai-0.2.0 → mathai-0.2.2}/mathai/factor.py +0 -0
  20. {mathai-0.2.0 → mathai-0.2.2}/mathai/fraction.py +0 -0
  21. {mathai-0.2.0 → mathai-0.2.2}/mathai/integrate.py +0 -0
  22. {mathai-0.2.0 → mathai-0.2.2}/mathai/inverse.py +0 -0
  23. {mathai-0.2.0 → mathai-0.2.2}/mathai/linear.py +0 -0
  24. {mathai-0.2.0 → mathai-0.2.2}/mathai/logic.py +0 -0
  25. {mathai-0.2.0 → mathai-0.2.2}/mathai/parser.py +0 -0
  26. {mathai-0.2.0 → mathai-0.2.2}/mathai/printeq.py +0 -0
  27. {mathai-0.2.0 → mathai-0.2.2}/mathai/tool.py +0 -0
  28. {mathai-0.2.0 → mathai-0.2.2}/mathai.egg-info/dependency_links.txt +0 -0
  29. {mathai-0.2.0 → mathai-0.2.2}/mathai.egg-info/requires.txt +0 -0
  30. {mathai-0.2.0 → mathai-0.2.2}/mathai.egg-info/top_level.txt +0 -0
  31. {mathai-0.2.0 → mathai-0.2.2}/setup.cfg +0 -0
mathai-0.2.2/PKG-INFO ADDED
@@ -0,0 +1,231 @@
1
+ Metadata-Version: 2.2
2
+ Name: mathai
3
+ Version: 0.2.2
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
+
20
+ ## Philosophy
21
+ I think it is a big realization in computer science and programming to realize that computers can solve mathematics.
22
+ This understanding should be made mainstream. It can help transform education, mathematical research, and computation of mathematical equations for work.
23
+
24
+ ## Societal Implications Of Such A Computer Program And The Author's Comment On Universities Of India
25
+ I think mathematics is valued by society because of education. Schools and universities teach them.
26
+ So this kind of software, if made mainstream, could bring real change.
27
+
28
+ ### The Author's Comments On The Universities In His Country
29
+ > Educated Indians are having a low IQ and are good for nothing.
30
+ > The Indian Institute of Technology (IITs) graduates are the leader of the fools.
31
+ > Every educated Indian is beneath me.
32
+ > Now learn how this Python library can solve the math questions of your exams.
33
+
34
+ ## The Summary Of How Computer "Solves" Math
35
+ Math equations are a tree data structure (`TreeNode` class).
36
+ We can manipulate the math equations using various algorithms (functions provided by the `mathai` library).
37
+ We first parse the math equation strings to get the tree data structure (`parse` function in `mathai`).
38
+
39
+ ## The Library
40
+ Import the library by doing:
41
+
42
+ ```python
43
+ from mathai import *
44
+ ```
45
+
46
+ ### str_form
47
+ It is the string representation of a `TreeNode` math equation.
48
+
49
+ #### Example
50
+ ```text
51
+ (cos(x)^2)+(sin(x)^2)
52
+ ```
53
+
54
+ Is represented internally as:
55
+
56
+ ```text
57
+ f_add
58
+ f_pow
59
+ f_cos
60
+ v_0
61
+ d_2
62
+ f_pow
63
+ f_sin
64
+ v_0
65
+ d_2
66
+ ```
67
+
68
+ #### Leaf Nodes
69
+
70
+ **Variables** (start with a `v_` prefix):
71
+
72
+ - `v_0` → x
73
+ - `v_1` → y
74
+ - `v_2` → z
75
+ - `v_3` → a
76
+
77
+ **Numbers** (start with `d_` prefix; only integers):
78
+
79
+ - `d_-1` → -1
80
+ - `d_0` → 0
81
+ - `d_1` → 1
82
+ - `d_2` → 2
83
+
84
+ #### Branch Nodes
85
+ - `f_add` → addition
86
+ - `f_mul` → multiplication
87
+ - `f_pow` → power
88
+
89
+ ### parse
90
+ Takes a math equation string and outputs a `TreeNode` object.
91
+
92
+ ```python
93
+ from mathai import *
94
+
95
+ equation = parse("sin(x)^2+cos(x)^2")
96
+ print(equation)
97
+ ```
98
+
99
+ #### Output
100
+ ```text
101
+ (cos(x)^2)+(sin(x)^2)
102
+ ```
103
+
104
+ ### printeq, printeq_str, printeq_log
105
+ Prints math equations in a more readable form than usual `print`.
106
+
107
+ ```python
108
+ from mathai import *
109
+
110
+ equation = simplify(parse("(x+1)/x"))
111
+ print(equation)
112
+ printeq(equation)
113
+ ```
114
+
115
+ #### Output
116
+ ```text
117
+ (1+x)*(x^-1)
118
+ (1+x)/x
119
+ ```
120
+
121
+ ### solve, simplify
122
+ `simplify` performs what `solve` does and more.
123
+ It simplifies and cleans up a given math equation.
124
+
125
+ ```python
126
+ from mathai import *
127
+
128
+ equation = simplify(parse("(x+x+x+x-1-1-1-1)*(4*x-4)*sin(sin(x+x+x)*sin(3*x))"))
129
+ printeq(equation)
130
+ ```
131
+
132
+ #### Output
133
+ ```text
134
+ ((-4+(4*x))^2)*sin((sin((3*x))^2))
135
+ ```
136
+
137
+ ### Incomplete Documentation, Will be updated and completed later on
138
+
139
+ ### Example Demonstration [limits questions can also be solved other than this these, try limit()]
140
+ ![pip-install-mathai-mathematics-solving-ai-system-in-python-v0-xcg3c22k51sf1](https://github.com/user-attachments/assets/799f576f-27d0-4d7c-86e9-ad55ff221bcc)
141
+ ```python
142
+ import sys, os, time
143
+ from mathai import *
144
+
145
+ sys.setrecursionlimit(10000)
146
+
147
+ def integration_byparts(item): return simplify(fraction(simplify(byparts(simplify(parse(item)))[0])))
148
+ def integration_apart(item): return simplify(fraction(integrate(apart(factor2(simplify(parse(item)))))[0]))
149
+ def integration_direct(item): return simplify(fraction(simplify(integrate(simplify(parse(item)))[0])))
150
+ def integration_trig(item): return simplify(trig0(integrate(trig1(simplify(parse(item))))[0]))
151
+ def algebra(item): return logic0(simplify(expand(simplify(parse(item)))))
152
+ def trig_basic(item): return logic0(simplify(expand(trig3(simplify(parse(item))))))
153
+ def trig_advanced(item): return logic0(simplify(trig0(trig1(trig4(simplify(fraction(trig0(simplify(parse(item))))))))))
154
+
155
+ all_tasks = [
156
+ *[(item, trig_advanced) for item in [
157
+ "cos(x)/(1+sin(x)) + (1+sin(x))/cos(x) = 2*sec(x)",
158
+ "(1+sec(x))/sec(x) = sin(x)^2/(1-cos(x))"]],
159
+ *[(item, integration_byparts) for item in ["sin(x)*x","x*sin(3*x)","x*log(abs(x))","arctan(x)"]],
160
+ *[(item, integration_apart) for item in ["x/((x+1)*(x+2))","1/(x^2-9)"]],
161
+ *[(item, integration_direct) for item in [
162
+ "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"]],
163
+ *[(item, integration_trig) for item in ["sin(2*x+5)^2","sin(x)^4","cos(2*x)^4"]],
164
+ *[(item, algebra) for item in ["(x+1)^2 = x^2+2*x+1","(x+1)*(x-1) = x^2-1"]],
165
+ *[(item, trig_basic) for item in ["2*sin(x)*cos(x)=sin(2*x)"]],
166
+ ]
167
+
168
+ def run_task(task):
169
+ item, func = task
170
+ try: result = func(item)
171
+ except Exception as e: result = str(e)
172
+ return item, result
173
+
174
+ if __name__=="__main__":
175
+ print(f"Solving {len(all_tasks)} math questions...\n")
176
+ start_time = time.time()
177
+ for task in all_tasks:
178
+ item, result = run_task(task)
179
+ print(f"{item} => {result}\n")
180
+ print(f"All tasks completed in {time.time()-start_time:.2f} seconds")
181
+ ```
182
+ ### Output
183
+
184
+ ```
185
+ Running 21 tasks asynchronously on 8 cores...
186
+
187
+ x*log(abs(x)) => ((-2*(x^2))+(4*log(abs(x))*(x^2)))*(8^-1)
188
+
189
+ arctan(x) => (log((abs((1+(x^2)))^-1))+(2*arctan(x)*x))*(2^-1)
190
+
191
+ sin(cos(x))*sin(x) => cos(cos(x))
192
+
193
+ 1/(x^2-9) => (log(abs((-3+x)))+log((abs((3+x))^-1)))*(6^-1)
194
+
195
+ x/((x+1)*(x+2)) => log((abs((1+x))^-1))+log(((2+x)^2))
196
+
197
+ x*sin(3*x) => ((-9*cos((3*x))*x)+(3*sin((3*x))))*(27^-1)
198
+
199
+ (1+sec(x))/sec(x) = sin(x)^2/(1-cos(x)) => true
200
+
201
+ e^(arctan(x))/(1+x^2) => e^arctan(x)
202
+
203
+ cos(sqrt(x))/sqrt(x) => 2*sin((x^(2^-1)))
204
+
205
+ sqrt(a*x+b) => 2*(3^-1)*(((x*a)+b)^(3*(2^-1)))*(a^-1)
206
+
207
+ sin(x)*x => (-1*cos(x)*x)+sin(x)
208
+
209
+ (x+1)^2 = x^2+2*x+1 => true
210
+
211
+ (x+1)*(x-1) = x^2-1 => true
212
+
213
+ cos(x)/(1+sin(x)) + (1+sin(x))/cos(x) = 2*sec(x) => true
214
+
215
+ 2*sin(x)*cos(x)=sin(2*x) => true
216
+
217
+ sqrt(sin(2*x))*cos(2*x) => (3^-1)*(sin((2*x))^(3*(2^-1)))
218
+
219
+ 2*x/(1+x^2) => log(abs((1+(x^2))))
220
+
221
+ sin(2*x+5)^2 => ((-1*(4^-1)*sin((10+(4*x))))+x)*(2^-1)
222
+
223
+ cos(2*x)^4 => ((4^-1)*x)+((64^-1)*sin((8*x)))+((8^-1)*sin((4*x)))+((8^-1)*x)
224
+
225
+ 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)
226
+
227
+ sin(x)^4 => (-1*(4^-1)*sin((2*x)))+((32^-1)*sin((4*x)))+((4^-1)*x)+((8^-1)*x)
228
+
229
+ All tasks completed in 129.78 seconds
230
+ ```
231
+
mathai-0.2.2/README.md ADDED
@@ -0,0 +1,214 @@
1
+ # Math AI Documentation
2
+
3
+ ## Philosophy
4
+ I think it is a big realization in computer science and programming to realize that computers can solve mathematics.
5
+ This understanding should be made mainstream. It can help transform education, mathematical research, and computation of mathematical equations for work.
6
+
7
+ ## Societal Implications Of Such A Computer Program And The Author's Comment On Universities Of India
8
+ I think mathematics is valued by society because of education. Schools and universities teach them.
9
+ So this kind of software, if made mainstream, could bring real change.
10
+
11
+ ### The Author's Comments On The Universities In His Country
12
+ > Educated Indians are having a low IQ and are good for nothing.
13
+ > The Indian Institute of Technology (IITs) graduates are the leader of the fools.
14
+ > Every educated Indian is beneath me.
15
+ > Now learn how this Python library can solve the math questions of your exams.
16
+
17
+ ## The Summary Of How Computer "Solves" Math
18
+ Math equations are a tree data structure (`TreeNode` class).
19
+ We can manipulate the math equations using various algorithms (functions provided by the `mathai` library).
20
+ We first parse the math equation strings to get the tree data structure (`parse` function in `mathai`).
21
+
22
+ ## The Library
23
+ Import the library by doing:
24
+
25
+ ```python
26
+ from mathai import *
27
+ ```
28
+
29
+ ### str_form
30
+ It is the string representation of a `TreeNode` math equation.
31
+
32
+ #### Example
33
+ ```text
34
+ (cos(x)^2)+(sin(x)^2)
35
+ ```
36
+
37
+ Is represented internally as:
38
+
39
+ ```text
40
+ f_add
41
+ f_pow
42
+ f_cos
43
+ v_0
44
+ d_2
45
+ f_pow
46
+ f_sin
47
+ v_0
48
+ d_2
49
+ ```
50
+
51
+ #### Leaf Nodes
52
+
53
+ **Variables** (start with a `v_` prefix):
54
+
55
+ - `v_0` → x
56
+ - `v_1` → y
57
+ - `v_2` → z
58
+ - `v_3` → a
59
+
60
+ **Numbers** (start with `d_` prefix; only integers):
61
+
62
+ - `d_-1` → -1
63
+ - `d_0` → 0
64
+ - `d_1` → 1
65
+ - `d_2` → 2
66
+
67
+ #### Branch Nodes
68
+ - `f_add` → addition
69
+ - `f_mul` → multiplication
70
+ - `f_pow` → power
71
+
72
+ ### parse
73
+ Takes a math equation string and outputs a `TreeNode` object.
74
+
75
+ ```python
76
+ from mathai import *
77
+
78
+ equation = parse("sin(x)^2+cos(x)^2")
79
+ print(equation)
80
+ ```
81
+
82
+ #### Output
83
+ ```text
84
+ (cos(x)^2)+(sin(x)^2)
85
+ ```
86
+
87
+ ### printeq, printeq_str, printeq_log
88
+ Prints math equations in a more readable form than usual `print`.
89
+
90
+ ```python
91
+ from mathai import *
92
+
93
+ equation = simplify(parse("(x+1)/x"))
94
+ print(equation)
95
+ printeq(equation)
96
+ ```
97
+
98
+ #### Output
99
+ ```text
100
+ (1+x)*(x^-1)
101
+ (1+x)/x
102
+ ```
103
+
104
+ ### solve, simplify
105
+ `simplify` performs what `solve` does and more.
106
+ It simplifies and cleans up a given math equation.
107
+
108
+ ```python
109
+ from mathai import *
110
+
111
+ equation = simplify(parse("(x+x+x+x-1-1-1-1)*(4*x-4)*sin(sin(x+x+x)*sin(3*x))"))
112
+ printeq(equation)
113
+ ```
114
+
115
+ #### Output
116
+ ```text
117
+ ((-4+(4*x))^2)*sin((sin((3*x))^2))
118
+ ```
119
+
120
+ ### Incomplete Documentation, Will be updated and completed later on
121
+
122
+ ### Example Demonstration [limits questions can also be solved other than this these, try limit()]
123
+ ![pip-install-mathai-mathematics-solving-ai-system-in-python-v0-xcg3c22k51sf1](https://github.com/user-attachments/assets/799f576f-27d0-4d7c-86e9-ad55ff221bcc)
124
+ ```python
125
+ import sys, os, time
126
+ from mathai import *
127
+
128
+ sys.setrecursionlimit(10000)
129
+
130
+ def integration_byparts(item): return simplify(fraction(simplify(byparts(simplify(parse(item)))[0])))
131
+ def integration_apart(item): return simplify(fraction(integrate(apart(factor2(simplify(parse(item)))))[0]))
132
+ def integration_direct(item): return simplify(fraction(simplify(integrate(simplify(parse(item)))[0])))
133
+ def integration_trig(item): return simplify(trig0(integrate(trig1(simplify(parse(item))))[0]))
134
+ def algebra(item): return logic0(simplify(expand(simplify(parse(item)))))
135
+ def trig_basic(item): return logic0(simplify(expand(trig3(simplify(parse(item))))))
136
+ def trig_advanced(item): return logic0(simplify(trig0(trig1(trig4(simplify(fraction(trig0(simplify(parse(item))))))))))
137
+
138
+ all_tasks = [
139
+ *[(item, trig_advanced) for item in [
140
+ "cos(x)/(1+sin(x)) + (1+sin(x))/cos(x) = 2*sec(x)",
141
+ "(1+sec(x))/sec(x) = sin(x)^2/(1-cos(x))"]],
142
+ *[(item, integration_byparts) for item in ["sin(x)*x","x*sin(3*x)","x*log(abs(x))","arctan(x)"]],
143
+ *[(item, integration_apart) for item in ["x/((x+1)*(x+2))","1/(x^2-9)"]],
144
+ *[(item, integration_direct) for item in [
145
+ "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"]],
146
+ *[(item, integration_trig) for item in ["sin(2*x+5)^2","sin(x)^4","cos(2*x)^4"]],
147
+ *[(item, algebra) for item in ["(x+1)^2 = x^2+2*x+1","(x+1)*(x-1) = x^2-1"]],
148
+ *[(item, trig_basic) for item in ["2*sin(x)*cos(x)=sin(2*x)"]],
149
+ ]
150
+
151
+ def run_task(task):
152
+ item, func = task
153
+ try: result = func(item)
154
+ except Exception as e: result = str(e)
155
+ return item, result
156
+
157
+ if __name__=="__main__":
158
+ print(f"Solving {len(all_tasks)} math questions...\n")
159
+ start_time = time.time()
160
+ for task in all_tasks:
161
+ item, result = run_task(task)
162
+ print(f"{item} => {result}\n")
163
+ print(f"All tasks completed in {time.time()-start_time:.2f} seconds")
164
+ ```
165
+ ### Output
166
+
167
+ ```
168
+ Running 21 tasks asynchronously on 8 cores...
169
+
170
+ x*log(abs(x)) => ((-2*(x^2))+(4*log(abs(x))*(x^2)))*(8^-1)
171
+
172
+ arctan(x) => (log((abs((1+(x^2)))^-1))+(2*arctan(x)*x))*(2^-1)
173
+
174
+ sin(cos(x))*sin(x) => cos(cos(x))
175
+
176
+ 1/(x^2-9) => (log(abs((-3+x)))+log((abs((3+x))^-1)))*(6^-1)
177
+
178
+ x/((x+1)*(x+2)) => log((abs((1+x))^-1))+log(((2+x)^2))
179
+
180
+ x*sin(3*x) => ((-9*cos((3*x))*x)+(3*sin((3*x))))*(27^-1)
181
+
182
+ (1+sec(x))/sec(x) = sin(x)^2/(1-cos(x)) => true
183
+
184
+ e^(arctan(x))/(1+x^2) => e^arctan(x)
185
+
186
+ cos(sqrt(x))/sqrt(x) => 2*sin((x^(2^-1)))
187
+
188
+ sqrt(a*x+b) => 2*(3^-1)*(((x*a)+b)^(3*(2^-1)))*(a^-1)
189
+
190
+ sin(x)*x => (-1*cos(x)*x)+sin(x)
191
+
192
+ (x+1)^2 = x^2+2*x+1 => true
193
+
194
+ (x+1)*(x-1) = x^2-1 => true
195
+
196
+ cos(x)/(1+sin(x)) + (1+sin(x))/cos(x) = 2*sec(x) => true
197
+
198
+ 2*sin(x)*cos(x)=sin(2*x) => true
199
+
200
+ sqrt(sin(2*x))*cos(2*x) => (3^-1)*(sin((2*x))^(3*(2^-1)))
201
+
202
+ 2*x/(1+x^2) => log(abs((1+(x^2))))
203
+
204
+ sin(2*x+5)^2 => ((-1*(4^-1)*sin((10+(4*x))))+x)*(2^-1)
205
+
206
+ cos(2*x)^4 => ((4^-1)*x)+((64^-1)*sin((8*x)))+((8^-1)*sin((4*x)))+((8^-1)*x)
207
+
208
+ 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)
209
+
210
+ sin(x)^4 => (-1*(4^-1)*sin((2*x)))+((32^-1)*sin((4*x)))+((4^-1)*x)+((8^-1)*x)
211
+
212
+ All tasks completed in 129.78 seconds
213
+ ```
214
+
@@ -11,4 +11,5 @@ from .trig import trig0, trig1, trig2, trig3, trig4
11
11
  from .logic import logic0, logic1, logic2, logic3
12
12
  from .apart import apart
13
13
  from .console import console
14
+ from .limit import limit
14
15
  from .base import *
@@ -145,6 +145,8 @@ def frac(eq):
145
145
  a = frac(eq.children[0])
146
146
  b = frac(eq.children[1])
147
147
  if isinstance(a, Fraction) and isinstance(b, Fraction) and b.denominator==1:
148
+ if a == 0 and b <= 0:
149
+ return None
148
150
  return a**b
149
151
  else:
150
152
  return None
@@ -4,6 +4,8 @@ from .base import *
4
4
  from .simplify import solve, simplify
5
5
 
6
6
  def expand(eq):
7
+ if eq is None:
8
+ return None
7
9
  if eq.name == "f_mul" or eq.name == "f_pow":
8
10
  if eq.name == "f_pow":
9
11
  eq = TreeNode("f_pow", [eq])
@@ -0,0 +1,126 @@
1
+ from .structure import structure
2
+ from .base import *
3
+ from .parser import parse
4
+ from .simplify import simplify, solve
5
+ from .expand import expand
6
+ from .diff import diff
7
+ from .trig import trig0
8
+ from .fraction import fraction
9
+ from .printeq import printeq_str
10
+ tab=0
11
+ def substitute_val(eq, val, var="v_0"):
12
+ eq = replace(eq, tree_form(var), tree_form("d_"+str(val)))
13
+ return eq
14
+
15
+ def subslimit(equation, var):
16
+ equation2 = trig0(replace(equation, var, tree_form("d_0")))
17
+ try:
18
+ tmp = simplify(equation2)
19
+ return simplify(expand(tmp))
20
+ except:
21
+ return None
22
+
23
+ def check(num, den, var):
24
+ n, d = None, None
25
+
26
+ n, d = (dowhile(replace(e, var, tree_form("d_0")), lambda x: trig0(simplify(x))) for e in (num, den))
27
+ if n is None or d is None:
28
+ return False
29
+ if n == 0 and d == 0: return True
30
+ if d != 0: return simplify(num/den)
31
+ return False
32
+ def lhospital(num, den, steps,var):
33
+ logs = []
34
+ out = check(num, den, var)
35
+ if isinstance(out, TreeNode):
36
+ return out,[]
37
+ for _ in range(steps):
38
+ num2, den2 = map(lambda e: simplify(diff(e, var.name)), (num, den))
39
+ out = check(num2, den2, var)
40
+ if out is True:
41
+ num, den = num2, den2
42
+ logs += [(0,"lim x->0 "+printeq_str(simplify(num/den)))]
43
+ continue
44
+ if out is False:
45
+ eq2 = simplify(fraction(simplify(num/den)))
46
+ return eq2,logs
47
+ return out,logs
48
+ def lhospital2(eq, var):
49
+ eq= simplify(eq)
50
+ if eq is None:
51
+ return None
52
+ if not contain(eq, var):
53
+ return eq,[]
54
+ num, dem = [simplify(item) for item in num_dem(eq)]
55
+ if num is None or dem is None:
56
+ return eq,[]
57
+
58
+ return lhospital(num, dem, 10,var)
59
+ ls = [parse("sin(A)"), parse("A^B-1"),parse("log(1+A)"), parse("cos(A)")]
60
+ ls= [simplify(item) for item in ls]
61
+
62
+ def approx(eq, var):
63
+ n, d= num_dem(eq)
64
+ n, d = solve(n), solve(d)
65
+ n, d = expand(n), expand(d)
66
+ out = []
67
+ for equation in [n, d]:
68
+ for item in factor_generation(equation):
69
+ tmp = structure(item, ls[0])
70
+ if tmp is not None and contain(tmp["v_-1"], var):
71
+ item2 = substitute_val(tmp["v_-1"], 0, var.name)
72
+ if tree_form("d_0") == expand(simplify(item2)):
73
+ equation = equation/item
74
+ equation = equation*tmp["v_-1"]
75
+ break
76
+ elif tree_form("d_0") == expand(simplify(tree_form("s_pi") - item2)):
77
+ equation = equation/item
78
+ equation = equation*(tree_form("s_pi") - tmp["v_-1"])
79
+ break
80
+ tmp = structure(item, ls[1])
81
+ if tmp is not None and contain(tmp["v_-1"], var) and not contain(tmp["v_-2"], var):
82
+ item2 = substitute_val(tmp["v_-1"], 0, var.name)
83
+ item2 = expand(solve(item2))
84
+ if tree_form("d_0") == item2:
85
+ equation = equation/item
86
+ equation = solve(equation*tmp["v_-1"]*tmp["v_-2"].fx("log"))
87
+ break
88
+ tmp = structure(item, ls[2])
89
+ if tmp is not None and contain(tmp["v_-1"], var):
90
+
91
+ item2 = substitute_val(tmp["v_-1"], 0, var.name)
92
+ item2 = expand(solve(item2))
93
+ if tree_form("d_0") == item2:
94
+ equation = equation/item
95
+ equation = solve(equation*tmp["v_-1"])
96
+ break
97
+ tmp = structure(item, ls[3])
98
+ if tmp is not None and contain(tmp["v_-1"], var):
99
+ item2 = substitute_val(item, 0, var.name)
100
+
101
+ if tree_form("d_0") == expand(solve(item2)):
102
+
103
+ equation = equation/item
104
+ equation = equation*(tree_form("d_1") - tmp["v_-1"]**tree_form("d_2"))
105
+ break
106
+
107
+ equation = solve(equation)
108
+ out.append(equation)
109
+ return simplify(out[0]/out[1])
110
+ def approx_limit(equation, var):
111
+ return dowhile(equation, lambda x: approx(x, var))
112
+
113
+ def limit(equation, var=tree_form("v_0")):
114
+ logs = [(0,"lim x->0 "+printeq_str(simplify(equation)))]
115
+ eq2 = dowhile(replace(equation, var, tree_form("d_0")), lambda x: trig0(simplify(x)))
116
+ if eq2 is not None and not contain(equation, var):
117
+ return eq2,logs
118
+ equation, tmp = lhospital2(equation, var)
119
+ equation = simplify(expand(simplify(equation)))
120
+ if not contain(equation, var):
121
+ return equation,logs+tmp
122
+ '''
123
+ if equation.name == "f_add":
124
+ return simplify(summation([limit(child, var) for child in equation.children]))
125
+ '''
126
+ return equation,logs+tmp
@@ -157,6 +157,7 @@ def clear_div(eq):
157
157
  return solve(product(lst2))
158
158
 
159
159
  def simplify(eq):
160
+ error = False
160
161
  if eq.name == "f_eq":
161
162
  if eq.children[1] != 0:
162
163
  return TreeNode(eq.name, [clear_div(simplify(eq.children[0]-eq.children[1])), tree_form("d_0")])
@@ -170,6 +171,11 @@ def simplify(eq):
170
171
 
171
172
  a, b = int(eq.children[0].name[2:]), int(eq.children[1].name[2:])
172
173
  a = a**abs(b)
174
+ if b == 0 and a == 0:
175
+ error= True
176
+ return eq
177
+ if b == 0:
178
+ b = 1
173
179
  b = int(b/abs(b))
174
180
  if b == 1:
175
181
  eq = tree_form("d_"+str(a))
@@ -195,6 +201,9 @@ def simplify(eq):
195
201
  for i in range(len(eq.children)-1,-1,-1):
196
202
  child= eq.children[i]
197
203
  if child.name == "f_pow" and child.children[0].name[:2] == "d_" and child.children[1] == -1:
204
+ if int(child.children[0].name[2:]) == 0:
205
+ error = True
206
+ return eq
198
207
  n = n*Fraction(1,int(child.children[0].name[2:]))
199
208
  eq.children.pop(i)
200
209
  elif child.name[:2] == "d_":
@@ -209,7 +218,7 @@ def simplify(eq):
209
218
  eq = eq.children[0]
210
219
  return TreeNode(eq.name, [helper3(child) for child in eq.children])
211
220
  def helper4(eq):
212
-
221
+ nonlocal error
213
222
  def perfect_nth_root_value(x, n):
214
223
  """Return integer y if x is a perfect n-th power (y**n == x), else None."""
215
224
  if x < 0 and n % 2 == 0:
@@ -253,7 +262,10 @@ def simplify(eq):
253
262
  if eq.name == "f_pow" and eq.children[0] == tree_form("d_1"):
254
263
  eq = tree_form("d_1")
255
264
  if eq.name == "f_pow" and eq.children[0] == tree_form("d_0"):
256
- eq = tree_form("d_0")
265
+ if frac(eq.children[1]) is not None and frac(eq.children[1]) <= 0:
266
+ error = True
267
+ else:
268
+ eq = tree_form("d_0")
257
269
  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"):
258
270
  eq = eq.children[0].children[0]
259
271
  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"):
@@ -323,4 +335,6 @@ def simplify(eq):
323
335
  return eq
324
336
  eq = dowhile(eq, fx3)
325
337
  eq = dowhile(eq, helper8)
338
+ if error:
339
+ return None
326
340
  return solve(eq)
@@ -55,7 +55,8 @@ def structure(equation, formula, formula_out=None, only_const=False):
55
55
  node.name = "f_mul"
56
56
  return TreeNode(node.name, [conversionrev(child) for child in node.children])
57
57
  equation = conversion(equation)
58
- formula_out = conversion(formula_out)
58
+ if formula_out is not None:
59
+ formula_out = conversion(formula_out)
59
60
  for item in lst(formula):
60
61
  varlist = {}
61
62
  if helper(equation, item):
@@ -32,6 +32,8 @@ for key in trig_cos_table.keys():
32
32
  for key in trig_sin_table.keys():
33
33
  trig_sin_table[key] = simplify(trig_sin_table[key])
34
34
  def trig0(eq):
35
+ if eq is None:
36
+ return None
35
37
  def isneg(eq):
36
38
  if eq.name[:2] != "d_":
37
39
  return False
@@ -0,0 +1,231 @@
1
+ Metadata-Version: 2.2
2
+ Name: mathai
3
+ Version: 0.2.2
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
+
20
+ ## Philosophy
21
+ I think it is a big realization in computer science and programming to realize that computers can solve mathematics.
22
+ This understanding should be made mainstream. It can help transform education, mathematical research, and computation of mathematical equations for work.
23
+
24
+ ## Societal Implications Of Such A Computer Program And The Author's Comment On Universities Of India
25
+ I think mathematics is valued by society because of education. Schools and universities teach them.
26
+ So this kind of software, if made mainstream, could bring real change.
27
+
28
+ ### The Author's Comments On The Universities In His Country
29
+ > Educated Indians are having a low IQ and are good for nothing.
30
+ > The Indian Institute of Technology (IITs) graduates are the leader of the fools.
31
+ > Every educated Indian is beneath me.
32
+ > Now learn how this Python library can solve the math questions of your exams.
33
+
34
+ ## The Summary Of How Computer "Solves" Math
35
+ Math equations are a tree data structure (`TreeNode` class).
36
+ We can manipulate the math equations using various algorithms (functions provided by the `mathai` library).
37
+ We first parse the math equation strings to get the tree data structure (`parse` function in `mathai`).
38
+
39
+ ## The Library
40
+ Import the library by doing:
41
+
42
+ ```python
43
+ from mathai import *
44
+ ```
45
+
46
+ ### str_form
47
+ It is the string representation of a `TreeNode` math equation.
48
+
49
+ #### Example
50
+ ```text
51
+ (cos(x)^2)+(sin(x)^2)
52
+ ```
53
+
54
+ Is represented internally as:
55
+
56
+ ```text
57
+ f_add
58
+ f_pow
59
+ f_cos
60
+ v_0
61
+ d_2
62
+ f_pow
63
+ f_sin
64
+ v_0
65
+ d_2
66
+ ```
67
+
68
+ #### Leaf Nodes
69
+
70
+ **Variables** (start with a `v_` prefix):
71
+
72
+ - `v_0` → x
73
+ - `v_1` → y
74
+ - `v_2` → z
75
+ - `v_3` → a
76
+
77
+ **Numbers** (start with `d_` prefix; only integers):
78
+
79
+ - `d_-1` → -1
80
+ - `d_0` → 0
81
+ - `d_1` → 1
82
+ - `d_2` → 2
83
+
84
+ #### Branch Nodes
85
+ - `f_add` → addition
86
+ - `f_mul` → multiplication
87
+ - `f_pow` → power
88
+
89
+ ### parse
90
+ Takes a math equation string and outputs a `TreeNode` object.
91
+
92
+ ```python
93
+ from mathai import *
94
+
95
+ equation = parse("sin(x)^2+cos(x)^2")
96
+ print(equation)
97
+ ```
98
+
99
+ #### Output
100
+ ```text
101
+ (cos(x)^2)+(sin(x)^2)
102
+ ```
103
+
104
+ ### printeq, printeq_str, printeq_log
105
+ Prints math equations in a more readable form than usual `print`.
106
+
107
+ ```python
108
+ from mathai import *
109
+
110
+ equation = simplify(parse("(x+1)/x"))
111
+ print(equation)
112
+ printeq(equation)
113
+ ```
114
+
115
+ #### Output
116
+ ```text
117
+ (1+x)*(x^-1)
118
+ (1+x)/x
119
+ ```
120
+
121
+ ### solve, simplify
122
+ `simplify` performs what `solve` does and more.
123
+ It simplifies and cleans up a given math equation.
124
+
125
+ ```python
126
+ from mathai import *
127
+
128
+ equation = simplify(parse("(x+x+x+x-1-1-1-1)*(4*x-4)*sin(sin(x+x+x)*sin(3*x))"))
129
+ printeq(equation)
130
+ ```
131
+
132
+ #### Output
133
+ ```text
134
+ ((-4+(4*x))^2)*sin((sin((3*x))^2))
135
+ ```
136
+
137
+ ### Incomplete Documentation, Will be updated and completed later on
138
+
139
+ ### Example Demonstration [limits questions can also be solved other than this these, try limit()]
140
+ ![pip-install-mathai-mathematics-solving-ai-system-in-python-v0-xcg3c22k51sf1](https://github.com/user-attachments/assets/799f576f-27d0-4d7c-86e9-ad55ff221bcc)
141
+ ```python
142
+ import sys, os, time
143
+ from mathai import *
144
+
145
+ sys.setrecursionlimit(10000)
146
+
147
+ def integration_byparts(item): return simplify(fraction(simplify(byparts(simplify(parse(item)))[0])))
148
+ def integration_apart(item): return simplify(fraction(integrate(apart(factor2(simplify(parse(item)))))[0]))
149
+ def integration_direct(item): return simplify(fraction(simplify(integrate(simplify(parse(item)))[0])))
150
+ def integration_trig(item): return simplify(trig0(integrate(trig1(simplify(parse(item))))[0]))
151
+ def algebra(item): return logic0(simplify(expand(simplify(parse(item)))))
152
+ def trig_basic(item): return logic0(simplify(expand(trig3(simplify(parse(item))))))
153
+ def trig_advanced(item): return logic0(simplify(trig0(trig1(trig4(simplify(fraction(trig0(simplify(parse(item))))))))))
154
+
155
+ all_tasks = [
156
+ *[(item, trig_advanced) for item in [
157
+ "cos(x)/(1+sin(x)) + (1+sin(x))/cos(x) = 2*sec(x)",
158
+ "(1+sec(x))/sec(x) = sin(x)^2/(1-cos(x))"]],
159
+ *[(item, integration_byparts) for item in ["sin(x)*x","x*sin(3*x)","x*log(abs(x))","arctan(x)"]],
160
+ *[(item, integration_apart) for item in ["x/((x+1)*(x+2))","1/(x^2-9)"]],
161
+ *[(item, integration_direct) for item in [
162
+ "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"]],
163
+ *[(item, integration_trig) for item in ["sin(2*x+5)^2","sin(x)^4","cos(2*x)^4"]],
164
+ *[(item, algebra) for item in ["(x+1)^2 = x^2+2*x+1","(x+1)*(x-1) = x^2-1"]],
165
+ *[(item, trig_basic) for item in ["2*sin(x)*cos(x)=sin(2*x)"]],
166
+ ]
167
+
168
+ def run_task(task):
169
+ item, func = task
170
+ try: result = func(item)
171
+ except Exception as e: result = str(e)
172
+ return item, result
173
+
174
+ if __name__=="__main__":
175
+ print(f"Solving {len(all_tasks)} math questions...\n")
176
+ start_time = time.time()
177
+ for task in all_tasks:
178
+ item, result = run_task(task)
179
+ print(f"{item} => {result}\n")
180
+ print(f"All tasks completed in {time.time()-start_time:.2f} seconds")
181
+ ```
182
+ ### Output
183
+
184
+ ```
185
+ Running 21 tasks asynchronously on 8 cores...
186
+
187
+ x*log(abs(x)) => ((-2*(x^2))+(4*log(abs(x))*(x^2)))*(8^-1)
188
+
189
+ arctan(x) => (log((abs((1+(x^2)))^-1))+(2*arctan(x)*x))*(2^-1)
190
+
191
+ sin(cos(x))*sin(x) => cos(cos(x))
192
+
193
+ 1/(x^2-9) => (log(abs((-3+x)))+log((abs((3+x))^-1)))*(6^-1)
194
+
195
+ x/((x+1)*(x+2)) => log((abs((1+x))^-1))+log(((2+x)^2))
196
+
197
+ x*sin(3*x) => ((-9*cos((3*x))*x)+(3*sin((3*x))))*(27^-1)
198
+
199
+ (1+sec(x))/sec(x) = sin(x)^2/(1-cos(x)) => true
200
+
201
+ e^(arctan(x))/(1+x^2) => e^arctan(x)
202
+
203
+ cos(sqrt(x))/sqrt(x) => 2*sin((x^(2^-1)))
204
+
205
+ sqrt(a*x+b) => 2*(3^-1)*(((x*a)+b)^(3*(2^-1)))*(a^-1)
206
+
207
+ sin(x)*x => (-1*cos(x)*x)+sin(x)
208
+
209
+ (x+1)^2 = x^2+2*x+1 => true
210
+
211
+ (x+1)*(x-1) = x^2-1 => true
212
+
213
+ cos(x)/(1+sin(x)) + (1+sin(x))/cos(x) = 2*sec(x) => true
214
+
215
+ 2*sin(x)*cos(x)=sin(2*x) => true
216
+
217
+ sqrt(sin(2*x))*cos(2*x) => (3^-1)*(sin((2*x))^(3*(2^-1)))
218
+
219
+ 2*x/(1+x^2) => log(abs((1+(x^2))))
220
+
221
+ sin(2*x+5)^2 => ((-1*(4^-1)*sin((10+(4*x))))+x)*(2^-1)
222
+
223
+ cos(2*x)^4 => ((4^-1)*x)+((64^-1)*sin((8*x)))+((8^-1)*sin((4*x)))+((8^-1)*x)
224
+
225
+ 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)
226
+
227
+ sin(x)^4 => (-1*(4^-1)*sin((2*x)))+((32^-1)*sin((4*x)))+((4^-1)*x)+((8^-1)*x)
228
+
229
+ All tasks completed in 129.78 seconds
230
+ ```
231
+
@@ -10,6 +10,7 @@ mathai/factor.py
10
10
  mathai/fraction.py
11
11
  mathai/integrate.py
12
12
  mathai/inverse.py
13
+ mathai/limit.py
13
14
  mathai/linear.py
14
15
  mathai/logic.py
15
16
  mathai/parser.py
@@ -2,7 +2,7 @@ from setuptools import setup, find_packages
2
2
 
3
3
  setup(
4
4
  name="mathai",
5
- version="0.2.0",
5
+ version="0.2.2",
6
6
  description="Mathematics solving Ai tailored to NCERT",
7
7
  long_description=open("README.md").read(),
8
8
  long_description_content_type="text/markdown",
mathai-0.2.0/PKG-INFO DELETED
@@ -1,24 +0,0 @@
1
- Metadata-Version: 2.2
2
- Name: mathai
3
- Version: 0.2.0
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
- educated indians are having a low iq and are good for nothing
19
-
20
- the indian institute of technology graduates are the leader of the fools
21
-
22
- every educated indian is beneath me
23
-
24
- now learn how this python library can solve the math questions of your exams
mathai-0.2.0/README.md DELETED
@@ -1,7 +0,0 @@
1
- educated indians are having a low iq and are good for nothing
2
-
3
- the indian institute of technology graduates are the leader of the fools
4
-
5
- every educated indian is beneath me
6
-
7
- now learn how this python library can solve the math questions of your exams
@@ -1,24 +0,0 @@
1
- Metadata-Version: 2.2
2
- Name: mathai
3
- Version: 0.2.0
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
- educated indians are having a low iq and are good for nothing
19
-
20
- the indian institute of technology graduates are the leader of the fools
21
-
22
- every educated indian is beneath me
23
-
24
- now learn how this python library can solve the math questions of your exams
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes