Spaces:
Runtime error
Runtime error
Upload plotter.py
Browse files- plotter.py +79 -0
plotter.py
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
import plotly.graph_objects as go
|
| 3 |
+
from sympy import symbols, lambdify
|
| 4 |
+
from sympy.parsing.sympy_parser import parse_expr, standard_transformations, implicit_multiplication_application
|
| 5 |
+
from solver import preprocess_equation
|
| 6 |
+
|
| 7 |
+
def plot_function(input_str):
|
| 8 |
+
"""Create an interactive plot of the function."""
|
| 9 |
+
try:
|
| 10 |
+
# Preprocess input
|
| 11 |
+
processed_input = preprocess_equation(input_str)
|
| 12 |
+
|
| 13 |
+
# Handle different types of input
|
| 14 |
+
if '=' in processed_input:
|
| 15 |
+
# Equation: move everything to left side
|
| 16 |
+
left_side, right_side = [side.strip() for side in processed_input.split('=')]
|
| 17 |
+
transformations = standard_transformations + (implicit_multiplication_application,)
|
| 18 |
+
expr = parse_expr(left_side, transformations=transformations) - parse_expr(right_side, transformations=transformations)
|
| 19 |
+
elif input_str.startswith('∫'):
|
| 20 |
+
# Integration: plot the original function
|
| 21 |
+
expr = parse_expr(processed_input[1:].strip(), transformations=(standard_transformations + (implicit_multiplication_application,)))
|
| 22 |
+
elif input_str.startswith('d/dx'):
|
| 23 |
+
# Derivative: plot the original function
|
| 24 |
+
expr = parse_expr(processed_input[4:].strip(), transformations=(standard_transformations + (implicit_multiplication_application,)))
|
| 25 |
+
else:
|
| 26 |
+
# Regular expression
|
| 27 |
+
expr = parse_expr(processed_input, transformations=(standard_transformations + (implicit_multiplication_application,)))
|
| 28 |
+
|
| 29 |
+
# Create lambda function for numpy evaluation
|
| 30 |
+
x = symbols('x')
|
| 31 |
+
f = lambdify(x, expr, 'numpy')
|
| 32 |
+
|
| 33 |
+
# Generate x values
|
| 34 |
+
x_vals = np.linspace(-10, 10, 1000)
|
| 35 |
+
|
| 36 |
+
# Calculate y values
|
| 37 |
+
y_vals = f(x_vals)
|
| 38 |
+
|
| 39 |
+
# Create plot
|
| 40 |
+
fig = go.Figure()
|
| 41 |
+
|
| 42 |
+
# Add function curve
|
| 43 |
+
fig.add_trace(go.Scatter(
|
| 44 |
+
x=x_vals,
|
| 45 |
+
y=y_vals,
|
| 46 |
+
mode='lines',
|
| 47 |
+
name='f(x)',
|
| 48 |
+
line=dict(color='#FF4B4B', width=2)
|
| 49 |
+
))
|
| 50 |
+
|
| 51 |
+
# Add x-axis line
|
| 52 |
+
fig.add_trace(go.Scatter(
|
| 53 |
+
x=x_vals,
|
| 54 |
+
y=[0]*len(x_vals),
|
| 55 |
+
mode='lines',
|
| 56 |
+
name='x-axis',
|
| 57 |
+
line=dict(color='black', width=1)
|
| 58 |
+
))
|
| 59 |
+
|
| 60 |
+
# Update layout
|
| 61 |
+
fig.update_layout(
|
| 62 |
+
title='Function Visualization',
|
| 63 |
+
xaxis_title='x',
|
| 64 |
+
yaxis_title='y',
|
| 65 |
+
showlegend=True,
|
| 66 |
+
hovermode='x',
|
| 67 |
+
plot_bgcolor='white',
|
| 68 |
+
width=800,
|
| 69 |
+
height=500
|
| 70 |
+
)
|
| 71 |
+
|
| 72 |
+
# Update axes
|
| 73 |
+
fig.update_xaxes(zeroline=True, zerolinewidth=1, zerolinecolor='black', gridcolor='lightgray')
|
| 74 |
+
fig.update_yaxes(zeroline=True, zerolinewidth=1, zerolinecolor='black', gridcolor='lightgray')
|
| 75 |
+
|
| 76 |
+
return fig
|
| 77 |
+
|
| 78 |
+
except Exception as e:
|
| 79 |
+
raise Exception(f"Error creating plot: {str(e)}")
|