ghaffarmumtat123 commited on
Commit
1a22c9a
·
verified ·
1 Parent(s): f5e1d69

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +97 -18
app.py CHANGED
@@ -4,37 +4,22 @@ import sympy as sp
4
  # Function to solve algebraic equations
5
  def solve_equation(equation):
6
  try:
7
- # Check if the equation contains "="
8
  if "=" not in equation:
9
  return "Invalid input. Please enter an equation with '=' (e.g., 2*x + 3 = 5)."
10
-
11
- # Replace '^' with '**' for exponentiation (common in math inputs)
12
  equation = equation.replace("^", "**")
13
-
14
- # Remove unwanted spaces and handle possible unexpected formatting
15
  equation = equation.replace(" ", "")
16
-
17
- # Split the equation at the equal sign
18
  lhs, rhs = equation.split("=")
19
-
20
- # Use sympy to parse the left and right hand sides of the equation
21
  lhs_expr = sp.sympify(lhs)
22
  rhs_expr = sp.sympify(rhs)
23
-
24
- # Solve the equation lhs = rhs
25
  solution = sp.solve(lhs_expr - rhs_expr)
26
-
27
  return f"Solution: {solution}"
28
-
29
  except Exception as e:
30
  return f"Error: {e}"
31
 
32
  # Function to simplify expressions
33
  def simplify_expression(expression):
34
  try:
35
- # Replace '^' with '**' for exponentiation
36
  expression = expression.replace("^", "**")
37
-
38
  expr = sp.sympify(expression)
39
  simplified_expr = sp.simplify(expr)
40
  return f"Simplified Expression: {simplified_expr}"
@@ -44,9 +29,7 @@ def simplify_expression(expression):
44
  # Function to factor expressions
45
  def factor_expression(expression):
46
  try:
47
- # Replace '^' with '**' for exponentiation
48
  expression = expression.replace("^", "**")
49
-
50
  expr = sp.sympify(expression)
51
  factored_expr = sp.factor(expr)
52
  return f"Factored Expression: {factored_expr}"
@@ -79,12 +62,61 @@ def find_factors(number):
79
  factors.append(i)
80
  return factors
81
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
82
  # Streamlit Interface
83
  def main():
84
  st.title("Math Problem-Solution App")
85
 
86
  # Tabs for different functionalities
87
- tab = st.radio("Select a problem type:", ["Solve Algebraic Equation", "Simplify Expression", "Factor Expression", "Solve Word Problem", "Find Factors of a Number"])
 
 
88
 
89
  if tab == "Solve Algebraic Equation":
90
  st.header("Solve Algebraic Equation")
@@ -135,6 +167,53 @@ def main():
135
  else:
136
  st.write("Please enter a number!")
137
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
138
  # Run the app
139
  if __name__ == "__main__":
140
  main()
 
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}"
 
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}"
 
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
+
112
  # Streamlit Interface
113
  def main():
114
  st.title("Math Problem-Solution App")
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")
 
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)
186
+ st.write(integral)
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()