AR14020's picture
Update app.py
374ddbe verified
import streamlit as st
from sympy import (
symbols, integrate, diff, exp, log, ln, sin, cos, tan, sec, csc, cot, latex, SympifyError, sympify
)
# Define the Streamlit app
def main():
st.set_page_config(layout="wide", page_title="Integration by Parts Solver")
st.title("Integration by Parts - Step-by-Step Solver")
st.markdown(
"""
## Formula for Integration by Parts
\[
\\int u \\cdot v' \\, dx = u \\cdot v - \\int v \\cdot u' \\, dx
\]
"""
)
# Sidebar for input
st.sidebar.header("Input Section")
st.sidebar.markdown("Provide the integrand and u for integration by parts.")
# Sidebar inputs
function_input = st.sidebar.text_input(
"Enter the integrand (e.g., x * exp(x), cos(ln(x)), x^2 * sin(x)):",
"x * exp(x)"
)
u_input = st.sidebar.text_input(
"Choose 'u' (e.g., x for x * exp(x) or ln(x)):", "x"
)
solve_button = st.sidebar.button("Solve")
if solve_button:
try:
# Define the variable and import common functions for eval()
x = symbols("x")
safe_globals = {"x": x, "exp": exp, "ln": ln, "log": log,
"sin": sin, "cos": cos, "tan": tan,
"sec": sec, "csc": csc, "cot": cot}
# Validate and parse inputs
try:
integrand = sympify(function_input, locals=safe_globals)
u = sympify(u_input, locals=safe_globals)
except SympifyError:
st.error("Invalid input. Please use valid mathematical syntax (e.g., cos(ln(x))).")
return
dv = integrand / u
# Compute derivatives and integrals
du = diff(u, x)
v = integrate(dv, x)
# Compute the result
first_term = u * v
second_term = integrate(v * du, x)
result = first_term - second_term
# Display steps
st.subheader("Step-by-Step Solution")
st.markdown("### 1. Choose u and v':")
st.latex(f"u = {latex(u)}, \\quad v' = {latex(dv)}")
st.markdown("### 2. Compute du and v:")
st.latex(f"du = \\frac{{d}}{{dx}}[{latex(u)}] = {latex(du)}")
st.latex(f"v = \\int {latex(dv)} \\, dx = {latex(v)}")
st.markdown("### 3. Apply the Integration by Parts Formula:")
st.latex(
f"\\int {latex(integrand)} \\, dx = u \\cdot v - \\int v \\cdot du"
)
st.latex(
f"= ({latex(u)})({latex(v)}) - \\int ({latex(v)})({latex(du)}) \\, dx"
)
st.markdown("### 4. Evaluate the Terms:")
st.latex(f"First Term = {latex(first_term)}")
st.latex(f"Second Term = \\int {latex(v * du)} \\, dx = {latex(second_term)}")
st.markdown("### 5. Combine Results:")
st.latex(f"\\int {latex(integrand)} \\, dx = {latex(result)} + C")
# Final output
st.subheader("Final Answer")
st.latex(f"{latex(result)} + C")
except Exception as e:
st.error(f"An error occurred: {e}")
# Run the app
if __name__ == "__main__":
main()