File size: 2,640 Bytes
dadb690
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b336eab
dadb690
a9fa731
7dd00f2
 
 
 
 
dadb690
 
 
 
b336eab
 
 
 
dadb690
b336eab
 
 
dadb690
 
 
 
b336eab
1c2b812
b336eab
4e9085b
b336eab
 
 
 
 
 
dadb690
b336eab
dadb690
 
f647df1
7dd00f2
 
dadb690
 
 
 
 
6cc8801
 
b336eab
dadb690
 
b336eab
dadb690
 
 
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
95
96
97
98
99
100
101
import gradio as gr
import matplotlib.pyplot as plt
import numpy as np
import cmath


class Quadratic:
    """Representing a quadratic expression in the form of ax² + bx + c"""

    def __init__(self, a: float, b: float, c: float) -> None:
        if all(isinstance(i, (int, float)) for i in (a, b, c)):
            if a == 0:
                raise ValueError("Coefficient 'a' cannot be 0 for a quadratic equation.")
            self.a = a
            self.b = b
            self.c = c
        else:
            raise ValueError("Arguments must be int or float")

    def __repr__(self) -> str:
        return f"{self.a}x² + {self.b}x + {self.c}"

    def solveQuad(self):
        """Returns roots (real or complex)"""
        discriminant = self.b**2 - 4 * self.a * self.c
        root1 = (-self.b + cmath.sqrt(discriminant)) / (2 * self.a)
        root2 = (-self.b - cmath.sqrt(discriminant)) / (2 * self.a)
        return root1, root2

    def evaluate(self, value):
        return (self.a * value**2) + (self.b * value) + self.c

    def drawFigure(self):
        x_axis = np.linspace(-10, 10, 200)
        y_axis = self.evaluate(x_axis)

        fig, ax = plt.subplots()
        ax.plot(x_axis, y_axis)
        ax.axhline(0)
        ax.axvline(0)
        ax.grid(True)
        ax.set_title(str(self))

        return fig


# -------- Function for Gradio --------
def solve_and_plot(a, b, c):
    try:
        # Convert string inputs to float (supports negative values)
        a = float(a)
        b = float(b)
        c = float(c)

        quad = Quadratic(a, b, c)
        root1, root2 = quad.solveQuad()
        fig = quad.drawFigure()

        result = (
            f"Equation: {quad}\n\n"
            f"Root 1: {root1}\n"
            f"Root 2: {root2}"
        )

        return result, fig

    except Exception as e:
        return f"Error: {str(e)}", None


# -------- Theme with Google Font --------
theme = gr.themes.Ocean(
    font=[
        gr.themes.GoogleFont("Libertinus Math"),
        "ui-sans-serif",
        "system-ui"
    ]
)


# -------- Gradio UI --------
interface = gr.Interface(
    fn=solve_and_plot,
    inputs=[
        gr.Textbox(label="Coefficient a", value="0"),
        gr.Textbox(label="Coefficient b", value="0"),
        gr.Textbox(label="Coefficient c", value="0"),
    ],
    outputs=[
        gr.Textbox(label="Solution"),
        gr.Plot(label="Graph"),
    ],
    title="Quadratic Equation Solver",
    description="Solve ax² + bx + c = 0 and visualize its graph • Made by Pratham Tripathi",
    theme=theme
)


# -------- Launch --------
if __name__ == "__main__":
    interface.launch()