apply setup_symbols function to all other routes

This commit is contained in:
tsb1995 2019-12-20 13:14:09 -08:00
parent 45a816c46c
commit bb0af12cfb
2 changed files with 21 additions and 14 deletions

Binary file not shown.

35
app.py
View File

@ -4,6 +4,7 @@ from helpers import apology
app = Flask(__name__) app = Flask(__name__)
# Convert our commonly used variables into sympy symbols
x, y, z, t, X, Y, Z, T = symbols('x y z t X Y Z T') x, y, z, t, X, Y, Z, T = symbols('x y z t X Y Z T')
@app.route('/') @app.route('/')
@ -18,6 +19,7 @@ def differentiation():
return apology("must provide a function", 400) return apology("must provide a function", 400)
f = request.form.get("function") f = request.form.get("function")
# Setup our symbols for SymPy
f = setup_symbols(f) f = setup_symbols(f)
# Differentiate and return latex expressions # Differentiate and return latex expressions
@ -33,11 +35,12 @@ def integration():
# Check if inputs were given # Check if inputs were given
if not request.form.get("function"): if not request.form.get("function"):
return apology("must provide a function", 400) return apology("must provide a function", 400)
f = request.form.get("function")
# Setup our symbols for SymPy
f = setup_symbols(f)
# Integrate and return latex expressions # Integrate and return latex expressions
f = request.form.get("function")
x = symbols('x')
f = sympify(f)
fintegral = latex(f.integrate(x)) fintegral = latex(f.integrate(x))
value = latex(f) value = latex(f)
return render_template("integrated.html", value=value, fintegral=fintegral) return render_template("integrated.html", value=value, fintegral=fintegral)
@ -59,19 +62,21 @@ def riemann():
if not request.form.get("sumtype"): if not request.form.get("sumtype"):
return apology("must choose left or right", 400) return apology("must choose left or right", 400)
# Get inputs, check for validity and sympify # Get our info from form
f = request.form.get("function") f = request.form.get("function")
sumtype = request.form.get("sumtype") sumtype = request.form.get("sumtype")
lb = int(request.form.get("lowerbound")) lb = int(request.form.get("lowerbound"))
ub = int(request.form.get("upperbound")) ub = int(request.form.get("upperbound"))
si = int(request.form.get("subintervals")) si = int(request.form.get("subintervals"))
x = symbols('x')
f = sympify(f) # Setup our symbols for SymPy
dx = (ub - lb) / si f = setup_symbols(f)
value = latex(f)
# Run through Riemann Sum algorithm, creatings lists for display # Run through Riemann Sum algorithm, creatings lists for display
# of inputs, outputs, and areas (their products) # of inputs, outputs, and areas (their products)
value = latex(f)
dx = (ub - lb) / si
inputs = list() inputs = list()
if sumtype == "1": if sumtype == "1":
for i in range(0, si): for i in range(0, si):
@ -115,9 +120,8 @@ def maxmin():
lb = sympify(request.form.get("lowerbound")) lb = sympify(request.form.get("lowerbound"))
ub = sympify(request.form.get("upperbound")) ub = sympify(request.form.get("upperbound"))
# Prep input for numpy / sympy # Setup our symbols for SymPy
x = symbols('x') f = setup_symbols(f)
f = sympify(f)
# Get Derivative, solve for real solutions, update candidates list # Get Derivative, solve for real solutions, update candidates list
fprime = f.diff(x) fprime = f.diff(x)
@ -164,8 +168,10 @@ def aprox():
f = request.form.get("function") f = request.form.get("function")
a = request.form.get("easy") a = request.form.get("easy")
h = request.form.get("hard") h = request.form.get("hard")
x = symbols('x') # Setup our symbols for SymPy
f = sympify(f) f = setup_symbols(f)
# Make sure a and h are numbers
a = sympify(a) a = sympify(a)
h = sympify(h) h = sympify(h)
if not a.is_number: if not a.is_number:
@ -197,9 +203,10 @@ def errorhandler(e):
def setup_symbols(f): def setup_symbols(f):
f = sympify(f) f = sympify(f)
# replace commonly used variables with x
for letter in [x, y, z, t, X, Y, Z, T]: for letter in [x, y, z, t, X, Y, Z, T]:
f = f.subs(letter, x) f = f.subs(letter, x)
return f return f