File size: 41,483 Bytes
dd5ca7d
1126f78
 
3940b3d
 
f84f14b
1126f78
 
13b3948
06d8fdb
1aa7fa4
aeff02a
80ed07e
7795d08
1126f78
aeff02a
 
80ed07e
1aa7fa4
aeff02a
1126f78
aeff02a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7795d08
 
aeff02a
7795d08
 
 
 
 
 
 
 
aeff02a
7795d08
 
 
aeff02a
e906ec1
 
 
 
 
 
aeff02a
 
 
 
7795d08
1126f78
a2a65af
 
 
 
1126f78
cb0df60
 
 
 
7795d08
 
 
 
cb0df60
7795d08
 
 
aeff02a
 
 
 
 
 
3940b3d
aeff02a
 
 
 
 
 
 
3940b3d
aeff02a
 
 
 
3940b3d
aeff02a
 
 
3940b3d
aeff02a
 
 
7795d08
06d8fdb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7795d08
 
 
 
 
 
 
359367f
7795d08
 
 
aeff02a
7795d08
 
 
 
 
 
 
 
aeff02a
7795d08
 
 
aeff02a
7795d08
 
 
 
 
 
 
 
 
 
aeff02a
7795d08
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
06d8fdb
 
 
aeff02a
7795d08
 
 
 
 
 
 
 
 
 
 
aeff02a
7795d08
 
aeff02a
7795d08
 
 
 
 
 
 
 
aeff02a
7795d08
06d8fdb
7795d08
 
 
 
 
aeff02a
06d8fdb
7795d08
 
 
 
 
 
 
 
 
 
 
 
 
06d8fdb
 
 
7795d08
06d8fdb
 
 
 
 
 
 
7795d08
 
06d8fdb
 
 
 
 
 
 
 
 
7795d08
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
06d8fdb
 
7795d08
 
 
 
 
 
aeff02a
 
 
 
 
 
 
 
 
 
f84f14b
 
aeff02a
f84f14b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
aeff02a
f84f14b
 
 
 
 
 
 
 
 
 
 
 
 
 
aeff02a
f84f14b
 
 
 
 
 
 
 
 
 
 
 
 
 
aeff02a
f84f14b
 
 
 
 
 
 
 
 
 
 
 
 
 
aeff02a
f84f14b
 
 
7795d08
f84f14b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7795d08
f84f14b
aeff02a
f84f14b
 
7795d08
 
 
e906ec1
7795d08
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
06d8fdb
 
 
7795d08
e906ec1
7795d08
 
e906ec1
7795d08
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f84f14b
7795d08
 
 
aeff02a
06d8fdb
7795d08
 
 
 
 
 
 
 
 
aeff02a
06d8fdb
7795d08
06d8fdb
aeff02a
06d8fdb
 
 
 
 
 
 
7795d08
 
 
06d8fdb
 
 
 
 
 
 
 
7795d08
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e906ec1
7795d08
 
 
 
e906ec1
7795d08
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
06d8fdb
 
7795d08
 
 
 
 
 
 
 
aeff02a
7795d08
 
 
 
 
aeff02a
7795d08
 
aeff02a
7795d08
 
 
 
 
 
 
 
 
aeff02a
7795d08
 
 
 
 
 
 
 
aeff02a
7795d08
 
 
 
 
 
 
 
aeff02a
7795d08
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
aeff02a
7795d08
 
 
aeff02a
7795d08
 
 
 
 
 
e906ec1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
import streamlit as st
import subprocess
import os
import json
import numpy as np
import plotly.graph_objects as go
from PIL import Image
import time
import io
import sys

# Set page config with wider layout
st.set_page_config(
    page_title="Matrix Analysis Dashboard",
    page_icon="📊",
    layout="wide",
    initial_sidebar_state="expanded"
)

# Apply custom CSS for a dashboard-like appearance
st.markdown("""
<style>
    .main-header {
        font-size: 2.5rem;
        color: #1E88E5;
        text-align: center;
        margin-bottom: 1rem;
        padding-bottom: 1rem;
        border-bottom: 2px solid #f0f0f0;
    }
    .dashboard-container {
        background-color: #f9f9f9;
        padding: 1.5rem;
        border-radius: 10px;
        box-shadow: 0 2px 5px rgba(0,0,0,0.1);
        margin-bottom: 1.5rem;
    }
    .panel-header {
        font-size: 1.3rem;
        font-weight: bold;
        margin-bottom: 1rem;
        color: #424242;
        border-left: 4px solid #1E88E5;
        padding-left: 10px;
    }
    .stTabs [data-baseweb="tab-list"] {
        gap: 12px;
    }
    .stTabs [data-baseweb="tab"] {
        height: 50px;
        white-space: pre-wrap;
        background-color: #f0f0f0;
        border-radius: 6px 6px 0 0;
        gap: 1;
        padding-top: 10px;
        padding-bottom: 10px;
    }
    .stTabs [aria-selected="true"] {
        background-color: #1E88E5 !important;
        color: white !important;
    }
    .math-box {
        background-color: #f8f9fa;
        border-left: 3px solid #1E88E5;
        padding: 10px;
        margin: 10px 0;
    }
</style>
""", unsafe_allow_html=True)

# Dashboard Header
st.markdown('<h1 class="main-header">Matrix Analysis Dashboard</h1>', unsafe_allow_html=True)

# Create output directory in the current working directory
current_dir = os.getcwd()
output_dir = os.path.join(current_dir, "output")
os.makedirs(output_dir, exist_ok=True)

# Compile the C++ code at runtime
cpp_file = os.path.join(current_dir, "app.cpp")
executable = os.path.join(current_dir, "eigen_analysis")

# Check if cpp file exists and compile if necessary
if not os.path.exists(cpp_file):
    st.error(f"C++ source file not found at: {cpp_file}")
    st.stop()

# Compile the C++ code with the right OpenCV libraries
if not os.path.exists(executable) or st.sidebar.button("Recompile C++ Code"):
    with st.sidebar:
        with st.spinner("Compiling C++ code..."):
            compile_commands = [
                f"g++ -o {executable} {cpp_file} `pkg-config --cflags --libs opencv4` -std=c++11",
                f"g++ -o {executable} {cpp_file} `pkg-config --cflags --libs opencv` -std=c++11",
                f"g++ -o {executable} {cpp_file} -I/usr/include/opencv4 -lopencv_core -lopencv_imgproc -std=c++11"
            ]
            
            compiled = False
            for cmd in compile_commands:
                compile_result = subprocess.run(
                    cmd, 
                    shell=True,
                    capture_output=True,
                    text=True
                )
                
                if compile_result.returncode == 0:
                    compiled = True
                    break
            
            if not compiled:
                st.error("All compilation attempts failed. Please check the system requirements.")
                st.stop()
            
            # Make sure the executable is executable
            os.chmod(executable, 0o755)
            st.success("C++ code compiled successfully")

# Helper function for running commands with better debugging
def run_command(cmd, show_output=True):
    cmd_str = " ".join(cmd)
    if show_output:
        st.code(f"Running command: {cmd_str}", language="bash")
    
    process = subprocess.Popen(
        cmd,
        stdout=subprocess.PIPE,
        stderr=subprocess.PIPE,
        text=True
    )
    
    stdout, stderr = process.communicate()
    
    if process.returncode != 0:
        if show_output:
            st.error(f"Command failed with return code {process.returncode}")
            st.error(f"Command: {cmd_str}")
            st.error(f"Error output: {stderr}")
        return False, stdout, stderr
    
    return True, stdout, stderr

# Create tabs for different analyses
tab1, tab2 = st.tabs(["Eigenvalue Analysis", "Im(s) vs z Analysis"])

# Tab 1: Eigenvalue Analysis
with tab1:
    # Two-column layout for the dashboard
    left_column, right_column = st.columns([1, 3])
    
    with left_column:
        st.markdown('<div class="dashboard-container">', unsafe_allow_html=True)
        st.markdown('<div class="panel-header">Eigenvalue Analysis Controls</div>', unsafe_allow_html=True)
        
        # Parameter inputs with defaults and validation
        st.markdown("### Matrix Parameters")
        n = st.number_input("Sample size (n)", min_value=5, max_value=1000, value=100, step=5, 
                           help="Number of samples", key="eig_n")
        p = st.number_input("Dimension (p)", min_value=5, max_value=1000, value=50, step=5, 
                           help="Dimensionality", key="eig_p")
        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", key="eig_a")
        
        # Automatically calculate y = p/n (as requested)
        y = p/n
        st.info(f"Value for y = p/n: {y:.4f}")
        
        st.markdown("### Calculation Controls")
        fineness = st.slider(
            "Beta points", 
            min_value=20, 
            max_value=500, 
            value=100, 
            step=10,
            help="Number of points to calculate along the β axis (0 to 1)",
            key="eig_fineness"
        )
        
        with st.expander("Advanced Settings"):
            # Add controls for theoretical calculation precision
            theory_grid_points = st.slider(
                "Theoretical grid points", 
                min_value=100, 
                max_value=1000, 
                value=200, 
                step=50,
                help="Number of points in initial grid search for theoretical calculations",
                key="eig_grid_points"
            )
            
            theory_tolerance = st.number_input(
                "Theoretical tolerance", 
                min_value=1e-12, 
                max_value=1e-6, 
                value=1e-10, 
                format="%.1e",
                help="Convergence tolerance for golden section search",
                key="eig_tolerance"
            )
            
            # Debug mode
            debug_mode = st.checkbox("Debug Mode", value=False, key="eig_debug")
        
        # Generate button
        eig_generate_button = st.button("Generate Eigenvalue Analysis", 
                                      type="primary", 
                                      use_container_width=True,
                                      key="eig_generate")
        st.markdown('</div>', unsafe_allow_html=True)
    
    with right_column:
        # Main visualization area
        st.markdown('<div class="dashboard-container">', unsafe_allow_html=True)
        st.markdown('<div class="panel-header">Eigenvalue Analysis Results</div>', unsafe_allow_html=True)
        
        # Container for the analysis results
        eig_results_container = st.container()
        
        # Process when generate button is clicked
        if eig_generate_button:
            with eig_results_container:
                # Show progress
                progress_container = st.container()
                with progress_container:
                    progress_bar = st.progress(0)
                    status_text = st.empty()
                
                try:
                    # Create data file path
                    data_file = os.path.join(output_dir, "eigenvalue_data.json")
                    
                    # Delete previous output if exists
                    if os.path.exists(data_file):
                        os.remove(data_file)
                    
                    # Build command for eigenvalue analysis
                    cmd = [
                        executable,
                        "eigenvalues",
                        str(n), 
                        str(p), 
                        str(a), 
                        str(y), 
                        str(fineness), 
                        str(theory_grid_points),
                        str(theory_tolerance),
                        data_file
                    ]
                    
                    # Run the command with our helper function
                    status_text.text("Running eigenvalue analysis...")
                    success, stdout, stderr = run_command(cmd, debug_mode)
                    
                    if not success:
                        st.error("Eigenvalue analysis failed. Please check the debug output.")
                        if debug_mode:
                            st.text("Command output:")
                            st.code(stdout)
                            st.text("Error output:")
                            st.code(stderr)
                    else:
                        progress_bar.progress(1.0)
                        status_text.text("Analysis complete! Generating visualization...")
                        
                        # Check if the output file was created
                        if not os.path.exists(data_file):
                            st.error(f"Output file not created: {data_file}")
                            if debug_mode:
                                st.text("Command output:")
                                st.code(stdout)
                            st.stop()
                        
                        # Load the results from the JSON file
                        with open(data_file, 'r') as f:
                            data = json.load(f)
                        
                        # Extract data
                        beta_values = np.array(data['beta_values'])
                        max_eigenvalues = np.array(data['max_eigenvalues'])
                        min_eigenvalues = np.array(data['min_eigenvalues'])
                        theoretical_max = np.array(data['theoretical_max'])
                        theoretical_min = np.array(data['theoretical_min'])
                        
                        # Create an interactive plot using Plotly
                        fig = go.Figure()
                        
                        # Add traces for each line
                        fig.add_trace(go.Scatter(
                            x=beta_values, 
                            y=max_eigenvalues,
                            mode='lines+markers',
                            name='Empirical Max Eigenvalue',
                            line=dict(color='rgb(220, 60, 60)', width=3),
                            marker=dict(
                                symbol='circle',
                                size=8,
                                color='rgb(220, 60, 60)',
                                line=dict(color='white', width=1)
                            ),
                            hovertemplate='β: %{x:.3f}<br>Value: %{y:.6f}<extra>Empirical Max</extra>'
                        ))
                        
                        fig.add_trace(go.Scatter(
                            x=beta_values, 
                            y=min_eigenvalues,
                            mode='lines+markers',
                            name='Empirical Min Eigenvalue',
                            line=dict(color='rgb(60, 60, 220)', width=3),
                            marker=dict(
                                symbol='circle',
                                size=8,
                                color='rgb(60, 60, 220)',
                                line=dict(color='white', width=1)
                            ),
                            hovertemplate='β: %{x:.3f}<br>Value: %{y:.6f}<extra>Empirical Min</extra>'
                        ))
                        
                        fig.add_trace(go.Scatter(
                            x=beta_values, 
                            y=theoretical_max,
                            mode='lines+markers',
                            name='Theoretical Max Function',
                            line=dict(color='rgb(30, 180, 30)', width=3),
                            marker=dict(
                                symbol='diamond',
                                size=8,
                                color='rgb(30, 180, 30)',
                                line=dict(color='white', width=1)
                            ),
                            hovertemplate='β: %{x:.3f}<br>Value: %{y:.6f}<extra>Theoretical Max</extra>'
                        ))
                        
                        fig.add_trace(go.Scatter(
                            x=beta_values, 
                            y=theoretical_min,
                            mode='lines+markers',
                            name='Theoretical Min Function',
                            line=dict(color='rgb(180, 30, 180)', width=3),
                            marker=dict(
                                symbol='diamond',
                                size=8,
                                color='rgb(180, 30, 180)',
                                line=dict(color='white', width=1)
                            ),
                            hovertemplate='β: %{x:.3f}<br>Value: %{y:.6f}<extra>Theoretical Min</extra>'
                        ))
                        
                        # Configure layout for better appearance
                        fig.update_layout(
                            title={
                                'text': f'Eigenvalue Analysis: n={n}, p={p}, a={a}, y={y:.4f}',
                                'font': {'size': 24, 'color': '#1E88E5'},
                                'y': 0.95,
                                'x': 0.5,
                                'xanchor': 'center',
                                'yanchor': 'top'
                            },
                            xaxis={
                                'title': 'β Parameter',
                                'titlefont': {'size': 18, 'color': '#424242'},
                                'tickfont': {'size': 14},
                                'gridcolor': 'rgba(220, 220, 220, 0.5)',
                                'showgrid': True
                            },
                            yaxis={
                                'title': 'Eigenvalues',
                                'titlefont': {'size': 18, 'color': '#424242'},
                                'tickfont': {'size': 14},
                                'gridcolor': 'rgba(220, 220, 220, 0.5)',
                                'showgrid': True
                            },
                            plot_bgcolor='rgba(240, 240, 240, 0.8)',
                            paper_bgcolor='rgba(249, 249, 249, 0.8)',
                            hovermode='closest',
                            legend={
                                'font': {'size': 14},
                                'bgcolor': 'rgba(255, 255, 255, 0.9)',
                                'bordercolor': 'rgba(200, 200, 200, 0.5)',
                                'borderwidth': 1
                            },
                            margin={'l': 60, 'r': 30, 't': 100, 'b': 60},
                            height=600,
                            annotations=[
                                {
                                    'text': f"Max Function: max{{k ∈ (0,∞)}} [yβ(a-1)k + (ak+1)((y-1)k-1)]/[(ak+1)(k²+k)]",
                                    'xref': 'paper', 'yref': 'paper',
                                    'x': 0.02, 'y': 0.02,
                                    'showarrow': False,
                                    'font': {'size': 12, 'color': 'rgb(30, 180, 30)'},
                                    'bgcolor': 'rgba(255, 255, 255, 0.9)',
                                    'bordercolor': 'rgb(30, 180, 30)',
                                    'borderwidth': 1,
                                    'borderpad': 4
                                },
                                {
                                    'text': f"Min Function: min{{t ∈ (-1/a,0)}} [yβ(a-1)t + (at+1)((y-1)t-1)]/[(at+1)(t²+t)]",
                                    'xref': 'paper', 'yref': 'paper',
                                    'x': 0.55, 'y': 0.02,
                                    'showarrow': False,
                                    'font': {'size': 12, 'color': 'rgb(180, 30, 180)'},
                                    'bgcolor': 'rgba(255, 255, 255, 0.9)',
                                    'bordercolor': 'rgb(180, 30, 180)',
                                    'borderwidth': 1,
                                    'borderpad': 4
                                }
                            ]
                        )
                        
                        # Add custom modebar buttons
                        fig.update_layout(
                            modebar_add=[
                                'drawline', 'drawopenpath', 'drawclosedpath',
                                'drawcircle', 'drawrect', 'eraseshape'
                            ],
                            modebar_remove=['lasso2d', 'select2d'],
                            dragmode='zoom'
                        )
                        
                        # Clear progress container
                        progress_container.empty()
                        
                        # Display the interactive plot in Streamlit
                        st.plotly_chart(fig, use_container_width=True)
                
                except Exception as e:
                    st.error(f"An error occurred: {str(e)}")
                    if debug_mode:
                        st.exception(e)
        
        else:
            # Try to load existing data if available
            data_file = os.path.join(output_dir, "eigenvalue_data.json")
            if os.path.exists(data_file):
                try:
                    with open(data_file, 'r') as f:
                        data = json.load(f)
                    
                    # Extract data
                    beta_values = np.array(data['beta_values'])
                    max_eigenvalues = np.array(data['max_eigenvalues'])
                    min_eigenvalues = np.array(data['min_eigenvalues'])
                    theoretical_max = np.array(data['theoretical_max'])
                    theoretical_min = np.array(data['theoretical_min'])
                    
                    # Create an interactive plot using Plotly
                    fig = go.Figure()
                    
                    # Add traces for each line
                    fig.add_trace(go.Scatter(
                        x=beta_values, 
                        y=max_eigenvalues,
                        mode='lines+markers',
                        name='Empirical Max Eigenvalue',
                        line=dict(color='rgb(220, 60, 60)', width=3),
                        marker=dict(
                            symbol='circle',
                            size=8,
                            color='rgb(220, 60, 60)',
                            line=dict(color='white', width=1)
                        ),
                        hovertemplate='β: %{x:.3f}<br>Value: %{y:.6f}<extra>Empirical Max</extra>'
                    ))
                    
                    fig.add_trace(go.Scatter(
                        x=beta_values, 
                        y=min_eigenvalues,
                        mode='lines+markers',
                        name='Empirical Min Eigenvalue',
                        line=dict(color='rgb(60, 60, 220)', width=3),
                        marker=dict(
                            symbol='circle',
                            size=8,
                            color='rgb(60, 60, 220)',
                            line=dict(color='white', width=1)
                        ),
                        hovertemplate='β: %{x:.3f}<br>Value: %{y:.6f}<extra>Empirical Min</extra>'
                    ))
                    
                    fig.add_trace(go.Scatter(
                        x=beta_values, 
                        y=theoretical_max,
                        mode='lines+markers',
                        name='Theoretical Max Function',
                        line=dict(color='rgb(30, 180, 30)', width=3),
                        marker=dict(
                            symbol='diamond',
                            size=8,
                            color='rgb(30, 180, 30)',
                            line=dict(color='white', width=1)
                        ),
                        hovertemplate='β: %{x:.3f}<br>Value: %{y:.6f}<extra>Theoretical Max</extra>'
                    ))
                    
                    fig.add_trace(go.Scatter(
                        x=beta_values, 
                        y=theoretical_min,
                        mode='lines+markers',
                        name='Theoretical Min Function',
                        line=dict(color='rgb(180, 30, 180)', width=3),
                        marker=dict(
                            symbol='diamond',
                            size=8,
                            color='rgb(180, 30, 180)',
                            line=dict(color='white', width=1)
                        ),
                        hovertemplate='β: %{x:.3f}<br>Value: %{y:.6f}<extra>Theoretical Min</extra>'
                    ))
                    
                    # Configure layout for better appearance
                    fig.update_layout(
                        title={
                            'text': f'Eigenvalue Analysis (Previous Result)',
                            'font': {'size': 24, 'color': '#1E88E5'},
                            'y': 0.95,
                            'x': 0.5,
                            'xanchor': 'center',
                            'yanchor': 'top'
                        },
                        xaxis={
                            'title': 'β Parameter',
                            'titlefont': {'size': 18, 'color': '#424242'},
                            'tickfont': {'size': 14},
                            'gridcolor': 'rgba(220, 220, 220, 0.5)',
                            'showgrid': True
                        },
                        yaxis={
                            'title': 'Eigenvalues',
                            'titlefont': {'size': 18, 'color': '#424242'},
                            'tickfont': {'size': 14},
                            'gridcolor': 'rgba(220, 220, 220, 0.5)',
                            'showgrid': True
                        },
                        plot_bgcolor='rgba(240, 240, 240, 0.8)',
                        paper_bgcolor='rgba(249, 249, 249, 0.8)',
                        hovermode='closest',
                        legend={
                            'font': {'size': 14},
                            'bgcolor': 'rgba(255, 255, 255, 0.9)',
                            'bordercolor': 'rgba(200, 200, 200, 0.5)',
                            'borderwidth': 1
                        },
                        margin={'l': 60, 'r': 30, 't': 100, 'b': 60},
                        height=600
                    )
                    
                    # Display the interactive plot in Streamlit
                    st.plotly_chart(fig, use_container_width=True)
                    st.info("This is the previous analysis result. Adjust parameters and click 'Generate Analysis' to create a new visualization.")
                    
                except Exception as e:
                    st.info("👈 Set parameters and click 'Generate Eigenvalue Analysis' to create a visualization.")
            else:
                # Show placeholder
                st.info("👈 Set parameters and click 'Generate Eigenvalue Analysis' to create a visualization.")
        
        st.markdown('</div>', unsafe_allow_html=True)

# Tab 2: Im(s) vs z Analysis
with tab2:
    # Two-column layout for the dashboard
    left_column, right_column = st.columns([1, 3])
    
    with left_column:
        st.markdown('<div class="dashboard-container">', unsafe_allow_html=True)
        st.markdown('<div class="panel-header">Im(s) vs z Analysis Controls</div>', unsafe_allow_html=True)
        
        # Parameter inputs with defaults and validation
        st.markdown("### Cubic Equation Parameters")
        cubic_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", key="cubic_a")
        cubic_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", key="cubic_y")
        cubic_beta = st.number_input("Value for β", min_value=0.0, max_value=1.0, value=0.5, step=0.05,
                                   help="Value between 0 and 1", key="cubic_beta")
        
        st.markdown("### Calculation Controls")
        cubic_points = st.slider(
            "Number of z points", 
            min_value=50, 
            max_value=1000, 
            value=300, 
            step=50,
            help="Number of points to calculate along the z axis",
            key="cubic_points"
        )
        
        # Debug mode
        cubic_debug_mode = st.checkbox("Debug Mode", value=False, key="cubic_debug")
        
        # Show cubic equation
        st.markdown('<div class="math-box">', unsafe_allow_html=True)
        st.markdown("### Cubic Equation")
        st.latex(r"zas^3 + [z(a+1)+a(1-y)]\,s^2 + [z+(a+1)-y-y\beta (a-1)]\,s + 1 = 0")
        st.markdown('</div>', unsafe_allow_html=True)
        
        # Generate button
        cubic_generate_button = st.button("Generate Im(s) vs z Analysis", 
                                        type="primary", 
                                        use_container_width=True,
                                        key="cubic_generate")
        st.markdown('</div>', unsafe_allow_html=True)
    
    with right_column:
        # Main visualization area
        st.markdown('<div class="dashboard-container">', unsafe_allow_html=True)
        st.markdown('<div class="panel-header">Im(s) vs z Analysis Results</div>', unsafe_allow_html=True)
        
        # Container for the analysis results
        cubic_results_container = st.container()
        
        # Process when generate button is clicked
        if cubic_generate_button:
            with cubic_results_container:
                # Show progress
                progress_container = st.container()
                with progress_container:
                    status_text = st.empty()
                    status_text.text("Starting cubic equation calculations...")
                
                try:
                    # Run the C++ executable with the parameters in JSON output mode
                    data_file = os.path.join(output_dir, "cubic_data.json")
                    
                    # Delete previous output if exists
                    if os.path.exists(data_file):
                        os.remove(data_file)
                    
                    # Build command for cubic equation analysis
                    cmd = [
                        executable,
                        "cubic",
                        str(cubic_a), 
                        str(cubic_y), 
                        str(cubic_beta), 
                        str(cubic_points),
                        data_file
                    ]
                    
                    # Run the command with our helper function
                    status_text.text("Calculating Im(s) vs z values...")
                    success, stdout, stderr = run_command(cmd, cubic_debug_mode)
                    
                    if not success:
                        st.error("Cubic equation analysis failed. Please check the debug output.")
                        if cubic_debug_mode:
                            st.text("Command output:")
                            st.code(stdout)
                            st.text("Error output:")
                            st.code(stderr)
                    else:
                        status_text.text("Calculations complete! Generating visualization...")
                        
                        # Check if the output file was created
                        if not os.path.exists(data_file):
                            st.error(f"Output file not created: {data_file}")
                            if cubic_debug_mode:
                                st.text("Command output:")
                                st.code(stdout)
                            st.stop()
                        
                        # Load the results from the JSON file
                        with open(data_file, 'r') as f:
                            data = json.load(f)
                        
                        # Extract data
                        z_values = np.array(data['z_values'])
                        ims_values1 = np.array(data['ims_values1'])
                        ims_values2 = np.array(data['ims_values2'])
                        ims_values3 = np.array(data['ims_values3'])
                        
                        # Create an interactive plot using Plotly
                        fig = go.Figure()
                        
                        # Add traces for each root's imaginary part
                        fig.add_trace(go.Scatter(
                            x=z_values, 
                            y=ims_values1,
                            mode='lines',
                            name='Im(s₁)',
                            line=dict(color='rgb(220, 60, 60)', width=3),
                            hovertemplate='z: %{x:.3f}<br>Im(s₁): %{y:.6f}<extra>Root 1</extra>'
                        ))
                        
                        fig.add_trace(go.Scatter(
                            x=z_values, 
                            y=ims_values2,
                            mode='lines',
                            name='Im(s₂)',
                            line=dict(color='rgb(60, 60, 220)', width=3),
                            hovertemplate='z: %{x:.3f}<br>Im(s₂): %{y:.6f}<extra>Root 2</extra>'
                        ))
                        
                        fig.add_trace(go.Scatter(
                            x=z_values, 
                            y=ims_values3,
                            mode='lines',
                            name='Im(s₃)',
                            line=dict(color='rgb(30, 180, 30)', width=3),
                            hovertemplate='z: %{x:.3f}<br>Im(s₃): %{y:.6f}<extra>Root 3</extra>'
                        ))
                        
                        # Configure layout for better appearance
                        fig.update_layout(
                            title={
                                'text': f'Im(s) vs z Analysis: a={cubic_a}, y={cubic_y}, β={cubic_beta}',
                                'font': {'size': 24, 'color': '#1E88E5'},
                                'y': 0.95,
                                'x': 0.5,
                                'xanchor': 'center',
                                'yanchor': 'top'
                            },
                            xaxis={
                                'title': 'z (logarithmic scale)',
                                'titlefont': {'size': 18, 'color': '#424242'},
                                'tickfont': {'size': 14},
                                'gridcolor': 'rgba(220, 220, 220, 0.5)',
                                'showgrid': True,
                                'type': 'log'  # Use logarithmic scale for better visualization
                            },
                            yaxis={
                                'title': 'Im(s)',
                                'titlefont': {'size': 18, 'color': '#424242'},
                                'tickfont': {'size': 14},
                                'gridcolor': 'rgba(220, 220, 220, 0.5)',
                                'showgrid': True
                            },
                            plot_bgcolor='rgba(240, 240, 240, 0.8)',
                            paper_bgcolor='rgba(249, 249, 249, 0.8)',
                            hovermode='closest',
                            legend={
                                'font': {'size': 14},
                                'bgcolor': 'rgba(255, 255, 255, 0.9)',
                                'bordercolor': 'rgba(200, 200, 200, 0.5)',
                                'borderwidth': 1
                            },
                            margin={'l': 60, 'r': 30, 't': 100, 'b': 60},
                            height=600,
                            annotations=[
                                {
                                    'text': f"Cubic Equation: {cubic_a}zs³ + [{cubic_a+1}z+{cubic_a}(1-{cubic_y})]s² + [z+{cubic_a+1}-{cubic_y}-{cubic_y*cubic_beta}({cubic_a-1})]s + 1 = 0",
                                    'xref': 'paper', 'yref': 'paper',
                                    'x': 0.5, 'y': 0.02,
                                    'showarrow': False,
                                    'font': {'size': 12, 'color': 'black'},
                                    'bgcolor': 'rgba(255, 255, 255, 0.9)',
                                    'bordercolor': 'rgba(0, 0, 0, 0.5)',
                                    'borderwidth': 1,
                                    'borderpad': 4,
                                    'align': 'center'
                                }
                            ]
                        )
                        
                        # Add custom modebar buttons
                        fig.update_layout(
                            modebar_add=[
                                'drawline', 'drawopenpath', 'drawclosedpath',
                                'drawcircle', 'drawrect', 'eraseshape'
                            ],
                            modebar_remove=['lasso2d', 'select2d'],
                            dragmode='zoom'
                        )
                        
                        # Clear progress container
                        progress_container.empty()
                        
                        # Display the interactive plot in Streamlit
                        st.plotly_chart(fig, use_container_width=True)
                        
                        # Add explanation text
                        st.markdown("""
                        ### Explanation of the Analysis
                        
                        This plot shows the imaginary parts of the three roots (s₁, s₂, s₃) of the cubic equation as a function of z. 
                        The cubic equation being solved is:
                        
                        ```
                        zas³ + [z(a+1)+a(1-y)]s² + [z+(a+1)-y-yβ(a-1)]s + 1 = 0
                        ```
                        
                        Where a, y, and β are parameters you can adjust in the control panel. The imaginary parts of the roots represent 
                        oscillatory behavior in the system.
                        
                        - When Im(s) = 0, the root is purely real
                        - When Im(s) ≠ 0, the root has an oscillatory component
                        """)
                
                except Exception as e:
                    st.error(f"An error occurred: {str(e)}")
                    if cubic_debug_mode:
                        st.exception(e)
        
        else:
            # Try to load existing data if available
            data_file = os.path.join(output_dir, "cubic_data.json")
            if os.path.exists(data_file):
                try:
                    with open(data_file, 'r') as f:
                        data = json.load(f)
                    
                    # Extract data
                    z_values = np.array(data['z_values'])
                    ims_values1 = np.array(data['ims_values1'])
                    ims_values2 = np.array(data['ims_values2'])
                    ims_values3 = np.array(data['ims_values3'])
                    
                    # Create an interactive plot using Plotly
                    fig = go.Figure()
                    
                    # Add traces for each root's imaginary part
                    fig.add_trace(go.Scatter(
                        x=z_values, 
                        y=ims_values1,
                        mode='lines',
                        name='Im(s₁)',
                        line=dict(color='rgb(220, 60, 60)', width=3),
                        hovertemplate='z: %{x:.3f}<br>Im(s₁): %{y:.6f}<extra>Root 1</extra>'
                    ))
                    
                    fig.add_trace(go.Scatter(
                        x=z_values, 
                        y=ims_values2,
                        mode='lines',
                        name='Im(s₂)',
                        line=dict(color='rgb(60, 60, 220)', width=3),
                        hovertemplate='z: %{x:.3f}<br>Im(s₂): %{y:.6f}<extra>Root 2</extra>'
                    ))
                    
                    fig.add_trace(go.Scatter(
                        x=z_values, 
                        y=ims_values3,
                        mode='lines',
                        name='Im(s₃)',
                        line=dict(color='rgb(30, 180, 30)', width=3),
                        hovertemplate='z: %{x:.3f}<br>Im(s₃): %{y:.6f}<extra>Root 3</extra>'
                    ))
                    
                    # Configure layout for better appearance
                    fig.update_layout(
                        title={
                            'text': f'Im(s) vs z Analysis (Previous Result)',
                            'font': {'size': 24, 'color': '#1E88E5'},
                            'y': 0.95,
                            'x': 0.5,
                            'xanchor': 'center',
                            'yanchor': 'top'
                        },
                        xaxis={
                            'title': 'z (logarithmic scale)',
                            'titlefont': {'size': 18, 'color': '#424242'},
                            'tickfont': {'size': 14},
                            'gridcolor': 'rgba(220, 220, 220, 0.5)',
                            'showgrid': True,
                            'type': 'log'  # Use logarithmic scale for better visualization
                        },
                        yaxis={
                            'title': 'Im(s)',
                            'titlefont': {'size': 18, 'color': '#424242'},
                            'tickfont': {'size': 14},
                            'gridcolor': 'rgba(220, 220, 220, 0.5)',
                            'showgrid': True
                        },
                        plot_bgcolor='rgba(240, 240, 240, 0.8)',
                        paper_bgcolor='rgba(249, 249, 249, 0.8)',
                        hovermode='closest',
                        legend={
                            'font': {'size': 14},
                            'bgcolor': 'rgba(255, 255, 255, 0.9)',
                            'bordercolor': 'rgba(200, 200, 200, 0.5)',
                            'borderwidth': 1
                        },
                        margin={'l': 60, 'r': 30, 't': 100, 'b': 60},
                        height=600
                    )
                    
                    # Display the interactive plot in Streamlit
                    st.plotly_chart(fig, use_container_width=True)
                    st.info("This is the previous analysis result. Adjust parameters and click 'Generate Analysis' to create a new visualization.")
                    
                except Exception as e:
                    st.info("👈 Set parameters and click 'Generate Im(s) vs z Analysis' to create a visualization.")
            else:
                # Show placeholder
                st.info("👈 Set parameters and click 'Generate Im(s) vs z Analysis' to create a visualization.")
        
        st.markdown('</div>', unsafe_allow_html=True)

# Add footer with information
with st.expander("About this Application"):
    st.markdown("""
    ## Matrix Analysis Dashboard
    
    This application provides tools for analyzing matrix properties and related cubic equations:
    
    ### Tab 1: Eigenvalue Analysis
    Visualizes the relationship between empirical and theoretical eigenvalues of matrices as a function of β.
    
    ### Tab 2: Im(s) vs z Analysis
    Explores the imaginary parts of the roots of the cubic equation:
    ```
    zas³ + [z(a+1)+a(1-y)]s² + [z+(a+1)-y-yβ(a-1)]s + 1 = 0
    ```
    
    The application uses C++ for high-performance numerical calculations and Python with Streamlit and Plotly for the interactive user interface and visualizations.
    """)