pplonski commited on
Commit
8b133a5
·
1 Parent(s): 9457cfd

Upload calculus.ipynb

Browse files
Files changed (1) hide show
  1. calculus.ipynb +283 -0
calculus.ipynb ADDED
@@ -0,0 +1,283 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "code",
5
+ "execution_count": 7,
6
+ "id": "f78791d5",
7
+ "metadata": {},
8
+ "outputs": [
9
+ {
10
+ "data": {
11
+ "application/mercury+json": "{\n \"widget\": \"App\",\n \"title\": \"Calculus Problem Generator\",\n \"description\": \"Generates expressions which students can apply differential and integral calculus to.\",\n \"show_code\": false,\n \"show_prompt\": false,\n \"output\": \"app\",\n \"schedule\": \"\",\n \"notify\": \"{}\",\n \"continuous_update\": true,\n \"static_notebook\": false,\n \"show_sidebar\": false,\n \"full_screen\": true,\n \"allow_download\": false,\n \"model_id\": \"mercury-app\",\n \"code_uid\": \"App.0.40.24.4-randf482e104\"\n}",
12
+ "text/html": [
13
+ "<h3>Mercury Application</h3><small>This output won't appear in the web app.</small>"
14
+ ],
15
+ "text/plain": [
16
+ "mercury.App"
17
+ ]
18
+ },
19
+ "metadata": {},
20
+ "output_type": "display_data"
21
+ }
22
+ ],
23
+ "source": [
24
+ "import mercury as mr\n",
25
+ " \n",
26
+ "# set Application parameters\n",
27
+ "app = mr.App(title=\"Calculus Problem Generator\",\n",
28
+ " description=\"Generates expressions which students can apply differential and integral calculus to.\",\n",
29
+ " show_code=False,\n",
30
+ " show_prompt=False,\n",
31
+ " continuous_update=True,\n",
32
+ " static_notebook=False,\n",
33
+ " show_sidebar=False,\n",
34
+ " full_screen=True,\n",
35
+ " allow_download=False)"
36
+ ]
37
+ },
38
+ {
39
+ "cell_type": "code",
40
+ "execution_count": 8,
41
+ "id": "46df7f52",
42
+ "metadata": {},
43
+ "outputs": [
44
+ {
45
+ "data": {
46
+ "application/mercury+json": "{\n \"widget\": \"Button\",\n \"label\": \"New equation\",\n \"style\": \"primary\",\n \"value\": false,\n \"model_id\": \"924849ab8db145cf8e9f7bc75f38bb23\",\n \"code_uid\": \"Button.0.40.11.1-randd017f351\",\n \"disabled\": false,\n \"hidden\": false\n}",
47
+ "application/vnd.jupyter.widget-view+json": {
48
+ "model_id": "924849ab8db145cf8e9f7bc75f38bb23",
49
+ "version_major": 2,
50
+ "version_minor": 0
51
+ },
52
+ "text/plain": [
53
+ "mercury.Button"
54
+ ]
55
+ },
56
+ "metadata": {},
57
+ "output_type": "display_data"
58
+ }
59
+ ],
60
+ "source": [
61
+ "generate = mr.Button(label=\"New equation\")"
62
+ ]
63
+ },
64
+ {
65
+ "cell_type": "code",
66
+ "execution_count": 10,
67
+ "id": "3a7db45a",
68
+ "metadata": {},
69
+ "outputs": [
70
+ {
71
+ "data": {
72
+ "text/latex": [
73
+ "$\\displaystyle \\frac{d}{d \\theta} \\left(- \\sqrt{\\theta} + \\sin{\\left(\\theta \\right)} + \\cos{\\left(\\theta \\right)}\\right) = ?$"
74
+ ],
75
+ "text/plain": [
76
+ "Eq(Derivative(-sqrt(theta) + sin(theta) + cos(theta), theta), ?)"
77
+ ]
78
+ },
79
+ "metadata": {},
80
+ "output_type": "display_data"
81
+ },
82
+ {
83
+ "data": {
84
+ "text/latex": [
85
+ "$\\displaystyle \\int \\left(z - 3\\right) \\left(6 z - 10\\right)\\, dz = ?$"
86
+ ],
87
+ "text/plain": [
88
+ "Eq(Integral((z - 3)*(6*z - 10), z), ?)"
89
+ ]
90
+ },
91
+ "metadata": {},
92
+ "output_type": "display_data"
93
+ },
94
+ {
95
+ "name": "stdout",
96
+ "output_type": "stream",
97
+ "text": [
98
+ "\n",
99
+ "\n",
100
+ "\n",
101
+ "\n",
102
+ "\n",
103
+ "\n"
104
+ ]
105
+ },
106
+ {
107
+ "data": {
108
+ "text/latex": [
109
+ "$\\displaystyle \\frac{d}{d \\theta} \\left(- \\sqrt{\\theta} + \\sin{\\left(\\theta \\right)} + \\cos{\\left(\\theta \\right)}\\right) = - \\sin{\\left(\\theta \\right)} + \\cos{\\left(\\theta \\right)} - \\frac{1}{2 \\sqrt{\\theta}}$"
110
+ ],
111
+ "text/plain": [
112
+ "Eq(Derivative(-sqrt(theta) + sin(theta) + cos(theta), theta), -sin(theta) + cos(theta) - 1/(2*sqrt(theta)))"
113
+ ]
114
+ },
115
+ "metadata": {},
116
+ "output_type": "display_data"
117
+ },
118
+ {
119
+ "data": {
120
+ "text/latex": [
121
+ "$\\displaystyle \\int \\left(z - 3\\right) \\left(6 z - 10\\right)\\, dz = 2 z^{3} - 14 z^{2} + 30 z$"
122
+ ],
123
+ "text/plain": [
124
+ "Eq(Integral((z - 3)*(6*z - 10), z), 2*z**3 - 14*z**2 + 30*z)"
125
+ ]
126
+ },
127
+ "metadata": {},
128
+ "output_type": "display_data"
129
+ }
130
+ ],
131
+ "source": [
132
+ "from sympy.simplify.fu import TR22,TR2i\n",
133
+ "from sympy import *\n",
134
+ "from sympy.abc import theta\n",
135
+ "import random\n",
136
+ "f = Function('f')\n",
137
+ "g = Function('g')\n",
138
+ "h = Function('h')\n",
139
+ "theta = Symbol('theta')\n",
140
+ "i = 0\n",
141
+ "dkeywords = {\"polylog\",\"Ei\",\"gamma\",\"Piecewise\",\"li\",\"erf\",\"Si\",\"Ci\",\"hyper\",\"fresnel\",\"Li\",\"expint\",\"zoo\",\n",
142
+ "\"nan\",\"oo\",\"abs\",\"re\",\"EulerGamma\", \"sinh\",\"tanh\", \"cosh\",'sign','abs','atan','csc','asin'} \n",
143
+ "ikeywords = {\"polylog\",\"Ei\",\"gamma\",\"Piecewise\", \"li\", \"erf\", \"atan\", \"Si\", \"Ci\", \"hyper\", \"fresnel\", \"Li\", \n",
144
+ "\"expint\",\"zoo\", \"nan\", \"oo\",\"EulerGamma\",\"sinh\",\"csc\",\"asin\"}\n",
145
+ "keywords2 = {\"sin\",\"cos\",\"tan\"}\n",
146
+ "def random_variable(i):\n",
147
+ " return Symbol(random.choice([i for i in ['v','t','x','z','y']]), real=True)\n",
148
+ "def random_value(i):\n",
149
+ " return random.choice([i for i in range(-10,10) if i not in [0]])\n",
150
+ "def power(a): \n",
151
+ " return random_value(i)*a**int(random_value(i)/2)\n",
152
+ "def scalar(a): \n",
153
+ " return a*random_value(i) + random_value(i)\n",
154
+ "def addSUBTR(a): \n",
155
+ " return a+random_value(i)\n",
156
+ "def dmain(a):\n",
157
+ " def random_math(a): \n",
158
+ " funs = [power,scalar,addSUBTR,power,scalar,addSUBTR,ln,exp,sin,cos,tan,sqrt] \n",
159
+ " operations = [f(a)+g(a)+h(a),\n",
160
+ " f(a)+g(a)-h(a),\n",
161
+ " f(a)+g(a)*h(a),\n",
162
+ " f(a)+g(a)/h(a),\n",
163
+ " \n",
164
+ " f(a)-g(a)+h(a),\n",
165
+ " f(a)-g(a)-h(a),\n",
166
+ " f(a)-g(a)*h(a),\n",
167
+ " f(a)-g(a)/h(a),\n",
168
+ "\n",
169
+ " f(a)*g(a)+h(a),\n",
170
+ " f(a)*g(a)-h(a),\n",
171
+ " f(a)*g(a)*h(a),\n",
172
+ " f(a)*g(a)/h(a), \n",
173
+ " \n",
174
+ " f(a)/g(a)+h(a),\n",
175
+ " f(a)/g(a)-h(a),\n",
176
+ " f(a)/g(a)*h(a),\n",
177
+ " f(a)/g(a)/h(a), \n",
178
+ "\n",
179
+ " f(a)* ( g(a)+h(a) ),\n",
180
+ " f(a)* ( g(a)-h(a) ),\n",
181
+ " f(a)/ ( g(a)+h(a) ),\n",
182
+ " f(a)/ ( g(a)-h(a) ),\n",
183
+ " \n",
184
+ " f(g(h(a))),\n",
185
+ " f(h(a))+g(a),\n",
186
+ " f(h(a))-g(a),\n",
187
+ " f(h(a))*g(a),\n",
188
+ " f(h(a))/g(a),\n",
189
+ " f(a)/g(h(a))]\n",
190
+ " operation = operations[random.randrange(0,len(operations))]\n",
191
+ " return [[[operation.replace(f, i) for i in funs][random.randrange(0,len(funs))].replace(g, i) for i in funs]\\\n",
192
+ " [random.randrange(0,len(funs))].replace(h, i) for i in funs][random.randrange(0,len(funs))]\n",
193
+ " return random_math(a)\n",
194
+ "def imain(a):\n",
195
+ " def random_math2(a): \n",
196
+ " funs = [power,scalar,addSUBTR,power,scalar,addSUBTR,ln,exp,sin,cos,tan,sqrt] \n",
197
+ " operations = [f(g(a)),f(a)+g(a),f(a)-g(a),f(a)/g(a),f(a)*g(a)]\n",
198
+ " operation = operations[random.randrange(0,len(operations))]\n",
199
+ " return [[operation.replace(f, i) for i in funs][random.randrange(0,len(funs))].replace(g, i) for i in funs]\\\n",
200
+ " [random.randrange(0,len(funs))]\n",
201
+ " return random_math2(a)\n",
202
+ "derror = True\n",
203
+ "def dtest():\n",
204
+ " global setup1\n",
205
+ " global derror\n",
206
+ " global practice1\n",
207
+ " a = random_variable(i)\n",
208
+ " setup1 = dmain(a)\n",
209
+ " practice1 = Derivative(setup1,a) \n",
210
+ " p1eq = TR22(Eq(practice1,practice1.doit(),evaluate=False))\n",
211
+ " if any(kw in str(setup1) for kw in keywords2):\n",
212
+ " setup1 = setup1.replace(a,theta)\n",
213
+ " practice1 = Derivative(setup1,theta) \n",
214
+ " p1eq = TR22(Eq(practice1,practice1.doit(),evaluate=False))\n",
215
+ " if p1eq.rhs != 0 and not any(kw in str(p1eq) for kw in dkeywords):\n",
216
+ " derror = False\n",
217
+ " return p1eq\n",
218
+ "while derror == True: \n",
219
+ " output1 = dtest()\n",
220
+ "ierror = True\n",
221
+ "def itest():\n",
222
+ " global ierror\n",
223
+ " global practice2\n",
224
+ " global setup2\n",
225
+ " a = random_variable(i)\n",
226
+ " setup2 = imain(a)\n",
227
+ " practice2 = Integral(setup2,a) \n",
228
+ " p2eq = TR22(Eq(practice2,practice2.doit(),evaluate=False))\n",
229
+ " if str(factor_terms(p2eq.lhs)) != str(factor_terms(p2eq.rhs)) and not any(kw in str(p2eq) for kw in ikeywords)\\\n",
230
+ " and str(p2eq.lhs) != str(-p2eq.rhs): \n",
231
+ " if any(kw in str(setup2) for kw in keywords2):\n",
232
+ " setup2 = setup2.replace(a,theta)\n",
233
+ " practice2 = Integral(setup2,theta) \n",
234
+ " p2eq = TR22(Eq(practice2,practice2.doit(),evaluate=False))\n",
235
+ " ierror = False\n",
236
+ " return p2eq\n",
237
+ "while ierror == True:\n",
238
+ " output2 = itest()\n",
239
+ "questionmark = Symbol('?')\n",
240
+ "def lhs():\n",
241
+ " return display(Eq(nsimplify(output1.lhs),questionmark),Eq(nsimplify(output2.lhs),questionmark)) \n",
242
+ "def rhs():\n",
243
+ " return display(Eq(nsimplify(output1.lhs),nsimplify(output1.rhs)),Eq(nsimplify(output2.lhs),nsimplify(output2.rhs)))\n",
244
+ "\n",
245
+ "if generate.clicked:\n",
246
+ " lhs()\n",
247
+ " print(\"\\n\")\n",
248
+ " print(\"\\n\")\n",
249
+ " print(\"\\n\")\n",
250
+ " rhs()"
251
+ ]
252
+ },
253
+ {
254
+ "cell_type": "code",
255
+ "execution_count": null,
256
+ "id": "08e768e2",
257
+ "metadata": {},
258
+ "outputs": [],
259
+ "source": []
260
+ }
261
+ ],
262
+ "metadata": {
263
+ "kernelspec": {
264
+ "display_name": "menv",
265
+ "language": "python",
266
+ "name": "menv"
267
+ },
268
+ "language_info": {
269
+ "codemirror_mode": {
270
+ "name": "ipython",
271
+ "version": 3
272
+ },
273
+ "file_extension": ".py",
274
+ "mimetype": "text/x-python",
275
+ "name": "python",
276
+ "nbconvert_exporter": "python",
277
+ "pygments_lexer": "ipython3",
278
+ "version": "3.8.10"
279
+ }
280
+ },
281
+ "nbformat": 4,
282
+ "nbformat_minor": 5
283
+ }