Spaces:
Sleeping
Sleeping
| import streamlit as st | |
| import math | |
| import numpy as np | |
| from sympy import sympify, solve, Symbol | |
| # Title and Description | |
| st.title("Mathematical Calculator") | |
| st.markdown("A powerful calculator to perform basic and advanced mathematical operations.") | |
| # Tabs for different functionalities | |
| tab1, tab2 = st.tabs(["Basic Operations", "Advanced Operations"]) | |
| # Basic Operations | |
| with tab1: | |
| st.subheader("Basic Operations") | |
| num1 = st.number_input("Enter the first number:", format="%.10f", key="num1_basic") | |
| num2 = st.number_input("Enter the second number:", format="%.10f", key="num2_basic") | |
| operation = st.selectbox("Select Operation", ["Add", "Subtract", "Multiply", "Divide"]) | |
| # Perform the basic operation | |
| result_basic = None | |
| if operation == "Add": | |
| result_basic = num1 + num2 | |
| elif operation == "Subtract": | |
| result_basic = num1 - num2 | |
| elif operation == "Multiply": | |
| result_basic = num1 * num2 | |
| elif operation == "Divide": | |
| if num2 != 0: | |
| result_basic = num1 / num2 | |
| else: | |
| result_basic = "Error: Division by zero" | |
| if st.button("Calculate Basic", key="basic_calc"): | |
| st.write(f"Result: {result_basic}") | |
| # Advanced Operations | |
| with tab2: | |
| st.subheader("Advanced Operations") | |
| advanced_option = st.selectbox("Select Advanced Operation", [ | |
| "Sine", "Cosine", "Tangent", "Logarithm", "Exponential", "Custom Expression" | |
| ]) | |
| if advanced_option in ["Sine", "Cosine", "Tangent", "Logarithm", "Exponential"]: | |
| num_adv = st.number_input("Enter the number:", format="%.10f", key="num_adv") | |
| result_adv = None | |
| if advanced_option == "Sine": | |
| result_adv = math.sin(math.radians(num_adv)) | |
| elif advanced_option == "Cosine": | |
| result_adv = math.cos(math.radians(num_adv)) | |
| elif advanced_option == "Tangent": | |
| result_adv = math.tan(math.radians(num_adv)) | |
| elif advanced_option == "Logarithm": | |
| if num_adv > 0: | |
| result_adv = math.log(num_adv) | |
| else: | |
| result_adv = "Error: Logarithm of non-positive number" | |
| elif advanced_option == "Exponential": | |
| result_adv = math.exp(num_adv) | |
| if st.button("Calculate Advanced", key="adv_calc"): | |
| st.write(f"Result: {result_adv}") | |
| elif advanced_option == "Custom Expression": | |
| st.markdown("Use variables like `x`, `y` for equations (e.g., `2*x + 3`).") | |
| expression = st.text_input("Enter the expression:", key="custom_expr") | |
| variables = st.text_input("Enter variable values (comma-separated, e.g., `x=2, y=3`):", key="vars") | |
| result_custom = None | |
| if expression: | |
| try: | |
| expr = sympify(expression) | |
| var_dict = {} | |
| if variables: | |
| for var in variables.split(","): | |
| key, value = var.split("=") | |
| var_dict[key.strip()] = float(value.strip()) | |
| result_custom = expr.evalf(subs=var_dict) | |
| except Exception as e: | |
| result_custom = f"Error: {str(e)}" | |
| if st.button("Calculate Custom", key="custom_calc"): | |
| st.write(f"Result: {result_custom}") | |
| # Footer and Deployment Note | |
| st.markdown("---") | |
| st.markdown("Deployed with ❤️ on [Hugging Face Spaces](https://huggingface.co/spaces).") | |
| # requirements.txt for Hugging Face | |
| # streamlit | |
| # sympy | |
| # numpy | |