Upload app.py
Browse files
app.py
CHANGED
|
@@ -1074,7 +1074,140 @@ def run_sympy(problem: str) -> dict:
|
|
| 1074 |
except Exception:
|
| 1075 |
pass # safe fallback to AI for all theory/proof questions
|
| 1076 |
|
| 1077 |
-
# ββ 9.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1078 |
elif any(k in p for k in ["matrix", "determinant", "eigenvalue",
|
| 1079 |
"eigenvector", "det("]):
|
| 1080 |
return {"type": "Matrix", "result": "matrix_detected", "latex": ""}
|
|
@@ -1217,6 +1350,20 @@ def ask_ai(problem: str, sympy_info: dict, history: list) -> str:
|
|
| 1217 |
"D. NEVER recompute sin(pi), cos(pi) etc β sin(pi)=0 exactly, always.\n"
|
| 1218 |
"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"
|
| 1219 |
"F. Your final answer must EXACTLY match the verified result β no exceptions.\n\n"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1220 |
"=== REAL ANALYSIS II RULES (follow exactly) ===\n"
|
| 1221 |
"For ANY Real Analysis II question:\n"
|
| 1222 |
"A. Always start with the FORMAL DEFINITION using proper mathematical notation.\n"
|
|
|
|
| 1074 |
except Exception:
|
| 1075 |
pass # safe fallback to AI for all theory/proof questions
|
| 1076 |
|
| 1077 |
+
# ββ 9. Differential Geometry β SymPy for computations ββββββββββ
|
| 1078 |
+
elif any(k in p for k in [
|
| 1079 |
+
"curvature", "torsion", "tangent vector", "normal vector",
|
| 1080 |
+
"binormal", "serret-frenet", "frenet", "osculating",
|
| 1081 |
+
"arc length", "space curve", "plane curve", "helix", "helices",
|
| 1082 |
+
"evolute", "involute", "rectifying plane",
|
| 1083 |
+
"first fundamental form", "second fundamental form",
|
| 1084 |
+
"fundamental form", "gaussian curvature", "mean curvature",
|
| 1085 |
+
"principal curvature", "geodesic",
|
| 1086 |
+
"parametric surface", "christoffel", "covariant derivative",
|
| 1087 |
+
"contravariant", "metric tensor",
|
| 1088 |
+
]):
|
| 1089 |
+
t_s = sp.Symbol('t')
|
| 1090 |
+
u_s, v_s = sp.symbols('u v')
|
| 1091 |
+
try:
|
| 1092 |
+
|
| 1093 |
+
# ββ Curvature of plane curve y=f(x) ββββββββββββββββββββββ
|
| 1094 |
+
if "curvature" in p and not any(k in p for k in ["gaussian","mean","space","torsion"]):
|
| 1095 |
+
# Extract function and point
|
| 1096 |
+
m = re.search(r"(?:of|for)\s+y\s*=\s*(.+?)(?:\s+at|\s*$)", p)
|
| 1097 |
+
pt_m = re.search(r"at\s+x\s*[=:]\s*([-\d\.]+)", p)
|
| 1098 |
+
if m:
|
| 1099 |
+
raw = clean(m.group(1).strip())
|
| 1100 |
+
expr_c = parse_expr(raw, transformations=tfms, local_dict=ld)
|
| 1101 |
+
dy = sp.diff(expr_c, x)
|
| 1102 |
+
d2y = sp.diff(expr_c, x, 2)
|
| 1103 |
+
kappa_expr = sp.Abs(d2y) / (1 + dy**2)**sp.Rational(3,2)
|
| 1104 |
+
if pt_m:
|
| 1105 |
+
pt_val = float(pt_m.group(1))
|
| 1106 |
+
kappa_val = sp.simplify(kappa_expr.subs(x, pt_val))
|
| 1107 |
+
return {
|
| 1108 |
+
"type": "Curvature",
|
| 1109 |
+
"result": f"ΞΊ at x={pt_val}: y'={dy.subs(x,pt_val)}, y''={d2y.subs(x,pt_val)}, ΞΊ={kappa_val}",
|
| 1110 |
+
"latex": f"\\kappa = {sp.latex(kappa_val)}"
|
| 1111 |
+
}
|
| 1112 |
+
else:
|
| 1113 |
+
return {
|
| 1114 |
+
"type": "Curvature",
|
| 1115 |
+
"result": f"ΞΊ(x) = {sp.simplify(kappa_expr)}",
|
| 1116 |
+
"latex": f"\\kappa = {sp.latex(sp.simplify(kappa_expr))}"
|
| 1117 |
+
}
|
| 1118 |
+
|
| 1119 |
+
# ββ Arc Length ββββββββββββββββββββββββββββββββββββββββββββ
|
| 1120 |
+
elif "arc length" in p:
|
| 1121 |
+
m = re.search(r"(?:of|for)\s+y\s*=\s*(.+?)\s+from\s+([-\d\.]+)\s+to\s+([-\d\.]+)", p)
|
| 1122 |
+
if m:
|
| 1123 |
+
raw = clean(m.group(1).strip())
|
| 1124 |
+
a_v = sp.sympify(m.group(2))
|
| 1125 |
+
b_v = sp.sympify(m.group(3))
|
| 1126 |
+
expr_al = parse_expr(raw, transformations=tfms, local_dict=ld)
|
| 1127 |
+
dy = sp.diff(expr_al, x)
|
| 1128 |
+
integrand = sp.sqrt(1 + dy**2)
|
| 1129 |
+
L = sp.integrate(integrand, (x, a_v, b_v))
|
| 1130 |
+
L_simplified = sp.simplify(L)
|
| 1131 |
+
return {
|
| 1132 |
+
"type": "ArcLength",
|
| 1133 |
+
"result": f"L = β«β(1+y'Β²)dx from {a_v} to {b_v} = {L_simplified}",
|
| 1134 |
+
"latex": f"L = {sp.latex(L_simplified)}"
|
| 1135 |
+
}
|
| 1136 |
+
|
| 1137 |
+
# ββ Space Curve: Curvature + Torsion βββββββββββββββββββββ
|
| 1138 |
+
elif any(k in p for k in ["space curve","torsion","frenet","serret"]):
|
| 1139 |
+
# Extract parametric curve r(t) = (x(t), y(t), z(t))
|
| 1140 |
+
pts = re.findall(r"\(\s*(.+?)\s*,\s*(.+?)\s*,\s*(.+?)\s*\)", p)
|
| 1141 |
+
pt_m = re.search(r"at\s+t\s*[=:]\s*([-\d\.]+)", p)
|
| 1142 |
+
if pts:
|
| 1143 |
+
rx = parse_expr(clean(pts[0][0]), transformations=tfms, local_dict={**ld, "t": t_s})
|
| 1144 |
+
ry = parse_expr(clean(pts[0][1]), transformations=tfms, local_dict={**ld, "t": t_s})
|
| 1145 |
+
rz = parse_expr(clean(pts[0][2]), transformations=tfms, local_dict={**ld, "t": t_s})
|
| 1146 |
+
r_vec = sp.Matrix([rx, ry, rz])
|
| 1147 |
+
dr = r_vec.diff(t_s)
|
| 1148 |
+
d2r = dr.diff(t_s)
|
| 1149 |
+
d3r = d2r.diff(t_s)
|
| 1150 |
+
speed = sp.sqrt(dr.dot(dr))
|
| 1151 |
+
cross = dr.cross(d2r)
|
| 1152 |
+
kappa = sp.simplify(sp.sqrt(cross.dot(cross)) / speed**3)
|
| 1153 |
+
torsion_val = sp.simplify(cross.dot(d3r) / cross.dot(cross))
|
| 1154 |
+
t_val = float(pt_m.group(1)) if pt_m else 0
|
| 1155 |
+
k_at = sp.simplify(kappa.subs(t_s, t_val))
|
| 1156 |
+
tau_at = sp.simplify(torsion_val.subs(t_s, t_val))
|
| 1157 |
+
T_vec = sp.simplify(dr / speed)
|
| 1158 |
+
return {
|
| 1159 |
+
"type": "FrenetSerret",
|
| 1160 |
+
"result": (f"r(t)={pts[0]}, at t={t_val}:\n"
|
| 1161 |
+
f"ΞΊ = {k_at}, Ο = {tau_at}\n"
|
| 1162 |
+
f"T = {T_vec.subs(t_s,t_val).T}"),
|
| 1163 |
+
"latex": f"\\kappa={sp.latex(k_at)}, \\tau={sp.latex(tau_at)}"
|
| 1164 |
+
}
|
| 1165 |
+
|
| 1166 |
+
# ββ First Fundamental Form ββββββββββββββββββββββββββββββββ
|
| 1167 |
+
elif "first fundamental form" in p:
|
| 1168 |
+
pts = re.findall(r"\(\s*(.+?)\s*,\s*(.+?)\s*,\s*(.+?)\s*\)", p)
|
| 1169 |
+
if pts:
|
| 1170 |
+
rx = parse_expr(clean(pts[0][0]), transformations=tfms, local_dict={**ld, "u": u_s, "v": v_s})
|
| 1171 |
+
ry = parse_expr(clean(pts[0][1]), transformations=tfms, local_dict={**ld, "u": u_s, "v": v_s})
|
| 1172 |
+
rz = parse_expr(clean(pts[0][2]), transformations=tfms, local_dict={**ld, "u": u_s, "v": v_s})
|
| 1173 |
+
r_vec = sp.Matrix([rx, ry, rz])
|
| 1174 |
+
ru = r_vec.diff(u_s)
|
| 1175 |
+
rv = r_vec.diff(v_s)
|
| 1176 |
+
E = sp.simplify(ru.dot(ru))
|
| 1177 |
+
F = sp.simplify(ru.dot(rv))
|
| 1178 |
+
G = sp.simplify(rv.dot(rv))
|
| 1179 |
+
return {
|
| 1180 |
+
"type": "FirstFundamentalForm",
|
| 1181 |
+
"result": f"E={E}, F={F}, G={G}, dsΒ²={E}duΒ²+{2*F}dudv+{G}dvΒ²",
|
| 1182 |
+
"latex": f"E={sp.latex(E)}, F={sp.latex(F)}, G={sp.latex(G)}"
|
| 1183 |
+
}
|
| 1184 |
+
|
| 1185 |
+
# ββ Gaussian + Mean Curvature βββββββββββββββββββββββββββββ
|
| 1186 |
+
elif any(k in p for k in ["gaussian curvature","mean curvature"]):
|
| 1187 |
+
pts = re.findall(r"\(\s*(.+?)\s*,\s*(.+?)\s*,\s*(.+?)\s*\)", p)
|
| 1188 |
+
if pts:
|
| 1189 |
+
rx = parse_expr(clean(pts[0][0]), transformations=tfms, local_dict={**ld, "u": u_s, "v": v_s})
|
| 1190 |
+
ry = parse_expr(clean(pts[0][1]), transformations=tfms, local_dict={**ld, "u": u_s, "v": v_s})
|
| 1191 |
+
rz = parse_expr(clean(pts[0][2]), transformations=tfms, local_dict={**ld, "u": u_s, "v": v_s})
|
| 1192 |
+
r_vec = sp.Matrix([rx, ry, rz])
|
| 1193 |
+
ru = r_vec.diff(u_s); rv = r_vec.diff(v_s)
|
| 1194 |
+
E = sp.simplify(ru.dot(ru)); F = sp.simplify(ru.dot(rv)); G = sp.simplify(rv.dot(rv))
|
| 1195 |
+
n = ru.cross(rv); N = sp.simplify(n / sp.sqrt(n.dot(n)))
|
| 1196 |
+
L = sp.simplify(N.dot(ru.diff(u_s)))
|
| 1197 |
+
M = sp.simplify(N.dot(ru.diff(v_s)))
|
| 1198 |
+
Nv = sp.simplify(N.dot(rv.diff(v_s)))
|
| 1199 |
+
K = sp.simplify((L*Nv - M**2)/(E*G - F**2))
|
| 1200 |
+
H = sp.simplify((E*Nv - 2*F*M + G*L)/(2*(E*G - F**2)))
|
| 1201 |
+
return {
|
| 1202 |
+
"type": "GaussianCurvature",
|
| 1203 |
+
"result": f"K (Gaussian) = {K}, H (Mean) = {H}",
|
| 1204 |
+
"latex": f"K={sp.latex(K)}, H={sp.latex(H)}"
|
| 1205 |
+
}
|
| 1206 |
+
|
| 1207 |
+
except Exception:
|
| 1208 |
+
pass # safe fallback to AI
|
| 1209 |
+
|
| 1210 |
+
# ββ 10. Matrix / Eigenvalues β delegate to AI ββββββββββββββββββββ
|
| 1211 |
elif any(k in p for k in ["matrix", "determinant", "eigenvalue",
|
| 1212 |
"eigenvector", "det("]):
|
| 1213 |
return {"type": "Matrix", "result": "matrix_detected", "latex": ""}
|
|
|
|
| 1350 |
"D. NEVER recompute sin(pi), cos(pi) etc β sin(pi)=0 exactly, always.\n"
|
| 1351 |
"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"
|
| 1352 |
"F. Your final answer must EXACTLY match the verified result β no exceptions.\n\n"
|
| 1353 |
+
"=== DIFFERENTIAL GEOMETRY RULES (follow exactly) ===\n"
|
| 1354 |
+
"For ANY Differential Geometry question:\n"
|
| 1355 |
+
"A. ALWAYS state the definition or theorem FIRST before computing.\n"
|
| 1356 |
+
"B. Plane curve curvature: ΞΊ = |y''| / (1+y'Β²)^(3/2).\n"
|
| 1357 |
+
"C. Space curve: ΞΊ = |r'Γr''| / |r'|Β³, Ο = (r'Γr'')Β·r''' / |r'Γr''|Β².\n"
|
| 1358 |
+
"D. Frenet-Serret formulas: dT/ds=ΞΊN, dN/ds=-ΞΊT+ΟB, dB/ds=-ΟN.\n"
|
| 1359 |
+
"E. Unit vectors: T=r'/|r'|, N=T'/|T'|, B=TΓN.\n"
|
| 1360 |
+
"F. First Fundamental Form: dsΒ²=EduΒ²+2Fdudv+GdvΒ², where E=ruΒ·ru, F=ruΒ·rv, G=rvΒ·rv.\n"
|
| 1361 |
+
"G. Second Fundamental Form: L=NΒ·ruu, M=NΒ·ruv, N_coeff=NΒ·rvv.\n"
|
| 1362 |
+
"H. Gaussian curvature: K=(LN-MΒ²)/(EG-FΒ²), Mean curvature: H=(EN-2FM+GL)/(2(EG-FΒ²)).\n"
|
| 1363 |
+
"I. For proofs: state Given β To Prove β Proof steps clearly.\n"
|
| 1364 |
+
"J. For mixed questions: definition first β formula β computation β geometric meaning.\n"
|
| 1365 |
+
"K. Christoffel symbols: Ξα΅’β±Όα΅ = (1/2)gα΅Λ‘(βgα΅’Λ‘/βxΚ² + βgβ±ΌΛ‘/βxβ± - βgα΅’β±Ό/βxΛ‘).\n"
|
| 1366 |
+
"L. For tensors: always specify contravariant (upper) vs covariant (lower) indices.\n\n"
|
| 1367 |
"=== REAL ANALYSIS II RULES (follow exactly) ===\n"
|
| 1368 |
"For ANY Real Analysis II question:\n"
|
| 1369 |
"A. Always start with the FORMAL DEFINITION using proper mathematical notation.\n"
|