-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinteractive_demo.py
More file actions
327 lines (264 loc) · 10.1 KB
/
interactive_demo.py
File metadata and controls
327 lines (264 loc) · 10.1 KB
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
#!/usr/bin/env python3
"""
Enhanced Interactive Demo for 3D Complex Number Visualization
This script demonstrates the new interactive and animation features.
"""
import numpy as np
import matplotlib.pyplot as plt
from complex_3d_visualization import ComplexNumber3DVisualizer
import plotly.graph_objects as go
from plotly.subplots import make_subplots
import plotly.offline as pyo
def create_enhanced_demo():
"""
Create an enhanced interactive demo showcasing all new features.
"""
print("🎨 Enhanced 3D Complex Number Visualization Demo")
print("=" * 60)
# Create visualizer
visualizer = ComplexNumber3DVisualizer()
# Demo 1: Interactive Widgets (Jupyter-style)
print("\n1. 🎛️ Interactive Widgets Demo")
print(" Creating interactive controls...")
# Create widgets
widgets = visualizer.create_interactive_widgets()
print(" Available controls:")
print(" - Real/Imaginary part sliders")
print(" - Range adjustment sliders")
print(" - Resolution control")
print(" - Color scheme selection")
print(" - Animation controls")
# Demo 2: Real-time Plotly Visualization
print("\n2. 🌐 Real-time Plotly Visualization")
print(" Creating interactive web visualization...")
fig = visualizer.create_real_time_plotly()
fig.write_html('enhanced_interactive_demo.html')
print(" ✅ Saved as 'enhanced_interactive_demo.html'")
# Demo 3: Animation Showcase
print("\n3. 🎬 Animation Showcase")
# Complex operations animation
print(" Creating animated complex operations...")
z1 = 1 + 1j
z2 = 2 - 1j
anim1 = visualizer.animate_complex_operations(z1, z2, frames=60, duration=4.0)
print(" ✅ Complex operations animation created")
# Phase evolution animation
print(" Creating phase evolution animation...")
anim2 = visualizer.create_phase_animation(frames=60, duration=4.0)
print(" ✅ Phase evolution animation created")
# Demo 4: Advanced Function Animations
print("\n4. 🔬 Advanced Function Animations")
# Define time-dependent complex functions
def f_rotating(z, t):
"""Rotating complex function"""
return z * np.exp(1j * t)
def f_scaling(z, t):
"""Scaling complex function"""
return z * (1 + 0.5 * np.sin(t))
def f_wave(z, t):
"""Wave-like complex function"""
return z + 0.5 * np.sin(t) * np.exp(1j * np.pi/4)
# Create animations for each function
functions = [
(f_rotating, "Rotating Function", "rotating"),
(f_scaling, "Scaling Function", "scaling"),
(f_wave, "Wave Function", "wave")
]
for func, name, filename in functions:
print(f" Creating {name} animation...")
anim = visualizer.animate_complex_function(
func, frames=60, duration=4.0,
save_path=f'animation_{filename}.gif'
)
print(f" ✅ {name} animation saved as 'animation_{filename}.gif'")
# Demo 5: Multi-panel Interactive Visualization
print("\n5. 📊 Multi-panel Interactive Visualization")
# Create a comprehensive Plotly dashboard
fig = make_subplots(
rows=2, cols=2,
specs=[[{'type': 'surface'}, {'type': 'scatter3d'}],
[{'type': 'surface'}, {'type': 'scatter3d'}]],
subplot_titles=('Magnitude Surface', 'Complex Numbers',
'Phase Surface', 'Function Visualization'),
vertical_spacing=0.1,
horizontal_spacing=0.1
)
# Add magnitude surface
real_vals = np.linspace(-3, 3, 30)
imag_vals = np.linspace(-3, 3, 30)
real_grid, imag_grid = np.meshgrid(real_vals, imag_vals)
complex_numbers = real_grid + 1j * imag_grid
magnitudes = np.abs(complex_numbers)
fig.add_trace(
go.Surface(x=real_grid, y=imag_grid, z=magnitudes,
colorscale='viridis', name='Magnitude'),
row=1, col=1
)
# Add phase surface
phases = np.angle(complex_numbers)
fig.add_trace(
go.Surface(x=real_grid, y=imag_grid, z=phases,
colorscale='hsv', name='Phase'),
row=2, col=1
)
# Add complex numbers
example_numbers = [1+1j, -1+1j, 1-1j, -1-1j, 2j, -2j, 2, -2]
colors = ['red', 'blue', 'green', 'orange', 'purple', 'brown', 'pink', 'cyan']
for i, z in enumerate(example_numbers):
fig.add_trace(
go.Scatter3d(
x=[z.real], y=[z.imag], z=[abs(z)],
mode='markers+text',
marker=dict(size=8, color=colors[i]),
text=[f'z = {z:.1f}'],
textposition='top center',
name=f'z = {z:.1f}'
),
row=1, col=2
)
# Add function visualization
def f(z): return z**2
function_values = f(complex_numbers)
function_magnitudes = np.abs(function_values)
fig.add_trace(
go.Scatter3d(
x=real_grid.flatten(),
y=imag_grid.flatten(),
z=function_magnitudes.flatten(),
mode='markers',
marker=dict(size=2, color=function_magnitudes.flatten(),
colorscale='plasma', showscale=True),
name='f(z) = z²'
),
row=2, col=2
)
# Update layout
fig.update_layout(
title='Comprehensive 3D Complex Number Dashboard',
height=800,
showlegend=True
)
# Update scene properties
for i in range(1, 3):
for j in range(1, 3):
scene_name = f'scene{i}{j}' if i > 1 or j > 1 else 'scene'
fig.update_layout(**{
scene_name: dict(
xaxis_title='Real Part',
yaxis_title='Imaginary Part',
zaxis_title='Magnitude' if j == 1 else 'Value'
)
})
fig.write_html('comprehensive_dashboard.html')
print(" ✅ Comprehensive dashboard saved as 'comprehensive_dashboard.html'")
# Demo 6: Performance Comparison
print("\n6. ⚡ Performance Comparison")
import time
# Test different resolutions
resolutions = [20, 50, 100]
times = []
for res in resolutions:
start_time = time.time()
fig, ax = visualizer.create_3d_complex_plane()
end_time = time.time()
times.append(end_time - start_time)
print(f" Resolution {res}x{res}: {end_time - start_time:.3f}s")
# Demo 7: Export Options
print("\n7. 📁 Export Options")
# Create static images
fig, ax = visualizer.create_3d_complex_plane()
plt.savefig('demo_static.png', dpi=300, bbox_inches='tight')
print(" ✅ Static image saved as 'demo_static.png'")
# Create interactive HTML
fig = visualizer.create_interactive_plotly_visualization()
fig.write_html('demo_interactive.html')
print(" ✅ Interactive HTML saved as 'demo_interactive.html'")
# Create animation
anim = visualizer.animate_complex_operations(1+1j, 2-1j, frames=30, duration=2.0)
anim.save('demo_animation.gif', writer='pillow', fps=15)
print(" ✅ Animation saved as 'demo_animation.gif'")
print("\n" + "=" * 60)
print("🎉 Enhanced Demo Complete!")
print("\nGenerated Files:")
print("📄 enhanced_interactive_demo.html - Real-time Plotly visualization")
print("📄 comprehensive_dashboard.html - Multi-panel dashboard")
print("📄 demo_interactive.html - Interactive visualization")
print("📄 demo_static.png - Static image")
print("📄 demo_animation.gif - Animation")
print("📄 animation_*.gif - Function animations")
print("\n🚀 New Features Demonstrated:")
print("✅ Real-time parameter controls")
print("✅ Interactive widgets")
print("✅ Animated visualizations")
print("✅ Multi-panel dashboards")
print("✅ Performance optimization")
print("✅ Multiple export formats")
print("✅ Advanced function animations")
return visualizer
def create_jupyter_notebook_demo():
"""
Create a Jupyter notebook demo for interactive exploration.
"""
notebook_content = """
# Enhanced 3D Complex Number Visualization - Interactive Demo
This notebook demonstrates the enhanced interactive and animation features.
## 1. Interactive Widgets
```python
from complex_3d_visualization import ComplexNumber3DVisualizer
# Create visualizer
visualizer = ComplexNumber3DVisualizer()
# Create interactive plot with real-time controls
interactive_plot = visualizer.create_interactive_plot()
display(interactive_plot)
```
## 2. Animation Examples
```python
# Animate complex operations
z1 = 1 + 1j
z2 = 2 - 1j
anim = visualizer.animate_complex_operations(z1, z2, frames=100, duration=5.0)
plt.show()
# Animate phase evolution
anim = visualizer.create_phase_animation(frames=100, duration=5.0)
plt.show()
```
## 3. Real-time Plotly Visualization
```python
# Create interactive Plotly visualization
fig = visualizer.create_real_time_plotly()
fig.show()
```
## 4. Custom Function Animations
```python
# Define time-dependent function
def f_rotating(z, t):
return z * np.exp(1j * t)
# Create animation
anim = visualizer.animate_complex_function(f_rotating, frames=100, duration=5.0)
plt.show()
```
## 5. Performance Testing
```python
import time
# Test different resolutions
resolutions = [20, 50, 100]
for res in resolutions:
start_time = time.time()
fig, ax = visualizer.create_3d_complex_plane()
end_time = time.time()
print(f"Resolution {res}x{res}: {end_time - start_time:.3f}s")
```
"""
with open('enhanced_demo_notebook.ipynb', 'w') as f:
f.write(notebook_content)
print("📓 Jupyter notebook demo created as 'enhanced_demo_notebook.ipynb'")
if __name__ == "__main__":
# Run the enhanced demo
visualizer = create_enhanced_demo()
# Create Jupyter notebook demo
create_jupyter_notebook_demo()
print("\n🎯 Next Steps:")
print("1. Open 'enhanced_interactive_demo.html' in your browser")
print("2. Run 'enhanced_demo_notebook.ipynb' in Jupyter")
print("3. Experiment with the interactive controls")
print("4. Create your own custom animations!")