Calc / app.py
mjolnir1122's picture
Update app.py
93b92dd verified
import streamlit as st
import math
import sympy as sp
def main():
st.title("Advanced Calculator")
option = st.sidebar.selectbox("Select Calculation Type",
["Basic Arithmetic", "Algebra", "Trigonometry", "Equation Solver"])
if option == "Basic Arithmetic":
st.header("Basic Arithmetic")
num1 = st.number_input("Enter first number", value=0.0, format="%.6f")
num2 = st.number_input("Enter second number", value=0.0, format="%.6f")
operation = st.selectbox("Choose an operation", ["Addition", "Subtraction", "Multiplication", "Division"])
if operation == "Addition":
result = num1 + num2
elif operation == "Subtraction":
result = num1 - num2
elif operation == "Multiplication":
result = num1 * num2
elif operation == "Division":
result = num1 / num2 if num2 != 0 else "Error: Division by zero"
st.write(f"### Result: {result}")
elif option == "Algebra":
st.header("Algebraic Operations")
expr = st.text_input("Enter an algebraic expression (e.g., x**2 + 3*x - 2)")
variable = st.text_input("Enter the variable (default: x)", value="x")
if expr:
try:
x = sp.symbols(variable)
expr_sympy = sp.sympify(expr)
st.write("**Simplified Expression:**", sp.simplify(expr_sympy))
st.write("**Expanded Expression:**", sp.expand(expr_sympy))
st.write("**Derivative:**", sp.diff(expr_sympy, x))
st.write("**Integral:**", sp.integrate(expr_sympy, x))
except Exception as e:
st.error(f"Error in algebraic computation: {e}")
elif option == "Trigonometry":
st.header("Trigonometric Functions")
angle = st.number_input("Enter angle in degrees", value=0.0)
trig_function = st.selectbox("Choose a function",
["sin", "cos", "tan", "cot", "sec", "csc"])
rad = math.radians(angle)
try:
if trig_function == "sin":
result = math.sin(rad)
elif trig_function == "cos":
result = math.cos(rad)
elif trig_function == "tan":
result = math.tan(rad)
elif trig_function == "cot":
result = 1 / math.tan(rad) if math.tan(rad) != 0 else "Undefined"
elif trig_function == "sec":
result = 1 / math.cos(rad) if math.cos(rad) != 0 else "Undefined"
elif trig_function == "csc":
result = 1 / math.sin(rad) if math.sin(rad) != 0 else "Undefined"
st.write(f"### Result: {result}")
except Exception as e:
st.error(f"Error in trigonometric computation: {e}")
elif option == "Equation Solver":
st.header("Equation Solver")
equation = st.text_input("Enter an equation (e.g., x**2 - 4 = 0)")
variable = st.text_input("Enter the variable (default: x)", value="x")
if equation:
try:
x = sp.symbols(variable)
lhs, rhs = equation.split("=")
eq = sp.Eq(sp.sympify(lhs.strip()), sp.sympify(rhs.strip()))
solutions = sp.solve(eq, x)
st.write("### Solutions:", solutions)
except Exception as e:
st.error(f"Error in solving equation: {e}")
if __name__ == "__main__":
main()