NSC9 commited on
Commit
e5edcb6
1 Parent(s): a2970bf

Upload Artificial_Calc_Teacher_v7.7.ipynb

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