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