Upload app.py
Browse files
app.py
CHANGED
|
@@ -945,7 +945,131 @@ def run_sympy(problem: str) -> dict:
|
|
| 945 |
"latex": r"x \in \{" + sol_latex + r"\}"
|
| 946 |
}
|
| 947 |
|
| 948 |
-
# ββ 8.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 949 |
elif any(k in p for k in ["matrix", "determinant", "eigenvalue",
|
| 950 |
"eigenvector", "det("]):
|
| 951 |
return {"type": "Matrix", "result": "matrix_detected", "latex": ""}
|
|
@@ -1088,6 +1212,16 @@ def ask_ai(problem: str, sympy_info: dict, history: list) -> str:
|
|
| 1088 |
"D. NEVER recompute sin(pi), cos(pi) etc β sin(pi)=0 exactly, always.\n"
|
| 1089 |
"E. For Lagrange/Newton interpolation: the polynomial is already given above β DO NOT re-expand or re-derive it. Just show the basis polynomials and state the final polynomial from the verified result.\n"
|
| 1090 |
"F. Your final answer must EXACTLY match the verified result β no exceptions.\n\n"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1091 |
"Topics: Calculus, Linear Algebra, Number Theory, ODEs, "
|
| 1092 |
"Numerical Methods, Differential Geometry, Hydro Mechanics, "
|
| 1093 |
"Theory of Numbers, Real Analysis II, General Math."
|
|
|
|
| 945 |
"latex": r"x \in \{" + sol_latex + r"\}"
|
| 946 |
}
|
| 947 |
|
| 948 |
+
# ββ 8. Real Analysis II β SymPy for computations, AI for theory ββ
|
| 949 |
+
elif any(k in p for k in [
|
| 950 |
+
# Sets & Real Numbers
|
| 951 |
+
"supremum", "infimum", "least upper bound", "greatest lower bound",
|
| 952 |
+
"lub", "glb", "archimedean", "bounded set", "completeness",
|
| 953 |
+
"cartesian product", "density of rational", "real number system",
|
| 954 |
+
"field propert", "order propert",
|
| 955 |
+
# Sequences
|
| 956 |
+
"sequence", "cauchy sequence", "bounded sequence",
|
| 957 |
+
"monotone sequence", "subsequence", "bolzano", "weierstrass",
|
| 958 |
+
# Series
|
| 959 |
+
"ratio test", "root test", "integral test", "comparison test",
|
| 960 |
+
"alternating series", "leibniz test", "absolute convergence",
|
| 961 |
+
"conditional convergence", "cauchy criterion",
|
| 962 |
+
"pointwise convergence", "uniform convergence",
|
| 963 |
+
"weierstrass m-test", "m-test",
|
| 964 |
+
# Limits & Continuity
|
| 965 |
+
"epsilon delta", "epsilon-delta", "uniform continuity",
|
| 966 |
+
"intermediate value", "extreme value theorem",
|
| 967 |
+
# Differentiation theorems
|
| 968 |
+
"mean value theorem", "rolle", "taylor's theorem",
|
| 969 |
+
"lhopital", "l'hopital",
|
| 970 |
+
# Riemann Integration
|
| 971 |
+
"riemann sum", "riemann integral", "upper sum", "lower sum",
|
| 972 |
+
"darboux", "integrability", "fundamental theorem of calculus",
|
| 973 |
+
]):
|
| 974 |
+
try:
|
| 975 |
+
n_s = sp.Symbol('n', positive=True)
|
| 976 |
+
|
| 977 |
+
# ββ Sequence limit ββββββββββββββββββββββββββββββββββββββββ
|
| 978 |
+
if any(k in p for k in ["sequence","limit of sequence"]):
|
| 979 |
+
# Extract expression after "of" or "for"
|
| 980 |
+
m = re.search(r"(?:of|for|lim)\s+(.+?)\s*(?:as|when|$)", p)
|
| 981 |
+
if m:
|
| 982 |
+
raw = clean(m.group(1))
|
| 983 |
+
try:
|
| 984 |
+
expr_s = parse_expr(raw, transformations=tfms,
|
| 985 |
+
local_dict={**ld, "n": n_s})
|
| 986 |
+
lim_val = sp.limit(expr_s, n_s, sp.oo)
|
| 987 |
+
return {
|
| 988 |
+
"type": "SequenceLimit",
|
| 989 |
+
"result": f"lim({m.group(1)}) as nββ = {lim_val}",
|
| 990 |
+
"latex": f"\\lim_{{n\\to\\infty}} = {sp.latex(lim_val)}"
|
| 991 |
+
}
|
| 992 |
+
except Exception:
|
| 993 |
+
pass
|
| 994 |
+
|
| 995 |
+
# ββ Series sum ββββββββββββββββββββββββββββββββββββββββββββ
|
| 996 |
+
elif any(k in p for k in ["series","sum of"]):
|
| 997 |
+
m = re.search(r"(?:sum|series)\s+(?:of\s+)?(.+?)\s*(?:from|$)", p)
|
| 998 |
+
if m:
|
| 999 |
+
raw = clean(m.group(1))
|
| 1000 |
+
try:
|
| 1001 |
+
expr_ser = parse_expr(raw, transformations=tfms,
|
| 1002 |
+
local_dict={**ld, "n": n_s})
|
| 1003 |
+
s_val = sp.summation(expr_ser, (n_s, 1, sp.oo))
|
| 1004 |
+
converges = s_val.is_finite
|
| 1005 |
+
return {
|
| 1006 |
+
"type": "SeriesConvergence",
|
| 1007 |
+
"result": (f"Series sum = {s_val}, "
|
| 1008 |
+
f"Converges: {converges}"),
|
| 1009 |
+
"latex": f"\\sum_{{n=1}}^{{\\infty}} = {sp.latex(s_val)}"
|
| 1010 |
+
}
|
| 1011 |
+
except Exception:
|
| 1012 |
+
pass
|
| 1013 |
+
|
| 1014 |
+
# ββ Taylor Series βββββββββββββββββββββββββββββββββββββββββ
|
| 1015 |
+
elif any(k in p for k in ["taylor", "maclaurin"]):
|
| 1016 |
+
funcs = {
|
| 1017 |
+
"sin": sp.sin(x), "cos": sp.cos(x),
|
| 1018 |
+
"exp": sp.exp(x), "e^x": sp.exp(x),
|
| 1019 |
+
"ln": sp.log(1+x), "log": sp.log(1+x),
|
| 1020 |
+
"tan": sp.tan(x)
|
| 1021 |
+
}
|
| 1022 |
+
for fname, fexpr in funcs.items():
|
| 1023 |
+
if fname in p:
|
| 1024 |
+
n_terms = 6
|
| 1025 |
+
ts = sp.series(fexpr, x, 0, n_terms)
|
| 1026 |
+
return {
|
| 1027 |
+
"type": "TaylorSeries",
|
| 1028 |
+
"result": f"Taylor series of {fname}: {ts}",
|
| 1029 |
+
"latex": sp.latex(ts)
|
| 1030 |
+
}
|
| 1031 |
+
|
| 1032 |
+
# βοΏ½οΏ½οΏ½ L'Hopital βββββββββββββββββββββββββββββββββββββββββββββ
|
| 1033 |
+
elif any(k in p for k in ["lhopital","l'hopital"]):
|
| 1034 |
+
m = re.search(r"(?:of|for)\s+(.+?)\s*(?:as|at|when)\s*x\s*[ββ=]\s*([\d\.]+|inf)", p)
|
| 1035 |
+
if m:
|
| 1036 |
+
raw = clean(m.group(1))
|
| 1037 |
+
pt_str = m.group(2)
|
| 1038 |
+
pt = sp.oo if pt_str in ("inf","infinity") else sp.sympify(pt_str)
|
| 1039 |
+
try:
|
| 1040 |
+
expr_lh = parse_expr(raw, transformations=tfms, local_dict=ld)
|
| 1041 |
+
lim_val = sp.limit(expr_lh, x, pt)
|
| 1042 |
+
return {
|
| 1043 |
+
"type": "LHopital",
|
| 1044 |
+
"result": f"lim({m.group(1)}) as xβ{pt_str} = {lim_val}",
|
| 1045 |
+
"latex": f"\\lim_{{x\\to {pt_str}}} = {sp.latex(lim_val)}"
|
| 1046 |
+
}
|
| 1047 |
+
except Exception:
|
| 1048 |
+
pass
|
| 1049 |
+
|
| 1050 |
+
# ββ Riemann Integral ββββββββββββββββββββββββββββββββββββββ
|
| 1051 |
+
elif any(k in p for k in ["riemann","riemann integral","riemann sum"]):
|
| 1052 |
+
# Try to extract definite integral
|
| 1053 |
+
m = re.search(r"(?:of|for)\s+(.+?)\s+(?:from|on)\s+([\d\.]+)\s+to\s+([\d\.]+)", p)
|
| 1054 |
+
if m:
|
| 1055 |
+
raw = clean(m.group(1))
|
| 1056 |
+
a_v = sp.sympify(m.group(2))
|
| 1057 |
+
b_v = sp.sympify(m.group(3))
|
| 1058 |
+
try:
|
| 1059 |
+
expr_r = parse_expr(raw, transformations=tfms, local_dict=ld)
|
| 1060 |
+
result_r = sp.integrate(expr_r, (x, a_v, b_v))
|
| 1061 |
+
return {
|
| 1062 |
+
"type": "RiemannIntegral",
|
| 1063 |
+
"result": f"β«({m.group(1)}) from {a_v} to {b_v} = {result_r}",
|
| 1064 |
+
"latex": f"\\int_{{{a_v}}}^{{{b_v}}} = {sp.latex(result_r)}"
|
| 1065 |
+
}
|
| 1066 |
+
except Exception:
|
| 1067 |
+
pass
|
| 1068 |
+
|
| 1069 |
+
except Exception:
|
| 1070 |
+
pass # safe fallback to AI for all theory/proof questions
|
| 1071 |
+
|
| 1072 |
+
# ββ 9. Matrix / Eigenvalues β delegate to AI βββββββββββββββββ
|
| 1073 |
elif any(k in p for k in ["matrix", "determinant", "eigenvalue",
|
| 1074 |
"eigenvector", "det("]):
|
| 1075 |
return {"type": "Matrix", "result": "matrix_detected", "latex": ""}
|
|
|
|
| 1212 |
"D. NEVER recompute sin(pi), cos(pi) etc β sin(pi)=0 exactly, always.\n"
|
| 1213 |
"E. For Lagrange/Newton interpolation: the polynomial is already given above β DO NOT re-expand or re-derive it. Just show the basis polynomials and state the final polynomial from the verified result.\n"
|
| 1214 |
"F. Your final answer must EXACTLY match the verified result β no exceptions.\n\n"
|
| 1215 |
+
"=== REAL ANALYSIS II RULES (follow exactly) ===\n"
|
| 1216 |
+
"For ANY Real Analysis II question:\n"
|
| 1217 |
+
"A. Always start with the FORMAL DEFINITION using proper mathematical notation.\n"
|
| 1218 |
+
"B. State the theorem/property COMPLETELY before proving or explaining.\n"
|
| 1219 |
+
"C. ALWAYS give a concrete numerical example after every definition or theorem.\n"
|
| 1220 |
+
"D. For proofs: state Given, To Prove, then step-by-step proof.\n"
|
| 1221 |
+
"E. For Ξ΅-Ξ΄ definitions: write the formal definition first, then explain in words.\n"
|
| 1222 |
+
"F. For convergence tests: state the test, conditions, then apply to example.\n"
|
| 1223 |
+
"G. For sequences/series: always check if bounded, monotone, Cauchy where relevant.\n"
|
| 1224 |
+
"H. Use proper notation: β (for all), β (there exists), Ξ΅, Ξ΄, sup, inf, lim.\n\n"
|
| 1225 |
"Topics: Calculus, Linear Algebra, Number Theory, ODEs, "
|
| 1226 |
"Numerical Methods, Differential Geometry, Hydro Mechanics, "
|
| 1227 |
"Theory of Numbers, Real Analysis II, General Math."
|