NSC9 commited on
Commit
192addc
·
1 Parent(s): c36faa6

Upload Artificial_Calc_Teacher_v7.7.ipynb

Browse files
Files changed (1) hide show
  1. Artificial_Calc_Teacher_v7.7.ipynb +317 -0
Artificial_Calc_Teacher_v7.7.ipynb ADDED
@@ -0,0 +1,317 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "code",
5
+ "execution_count": null,
6
+ "id": "f6c4f747",
7
+ "metadata": {},
8
+ "outputs": [],
9
+ "source": [
10
+ "import mercury as mr # for widgets\n",
11
+ "\n",
12
+ "app = mr.App(title=\"Calculus Problem Generator\",\n",
13
+ " description=\"Generates expressions for applying differential and integral calculus.\",\n",
14
+ " show_code=False,\n",
15
+ " show_prompt=True,\n",
16
+ " continuous_update=False,\n",
17
+ " static_notebook=False,\n",
18
+ " show_sidebar=True,\n",
19
+ " full_screen=True,\n",
20
+ " allow_download=False)"
21
+ ]
22
+ },
23
+ {
24
+ "cell_type": "code",
25
+ "execution_count": 82,
26
+ "id": "c5197005",
27
+ "metadata": {},
28
+ "outputs": [],
29
+ "source": [
30
+ "# changelog: string spliced for easy copy/paste"
31
+ ]
32
+ },
33
+ {
34
+ "cell_type": "markdown",
35
+ "id": "6bdf2faf",
36
+ "metadata": {},
37
+ "source": [
38
+ "# Solve with pen and paper:"
39
+ ]
40
+ },
41
+ {
42
+ "cell_type": "code",
43
+ "execution_count": 2,
44
+ "id": "108761f9",
45
+ "metadata": {
46
+ "scrolled": false
47
+ },
48
+ "outputs": [],
49
+ "source": [
50
+ "from sympy.simplify.fu import TR22,TR2i\n",
51
+ "from sympy import ln,exp,Function,Derivative,Eq,Integral,factor_terms, sqrt, Symbol,Limit\n",
52
+ "from sympy import sin,cos,tan,Rational,nsimplify\n",
53
+ "import random\n",
54
+ "f = Function('f')\n",
55
+ "g = Function('g')\n",
56
+ "h = Function('h')\n",
57
+ "theta = Symbol('theta')\n",
58
+ "i = 0\n",
59
+ "dkeywords = {\"polylog\",\"Ei\",\"gamma\",\"Piecewise\",\"li\",\"erf\",\"Si\",\"Ci\",\"hyper\",\"fresnel\",\"Li\",\"expint\",\"zoo\",\n",
60
+ "\"nan\",\"oo\",\"abs\",\"re\",\"EulerGamma\", \"sinh\",\"tanh\", \"cosh\",'sign','abs','atan','csc','asin'} \n",
61
+ "ikeywords = {\"polylog\",\"Ei\",\"gamma\",\"Piecewise\", \"li\", \"erf\", \"atan\", \"Si\", \"Ci\", \"hyper\", \"fresnel\", \"Li\", \n",
62
+ "\"expint\",\"zoo\", \"nan\", \"oo\",\"EulerGamma\",\"sinh\",\"csc\",\"asin\"}\n",
63
+ "keywords2 = {\"sin\",\"cos\",\"tan\"}\n",
64
+ "def random_variable(i):\n",
65
+ " return Symbol(random.choice([i for i in ['v','t','x','z','y']]), real=True)\n",
66
+ "def random_value(i):\n",
67
+ " return random.choice([i for i in range(-10,10) if i not in [0]])\n",
68
+ "def power(a): \n",
69
+ " return random_value(i)*a**int(random_value(i)/2)\n",
70
+ "def scalar(a): \n",
71
+ " return a*random_value(i) + random_value(i)\n",
72
+ "def addSUBTR(a): \n",
73
+ " return a+random_value(i)\n",
74
+ "def dmain(a):\n",
75
+ " def random_math(a): \n",
76
+ " funs = [power,scalar,addSUBTR,power,scalar,addSUBTR,ln,exp,sin,cos,tan,sqrt] \n",
77
+ " operations = [f(a)+g(a)+h(a),\n",
78
+ " f(a)+g(a)-h(a),\n",
79
+ " f(a)+g(a)*h(a),\n",
80
+ " f(a)+g(a)/h(a),\n",
81
+ " \n",
82
+ " f(a)-g(a)+h(a),\n",
83
+ " f(a)-g(a)-h(a),\n",
84
+ " f(a)-g(a)*h(a),\n",
85
+ " f(a)-g(a)/h(a),\n",
86
+ "\n",
87
+ " f(a)*g(a)+h(a),\n",
88
+ " f(a)*g(a)-h(a),\n",
89
+ " f(a)*g(a)*h(a),\n",
90
+ " f(a)*g(a)/h(a), \n",
91
+ " \n",
92
+ " f(a)/g(a)+h(a),\n",
93
+ " f(a)/g(a)-h(a),\n",
94
+ " f(a)/g(a)*h(a),\n",
95
+ " f(a)/g(a)/h(a), \n",
96
+ "\n",
97
+ " f(a)* ( g(a)+h(a) ),\n",
98
+ " f(a)* ( g(a)-h(a) ),\n",
99
+ " f(a)/ ( g(a)+h(a) ),\n",
100
+ " f(a)/ ( g(a)-h(a) ),\n",
101
+ " \n",
102
+ " f(g(h(a))),\n",
103
+ " f(h(a))+g(a),\n",
104
+ " f(h(a))-g(a),\n",
105
+ " f(h(a))*g(a),\n",
106
+ " f(h(a))/g(a),\n",
107
+ " f(a)/g(h(a))]\n",
108
+ " operation = operations[random.randrange(0,len(operations))]\n",
109
+ " return [[[operation.replace(f, i) for i in funs][random.randrange(0,len(funs))].replace(g, i) for i in funs]\\\n",
110
+ " [random.randrange(0,len(funs))].replace(h, i) for i in funs][random.randrange(0,len(funs))]\n",
111
+ " return random_math(a)\n",
112
+ "def imain(a):\n",
113
+ " def random_math2(a): \n",
114
+ " funs = [power,scalar,addSUBTR,power,scalar,addSUBTR,ln,exp,sin,cos,tan,sqrt] \n",
115
+ " operations = [f(g(a)),f(a)+g(a),f(a)-g(a),f(a)/g(a),f(a)*g(a)]\n",
116
+ " operation = operations[random.randrange(0,len(operations))]\n",
117
+ " return [[operation.replace(f, i) for i in funs][random.randrange(0,len(funs))].replace(g, i) for i in funs]\\\n",
118
+ " [random.randrange(0,len(funs))]\n",
119
+ " return random_math2(a)"
120
+ ]
121
+ },
122
+ {
123
+ "cell_type": "code",
124
+ "execution_count": 79,
125
+ "id": "5ddcf6a5",
126
+ "metadata": {
127
+ "scrolled": false
128
+ },
129
+ "outputs": [
130
+ {
131
+ "data": {
132
+ "text/latex": [
133
+ "$\\displaystyle \\frac{d}{d z} \\frac{\\sqrt{z} \\left(z - 1\\right)}{z + 1} = ?$"
134
+ ],
135
+ "text/plain": [
136
+ "Eq(Derivative(sqrt(z)*(z - 1)/(z + 1), z), ?)"
137
+ ]
138
+ },
139
+ "metadata": {},
140
+ "output_type": "display_data"
141
+ },
142
+ {
143
+ "data": {
144
+ "text/latex": [
145
+ "$\\displaystyle \\int \\left(9 \\log{\\left(v \\right)} + 4\\right)\\, dv = ?$"
146
+ ],
147
+ "text/plain": [
148
+ "Eq(Integral(9*log(v) + 4, v), ?)"
149
+ ]
150
+ },
151
+ "metadata": {},
152
+ "output_type": "display_data"
153
+ }
154
+ ],
155
+ "source": [
156
+ "derror = True\n",
157
+ "def dtest():\n",
158
+ " global setup1\n",
159
+ " global derror\n",
160
+ " global practice1\n",
161
+ " a = random_variable(i)\n",
162
+ " setup1 = dmain(a)\n",
163
+ " practice1 = Derivative(setup1,a) \n",
164
+ " p1eq = TR22(Eq(practice1,practice1.doit(),evaluate=False))\n",
165
+ " if any(kw in str(setup1) for kw in keywords2):\n",
166
+ " setup1 = setup1.replace(a,theta)\n",
167
+ " practice1 = Derivative(setup1,theta) \n",
168
+ " p1eq = TR22(Eq(practice1,practice1.doit(),evaluate=False))\n",
169
+ " if p1eq.rhs != 0 and not any(kw in str(p1eq) for kw in dkeywords):\n",
170
+ " derror = False\n",
171
+ " return p1eq\n",
172
+ "while derror == True: \n",
173
+ " output1 = dtest()\n",
174
+ "ierror = True\n",
175
+ "def itest():\n",
176
+ " global ierror\n",
177
+ " global practice2\n",
178
+ " global setup2\n",
179
+ " a = random_variable(i)\n",
180
+ " setup2 = imain(a)\n",
181
+ " practice2 = Integral(setup2,a) \n",
182
+ " p2eq = TR22(Eq(practice2,practice2.doit(),evaluate=False))\n",
183
+ " if str(factor_terms(p2eq.lhs)) != str(factor_terms(p2eq.rhs)) and not any(kw in str(p2eq) for kw in ikeywords)\\\n",
184
+ " and str(p2eq.lhs) != str(-p2eq.rhs): \n",
185
+ " if any(kw in str(setup2) for kw in keywords2):\n",
186
+ " setup2 = setup2.replace(a,theta)\n",
187
+ " practice2 = Integral(setup2,theta) \n",
188
+ " p2eq = TR22(Eq(practice2,practice2.doit(),evaluate=False))\n",
189
+ " ierror = False\n",
190
+ " return p2eq\n",
191
+ "while ierror == True:\n",
192
+ " output2 = itest()\n",
193
+ "questionmark = Symbol('?')\n",
194
+ "def lhs():\n",
195
+ " return display(Eq(nsimplify(output1.lhs),questionmark),Eq(nsimplify(output2.lhs),questionmark)) \n",
196
+ "def rhs():\n",
197
+ " return display(Eq(nsimplify(output1.lhs),nsimplify(output1.rhs)),Eq(nsimplify(output2.lhs),nsimplify(output2.rhs)))\n",
198
+ "lhs()"
199
+ ]
200
+ },
201
+ {
202
+ "cell_type": "markdown",
203
+ "id": "2393180e",
204
+ "metadata": {},
205
+ "source": [
206
+ "___________________________________________________________________________________________________________________\n",
207
+ "If LaTeX display breaks, refresh the page. Runtime is under a second if you run the Jupyter-Notebook locally.\n",
208
+ "\n",
209
+ "**Created by GitHub.com/NSC9 - Live @ https://nsc9.github.io/ - MIT License - v7.7**\n",
210
+ "\n",
211
+ "Latest version source code: https://github.com/NSC9/Sample_of_Work/tree/Main/Artificial_Calculus_Teacher\n",
212
+ "\n",
213
+ "Donate by sending Bitcoin (BTC) to address: **bc1qtawr2gw52ftufzu0r3r20pnj3vmynssxs0mjl4**\n",
214
+ "\n"
215
+ ]
216
+ },
217
+ {
218
+ "cell_type": "code",
219
+ "execution_count": 80,
220
+ "id": "15c3f34f",
221
+ "metadata": {},
222
+ "outputs": [
223
+ {
224
+ "name": "stdout",
225
+ "output_type": "stream",
226
+ "text": [
227
+ "For easy copy/paste into calculators below:\n",
228
+ "sqrt(z)*(z - 1)/(z + 1)\n",
229
+ "9*log(v) + 4\n"
230
+ ]
231
+ }
232
+ ],
233
+ "source": [
234
+ "print(\"For easy copy/paste into calculators below:\")\n",
235
+ "keywords3 = {\"theta\"}\n",
236
+ "if any(kw in str(nsimplify(output1.lhs)) for kw in keywords3):\n",
237
+ " print(str(nsimplify(output1.lhs))[11:-8])\n",
238
+ "else:\n",
239
+ " print(str(nsimplify(output1.lhs))[11:-4])\n",
240
+ " \n",
241
+ "if any(kw in str(nsimplify(output2.lhs)) for kw in keywords3):\n",
242
+ " print(str(nsimplify(output2.lhs))[9:-8])\n",
243
+ "else:\n",
244
+ " print(str(nsimplify(output2.lhs))[9:-4]) \n"
245
+ ]
246
+ },
247
+ {
248
+ "cell_type": "markdown",
249
+ "id": "30b55c90",
250
+ "metadata": {},
251
+ "source": [
252
+ "https://www.derivative-calculator.net/\n",
253
+ "\n",
254
+ "https://www.integral-calculator.com/\n",
255
+ "___________________________________________________________________________________________________________________\n",
256
+ "\n",
257
+ "# Answers:"
258
+ ]
259
+ },
260
+ {
261
+ "cell_type": "code",
262
+ "execution_count": 81,
263
+ "id": "fbec286d",
264
+ "metadata": {},
265
+ "outputs": [
266
+ {
267
+ "data": {
268
+ "text/latex": [
269
+ "$\\displaystyle \\frac{d}{d z} \\frac{\\sqrt{z} \\left(z - 1\\right)}{z + 1} = - \\frac{\\sqrt{z} \\left(z - 1\\right)}{\\left(z + 1\\right)^{2}} + \\frac{\\sqrt{z}}{z + 1} + \\frac{z - 1}{2 \\sqrt{z} \\left(z + 1\\right)}$"
270
+ ],
271
+ "text/plain": [
272
+ "Eq(Derivative(sqrt(z)*(z - 1)/(z + 1), z), -sqrt(z)*(z - 1)/(z + 1)**2 + sqrt(z)/(z + 1) + (z - 1)/(2*sqrt(z)*(z + 1)))"
273
+ ]
274
+ },
275
+ "metadata": {},
276
+ "output_type": "display_data"
277
+ },
278
+ {
279
+ "data": {
280
+ "text/latex": [
281
+ "$\\displaystyle \\int \\left(9 \\log{\\left(v \\right)} + 4\\right)\\, dv = 9 v \\log{\\left(v \\right)} - 5 v$"
282
+ ],
283
+ "text/plain": [
284
+ "Eq(Integral(9*log(v) + 4, v), 9*v*log(v) - 5*v)"
285
+ ]
286
+ },
287
+ "metadata": {},
288
+ "output_type": "display_data"
289
+ }
290
+ ],
291
+ "source": [
292
+ "rhs()"
293
+ ]
294
+ }
295
+ ],
296
+ "metadata": {
297
+ "kernelspec": {
298
+ "display_name": "Python 3 (ipykernel)",
299
+ "language": "python",
300
+ "name": "python3"
301
+ },
302
+ "language_info": {
303
+ "codemirror_mode": {
304
+ "name": "ipython",
305
+ "version": 3
306
+ },
307
+ "file_extension": ".py",
308
+ "mimetype": "text/x-python",
309
+ "name": "python",
310
+ "nbconvert_exporter": "python",
311
+ "pygments_lexer": "ipython3",
312
+ "version": "3.10.6"
313
+ }
314
+ },
315
+ "nbformat": 4,
316
+ "nbformat_minor": 5
317
+ }