import streamlit as st
from sympy import symbols, diff, integrate, simplify, sin, cos, tan, exp, log, pi
from sympy.parsing.sympy_parser import parse_expr

def main():
    st.title("Math Solver: Derivatives, Integrals, and Trigonometry")

    st.sidebar.header("Choose an Operation")
    option = st.sidebar.radio("Select:", ("Derivative", "Integral", "Simplify Expression", "Trigonometric Evaluation"))

    st.write("### Input your mathematical expression below")

    user_input = st.text_input("Expression", "e.g., x**2 + 3*x + sin(x)")
    variable = st.text_input("Variable (default is x)", "x")

    if user_input:
        try:
            # Parse the user input
            var = symbols(variable)
            expression = parse_expr(user_input)

            if option == "Derivative":
                result = diff(expression, var)
                st.write(f"The derivative of {expression} with respect to {variable} is:")
                st.latex(f"{result}")

            elif option == "Integral":
                result = integrate(expression, var)
                st.write(f"The integral of {expression} with respect to {variable} is:")
                st.latex(f"{result}")

            elif option == "Simplify Expression":
                result = simplify(expression)
                st.write(f"The simplified form of {expression} is:")
                st.latex(f"{result}")

            elif option == "Trigonometric Evaluation":
                st.write("### Provide a value for the variable")
                value = st.number_input(f"Value of {variable}")

                evaluated_result = expression.subs(var, value)
                st.write(f"The result of {expression} when {variable} = {value} is:")
                st.latex(f"{evaluated_result}")

        except Exception as e:
            st.error(f"Error in processing the expression: {e}")

if __name__ == "__main__":
    main()