Amaanali01/Advanced_AI-Powered_Calculator
0
1import streamlit as st2import sympy as sp3import numpy as np4 5st.title("Advanced AI-Powered Calculator")6 7# User input: Mathematical expression8expr = st.text_input("Enter a mathematical expression (e.g., x^2 + 2*x + 1):")9 10# Operation selection11operation = st.selectbox("Choose operation", ["Evaluate", "Derivative", "Integral", "Solve Equation", "Matrix Operations"])12 13# Define symbol14x = sp.symbols('x')15 16# Process based on operation17result = None18 19if operation == "Evaluate":20 try:21 result = sp.sympify(expr).evalf()22 except Exception as e:23 result = f"Error: {e}"24 25elif operation == "Derivative":26 try:27 result = sp.diff(sp.sympify(expr), x)28 except Exception as e:29 result = f"Error: {e}"30 31elif operation == "Integral":32 try:33 result = sp.integrate(sp.sympify(expr), x)34 except Exception as e:35 result = f"Error: {e}"36 37elif operation == "Solve Equation":38 try:39 equation = sp.sympify(expr)40 result = sp.solve(equation, x)41 except Exception as e:42 result = f"Error: {e}"43 44elif operation == "Matrix Operations":45 try:46 # Convert input into matrix47 matrix_data = [[int(num) for num in row.split()] for row in expr.split(";")]48 matrix = sp.Matrix(matrix_data)49 50 # Show options for matrix calculations51 matrix_operation = st.selectbox("Choose matrix operation", ["Determinant", "Inverse", "Transpose"])52 53 if matrix_operation == "Determinant":54 result = matrix.det()55 elif matrix_operation == "Inverse":56 result = matrix.inv() if matrix.det() != 0 else "Matrix is singular (no inverse)."57 elif matrix_operation == "Transpose":58 result = matrix.T59 except Exception as e:60 result = f"Error: {e}"61 62# Display result63st.write("Result:")64st.success(result)65 