File size: 3,343 Bytes
454a534
f3e0b15
4ef2822
f3e0b15
a213c8b
454a534
 
f3e0b15
 
454a534
 
 
34f4225
 
454a534
34f4225
454a534
 
 
3ecea7b
 
 
 
 
 
4ef2822
3ecea7b
454a534
3ecea7b
34f4225
454a534
3ecea7b
454a534
3ecea7b
454a534
f3e0b15
454a534
f3e0b15
 
 
34f4225
4ef2822
 
 
 
 
 
 
 
454a534
 
 
 
 
 
34f4225
 
 
 
454a534
 
 
374ddbe
34f4225
454a534
34f4225
 
 
 
 
 
454a534
34f4225
454a534
34f4225
 
 
 
 
 
 
 
 
 
454a534
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
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()