Spaces:
Runtime error
Runtime error
feat: add Gradio interface for simultaneous equation solving; update equations tab to include new solver
Browse files
maths/equations/equations_tab.py
CHANGED
|
@@ -2,10 +2,23 @@ import gradio as gr
|
|
| 2 |
from maths.equations.solve_quadratic import solve_quadratic_interface, quadratic_visualizer_interface
|
| 3 |
from maths.equations.solve_cubic import cubic_solver_interface
|
| 4 |
from maths.equations.solve_poly import poly_solver_interface
|
|
|
|
| 5 |
|
| 6 |
|
| 7 |
equations_tab = gr.TabbedInterface(
|
| 8 |
-
[
|
| 9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
title="Equations"
|
| 11 |
)
|
|
|
|
| 2 |
from maths.equations.solve_quadratic import solve_quadratic_interface, quadratic_visualizer_interface
|
| 3 |
from maths.equations.solve_cubic import cubic_solver_interface
|
| 4 |
from maths.equations.solve_poly import poly_solver_interface
|
| 5 |
+
from maths.equations.solve_simultaneous import simultaneous_solver_interface
|
| 6 |
|
| 7 |
|
| 8 |
equations_tab = gr.TabbedInterface(
|
| 9 |
+
[
|
| 10 |
+
solve_quadratic_interface,
|
| 11 |
+
quadratic_visualizer_interface,
|
| 12 |
+
cubic_solver_interface,
|
| 13 |
+
poly_solver_interface,
|
| 14 |
+
simultaneous_solver_interface
|
| 15 |
+
],
|
| 16 |
+
[
|
| 17 |
+
"Quadratic Solver",
|
| 18 |
+
"Quadratic Visualizer",
|
| 19 |
+
"Cubic Solver",
|
| 20 |
+
"Polynomial Solver",
|
| 21 |
+
"Simultaneous Equation Solver"
|
| 22 |
+
],
|
| 23 |
title="Equations"
|
| 24 |
)
|
maths/equations/solve_poly.py
CHANGED
|
@@ -1,4 +1,5 @@
|
|
| 1 |
import numpy as np
|
|
|
|
| 2 |
|
| 3 |
def solve_polynomial(coeffs):
|
| 4 |
"""
|
|
|
|
| 1 |
import numpy as np
|
| 2 |
+
import gradio as gr
|
| 3 |
|
| 4 |
def solve_polynomial(coeffs):
|
| 5 |
"""
|
maths/equations/solve_simultaneous.py
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import numpy as np
|
| 3 |
+
|
| 4 |
+
def solve_simultaneous_interface(coeffs, constants):
|
| 5 |
+
"""
|
| 6 |
+
coeffs: Multiline string, each line is comma-separated coefficients for one equation.
|
| 7 |
+
constants: Comma-separated string of constants (right-hand side values).
|
| 8 |
+
"""
|
| 9 |
+
try:
|
| 10 |
+
coeff_lines = [line.strip() for line in coeffs.strip().splitlines() if line.strip()]
|
| 11 |
+
matrix = [list(map(float, line.split(','))) for line in coeff_lines]
|
| 12 |
+
A = np.array(matrix)
|
| 13 |
+
b = np.array([float(x) for x in constants.strip().split(',')])
|
| 14 |
+
if A.shape[0] != A.shape[1]:
|
| 15 |
+
return f"Number of equations and unknowns must match (got {A.shape[0]} equations, {A.shape[1]} unknowns)."
|
| 16 |
+
if b.shape[0] != A.shape[0]:
|
| 17 |
+
return f"Number of constants must match number of equations."
|
| 18 |
+
solution = np.linalg.solve(A, b)
|
| 19 |
+
output = ""
|
| 20 |
+
for idx, val in enumerate(solution):
|
| 21 |
+
output += f"Variable {idx+1}: {val}\n"
|
| 22 |
+
return output
|
| 23 |
+
except Exception as e:
|
| 24 |
+
return f"Error: {e}"
|
| 25 |
+
|
| 26 |
+
simultaneous_solver_interface = gr.Interface(
|
| 27 |
+
fn=solve_simultaneous_interface,
|
| 28 |
+
inputs=[
|
| 29 |
+
gr.Textbox(lines=4, label="Coefficients (one equation per line, comma-separated)", placeholder="e.g.\n1,2\n3,4"),
|
| 30 |
+
gr.Textbox(label="Constants (comma-separated)", placeholder="e.g. 5, 6")
|
| 31 |
+
],
|
| 32 |
+
outputs="text",
|
| 33 |
+
title="Simultaneous Linear Equation Solver",
|
| 34 |
+
description="Solve simultaneous linear equations. Enter coefficients for each equation (one per line), and constants."
|
| 35 |
+
)
|