-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathgenerate_limits.py
More file actions
1460 lines (1275 loc) · 52 KB
/
generate_limits.py
File metadata and controls
1460 lines (1275 loc) · 52 KB
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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
Limit Question Bank Generator — Full Curriculum Coverage
Answers verified by SymPy where applicable.
Section 2.1: Tangent Lines & Rates of Change
secant_slope, tangent_line, avg_vs_instantaneous_rate
Section 2.2–2.5: Evaluating Limits
direct_evaluation, rational_factoring, radical_rationalizing,
one_sided_limit, trig_standard, exponential_standard,
difference_quotient, lhopital_basic
Section 2.6–2.8: Infinite Limits & Limits at Infinity
vertical_asymptote, rational_at_infinity, transcendental_at_infinity,
radical_at_infinity
Section 2.9: Continuity
piecewise_continuity, ivt_application, removable_discontinuity,
continuity_unknown
Section 2.10: Formal Definition
epsilon_delta
Advanced / Scholar:
squeeze_theorem, lhopital_advanced, infinity_advanced,
exponent_indeterminate, dne_absolute_value, absolute_value_limit
"""
import sympy as sp
import json
import random
import os
import sys
# Figure generation (optional — gracefully degrades if matplotlib unavailable)
_fig_gen_available = False
_fig_gen = None
_FIGURE_TYPES_LIMITS = set()
_figures_dir_limits = ""
try:
import worksheet_figure_gen as _fig_gen
_FIGURE_TYPES_LIMITS = _fig_gen.FIGURE_TYPES_LIMITS
_figures_dir_limits = os.path.join(
os.path.dirname(os.path.abspath(__file__)),
"src", "main", "webapp", "worksheet", "math", "calculus",
"figures", "limits")
_fig_gen_available = True
print("Figure generation enabled.")
except ImportError:
print("worksheet_figure_gen not available — skipping figure generation.")
x, h, t, k_sym = sp.symbols('x h t k')
# ---------------------------------------------------------------------------
# Helpers
# ---------------------------------------------------------------------------
NZ = [-4, -3, -2, -1, 1, 2, 3, 4]
POS = [1, 2, 3, 4]
SML = [-2, -1, 1, 2]
def rc(pool):
return random.choice(pool)
def safe_limit(f, var, a, direction='+-'):
"""Evaluate a limit, returning None if it fails or is unevaluable."""
try:
L = sp.limit(f, var, a, dir=direction)
if L.has(sp.zoo) or L == sp.nan or L.has(sp.Integral):
return None
return L
except Exception:
return None
def fmt_ans(L):
"""Format a limit result into (latex, plain) strings."""
if L == sp.oo:
return "\\infty", "Infinity"
elif L == -sp.oo:
return "-\\infty", "-Infinity"
else:
return sp.latex(L), str(L)
def fmt_dne():
return "\\text{DNE}", "Does Not Exist"
# ---------------------------------------------------------------------------
# Section 2.1: Tangent Lines & Rates of Change
# ---------------------------------------------------------------------------
def gen_secant_slope():
"""Secant slope estimation: m_PQ = (f(x)-f(a))/(x-a)."""
a_val = rc([1, 2, 3, -1, -2])
c1, c2, c3 = rc(NZ), rc(NZ), rc(POS)
funcs = [
(c1*x**2 + c2*x + c3, "polynomial"),
(sp.sqrt(x + c3), "radical"),
(c1*sp.sin(x) + c2, "trig"),
(c1*sp.exp(x/c3), "exponential"),
(sp.Rational(c1, 1) / (x + c3), "rational"),
]
expr, label = rc(funcs)
# Ensure a_val is in domain
try:
fa = expr.subs(x, a_val)
if not fa.is_finite:
expr = c1*x**2 + c2*x + c3
fa = expr.subs(x, a_val)
except Exception:
expr = c1*x**2 + c2*x + c3
fa = expr.subs(x, a_val)
# Pick a nearby point for secant
x_val = a_val + rc([sp.Rational(1,10), sp.Rational(1,2), 1, sp.Rational(-1,2)])
try:
fx = expr.subs(x, x_val)
if not fx.is_finite:
return None
except Exception:
return None
slope = sp.Rational(fx - fa, x_val - a_val) if (fx - fa).is_rational and (x_val - a_val).is_rational else (fx - fa) / (x_val - a_val)
slope = sp.simplify(slope)
q = (f"Let \\( f(x) = {sp.latex(expr)} \\). "
f"Compute the slope of the secant line between "
f"\\( x = {sp.latex(sp.Integer(a_val) if isinstance(a_val, int) else a_val)} \\) and "
f"\\( x = {sp.latex(x_val)} \\).")
al, ap = sp.latex(slope), str(slope)
return {"q_text": q, "f": expr, "a": a_val, "type": "secant_slope",
"dir": "+-", "var": x, "ans_latex": al, "ans_plain": ap}
def gen_tangent_line():
"""Tangent line equation via limit definition: y - f(a) = m_tan(x - a)."""
a_val = rc([-2, -1, 0, 1, 2, 3])
c1, c2, c3 = rc(SML), rc(SML), rc(POS)
funcs = [
c1*x**2 + c2*x + c3,
c1*x**3 + c2,
sp.Rational(c1, 1) * sp.sqrt(x + c3 + abs(a_val) + 1),
]
expr = rc(funcs)
try:
fa = sp.simplify(expr.subs(x, a_val))
deriv = sp.diff(expr, x)
m = sp.simplify(deriv.subs(x, a_val))
if not fa.is_finite or not m.is_finite:
return None
except Exception:
return None
# y = m(x-a) + fa
line_expr = sp.expand(m*(x - a_val) + fa)
q = (f"Find the equation of the tangent line to \\( f(x) = {sp.latex(expr)} \\) "
f"at \\( x = {sp.latex(sp.Integer(a_val) if isinstance(a_val, int) else a_val)} \\). "
f"Express in slope-intercept form \\( y = mx + b \\).")
al = f"y = {sp.latex(line_expr)}"
ap = f"y = {line_expr}"
return {"q_text": q, "f": expr, "a": a_val, "type": "tangent_line",
"dir": "+-", "var": x, "ans_latex": al, "ans_plain": ap}
def gen_avg_vs_instantaneous_rate():
"""Average rate on [a,b] and instantaneous rate at a point."""
a_val = rc([0, 1, 2])
b_val = a_val + rc([1, 2, 3])
c1, c2, c3 = rc(SML), rc(NZ), rc(POS)
# Use t as the variable for "rate of change" context
funcs = [
(c1*t**2 + c2*t + c3, "position"),
(-sp.Rational(1,2)*c3*t**2 + c2*t + 10, "height"),
(c3*sp.exp(sp.Rational(c1, 2)*t), "population"),
]
expr, context = rc(funcs)
try:
fa = sp.simplify(expr.subs(t, a_val))
fb = sp.simplify(expr.subs(t, b_val))
if not fa.is_finite or not fb.is_finite:
return None
except Exception:
return None
avg_rate = sp.simplify((fb - fa) / (b_val - a_val))
sub = rc(["avg", "inst"])
if sub == "avg":
q = (f"Let \\( s(t) = {sp.latex(expr)} \\). "
f"Find the average rate of change on "
f"\\( [{sp.latex(sp.Integer(a_val))}, {sp.latex(sp.Integer(b_val))}] \\).")
al, ap = sp.latex(avg_rate), str(avg_rate)
else:
pt = rc([a_val, b_val])
deriv = sp.diff(expr, t)
inst = sp.simplify(deriv.subs(t, pt))
if not inst.is_finite:
return None
q = (f"Let \\( s(t) = {sp.latex(expr)} \\). "
f"Find the instantaneous rate of change at "
f"\\( t = {sp.latex(sp.Integer(pt))} \\).")
al, ap = sp.latex(inst), str(inst)
return {"q_text": q, "f": expr, "a": a_val, "type": "avg_vs_instantaneous_rate",
"dir": "+-", "var": t, "ans_latex": al, "ans_plain": ap}
# ---------------------------------------------------------------------------
# Section 2.2–2.5: Evaluating Limits
# ---------------------------------------------------------------------------
def gen_direct_evaluation():
"""Direct substitution — continuous function at the point."""
a_val = rc([-2, -1, 0, 1, 2, 3])
c1, c2, c3 = rc(NZ), rc(NZ), rc(POS)
funcs = [
c1*x**2 + c2*x + c3,
c1*sp.exp(c2*x),
sp.sin(c1*x) + c2,
sp.cos(c1*x) * c2,
(c1*x + c2) / (x + c3 + abs(a_val) + 1), # avoid div by zero
sp.sqrt(x**2 + c3),
]
f = rc(funcs)
L = safe_limit(f, x, a_val)
if L is None:
return None
al, ap = fmt_ans(L)
return {"q_text": None, "f": f, "a": a_val, "type": "direct_evaluation",
"dir": "+-", "var": x, "ans_latex": al, "ans_plain": ap}
def gen_rational_factoring():
"""0/0 indeterminate resolved by factoring: (x-a) cancels."""
a_val = rc([-3, -2, -1, 1, 2, 3])
r2 = rc([v for v in NZ if v != a_val])
c = rc(POS)
sub = rc(["quadratic_linear", "quadratic_quadratic", "cubic_linear"])
if sub == "quadratic_linear":
numer = sp.expand(c * (x - a_val) * (x - r2))
denom = sp.expand(x - a_val)
elif sub == "quadratic_quadratic":
r3 = rc([v for v in NZ if v != a_val])
numer = sp.expand((x - a_val) * (x - r2))
denom = sp.expand((x - a_val) * (x - r3))
else:
r2b = rc([v for v in NZ if v != a_val])
numer = sp.expand((x - a_val) * (x - r2) * (x - r2b))
denom = sp.expand(x - a_val)
f = numer / denom
L = safe_limit(f, x, a_val)
if L is None:
return None
al, ap = fmt_ans(L)
return {"q_text": None, "f": f, "a": a_val, "type": "rational_factoring",
"dir": "+-", "var": x, "ans_latex": al, "ans_plain": ap}
def gen_radical_rationalizing():
"""Limits requiring multiply by conjugate: (sqrt(g(x)) - c) / h(x)."""
a_val = rc([0, 1, 2, 3, 4])
c1 = rc(POS)
sub = rc(["sqrt_minus", "sqrt_diff", "sqrt_plus_over"])
if sub == "sqrt_minus":
# (sqrt(x+c1) - sqrt(a+c1)) / (x - a)
val_at_a = sp.sqrt(a_val + c1)
f = (sp.sqrt(x + c1) - val_at_a) / (x - a_val)
elif sub == "sqrt_diff":
# (sqrt(x) - sqrt(a)) / (x - a) for a > 0
if a_val <= 0:
a_val = rc([1, 2, 4])
f = (sp.sqrt(x) - sp.sqrt(a_val)) / (x - a_val)
else:
# (x - a) / (sqrt(x+c1) - sqrt(a+c1))
f = (x - a_val) / (sp.sqrt(x + c1) - sp.sqrt(a_val + c1))
L = safe_limit(f, x, a_val)
if L is None:
return None
al, ap = fmt_ans(sp.simplify(L))
return {"q_text": None, "f": f, "a": a_val, "type": "radical_rationalizing",
"dir": "+-", "var": x, "ans_latex": al, "ans_plain": ap}
def gen_one_sided_limit():
"""One-sided limits where left and right may differ or one may be ±∞."""
a_val = rc([-2, -1, 0, 1, 2, 3])
c1, c2 = rc(NZ), rc(NZ)
direction = rc(['+', '-'])
sub = rc(["rational_pole", "abs_value", "sqrt_boundary", "piecewise_simple"])
if sub == "rational_pole":
f = (c1*x + c2) / (x - a_val)
elif sub == "abs_value":
f = sp.Abs(x - a_val) / (x - a_val)
elif sub == "sqrt_boundary":
# sqrt(x - a) from the right only
direction = '+'
f = sp.sqrt(x - a_val) / (x - a_val + 1)
else:
# floor/ceiling style: different expression each side
if direction == '+':
f = c1*x + c2 + 1
else:
f = c1*x**2 + c2
L = safe_limit(f, x, a_val, direction)
if L is None:
return None
al, ap = fmt_ans(L)
q_dir = "^+" if direction == '+' else "^-"
q = (f"Evaluate the one-sided limit: \\( \\lim_{{x \\to {sp.latex(sp.Integer(a_val) if isinstance(a_val, int) else a_val)}{q_dir}}} "
f"\\left({sp.latex(f)}\\right) \\)")
return {"q_text": q, "f": f, "a": a_val, "type": "one_sided_limit",
"dir": direction, "var": x, "ans_latex": al, "ans_plain": ap}
def gen_trig_standard():
"""Standard trig limits: sin(ax)/bx, (1-cos(ax))/x, tan(ax)/bx, etc."""
c1, c2 = rc(SML), rc(SML)
if c2 == 0: c2 = 1
sub = rc(["sin_over_x", "one_minus_cos", "tan_over_x", "sin_over_sin", "x_over_sin"])
if sub == "sin_over_x":
f = sp.sin(c1*x) / (c2*x)
elif sub == "one_minus_cos":
f = (1 - sp.cos(c1*x)) / x
elif sub == "tan_over_x":
f = sp.tan(c1*x) / (c2*x)
elif sub == "sin_over_sin":
f = sp.sin(c1*x) / sp.sin(c2*x)
else:
f = (c2*x) / sp.sin(c1*x)
L = safe_limit(f, x, 0)
if L is None:
return None
al, ap = fmt_ans(sp.simplify(L))
return {"q_text": None, "f": f, "a": 0, "type": "trig_standard",
"dir": "+-", "var": x, "ans_latex": al, "ans_plain": ap}
def gen_exponential_standard():
"""Exponential/log limits at 0 or ∞: (e^(ax)-1)/bx, ln-based, etc."""
c1, c2 = rc(SML), rc(SML)
if c2 == 0: c2 = 1
sub = rc(["exp_minus_1", "ln_1_plus", "exp_ratio", "log_over_power"])
if sub == "exp_minus_1":
f = (sp.exp(c1*x) - 1) / (c2*x)
a_val = 0
elif sub == "ln_1_plus":
f = sp.ln(1 + c1*x) / (c2*x)
a_val = 0
elif sub == "exp_ratio":
f = sp.exp(c1*x) / sp.exp(c2*x)
a_val = sp.oo
else:
n = rc([1, 2, 3])
f = sp.ln(x) / x**n
a_val = sp.oo
L = safe_limit(f, x, a_val)
if L is None:
return None
al, ap = fmt_ans(sp.simplify(L))
return {"q_text": None, "f": f, "a": a_val, "type": "exponential_standard",
"dir": "+-", "var": x, "ans_latex": al, "ans_plain": ap}
def gen_difference_quotient():
"""Difference quotient: lim_{h->0} (f(a+h)-f(a))/h."""
a_val = rc([-1, 0, 1, 2, 3])
c1, c2, c3 = rc(SML), rc(NZ), rc(POS)
funcs = [
c1*x**2 + c2*x,
c1*x**3,
sp.Rational(c1, 1) / x if a_val != 0 else c1*x**2,
sp.sqrt(x + c3),
c1*sp.sin(x),
]
base_f = rc(funcs)
f = (base_f.subs(x, a_val + h) - base_f.subs(x, a_val)) / h
f = sp.simplify(f)
L = safe_limit(f, h, 0)
if L is None:
return None
al, ap = fmt_ans(sp.simplify(L))
q = (f"Evaluate the difference quotient limit: "
f"\\( \\lim_{{h \\to 0}} \\frac{{f({sp.latex(sp.Integer(a_val) if isinstance(a_val, int) else a_val)}+h) - f({sp.latex(sp.Integer(a_val) if isinstance(a_val, int) else a_val)})}}{{h}} \\) "
f"where \\( f(x) = {sp.latex(base_f)} \\).")
return {"q_text": q, "f": f, "a": 0, "type": "difference_quotient",
"dir": "+-", "var": h, "ans_latex": al, "ans_plain": ap}
def gen_lhopital_basic():
"""L'Hôpital once: 0/0 or ∞/∞ at a point."""
c1, c2 = rc(SML), rc(SML)
if c2 == 0: c2 = 1
sub = rc(["zero_zero_trig", "zero_zero_exp", "inf_inf"])
if sub == "zero_zero_trig":
a_val = 0
f = rc([
(sp.sin(c1*x) - c1*x*sp.cos(c1*x)) / (x - sp.sin(x)),
(sp.tan(c1*x) - sp.sin(c1*x)) / x**3,
(sp.asin(c1*x)) / (c2*x),
(sp.atan(c1*x)) / (c2*x),
])
elif sub == "zero_zero_exp":
a_val = 0
f = rc([
(sp.exp(c1*x) - 1 - c1*x) / x**2,
x * sp.ln(x), # 0·(-∞) form, rewrite as ln(x)/(1/x)
])
if f == x * sp.ln(x):
a_val = 0
# need direction from right
L = safe_limit(f, x, a_val, '+')
if L is None:
return None
al, ap = fmt_ans(sp.simplify(L))
q = f"Evaluate the limit: \\( \\lim_{{x \\to 0^+}} {sp.latex(f)} \\)"
return {"q_text": q, "f": f, "a": a_val, "type": "lhopital_basic",
"dir": "+", "var": x, "ans_latex": al, "ans_plain": ap}
else:
a_val = sp.oo
n1, n2 = rc([1, 2, 3]), rc([1, 2, 3])
f = rc([
x**n1 / sp.exp(x),
sp.ln(x)**n1 / x**n2,
x**n1 / sp.ln(x)**n2 if n2 > 0 else x / sp.exp(x),
])
L = safe_limit(f, x, a_val)
if L is None:
return None
al, ap = fmt_ans(sp.simplify(L))
return {"q_text": None, "f": f, "a": a_val, "type": "lhopital_basic",
"dir": "+-", "var": x, "ans_latex": al, "ans_plain": ap}
# ---------------------------------------------------------------------------
# Section 2.6–2.8: Infinite Limits & Limits at Infinity
# ---------------------------------------------------------------------------
def gen_vertical_asymptote():
"""Limits at vertical asymptotes: lim_{x->a} c/(x-a)^n = ±∞."""
a_val = rc([-2, -1, 0, 1, 2, 3])
c1 = rc(NZ)
n = rc([1, 2, 3])
direction = rc(['+', '-'])
sub = rc(["simple_pole", "rational_pole", "log_pole"])
if sub == "simple_pole":
f = sp.Rational(c1, 1) / (x - a_val)**n
elif sub == "rational_pole":
c2 = rc(NZ)
f = (c1*x + c2) / ((x - a_val) * (x + rc(POS)))
else:
# ln(|x - a|)
f = sp.ln(sp.Abs(x - a_val))
direction = '+'
L = safe_limit(f, x, a_val, direction)
if L is None:
return None
al, ap = fmt_ans(L)
q_dir = "^+" if direction == '+' else "^-"
q = (f"Evaluate the limit: \\( \\lim_{{x \\to {sp.latex(sp.Integer(a_val) if isinstance(a_val, int) else a_val)}{q_dir}}} "
f"\\left({sp.latex(f)}\\right) \\)")
return {"q_text": q, "f": f, "a": a_val, "type": "vertical_asymptote",
"dir": direction, "var": x, "ans_latex": al, "ans_plain": ap}
def gen_rational_at_infinity():
"""Rational functions as x -> ±∞: leading coefficient comparison."""
c1, c2, c3, c4 = rc(NZ), rc(NZ), rc(NZ), rc(NZ)
direction = rc(['+-']) # for oo we use +-
a_val = rc([sp.oo, -sp.oo])
sub = rc(["same_degree", "top_heavy", "bottom_heavy", "with_radical"])
if sub == "same_degree":
n = rc([1, 2, 3])
f = (c1*x**n + c2*x**(n-1) + 1) / (c3*x**n + c4)
elif sub == "top_heavy":
f = (c1*x**3 + c2) / (c3*x**2 + c4*x + 1)
elif sub == "bottom_heavy":
f = (c1*x + c2) / (c3*x**2 + c4*x + 1)
else:
# sqrt(ax^2 + b) / (cx + d) ~ |a|^{1/2}/c
a_coeff = rc(POS)
f = sp.sqrt(a_coeff*x**2 + rc(NZ)) / (c3*x + c4)
L = safe_limit(f, x, a_val)
if L is None:
return None
al, ap = fmt_ans(sp.simplify(L))
return {"q_text": None, "f": f, "a": a_val, "type": "rational_at_infinity",
"dir": "+-", "var": x, "ans_latex": al, "ans_plain": ap}
def gen_transcendental_at_infinity():
"""Transcendental limits at ∞: e^{g(x)}, ln(g(x)), mixed."""
c1 = rc(NZ)
c2 = rc(POS)
a_val = sp.oo
sub = rc(["exp_dominates", "log_slow", "exp_decay", "mixed_exp_poly",
"arctan_inf", "exp_difference"])
if sub == "exp_dominates":
f = sp.exp(c1*x) / (x**rc([2, 3, 4]) + 1)
elif sub == "log_slow":
f = sp.ln(x**c2 + 1) / x
elif sub == "exp_decay":
f = x**rc([2, 3]) * sp.exp(-x)
elif sub == "mixed_exp_poly":
f = (sp.exp(x) + x**2) / (sp.exp(x) - x)
elif sub == "arctan_inf":
f = sp.atan(c1*x)
else:
# e^x - e^{x-a}
f = sp.exp(x) - sp.exp(x - c2)
L = safe_limit(f, x, a_val)
if L is None:
return None
al, ap = fmt_ans(sp.simplify(L))
return {"q_text": None, "f": f, "a": a_val, "type": "transcendental_at_infinity",
"dir": "+-", "var": x, "ans_latex": al, "ans_plain": ap}
def gen_radical_at_infinity():
"""Limits with radicals at infinity: sqrt(x^2+ax) - x, etc."""
c1 = rc(NZ)
a_val = rc([sp.oo, -sp.oo])
sub = rc(["sqrt_minus_x", "sqrt_diff", "nested_sqrt"])
if sub == "sqrt_minus_x":
f = sp.sqrt(x**2 + c1*x) - x if a_val == sp.oo else sp.sqrt(x**2 + c1*x) + x
elif sub == "sqrt_diff":
c2 = rc(POS)
f = sp.sqrt(x + c1) - sp.sqrt(x + c2)
a_val = sp.oo
else:
f = sp.sqrt(sp.sqrt(x**4 + c1*x**2) - x**2)
a_val = sp.oo
L = safe_limit(f, x, a_val)
if L is None:
return None
al, ap = fmt_ans(sp.simplify(L))
return {"q_text": None, "f": f, "a": a_val, "type": "radical_at_infinity",
"dir": "+-", "var": x, "ans_latex": al, "ans_plain": ap}
# ---------------------------------------------------------------------------
# Section 2.9: Continuity
# ---------------------------------------------------------------------------
def gen_piecewise_continuity():
"""Find k so that a piecewise function is continuous at x = a."""
a_val = rc([-1, 0, 1, 2, 3])
c1, c2, c3 = rc(NZ), rc(NZ), rc(POS)
sub = rc(["poly_poly", "trig_poly", "exp_poly"])
if sub == "poly_poly":
left_expr = c1*x**2 + c2*x + k_sym
right_expr = c3*x + rc(NZ)
elif sub == "trig_poly":
left_expr = k_sym * sp.sin(x - a_val) / (x - a_val) if a_val != 0 else k_sym * sp.sin(x) / x
right_expr = c1*x + c3
# Left limit as x->a is k (since sin(u)/u -> 1)
else:
left_expr = k_sym * sp.exp(x - a_val)
right_expr = c1*x**2 + c2
# For continuity: lim_{x->a-} left = lim_{x->a+} right = f(a)
right_val = right_expr.subs(x, a_val)
if not right_val.is_finite:
return None
# Solve for k: left_limit = right_val
try:
left_lim = sp.limit(left_expr, x, a_val, '-')
k_solutions = sp.solve(left_lim - right_val, k_sym)
if not k_solutions:
return None
k_val = k_solutions[0]
if not sp.simplify(k_val).is_finite:
return None
except Exception:
return None
left_disp = sp.latex(left_expr)
right_disp = sp.latex(right_expr)
a_disp = sp.latex(sp.Integer(a_val) if isinstance(a_val, int) else a_val)
q = (f"Find the value of \\( k \\) that makes the function continuous at "
f"\\( x = {a_disp} \\): "
f"\\( f(x) = \\begin{{cases}} {left_disp} & x < {a_disp} "
f"\\\\ {right_disp} & x \\ge {a_disp} \\end{{cases}} \\)")
al = sp.latex(sp.simplify(k_val))
ap = str(sp.simplify(k_val))
return {"q_text": q, "f": left_expr, "a": a_val, "type": "piecewise_continuity",
"dir": "+-", "var": x, "ans_latex": al, "ans_plain": ap}
def gen_ivt_application():
"""IVT: show a root exists on [a,b] or find guaranteed interval."""
a_val = rc([0, 1, -1, 2])
b_val = a_val + rc([1, 2, 3])
c1, c2 = rc(NZ), rc(NZ)
funcs = [
x**3 + c1*x + c2,
sp.cos(x) - x + c2,
sp.exp(x) + c1*x - 3,
x**2 + c1*x + c2,
]
expr = rc(funcs)
try:
fa = sp.simplify(expr.subs(x, a_val))
fb = sp.simplify(expr.subs(x, b_val))
if not fa.is_finite or not fb.is_finite:
return None
# Need sign change for IVT to guarantee root
if fa * fb >= 0:
return None
except Exception:
return None
a_disp = sp.latex(sp.Integer(a_val) if isinstance(a_val, int) else a_val)
b_disp = sp.latex(sp.Integer(b_val) if isinstance(b_val, int) else b_val)
q = (f"Let \\( f(x) = {sp.latex(expr)} \\). "
f"Show that by the Intermediate Value Theorem, "
f"\\( f \\) has at least one root on \\( [{a_disp}, {b_disp}] \\). "
f"Compute \\( f({a_disp}) \\) and \\( f({b_disp}) \\).")
al = (f"f({a_disp}) = {sp.latex(fa)},\\; f({b_disp}) = {sp.latex(fb)}"
"\\text{ (sign change} \\Rightarrow \\text{root exists)}")
ap = f"f({a_val}) = {fa}, f({b_val}) = {fb}, sign change => root exists by IVT"
return {"q_text": q, "f": expr, "a": a_val, "type": "ivt_application",
"dir": "+-", "var": x, "ans_latex": al, "ans_plain": ap}
def gen_removable_discontinuity():
"""Identify removable discontinuity and find the limit."""
a_val = rc([-3, -2, -1, 1, 2, 3])
c1 = rc(NZ)
r2 = rc([v for v in NZ if v != a_val])
sub = rc(["rational", "trig_sinc"])
if sub == "rational":
numer = sp.expand((x - a_val) * (c1*x + r2))
denom = sp.expand(x - a_val)
f = numer / denom
else:
# sin(x-a)/(x-a) has removable discontinuity at a
f = sp.sin(x - a_val) / (x - a_val)
L = safe_limit(f, x, a_val)
if L is None:
return None
a_disp = sp.latex(sp.Integer(a_val) if isinstance(a_val, int) else a_val)
q = (f"The function \\( f(x) = {sp.latex(f)} \\) has a removable discontinuity at "
f"\\( x = {a_disp} \\). Find the value that makes \\( f \\) continuous there.")
al, ap = fmt_ans(sp.simplify(L))
return {"q_text": q, "f": f, "a": a_val, "type": "removable_discontinuity",
"dir": "+-", "var": x, "ans_latex": al, "ans_plain": ap}
def gen_continuity_unknown():
"""Find k so limit exists: (x^2 - kx + c) / (x - a)."""
a_val = rc([1, 2, 3, -1, -2, -3])
c_val = a_val * rc([1, 2, 3, -1, -2])
k_val = a_val + c_val // a_val
f = (x**2 - k_sym*x + c_val) / (x - a_val)
q = (f"Find the numerical value of the constant \\( k \\) that makes the limit exist: "
f"\\( \\lim_{{x \\to {sp.latex(sp.Integer(a_val))}}} \\left({sp.latex(f)}\\right) \\)")
al = sp.latex(sp.Integer(k_val))
ap = str(k_val)
return {"q_text": q, "f": f, "a": a_val, "type": "continuity_unknown",
"dir": "+-", "var": x, "ans_latex": al, "ans_plain": ap}
# ---------------------------------------------------------------------------
# Section 2.10: Formal Definition (ε-δ)
# ---------------------------------------------------------------------------
def gen_epsilon_delta():
"""ε-δ proof: find δ in terms of ε for a linear or simple function."""
a_val = rc([-2, -1, 0, 1, 2, 3])
c1 = rc(NZ)
c2 = rc(NZ)
sub = rc(["linear", "quadratic_at_point", "sqrt_bound"])
if sub == "linear":
# f(x) = c1*x + c2, L = c1*a + c2, |f(x)-L| = |c1||x-a| < ε => δ = ε/|c1|
f_expr = c1*x + c2
L_val = c1*a_val + c2
delta_expr = sp.Symbol('varepsilon') / abs(c1)
q = (f"Using the \\( \\varepsilon\\text{{-}}\\delta \\) definition, prove that "
f"\\( \\lim_{{x \\to {sp.latex(sp.Integer(a_val))}}} ({sp.latex(f_expr)}) = {sp.latex(sp.Integer(L_val))} \\). "
f"Find \\( \\delta \\) in terms of \\( \\varepsilon \\).")
al = f"\\delta = \\frac{{\\varepsilon}}{{{abs(c1)}}}"
ap = f"delta = epsilon / {abs(c1)}"
elif sub == "quadratic_at_point":
# f(x) = x^2 at x=a, restrict |x-a| < 1 first, then δ = min(1, ε/(2|a|+1))
f_expr = x**2
L_val = a_val**2
q = (f"Using the \\( \\varepsilon\\text{{-}}\\delta \\) definition, prove that "
f"\\( \\lim_{{x \\to {sp.latex(sp.Integer(a_val))}}} x^2 = {sp.latex(sp.Integer(L_val))} \\). "
f"Find \\( \\delta \\) in terms of \\( \\varepsilon \\).")
bound = 2*abs(a_val) + 1
al = f"\\delta = \\min\\left(1,\\; \\frac{{\\varepsilon}}{{{bound}}}\\right)"
ap = f"delta = min(1, epsilon / {bound})"
else:
# f(x) = sqrt(x) at x = a (a>0)
a_val = rc([1, 4, 9])
sqrt_a = int(a_val**0.5)
f_expr = sp.sqrt(x)
q = (f"Using the \\( \\varepsilon\\text{{-}}\\delta \\) definition, prove that "
f"\\( \\lim_{{x \\to {a_val}}} \\sqrt{{x}} = {sqrt_a} \\). "
f"Find \\( \\delta \\) in terms of \\( \\varepsilon \\).")
al = f"\\delta = \\min\\left({sqrt_a}\\varepsilon,\\; {sqrt_a}^2\\right) = {sqrt_a}\\varepsilon \\text{{ (for small }} \\varepsilon\\text{{)}}"
ap = f"delta = {sqrt_a} * epsilon (for small epsilon)"
return {"q_text": q, "f": f_expr, "a": a_val, "type": "epsilon_delta",
"dir": "+-", "var": x, "ans_latex": al, "ans_plain": ap}
# ---------------------------------------------------------------------------
# Advanced / Scholar
# ---------------------------------------------------------------------------
def gen_squeeze_theorem():
"""Squeeze theorem: x^n sin(c/x) or x^n cos(c/x) -> 0."""
c1 = rc(NZ)
n = rc([2, 3])
f = x**n * rc([sp.sin(c1/x), sp.cos(c1/x)])
L = safe_limit(f, x, 0)
if L is None:
return None
q = (f"Use the Squeeze Theorem to evaluate: "
f"\\( \\lim_{{x \\to 0}} {sp.latex(f)} \\)")
al, ap = fmt_ans(L)
return {"q_text": q, "f": f, "a": 0, "type": "squeeze_theorem",
"dir": "+-", "var": x, "ans_latex": al, "ans_plain": ap}
def gen_lhopital_advanced():
"""L'Hôpital requiring 2+ applications or algebraic rewriting."""
c1 = rc(SML)
a_val = rc([0, sp.oo, sp.pi/2])
if a_val == 0:
funcs = [
(sp.sin(c1*x) - c1*x) / x**3,
(x - sp.sin(x)) / (x - sp.tan(x)),
(sp.exp(x) - 1 - x - x**2/2) / x**3,
1/sp.sin(x) - 1/x,
(sp.cos(x) - 1 + x**2/2) / x**4,
]
elif a_val == sp.oo:
funcs = [
x * sp.sin(c1/x),
(1 + c1/x)**x,
x * (sp.ln(x + 1) - sp.ln(x)),
(x**2 + 1)**(sp.Rational(1, 2)) - x,
]
else: # pi/2
funcs = [
(sp.pi/2 - x) * sp.tan(x),
sp.cos(x) / (x - sp.pi/2),
]
f = rc(funcs)
L = safe_limit(f, x, a_val)
if L is None:
return None
al, ap = fmt_ans(sp.simplify(L))
return {"q_text": None, "f": f, "a": a_val, "type": "lhopital_advanced",
"dir": "+-", "var": x, "ans_latex": al, "ans_plain": ap}
def gen_infinity_advanced():
"""Hard limits at infinity: conjugate tricks, (1+1/n)^n style."""
c1, c2 = rc(NZ), rc(NZ)
a_val = rc([sp.oo, -sp.oo])
funcs = [
sp.sqrt(x**2 + c1*x) - x if a_val == sp.oo else sp.sqrt(x**2 + c1*x) + x,
(1 + c1/x)**x,
(1 + c1/x + c2/x**2)**x,
sp.Rational(c1, 1) * x * (sp.exp(1/x) - 1),
]
f = rc(funcs)
L = safe_limit(f, x, a_val)
if L is None:
return None
al, ap = fmt_ans(sp.simplify(L))
return {"q_text": None, "f": f, "a": a_val, "type": "infinity_advanced",
"dir": "+-", "var": x, "ans_latex": al, "ans_plain": ap}
def gen_exponent_indeterminate():
"""Indeterminate exponent forms: 1^∞, 0^0, ∞^0."""
a_val = rc([0, sp.oo, 1])
c1 = rc(SML)
c2 = rc(POS)
direction = '+-'
if a_val == 0:
funcs = [
(sp.cos(x))**(1/x**2),
x**x,
(sp.sin(x)/x)**(1/x**2),
(1 + c1*x)**(1/x),
x**(sp.sin(x)),
]
f = rc(funcs)
if f == x**x or f == x**(sp.sin(x)):
direction = '+'
elif a_val == sp.oo:
funcs = [
x**(1/x),
(1 + c1/x + c2/x**2)**x,
x**(c1/sp.ln(x)),
]
f = rc(funcs)
else: # a = 1
funcs = [
x**(1/(1-x)),
x**(sp.Rational(1, 1)/(x - 1)),
(2 - x)**(sp.tan(sp.pi*x/2)),
]
f = rc(funcs)
L = safe_limit(f, x, a_val, direction)
if L is None:
return None
al, ap = fmt_ans(sp.simplify(L))
return {"q_text": None, "f": f, "a": a_val, "type": "exponent_indeterminate",
"dir": direction, "var": x, "ans_latex": al, "ans_plain": ap}
# ---------------------------------------------------------------------------
# Graph Reading Questions — helpers
# ---------------------------------------------------------------------------
_Y_POOL = list(range(-5, 8)) # [-5, 7]
def _pick_y(used):
"""Pick an unused integer y-value from _Y_POOL."""
avail = [v for v in _Y_POOL if v not in used]
if not avail:
avail = _Y_POOL # fallback: allow repeats
y = random.choice(avail)
used.add(y)
return y
def _fit_segment(lo, hi, y_lo, y_hi, kind=None):
"""Return a sympy expression (in x) that hits (lo, y_lo) and (hi, y_hi).
kind: 'linear' or 'quadratic'. If None, randomly chosen (70/30)."""
if kind is None:
kind = 'linear' if random.random() < 0.70 else 'quadratic'
if kind == 'linear' or lo == hi:
# y = mx + b through the two points
if hi == lo:
return sp.Integer(y_lo)
m = sp.Rational(y_hi - y_lo, hi - lo)
b = sp.Rational(y_lo) - m * lo
return sp.simplify(m * x + b)
else:
# y = a*x^2 + b*x + c — pick a random nonzero 'a', solve for b, c
a_coeff = sp.Rational(random.choice([-1, 1]), random.choice([1, 2]))
# y_lo = a*lo^2 + b*lo + c
# y_hi = a*hi^2 + b*hi + c
# Two equations, two unknowns (b, c)
b_sym, c_sym = sp.symbols('_b _c')
eqs = [
a_coeff * lo**2 + b_sym * lo + c_sym - y_lo,
a_coeff * hi**2 + b_sym * hi + c_sym - y_hi,
]
sol = sp.solve(eqs, [b_sym, c_sym])
if not sol:
# Fallback to linear
return _fit_segment(lo, hi, y_lo, y_hi, kind='linear')
return sp.simplify(a_coeff * x**2 + sol[b_sym] * x + sol[c_sym])
def _build_graph_def():
"""Build a random piecewise graph definition with 2-4 special join points."""
domain = (-4, 6)
n_joins = random.randint(2, 4)
interior = list(range(domain[0] + 1, domain[1])) # -3..5
joins = sorted(random.sample(interior, n_joins))
used_y = set()
# Assign feature types
feature_weights = ['jump'] * 35 + ['removable'] * 25 + ['continuous'] * 25 + ['infinite'] * 15
features = [random.choice(feature_weights) for _ in joins]
# Guarantee at least one non-continuous
if all(f == 'continuous' for f in features):
features[random.randint(0, len(features) - 1)] = random.choice(['jump', 'removable'])
# Build special points and determine y-values at each boundary
# boundary_y[i] = (y_left_limit, y_right_limit, f_val) at joins[i]
special_points = []
boundary_y = []
for i, (jx, feat) in enumerate(zip(joins, features)):
if feat == 'jump':
y_left = _pick_y(used_y)
y_right = _pick_y(used_y)
f_val = random.choice([y_left, y_right])
sp_info = {
"x": jx, "f_val": f_val,
"lim_left": y_left, "lim_right": y_right,
"lim_both": None, "disc_type": "jump",
}
elif feat == 'removable':
y_lim = _pick_y(used_y)
f_val = _pick_y(used_y) # different from limit
sp_info = {
"x": jx, "f_val": f_val,
"lim_left": y_lim, "lim_right": y_lim,
"lim_both": y_lim, "disc_type": "removable",
}
elif feat == 'continuous':
y_val = _pick_y(used_y)
sp_info = {
"x": jx, "f_val": y_val,
"lim_left": y_val, "lim_right": y_val,
"lim_both": y_val, "disc_type": "continuous",
}
else: # infinite
f_val = None
sp_info = {
"x": jx, "f_val": None,
"lim_left": None, "lim_right": None,
"lim_both": None, "disc_type": "infinite",
}
special_points.append(sp_info)
boundary_y.append(sp_info)
# Build segment list
# Boundaries: domain[0], joins[0], joins[1], ..., joins[-1], domain[1]
edges = [domain[0]] + joins + [domain[1]]
segments = []
isolated_points = []
# We need y-values at each edge (left-endpoint of next segment)
# For domain endpoints, pick a y
y_at_domain_start = _pick_y(used_y)
# Determine y-value for the right side of each edge
# edge_right_y[i] = y-value of the curve as we START the segment [edges[i], edges[i+1]]
# edge_left_y[i+1] = y-value of the curve as we END the segment [edges[i], edges[i+1]]
for seg_idx in range(len(edges) - 1):
seg_lo = edges[seg_idx]
seg_hi = edges[seg_idx + 1]
# Determine y at start of this segment
if seg_idx == 0:
y_start = y_at_domain_start
else:
sp_info = special_points[seg_idx - 1]
if sp_info["disc_type"] == "infinite":
# Segment starts after asymptote — pick arbitrary y
y_start = _pick_y(used_y)
else:
y_start = sp_info["lim_right"]
# Determine y at end of this segment
if seg_idx == len(edges) - 2:
y_end = _pick_y(used_y)
else:
sp_info = special_points[seg_idx]
if sp_info["disc_type"] == "infinite":
y_end = _pick_y(used_y)
else:
y_end = sp_info["lim_left"]
# For infinite type at either end, use a rational pole expression
left_is_inf = (seg_idx > 0 and
special_points[seg_idx - 1]["disc_type"] == "infinite")
right_is_inf = (seg_idx < len(special_points) and
special_points[seg_idx]["disc_type"] == "infinite"
if seg_idx < len(special_points) else False)
if left_is_inf or right_is_inf:
# Use a simple rational form: c/(x - pole) + offset
if right_is_inf:
pole = seg_hi
sign = random.choice([-1, 1])
expr = sp.Rational(sign, 1) / (x - pole) + y_start
else:
pole = seg_lo
sign = random.choice([-1, 1])
expr = sp.Rational(sign, 1) / (x - pole) + y_end
else:
expr = _fit_segment(seg_lo, seg_hi, y_start, y_end)