⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀         ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

51 lines
5.7 KiB

  1. ��import numpy as np
  2. import plotly.graph_objects as go
  3. from math import *
  4. import mpmath
  5. from ipywidgets import interact, widgets, Text, Layout
  6. formula = Text(value='(1 - cos(((4)/2)*x))/2', layout=Layout(width='100%'),description='�&��&A�&N�&O�&%�&�$�&���&�%�&�%�&�%�&�%�&�%�&�%�&���&�$�&%�&O�&N�&A�&��&')
  7. N_slider = widgets.IntSlider(min=1, max=16, value=8,layout=Layout(width='100%'),readout_format='.256f',description='�&%�&���&��&�$�&��&�%�&%�&���&�&�&O�&N�&N�&�%�&$'�&N�&���&�$�&km�&�%�&�%�&�%�&�%�&�%�&�%�&km�&�$�&���&N�&$'�&�%�&N�&N�&O�&�&�&���&%�&�%�&��&�$�&��&���&%�&')
  8. RANGE_FROM_SLIDER=widgets.FloatSlider(min=0, max=4*(4*atan(1)), value=0*(4*atan(1)), step=(4*atan(1))/4, layout=Layout(width='100%'),readout_format='.256f',description='�&O�&�$�&%�&���&�%�&���&df�&N�&��&%�&�%�&�%�&�%�&�%�&�%�&�%�&%�&��&N�&df�&���&�%�&���&%�&�$�&O�&')
  9. RANGE_TO_SLIDER=widgets.FloatSlider(min=0, max=4*(4*atan(1)), value=4*(4*atan(1)),step=(4*atan(1))/4, layout=Layout(width='100%'),readout_format='.256f',description='�&�$�&$'�&�%�&���&df�&N�&��&%�&�%�&�%�&�%�&�%�&�%�&�%�&%�&��&N�&df�&���&�%�&$'�&�$�&')
  10. def clamp(x):
  11. return max(min(1, x), -1)
  12. def kappa(formula, x):
  13. func_dict = {fn: eval(f'lambda *args: mpmath.{fn}(*args)') for fn in dir(mpmath)}
  14. return float(eval(formula, {'x': x, 'clamp': clamp, **func_dict}))
  15. def plot(formula='(1 - cos(((4)/2)*x))/2', RANGE_FROM=0*(4*atan(1)), RANGE_TO=4*(4*atan(1)), N=8):
  16. num_points = 1+2**N
  17. # Generate x values with the specified number of points
  18. x_vals = np.linspace(RANGE_FROM, RANGE_TO, num_points)
  19. # Compute kappa values
  20. kappa_vals = np.array([kappa(formula, x_val) for x_val in x_vals])
  21. theta_vals = np.cumsum(kappa_vals) * (x_vals[1]-x_vals[0]) if num_points > 1 else np.array([0])
  22. x_coords_ = np.cumsum(np.cos(theta_vals)) * (x_vals[1] - x_vals[0]) if num_points > 1 else np.array([0])
  23. y_coords_ = np.cumsum(np.sin(theta_vals)) * (x_vals[1] - x_vals[0]) if num_points > 1 else np.array([0])
  24. # Check if the first point is zero, if not, add it manually
  25. if x_coords_[0] != 0 or y_coords_[0] != 0:
  26. x_coords = np.insert(x_coords_, 0, 0)
  27. y_coords = np.insert(y_coords_, 0, 0)
  28. else:
  29. x_coords = x_coords_
  30. y_coords = y_coords_
  31. fig = go.Figure()
  32. fig.add_trace(go.Scatter(x=x_coords,y=y_coords,mode='lines',line=dict(color='#CECECE'),name='',hovertemplate ='X:%{x:.256f}'+'<br>Y:%{y:.256f}'))
  33. fig.update_layout(
  34. autosize=True,
  35. xaxis=dict(scaleanchor='y',scaleratio=1,gridcolor='#CECECE',zeroline=True,zerolinecolor='#CECECE',tickfont=dict(color='#9C9C9C')),
  36. yaxis=dict( gridcolor='#CECECE',zeroline=True,zerolinecolor='#CECECE',tickfont=dict(color='#9C9C9C')),
  37. hoverlabel=dict(bgcolor="#FFFFFF",font_color='#9C9C9C',bordercolor="#CECECE"),plot_bgcolor='#FFFFFF'
  38. )
  39. fig.show()
  40. # Create the interactive plot
  41. interact(plot, formula=formula, RANGE_FROM=RANGE_FROM_SLIDER, RANGE_TO=RANGE_TO_SLIDER, N=N_slider);