File size: 15,309 Bytes
923ca8b
 
 
 
 
 
 
 
 
218ef3d
923ca8b
 
218ef3d
 
 
 
923ca8b
218ef3d
 
 
 
 
 
 
 
 
 
923ca8b
218ef3d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
923ca8b
218ef3d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
923ca8b
218ef3d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
923ca8b
 
218ef3d
 
 
 
 
923ca8b
218ef3d
 
 
 
 
923ca8b
218ef3d
 
 
 
 
923ca8b
218ef3d
 
 
 
 
923ca8b
218ef3d
 
 
 
 
923ca8b
 
 
 
218ef3d
923ca8b
 
218ef3d
 
923ca8b
 
 
218ef3d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
923ca8b
 
 
218ef3d
 
 
 
 
 
 
 
 
923ca8b
 
218ef3d
 
923ca8b
 
 
 
 
 
218ef3d
 
 
 
 
 
 
 
 
 
 
 
 
923ca8b
 
218ef3d
 
 
 
 
923ca8b
218ef3d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
923ca8b
218ef3d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
923ca8b
218ef3d
 
 
 
 
 
 
923ca8b
218ef3d
 
 
 
 
 
 
923ca8b
218ef3d
 
 
923ca8b
218ef3d
 
 
 
923ca8b
218ef3d
 
 
 
 
923ca8b
218ef3d
 
 
 
923ca8b
 
218ef3d
 
 
 
 
923ca8b
218ef3d
 
 
 
 
 
 
 
 
923ca8b
218ef3d
 
 
 
 
 
 
 
 
 
923ca8b
218ef3d
 
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
import gradio as gr
import matplotlib.pyplot as plt
import numpy as np
import sympy as sp
from matplotlib.patches import Circle
import io
import base64
from PIL import Image
import warnings
warnings.filterwarnings('ignore')

class MathVisualizer:
    def __init__(self):
        self.x = sp.Symbol('x')
        self.y = sp.Symbol('y')
        self.t = sp.Symbol('t')

    def safe_eval(self, expression, variables):
        """Safely evaluate mathematical expressions"""
        try:
            # Convert string to sympy expression
            expr = sp.sympify(expression)
            # Convert to numpy function for evaluation
            func = sp.lambdify(variables, expr, 'numpy')
            return func
        except Exception as e:
            raise ValueError(f"Invalid expression: {str(e)}")

    def plot_2d_function(self, equation, x_range, y_range, color, style, grid, title):
        """Plot 2D function y = f(x)"""
        try:
            func = self.safe_eval(equation, self.x)
            x_vals = np.linspace(x_range[0], x_range[1], 1000)
            y_vals = func(x_vals)
            
            # Handle complex numbers and infinities
            y_vals = np.real(y_vals)
            y_vals = np.where(np.abs(y_vals) > 1e10, np.nan, y_vals)
            
            plt.figure(figsize=(10, 8))
            plt.plot(x_vals, y_vals, color=color, linewidth=2, linestyle=style)
            plt.xlim(x_range)
            plt.ylim(y_range)
            plt.xlabel('x', fontsize=12)
            plt.ylabel('y', fontsize=12)
            plt.title(title or f'y = {equation}', fontsize=14)
            plt.grid(grid, alpha=0.3)
            
            return plt.gcf()
            
        except Exception as e:
            plt.figure(figsize=(10, 8))
            plt.text(0.5, 0.5, f'Error: {str(e)}', ha='center', va='center', 
                    transform=plt.gca().transAxes, fontsize=12, color='red')
            plt.title('Error in equation')
            return plt.gcf()

    def plot_parametric(self, x_equation, y_equation, t_range, color, style, grid, title):
        """Plot parametric equations x = f(t), y = g(t)"""
        try:
            x_func = self.safe_eval(x_equation, self.t)
            y_func = self.safe_eval(y_equation, self.t)
            
            t_vals = np.linspace(t_range[0], t_range[1], 1000)
            x_vals = x_func(t_vals)
            y_vals = y_func(t_vals)
            
            # Handle complex numbers
            x_vals = np.real(x_vals)
            y_vals = np.real(y_vals)
            
            plt.figure(figsize=(10, 8))
            plt.plot(x_vals, y_vals, color=color, linewidth=2, linestyle=style)
            plt.xlabel('x', fontsize=12)
            plt.ylabel('y', fontsize=12)
            plt.title(title or f'x = {x_equation}, y = {y_equation}', fontsize=14)
            plt.grid(grid, alpha=0.3)
            plt.axis('equal')
            
            return plt.gcf()
            
        except Exception as e:
            plt.figure(figsize=(10, 8))
            plt.text(0.5, 0.5, f'Error: {str(e)}', ha='center', va='center', 
                    transform=plt.gca().transAxes, fontsize=12, color='red')
            plt.title('Error in parametric equations')
            return plt.gcf()

    def plot_polar(self, equation, theta_range, color, style, grid, title):
        """Plot polar equations r = f(θ)"""
        try:
            theta = sp.Symbol('theta')
            func = self.safe_eval(equation.replace('θ', 'theta').replace('theta', 'theta'), theta)
            
            theta_vals = np.linspace(theta_range[0], theta_range[1], 1000)
            r_vals = func(theta_vals)
            r_vals = np.real(r_vals)
            
            plt.figure(figsize=(10, 8))
            ax = plt.subplot(111, projection='polar')
            ax.plot(theta_vals, r_vals, color=color, linewidth=2, linestyle=style)
            ax.set_title(title or f'r = {equation}', fontsize=14, pad=20)
            ax.grid(grid, alpha=0.3)
            
            return plt.gcf()
            
        except Exception as e:
            plt.figure(figsize=(10, 8))
            plt.text(0.5, 0.5, f'Error: {str(e)}', ha='center', va='center', 
                    transform=plt.gca().transAxes, fontsize=12, color='red')
            plt.title('Error in polar equation')
            return plt.gcf()

    def plot_implicit(self, equation, x_range, y_range, color, grid, title):
        """Plot implicit equations F(x,y) = 0"""
        try:
            # Parse equation (assume it equals 0)
            expr = sp.sympify(equation)
            
            x_vals = np.linspace(x_range[0], x_range[1], 400)
            y_vals = np.linspace(y_range[0], y_range[1], 400)
            X, Y = np.meshgrid(x_vals, y_vals)
            
            # Convert to numpy function
            func = sp.lambdify([self.x, self.y], expr, 'numpy')
            Z = func(X, Y)
            Z = np.real(Z)
            
            plt.figure(figsize=(10, 8))
            plt.contour(X, Y, Z, levels=[0], colors=[color], linewidths=2)
            plt.xlim(x_range)
            plt.ylim(y_range)
            plt.xlabel('x', fontsize=12)
            plt.ylabel('y', fontsize=12)
            plt.title(title or f'{equation} = 0', fontsize=14)
            plt.grid(grid, alpha=0.3)
            
            return plt.gcf()
            
        except Exception as e:
            plt.figure(figsize=(10, 8))
            plt.text(0.5, 0.5, f'Error: {str(e)}', ha='center', va='center', 
                    transform=plt.gca().transAxes, fontsize=12, color='red')
            plt.title('Error in implicit equation')
            return plt.gcf()

    def plot_3d_surface(self, equation, x_range, y_range, color_scheme, title):
        """Plot 3D surface z = f(x,y)"""
        try:
            func = self.safe_eval(equation, [self.x, self.y])
            
            x_vals = np.linspace(x_range[0], x_range[1], 50)
            y_vals = np.linspace(y_range[0], y_range[1], 50)
            X, Y = np.meshgrid(x_vals, y_vals)
            Z = func(X, Y)
            Z = np.real(Z)
            
            fig = plt.figure(figsize=(12, 10))
            ax = fig.add_subplot(111, projection='3d')
            surf = ax.plot_surface(X, Y, Z, cmap=color_scheme, alpha=0.8)
            ax.set_xlabel('x', fontsize=12)
            ax.set_ylabel('y', fontsize=12)
            ax.set_zlabel('z', fontsize=12)
            ax.set_title(title or f'z = {equation}', fontsize=14)
            fig.colorbar(surf, shrink=0.5, aspect=5)
            
            return fig
            
        except Exception as e:
            fig = plt.figure(figsize=(12, 10))
            plt.text(0.5, 0.5, f'Error: {str(e)}', ha='center', va='center', 
                    transform=plt.gca().transAxes, fontsize=12, color='red')
            plt.title('Error in 3D equation')
            return fig

# Initialize visualizer
visualizer = MathVisualizer()

def generate_plot(plot_type, equation, x_equation, y_equation,
                  x_min, x_max, y_min, y_max, t_min, t_max, theta_min, theta_max,
                  color, line_style, color_scheme, show_grid, custom_title):
    
    plt.close('all')  # Close any existing plots

    try:
        if plot_type == "2D Function (y = f(x))":
            fig = visualizer.plot_2d_function(
                equation, (x_min, x_max), (y_min, y_max), 
                color, line_style, show_grid, custom_title
            )
        
        elif plot_type == "Parametric (x = f(t), y = g(t))":
            fig = visualizer.plot_parametric(
                x_equation, y_equation, (t_min, t_max), 
                color, line_style, show_grid, custom_title
            )
        
        elif plot_type == "Polar (r = f(θ))":
            fig = visualizer.plot_polar(
                equation, (theta_min, theta_max), 
                color, line_style, show_grid, custom_title
            )
        
        elif plot_type == "Implicit (F(x,y) = 0)":
            fig = visualizer.plot_implicit(
                equation, (x_min, x_max), (y_min, y_max), 
                color, show_grid, custom_title
            )
        
        elif plot_type == "3D Surface (z = f(x,y))":
            fig = visualizer.plot_3d_surface(
                equation, (x_min, x_max), (y_min, y_max), 
                color_scheme, custom_title
            )
        
        return fig
        
    except Exception as e:
        plt.figure(figsize=(10, 8))
        plt.text(0.5, 0.5, f'Error: {str(e)}', ha='center', va='center', 
                transform=plt.gca().transAxes, fontsize=12, color='red')
        plt.title('Error generating plot')
        return plt.gcf()

# Example equations for different types
examples = {
    "2D Function (y = f(x))": [
        "sin(x)",
        "x**2 + 2*x + 1",
        "exp(-x**2)",
        "tan(x)",
        "log(abs(x))"
    ],
    "Parametric (x = f(t), y = g(t))": [
        ("cos(t)", "sin(t)"),  # Circle
        ("t*cos(t)", "t*sin(t)"),  # Spiral
        ("cos(3*t)", "sin(2*t)"),  # Lissajous
    ],
    "Polar (r = f(θ))": [
        "1 + cos(theta)",  # Cardioid
        "sin(4*theta)",  # Rose
        "theta",  # Spiral
    ],
    "Implicit (F(x,y) = 0)": [
        "x**2 + y**2 - 1",  # Circle
        "(x**2 + y**2)**2 - 2*(x**2 - y**2)",  # Lemniscate
        "x**3 + y**3 - 3*x*y",  # Folium of Descartes
    ],
    "3D Surface (z = f(x,y))": [
        "sin(sqrt(x**2 + y**2))",
        "x**2 - y**2",
        "exp(-(x**2 + y**2))",
    ]
}

def load_example(plot_type, example_idx):
    if plot_type in examples:
        example_list = examples[plot_type]
        if 0 <= example_idx < len(example_list):
            example = example_list[example_idx]
            if plot_type == "Parametric (x = f(t), y = g(t))":
                return example[0], example[1], "", ""
            else:
                return example, "", "", ""
    return "", "", "", ""

# Create Gradio interface
with gr.Blocks(title="Mathematical Equation Visualizer", theme=gr.themes.Soft()) as demo:
    gr.Markdown("""
# 📊 Mathematical Equation Visualizer

Generate beautiful visualizations of mathematical equations with various plot types and customization options.

**Supported functions:** sin, cos, tan, exp, log, sqrt, abs, and basic arithmetic (+, -, *, /, **)
""")
    
    with gr.Row():
        with gr.Column(scale=1):
            plot_type = gr.Dropdown(
                choices=[
                    "2D Function (y = f(x))",
                    "Parametric (x = f(t), y = g(t))",
                    "Polar (r = f(θ))",
                    "Implicit (F(x,y) = 0)",
                    "3D Surface (z = f(x,y))"
                ],
                value="2D Function (y = f(x))",
                label="Plot Type"
            )
            
            with gr.Group():
                equation = gr.Textbox(
                    value="sin(x)",
                    label="Equation",
                    placeholder="e.g., sin(x), x**2 + 1, etc."
                )
                
                with gr.Row(visible=False) as parametric_inputs:
                    x_equation = gr.Textbox(
                        label="x = f(t)",
                        placeholder="e.g., cos(t)"
                    )
                    y_equation = gr.Textbox(
                        label="y = g(t)",
                        placeholder="e.g., sin(t)"
                    )
            
            with gr.Group():
                gr.Markdown("### Range Settings")
                with gr.Row():
                    x_min = gr.Number(value=-10, label="x min")
                    x_max = gr.Number(value=10, label="x max")
                with gr.Row():
                    y_min = gr.Number(value=-10, label="y min")
                    y_max = gr.Number(value=10, label="y max")
                with gr.Row():
                    t_min = gr.Number(value=0, label="t min")
                    t_max = gr.Number(value=6.28, label="t max")
                with gr.Row():
                    theta_min = gr.Number(value=0, label="θ min")
                    theta_max = gr.Number(value=6.28, label="θ max")
            
            with gr.Group():
                gr.Markdown("### Style Settings")
                color = gr.ColorPicker(value="#1f77b4", label="Line Color")
                line_style = gr.Dropdown(
                    choices=["-", "--", "-.", ":"],
                    value="-",
                    label="Line Style"
                )
                color_scheme = gr.Dropdown(
                    choices=["viridis", "plasma", "inferno", "magma", "coolwarm", "RdYlBu"],
                    value="viridis",
                    label="3D Color Scheme"
                )
                show_grid = gr.Checkbox(value=True, label="Show Grid")
                custom_title = gr.Textbox(label="Custom Title (optional)")
            
            generate_btn = gr.Button("🎨 Generate Plot", variant="primary")
            
            # Example buttons
            gr.Markdown("### 📚 Examples")
            example_dropdown = gr.Dropdown(
                choices=["Select an example..."],
                label="Load Example"
            )
            
        with gr.Column(scale=2):
            output_plot = gr.Plot(label="Generated Plot")

    # Event handlers
    def update_inputs(plot_type):
        parametric_visible = plot_type == "Parametric (x = f(t), y = g(t))"
        
        # Update example dropdown
        if plot_type in examples:
            example_choices = ["Select an example..."] + [
                f"Example {i+1}" for i in range(len(examples[plot_type]))
            ]
        else:
            example_choices = ["Select an example..."]
        
        return (
            gr.update(visible=parametric_visible),
            gr.update(choices=example_choices, value="Select an example...")
        )

    def load_example_equations(plot_type, example_choice):
        if example_choice == "Select an example...":
            return "", "", "", ""
        
        try:
            example_idx = int(example_choice.split()[-1]) - 1
            return load_example(plot_type, example_idx)
        except:
            return "", "", "", ""

    plot_type.change(
        update_inputs,
        inputs=[plot_type],
        outputs=[parametric_inputs, example_dropdown]
    )

    example_dropdown.change(
        load_example_equations,
        inputs=[plot_type, example_dropdown],
        outputs=[equation, x_equation, y_equation, custom_title]
    )

    generate_btn.click(
        generate_plot,
        inputs=[
            plot_type, equation, x_equation, y_equation,
            x_min, x_max, y_min, y_max, t_min, t_max, theta_min, theta_max,
            color, line_style, color_scheme, show_grid, custom_title
        ],
        outputs=[output_plot]
    )

    # Auto-generate on equation change
    equation.change(
        generate_plot,
        inputs=[
            plot_type, equation, x_equation, y_equation,
            x_min, x_max, y_min, y_max, t_min, t_max, theta_min, theta_max,
            color, line_style, color_scheme, show_grid, custom_title
        ],
        outputs=[output_plot]
    )

if __name__ == "__main__":
    demo.launch(share=True)