File size: 28,433 Bytes
6fc87f8
 
55b48e8
 
 
 
6fc87f8
55b48e8
6fc87f8
55b48e8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6fc87f8
55b48e8
6fc87f8
 
55b48e8
 
 
 
 
6fc87f8
 
55b48e8
 
 
 
 
6fc87f8
 
 
 
55b48e8
6fc87f8
 
55b48e8
 
 
 
 
6fc87f8
 
55b48e8
 
 
 
 
6fc87f8
 
 
 
55b48e8
6fc87f8
 
55b48e8
 
 
 
 
6fc87f8
 
55b48e8
 
 
 
 
6fc87f8
 
 
 
 
 
 
55b48e8
 
 
 
 
6fc87f8
 
55b48e8
 
 
 
 
6fc87f8
 
 
 
55b48e8
6fc87f8
 
55b48e8
 
 
 
 
6fc87f8
 
55b48e8
 
 
 
 
6fc87f8
 
 
 
55b48e8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6fc87f8
 
55b48e8
 
 
 
 
6fc87f8
 
55b48e8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6fc87f8
 
 
 
 
55b48e8
6fc87f8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
55b48e8
6fc87f8
 
 
 
 
 
 
 
 
55b48e8
 
 
 
 
 
 
 
 
6fc87f8
55b48e8
 
 
 
6fc87f8
55b48e8
 
 
6fc87f8
55b48e8
 
 
 
 
 
6fc87f8
55b48e8
6fc87f8
55b48e8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6fc87f8
 
55b48e8
 
 
6fc87f8
 
 
 
 
 
55b48e8
6fc87f8
 
 
55b48e8
6fc87f8
 
 
 
 
 
55b48e8
6fc87f8
 
 
55b48e8
6fc87f8
 
55b48e8
 
6fc87f8
 
 
 
55b48e8
 
 
 
 
 
 
6fc87f8
 
55b48e8
6fc87f8
 
 
 
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
import gradio as gr
import random
import json
import tempfile
import os
from datetime import datetime

# Define a comprehensive list of calculus topics based on James Stewart's textbook
TOPICS = {
    "Limits and Continuity": {
        "formula": "For a function $f(x)$, $\\lim_{x \\to a} f(x) = L$",
        "functions": {
            "easy": [
                {"func": "$\\lim_{x \\to 2} (3x+4)$", "domain": ["$x \\to 2$"], "solution": "$10$"},
                {"func": "$\\lim_{x \\to 0} \\frac{\\sin(x)}{x}$", "domain": ["$x \\to 0$"], "solution": "$1$"},
                {"func": "$\\lim_{x \\to 3} (x^2-5x+2)$", "domain": ["$x \\to 3$"], "solution": "$-4$"},
                {"func": "$\\lim_{x \\to 1} \\frac{x^2-1}{x-1}$", "domain": ["$x \\to 1$"], "solution": "$2$"},
                {"func": "$\\lim_{x \\to \\infty} \\frac{2x^2+3x-5}{x^2}$", "domain": ["$x \\to \\infty$"], "solution": "$2$"}
            ],
            "hard": [
                {"func": "$\\lim_{x \\to 0} \\frac{1-\\cos(x)}{x^2}$", "domain": ["$x \\to 0$"], "solution": "$\\frac{1}{2}$"},
                {"func": "$\\lim_{x \\to 0} (\\frac{1}{x} - \\frac{1}{\\sin(x)})$", "domain": ["$x \\to 0$"], "solution": "$0$"},
                {"func": "$\\lim_{x \\to 0} \\frac{e^x-1-x}{x^2}$", "domain": ["$x \\to 0$"], "solution": "$\\frac{1}{2}$"},
                {"func": "$\\lim_{x \\to \\infty} (1 + \\frac{1}{x})^x$", "domain": ["$x \\to \\infty$"], "solution": "$e$"},
                {"func": "$\\lim_{x \\to 0^+} x^{\\alpha}\\ln(x)$ where $\\alpha > 0$", "domain": ["$x \\to 0^+$"], "solution": "$0$"}
            ]
        }
    },
    "Derivatives and Differentiation": {
        "formula": "$f'(x) = \\lim_{h \\to 0} \\frac{f(x+h) - f(x)}{h}$",
        "functions": {
            "easy": [
                {"func": "$f(x) = x^3 - 4x^2 + 7x - 2$", "domain": ["Find $f'(x)$"], "solution": "$f'(x) = 3x^2 - 8x + 7$"},
                {"func": "$f(x) = \\sin(2x)$", "domain": ["Find $f'(x)$"], "solution": "$f'(x) = 2\\cos(2x)$"},
                {"func": "$f(x) = e^{3x}$", "domain": ["Find $f'(x)$"], "solution": "$f'(x) = 3e^{3x}$"},
                {"func": "$f(x) = \\ln(x^2 + 1)$", "domain": ["Find $f'(x)$"], "solution": "$f'(x) = \\frac{2x}{x^2+1}$"},
                {"func": "$f(x) = x^2 e^x$", "domain": ["Find $f'(x)$"], "solution": "$f'(x) = x^2 e^x + 2x e^x$"}
            ],
            "hard": [
                {"func": "$f(x) = \\frac{\\sin(x)}{\\cos(x) + 2}$", "domain": ["Find $f'(x)$"], "solution": "$f'(x) = \\frac{\\cos(x)(\\cos(x) + 2) + \\sin^2(x)}{(\\cos(x) + 2)^2}$"},
                {"func": "$f(x) = \\int_{0}^{x^2} \\sin(t^2) dt$", "domain": ["Find $f'(x)$"], "solution": "$f'(x) = 2x\\sin(x^4)$"},
                {"func": "$f(x) = \\arctan(e^x)$", "domain": ["Find $f'(x)$"], "solution": "$f'(x) = \\frac{e^x}{1 + e^{2x}}$"},
                {"func": "$f(x) = x^{\\sin(x)}$", "domain": ["Find $f'(x)$"], "solution": "$f'(x) = x^{\\sin(x)}(\\cos(x)\\ln(x) + \\frac{\\sin(x)}{x})$"},
                {"func": "$f(x) = \\ln(\\sin(x))$", "domain": ["Find $f'(x)$"], "solution": "$f'(x) = \\cot(x)$"}
            ]
        }
    },
    "Applications of Derivatives": {
        "formula": "Related Rates, Optimization, L'Hôpital's Rule",
        "functions": {
            "easy": [
                {"func": "A particle moves according to $s(t) = t^3 - 6t^2 + 9t$. Find its velocity at $t = 2$", "domain": ["$t = 2$"], "solution": "$v(2) = -3$ units/sec"},
                {"func": "Find the critical points of $f(x) = x^3 - 3x^2 - 9x + 5$", "domain": ["$x \\in \\mathbb{R}$"], "solution": "$x = -1$ and $x = 3$"},
                {"func": "Find the absolute maximum and minimum of $f(x) = x^2 - 4x + 3$ on $[0, 3]$", "domain": ["$[0, 3]$"], "solution": "Maximum: $f(0) = 3$, Minimum: $f(2) = -1$"},
                {"func": "Use L'Hôpital's Rule to evaluate $\\lim_{x \\to 0} \\frac{\\tan(3x)}{x}$", "domain": ["$x \\to 0$"], "solution": "$3$"},
                {"func": "Find the equation of the tangent line to $f(x) = x^2 + 2x - 3$ at $x = 1$", "domain": ["$x = 1$"], "solution": "$y = 4x - 2$"}
            ],
            "hard": [
                {"func": "A ladder 10 feet long leans against a wall. If the bottom slides away at 2 ft/s, how fast is the top sliding down when it's 6 feet above ground?", "domain": ["Rate problem"], "solution": "$\\frac{3}{2}$ ft/s"},
                {"func": "Find the dimensions of the rectangle with perimeter 100 m that has the maximum area", "domain": ["Optimization"], "solution": "25 m × 25 m square"},
                {"func": "Use Newton's method to approximate a root of $f(x) = x^3 - 2x - 5$ starting with $x_1 = 2$", "domain": ["Newton's Method"], "solution": "$x \\approx 2.0946$ after 3 iterations"},
                {"func": "Find the absolute extrema of $f(x) = xe^{-x^2}$ on $[0, \\infty)$", "domain": ["$[0, \\infty)$"], "solution": "Maximum: $f(\\frac{1}{\\sqrt{2}}) = \\frac{1}{\\sqrt{2e}}$, Minimum: $f(0) = f(\\infty) = 0$"},
                {"func": "Use implicit differentiation to find $\\frac{dy}{dx}$ for $x^3 + y^3 = 6xy$", "domain": ["Implicit"], "solution": "$\\frac{dy}{dx} = \\frac{6y - 3x^2}{3y^2 - 6x}$"}
            ]
        }
    },
    "Integration Techniques": {
        "formula": "$\\int f(x) dx$ using various methods",
        "functions": {
            "easy": [
                {"func": "$\\int x^3(x^2+1)^4 dx$", "domain": ["Use Substitution"], "solution": "$\\frac{1}{10}(x^2+1)^5 - \\frac{1}{6}(x^2+1)^3 + C$"},
                {"func": "$\\int \\frac{1}{x^2-4} dx$", "domain": ["Use Partial Fractions"], "solution": "$\\frac{1}{4}\\ln|\\frac{x-2}{x+2}| + C$"},
                {"func": "$\\int x\\sin(x) dx$", "domain": ["Use Integration by Parts"], "solution": "$\\sin(x) - x\\cos(x) + C$"},
                {"func": "$\\int \\sec^2(3x) dx$", "domain": ["Trigonometric"], "solution": "$\\frac{1}{3}\\tan(3x) + C$"},
                {"func": "$\\int \\frac{5}{3x+6} dx$", "domain": ["Substitution"], "solution": "$\\frac{5}{3}\\ln|3x+6| + C$"}
            ],
            "hard": [
                {"func": "$\\int \\frac{x^2}{\\sqrt{1-x^2}} dx$", "domain": ["Trigonometric Substitution"], "solution": "$-\\frac{x\\sqrt{1-x^2}}{2} - \\frac{\\arcsin(x)}{2} + C$"},
                {"func": "$\\int \\frac{\\ln(x)}{x^2} dx$", "domain": ["Integration by Parts"], "solution": "$-\\frac{\\ln(x)}{x} - \\frac{1}{x} + C$"},
                {"func": "$\\int e^x\\sin(x) dx$", "domain": ["Integration by Parts twice"], "solution": "$\\frac{e^x(\\sin(x)-\\cos(x))}{2} + C$"},
                {"func": "$\\int \\frac{1}{x^2-x-6} dx$", "domain": ["Partial Fractions"], "solution": "$\\frac{1}{5}\\ln|\\frac{x+2}{x-3}| + C$"},
                {"func": "$\\int \\frac{1}{\\sqrt{x^2-a^2}} dx$", "domain": ["$a > 0$"], "solution": "$\\ln|x + \\sqrt{x^2-a^2}| + C$"}
            ]
        }
    },
    "Average Value": {
        "formula": "$f_{avg} = \\frac{1}{b-a} \\int_{a}^{b} f(x) dx$",
        "functions": {
            "easy": [
                {"func": "$x^2$", "domain": [0, 2], "solution": "$\\frac{4}{3}$"},
                {"func": "$\\sin(x)$", "domain": [0, "π"], "solution": "$\\frac{2}{\\pi}$"},
                {"func": "$e^x$", "domain": [0, 1], "solution": "$(e-1)$"},
                {"func": "$x$", "domain": [1, 4], "solution": "$\\frac{5}{2}$"},
                {"func": "$x^3$", "domain": [0, 1], "solution": "$\\frac{1}{4}$"}
            ],
            "hard": [
                {"func": "$x\\sin(x)$", "domain": [0, "π"], "solution": "$\\frac{\\pi}{2}$"},
                {"func": "$\\ln(x)$", "domain": [1, "e"], "solution": "$1-\\frac{1}{e}$"},
                {"func": "$x^2e^x$", "domain": [0, 1], "solution": "$2e-2$"},
                {"func": "$\\frac{1}{1+x^2}$", "domain": [0, 1], "solution": "$\\frac{\\pi}{4}$"},
                {"func": "$\\sqrt{x}$", "domain": [0, 4], "solution": "$\\frac{4}{3}$"}
            ]
        }
    },
    "Arc Length": {
        "formula": "$L = \\int_{a}^{b} \\sqrt{1 + (f'(x))^2} dx$",
        "functions": {
            "easy": [
                {"func": "$x^2$", "domain": [0, 1], "solution": "$\\approx 1.4789$"},
                {"func": "$x^{3/2}$", "domain": [0, 1], "solution": "$\\approx 1.1919$"},
                {"func": "$2x+1$", "domain": [0, 2], "solution": "$2\\sqrt{5}$"},
                {"func": "$x^3$", "domain": [0, 1], "solution": "$\\approx 1.0801$"},
                {"func": "$\\sin(x)$", "domain": [0, "π/2"], "solution": "$\\approx 1.9118$"}
            ],
            "hard": [
                {"func": "$\\ln(x)$", "domain": [1, 3], "solution": "$\\approx 2.3861$"},
                {"func": "$e^x$", "domain": [0, 1], "solution": "$\\approx 1.1752$"},
                {"func": "$\\cosh(x)$", "domain": [0, 1], "solution": "$\\sinh(1)$"},
                {"func": "$x^2 - \\ln(x)$", "domain": [1, 2], "solution": "$\\approx 3.1623$"},
                {"func": "$x = \\cos(t)$, $y = \\sin(t)$ for $t\\in[0,\\pi]$", "domain": [0, "π"], "solution": "$\\pi$"}
            ]
        }
    },
    "Surface Area": {
        "formula": "$S = 2\\pi \\int_{a}^{b} f(x) \\sqrt{1 + (f'(x))^2} dx$",
        "functions": {
            "easy": [
                {"func": "$x$", "domain": [0, 3], "solution": "$2\\pi \\cdot 4.5$"},
                {"func": "$x^2$", "domain": [0, 1], "solution": "$\\approx 2\\pi \\cdot 0.7169$"},
                {"func": "$\\sqrt{x}$", "domain": [0, 4], "solution": "$\\approx 2\\pi \\cdot 4.5177$"},
                {"func": "$1$", "domain": [0, 2], "solution": "$2\\pi \\cdot 2$"},
                {"func": "$\\frac{x}{2}$", "domain": [0, 4], "solution": "$2\\pi \\cdot 4.1231$"}
            ],
            "hard": [
                {"func": "$x^3$", "domain": [0, 1], "solution": "$\\approx 2\\pi \\cdot 0.6004$"},
                {"func": "$e^x$", "domain": [0, 1], "solution": "$\\approx 2\\pi \\cdot 1.1793$"},
                {"func": "$\\sin(x)$", "domain": [0, "π/2"], "solution": "$\\approx 2\\pi \\cdot 0.6366$"},
                {"func": "$\\frac{1}{x}$", "domain": [1, 2], "solution": "$\\approx 2\\pi \\cdot 1.1478$"},
                {"func": "$\\ln(x)$", "domain": [1, 2], "solution": "$\\approx 2\\pi \\cdot 0.5593$"}
            ]
        }
    },
    "Differential Equations": {
        "formula": "Various types",
        "functions": {
            "easy": [
                {"func": "$\\frac{dy}{dx} = 2x$", "domain": ["$y(0)=1$"], "solution": "$y = x^2 + 1$"},
                {"func": "$\\frac{dy}{dx} = y$", "domain": ["$y(0)=1$"], "solution": "$y = e^x$"},
                {"func": "$\\frac{dy}{dx} = 3x^2$", "domain": ["$y(0)=2$"], "solution": "$y = x^3 + 2$"},
                {"func": "$\\frac{dy}{dx} = -y$", "domain": ["$y(0)=4$"], "solution": "$y = 4e^{-x}$"},
                {"func": "$\\frac{dy}{dx} = x+1$", "domain": ["$y(0)=-2$"], "solution": "$y = \\frac{x^2}{2} + x - 2$"}
            ],
            "hard": [
                {"func": "$y'' + 4y = 0$", "domain": ["$y(0)=1$, $y'(0)=0$"], "solution": "$y = \\cos(2x)$"},
                {"func": "$y'' - y = x$", "domain": ["$y(0)=0$, $y'(0)=1$"], "solution": "$y = \\frac{e^x}{2} - \\frac{e^{-x}}{2} - x$"},
                {"func": "$y' + y = e^x$", "domain": ["$y(0)=0$"], "solution": "$y = xe^x$"},
                {"func": "$y'' + 2y' + y = 0$", "domain": ["$y(0)=1$, $y'(0)=-1$"], "solution": "$y = (1-x)e^{-x}$"},
                {"func": "$y'' - 2y' + y = x^2$", "domain": ["$y(0)=1$, $y'(0)=1$"], "solution": "$y = \\frac{x^2}{2} + 2x + 1$"}
            ]
        }
    },
    "Area and Volume": {
        "formula": "$A = \\int_{a}^{b} f(x) dx$, $V = \\pi \\int_{a}^{b} [f(x)]^2 dx$",
        "functions": {
            "easy": [
                {"func": "$f(x) = x^2$, find area under the curve", "domain": [0, 3], "solution": "$9$"},
                {"func": "$f(x) = \\sin(x)$, find area under the curve", "domain": [0, "π"], "solution": "$2$"},
                {"func": "$f(x) = 4-x^2$, find area under the curve", "domain": [-2, 2], "solution": "$\\frac{16}{3}$"},
                {"func": "$f(x) = \\sqrt{x}$, find volume of revolution around x-axis", "domain": [0, 4], "solution": "$\\frac{16\\pi}{3}$"},
                {"func": "$f(x) = x$, find volume of revolution around x-axis", "domain": [0, 2], "solution": "$\\frac{8\\pi}{3}$"}
            ],
            "hard": [
                {"func": "Area between $f(x) = x^2$ and $g(x) = x^3$", "domain": [0, 1], "solution": "$\\frac{1}{12}$"},
                {"func": "Volume of solid bounded by $z = 4-x^2-y^2$ and $z = 0$", "domain": ["$x^2+y^2\\leq 4$"], "solution": "$8\\pi$"},
                {"func": "Volume of solid formed by rotating region bounded by $y = x^2$, $y = 0$, $x = 2$ around y-axis", "domain": [0, 2], "solution": "$\\frac{8\\pi}{5}$"},
                {"func": "Area between $f(x) = \\sin(x)$ and $g(x) = \\cos(x)$", "domain": [0, "π/4"], "solution": "$\\sqrt{2}-1$"},
                {"func": "Volume of solid formed by rotating region bounded by $y = e^x$, $y = 0$, $x = 0$, $x = 1$ around x-axis", "domain": [0, 1], "solution": "$\\frac{\\pi(e^2-1)}{2}$"}
            ]
        }
    },
    "Parametric Curves and Equations": {
        "formula": "$x = x(t)$, $y = y(t)$, Arc length = $\\int_{a}^{b} \\sqrt{(\\frac{dx}{dt})^2 + (\\frac{dy}{dt})^2} dt$",
        "functions": {
            "easy": [
                {"func": "$x = t$, $y = t^2$, find $\\frac{dy}{dx}$", "domain": ["$t$"], "solution": "$\\frac{dy}{dx} = 2t$"},
                {"func": "$x = \\cos(t)$, $y = \\sin(t)$, find the arc length", "domain": [0, "π/2"], "solution": "$\\frac{\\pi}{2}$"},
                {"func": "$x = t^2$, $y = t^3$, find $\\frac{dy}{dx}$", "domain": ["$t$"], "solution": "$\\frac{dy}{dx} = \\frac{3t}{2}$"},
                {"func": "$x = 2t$, $y = t^2$, find the area under the curve", "domain": [0, 2], "solution": "$\\frac{4}{3}$"},
                {"func": "$x = t$, $y = \\sin(t)$, find $\\frac{dy}{dx}$", "domain": ["$t$"], "solution": "$\\frac{dy}{dx} = \\cos(t)$"}
            ],
            "hard": [
                {"func": "$x = e^t\\cos(t)$, $y = e^t\\sin(t)$, find $\\frac{dy}{dx}$", "domain": ["$t$"], "solution": "$\\frac{dy}{dx} = \\tan(t) + 1$"},
                {"func": "$x = t-\\sin(t)$, $y = 1-\\cos(t)$, find the arc length", "domain": [0, "2π"], "solution": "$8$"},
                {"func": "$x = \\ln(\\sec(t))$, $y = \\tan(t)$, find $\\frac{dy}{dx}$", "domain": ["$t$"], "solution": "$\\frac{dy}{dx} = \\sec^2(t)$"},
                {"func": "$x = \\cos^3(t)$, $y = \\sin^3(t)$, find the area enclosed", "domain": [0, "2π"], "solution": "$\\frac{3\\pi}{8}$"},
                {"func": "$x = \\cos(t)+t\\sin(t)$, $y = \\sin(t)-t\\cos(t)$, find the arc length", "domain": [0, "2π"], "solution": "$2\\pi\\sqrt{1+4\\pi^2}$"}
            ]
        }
    },
    "Infinite Sequences and Series": {
        "formula": "$\\sum_{n=1}^{\\infty} a_n = a_1 + a_2 + a_3 + ...$",
        "functions": {
            "easy": [
                {"func": "Determine if the sequence $a_n = \\frac{n+3}{2n+1}$ converges and find its limit", "domain": ["$n \\to \\infty$"], "solution": "Converges to $\\frac{1}{2}$"},
                {"func": "Find the sum of the geometric series $\\sum_{n=0}^{\\infty} \\frac{1}{3^n}$", "domain": ["Geometric Series"], "solution": "$\\frac{3}{2}$"},
                {"func": "Determine if the series $\\sum_{n=1}^{\\infty} \\frac{1}{n^2}$ converges", "domain": ["p-series"], "solution": "Converges (p-series with $p=2 > 1$)"},
                {"func": "Find the first three non-zero terms in the Taylor series for $f(x) = e^x$ centered at $a = 0$", "domain": ["Taylor Series"], "solution": "$1 + x + \\frac{x^2}{2} + ...$"},
                {"func": "Find the radius of convergence of the power series $\\sum_{n=1}^{\\infty} \\frac{x^n}{n}$", "domain": ["Power Series"], "solution": "$R = 1$"}
            ],
            "hard": [
                {"func": "Test the convergence of the alternating series $\\sum_{n=1}^{\\infty} (-1)^{n+1}\\frac{\\ln(n)}{n}$", "domain": ["Alternating Series"], "solution": "Converges by the Alternating Series Test"},
                {"func": "Find the radius and interval of convergence for $\\sum_{n=1}^{\\infty} \\frac{n^2 x^n}{3^n}$", "domain": ["Power Series"], "solution": "$R = 3$, interval of convergence is $(-3, 3)$"},
                {"func": "Determine if the series $\\sum_{n=2}^{\\infty} \\frac{1}{n\\ln(n)}$ converges", "domain": ["Integral Test"], "solution": "Diverges by the Integral Test"},
                {"func": "Find the sum of the series $\\sum_{n=1}^{\\infty} \\frac{1}{n(n+1)}$", "domain": ["Telescoping Series"], "solution": "$1$"},
                {"func": "Find the Taylor series of $f(x) = \\ln(1+x)$ and its radius of convergence", "domain": ["Taylor Series"], "solution": "$\\sum_{n=1}^{\\infty} \\frac{(-1)^{n+1}x^n}{n}$, $R = 1$"}
            ]
        }
    },
    "Polar Coordinates": {
        "formula": "$x = r\\cos(\\theta)$, $y = r\\sin(\\theta)$, Area = $\\frac{1}{2}\\int_{\\alpha}^{\\beta} r^2 d\\theta$",
        "functions": {
            "easy": [
                {"func": "Convert the Cartesian point $(1, \\sqrt{3})$ to polar coordinates", "domain": ["Conversion"], "solution": "$r = 2$, $\\theta = \\frac{\\pi}{3}$"},
                {"func": "Find the area enclosed by the circle $r = 3\\sin(\\theta)$", "domain": ["Polar Area"], "solution": "$\\frac{9\\pi}{2}$"},
                {"func": "Convert the polar equation $r = 2$ to Cartesian form", "domain": ["Conversion"], "solution": "$x^2 + y^2 = 4$"},
                {"func": "Find the area enclosed by $r = 2\\cos(\\theta)$", "domain": ["Polar Area"], "solution": "$2\\pi$"},
                {"func": "Convert the Cartesian equation $x^2 + y^2 = 4y$ to polar form", "domain": ["Conversion"], "solution": "$r = 4\\sin(\\theta)$"}
            ],
            "hard": [
                {"func": "Find the area of the region enclosed by the lemniscate $r^2 = 4\\cos(2\\theta)$", "domain": ["Polar Area"], "solution": "$4$"},
                {"func": "Find the area of the region inside $r = 1 + \\cos(\\theta)$ and outside $r = 1$", "domain": ["Polar Area"], "solution": "$\\frac{\\pi}{2}$"},
                {"func": "Find the points of intersection of the polar curves $r = 1 + \\sin(\\theta)$ and $r = 1 - \\sin(\\theta)$", "domain": ["Polar Curves"], "solution": "$(0, 0)$ and $(1, \\frac{\\pi}{2})$, $(1, \\frac{3\\pi}{2})$"},
                {"func": "Find the area of the region enclosed by the cardioid $r = 1 + \\cos(\\theta)$", "domain": ["Polar Area"], "solution": "$\\frac{3\\pi}{2}$"},
                {"func": "Find the length of the spiral $r = \\theta$ for $0 \\leq \\theta \\leq 2\\pi$", "domain": ["Polar Arc Length"], "solution": "$\\frac{1}{2}\\sqrt{1+4\\pi^2} + \\frac{1}{2}\\ln(2\\pi + \\sqrt{1+4\\pi^2})$"}
            ]
        }
    },
    "Vector Calculus": {
        "formula": "$\\vec{r}(t) = x(t)\\vec{i} + y(t)\\vec{j} + z(t)\\vec{k}$, $\\vec{v}(t) = \\vec{r}'(t)$, $\\vec{a}(t) = \\vec{v}'(t)$",
        "functions": {
            "easy": [
                {"func": "Find the derivative of the vector function $\\vec{r}(t) = t^2\\vec{i} + \\sin(t)\\vec{j} + e^t\\vec{k}$", "domain": ["Vector Function"], "solution": "$\\vec{r}'(t) = 2t\\vec{i} + \\cos(t)\\vec{j} + e^t\\vec{k}$"},
                {"func": "Find the unit tangent vector of $\\vec{r}(t) = \\cos(t)\\vec{i} + \\sin(t)\\vec{j}$ at $t = 0$", "domain": ["$t = 0$"], "solution": "$\\vec{T}(0) = \\vec{j}$"},
                {"func": "Calculate $\\nabla f$ where $f(x,y,z) = x^2y + yz^2$", "domain": ["Gradient"], "solution": "$\\nabla f = 2xy\\vec{i} + (x^2 + z^2)\\vec{j} + 2yz\\vec{k}$"},
                {"func": "Find the divergence of the vector field $\\vec{F}(x,y,z) = x^2\\vec{i} + 2xy\\vec{j} + yz\\vec{k}$", "domain": ["Divergence"], "solution": "$\\nabla \\cdot \\vec{F} = 2x + 2x + z = 4x + z$"},
                {"func": "Find the curl of $\\vec{F}(x,y,z) = y\\vec{i} + z\\vec{j} + x\\vec{k}$", "domain": ["Curl"], "solution": "$\\nabla \\times \\vec{F} = (1-0)\\vec{i} + (1-0)\\vec{j} + (1-0)\\vec{k} = \\vec{i} + \\vec{j} + \\vec{k}$"}
            ],
            "hard": [
                {"func": "Find the curvature of $\\vec{r}(t) = t\\vec{i} + t^2\\vec{j} + t^3\\vec{k}$ at $t = 1$", "domain": ["Curvature at $t = 1$"], "solution": "$\\kappa = \\frac{2\\sqrt{37}}{49\\sqrt{3}}$"},
                {"func": "Verify Stokes' Theorem for $\\vec{F} = x^2\\vec{i} + xy\\vec{j} + z^2\\vec{k}$ on the hemisphere $z = \\sqrt{1-x^2-y^2}$, $z \\geq 0$", "domain": ["Stokes' Theorem"], "solution": "Both integrals equal $\\frac{\\pi}{2}$"},
                {"func": "Use the Divergence Theorem to evaluate $\\iint_S \\vec{F} \\cdot \\vec{n} \\, dS$ where $\\vec{F}(x,y,z) = x\\vec{i} + y\\vec{j} + z\\vec{k}$ and $S$ is the sphere $x^2+y^2+z^2=4$", "domain": ["Divergence Theorem"], "solution": "$\\iint_S \\vec{F} \\cdot \\vec{n} \\, dS = \\iiint_V 3 \\, dV = 3 \\cdot \\frac{4}{3}\\pi \\cdot 4^{3/2} = 16\\pi$"},
                {"func": "Find the potential function $f$ for the conservative vector field $\\vec{F} = (2x+y)\\vec{i} + (x+2z)\\vec{j} + (2y)\\vec{k}$", "domain": ["Potential Function"], "solution": "$f(x,y,z) = x^2 + xy + 2yz + C$"},
                {"func": "Use Green's Theorem to evaluate $\\oint_C (y^2\\,dx + x^2\\,dy)$ where $C$ is the boundary of the region enclosed by $y = x^2$ and $y = 4$", "domain": ["Green's Theorem"], "solution": "$\\frac{256}{15}$"}
            ]
        }
    },
    "Partial Derivatives and Multiple Integrals": {
        "formula": "$\\frac{\\partial f}{\\partial x}$, $\\frac{\\partial f}{\\partial y}$, $\\iint_D f(x,y) \\, dA$, $\\iiint_E f(x,y,z) \\, dV$",
        "functions": {
            "easy": [
                {"func": "Find $\\frac{\\partial z}{\\partial x}$ and $\\frac{\\partial z}{\\partial y}$ for $z = x^2 + 3xy - y^3$", "domain": ["Partial Derivatives"], "solution": "$\\frac{\\partial z}{\\partial x} = 2x + 3y$, $\\frac{\\partial z}{\\partial y} = 3x - 3y^2$"},
                {"func": "Evaluate $\\iint_D (x + y) \\, dA$ where $D = \\{(x, y) | 0 \\leq x \\leq 1, 0 \\leq y \\leq 2\\}$", "domain": ["Double Integral"], "solution": "$3$"},
                {"func": "Find all critical points of $f(x,y) = x^2 + y^2 - 4x - 6y + 12$", "domain": ["Critical Points"], "solution": "$(2, 3)$"},
                {"func": "Convert the double integral $\\iint_D x^2y \\, dA$ to polar coordinates where $D$ is the disc $x^2 + y^2 \\leq 4$", "domain": ["Change to Polar"], "solution": "$\\int_0^{2\\pi} \\int_0^2 r^3 \\cos^2(\\theta)\\sin(\\theta) \\, dr \\, d\\theta$"},
                {"func": "Evaluate $\\iint_D xy \\, dA$ where $D$ is the triangle with vertices $(0,0)$, $(1,0)$, and $(0,1)$", "domain": ["Double Integral"], "solution": "$\\frac{1}{24}$"}
            ],
            "hard": [
                {"func": "Find the absolute maximum and minimum values of $f(x,y) = 2x^2 + y^2 - 4x + 6y + 10$ on the closed disc $x^2 + y^2 \\leq 25$", "domain": ["Extrema on Region"], "solution": "Maximum: $135$ at $(5,0)$, Minimum: $1$ at $(1,-3)$"},
                {"func": "Evaluate $\\iiint_E xy^2z^3 \\, dV$ where $E$ is the solid bounded by $x=0$, $y=0$, $z=0$, $x+y+z=1$", "domain": ["Triple Integral"], "solution": "$\\frac{1}{840}$"},
                {"func": "Use Lagrange multipliers to find the maximum value of $f(x,y) = xy$ subject to the constraint $x^2 + y^2 = 8$", "domain": ["Lagrange Multipliers"], "solution": "$4$ at $(\\pm 2, \\pm 2)$"},
                {"func": "Change the order of integration in $\\int_0^1 \\int_y^1 e^{xy} \\, dx \\, dy$", "domain": ["Change of Order"], "solution": "$\\int_0^1 \\int_0^x e^{xy} \\, dy \\, dx$"},
                {"func": "Evaluate $\\iint_D \\frac{1}{1+x^2+y^2} \\, dA$ where $D = \\{(x,y) | x^2 + y^2 \\leq 4\\}$", "domain": ["Double Integral"], "solution": "$\\pi\\ln(5)$"}
            ]
        }
    }
}

# Function to generate a question for a given topic and difficulty
def generate_question(topic_name, difficulty):
    topic_data = TOPICS[topic_name]
    formula = topic_data["formula"]
    
    # Select a random function from the available ones for this topic and difficulty
    function_data = random.choice(topic_data["functions"][difficulty])
    func = function_data["func"]
    domain = function_data["domain"]
    solution = function_data["solution"]
    
    # Format domain for display
    if isinstance(domain, list) and len(domain) == 2:
        domain_str = f"[{domain[0]}, {domain[1]}]"
    else:
        domain_str = str(domain)
    
    # Create question and solution based on difficulty
    if difficulty == "easy":
        question = f"Find the {topic_name.lower()} of {func} over the domain {domain_str}."
        solution_text = f"Step 1: Apply the formula for {topic_name.lower()}: {formula}\n\n"
        solution_text += f"Step 2: Substitute $f(x) = {func}$ and evaluate over {domain_str}\n\n"
        solution_text += f"Step 3: Solve the resulting integral or calculation\n\n"
        solution_text += f"Final Answer: {solution}"
    else:
        question = f"Compute the {topic_name.lower()} for {func} over {domain_str}."
        solution_text = f"Step 1: Apply the formula for {topic_name.lower()}: {formula}\n\n"
        solution_text += f"Step 2: For {func}, substitute into the formula and evaluate over {domain_str}\n\n"
        solution_text += f"Step 3: This requires advanced integration techniques or careful analysis\n\n"
        solution_text += f"Final Answer: {solution}"
    
    return {
        "topic": topic_name,
        "difficulty": difficulty,
        "question": question,
        "solution": solution_text
    }

# Store the latest generated questions
latest_questions = []

# Generate questions function for Gradio
def generate_calculus_questions(topic, difficulty, count):
    global latest_questions
    latest_questions = []
    
    for _ in range(int(count)):
        question_data = generate_question(topic, difficulty)
        latest_questions.append(question_data)
    
    # Format the output for display
    result = ""
    for i, q in enumerate(latest_questions):
        result += f"Question {i+1}: {q['question']}\n\n"
        result += f"Solution {i+1}: {q['solution']}\n\n"
        result += "-" * 40 + "\n\n"
    
    return result

# Function to export questions to JSON
def export_to_json():
    if not latest_questions:
        return None
    
    # Create a JSON file
    timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
    json_data = {
        "generated_at": timestamp,
        "questions": latest_questions
    }
    
    # Create a temporary file
    with tempfile.NamedTemporaryFile(mode="w+", delete=False, suffix=".json") as temp_file:
        json.dump(json_data, temp_file, indent=2)
        temp_file_path = temp_file.name
    
    return temp_file_path

# Create the Gradio interface
with gr.Blocks(title="Math Mento - Calculus Questions Generator") as demo:
    gr.Markdown("# Math Mento - Calculus Questions Generator")
    gr.Markdown("Generate LaTeX-formatted calculus practice questions with solutions based on James Stewart's calculus textbook")
    
    with gr.Row():
        with gr.Column():
            topic = gr.Dropdown(
                choices=list(TOPICS.keys()),
                label="Calculus Topic",
                value="Limits and Continuity"
            )
            difficulty = gr.Radio(
                choices=["easy", "hard"],
                label="Difficulty",
                value="easy"
            )
            count = gr.Slider(
                minimum=1,
                maximum=10,
                step=1,
                value=3,
                label="Number of Questions"
            )
            generate_button = gr.Button("Generate Questions")
            export_button = gr.Button("Export to JSON")
        
        with gr.Column():
            output = gr.Markdown()
            json_file = gr.File(label="Exported JSON")
    
    generate_button.click(
        generate_calculus_questions,
        inputs=[topic, difficulty, count],
        outputs=output
    )
    
    export_button.click(
        export_to_json,
        inputs=[],
        outputs=json_file
    )
    
    gr.Markdown("### Created by Kamogelo Mosia | Math Mento © 2025")

# Launch the app
if __name__ == "__main__":
    demo.launch()