File size: 3,521 Bytes
66cddc0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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