euler314 commited on
Commit
1126f78
·
verified ·
1 Parent(s): 359367f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +112 -1370
app.py CHANGED
@@ -1,1392 +1,134 @@
1
  import streamlit as st
2
- import sympy as sp
3
- import numpy as np
4
- import plotly.graph_objects as go
5
- from scipy.optimize import fsolve
6
- from scipy.stats import gaussian_kde
7
 
8
- # Configure Streamlit for Hugging Face Spaces
9
  st.set_page_config(
10
- page_title="Cubic Root Analysis",
11
- layout="wide",
12
- initial_sidebar_state="collapsed"
13
  )
14
 
15
- def add_sqrt_support(expr_str):
16
- """Replace 'sqrt(' with 'sp.sqrt(' for sympy compatibility"""
17
- return expr_str.replace('sqrt(', 'sp.sqrt(')
18
-
19
- #############################
20
- # 1) Define the discriminant
21
- #############################
22
-
23
- # Symbolic variables for the cubic discriminant
24
- z_sym, beta_sym, z_a_sym, y_sym = sp.symbols("z beta z_a y", real=True, positive=True)
25
-
26
- # Define coefficients a, b, c, d in terms of z_sym, beta_sym, z_a_sym, y_sym
27
- a_sym = z_sym * z_a_sym
28
- b_sym = z_sym * z_a_sym + z_sym + z_a_sym - z_a_sym*y_sym
29
- c_sym = z_sym + z_a_sym + 1 - y_sym*(beta_sym*z_a_sym + 1 - beta_sym)
30
- d_sym = 1
31
-
32
- # Symbolic expression for the cubic discriminant
33
- Delta_expr = (
34
- ((b_sym*c_sym)/(6*a_sym**2) - (b_sym**3)/(27*a_sym**3) - d_sym/(2*a_sym))**2
35
- + (c_sym/(3*a_sym) - (b_sym**2)/(9*a_sym**2))**3
36
- )
37
-
38
- # Fast numeric function for the discriminant
39
- discriminant_func = sp.lambdify((z_sym, beta_sym, z_a_sym, y_sym), Delta_expr, "numpy")
40
-
41
- @st.cache_data
42
- def find_z_at_discriminant_zero(z_a, y, beta, z_min, z_max, steps):
43
- """
44
- Scan z in [z_min, z_max] for sign changes in the discriminant,
45
- and return approximated roots (where the discriminant is zero).
46
- """
47
- # Apply the condition for y
48
- y_effective = y if y > 1 else 1/y
49
-
50
- z_grid = np.linspace(z_min, z_max, steps)
51
- disc_vals = discriminant_func(z_grid, beta, z_a, y_effective)
52
- roots_found = []
53
- for i in range(len(z_grid) - 1):
54
- f1, f2 = disc_vals[i], disc_vals[i+1]
55
- if np.isnan(f1) or np.isnan(f2):
56
- continue
57
- if f1 == 0.0:
58
- roots_found.append(z_grid[i])
59
- elif f2 == 0.0:
60
- roots_found.append(z_grid[i+1])
61
- elif f1 * f2 < 0:
62
- zl, zr = z_grid[i], z_grid[i+1]
63
- for _ in range(50):
64
- mid = 0.5 * (zl + zr)
65
- fm = discriminant_func(mid, beta, z_a, y_effective)
66
- if fm == 0:
67
- zl = zr = mid
68
- break
69
- if np.sign(fm) == np.sign(f1):
70
- zl, f1 = mid, fm
71
- else:
72
- zr, f2 = mid, fm
73
- roots_found.append(0.5 * (zl + zr))
74
- return np.array(roots_found)
75
-
76
- @st.cache_data
77
- def sweep_beta_and_find_z_bounds(z_a, y, z_min, z_max, beta_steps, z_steps):
78
- """
79
- For each beta in [0,1] (with beta_steps points), find the minimum and maximum z
80
- for which the discriminant is zero.
81
- Returns: betas, lower z*(β) values, and upper z*(β) values.
82
- """
83
- betas = np.linspace(0, 1, beta_steps)
84
- z_min_values = []
85
- z_max_values = []
86
- for b in betas:
87
- roots = find_z_at_discriminant_zero(z_a, y, b, z_min, z_max, z_steps)
88
- if len(roots) == 0:
89
- z_min_values.append(np.nan)
90
- z_max_values.append(np.nan)
91
- else:
92
- z_min_values.append(np.min(roots))
93
- z_max_values.append(np.max(roots))
94
- return betas, np.array(z_min_values), np.array(z_max_values)
95
-
96
- @st.cache_data
97
- def compute_eigenvalue_support_boundaries(z_a, y, beta_values, n_samples=100, seeds=5):
98
- """
99
- Compute the support boundaries of the eigenvalue distribution by directly
100
- finding the minimum and maximum eigenvalues of B_n = S_n T_n for different beta values.
101
- """
102
- # Apply the condition for y
103
- y_effective = y if y > 1 else 1/y
104
-
105
- min_eigenvalues = np.zeros_like(beta_values)
106
- max_eigenvalues = np.zeros_like(beta_values)
107
-
108
- # Use a progress bar for Streamlit
109
- progress_bar = st.progress(0)
110
- status_text = st.empty()
111
-
112
- for i, beta in enumerate(beta_values):
113
- # Update progress
114
- progress_bar.progress((i + 1) / len(beta_values))
115
- status_text.text(f"Processing β = {beta:.2f} ({i+1}/{len(beta_values)})")
116
-
117
- min_vals = []
118
- max_vals = []
119
-
120
- # Run multiple trials with different seeds for more stable results
121
- for seed in range(seeds):
122
- # Set random seed
123
- np.random.seed(seed * 100 + i)
124
-
125
- # Compute dimension p based on aspect ratio y
126
- n = n_samples
127
- p = int(y_effective * n)
128
-
129
- # Constructing T_n (Population / Shape Matrix)
130
- k = int(np.floor(beta * p))
131
- diag_entries = np.concatenate([
132
- np.full(k, z_a),
133
- np.full(p - k, 1.0)
134
- ])
135
- np.random.shuffle(diag_entries)
136
- T_n = np.diag(diag_entries)
137
-
138
- # Generate the data matrix X with i.i.d. standard normal entries
139
- X = np.random.randn(p, n)
140
-
141
- # Compute the sample covariance matrix S_n = (1/n) * XX^T
142
- S_n = (1 / n) * (X @ X.T)
143
 
144
- # Compute B_n = S_n T_n
145
- B_n = S_n @ T_n
146
 
147
- # Compute eigenvalues of B_n
148
- eigenvalues = np.linalg.eigvalsh(B_n)
 
 
 
 
149
 
150
- # Find minimum and maximum eigenvalues
151
- min_vals.append(np.min(eigenvalues))
152
- max_vals.append(np.max(eigenvalues))
153
-
154
- # Average over seeds for stability
155
- min_eigenvalues[i] = np.mean(min_vals)
156
- max_eigenvalues[i] = np.mean(max_vals)
157
-
158
- # Clear progress indicators
159
- progress_bar.empty()
160
- status_text.empty()
161
-
162
- return min_eigenvalues, max_eigenvalues
163
-
164
- @st.cache_data
165
- def compute_high_y_curve(betas, z_a, y):
166
- """
167
- Compute the "High y Expression" curve.
168
- """
169
- # Apply the condition for y
170
- y_effective = y if y > 1 else 1/y
171
-
172
- a = z_a
173
- betas = np.array(betas)
174
- denominator = 1 - 2*a
175
- if denominator == 0:
176
- return np.full_like(betas, np.nan)
177
- numerator = -4*a*(a-1)*y_effective*betas - 2*a*y_effective - 2*a*(2*a-1)
178
- return numerator/denominator
179
-
180
- @st.cache_data
181
- def compute_alternate_low_expr(betas, z_a, y):
182
- """
183
- Compute the alternate low expression:
184
- (z_a*y*beta*(z_a-1) - 2*z_a*(1-y) - 2*z_a**2) / (2+2*z_a)
185
- """
186
- # Apply the condition for y
187
- y_effective = y if y > 1 else 1/y
188
-
189
- betas = np.array(betas)
190
- return (z_a * y_effective * betas * (z_a - 1) - 2*z_a*(1 - y_effective) - 2*z_a**2) / (2 + 2*z_a)
191
-
192
- @st.cache_data
193
- def compute_max_k_expression(betas, z_a, y, k_samples=1000):
194
- """
195
- Compute max_{k ∈ (0,∞)} (y*beta*(a-1)*k + (a*k+1)*((y-1)*k-1)) / ((a*k+1)*(k^2+k))
196
- """
197
- # Apply the condition for y
198
- y_effective = y if y > 1 else 1/y
199
-
200
- a = z_a
201
- # Sample k values on a logarithmic scale
202
- k_values = np.logspace(-3, 3, k_samples)
203
-
204
- max_vals = np.zeros_like(betas)
205
- for i, beta in enumerate(betas):
206
- values = np.zeros_like(k_values)
207
- for j, k in enumerate(k_values):
208
- numerator = y_effective*beta*(a-1)*k + (a*k+1)*((y_effective-1)*k-1)
209
- denominator = (a*k+1)*(k**2+k)
210
- if abs(denominator) < 1e-10:
211
- values[j] = np.nan
212
- else:
213
- values[j] = numerator/denominator
214
-
215
- valid_indices = ~np.isnan(values)
216
- if np.any(valid_indices):
217
- max_vals[i] = np.max(values[valid_indices])
218
- else:
219
- max_vals[i] = np.nan
220
 
221
- return max_vals
222
-
223
- @st.cache_data
224
- def compute_min_t_expression(betas, z_a, y, t_samples=1000):
225
- """
226
- Compute min_{t ∈ (-1/a, 0)} (y*beta*(a-1)*t + (a*t+1)*((y-1)*t-1)) / ((a*t+1)*(t^2+t))
227
- """
228
- # Apply the condition for y
229
- y_effective = y if y > 1 else 1/y
230
-
231
- a = z_a
232
- if a <= 0:
233
- return np.full_like(betas, np.nan)
234
-
235
- lower_bound = -1/a + 1e-10 # Avoid division by zero
236
- t_values = np.linspace(lower_bound, -1e-10, t_samples)
237
-
238
- min_vals = np.zeros_like(betas)
239
- for i, beta in enumerate(betas):
240
- values = np.zeros_like(t_values)
241
- for j, t in enumerate(t_values):
242
- numerator = y_effective*beta*(a-1)*t + (a*t+1)*((y_effective-1)*t-1)
243
- denominator = (a*t+1)*(t**2+t)
244
- if abs(denominator) < 1e-10:
245
- values[j] = np.nan
246
  else:
247
- values[j] = numerator/denominator
248
-
249
- valid_indices = ~np.isnan(values)
250
- if np.any(valid_indices):
251
- min_vals[i] = np.min(values[valid_indices])
252
- else:
253
- min_vals[i] = np.nan
254
-
255
- return min_vals
256
-
257
- @st.cache_data
258
- def compute_derivatives(curve, betas):
259
- """Compute first and second derivatives of a curve"""
260
- d1 = np.gradient(curve, betas)
261
- d2 = np.gradient(d1, betas)
262
- return d1, d2
263
-
264
- def compute_all_derivatives(betas, z_mins, z_maxs, low_y_curve, high_y_curve, alt_low_expr, custom_curve1=None, custom_curve2=None):
265
- """Compute derivatives for all curves"""
266
- derivatives = {}
267
-
268
- # Upper z*(β)
269
- derivatives['upper'] = compute_derivatives(z_maxs, betas)
270
-
271
- # Lower z*(β)
272
- derivatives['lower'] = compute_derivatives(z_mins, betas)
273
-
274
- # Low y Expression (only if provided)
275
- if low_y_curve is not None:
276
- derivatives['low_y'] = compute_derivatives(low_y_curve, betas)
277
-
278
- # High y Expression
279
- if high_y_curve is not None:
280
- derivatives['high_y'] = compute_derivatives(high_y_curve, betas)
281
-
282
- # Alternate Low Expression
283
- if alt_low_expr is not None:
284
- derivatives['alt_low'] = compute_derivatives(alt_low_expr, betas)
285
-
286
- # Custom Expression 1 (if provided)
287
- if custom_curve1 is not None:
288
- derivatives['custom1'] = compute_derivatives(custom_curve1, betas)
289
-
290
- # Custom Expression 2 (if provided)
291
- if custom_curve2 is not None:
292
- derivatives['custom2'] = compute_derivatives(custom_curve2, betas)
293
-
294
- return derivatives
295
-
296
- def compute_custom_expression(betas, z_a, y, s_num_expr, s_denom_expr, is_s_based=True):
297
- """
298
- Compute custom curve. If is_s_based=True, compute using s substitution.
299
- Otherwise, compute direct z(β) expression.
300
- """
301
- # Apply the condition for y
302
- y_effective = y if y > 1 else 1/y
303
-
304
- beta_sym, z_a_sym, y_sym = sp.symbols("beta z_a y", positive=True)
305
- local_dict = {"beta": beta_sym, "z_a": z_a_sym, "y": y_sym, "sp": sp}
306
-
307
- try:
308
- # Add sqrt support
309
- s_num_expr = add_sqrt_support(s_num_expr)
310
- s_denom_expr = add_sqrt_support(s_denom_expr)
311
-
312
- num_expr = sp.sympify(s_num_expr, locals=local_dict)
313
- denom_expr = sp.sympify(s_denom_expr, locals=local_dict)
314
-
315
- if is_s_based:
316
- # Compute s and substitute into main expression
317
- s_expr = num_expr / denom_expr
318
- a = z_a_sym
319
- numerator = y_sym*beta_sym*(z_a_sym-1)*s_expr + (a*s_expr+1)*((y_sym-1)*s_expr-1)
320
- denominator = (a*s_expr+1)*(s_expr**2 + s_expr)
321
- final_expr = numerator/denominator
322
- else:
323
- # Direct z(β) expression
324
- final_expr = num_expr / denom_expr
325
-
326
- except sp.SympifyError as e:
327
- st.error(f"Error parsing expressions: {e}")
328
- return np.full_like(betas, np.nan)
329
-
330
- final_func = sp.lambdify((beta_sym, z_a_sym, y_sym), final_expr, modules=["numpy"])
331
- with np.errstate(divide='ignore', invalid='ignore'):
332
- result = final_func(betas, z_a, y_effective)
333
- if np.isscalar(result):
334
- result = np.full_like(betas, result)
335
- return result
336
-
337
- def generate_z_vs_beta_plot(z_a, y, z_min, z_max, beta_steps, z_steps,
338
- s_num_expr=None, s_denom_expr=None,
339
- z_num_expr=None, z_denom_expr=None,
340
- show_derivatives=False,
341
- show_high_y=False,
342
- show_low_y=False,
343
- show_max_k=True,
344
- show_min_t=True,
345
- use_eigenvalue_method=True,
346
- n_samples=1000,
347
- seeds=5):
348
- if z_a <= 0 or y <= 0 or z_min >= z_max:
349
- st.error("Invalid input parameters.")
350
- return None
351
-
352
- betas = np.linspace(0, 1, beta_steps)
353
-
354
- if use_eigenvalue_method:
355
- # Use the eigenvalue method to compute boundaries
356
- st.info("Computing eigenvalue support boundaries. This may take a moment...")
357
- min_eigs, max_eigs = compute_eigenvalue_support_boundaries(z_a, y, betas, n_samples, seeds)
358
- z_mins, z_maxs = min_eigs, max_eigs
359
- else:
360
- # Use the original discriminant method
361
- betas, z_mins, z_maxs = sweep_beta_and_find_z_bounds(z_a, y, z_min, z_max, beta_steps, z_steps)
362
-
363
- high_y_curve = compute_high_y_curve(betas, z_a, y) if show_high_y else None
364
- alt_low_expr = compute_alternate_low_expr(betas, z_a, y) if show_low_y else None
365
-
366
- # Compute the max/min expressions
367
- max_k_curve = compute_max_k_expression(betas, z_a, y) if show_max_k else None
368
- min_t_curve = compute_min_t_expression(betas, z_a, y) if show_min_t else None
369
-
370
- # Compute both custom curves
371
- custom_curve1 = None
372
- custom_curve2 = None
373
- if s_num_expr and s_denom_expr:
374
- custom_curve1 = compute_custom_expression(betas, z_a, y, s_num_expr, s_denom_expr, True)
375
- if z_num_expr and z_denom_expr:
376
- custom_curve2 = compute_custom_expression(betas, z_a, y, z_num_expr, z_denom_expr, False)
377
-
378
- # Compute derivatives if needed
379
- if show_derivatives:
380
- derivatives = compute_all_derivatives(betas, z_mins, z_maxs, None, high_y_curve,
381
- alt_low_expr, custom_curve1, custom_curve2)
382
- # Calculate derivatives for max_k and min_t curves if they exist
383
- if show_max_k:
384
- max_k_derivatives = compute_derivatives(max_k_curve, betas)
385
- if show_min_t:
386
- min_t_derivatives = compute_derivatives(min_t_curve, betas)
387
-
388
- fig = go.Figure()
389
-
390
- # Original curves
391
- if use_eigenvalue_method:
392
- fig.add_trace(go.Scatter(x=betas, y=z_maxs, mode="markers+lines",
393
- name="Upper Bound (Max Eigenvalue)", line=dict(color='blue')))
394
- fig.add_trace(go.Scatter(x=betas, y=z_mins, mode="markers+lines",
395
- name="Lower Bound (Min Eigenvalue)", line=dict(color='blue')))
396
- # Add shaded region between curves
397
- fig.add_trace(go.Scatter(
398
- x=np.concatenate([betas, betas[::-1]]),
399
- y=np.concatenate([z_maxs, z_mins[::-1]]),
400
- fill='toself',
401
- fillcolor='rgba(0,0,255,0.2)',
402
- line=dict(color='rgba(255,255,255,0)'),
403
- showlegend=False,
404
- hoverinfo='skip'
405
- ))
406
- else:
407
- fig.add_trace(go.Scatter(x=betas, y=z_maxs, mode="markers+lines",
408
- name="Upper z*(β)", line=dict(color='blue')))
409
- fig.add_trace(go.Scatter(x=betas, y=z_mins, mode="markers+lines",
410
- name="Lower z*(β)", line=dict(color='blue')))
411
-
412
- # Add High y Expression only if selected
413
- if show_high_y and high_y_curve is not None:
414
- fig.add_trace(go.Scatter(x=betas, y=high_y_curve, mode="markers+lines",
415
- name="High y Expression", line=dict(color='green')))
416
-
417
- # Add Low Expression only if selected
418
- if show_low_y and alt_low_expr is not None:
419
- fig.add_trace(go.Scatter(x=betas, y=alt_low_expr, mode="markers+lines",
420
- name="Low Expression", line=dict(color='orange')))
421
-
422
- # Add the max/min curves if selected
423
- if show_max_k and max_k_curve is not None:
424
- fig.add_trace(go.Scatter(x=betas, y=max_k_curve, mode="lines",
425
- name="Max k Expression", line=dict(color='red', width=2)))
426
-
427
- if show_min_t and min_t_curve is not None:
428
- fig.add_trace(go.Scatter(x=betas, y=min_t_curve, mode="lines",
429
- name="Min t Expression", line=dict(color='purple', width=2)))
430
-
431
- if custom_curve1 is not None:
432
- fig.add_trace(go.Scatter(x=betas, y=custom_curve1, mode="markers+lines",
433
- name="Custom 1 (s-based)", line=dict(color='magenta')))
434
- if custom_curve2 is not None:
435
- fig.add_trace(go.Scatter(x=betas, y=custom_curve2, mode="markers+lines",
436
- name="Custom 2 (direct)", line=dict(color='brown')))
437
-
438
- if show_derivatives:
439
- # First derivatives
440
- curve_info = [
441
- ('upper', 'Upper Bound' if use_eigenvalue_method else 'Upper z*(β)', 'blue'),
442
- ('lower', 'Lower Bound' if use_eigenvalue_method else 'Lower z*(β)', 'lightblue'),
443
- ]
444
-
445
- if show_high_y and high_y_curve is not None:
446
- curve_info.append(('high_y', 'High y', 'green'))
447
- if show_low_y and alt_low_expr is not None:
448
- curve_info.append(('alt_low', 'Alt Low', 'orange'))
449
-
450
- if custom_curve1 is not None:
451
- curve_info.append(('custom1', 'Custom 1', 'magenta'))
452
- if custom_curve2 is not None:
453
- curve_info.append(('custom2', 'Custom 2', 'brown'))
454
-
455
- for key, name, color in curve_info:
456
- if key in derivatives:
457
- fig.add_trace(go.Scatter(x=betas, y=derivatives[key][0], mode="lines",
458
- name=f"{name} d/dβ", line=dict(color=color, dash='dash')))
459
- fig.add_trace(go.Scatter(x=betas, y=derivatives[key][1], mode="lines",
460
- name=f"{name} d²/dβ²", line=dict(color=color, dash='dot')))
461
-
462
- # Add derivatives for max_k and min_t curves if they exist
463
- if show_max_k and max_k_curve is not None:
464
- fig.add_trace(go.Scatter(x=betas, y=max_k_derivatives[0], mode="lines",
465
- name="Max k d/dβ", line=dict(color='red', dash='dash')))
466
- fig.add_trace(go.Scatter(x=betas, y=max_k_derivatives[1], mode="lines",
467
- name="Max k d²/dβ²", line=dict(color='red', dash='dot')))
468
-
469
- if show_min_t and min_t_curve is not None:
470
- fig.add_trace(go.Scatter(x=betas, y=min_t_derivatives[0], mode="lines",
471
- name="Min t d/dβ", line=dict(color='purple', dash='dash')))
472
- fig.add_trace(go.Scatter(x=betas, y=min_t_derivatives[1], mode="lines",
473
- name="Min t d²/dβ²", line=dict(color='purple', dash='dot')))
474
-
475
- fig.update_layout(
476
- title="Curves vs β: Eigenvalue Support Boundaries and Asymptotic Expressions" if use_eigenvalue_method
477
- else "Curves vs β: z*(β) Boundaries and Asymptotic Expressions",
478
- xaxis_title="β",
479
- yaxis_title="Value",
480
- hovermode="x unified",
481
- showlegend=True,
482
- legend=dict(
483
- yanchor="top",
484
- y=0.99,
485
- xanchor="left",
486
- x=0.01
487
- )
488
- )
489
- return fig
490
-
491
- def compute_cubic_roots(z, beta, z_a, y):
492
- """
493
- Compute the roots of the cubic equation for given parameters using SymPy for maximum accuracy.
494
- """
495
- # Apply the condition for y
496
- y_effective = y if y > 1 else 1/y
497
-
498
- # Import SymPy functions
499
- from sympy import symbols, solve, im, re, N, Poly
500
-
501
- # Create a symbolic variable for the equation
502
- s = symbols('s')
503
-
504
- # Coefficients in the form as^3 + bs^2 + cs + d = 0
505
- a = z * z_a
506
- b = z * z_a + z + z_a - z_a*y_effective
507
- c = z + z_a + 1 - y_effective*(beta*z_a + 1 - beta)
508
- d = 1
509
-
510
- # Handle special cases
511
- if abs(a) < 1e-10:
512
- if abs(b) < 1e-10: # Linear case
513
- roots = np.array([-d/c, 0, 0], dtype=complex)
514
- else: # Quadratic case
515
- quad_roots = np.roots([b, c, d])
516
- roots = np.append(quad_roots, 0).astype(complex)
517
- return roots
518
-
519
- try:
520
- # Create the cubic polynomial
521
- cubic_eq = Poly(a*s**3 + b*s**2 + c*s + d, s)
522
-
523
- # Solve the equation symbolically
524
- symbolic_roots = solve(cubic_eq, s)
525
-
526
- # Convert symbolic roots to complex numbers with high precision
527
- numerical_roots = []
528
- for root in symbolic_roots:
529
- # Use SymPy's N function with high precision
530
- numerical_root = complex(N(root, 30))
531
- numerical_roots.append(numerical_root)
532
-
533
- # If we got fewer than 3 roots (due to multiplicity), pad with zeros
534
- while len(numerical_roots) < 3:
535
- numerical_roots.append(0j)
536
-
537
- return np.array(numerical_roots, dtype=complex)
538
-
539
- except Exception as e:
540
- # Fallback to numpy if SymPy has issues
541
- coeffs = [a, b, c, d]
542
- return np.roots(coeffs)
543
-
544
- def track_roots_consistently(z_values, all_roots):
545
- """
546
- Ensure consistent tracking of roots across z values by minimizing discontinuity.
547
- """
548
- n_points = len(z_values)
549
- n_roots = all_roots[0].shape[0]
550
- tracked_roots = np.zeros((n_points, n_roots), dtype=complex)
551
- tracked_roots[0] = all_roots[0]
552
-
553
- for i in range(1, n_points):
554
- prev_roots = tracked_roots[i-1]
555
- current_roots = all_roots[i]
556
-
557
- # For each previous root, find the closest current root
558
- assigned = np.zeros(n_roots, dtype=bool)
559
- assignments = np.zeros(n_roots, dtype=int)
560
-
561
- for j in range(n_roots):
562
- distances = np.abs(current_roots - prev_roots[j])
563
-
564
- # Find the closest unassigned root
565
- while True:
566
- best_idx = np.argmin(distances)
567
- if not assigned[best_idx]:
568
- assignments[j] = best_idx
569
- assigned[best_idx] = True
570
- break
571
- else:
572
- # Mark as infinite distance and try again
573
- distances[best_idx] = np.inf
574
 
575
- # Safety check if all are assigned (shouldn't happen)
576
- if np.all(distances == np.inf):
577
- assignments[j] = j # Default to same index
578
- break
579
-
580
- # Reorder current roots based on assignments
581
- tracked_roots[i] = current_roots[assignments]
582
-
583
- return tracked_roots
584
-
585
- def generate_cubic_discriminant(z, beta, z_a, y_effective):
586
- """
587
- Calculate the cubic discriminant using the standard formula.
588
- For a cubic ax^3 + bx^2 + cx + d:
589
- Δ = 18abcd - 27a^2d^2 + b^2c^2 - 2b^3d - 9ac^3
590
- """
591
- a = z * z_a
592
- b = z * z_a + z + z_a - z_a*y_effective
593
- c = z + z_a + 1 - y_effective*(beta*z_a + 1 - beta)
594
- d = 1
595
-
596
- # Standard formula for cubic discriminant
597
- discriminant = (18*a*b*c*d - 27*a**2*d**2 + b**2*c**2 - 2*b**3*d - 9*a*c**3)
598
- return discriminant
599
-
600
- def generate_root_plots(beta, y, z_a, z_min, z_max, n_points):
601
- """
602
- Generate Im(s) and Re(s) vs. z plots with improved accuracy using SymPy.
603
- """
604
- if z_a <= 0 or y <= 0 or z_min >= z_max:
605
- st.error("Invalid input parameters.")
606
- return None, None, None
607
-
608
- # Apply the condition for y
609
- y_effective = y if y > 1 else 1/y
610
-
611
- z_points = np.linspace(z_min, z_max, n_points)
612
-
613
- # Collect all roots first
614
- all_roots = []
615
- discriminants = []
616
-
617
- # Progress indicator
618
- progress_bar = st.progress(0)
619
- status_text = st.empty()
620
-
621
- for i, z in enumerate(z_points):
622
- # Update progress
623
- progress_bar.progress((i + 1) / n_points)
624
- status_text.text(f"Computing roots for z = {z:.3f} ({i+1}/{n_points})")
625
-
626
- # Calculate roots using SymPy
627
- roots = compute_cubic_roots(z, beta, z_a, y)
628
-
629
- # Initial sorting to help with tracking
630
- roots = sorted(roots, key=lambda x: (abs(x.imag), x.real))
631
- all_roots.append(roots)
632
-
633
- # Calculate discriminant
634
- disc = generate_cubic_discriminant(z, beta, z_a, y_effective)
635
- discriminants.append(disc)
636
-
637
- # Clear progress indicators
638
- progress_bar.empty()
639
- status_text.empty()
640
-
641
- all_roots = np.array(all_roots)
642
- discriminants = np.array(discriminants)
643
-
644
- # Track roots consistently across z values
645
- tracked_roots = track_roots_consistently(z_points, all_roots)
646
-
647
- # Extract imaginary and real parts
648
- ims = np.imag(tracked_roots)
649
- res = np.real(tracked_roots)
650
-
651
- # Create figure for imaginary parts
652
- fig_im = go.Figure()
653
- for i in range(3):
654
- fig_im.add_trace(go.Scatter(x=z_points, y=ims[:, i], mode="lines", name=f"Im{{s{i+1}}}",
655
- line=dict(width=2)))
656
-
657
- # Add vertical lines at discriminant zero crossings
658
- disc_zeros = []
659
- for i in range(len(discriminants)-1):
660
- if discriminants[i] * discriminants[i+1] <= 0: # Sign change
661
- zero_pos = z_points[i] + (z_points[i+1] - z_points[i]) * (0 - discriminants[i]) / (discriminants[i+1] - discriminants[i])
662
- disc_zeros.append(zero_pos)
663
- fig_im.add_vline(x=zero_pos, line=dict(color="red", width=1, dash="dash"))
664
-
665
- fig_im.update_layout(title=f"Im{{s}} vs. z (β={beta:.3f}, y={y:.3f}, z_a={z_a:.3f})",
666
- xaxis_title="z", yaxis_title="Im{s}", hovermode="x unified")
667
-
668
- # Create figure for real parts
669
- fig_re = go.Figure()
670
- for i in range(3):
671
- fig_re.add_trace(go.Scatter(x=z_points, y=res[:, i], mode="lines", name=f"Re{{s{i+1}}}",
672
- line=dict(width=2)))
673
-
674
- # Add vertical lines at discriminant zero crossings
675
- for zero_pos in disc_zeros:
676
- fig_re.add_vline(x=zero_pos, line=dict(color="red", width=1, dash="dash"))
677
-
678
- fig_re.update_layout(title=f"Re{{s}} vs. z (β={beta:.3f}, y={y:.3f}, z_a={z_a:.3f})",
679
- xaxis_title="z", yaxis_title="Re{s}", hovermode="x unified")
680
-
681
- # Create discriminant plot
682
- fig_disc = go.Figure()
683
- fig_disc.add_trace(go.Scatter(x=z_points, y=discriminants, mode="lines",
684
- name="Cubic Discriminant", line=dict(color="black", width=2)))
685
- fig_disc.add_hline(y=0, line=dict(color="red", width=1, dash="dash"))
686
-
687
- fig_disc.update_layout(title=f"Cubic Discriminant vs. z (β={beta:.3f}, y={y:.3f}, z_a={z_a:.3f})",
688
- xaxis_title="z", yaxis_title="Discriminant", hovermode="x unified")
689
-
690
- return fig_im, fig_re, fig_disc
691
-
692
- def analyze_complex_root_structure(beta_values, z, z_a, y):
693
- """
694
- Analyze when the cubic equation switches between having all real roots
695
- and having a complex conjugate pair plus one real root.
696
-
697
- Returns:
698
- - transition_points: beta values where the root structure changes
699
- - structure_types: list indicating whether each interval has all real roots or complex roots
700
- """
701
- # Apply the condition for y
702
- y_effective = y if y > 1 else 1/y
703
-
704
- transition_points = []
705
- structure_types = []
706
-
707
- previous_type = None
708
-
709
- for beta in beta_values:
710
- roots = compute_cubic_roots(z, beta, z_a, y)
711
-
712
- # Check if all roots are real (imaginary parts close to zero)
713
- is_all_real = all(abs(root.imag) < 1e-10 for root in roots)
714
-
715
- current_type = "real" if is_all_real else "complex"
716
-
717
- if previous_type is not None and current_type != previous_type:
718
- # Found a transition point
719
- transition_points.append(beta)
720
- structure_types.append(previous_type)
721
-
722
- previous_type = current_type
723
-
724
- # Add the final interval type
725
- if previous_type is not None:
726
- structure_types.append(previous_type)
727
-
728
- return transition_points, structure_types
729
-
730
- def generate_roots_vs_beta_plots(z, y, z_a, beta_min, beta_max, n_points):
731
- """
732
- Generate Im(s) and Re(s) vs. β plots with improved accuracy using SymPy.
733
- """
734
- if z_a <= 0 or y <= 0 or beta_min >= beta_max:
735
- st.error("Invalid input parameters.")
736
- return None, None, None
737
-
738
- # Apply the condition for y
739
- y_effective = y if y > 1 else 1/y
740
-
741
- beta_points = np.linspace(beta_min, beta_max, n_points)
742
-
743
- # Collect all roots first
744
- all_roots = []
745
- discriminants = []
746
-
747
- # Progress indicator
748
- progress_bar = st.progress(0)
749
- status_text = st.empty()
750
-
751
- for i, beta in enumerate(beta_points):
752
- # Update progress
753
- progress_bar.progress((i + 1) / n_points)
754
- status_text.text(f"Computing roots for β = {beta:.3f} ({i+1}/{n_points})")
755
-
756
- # Calculate roots using SymPy
757
- roots = compute_cubic_roots(z, beta, z_a, y)
758
-
759
- # Initial sorting to help with tracking
760
- roots = sorted(roots, key=lambda x: (abs(x.imag), x.real))
761
- all_roots.append(roots)
762
-
763
- # Calculate discriminant
764
- disc = generate_cubic_discriminant(z, beta, z_a, y_effective)
765
- discriminants.append(disc)
766
-
767
- # Clear progress indicators
768
- progress_bar.empty()
769
- status_text.empty()
770
-
771
- all_roots = np.array(all_roots)
772
- discriminants = np.array(discriminants)
773
-
774
- # Track roots consistently across beta values
775
- tracked_roots = track_roots_consistently(beta_points, all_roots)
776
-
777
- # Extract imaginary and real parts
778
- ims = np.imag(tracked_roots)
779
- res = np.real(tracked_roots)
780
-
781
- # Create figure for imaginary parts
782
- fig_im = go.Figure()
783
- for i in range(3):
784
- fig_im.add_trace(go.Scatter(x=beta_points, y=ims[:, i], mode="lines", name=f"Im{{s{i+1}}}",
785
- line=dict(width=2)))
786
-
787
- # Add vertical lines at discriminant zero crossings
788
- disc_zeros = []
789
- for i in range(len(discriminants)-1):
790
- if discriminants[i] * discriminants[i+1] <= 0: # Sign change
791
- zero_pos = beta_points[i] + (beta_points[i+1] - beta_points[i]) * (0 - discriminants[i]) / (discriminants[i+1] - discriminants[i])
792
- disc_zeros.append(zero_pos)
793
- fig_im.add_vline(x=zero_pos, line=dict(color="red", width=1, dash="dash"))
794
-
795
- fig_im.update_layout(title=f"Im{{s}} vs. β (z={z:.3f}, y={y:.3f}, z_a={z_a:.3f})",
796
- xaxis_title="β", yaxis_title="Im{s}", hovermode="x unified")
797
-
798
- # Create figure for real parts
799
- fig_re = go.Figure()
800
- for i in range(3):
801
- fig_re.add_trace(go.Scatter(x=beta_points, y=res[:, i], mode="lines", name=f"Re{{s{i+1}}}",
802
- line=dict(width=2)))
803
-
804
- # Add vertical lines at discriminant zero crossings
805
- for zero_pos in disc_zeros:
806
- fig_re.add_vline(x=zero_pos, line=dict(color="red", width=1, dash="dash"))
807
-
808
- fig_re.update_layout(title=f"Re{{s}} vs. β (z={z:.3f}, y={y:.3f}, z_a={z_a:.3f})",
809
- xaxis_title="β", yaxis_title="Re{s}", hovermode="x unified")
810
-
811
- # Create discriminant plot
812
- fig_disc = go.Figure()
813
- fig_disc.add_trace(go.Scatter(x=beta_points, y=discriminants, mode="lines",
814
- name="Cubic Discriminant", line=dict(color="black", width=2)))
815
- fig_disc.add_hline(y=0, line=dict(color="red", width=1, dash="dash"))
816
-
817
- fig_disc.update_layout(title=f"Cubic Discriminant vs. β (z={z:.3f}, y={y:.3f}, z_a={z_a:.3f})",
818
- xaxis_title="β", yaxis_title="Discriminant", hovermode="x unified")
819
-
820
- return fig_im, fig_re, fig_disc
821
-
822
- def generate_phase_diagram(z_a, y, beta_min=0.0, beta_max=1.0, z_min=-10.0, z_max=10.0,
823
- beta_steps=100, z_steps=100):
824
- """
825
- Generate a phase diagram showing regions of complex and real roots.
826
-
827
- Returns a heatmap where:
828
- - Value 1 (red): Region with all real roots
829
- - Value -1 (blue): Region with complex roots
830
- """
831
- # Apply the condition for y
832
- y_effective = y if y > 1 else 1/y
833
-
834
- beta_values = np.linspace(beta_min, beta_max, beta_steps)
835
- z_values = np.linspace(z_min, z_max, z_steps)
836
-
837
- # Initialize phase map
838
- phase_map = np.zeros((z_steps, beta_steps))
839
-
840
- # Progress tracking
841
- progress_bar = st.progress(0)
842
- status_text = st.empty()
843
-
844
- for i, z in enumerate(z_values):
845
- # Update progress
846
- progress_bar.progress((i + 1) / len(z_values))
847
- status_text.text(f"Analyzing phase at z = {z:.2f} ({i+1}/{len(z_values)})")
848
 
849
- for j, beta in enumerate(beta_values):
850
- roots = compute_cubic_roots(z, beta, z_a, y)
851
-
852
- # Check if all roots are real (imaginary parts close to zero)
853
- is_all_real = all(abs(root.imag) < 1e-10 for root in roots)
854
-
855
- phase_map[i, j] = 1 if is_all_real else -1
856
-
857
- # Clear progress indicators
858
- progress_bar.empty()
859
- status_text.empty()
860
-
861
- # Create heatmap
862
- fig = go.Figure(data=go.Heatmap(
863
- z=phase_map,
864
- x=beta_values,
865
- y=z_values,
866
- colorscale=[[0, 'blue'], [0.5, 'white'], [1.0, 'red']],
867
- zmin=-1,
868
- zmax=1,
869
- showscale=True,
870
- colorbar=dict(
871
- title="Root Type",
872
- tickvals=[-1, 1],
873
- ticktext=["Complex Roots", "All Real Roots"]
874
- )
875
- ))
876
-
877
- fig.update_layout(
878
- title=f"Phase Diagram: Root Structure (y={y:.3f}, z_a={z_a:.3f})",
879
- xaxis_title="β",
880
- yaxis_title="z",
881
- hovermode="closest"
882
- )
883
-
884
- return fig
885
-
886
- @st.cache_data
887
- def generate_eigenvalue_distribution(beta, y, z_a, n=1000, seed=42):
888
- """
889
- Generate the eigenvalue distribution of B_n = S_n T_n as n→∞
890
- """
891
- # Apply the condition for y
892
- y_effective = y if y > 1 else 1/y
893
-
894
- # Set random seed
895
- np.random.seed(seed)
896
-
897
- # Compute dimension p based on aspect ratio y
898
- p = int(y_effective * n)
899
-
900
- # Constructing T_n (Population / Shape Matrix) - using the approach from the second script
901
- k = int(np.floor(beta * p))
902
- diag_entries = np.concatenate([
903
- np.full(k, z_a),
904
- np.full(p - k, 1.0)
905
- ])
906
- np.random.shuffle(diag_entries)
907
- T_n = np.diag(diag_entries)
908
-
909
- # Generate the data matrix X with i.i.d. standard normal entries
910
- X = np.random.randn(p, n)
911
-
912
- # Compute the sample covariance matrix S_n = (1/n) * XX^T
913
- S_n = (1 / n) * (X @ X.T)
914
-
915
- # Compute B_n = S_n T_n
916
- B_n = S_n @ T_n
917
-
918
- # Compute eigenvalues of B_n
919
- eigenvalues = np.linalg.eigvalsh(B_n)
920
-
921
- # Use KDE to compute a smooth density estimate
922
- kde = gaussian_kde(eigenvalues)
923
- x_vals = np.linspace(min(eigenvalues), max(eigenvalues), 500)
924
- kde_vals = kde(x_vals)
925
-
926
- # Create figure
927
- fig = go.Figure()
928
-
929
- # Add histogram trace
930
- fig.add_trace(go.Histogram(x=eigenvalues, histnorm='probability density',
931
- name="Histogram", marker=dict(color='blue', opacity=0.6)))
932
-
933
- # Add KDE trace
934
- fig.add_trace(go.Scatter(x=x_vals, y=kde_vals, mode="lines",
935
- name="KDE", line=dict(color='red', width=2)))
936
-
937
- fig.update_layout(
938
- title=f"Eigenvalue Distribution for B_n = S_n T_n (y={y:.1f}, β={beta:.2f}, a={z_a:.1f})",
939
- xaxis_title="Eigenvalue",
940
- yaxis_title="Density",
941
- hovermode="closest",
942
- showlegend=True
943
- )
944
-
945
- return fig, eigenvalues
946
 
947
- # ----------------- Streamlit UI -----------------
948
- st.title("Cubic Root Analysis")
 
 
 
 
 
 
949
 
950
- # Define three tabs
951
- tab1, tab2, tab3 = st.tabs(["z*(β) Curves", "Complex Root Analysis", "Differential Analysis"])
952
-
953
- # ----- Tab 1: z*(β) Curves -----
954
- with tab1:
955
- st.header("Eigenvalue Support Boundaries")
956
 
957
- # Cleaner layout with better column organization
958
- col1, col2, col3 = st.columns([1, 1, 2])
959
 
960
- with col1:
961
- z_a_1 = st.number_input("z_a", value=1.0, key="z_a_1")
962
- y_1 = st.number_input("y", value=1.0, key="y_1")
963
-
964
- with col2:
965
- z_min_1 = st.number_input("z_min", value=-10.0, key="z_min_1")
966
- z_max_1 = st.number_input("z_max", value=10.0, key="z_max_1")
967
 
968
- with col1:
969
- method_type = st.radio(
970
- "Calculation Method",
971
- ["Eigenvalue Method", "Discriminant Method"],
972
- index=0 # Default to eigenvalue method
973
- )
974
 
975
- # Advanced settings in collapsed expanders
976
- with st.expander("Method Settings", expanded=False):
977
- if method_type == "Eigenvalue Method":
978
- beta_steps = st.slider("β steps", min_value=21, max_value=101, value=51, step=10,
979
- key="beta_steps_eigen")
980
- n_samples = st.slider("Matrix size (n)", min_value=100, max_value=2000, value=1000,
981
- step=100)
982
- seeds = st.slider("Number of seeds", min_value=1, max_value=10, value=5, step=1)
983
- else:
984
- beta_steps = st.slider("β steps", min_value=51, max_value=501, value=201, step=50,
985
- key="beta_steps")
986
- z_steps = st.slider("z grid steps", min_value=1000, max_value=100000, value=50000,
987
- step=1000, key="z_steps")
988
 
989
- # Curve visibility options
990
- with st.expander("Curve Visibility", expanded=False):
991
- col_vis1, col_vis2 = st.columns(2)
992
- with col_vis1:
993
- show_high_y = st.checkbox("Show High y Expression", value=False, key="show_high_y")
994
- show_max_k = st.checkbox("Show Max k Expression", value=True, key="show_max_k")
995
- with col_vis2:
996
- show_low_y = st.checkbox("Show Low y Expression", value=False, key="show_low_y")
997
- show_min_t = st.checkbox("Show Min t Expression", value=True, key="show_min_t")
998
-
999
- # Custom expressions collapsed by default
1000
- with st.expander("Custom Expression 1 (s-based)", expanded=False):
1001
- st.markdown("""Enter expressions for s = numerator/denominator
1002
- (using variables `y`, `beta`, `z_a`, and `sqrt()`)""")
1003
- st.latex(r"\text{This s will be inserted into:}")
1004
- st.latex(r"\frac{y\beta(z_a-1)\underline{s}+(a\underline{s}+1)((y-1)\underline{s}-1)}{(a\underline{s}+1)(\underline{s}^2 + \underline{s})}")
1005
- s_num = st.text_input("s numerator", value="", key="s_num")
1006
- s_denom = st.text_input("s denominator", value="", key="s_denom")
1007
-
1008
- with st.expander("Custom Expression 2 (direct z(β))", expanded=False):
1009
- st.markdown("""Enter direct expression for z(β) = numerator/denominator
1010
- (using variables `y`, `beta`, `z_a`, and `sqrt()`)""")
1011
- z_num = st.text_input("z(β) numerator", value="", key="z_num")
1012
- z_denom = st.text_input("z(β) denominator", value="", key="z_denom")
1013
-
1014
- # Move show_derivatives to main UI level for better visibility
1015
- with col2:
1016
- show_derivatives = st.checkbox("Show derivatives", value=False)
1017
-
1018
- # Compute button
1019
- if st.button("Compute Curves", key="tab1_button"):
1020
- with col3:
1021
- use_eigenvalue_method = (method_type == "Eigenvalue Method")
1022
- if use_eigenvalue_method:
1023
- fig = generate_z_vs_beta_plot(z_a_1, y_1, z_min_1, z_max_1, beta_steps, None,
1024
- s_num, s_denom, z_num, z_denom, show_derivatives,
1025
- show_high_y, show_low_y, show_max_k, show_min_t,
1026
- use_eigenvalue_method=True, n_samples=n_samples,
1027
- seeds=seeds)
1028
- else:
1029
- fig = generate_z_vs_beta_plot(z_a_1, y_1, z_min_1, z_max_1, beta_steps, z_steps,
1030
- s_num, s_denom, z_num, z_denom, show_derivatives,
1031
- show_high_y, show_low_y, show_max_k, show_min_t,
1032
- use_eigenvalue_method=False)
1033
-
1034
- if fig is not None:
1035
- st.plotly_chart(fig, use_container_width=True)
1036
-
1037
- # Curve explanations in collapsed expander
1038
- with st.expander("Curve Explanations", expanded=False):
1039
- if use_eigenvalue_method:
1040
- st.markdown("""
1041
- - **Upper/Lower Bounds** (Blue): Maximum/minimum eigenvalues of B_n = S_n T_n
1042
- - **Shaded Region**: Eigenvalue support region
1043
- - **High y Expression** (Green): Asymptotic approximation for high y values
1044
- - **Low Expression** (Orange): Alternative asymptotic expression
1045
- - **Max k Expression** (Red): $\\max_{k \\in (0,\\infty)} \\frac{y\\beta (a-1)k + \\bigl(ak+1\\bigr)\\bigl((y-1)k-1\\bigr)}{(ak+1)(k^2+k)}$
1046
- - **Min t Expression** (Purple): $\\min_{t \\in \\left(-\\frac{1}{a},\\, 0\\right)} \\frac{y\\beta (a-1)t + \\bigl(at+1\\bigr)\\bigl((y-1)t-1\\bigr)}{(at+1)(t^2+t)}$
1047
- - **Custom Expression 1** (Magenta): Result from user-defined s substituted into the main formula
1048
- - **Custom Expression 2** (Brown): Direct z(β) expression
1049
- """)
1050
- else:
1051
- st.markdown("""
1052
- - **Upper z*(β)** (Blue): Maximum z value where discriminant is zero
1053
- - **Lower z*(β)** (Blue): Minimum z value where discriminant is zero
1054
- - **High y Expression** (Green): Asymptotic approximation for high y values
1055
- - **Low Expression** (Orange): Alternative asymptotic expression
1056
- - **Max k Expression** (Red): $\\max_{k \\in (0,\\infty)} \\frac{y\\beta (a-1)k + \\bigl(ak+1\\bigr)\\bigl((y-1)k-1\\bigr)}{(ak+1)(k^2+k)}$
1057
- - **Min t Expression** (Purple): $\\min_{t \\in \\left(-\\frac{1}{a},\\, 0\\right)} \\frac{y\\beta (a-1)t + \\bigl(at+1\\bigr)\\bigl((y-1)t-1\\bigr)}{(at+1)(t^2+t)}$
1058
- - **Custom Expression 1** (Magenta): Result from user-defined s substituted into the main formula
1059
- - **Custom Expression 2** (Brown): Direct z(β) expression
1060
- """)
1061
- if show_derivatives:
1062
- st.markdown("""
1063
- Derivatives are shown as:
1064
- - Dashed lines: First derivatives (d/dβ)
1065
- - Dotted lines: Second derivatives (d²/dβ²)
1066
- """)
1067
-
1068
- # ----- Tab 2: Complex Root Analysis -----
1069
- with tab2:
1070
- st.header("Complex Root Analysis")
1071
 
1072
- # Create tabs within the page for different plots
1073
- plot_tabs = st.tabs(["Im{s} vs. z", "Im{s} vs. β", "Phase Diagram", "Eigenvalue Distribution"])
1074
 
1075
- # Tab for Im{s} vs. z plot
1076
- with plot_tabs[0]:
1077
- col1, col2 = st.columns([1, 2])
1078
- with col1:
1079
- beta_z = st.number_input("β", value=0.5, min_value=0.0, max_value=1.0, key="beta_tab2_z")
1080
- y_z = st.number_input("y", value=1.0, key="y_tab2_z")
1081
- z_a_z = st.number_input("z_a", value=1.0, key="z_a_tab2_z")
1082
- z_min_z = st.number_input("z_min", value=-10.0, key="z_min_tab2_z")
1083
- z_max_z = st.number_input("z_max", value=10.0, key="z_max_tab2_z")
1084
- with st.expander("Resolution Settings", expanded=False):
1085
- z_points = st.slider("z grid points", min_value=100, max_value=2000, value=500, step=100, key="z_points_z")
1086
- if st.button("Compute Complex Roots vs. z", key="tab2_button_z"):
1087
- with col2:
1088
- fig_im, fig_re, fig_disc = generate_root_plots(beta_z, y_z, z_a_z, z_min_z, z_max_z, z_points)
1089
- if fig_im is not None and fig_re is not None and fig_disc is not None:
1090
- st.plotly_chart(fig_im, use_container_width=True)
1091
- st.plotly_chart(fig_re, use_container_width=True)
1092
- st.plotly_chart(fig_disc, use_container_width=True)
1093
-
1094
- with st.expander("Root Structure Analysis", expanded=False):
1095
- st.markdown("""
1096
- ### Root Structure Explanation
1097
-
1098
- The red dashed vertical lines mark the points where the cubic discriminant equals zero.
1099
- At these points, the cubic equation's root structure changes:
1100
-
1101
- - When the discriminant is positive, the cubic has three distinct real roots.
1102
- - When the discriminant is negative, the cubic has one real root and two complex conjugate roots.
1103
- - When the discriminant is exactly zero, the cubic has at least two equal roots.
1104
-
1105
- These transition points align perfectly with the z*(β) boundary curves from the first tab,
1106
- which represent exactly these transitions in the (β,z) plane.
1107
- """)
1108
-
1109
- # New tab for Im{s} vs. β plot
1110
- with plot_tabs[1]:
1111
- col1, col2 = st.columns([1, 2])
1112
- with col1:
1113
- z_beta = st.number_input("z", value=1.0, key="z_tab2_beta")
1114
- y_beta = st.number_input("y", value=1.0, key="y_tab2_beta")
1115
- z_a_beta = st.number_input("z_a", value=1.0, key="z_a_tab2_beta")
1116
- beta_min = st.number_input("β_min", value=0.0, min_value=0.0, max_value=1.0, key="beta_min_tab2")
1117
- beta_max = st.number_input("β_max", value=1.0, min_value=0.0, max_value=1.0, key="beta_max_tab2")
1118
- with st.expander("Resolution Settings", expanded=False):
1119
- beta_points = st.slider("β grid points", min_value=100, max_value=1000, value=500, step=100, key="beta_points")
1120
- if st.button("Compute Complex Roots vs. β", key="tab2_button_beta"):
1121
- with col2:
1122
- fig_im_beta, fig_re_beta, fig_disc = generate_roots_vs_beta_plots(
1123
- z_beta, y_beta, z_a_beta, beta_min, beta_max, beta_points)
1124
-
1125
- if fig_im_beta is not None and fig_re_beta is not None and fig_disc is not None:
1126
- st.plotly_chart(fig_im_beta, use_container_width=True)
1127
- st.plotly_chart(fig_re_beta, use_container_width=True)
1128
- st.plotly_chart(fig_disc, use_container_width=True)
1129
-
1130
- # Add analysis of transition points
1131
- transition_points, structure_types = analyze_complex_root_structure(
1132
- np.linspace(beta_min, beta_max, beta_points), z_beta, z_a_beta, y_beta)
1133
-
1134
- if transition_points:
1135
- st.subheader("Root Structure Transition Points")
1136
- for i, beta in enumerate(transition_points):
1137
- prev_type = structure_types[i]
1138
- next_type = structure_types[i+1] if i+1 < len(structure_types) else "unknown"
1139
- st.markdown(f"- At β = {beta:.6f}: Transition from {prev_type} roots to {next_type} roots")
1140
- else:
1141
- st.info("No transitions detected in root structure across this β range.")
1142
-
1143
- # Explanation
1144
- with st.expander("Analysis Explanation", expanded=False):
1145
- st.markdown("""
1146
- ### Interpreting the Plots
1147
-
1148
- - **Im{s} vs. β**: Shows how the imaginary parts of the roots change with β. When all curves are at Im{s}=0, all roots are real.
1149
- - **Re{s} vs. β**: Shows how the real parts of the roots change with β.
1150
- - **Discriminant Plot**: The cubic discriminant changes sign at points where the root structure changes.
1151
- - When discriminant < 0: The cubic has one real root and two complex conjugate roots.
1152
- - When discriminant > 0: The cubic has three distinct real roots.
1153
- - When discriminant = 0: The cubic has multiple roots (at least two roots are equal).
1154
-
1155
- The vertical red dashed lines mark the transition points where the root structure changes.
1156
- """)
1157
 
1158
- # Tab for Phase Diagram
1159
- with plot_tabs[2]:
1160
- col1, col2 = st.columns([1, 2])
1161
- with col1:
1162
- z_a_phase = st.number_input("z_a", value=1.0, key="z_a_phase")
1163
- y_phase = st.number_input("y", value=1.0, key="y_phase")
1164
- beta_min_phase = st.number_input("β_min", value=0.0, min_value=0.0, max_value=1.0, key="beta_min_phase")
1165
- beta_max_phase = st.number_input("β_max", value=1.0, min_value=0.0, max_value=1.0, key="beta_max_phase")
1166
- z_min_phase = st.number_input("z_min", value=-10.0, key="z_min_phase")
1167
- z_max_phase = st.number_input("z_max", value=10.0, key="z_max_phase")
1168
-
1169
- with st.expander("Resolution Settings", expanded=False):
1170
- beta_steps_phase = st.slider("β grid points", min_value=20, max_value=200, value=100, step=20, key="beta_steps_phase")
1171
- z_steps_phase = st.slider("z grid points", min_value=20, max_value=200, value=100, step=20, key="z_steps_phase")
1172
-
1173
- if st.button("Generate Phase Diagram", key="tab2_button_phase"):
1174
- with col2:
1175
- st.info("Generating phase diagram. This may take a while depending on resolution...")
1176
- fig_phase = generate_phase_diagram(
1177
- z_a_phase, y_phase, beta_min_phase, beta_max_phase, z_min_phase, z_max_phase,
1178
- beta_steps_phase, z_steps_phase)
1179
-
1180
- if fig_phase is not None:
1181
- st.plotly_chart(fig_phase, use_container_width=True)
1182
-
1183
- with st.expander("Phase Diagram Explanation", expanded=False):
1184
- st.markdown("""
1185
- ### Understanding the Phase Diagram
1186
-
1187
- This heatmap shows the regions in the (β, z) plane where:
1188
-
1189
- - **Red Regions**: The cubic equation has all real roots
1190
- - **Blue Regions**: The cubic equation has one real root and two complex conjugate roots
1191
-
1192
- The boundaries between these regions represent values where the discriminant is zero,
1193
- which are the exact same curves as the z*(β) boundaries in the first tab. This phase
1194
- diagram provides a comprehensive view of the eigenvalue support structure.
1195
- """)
1196
-
1197
- # Eigenvalue distribution tab
1198
- with plot_tabs[3]:
1199
- st.subheader("Eigenvalue Distribution for B_n = S_n T_n")
1200
- with st.expander("Simulation Information", expanded=False):
1201
- st.markdown("""
1202
- This simulation generates the eigenvalue distribution of B_n as n→∞, where:
1203
- - B_n = (1/n)XX^T with X being a p×n matrix
1204
- - p/n → y as n→∞
1205
- - The diagonal entries of T_n follow distribution β·δ(z_a) + (1-β)·δ(1)
1206
- """)
1207
-
1208
- col_eigen1, col_eigen2 = st.columns([1, 2])
1209
- with col_eigen1:
1210
- beta_eigen = st.number_input("β", value=0.5, min_value=0.0, max_value=1.0, key="beta_eigen")
1211
- y_eigen = st.number_input("y", value=1.0, key="y_eigen")
1212
- z_a_eigen = st.number_input("z_a", value=1.0, key="z_a_eigen")
1213
- n_samples = st.slider("Number of samples (n)", min_value=100, max_value=2000, value=1000, step=100)
1214
- sim_seed = st.number_input("Random seed", min_value=1, max_value=1000, value=42, step=1)
1215
-
1216
- # Add comparison option
1217
- show_theoretical = st.checkbox("Show theoretical boundaries", value=True)
1218
- show_empirical_stats = st.checkbox("Show empirical statistics", value=True)
1219
-
1220
- if st.button("Generate Eigenvalue Distribution", key="tab2_eigen_button"):
1221
- with col_eigen2:
1222
- # Generate the eigenvalue distribution
1223
- fig_eigen, eigenvalues = generate_eigenvalue_distribution(beta_eigen, y_eigen, z_a_eigen, n=n_samples, seed=sim_seed)
1224
-
1225
- # If requested, compute and add theoretical boundaries
1226
- if show_theoretical:
1227
- # Calculate min and max eigenvalues using the support boundary functions
1228
- betas = np.array([beta_eigen])
1229
- min_eig, max_eig = compute_eigenvalue_support_boundaries(z_a_eigen, y_eigen, betas, n_samples=n_samples, seeds=5)
1230
-
1231
- # Add vertical lines for boundaries
1232
- fig_eigen.add_vline(
1233
- x=min_eig[0],
1234
- line=dict(color="red", width=2, dash="dash"),
1235
- annotation_text="Min theoretical",
1236
- annotation_position="top right"
1237
- )
1238
- fig_eigen.add_vline(
1239
- x=max_eig[0],
1240
- line=dict(color="red", width=2, dash="dash"),
1241
- annotation_text="Max theoretical",
1242
- annotation_position="top left"
1243
- )
1244
-
1245
- # Display the plot
1246
- st.plotly_chart(fig_eigen, use_container_width=True)
1247
-
1248
- # Add comparison of empirical vs theoretical bounds
1249
- if show_theoretical and show_empirical_stats:
1250
- empirical_min = eigenvalues.min()
1251
- empirical_max = eigenvalues.max()
1252
-
1253
- st.markdown("### Comparison of Empirical vs Theoretical Bounds")
1254
- col1, col2, col3 = st.columns(3)
1255
- with col1:
1256
- st.metric("Theoretical Min", f"{min_eig[0]:.4f}")
1257
- st.metric("Theoretical Max", f"{max_eig[0]:.4f}")
1258
- st.metric("Theoretical Width", f"{max_eig[0] - min_eig[0]:.4f}")
1259
- with col2:
1260
- st.metric("Empirical Min", f"{empirical_min:.4f}")
1261
- st.metric("Empirical Max", f"{empirical_max:.4f}")
1262
- st.metric("Empirical Width", f"{empirical_max - empirical_min:.4f}")
1263
- with col3:
1264
- st.metric("Min Difference", f"{empirical_min - min_eig[0]:.4f}")
1265
- st.metric("Max Difference", f"{empirical_max - max_eig[0]:.4f}")
1266
- st.metric("Width Difference", f"{(empirical_max - empirical_min) - (max_eig[0] - min_eig[0]):.4f}")
1267
-
1268
- # Display additional statistics
1269
- if show_empirical_stats:
1270
- st.markdown("### Eigenvalue Statistics")
1271
- col1, col2 = st.columns(2)
1272
- with col1:
1273
- st.metric("Mean", f"{np.mean(eigenvalues):.4f}")
1274
- st.metric("Median", f"{np.median(eigenvalues):.4f}")
1275
- with col2:
1276
- st.metric("Standard Deviation", f"{np.std(eigenvalues):.4f}")
1277
- st.metric("Interquartile Range", f"{np.percentile(eigenvalues, 75) - np.percentile(eigenvalues, 25):.4f}")
1278
-
1279
- # ----- Tab 3: Differential Analysis -----
1280
- with tab3:
1281
- st.header("Differential Analysis vs. β")
1282
- with st.expander("Description", expanded=False):
1283
- st.markdown("This page shows the difference between the Upper (blue) and Lower (lightblue) z*(β) curves, along with their first and second derivatives with respect to β.")
1284
-
1285
- col1, col2 = st.columns([1, 2])
1286
- with col1:
1287
- z_a_diff = st.number_input("z_a", value=1.0, key="z_a_diff")
1288
- y_diff = st.number_input("y", value=1.0, key="y_diff")
1289
- z_min_diff = st.number_input("z_min", value=-10.0, key="z_min_diff")
1290
- z_max_diff = st.number_input("z_max", value=10.0, key="z_max_diff")
1291
-
1292
- diff_method_type = st.radio(
1293
- "Boundary Calculation Method",
1294
- ["Eigenvalue Method", "Discriminant Method"],
1295
- index=0,
1296
- key="diff_method_type"
1297
- )
1298
-
1299
- with st.expander("Resolution Settings", expanded=False):
1300
- if diff_method_type == "Eigenvalue Method":
1301
- beta_steps_diff = st.slider("β steps", min_value=21, max_value=101, value=51, step=10,
1302
- key="beta_steps_diff_eigen")
1303
- diff_n_samples = st.slider("Matrix size (n)", min_value=100, max_value=2000, value=1000,
1304
- step=100, key="diff_n_samples")
1305
- diff_seeds = st.slider("Number of seeds", min_value=1, max_value=10, value=5, step=1,
1306
- key="diff_seeds")
1307
- else:
1308
- beta_steps_diff = st.slider("β steps", min_value=51, max_value=501, value=201, step=50,
1309
- key="beta_steps_diff")
1310
- z_steps_diff = st.slider("z grid steps", min_value=1000, max_value=100000, value=50000,
1311
- step=1000, key="z_steps_diff")
1312
-
1313
- # Add options for curve selection
1314
- st.subheader("Curves to Analyze")
1315
- analyze_upper_lower = st.checkbox("Upper-Lower Difference", value=True)
1316
- analyze_high_y = st.checkbox("High y Expression", value=False)
1317
- analyze_alt_low = st.checkbox("Low y Expression", value=False)
1318
-
1319
- if st.button("Compute Differentials", key="tab3_button"):
1320
- with col2:
1321
- use_eigenvalue_method_diff = (diff_method_type == "Eigenvalue Method")
1322
-
1323
- if use_eigenvalue_method_diff:
1324
- betas_diff = np.linspace(0, 1, beta_steps_diff)
1325
- st.info("Computing eigenvalue support boundaries. This may take a moment...")
1326
- lower_vals, upper_vals = compute_eigenvalue_support_boundaries(
1327
- z_a_diff, y_diff, betas_diff, diff_n_samples, diff_seeds)
1328
- else:
1329
- betas_diff, lower_vals, upper_vals = sweep_beta_and_find_z_bounds(
1330
- z_a_diff, y_diff, z_min_diff, z_max_diff, beta_steps_diff, z_steps_diff)
1331
-
1332
- # Create figure
1333
- fig_diff = go.Figure()
1334
-
1335
- if analyze_upper_lower:
1336
- diff_curve = upper_vals - lower_vals
1337
- d1 = np.gradient(diff_curve, betas_diff)
1338
- d2 = np.gradient(d1, betas_diff)
1339
-
1340
- fig_diff.add_trace(go.Scatter(x=betas_diff, y=diff_curve, mode="lines",
1341
- name="Upper-Lower Difference", line=dict(color="magenta", width=2)))
1342
- fig_diff.add_trace(go.Scatter(x=betas_diff, y=d1, mode="lines",
1343
- name="Upper-Lower d/dβ", line=dict(color="magenta", dash='dash')))
1344
- fig_diff.add_trace(go.Scatter(x=betas_diff, y=d2, mode="lines",
1345
- name="Upper-Lower d²/dβ²", line=dict(color="magenta", dash='dot')))
1346
-
1347
- if analyze_high_y:
1348
- high_y_curve = compute_high_y_curve(betas_diff, z_a_diff, y_diff)
1349
- d1 = np.gradient(high_y_curve, betas_diff)
1350
- d2 = np.gradient(d1, betas_diff)
1351
-
1352
- fig_diff.add_trace(go.Scatter(x=betas_diff, y=high_y_curve, mode="lines",
1353
- name="High y", line=dict(color="green", width=2)))
1354
- fig_diff.add_trace(go.Scatter(x=betas_diff, y=d1, mode="lines",
1355
- name="High y d/dβ", line=dict(color="green", dash='dash')))
1356
- fig_diff.add_trace(go.Scatter(x=betas_diff, y=d2, mode="lines",
1357
- name="High y d²/dβ²", line=dict(color="green", dash='dot')))
1358
-
1359
- if analyze_alt_low:
1360
- alt_low_curve = compute_alternate_low_expr(betas_diff, z_a_diff, y_diff)
1361
- d1 = np.gradient(alt_low_curve, betas_diff)
1362
- d2 = np.gradient(d1, betas_diff)
1363
-
1364
- fig_diff.add_trace(go.Scatter(x=betas_diff, y=alt_low_curve, mode="lines",
1365
- name="Low y", line=dict(color="orange", width=2)))
1366
- fig_diff.add_trace(go.Scatter(x=betas_diff, y=d1, mode="lines",
1367
- name="Low y d/dβ", line=dict(color="orange", dash='dash')))
1368
- fig_diff.add_trace(go.Scatter(x=betas_diff, y=d2, mode="lines",
1369
- name="Low y d²/dβ²", line=dict(color="orange", dash='dot')))
1370
-
1371
- fig_diff.update_layout(
1372
- title="Differential Analysis vs. β" +
1373
- (" (Eigenvalue Method)" if use_eigenvalue_method_diff else " (Discriminant Method)"),
1374
- xaxis_title="β",
1375
- yaxis_title="Value",
1376
- hovermode="x unified",
1377
- showlegend=True,
1378
- legend=dict(
1379
- yanchor="top",
1380
- y=0.99,
1381
- xanchor="left",
1382
- x=0.01
1383
- )
1384
- )
1385
- st.plotly_chart(fig_diff, use_container_width=True)
1386
-
1387
- with st.expander("Curve Types", expanded=False):
1388
- st.markdown("""
1389
- - Solid lines: Original curves
1390
- - Dashed lines: First derivatives (d/dβ)
1391
- - Dotted lines: Second derivatives (d²/dβ²)
1392
- """)
 
1
  import streamlit as st
2
+ import subprocess
3
+ import os
4
+ from PIL import Image
5
+ import time
 
6
 
7
+ # Set page config
8
  st.set_page_config(
9
+ page_title="Eigenvalue Analysis",
10
+ page_icon="📊",
11
+ layout="wide"
12
  )
13
 
14
+ # Title and description
15
+ st.title("Eigenvalue Analysis Visualization")
16
+ st.markdown("""
17
+ This application visualizes eigenvalue analysis for matrices with specific properties.
18
+ Adjust the parameters below to generate a plot showing the relationship between empirical
19
+ and theoretical eigenvalues.
20
+ """)
21
+
22
+ # Create output directory if it doesn't exist
23
+ os.makedirs("/app/output", exist_ok=True)
24
+
25
+ # Input parameters sidebar
26
+ st.sidebar.header("Parameters")
27
+
28
+ # Parameter inputs with defaults and validation
29
+ col1, col2 = st.sidebar.columns(2)
30
+ with col1:
31
+ n = st.number_input("Sample size (n)", min_value=5, max_value=1000, value=100, step=5, help="Number of samples")
32
+ a = st.number_input("Value for a", min_value=1.1, max_value=10.0, value=2.0, step=0.1, help="Parameter a > 1")
33
+
34
+ with col2:
35
+ p = st.number_input("Dimension (p)", min_value=5, max_value=1000, value=50, step=5, help="Dimensionality")
36
+ y = st.number_input("Value for y", min_value=0.1, max_value=10.0, value=1.0, step=0.1, help="Parameter y > 0")
37
+
38
+ # Generate button
39
+ if st.sidebar.button("Generate Plot", type="primary"):
40
+ # Show progress
41
+ with st.spinner("Generating eigenvalue analysis plot... This may take a few moments."):
42
+ # Run the C++ executable with the parameters
43
+ output_file = "/app/output/eigenvalue_analysis.png"
44
+
45
+ # Delete previous output if exists
46
+ if os.path.exists(output_file):
47
+ os.remove(output_file)
48
+
49
+ # Execute the C++ program
50
+ try:
51
+ cmd = ["/app/eigen_analysis", str(n), str(p), str(a), str(y)]
52
+ process = subprocess.Popen(
53
+ cmd,
54
+ stdout=subprocess.PIPE,
55
+ stderr=subprocess.PIPE,
56
+ text=True
57
+ )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
58
 
59
+ # Show output in a status area
60
+ status_area = st.empty()
61
 
62
+ while True:
63
+ output = process.stdout.readline()
64
+ if output == '' and process.poll() is not None:
65
+ break
66
+ if output:
67
+ status_area.info(output.strip())
68
 
69
+ return_code = process.poll()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
70
 
71
+ if return_code != 0:
72
+ error = process.stderr.read()
73
+ st.error(f"Error executing the analysis: {error}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
74
  else:
75
+ status_area.success("Analysis completed successfully!")
76
+
77
+ # Wait a moment to ensure the file is written
78
+ time.sleep(1)
79
+
80
+ # Display the image if it exists
81
+ if os.path.exists(output_file):
82
+ img = Image.open(output_file)
83
+ st.image(img, use_column_width=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
84
 
85
+ # Provide download button
86
+ with open(output_file, "rb") as file:
87
+ btn = st.download_button(
88
+ label="Download Plot",
89
+ data=file,
90
+ file_name=f"eigenvalue_analysis_n{n}_p{p}_a{a}_y{y}.png",
91
+ mime="image/png"
92
+ )
93
+ else:
94
+ st.error("Plot generation failed. Output file not found.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
95
 
96
+ except Exception as e:
97
+ st.error(f"An error occurred: {str(e)}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
98
 
99
+ # Show example plot on startup
100
+ if not os.path.exists("/app/output/eigenvalue_analysis.png"):
101
+ st.info("👈 Set parameters and click 'Generate Plot' to create a visualization.")
102
+ else:
103
+ # Show the most recent plot by default
104
+ st.subheader("Current Plot")
105
+ img = Image.open("/app/output/eigenvalue_analysis.png")
106
+ st.image(img, use_column_width=True)
107
 
108
+ # Add information about the analysis
109
+ with st.expander("About Eigenvalue Analysis"):
110
+ st.markdown("""
111
+ ## Theory
 
 
112
 
113
+ This application visualizes the relationship between empirical and theoretical eigenvalues for matrices with specific properties.
 
114
 
115
+ The analysis examines:
 
 
 
 
 
 
116
 
117
+ - **Empirical Max/Min Eigenvalues**: The maximum and minimum eigenvalues calculated from the generated matrices
118
+ - **Theoretical Max/Min Functions**: The theoretical bounds derived from mathematical analysis
 
 
 
 
119
 
120
+ ### Key Parameters
 
 
 
 
 
 
 
 
 
 
 
 
121
 
122
+ - **n**: Sample size
123
+ - **p**: Dimension
124
+ - **a**: Value > 1 that affects the distribution of eigenvalues
125
+ - **y**: Value that affects scaling
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
126
 
127
+ ### Mathematical Formulas
 
128
 
129
+ Max Function:
130
+ max{k ∈ (0,∞)} [yβ(a-1)k + (ak+1)((y-1)k-1)]/[(ak+1)(k²+k)y]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
131
 
132
+ Min Function:
133
+ min{t ∈ (-1/a,0)} [yβ(a-1)t + (at+1)((y-1)t-1)]/[(at+1)(t²+t)y]
134
+ """)