Upload app.py
Browse files
app.py
CHANGED
|
@@ -5,9 +5,8 @@ import anthropic
|
|
| 5 |
from sympy import (
|
| 6 |
symbols, diff, integrate, limit, solve, simplify, expand, factor,
|
| 7 |
latex, sympify, oo, sin, cos, tan, exp, log, sqrt, pi, E,
|
| 8 |
-
Matrix,
|
| 9 |
-
series, Sum, Product, Rational, Integer, Float, Abs
|
| 10 |
-
standard_transformations
|
| 11 |
)
|
| 12 |
from sympy.parsing.sympy_parser import (
|
| 13 |
parse_expr,
|
|
@@ -249,7 +248,7 @@ def run_sympy(problem: str) -> dict:
|
|
| 249 |
raw = p.split(kw, 1)[-1].strip()
|
| 250 |
break
|
| 251 |
raw = re.sub(r"\s*(dx|with\s*respect\s*to\s*x).*$", "", raw).strip()
|
| 252 |
-
expr = parse_expr(clean(raw), transformations=tfms, local_dict={"x": x})
|
| 253 |
result = sp.diff(expr, x)
|
| 254 |
return {"type": "Derivative", "result": str(result), "latex": sp.latex(result)}
|
| 255 |
|
|
@@ -261,7 +260,7 @@ def run_sympy(problem: str) -> dict:
|
|
| 261 |
raw = p.split(kw, 1)[-1].strip()
|
| 262 |
break
|
| 263 |
raw = re.sub(r"\s*dx.*$", "", raw).strip()
|
| 264 |
-
expr = parse_expr(clean(raw), transformations=tfms, local_dict={"x": x})
|
| 265 |
result = sp.integrate(expr, x)
|
| 266 |
return {"type": "Integral", "result": str(result), "latex": sp.latex(result)}
|
| 267 |
|
|
@@ -275,7 +274,7 @@ def run_sympy(problem: str) -> dict:
|
|
| 275 |
raw_expr = clean(match.group(1))
|
| 276 |
point_str = match.group(2).strip()
|
| 277 |
point = sp.oo if point_str in ("inf", "infinity", "oo") else sp.sympify(point_str)
|
| 278 |
-
expr = parse_expr(raw_expr, transformations=tfms, local_dict={"x": x})
|
| 279 |
result = sp.limit(expr, x, point)
|
| 280 |
return {"type": "Limit", "result": str(result), "latex": sp.latex(result)}
|
| 281 |
|
|
@@ -284,11 +283,11 @@ def run_sympy(problem: str) -> dict:
|
|
| 284 |
raw = re.sub(r"(solve|find x|roots of|roots|the equation)", "", p).strip().strip(": ")
|
| 285 |
if "=" in raw:
|
| 286 |
lhs_s, rhs_s = raw.split("=", 1)
|
| 287 |
-
lhs_e = parse_expr(clean(lhs_s), transformations=tfms, local_dict={"x": x})
|
| 288 |
-
rhs_e = parse_expr(clean(rhs_s), transformations=tfms, local_dict={"x": x})
|
| 289 |
expr = lhs_e - rhs_e
|
| 290 |
else:
|
| 291 |
-
expr = parse_expr(clean(raw), transformations=tfms, local_dict={"x": x})
|
| 292 |
if x in expr.free_symbols:
|
| 293 |
sol = sp.solve(expr, x)
|
| 294 |
sol_latex = ", ".join([sp.latex(s) for s in sol])
|
|
@@ -509,4 +508,4 @@ if send_btn and problem and problem != st.session_state.last_submitted:
|
|
| 509 |
|
| 510 |
st.session_state.messages.append({"role": "user", "content": problem})
|
| 511 |
st.session_state.messages.append({"role": "assistant", "content": answer})
|
| 512 |
-
st.rerun()
|
|
|
|
| 5 |
from sympy import (
|
| 6 |
symbols, diff, integrate, limit, solve, simplify, expand, factor,
|
| 7 |
latex, sympify, oo, sin, cos, tan, exp, log, sqrt, pi, E,
|
| 8 |
+
Matrix, Symbol, Function, dsolve, Eq,
|
| 9 |
+
series, Sum, Product, Rational, Integer, Float, Abs
|
|
|
|
| 10 |
)
|
| 11 |
from sympy.parsing.sympy_parser import (
|
| 12 |
parse_expr,
|
|
|
|
| 248 |
raw = p.split(kw, 1)[-1].strip()
|
| 249 |
break
|
| 250 |
raw = re.sub(r"\s*(dx|with\s*respect\s*to\s*x).*$", "", raw).strip()
|
| 251 |
+
expr = parse_expr(clean(raw), transformations=tfms, local_dict={"x": x, "e": sp.E, "E": sp.E, "pi": sp.pi, "ln": sp.log, "inf": sp.oo})
|
| 252 |
result = sp.diff(expr, x)
|
| 253 |
return {"type": "Derivative", "result": str(result), "latex": sp.latex(result)}
|
| 254 |
|
|
|
|
| 260 |
raw = p.split(kw, 1)[-1].strip()
|
| 261 |
break
|
| 262 |
raw = re.sub(r"\s*dx.*$", "", raw).strip()
|
| 263 |
+
expr = parse_expr(clean(raw), transformations=tfms, local_dict={"x": x, "e": sp.E, "E": sp.E, "pi": sp.pi, "ln": sp.log, "inf": sp.oo})
|
| 264 |
result = sp.integrate(expr, x)
|
| 265 |
return {"type": "Integral", "result": str(result), "latex": sp.latex(result)}
|
| 266 |
|
|
|
|
| 274 |
raw_expr = clean(match.group(1))
|
| 275 |
point_str = match.group(2).strip()
|
| 276 |
point = sp.oo if point_str in ("inf", "infinity", "oo") else sp.sympify(point_str)
|
| 277 |
+
expr = parse_expr(raw_expr, transformations=tfms, local_dict={"x": x, "e": sp.E, "E": sp.E, "pi": sp.pi, "ln": sp.log, "inf": sp.oo})
|
| 278 |
result = sp.limit(expr, x, point)
|
| 279 |
return {"type": "Limit", "result": str(result), "latex": sp.latex(result)}
|
| 280 |
|
|
|
|
| 283 |
raw = re.sub(r"(solve|find x|roots of|roots|the equation)", "", p).strip().strip(": ")
|
| 284 |
if "=" in raw:
|
| 285 |
lhs_s, rhs_s = raw.split("=", 1)
|
| 286 |
+
lhs_e = parse_expr(clean(lhs_s), transformations=tfms, local_dict={"x": x, "e": sp.E, "E": sp.E, "pi": sp.pi, "ln": sp.log, "inf": sp.oo})
|
| 287 |
+
rhs_e = parse_expr(clean(rhs_s), transformations=tfms, local_dict={"x": x, "e": sp.E, "E": sp.E, "pi": sp.pi, "ln": sp.log, "inf": sp.oo})
|
| 288 |
expr = lhs_e - rhs_e
|
| 289 |
else:
|
| 290 |
+
expr = parse_expr(clean(raw), transformations=tfms, local_dict={"x": x, "e": sp.E, "E": sp.E, "pi": sp.pi, "ln": sp.log, "inf": sp.oo})
|
| 291 |
if x in expr.free_symbols:
|
| 292 |
sol = sp.solve(expr, x)
|
| 293 |
sol_latex = ", ".join([sp.latex(s) for s in sol])
|
|
|
|
| 508 |
|
| 509 |
st.session_state.messages.append({"role": "user", "content": problem})
|
| 510 |
st.session_state.messages.append({"role": "assistant", "content": answer})
|
| 511 |
+
st.rerun()
|