ghaffarmumtat123 commited on
Commit
7cefee2
·
verified ·
1 Parent(s): 83dc7cb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +187 -19
app.py CHANGED
@@ -1,30 +1,111 @@
1
  import streamlit as st
2
  import sympy as sp
3
 
4
- # Function to calculate the integral of an expression
5
- def calculate_integral(expression):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6
  try:
7
- # Replace '^' with '**' for exponentiation and remove spaces
8
  expression = expression.replace("^", "**")
9
- expression = expression.replace(" ", "") # Remove spaces
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
 
11
- # Ensure that implicit multiplication (like 2x) is correctly interpreted
12
- # For example, replace '2x' with '2*x' and 'x' with '1*x' if necessary
13
- if 'x' in expression and not expression.startswith('x'):
14
- expression = expression.replace("x", "*x") # Adding '*' for implicit multiplication
 
 
 
 
 
 
 
 
 
 
 
 
 
15
 
16
- # Check if the expression starts with 'x', if so prepend '1*' to make it '1*x'
17
- if expression.startswith("x"):
18
- expression = "1*" + expression
 
 
 
 
19
 
20
- # Parse the expression using sympy
 
 
 
 
 
 
 
 
 
 
 
 
 
21
  expr = sp.sympify(expression)
22
-
23
- # Calculate the integral
24
  integral = sp.integrate(expr)
25
-
26
  return f"Integral: {integral}"
27
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
28
  except Exception as e:
29
  return f"Error: {e}"
30
 
@@ -34,11 +115,71 @@ def main():
34
 
35
  # Tabs for different functionalities
36
  tab = st.radio("Select a problem type:", ["Solve Algebraic Equation", "Simplify Expression", "Factor Expression",
37
- "Solve Word Problem", "Find Factors of a Number", "Derivative", "Integral"])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
38
 
39
- if tab == "Integral":
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
40
  st.header("Calculate Integral")
41
- integral_input = st.text_input("Enter an expression to integrate (e.g., 2*x**2 + 5*x - 3)", "")
42
  if st.button("Calculate Integral"):
43
  if integral_input:
44
  integral = calculate_integral(integral_input)
@@ -46,6 +187,33 @@ def main():
46
  else:
47
  st.write("Please enter an expression!")
48
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
49
  # Run the app
50
  if __name__ == "__main__":
51
  main()
 
1
  import streamlit as st
2
  import sympy as sp
3
 
4
+ # Function to solve algebraic equations
5
+ def solve_equation(equation):
6
+ try:
7
+ if "=" not in equation:
8
+ return "Invalid input. Please enter an equation with '=' (e.g., 2*x + 3 = 5)."
9
+ equation = equation.replace("^", "**")
10
+ equation = equation.replace(" ", "")
11
+ lhs, rhs = equation.split("=")
12
+ lhs_expr = sp.sympify(lhs)
13
+ rhs_expr = sp.sympify(rhs)
14
+ solution = sp.solve(lhs_expr - rhs_expr)
15
+ return f"Solution: {solution}"
16
+ except Exception as e:
17
+ return f"Error: {e}"
18
+
19
+ # Function to simplify expressions
20
+ def simplify_expression(expression):
21
  try:
 
22
  expression = expression.replace("^", "**")
23
+ expr = sp.sympify(expression)
24
+ simplified_expr = sp.simplify(expr)
25
+ return f"Simplified Expression: {simplified_expr}"
26
+ except Exception as e:
27
+ return f"Error: {e}"
28
+
29
+ # Function to factor expressions
30
+ def factor_expression(expression):
31
+ try:
32
+ expression = expression.replace("^", "**")
33
+ expr = sp.sympify(expression)
34
+ factored_expr = sp.factor(expr)
35
+ return f"Factored Expression: {factored_expr}"
36
+ except Exception as e:
37
+ return f"Error: {e}"
38
 
39
+ # Function to solve basic arithmetic word problems
40
+ def solve_word_problem(operation, num1, num2):
41
+ try:
42
+ if operation == "addition":
43
+ result = num1 + num2
44
+ elif operation == "subtraction":
45
+ result = num1 - num2
46
+ elif operation == "multiplication":
47
+ result = num1 * num2
48
+ elif operation == "division":
49
+ if num2 == 0:
50
+ result = "Error! Division by zero."
51
+ else:
52
+ result = num1 / num2
53
+ return f"Result: {result}"
54
+ except Exception as e:
55
+ return f"Error: {e}"
56
 
57
+ # Function to find the factors of a number
58
+ def find_factors(number):
59
+ factors = []
60
+ for i in range(1, number + 1):
61
+ if number % i == 0:
62
+ factors.append(i)
63
+ return factors
64
 
65
+ # Function to calculate the derivative of an expression
66
+ def calculate_derivative(expression):
67
+ try:
68
+ expression = expression.replace("^", "**")
69
+ expr = sp.sympify(expression)
70
+ derivative = sp.diff(expr)
71
+ return f"Derivative: {derivative}"
72
+ except Exception as e:
73
+ return f"Error: {e}"
74
+
75
+ # Function to calculate the integral of an expression
76
+ def calculate_integral(expression):
77
+ try:
78
+ expression = expression.replace("^", "**")
79
  expr = sp.sympify(expression)
 
 
80
  integral = sp.integrate(expr)
 
81
  return f"Integral: {integral}"
82
+ except Exception as e:
83
+ return f"Error: {e}"
84
+
85
+ # Function to calculate basic trigonometric functions
86
+ def calculate_trigonometry(expression):
87
+ try:
88
+ expression = expression.replace("^", "**")
89
+ expr = sp.sympify(expression)
90
+ trig_value = sp.sin(expr), sp.cos(expr), sp.tan(expr)
91
+ return f"Sine: {trig_value[0]}, Cosine: {trig_value[1]}, Tangent: {trig_value[2]}"
92
+ except Exception as e:
93
+ return f"Error: {e}"
94
+
95
+ # Function for linear algebra operations
96
+ def linear_algebra_operations(operation, matrix1, matrix2=None):
97
+ try:
98
+ matrix1 = sp.Matrix(matrix1)
99
+ if matrix2:
100
+ matrix2 = sp.Matrix(matrix2)
101
+
102
+ if operation == "multiply":
103
+ result = matrix1 * matrix2
104
+ elif operation == "determinant":
105
+ result = matrix1.det()
106
+ elif operation == "inverse":
107
+ result = matrix1.inv()
108
+ return f"Result: {result}"
109
  except Exception as e:
110
  return f"Error: {e}"
111
 
 
115
 
116
  # Tabs for different functionalities
117
  tab = st.radio("Select a problem type:", ["Solve Algebraic Equation", "Simplify Expression", "Factor Expression",
118
+ "Solve Word Problem", "Find Factors of a Number", "Derivative", "Integral",
119
+ "Trigonometry", "Linear Algebra"])
120
+
121
+ if tab == "Solve Algebraic Equation":
122
+ st.header("Solve Algebraic Equation")
123
+ equation_input = st.text_input("Enter an equation (e.g., 2*x + 3 = 5)", "")
124
+ if st.button("Solve Equation"):
125
+ if equation_input:
126
+ solution = solve_equation(equation_input)
127
+ st.write(solution)
128
+ else:
129
+ st.write("Please enter an equation!")
130
+
131
+ elif tab == "Simplify Expression":
132
+ st.header("Simplify Expression")
133
+ expression_input = st.text_input("Enter an expression (e.g., 2*x + 3*x - 4)", "")
134
+ if st.button("Simplify Expression"):
135
+ if expression_input:
136
+ simplified_expr = simplify_expression(expression_input)
137
+ st.write(simplified_expr)
138
+ else:
139
+ st.write("Please enter an expression!")
140
 
141
+ elif tab == "Factor Expression":
142
+ st.header("Factor Expression")
143
+ factor_input = st.text_input("Enter an expression to factor (e.g., x^2 - 5*x + 6)", "")
144
+ if st.button("Factor Expression"):
145
+ if factor_input:
146
+ factored_expr = factor_expression(factor_input)
147
+ st.write(factored_expr)
148
+ else:
149
+ st.write("Please enter an expression!")
150
+
151
+ elif tab == "Solve Word Problem":
152
+ st.header("Solve Word Problem")
153
+ operation = st.selectbox("Choose an operation:", ["addition", "subtraction", "multiplication", "division"])
154
+ num1_input = st.number_input("Enter the first number", 0)
155
+ num2_input = st.number_input("Enter the second number", 0)
156
+ if st.button("Solve Word Problem"):
157
+ result = solve_word_problem(operation, num1_input, num2_input)
158
+ st.write(result)
159
+
160
+ elif tab == "Find Factors of a Number":
161
+ st.header("Find Factors of a Number")
162
+ number_input = st.number_input("Enter a number", min_value=1)
163
+ if st.button("Find Factors"):
164
+ if number_input:
165
+ factors = find_factors(number_input)
166
+ st.write(f"Factors of {number_input}: {factors}")
167
+ else:
168
+ st.write("Please enter a number!")
169
+
170
+ elif tab == "Derivative":
171
+ st.header("Calculate Derivative")
172
+ derivative_input = st.text_input("Enter an expression to differentiate (e.g., x^2 + 3*x)", "")
173
+ if st.button("Calculate Derivative"):
174
+ if derivative_input:
175
+ derivative = calculate_derivative(derivative_input)
176
+ st.write(derivative)
177
+ else:
178
+ st.write("Please enter an expression!")
179
+
180
+ elif tab == "Integral":
181
  st.header("Calculate Integral")
182
+ integral_input = st.text_input("Enter an expression to integrate (e.g., x^2 + 3*x)", "")
183
  if st.button("Calculate Integral"):
184
  if integral_input:
185
  integral = calculate_integral(integral_input)
 
187
  else:
188
  st.write("Please enter an expression!")
189
 
190
+ elif tab == "Trigonometry":
191
+ st.header("Trigonometric Functions")
192
+ trig_input = st.text_input("Enter an angle (in radians) for sine, cosine, tangent (e.g., pi/4)", "")
193
+ if st.button("Calculate Trigonometry"):
194
+ if trig_input:
195
+ trig_result = calculate_trigonometry(trig_input)
196
+ st.write(trig_result)
197
+ else:
198
+ st.write("Please enter an angle!")
199
+
200
+ elif tab == "Linear Algebra":
201
+ st.header("Linear Algebra Operations")
202
+ operation = st.selectbox("Choose an operation:", ["multiply", "determinant", "inverse"])
203
+ matrix1_input = st.text_area("Enter the first matrix (e.g., [[1,2],[3,4]])", "")
204
+ matrix2_input = None
205
+ if operation == "multiply":
206
+ matrix2_input = st.text_area("Enter the second matrix (e.g., [[5,6],[7,8]])", "")
207
+
208
+ if st.button("Perform Operation"):
209
+ if matrix1_input:
210
+ matrix1 = eval(matrix1_input)
211
+ matrix2 = eval(matrix2_input) if matrix2_input else None
212
+ result = linear_algebra_operations(operation, matrix1, matrix2)
213
+ st.write(result)
214
+ else:
215
+ st.write("Please enter a matrix!")
216
+
217
  # Run the app
218
  if __name__ == "__main__":
219
  main()