Spaces:
Sleeping
Sleeping
Create linear.py
Browse files
linear.py
ADDED
|
@@ -0,0 +1,136 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#linear.py
|
| 2 |
+
|
| 3 |
+
import gradio as gr
|
| 4 |
+
import sympy as sp
|
| 5 |
+
import numpy as np
|
| 6 |
+
import matplotlib.pyplot as plt
|
| 7 |
+
import random
|
| 8 |
+
|
| 9 |
+
x, y = sp.symbols('x y')
|
| 10 |
+
|
| 11 |
+
def generate_linear_template():
|
| 12 |
+
return "$$ a_1x + b_1y = c_1 \\ a_2x + b_2y = c_2 $$"
|
| 13 |
+
|
| 14 |
+
def load_linear_example():
|
| 15 |
+
examples = [
|
| 16 |
+
("1 -4 -2", "5 1 9"),
|
| 17 |
+
("2 1 8", "1 -1 2"),
|
| 18 |
+
("3 2 12", "1 1 5"),
|
| 19 |
+
("4 -1 3", "2 3 6"),
|
| 20 |
+
("1 2 10", "3 -1 5")
|
| 21 |
+
]
|
| 22 |
+
return random.choice(examples)
|
| 23 |
+
|
| 24 |
+
def solve_linear_system_from_coeffs(eq1_str, eq2_str):
|
| 25 |
+
try:
|
| 26 |
+
coeffs1 = list(map(float, eq1_str.strip().split()))
|
| 27 |
+
coeffs2 = list(map(float, eq2_str.strip().split()))
|
| 28 |
+
|
| 29 |
+
if len(coeffs1) != 3 or len(coeffs2) != 3:
|
| 30 |
+
return "β οΈ Please enter exactly 3 coefficients for each equation.", None, None, None
|
| 31 |
+
|
| 32 |
+
a1, b1, c1 = coeffs1
|
| 33 |
+
a2, b2, c2 = coeffs2
|
| 34 |
+
|
| 35 |
+
eq1 = sp.Eq(a1 * x + b1 * y, c1)
|
| 36 |
+
eq2 = sp.Eq(a2 * x + b2 * y, c2)
|
| 37 |
+
|
| 38 |
+
sol = sp.solve([eq1, eq2], (x, y), dict=True)
|
| 39 |
+
if not sol:
|
| 40 |
+
return "β No unique solution.", None, None, None
|
| 41 |
+
|
| 42 |
+
solution = sol[0]
|
| 43 |
+
eq_latex = f"$$ {sp.latex(eq1)} \\ {sp.latex(eq2)} $$"
|
| 44 |
+
|
| 45 |
+
steps = rf"""
|
| 46 |
+
### π Step-by-step Solution
|
| 47 |
+
1. **Original Equations:**
|
| 48 |
+
$$ {sp.latex(eq1)} $$
|
| 49 |
+
$$ {sp.latex(eq2)} $$
|
| 50 |
+
2. **Standard Form:** Already provided.
|
| 51 |
+
3. **Solve using SymPy `solve`:** Internally applies substitution/elimination.
|
| 52 |
+
4. **Solve for `x` and `y`:**
|
| 53 |
+
$$ x = {sp.latex(solution[x])}, \quad y = {sp.latex(solution[y])} $$
|
| 54 |
+
5. **Verification:** Substitute back into both equations."""
|
| 55 |
+
|
| 56 |
+
x_vals = np.linspace(-10, 10, 400)
|
| 57 |
+
f1 = sp.solve(eq1, y)
|
| 58 |
+
f2 = sp.solve(eq2, y)
|
| 59 |
+
|
| 60 |
+
fig, ax = plt.subplots()
|
| 61 |
+
if f1:
|
| 62 |
+
f1_func = sp.lambdify(x, f1[0], modules='numpy')
|
| 63 |
+
ax.plot(x_vals, f1_func(x_vals), label=sp.latex(eq1))
|
| 64 |
+
if f2:
|
| 65 |
+
f2_func = sp.lambdify(x, f2[0], modules='numpy')
|
| 66 |
+
ax.plot(x_vals, f2_func(x_vals), label=sp.latex(eq2))
|
| 67 |
+
|
| 68 |
+
ax.plot(solution[x], solution[y], 'ro', label=f"Solution ({solution[x]}, {solution[y]})")
|
| 69 |
+
ax.axhline(0, color='black', linewidth=0.5)
|
| 70 |
+
ax.axvline(0, color='black', linewidth=0.5)
|
| 71 |
+
ax.legend()
|
| 72 |
+
ax.set_title("π Graph of the Equations")
|
| 73 |
+
ax.grid(True)
|
| 74 |
+
|
| 75 |
+
return eq_latex, steps, fig, ""
|
| 76 |
+
except Exception as e:
|
| 77 |
+
return f"β Error: {e}", None, None, None
|
| 78 |
+
|
| 79 |
+
def linear_tab():
|
| 80 |
+
with gr.Tab("π Linear System Solver"):
|
| 81 |
+
gr.Markdown("## π Solve 2x2 Linear System")
|
| 82 |
+
linear_template = gr.Markdown(value=generate_linear_template())
|
| 83 |
+
|
| 84 |
+
with gr.Row():
|
| 85 |
+
linear_eq1_input = gr.Textbox(label="Equation 1 Coefficients (a1 b1 c1)", placeholder="e.g. 2 1 8")
|
| 86 |
+
linear_eq2_input = gr.Textbox(label="Equation 2 Coefficients (a2 b2 c2)", placeholder="e.g. 1 -1 2")
|
| 87 |
+
|
| 88 |
+
linear_example_btn = gr.Button("π Load Example")
|
| 89 |
+
preview_button = gr.Button("π Preview Equations")
|
| 90 |
+
|
| 91 |
+
linear_equation_display = gr.Markdown()
|
| 92 |
+
with gr.Row():
|
| 93 |
+
confirm_btn = gr.Button("β
Display Solution", visible=False)
|
| 94 |
+
cancel_btn = gr.Button("βοΈ Make Changes in Equation", visible=False)
|
| 95 |
+
|
| 96 |
+
linear_steps_md = gr.Markdown()
|
| 97 |
+
linear_plot = gr.Plot()
|
| 98 |
+
linear_error = gr.Textbox(visible=False)
|
| 99 |
+
|
| 100 |
+
def update_example():
|
| 101 |
+
eq1, eq2 = load_linear_example()
|
| 102 |
+
return eq1, eq2
|
| 103 |
+
|
| 104 |
+
linear_example_btn.click(fn=update_example, inputs=[], outputs=[linear_eq1_input, linear_eq2_input])
|
| 105 |
+
|
| 106 |
+
def preview_equations(eq1_str, eq2_str):
|
| 107 |
+
try:
|
| 108 |
+
coeffs1 = list(map(float, eq1_str.strip().split()))
|
| 109 |
+
coeffs2 = list(map(float, eq2_str.strip().split()))
|
| 110 |
+
if len(coeffs1) != 3 or len(coeffs2) != 3:
|
| 111 |
+
return "β οΈ Please enter exactly 3 coefficients for each equation.", gr.update(visible=False), gr.update(visible=False)
|
| 112 |
+
a1, b1, c1 = coeffs1
|
| 113 |
+
a2, b2, c2 = coeffs2
|
| 114 |
+
eq1 = sp.Eq(a1 * x + b1 * y, c1)
|
| 115 |
+
eq2 = sp.Eq(a2 * x + b2 * y, c2)
|
| 116 |
+
eq_latex = f"### β
Confirm Equations\n\n$$ {sp.latex(eq1)} \\\\ {sp.latex(eq2)} $$"
|
| 117 |
+
return eq_latex, gr.update(visible=True), gr.update(visible=True)
|
| 118 |
+
except Exception as e:
|
| 119 |
+
return f"β Error parsing equations: {e}", gr.update(visible=False), gr.update(visible=False)
|
| 120 |
+
|
| 121 |
+
preview_button.click(
|
| 122 |
+
fn=preview_equations,
|
| 123 |
+
inputs=[linear_eq1_input, linear_eq2_input],
|
| 124 |
+
outputs=[linear_equation_display, confirm_btn, cancel_btn]
|
| 125 |
+
)
|
| 126 |
+
|
| 127 |
+
cancel_btn.click(
|
| 128 |
+
fn=lambda: (gr.update(visible=False), gr.update(visible=False), "", None, None),
|
| 129 |
+
outputs=[confirm_btn, cancel_btn, linear_equation_display, linear_steps_md, linear_plot]
|
| 130 |
+
)
|
| 131 |
+
|
| 132 |
+
confirm_btn.click(
|
| 133 |
+
fn=solve_linear_system_from_coeffs,
|
| 134 |
+
inputs=[linear_eq1_input, linear_eq2_input],
|
| 135 |
+
outputs=[linear_equation_display, linear_steps_md, linear_plot, linear_error]
|
| 136 |
+
)
|