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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +887 -691
app.py CHANGED
@@ -4,12 +4,6 @@ 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
- import os
8
- import sys
9
- import tempfile
10
- import subprocess
11
- import importlib.util
12
- import shutil
13
 
14
  # Configure Streamlit for Hugging Face Spaces
15
  st.set_page_config(
@@ -18,539 +12,6 @@ st.set_page_config(
18
  initial_sidebar_state="collapsed"
19
  )
20
 
21
- # Define C++ extension code as a string
22
- CPP_CODE = r'''
23
- #include <pybind11/pybind11.h>
24
- #include <pybind11/numpy.h>
25
- #include <pybind11/eigen.h>
26
- #include <Eigen/Dense>
27
- #include <vector>
28
- #include <cmath>
29
- #include <limits>
30
-
31
- namespace py = pybind11;
32
-
33
- // Compute the cubic discriminant
34
- double compute_discriminant(double z, double beta, double z_a, double y_effective) {
35
- double a = z * z_a;
36
- double b = z * z_a + z + z_a - z_a * y_effective;
37
- double c = z + z_a + 1 - y_effective * (beta * z_a + 1 - beta);
38
- double d = 1;
39
-
40
- // Symbolic expression for the cubic discriminant
41
- return std::pow((b*c)/(6*a*a) - std::pow(b, 3)/(27*std::pow(a, 3)) - d/(2*a), 2) +
42
- std::pow(c/(3*a) - std::pow(b, 2)/(9*std::pow(a, 2)), 3);
43
- }
44
-
45
- // Find z values where the discriminant equals zero
46
- std::vector<double> find_z_at_discriminant_zero(double z_a, double y, double beta,
47
- double z_min, double z_max, int steps) {
48
- // Apply the condition for y
49
- double y_effective = y > 1 ? y : 1/y;
50
-
51
- // Create z grid
52
- std::vector<double> z_grid(steps);
53
- double step_size = (z_max - z_min) / (steps - 1);
54
- for (int i = 0; i < steps; i++) {
55
- z_grid[i] = z_min + i * step_size;
56
- }
57
-
58
- // Calculate discriminant values
59
- std::vector<double> disc_vals(steps);
60
- for (int i = 0; i < steps; i++) {
61
- disc_vals[i] = compute_discriminant(z_grid[i], beta, z_a, y_effective);
62
- }
63
-
64
- // Find roots
65
- std::vector<double> roots_found;
66
-
67
- for (int i = 0; i < steps - 1; i++) {
68
- double f1 = disc_vals[i];
69
- double f2 = disc_vals[i+1];
70
-
71
- // Skip if NaN
72
- if (std::isnan(f1) || std::isnan(f2)) {
73
- continue;
74
- }
75
-
76
- // Check for exact zero
77
- if (f1 == 0.0) {
78
- roots_found.push_back(z_grid[i]);
79
- }
80
- else if (f2 == 0.0) {
81
- roots_found.push_back(z_grid[i+1]);
82
- }
83
- // Check for sign change
84
- else if (f1 * f2 < 0) {
85
- double zl = z_grid[i];
86
- double zr = z_grid[i+1];
87
- double f1_local = f1;
88
- double f2_local = f2;
89
-
90
- // Use binary search to refine the root
91
- for (int j = 0; j < 50; j++) {
92
- double mid = 0.5 * (zl + zr);
93
- double fm = compute_discriminant(mid, beta, z_a, y_effective);
94
-
95
- if (fm == 0) {
96
- zl = zr = mid;
97
- break;
98
- }
99
-
100
- if ((fm > 0 && f1_local > 0) || (fm < 0 && f1_local < 0)) {
101
- zl = mid;
102
- f1_local = fm;
103
- } else {
104
- zr = mid;
105
- f2_local = fm;
106
- }
107
- }
108
-
109
- roots_found.push_back(0.5 * (zl + zr));
110
- }
111
- }
112
-
113
- return roots_found;
114
- }
115
-
116
- // Sweep beta values and find z boundary values
117
- std::tuple<py::array_t<double>, py::array_t<double>, py::array_t<double>>
118
- sweep_beta_and_find_z_bounds(double z_a, double y, double z_min, double z_max, int beta_steps, int z_steps) {
119
- // Create beta values
120
- py::array_t<double> betas(beta_steps);
121
- auto betas_ptr = betas.mutable_data();
122
- double beta_step = 1.0 / (beta_steps - 1);
123
- for (int i = 0; i < beta_steps; i++) {
124
- betas_ptr[i] = i * beta_step;
125
- }
126
-
127
- // Initialize arrays for min and max z values
128
- py::array_t<double> z_min_values(beta_steps);
129
- py::array_t<double> z_max_values(beta_steps);
130
- auto z_min_ptr = z_min_values.mutable_data();
131
- auto z_max_ptr = z_max_values.mutable_data();
132
-
133
- for (int i = 0; i < beta_steps; i++) {
134
- double beta = betas_ptr[i];
135
- std::vector<double> roots = find_z_at_discriminant_zero(z_a, y, beta, z_min, z_max, z_steps);
136
-
137
- if (roots.size() == 0) {
138
- z_min_ptr[i] = std::numeric_limits<double>::quiet_NaN();
139
- z_max_ptr[i] = std::numeric_limits<double>::quiet_NaN();
140
- } else {
141
- // Find min and max roots
142
- double min_root = roots[0];
143
- double max_root = roots[0];
144
- for (size_t j = 1; j < roots.size(); j++) {
145
- if (roots[j] < min_root) min_root = roots[j];
146
- if (roots[j] > max_root) max_root = roots[j];
147
- }
148
- z_min_ptr[i] = min_root;
149
- z_max_ptr[i] = max_root;
150
- }
151
- }
152
-
153
- return std::make_tuple(betas, z_min_values, z_max_values);
154
- }
155
-
156
- // Compute High y Expression curve
157
- py::array_t<double> compute_high_y_curve(py::array_t<double> betas, double z_a, double y) {
158
- // Apply the condition for y
159
- double y_effective = y > 1 ? y : 1/y;
160
-
161
- auto betas_ptr = betas.data();
162
- size_t n = betas.size();
163
- py::array_t<double> result(n);
164
- auto result_ptr = result.mutable_data();
165
-
166
- double a = z_a;
167
- double denominator = 1 - 2*a;
168
-
169
- if (std::abs(denominator) < 1e-10) {
170
- for (size_t i = 0; i < n; i++) {
171
- result_ptr[i] = std::numeric_limits<double>::quiet_NaN();
172
- }
173
- } else {
174
- for (size_t i = 0; i < n; i++) {
175
- double beta = betas_ptr[i];
176
- double numerator = -4*a*(a-1)*y_effective*beta - 2*a*y_effective - 2*a*(2*a-1);
177
- result_ptr[i] = numerator/denominator;
178
- }
179
- }
180
-
181
- return result;
182
- }
183
-
184
- // Compute alternative low expression
185
- py::array_t<double> compute_alternate_low_expr(py::array_t<double> betas, double z_a, double y) {
186
- // Apply the condition for y
187
- double y_effective = y > 1 ? y : 1/y;
188
-
189
- auto betas_ptr = betas.data();
190
- size_t n = betas.size();
191
- py::array_t<double> result(n);
192
- auto result_ptr = result.mutable_data();
193
-
194
- for (size_t i = 0; i < n; i++) {
195
- double beta = betas_ptr[i];
196
- result_ptr[i] = (z_a * y_effective * beta * (z_a - 1) - 2*z_a*(1 - y_effective) - 2*z_a*z_a) / (2 + 2*z_a);
197
- }
198
-
199
- return result;
200
- }
201
-
202
- // Compute max k expression
203
- py::array_t<double> compute_max_k_expression(py::array_t<double> betas, double z_a, double y, int k_samples=1000) {
204
- // Apply the condition for y
205
- double y_effective = y > 1 ? y : 1/y;
206
-
207
- auto betas_ptr = betas.data();
208
- size_t n = betas.size();
209
- py::array_t<double> result(n);
210
- auto result_ptr = result.mutable_data();
211
-
212
- double a = z_a;
213
-
214
- // Sample k values on a logarithmic scale
215
- std::vector<double> k_values(k_samples);
216
- double log_min = -3;
217
- double log_max = 3;
218
- double log_step = (log_max - log_min) / (k_samples - 1);
219
-
220
- for (int j = 0; j < k_samples; j++) {
221
- k_values[j] = std::pow(10, log_min + j * log_step);
222
- }
223
-
224
- for (size_t i = 0; i < n; i++) {
225
- double beta = betas_ptr[i];
226
- std::vector<double> values(k_samples);
227
-
228
- for (int j = 0; j < k_samples; j++) {
229
- double k = k_values[j];
230
- double numerator = y_effective*beta*(a-1)*k + (a*k+1)*((y_effective-1)*k-1);
231
- double denominator = (a*k+1)*(k*k+k);
232
-
233
- if (std::abs(denominator) < 1e-10) {
234
- values[j] = std::numeric_limits<double>::quiet_NaN();
235
- } else {
236
- values[j] = numerator/denominator;
237
- }
238
- }
239
-
240
- // Find max value, ignoring NaNs
241
- double max_val = -std::numeric_limits<double>::infinity();
242
- bool found_valid = false;
243
-
244
- for (double val : values) {
245
- if (!std::isnan(val) && val > max_val) {
246
- max_val = val;
247
- found_valid = true;
248
- }
249
- }
250
-
251
- result_ptr[i] = found_valid ? max_val : std::numeric_limits<double>::quiet_NaN();
252
- }
253
-
254
- return result;
255
- }
256
-
257
- // Compute min t expression
258
- py::array_t<double> compute_min_t_expression(py::array_t<double> betas, double z_a, double y, int t_samples=1000) {
259
- // Apply the condition for y
260
- double y_effective = y > 1 ? y : 1/y;
261
-
262
- auto betas_ptr = betas.data();
263
- size_t n = betas.size();
264
- py::array_t<double> result(n);
265
- auto result_ptr = result.mutable_data();
266
-
267
- double a = z_a;
268
-
269
- if (a <= 0) {
270
- for (size_t i = 0; i < n; i++) {
271
- result_ptr[i] = std::numeric_limits<double>::quiet_NaN();
272
- }
273
- return result;
274
- }
275
-
276
- // Create t values from -1/a to 0
277
- double lower_bound = -1/a + 1e-10; // Avoid division by zero
278
- double step_size = (-1e-10 - lower_bound) / (t_samples - 1);
279
- std::vector<double> t_values(t_samples);
280
-
281
- for (int j = 0; j < t_samples; j++) {
282
- t_values[j] = lower_bound + j * step_size;
283
- }
284
-
285
- for (size_t i = 0; i < n; i++) {
286
- double beta = betas_ptr[i];
287
- std::vector<double> values(t_samples);
288
-
289
- for (int j = 0; j < t_samples; j++) {
290
- double t = t_values[j];
291
- double numerator = y_effective*beta*(a-1)*t + (a*t+1)*((y_effective-1)*t-1);
292
- double denominator = (a*t+1)*(t*t+t);
293
-
294
- if (std::abs(denominator) < 1e-10) {
295
- values[j] = std::numeric_limits<double>::quiet_NaN();
296
- } else {
297
- values[j] = numerator/denominator;
298
- }
299
- }
300
-
301
- // Find min value, ignoring NaNs
302
- double min_val = std::numeric_limits<double>::infinity();
303
- bool found_valid = false;
304
-
305
- for (double val : values) {
306
- if (!std::isnan(val) && val < min_val) {
307
- min_val = val;
308
- found_valid = true;
309
- }
310
- }
311
-
312
- result_ptr[i] = found_valid ? min_val : std::numeric_limits<double>::quiet_NaN();
313
- }
314
-
315
- return result;
316
- }
317
-
318
- // Compute eigenvalue support boundaries
319
- std::tuple<py::array_t<double>, py::array_t<double>>
320
- compute_eigenvalue_support_boundaries(double z_a, double y, py::array_t<double> beta_values,
321
- int n_samples = 100, int seeds = 5) {
322
- // Apply the condition for y
323
- double y_effective = y > 1 ? y : 1/y;
324
-
325
- auto beta_ptr = beta_values.data();
326
- size_t num_betas = beta_values.size();
327
-
328
- py::array_t<double> min_eigenvalues(num_betas);
329
- py::array_t<double> max_eigenvalues(num_betas);
330
- auto min_eig_ptr = min_eigenvalues.mutable_data();
331
- auto max_eig_ptr = max_eigenvalues.mutable_data();
332
-
333
- for (size_t i = 0; i < num_betas; i++) {
334
- double beta = beta_ptr[i];
335
-
336
- std::vector<double> min_vals;
337
- std::vector<double> max_vals;
338
-
339
- // Run multiple trials with different seeds
340
- for (int seed = 0; seed < seeds; seed++) {
341
- // Set random seed
342
- srand(seed * 100 + i);
343
-
344
- // Compute dimension p based on aspect ratio y
345
- int n = n_samples;
346
- int p = int(y_effective * n);
347
-
348
- // Constructing T_n (Population / Shape Matrix)
349
- int k = int(std::floor(beta * p));
350
-
351
- // Create diagonal entries
352
- std::vector<double> diag_entries(p);
353
- for (int j = 0; j < k; j++) {
354
- diag_entries[j] = z_a;
355
- }
356
- for (int j = k; j < p; j++) {
357
- diag_entries[j] = 1.0;
358
- }
359
-
360
- // Shuffle the diagonal entries (simple Fisher-Yates shuffle)
361
- for (int j = p-1; j > 0; j--) {
362
- int idx = rand() % (j+1);
363
- std::swap(diag_entries[j], diag_entries[idx]);
364
- }
365
-
366
- // Generate the data matrix X with i.i.d. standard normal entries
367
- std::vector<std::vector<double>> X(p, std::vector<double>(n));
368
- for (int row = 0; row < p; row++) {
369
- for (int col = 0; col < n; col++) {
370
- // Box-Muller transform to generate normal distribution
371
- double u1 = rand() / (RAND_MAX + 1.0);
372
- double u2 = rand() / (RAND_MAX + 1.0);
373
- if (u1 < 1e-10) u1 = 1e-10; // Avoid log(0)
374
- double z = sqrt(-2.0 * log(u1)) * cos(2.0 * M_PI * u2);
375
- X[row][col] = z;
376
- }
377
- }
378
-
379
- // Compute the sample covariance matrix S_n = (1/n) * XX^T
380
- std::vector<std::vector<double>> S_n(p, std::vector<double>(p, 0.0));
381
- for (int row = 0; row < p; row++) {
382
- for (int col = 0; col < p; col++) {
383
- double sum = 0.0;
384
- for (int k = 0; k < n; k++) {
385
- sum += X[row][k] * X[col][k];
386
- }
387
- S_n[row][col] = sum / n;
388
- }
389
- }
390
-
391
- // Compute B_n = S_n T_n
392
- std::vector<std::vector<double>> B_n(p, std::vector<double>(p, 0.0));
393
- for (int row = 0; row < p; row++) {
394
- for (int col = 0; col < p; col++) {
395
- B_n[row][col] = S_n[row][col] * diag_entries[col];
396
- }
397
- }
398
-
399
- // Use Eigen library to compute eigenvalues
400
- Eigen::MatrixXd B_n_eigen(p, p);
401
- for (int row = 0; row < p; row++) {
402
- for (int col = 0; col < p; col++) {
403
- B_n_eigen(row, col) = B_n[row][col];
404
- }
405
- }
406
-
407
- Eigen::SelfAdjointEigenSolver<Eigen::MatrixXd> solver(B_n_eigen);
408
- Eigen::VectorXd eigenvalues = solver.eigenvalues();
409
-
410
- // Find min and max eigenvalues
411
- if (p > 0) {
412
- min_vals.push_back(eigenvalues(0));
413
- max_vals.push_back(eigenvalues(p-1));
414
- }
415
- }
416
-
417
- // Average over seeds for stability
418
- if (!min_vals.empty() && !max_vals.empty()) {
419
- double min_sum = 0.0, max_sum = 0.0;
420
- for (double val : min_vals) min_sum += val;
421
- for (double val : max_vals) max_sum += val;
422
-
423
- min_eig_ptr[i] = min_sum / min_vals.size();
424
- max_eig_ptr[i] = max_sum / max_vals.size();
425
- } else {
426
- min_eig_ptr[i] = std::numeric_limits<double>::quiet_NaN();
427
- max_eig_ptr[i] = std::numeric_limits<double>::quiet_NaN();
428
- }
429
- }
430
-
431
- return std::make_tuple(min_eigenvalues, max_eigenvalues);
432
- }
433
-
434
- PYBIND11_MODULE(cubic_cpp, m) {
435
- m.doc() = "C++ implementation of cubic root analysis functions";
436
-
437
- m.def("sweep_beta_and_find_z_bounds", &sweep_beta_and_find_z_bounds,
438
- "Sweep beta values and find z boundary values",
439
- py::arg("z_a"), py::arg("y"), py::arg("z_min"), py::arg("z_max"),
440
- py::arg("beta_steps"), py::arg("z_steps"));
441
-
442
- m.def("compute_high_y_curve", &compute_high_y_curve,
443
- "Compute High y Expression curve",
444
- py::arg("betas"), py::arg("z_a"), py::arg("y"));
445
-
446
- m.def("compute_alternate_low_expr", &compute_alternate_low_expr,
447
- "Compute alternative low expression",
448
- py::arg("betas"), py::arg("z_a"), py::arg("y"));
449
-
450
- m.def("compute_max_k_expression", &compute_max_k_expression,
451
- "Compute max k expression",
452
- py::arg("betas"), py::arg("z_a"), py::arg("y"), py::arg("k_samples")=1000);
453
-
454
- m.def("compute_min_t_expression", &compute_min_t_expression,
455
- "Compute min t expression",
456
- py::arg("betas"), py::arg("z_a"), py::arg("y"), py::arg("t_samples")=1000);
457
-
458
- m.def("compute_eigenvalue_support_boundaries", &compute_eigenvalue_support_boundaries,
459
- "Compute eigenvalue support boundaries",
460
- py::arg("z_a"), py::arg("y"), py::arg("beta_values"),
461
- py::arg("n_samples")=100, py::arg("seeds")=5);
462
- }
463
- '''
464
-
465
- # Function to build and load the C++ extension
466
- @st.cache_resource
467
- def build_cpp_extension():
468
- try:
469
- # Create temporary directory
470
- temp_dir = tempfile.mkdtemp()
471
-
472
- # Write C++ code to a file
473
- cpp_file = os.path.join(temp_dir, "cubic_cpp.cpp")
474
- with open(cpp_file, "w") as f:
475
- f.write(CPP_CODE)
476
-
477
- # Check if pybind11 and Eigen are installed
478
- try:
479
- import pybind11
480
- pybind11_include = pybind11.get_include()
481
- except ImportError:
482
- # Install pybind11 if not available
483
- subprocess.check_call([sys.executable, "-m", "pip", "install", "pybind11"])
484
- import pybind11
485
- pybind11_include = pybind11.get_include()
486
-
487
- # Try to find Eigen or download it
488
- eigen_include = os.path.join(temp_dir, "eigen")
489
- if not os.path.exists(eigen_include):
490
- os.makedirs(eigen_include)
491
- # Download Eigen headers (just the minimal required parts)
492
- subprocess.check_call(["wget", "https://gitlab.com/libeigen/eigen/-/archive/3.4.0/eigen-3.4.0.tar.gz", "-O", os.path.join(temp_dir, "eigen.tar.gz")])
493
- subprocess.check_call(["tar", "-xzf", os.path.join(temp_dir, "eigen.tar.gz"), "-C", temp_dir])
494
- # Move Eigen headers to the include directory
495
- eigen_src = os.path.join(temp_dir, "eigen-3.4.0")
496
- for folder in ["Eigen", "unsupported"]:
497
- if os.path.exists(os.path.join(eigen_src, folder)):
498
- shutil.copytree(os.path.join(eigen_src, folder), os.path.join(eigen_include, folder))
499
-
500
- # Build the extension module
501
- setup_py = os.path.join(temp_dir, "setup.py")
502
- with open(setup_py, "w") as f:
503
- f.write(f'''
504
- from setuptools import setup, Extension
505
- import pybind11
506
- import os
507
-
508
- ext_modules = [
509
- Extension(
510
- 'cubic_cpp',
511
- ['cubic_cpp.cpp'],
512
- include_dirs=[
513
- pybind11.get_include(),
514
- os.path.dirname(os.path.abspath(__file__))
515
- ],
516
- language='c++'
517
- )
518
- ]
519
-
520
- setup(
521
- name='cubic_cpp',
522
- ext_modules=ext_modules,
523
- py_modules=[],
524
- )
525
- ''')
526
-
527
- # Build the extension in place
528
- subprocess.check_call([sys.executable, setup_py, "build_ext", "--inplace"], cwd=temp_dir)
529
-
530
- # Find the compiled module
531
- extension_path = None
532
- for file in os.listdir(temp_dir):
533
- if file.startswith("cubic_cpp") and file.endswith(".so"):
534
- extension_path = os.path.join(temp_dir, file)
535
- break
536
-
537
- if extension_path is None:
538
- st.warning("Failed to find the compiled C++ extension")
539
- return None
540
-
541
- # Load the module
542
- spec = importlib.util.spec_from_file_location("cubic_cpp", extension_path)
543
- cubic_cpp = importlib.util.module_from_spec(spec)
544
- spec.loader.exec_module(cubic_cpp)
545
-
546
- return cubic_cpp
547
- except Exception as e:
548
- st.warning(f"Failed to build C++ extension: {str(e)}")
549
- return None
550
-
551
- # Try to build and load the C++ extension
552
- cubic_cpp = build_cpp_extension()
553
-
554
  def add_sqrt_support(expr_str):
555
  """Replace 'sqrt(' with 'sp.sqrt(' for sympy compatibility"""
556
  return expr_str.replace('sqrt(', 'sp.sqrt(')
@@ -583,12 +44,6 @@ def find_z_at_discriminant_zero(z_a, y, beta, z_min, z_max, steps):
583
  Scan z in [z_min, z_max] for sign changes in the discriminant,
584
  and return approximated roots (where the discriminant is zero).
585
  """
586
- # Use C++ implementation if available
587
- if cubic_cpp is not None:
588
- roots = np.array(cubic_cpp.find_z_at_discriminant_zero(z_a, y, beta, z_min, z_max, steps))
589
- return roots
590
-
591
- # Python fallback implementation
592
  # Apply the condition for y
593
  y_effective = y if y > 1 else 1/y
594
 
@@ -625,13 +80,6 @@ def sweep_beta_and_find_z_bounds(z_a, y, z_min, z_max, beta_steps, z_steps):
625
  for which the discriminant is zero.
626
  Returns: betas, lower z*(β) values, and upper z*(β) values.
627
  """
628
- # Use C++ implementation if available
629
- if cubic_cpp is not None:
630
- betas, z_min_values, z_max_values = cubic_cpp.sweep_beta_and_find_z_bounds(
631
- z_a, y, z_min, z_max, beta_steps, z_steps)
632
- return np.array(betas), np.array(z_min_values), np.array(z_max_values)
633
-
634
- # Python fallback implementation
635
  betas = np.linspace(0, 1, beta_steps)
636
  z_min_values = []
637
  z_max_values = []
@@ -651,13 +99,6 @@ def compute_eigenvalue_support_boundaries(z_a, y, beta_values, n_samples=100, se
651
  Compute the support boundaries of the eigenvalue distribution by directly
652
  finding the minimum and maximum eigenvalues of B_n = S_n T_n for different beta values.
653
  """
654
- # Use C++ implementation if available
655
- if cubic_cpp is not None:
656
- min_eigenvalues, max_eigenvalues = cubic_cpp.compute_eigenvalue_support_boundaries(
657
- z_a, y, beta_values, n_samples, seeds)
658
- return np.array(min_eigenvalues), np.array(max_eigenvalues)
659
-
660
- # Python fallback implementation
661
  # Apply the condition for y
662
  y_effective = y if y > 1 else 1/y
663
 
@@ -725,12 +166,6 @@ def compute_high_y_curve(betas, z_a, y):
725
  """
726
  Compute the "High y Expression" curve.
727
  """
728
- # Use C++ implementation if available
729
- if cubic_cpp is not None:
730
- curve = cubic_cpp.compute_high_y_curve(betas, z_a, y)
731
- return np.array(curve)
732
-
733
- # Python fallback implementation
734
  # Apply the condition for y
735
  y_effective = y if y > 1 else 1/y
736
 
@@ -748,12 +183,6 @@ def compute_alternate_low_expr(betas, z_a, y):
748
  Compute the alternate low expression:
749
  (z_a*y*beta*(z_a-1) - 2*z_a*(1-y) - 2*z_a**2) / (2+2*z_a)
750
  """
751
- # Use C++ implementation if available
752
- if cubic_cpp is not None:
753
- curve = cubic_cpp.compute_alternate_low_expr(betas, z_a, y)
754
- return np.array(curve)
755
-
756
- # Python fallback implementation
757
  # Apply the condition for y
758
  y_effective = y if y > 1 else 1/y
759
 
@@ -765,12 +194,6 @@ def compute_max_k_expression(betas, z_a, y, k_samples=1000):
765
  """
766
  Compute max_{k ∈ (0,∞)} (y*beta*(a-1)*k + (a*k+1)*((y-1)*k-1)) / ((a*k+1)*(k^2+k))
767
  """
768
- # Use C++ implementation if available
769
- if cubic_cpp is not None:
770
- curve = cubic_cpp.compute_max_k_expression(betas, z_a, y, k_samples)
771
- return np.array(curve)
772
-
773
- # Python fallback implementation
774
  # Apply the condition for y
775
  y_effective = y if y > 1 else 1/y
776
 
@@ -802,12 +225,6 @@ def compute_min_t_expression(betas, z_a, y, t_samples=1000):
802
  """
803
  Compute min_{t ∈ (-1/a, 0)} (y*beta*(a-1)*t + (a*t+1)*((y-1)*t-1)) / ((a*t+1)*(t^2+t))
804
  """
805
- # Use C++ implementation if available
806
- if cubic_cpp is not None:
807
- curve = cubic_cpp.compute_min_t_expression(betas, z_a, y, t_samples)
808
- return np.array(curve)
809
-
810
- # Python fallback implementation
811
  # Apply the condition for y
812
  y_effective = y if y > 1 else 1/y
813
 
@@ -1071,126 +488,905 @@ def generate_z_vs_beta_plot(z_a, y, z_min, z_max, beta_steps, z_steps,
1071
  )
1072
  return fig
1073
 
1074
- # Rest of your code for other functions and tabs...
1075
- # [...]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1076
 
1077
- # ----- Tab 1: z*(β) Curves -----
1078
- st.title("Cubic Root Analysis")
1079
- st.header("Eigenvalue Support Boundaries")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1080
 
1081
- # Cleaner layout with better column organization
1082
- col1, col2, col3 = st.columns([1, 1, 2])
 
 
 
 
 
 
 
 
 
 
 
 
1083
 
1084
- with col1:
1085
- z_a_1 = st.number_input("z_a", value=1.0, key="z_a_1")
1086
- y_1 = st.number_input("y", value=1.0, key="y_1")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1087
 
1088
- with col2:
1089
- z_min_1 = st.number_input("z_min", value=-10.0, key="z_min_1")
1090
- z_max_1 = st.number_input("z_max", value=10.0, key="z_max_1")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1091
 
1092
- with col1:
1093
- method_type = st.radio(
1094
- "Calculation Method",
1095
- ["Eigenvalue Method", "Discriminant Method"],
1096
- index=0 # Default to eigenvalue method
1097
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1098
 
1099
- # Advanced settings in collapsed expanders
1100
- with st.expander("Method Settings", expanded=False):
1101
- if method_type == "Eigenvalue Method":
1102
- beta_steps = st.slider("β steps", min_value=21, max_value=101, value=51, step=10,
1103
- key="beta_steps_eigen")
1104
- n_samples = st.slider("Matrix size (n)", min_value=100, max_value=2000, value=1000,
1105
- step=100)
1106
- seeds = st.slider("Number of seeds", min_value=1, max_value=10, value=5, step=1)
1107
- else:
1108
- beta_steps = st.slider("β steps", min_value=51, max_value=501, value=201, step=50,
1109
- key="beta_steps")
1110
- z_steps = st.slider("z grid steps", min_value=1000, max_value=100000, value=50000,
1111
- step=1000, key="z_steps")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1112
 
1113
- # Curve visibility options
1114
- with st.expander("Curve Visibility", expanded=False):
1115
- col_vis1, col_vis2 = st.columns(2)
1116
- with col_vis1:
1117
- show_high_y = st.checkbox("Show High y Expression", value=False, key="show_high_y")
1118
- show_max_k = st.checkbox("Show Max k Expression", value=True, key="show_max_k")
1119
- with col_vis2:
1120
- show_low_y = st.checkbox("Show Low y Expression", value=False, key="show_low_y")
1121
- show_min_t = st.checkbox("Show Min t Expression", value=True, key="show_min_t")
1122
 
1123
- # Custom expressions collapsed by default
1124
- with st.expander("Custom Expression 1 (s-based)", expanded=False):
1125
- st.markdown("""Enter expressions for s = numerator/denominator
1126
- (using variables `y`, `beta`, `z_a`, and `sqrt()`)""")
1127
- st.latex(r"\text{This s will be inserted into:}")
1128
- 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})}")
1129
- s_num = st.text_input("s numerator", value="", key="s_num")
1130
- s_denom = st.text_input("s denominator", value="", key="s_denom")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1131
 
1132
- with st.expander("Custom Expression 2 (direct z(β))", expanded=False):
1133
- st.markdown("""Enter direct expression for z(β) = numerator/denominator
1134
- (using variables `y`, `beta`, `z_a`, and `sqrt()`)""")
1135
- z_num = st.text_input("z(β) numerator", value="", key="z_num")
1136
- z_denom = st.text_input("z(β) denominator", value="", key="z_denom")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1137
 
1138
- # Move show_derivatives to main UI level for better visibility
1139
- with col2:
1140
- show_derivatives = st.checkbox("Show derivatives", value=False)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1141
 
1142
- # Compute button
1143
- if st.button("Compute Curves", key="tab1_button"):
1144
- with col3:
1145
- use_eigenvalue_method = (method_type == "Eigenvalue Method")
1146
- if use_eigenvalue_method:
1147
- fig = generate_z_vs_beta_plot(z_a_1, y_1, z_min_1, z_max_1, beta_steps, None,
1148
- s_num, s_denom, z_num, z_denom, show_derivatives,
1149
- show_high_y, show_low_y, show_max_k, show_min_t,
1150
- use_eigenvalue_method=True, n_samples=n_samples,
1151
- seeds=seeds)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1152
  else:
1153
- fig = generate_z_vs_beta_plot(z_a_1, y_1, z_min_1, z_max_1, beta_steps, z_steps,
1154
- s_num, s_denom, z_num, z_denom, show_derivatives,
1155
- show_high_y, show_low_y, show_max_k, show_min_t,
1156
- use_eigenvalue_method=False)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1157
 
1158
- if fig is not None:
1159
- st.plotly_chart(fig, use_container_width=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1160
 
1161
- # Curve explanations in collapsed expander
1162
- with st.expander("Curve Explanations", expanded=False):
1163
- if use_eigenvalue_method:
1164
- st.markdown("""
1165
- - **Upper/Lower Bounds** (Blue): Maximum/minimum eigenvalues of B_n = S_n T_n
1166
- - **Shaded Region**: Eigenvalue support region
1167
- - **High y Expression** (Green): Asymptotic approximation for high y values
1168
- - **Low Expression** (Orange): Alternative asymptotic expression
1169
- - **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)}$
1170
- - **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)}$
1171
- - **Custom Expression 1** (Magenta): Result from user-defined s substituted into the main formula
1172
- - **Custom Expression 2** (Brown): Direct z(β) expression
1173
- """)
1174
- else:
1175
- st.markdown("""
1176
- - **Upper z*(β)** (Blue): Maximum z value where discriminant is zero
1177
- - **Lower z*(β)** (Blue): Minimum z value where discriminant is zero
1178
- - **High y Expression** (Green): Asymptotic approximation for high y values
1179
- - **Low Expression** (Orange): Alternative asymptotic expression
1180
- - **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)}$
1181
- - **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)}$
1182
- - **Custom Expression 1** (Magenta): Result from user-defined s substituted into the main formula
1183
- - **Custom Expression 2** (Brown): Direct z(β) expression
1184
- """)
1185
- if show_derivatives:
1186
- st.markdown("""
1187
- Derivatives are shown as:
1188
- - Dashed lines: First derivatives (d/dβ)
1189
- - Dotted lines: Second derivatives (d²/dβ²)
1190
- """)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1191
 
1192
- # Display C++ build status
1193
- if cubic_cpp is None:
1194
- st.warning("C++ extension could not be built. Using Python implementation.")
1195
- else:
1196
- st.success("C++ extension successfully built and loaded.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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(
 
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(')
 
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
 
 
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 = []
 
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
 
 
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
 
 
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
 
 
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
 
 
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
 
 
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
+ """)