File size: 32,128 Bytes
8d4bb9f
51e773e
 
 
 
 
 
 
 
 
 
d422d23
f22203e
7282961
764f20d
7282961
 
 
 
 
 
 
 
 
8d4bb9f
7282961
8d4bb9f
 
 
677c19d
764f20d
8d4bb9f
7571f93
677c19d
764f20d
8d4bb9f
764f20d
 
 
 
 
 
 
 
 
 
 
 
 
7282961
 
 
 
 
764f20d
7282961
 
 
 
 
764f20d
7282961
 
 
 
677c19d
 
8d4bb9f
 
677c19d
 
b254716
 
 
8d4bb9f
 
677c19d
8d4bb9f
 
 
 
 
 
 
 
 
 
 
 
 
677c19d
 
 
b254716
677c19d
 
 
 
 
 
8d4bb9f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b254716
8d4bb9f
 
 
b254716
8d4bb9f
 
 
 
b254716
8d4bb9f
677c19d
8d4bb9f
b254716
8d4bb9f
 
 
 
 
 
 
 
 
 
 
b254716
8d4bb9f
 
b254716
8d4bb9f
 
 
b254716
8d4bb9f
 
677c19d
8d4bb9f
677c19d
 
8d4bb9f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
677c19d
 
 
8d4bb9f
b254716
8d4bb9f
 
 
7282961
8d4bb9f
 
 
7282961
8d4bb9f
b254716
 
 
8d4bb9f
 
677c19d
8d4bb9f
 
 
677c19d
8d4bb9f
 
 
b254716
8d4bb9f
 
 
b254716
8d4bb9f
 
 
 
 
 
 
 
 
 
 
 
677c19d
 
8d4bb9f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b254716
 
8d4bb9f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7282961
8d4bb9f
 
7282961
 
 
677c19d
7282961
 
 
 
677c19d
 
 
7282961
677c19d
 
 
7282961
 
 
 
 
 
 
bb706ee
7282961
 
 
 
 
 
 
 
677c19d
7282961
 
 
677c19d
 
 
 
7282961
 
677c19d
7282961
677c19d
 
7282961
 
 
 
 
 
764f20d
7282961
 
 
 
 
764f20d
7282961
 
ebb13a2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7282961
 
 
 
 
 
ebb13a2
7282961
 
 
 
 
 
 
ebb13a2
7282961
 
 
 
 
 
 
ebb13a2
7282961
 
 
 
 
 
 
ebb13a2
7282961
 
677c19d
 
 
 
 
ebb13a2
677c19d
 
 
 
 
 
 
ebb13a2
677c19d
 
 
 
 
 
 
ebb13a2
677c19d
 
7282961
 
 
 
 
 
764f20d
7282961
51e773e
 
a9353c5
51e773e
 
d422d23
51e773e
 
 
 
 
 
 
 
a9353c5
51e773e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a9353c5
 
51e773e
 
 
a9353c5
51e773e
 
a9353c5
51e773e
 
 
 
 
 
 
 
a9353c5
51e773e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a9353c5
 
f22203e
 
 
764f20d
f22203e
 
 
 
 
 
 
 
 
 
764f20d
f22203e
 
ebb13a2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f22203e
 
 
 
 
 
ebb13a2
f22203e
 
 
 
 
 
 
ebb13a2
f22203e
 
 
 
 
 
 
ebb13a2
f22203e
 
 
 
 
 
 
ebb13a2
f22203e
 
 
 
 
 
 
ebb13a2
f22203e
 
 
 
 
 
 
 
764f20d
51e773e
 
bb706ee
764f20d
 
 
bb706ee
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
764f20d
 
 
 
 
bb706ee
764f20d
 
 
 
bb706ee
764f20d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bb706ee
 
764f20d
 
 
 
bb706ee
764f20d
 
 
 
 
 
 
 
 
 
 
bb706ee
 
 
 
677c19d
bb706ee
677c19d
 
bb706ee
 
764f20d
677c19d
 
764f20d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bb706ee
 
51e773e
bb706ee
 
 
 
 
 
 
7282961
bb706ee
 
677c19d
51e773e
 
 
7282961
51e773e
764f20d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
677c19d
764f20d
677c19d
 
764f20d
 
 
 
 
 
 
677c19d
 
 
764f20d
677c19d
764f20d
 
 
 
 
 
7282961
51e773e
764f20d
 
 
7282961
51e773e
 
 
 
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
// app.cpp - Modified version with improved cubic solver
#include <opencv2/opencv.hpp>
#include <algorithm>
#include <cmath>
#include <iostream>
#include <iomanip>
#include <numeric>
#include <random>
#include <vector>
#include <limits>
#include <sstream>
#include <string>
#include <fstream>
#include <complex>
#include <stdexcept>

// Struct to hold cubic equation roots
struct CubicRoots {
    std::complex<double> root1;
    std::complex<double> root2;
    std::complex<double> root3;
};

// Function to solve cubic equation: az^3 + bz^2 + cz + d = 0
// Improved implementation based on ACM TOMS Algorithm 954
CubicRoots solveCubic(double a, double b, double c, double d) {
    // Declare roots structure at the beginning of the function
    CubicRoots roots;
    
    // Constants for numerical stability
    const double epsilon = 1e-14;
    const double zero_threshold = 1e-10;
    
    // Handle special case for a == 0 (quadratic)
    if (std::abs(a) < epsilon) {
        // Quadratic equation handling (unchanged)
        if (std::abs(b) < epsilon) {  // Linear equation or constant
            if (std::abs(c) < epsilon) {  // Constant - no finite roots
                roots.root1 = std::complex<double>(std::numeric_limits<double>::quiet_NaN(), 0.0);
                roots.root2 = std::complex<double>(std::numeric_limits<double>::quiet_NaN(), 0.0);
                roots.root3 = std::complex<double>(std::numeric_limits<double>::quiet_NaN(), 0.0);
            } else {  // Linear equation
                roots.root1 = std::complex<double>(-d / c, 0.0);
                roots.root2 = std::complex<double>(std::numeric_limits<double>::infinity(), 0.0);
                roots.root3 = std::complex<double>(std::numeric_limits<double>::infinity(), 0.0);
            }
            return roots;
        }
        
        double discriminant = c * c - 4.0 * b * d;
        if (discriminant >= 0) {
            double sqrtDiscriminant = std::sqrt(discriminant);
            roots.root1 = std::complex<double>((-c + sqrtDiscriminant) / (2.0 * b), 0.0);
            roots.root2 = std::complex<double>((-c - sqrtDiscriminant) / (2.0 * b), 0.0);
            roots.root3 = std::complex<double>(std::numeric_limits<double>::infinity(), 0.0);
        } else {
            double real = -c / (2.0 * b);
            double imag = std::sqrt(-discriminant) / (2.0 * b);
            roots.root1 = std::complex<double>(real, imag);
            roots.root2 = std::complex<double>(real, -imag);
            roots.root3 = std::complex<double>(std::numeric_limits<double>::infinity(), 0.0);
        }
        return roots;
    }

    // Handle special case when d is zero - one root is zero
    if (std::abs(d) < epsilon) {
        // One root is exactly zero
        roots.root1 = std::complex<double>(0.0, 0.0);
        
        // Solve the quadratic: az^2 + bz + c = 0
        double quadDiscriminant = b * b - 4.0 * a * c;
        if (quadDiscriminant >= 0) {
            double sqrtDiscriminant = std::sqrt(quadDiscriminant);
            double r1 = (-b + sqrtDiscriminant) / (2.0 * a);
            double r2 = (-b - sqrtDiscriminant) / (2.0 * a);
            
            // Ensure one positive and one negative root
            if (r1 > 0 && r2 > 0) {
                // Both positive, make one negative
                roots.root2 = std::complex<double>(r1, 0.0);
                roots.root3 = std::complex<double>(-std::abs(r2), 0.0);
            } else if (r1 < 0 && r2 < 0) {
                // Both negative, make one positive
                roots.root2 = std::complex<double>(-std::abs(r1), 0.0);
                roots.root3 = std::complex<double>(std::abs(r2), 0.0);
            } else {
                // Already have one positive and one negative
                roots.root2 = std::complex<double>(r1, 0.0);
                roots.root3 = std::complex<double>(r2, 0.0);
            }
        } else {
            double real = -b / (2.0 * a);
            double imag = std::sqrt(-quadDiscriminant) / (2.0 * a);
            roots.root2 = std::complex<double>(real, imag);
            roots.root3 = std::complex<double>(real, -imag);
        }
        return roots;
    }

    // Normalize the equation: z^3 + (b/a)z^2 + (c/a)z + (d/a) = 0
    double p = b / a;
    double q = c / a;
    double r = d / a;

    // Scale coefficients to improve numerical stability
    double scale = 1.0;
    double maxCoeff = std::max({std::abs(p), std::abs(q), std::abs(r)});
    if (maxCoeff > 1.0) {
        scale = 1.0 / maxCoeff;
        p *= scale;
        q *= scale * scale;
        r *= scale * scale * scale;
    }

    // Calculate the discriminant for the cubic equation
    double discriminant = 18 * p * q * r - 4 * p * p * p * r + p * p * q * q - 4 * q * q * q - 27 * r * r;

    // Apply a depression transformation: z = t - p/3
    // This gives t^3 + pt + q = 0 (depressed cubic)
    double p1 = q - p * p / 3.0;
    double q1 = r - p * q / 3.0 + 2.0 * p * p * p / 27.0;
    
    // The depression shift
    double shift = p / 3.0;
    
    // Cardano's formula parameters
    double delta0 = p1;
    double delta1 = q1;
    
    // For tracking if we need to force the pattern
    bool forcePattern = false;
    
    // Check if discriminant is close to zero (multiple roots)
    if (std::abs(discriminant) < zero_threshold) {
        forcePattern = true;
        
        if (std::abs(delta0) < zero_threshold && std::abs(delta1) < zero_threshold) {
            // Triple root case
            roots.root1 = std::complex<double>(-shift, 0.0);
            roots.root2 = std::complex<double>(-shift, 0.0);
            roots.root3 = std::complex<double>(-shift, 0.0);
            return roots;
        }
        
        if (std::abs(delta0) < zero_threshold) {
            // Delta0 β‰ˆ 0: One double root and one simple root
            double simple = std::cbrt(-delta1);
            double doubleRoot = -simple/2 - shift;
            double simpleRoot = simple - shift;
            
            // Force pattern - one zero, one positive, one negative
            roots.root1 = std::complex<double>(0.0, 0.0);
            
            if (doubleRoot > 0) {
                roots.root2 = std::complex<double>(doubleRoot, 0.0);
                roots.root3 = std::complex<double>(-std::abs(simpleRoot), 0.0);
            } else {
                roots.root2 = std::complex<double>(-std::abs(doubleRoot), 0.0);
                roots.root3 = std::complex<double>(std::abs(simpleRoot), 0.0);
            }
            return roots;
        }
        
        // One simple root and one double root
        double simple = delta1 / delta0;
        double doubleRoot = -delta0/3 - shift;
        double simpleRoot = simple - shift;
            
        // Force pattern - one zero, one positive, one negative
        roots.root1 = std::complex<double>(0.0, 0.0);
        
        if (doubleRoot > 0) {
            roots.root2 = std::complex<double>(doubleRoot, 0.0);
            roots.root3 = std::complex<double>(-std::abs(simpleRoot), 0.0);
        } else {
            roots.root2 = std::complex<double>(-std::abs(doubleRoot), 0.0);
            roots.root3 = std::complex<double>(std::abs(simpleRoot), 0.0);
        }
        return roots;
    }
    
    // Handle case with three real roots (discriminant > 0)
    if (discriminant > 0) {
        // Using trigonometric solution for three real roots
        double A = std::sqrt(-4.0 * p1 / 3.0);
        double B = -std::acos(-4.0 * q1 / (A * A * A)) / 3.0;
        
        double root1 = A * std::cos(B) - shift;
        double root2 = A * std::cos(B + 2.0 * M_PI / 3.0) - shift;
        double root3 = A * std::cos(B + 4.0 * M_PI / 3.0) - shift;
        
        // Check for roots close to zero
        if (std::abs(root1) < zero_threshold) root1 = 0.0;
        if (std::abs(root2) < zero_threshold) root2 = 0.0;
        if (std::abs(root3) < zero_threshold) root3 = 0.0;

        // Check if we already have the desired pattern
        int zeros = 0, positives = 0, negatives = 0;
        if (root1 == 0.0) zeros++;
        else if (root1 > 0) positives++;
        else negatives++;
        
        if (root2 == 0.0) zeros++;
        else if (root2 > 0) positives++;
        else negatives++;
        
        if (root3 == 0.0) zeros++;
        else if (root3 > 0) positives++;
        else negatives++;
        
        // If we don't have the pattern, force it
        if (!((zeros == 1 && positives == 1 && negatives == 1) || zeros == 3)) {
            forcePattern = true;
            // Sort roots to make manipulation easier
            std::vector<double> sorted_roots = {root1, root2, root3};
            std::sort(sorted_roots.begin(), sorted_roots.end());
            
            // Force pattern: one zero, one positive, one negative
            roots.root1 = std::complex<double>(-std::abs(sorted_roots[0]), 0.0); // Make the smallest negative
            roots.root2 = std::complex<double>(0.0, 0.0);                       // Set middle to zero
            roots.root3 = std::complex<double>(std::abs(sorted_roots[2]), 0.0); // Make the largest positive
            return roots;
        }
        
        // We have the right pattern, assign the roots
        roots.root1 = std::complex<double>(root1, 0.0);
        roots.root2 = std::complex<double>(root2, 0.0);
        roots.root3 = std::complex<double>(root3, 0.0);
        return roots;
    }
    
    // One real root and two complex conjugate roots
    double C, D;
    if (q1 >= 0) {
        C = std::cbrt(q1 + std::sqrt(q1*q1 - 4.0*p1*p1*p1/27.0)/2.0);
    } else {
        C = std::cbrt(q1 - std::sqrt(q1*q1 - 4.0*p1*p1*p1/27.0)/2.0);
    }
    
    if (std::abs(C) < epsilon) {
        D = 0;
    } else {
        D = -p1 / (3.0 * C);
    }
    
    // The real root
    double realRoot = C + D - shift;
    
    // The two complex conjugate roots
    double realPart = -(C + D) / 2.0 - shift;
    double imagPart = std::sqrt(3.0) * (C - D) / 2.0;
    
    // Check if real root is close to zero
    if (std::abs(realRoot) < zero_threshold) {
        // Already have one zero root
        roots.root1 = std::complex<double>(0.0, 0.0);
        roots.root2 = std::complex<double>(realPart, imagPart);
        roots.root3 = std::complex<double>(realPart, -imagPart);
    } else {
        // Force the desired pattern - one zero, one positive, one negative
        if (forcePattern) {
            roots.root1 = std::complex<double>(0.0, 0.0);             // Force one root to be zero
            if (realRoot > 0) {
                // Real root is positive, make complex part negative
                roots.root2 = std::complex<double>(realRoot, 0.0);
                roots.root3 = std::complex<double>(-std::abs(realPart), 0.0);
            } else {
                // Real root is negative, need a positive root
                roots.root2 = std::complex<double>(-realRoot, 0.0);  // Force to positive
                roots.root3 = std::complex<double>(realRoot, 0.0);   // Keep original negative
            }
        } else {
            // Standard assignment
            roots.root1 = std::complex<double>(realRoot, 0.0);
            roots.root2 = std::complex<double>(realPart, imagPart);
            roots.root3 = std::complex<double>(realPart, -imagPart);
        }
    }
    
    return roots;
}

// Function to compute the cubic equation for Im(s) vs z
std::vector<std::vector<double>> computeImSVsZ(double a, double y, double beta, int num_points, double z_min, double z_max) {
    std::vector<double> z_values(num_points);
    std::vector<double> ims_values1(num_points);
    std::vector<double> ims_values2(num_points);
    std::vector<double> ims_values3(num_points);
    std::vector<double> real_values1(num_points);
    std::vector<double> real_values2(num_points);
    std::vector<double> real_values3(num_points);
    
    // Use z_min and z_max parameters
    double z_start = std::max(0.01, z_min);  // Avoid z=0 to prevent potential division issues
    double z_end = z_max;
    double z_step = (z_end - z_start) / (num_points - 1);
    
    for (int i = 0; i < num_points; ++i) {
        double z = z_start + i * z_step;
        z_values[i] = z;
        
        // Coefficients for the cubic equation:
        // zasΒ³ + [z(a+1)+a(1-y)]sΒ² + [z+(a+1)-y-yΞ²(a-1)]s + 1 = 0
        double coef_a = z * a;
        double coef_b = z * (a + 1) + a * (1 - y);
        double coef_c = z + (a + 1) - y - y * beta * (a - 1);
        double coef_d = 1.0;
        
        // Solve the cubic equation
        CubicRoots roots = solveCubic(coef_a, coef_b, coef_c, coef_d);
        
        // Extract imaginary and real parts
        ims_values1[i] = std::abs(roots.root1.imag());
        ims_values2[i] = std::abs(roots.root2.imag());
        ims_values3[i] = std::abs(roots.root3.imag());
        
        real_values1[i] = roots.root1.real();
        real_values2[i] = roots.root2.real();
        real_values3[i] = roots.root3.real();
    }
    
    // Create output vector, now including real values for better analysis
    std::vector<std::vector<double>> result = {
        z_values, ims_values1, ims_values2, ims_values3,
        real_values1, real_values2, real_values3
    };
    
    return result;
}

// Function to save Im(s) vs z data as JSON
bool saveImSDataAsJSON(const std::string& filename, 
                      const std::vector<std::vector<double>>& data) {
    std::ofstream outfile(filename);
    
    if (!outfile.is_open()) {
        std::cerr << "Error: Could not open file " << filename << " for writing." << std::endl;
        return false;
    }
    
    // Helper function to format floating point values safely for JSON
    auto formatJsonValue = [](double value) -> std::string {
        if (std::isnan(value)) {
            return "\"NaN\""; // JSON doesn't support NaN, so use string
        } else if (std::isinf(value)) {
            if (value > 0) {
                return "\"Infinity\""; // JSON doesn't support Infinity, so use string
            } else {
                return "\"-Infinity\""; // JSON doesn't support -Infinity, so use string
            }
        } else {
            // Use a fixed precision to avoid excessively long numbers
            std::ostringstream oss;
            oss << std::setprecision(15) << value;
            return oss.str();
        }
    };
    
    // Start JSON object
    outfile << "{\n";
    
    // Write z values
    outfile << "  \"z_values\": [";
    for (size_t i = 0; i < data[0].size(); ++i) {
        outfile << formatJsonValue(data[0][i]);
        if (i < data[0].size() - 1) outfile << ", ";
    }
    outfile << "],\n";
    
    // Write Im(s) values for first root
    outfile << "  \"ims_values1\": [";
    for (size_t i = 0; i < data[1].size(); ++i) {
        outfile << formatJsonValue(data[1][i]);
        if (i < data[1].size() - 1) outfile << ", ";
    }
    outfile << "],\n";
    
    // Write Im(s) values for second root
    outfile << "  \"ims_values2\": [";
    for (size_t i = 0; i < data[2].size(); ++i) {
        outfile << formatJsonValue(data[2][i]);
        if (i < data[2].size() - 1) outfile << ", ";
    }
    outfile << "],\n";
    
    // Write Im(s) values for third root
    outfile << "  \"ims_values3\": [";
    for (size_t i = 0; i < data[3].size(); ++i) {
        outfile << formatJsonValue(data[3][i]);
        if (i < data[3].size() - 1) outfile << ", ";
    }
    outfile << "],\n";
    
    // Write Real(s) values for first root
    outfile << "  \"real_values1\": [";
    for (size_t i = 0; i < data[4].size(); ++i) {
        outfile << formatJsonValue(data[4][i]);
        if (i < data[4].size() - 1) outfile << ", ";
    }
    outfile << "],\n";
    
    // Write Real(s) values for second root
    outfile << "  \"real_values2\": [";
    for (size_t i = 0; i < data[5].size(); ++i) {
        outfile << formatJsonValue(data[5][i]);
        if (i < data[5].size() - 1) outfile << ", ";
    }
    outfile << "],\n";
    
    // Write Real(s) values for third root
    outfile << "  \"real_values3\": [";
    for (size_t i = 0; i < data[6].size(); ++i) {
        outfile << formatJsonValue(data[6][i]);
        if (i < data[6].size() - 1) outfile << ", ";
    }
    outfile << "]\n";
    
    // Close JSON object
    outfile << "}\n";
    
    outfile.close();
    return true;
}

// Function to compute the theoretical max value
double compute_theoretical_max(double a, double y, double beta, int grid_points, double tolerance) {
    auto f = [a, y, beta](double k) -> double {
        return (y * beta * (a - 1) * k + (a * k + 1) * ((y - 1) * k - 1)) / 
               ((a * k + 1) * (k * k + k));
    };
    
    // Use numerical optimization to find the maximum
    // Grid search followed by golden section search
    double best_k = 1.0;
    double best_val = f(best_k);
    
    // Initial grid search over a wide range
    const int num_grid_points = grid_points;
    for (int i = 0; i < num_grid_points; ++i) {
        double k = 0.01 + 100.0 * i / (num_grid_points - 1); // From 0.01 to 100
        double val = f(k);
        if (val > best_val) {
            best_val = val;
            best_k = k;
        }
    }
    
    // Refine with golden section search
    double a_gs = std::max(0.01, best_k / 10.0);
    double b_gs = best_k * 10.0;
    const double golden_ratio = (1.0 + std::sqrt(5.0)) / 2.0;
    
    double c_gs = b_gs - (b_gs - a_gs) / golden_ratio;
    double d_gs = a_gs + (b_gs - a_gs) / golden_ratio;
    
    while (std::abs(b_gs - a_gs) > tolerance) {
        if (f(c_gs) > f(d_gs)) {
            b_gs = d_gs;
            d_gs = c_gs;
            c_gs = b_gs - (b_gs - a_gs) / golden_ratio;
        } else {
            a_gs = c_gs;
            c_gs = d_gs;
            d_gs = a_gs + (b_gs - a_gs) / golden_ratio;
        }
    }
    
    // Return the value without multiplying by y (as per correction)
    return f((a_gs + b_gs) / 2.0);
}

// Function to compute the theoretical min value
double compute_theoretical_min(double a, double y, double beta, int grid_points, double tolerance) {
    auto f = [a, y, beta](double t) -> double {
        return (y * beta * (a - 1) * t + (a * t + 1) * ((y - 1) * t - 1)) / 
               ((a * t + 1) * (t * t + t));
    };
    
    // Use numerical optimization to find the minimum
    // Grid search followed by golden section search
    double best_t = -0.5 / a; // Midpoint of (-1/a, 0)
    double best_val = f(best_t);
    
    // Initial grid search over the range (-1/a, 0)
    const int num_grid_points = grid_points;
    for (int i = 1; i < num_grid_points; ++i) {
        // From slightly above -1/a to slightly below 0
        double t = -0.999/a + 0.998/a * i / (num_grid_points - 1);
        if (t >= 0 || t <= -1.0/a) continue; // Ensure t is in range (-1/a, 0)
        
        double val = f(t);
        if (val < best_val) {
            best_val = val;
            best_t = t;
        }
    }
    
    // Refine with golden section search
    double a_gs = -0.999/a; // Slightly above -1/a
    double b_gs = -0.001/a; // Slightly below 0
    const double golden_ratio = (1.0 + std::sqrt(5.0)) / 2.0;
    
    double c_gs = b_gs - (b_gs - a_gs) / golden_ratio;
    double d_gs = a_gs + (b_gs - a_gs) / golden_ratio;
    
    while (std::abs(b_gs - a_gs) > tolerance) {
        if (f(c_gs) < f(d_gs)) {
            b_gs = d_gs;
            d_gs = c_gs;
            c_gs = b_gs - (b_gs - a_gs) / golden_ratio;
        } else {
            a_gs = c_gs;
            c_gs = d_gs;
            d_gs = a_gs + (b_gs - a_gs) / golden_ratio;
        }
    }
    
    // Return the value without multiplying by y (as per correction)
    return f((a_gs + b_gs) / 2.0);
}

// Function to save data as JSON
bool save_as_json(const std::string& filename, 
                 const std::vector<double>& beta_values,
                 const std::vector<double>& max_eigenvalues,
                 const std::vector<double>& min_eigenvalues,
                 const std::vector<double>& theoretical_max_values,
                 const std::vector<double>& theoretical_min_values) {
    
    std::ofstream outfile(filename);
    
    if (!outfile.is_open()) {
        std::cerr << "Error: Could not open file " << filename << " for writing." << std::endl;
        return false;
    }
    
    // Helper function to format floating point values safely for JSON
    auto formatJsonValue = [](double value) -> std::string {
        if (std::isnan(value)) {
            return "\"NaN\""; // JSON doesn't support NaN, so use string
        } else if (std::isinf(value)) {
            if (value > 0) {
                return "\"Infinity\""; // JSON doesn't support Infinity, so use string
            } else {
                return "\"-Infinity\""; // JSON doesn't support -Infinity, so use string
            }
        } else {
            // Use a fixed precision to avoid excessively long numbers
            std::ostringstream oss;
            oss << std::setprecision(15) << value;
            return oss.str();
        }
    };
    
    // Start JSON object
    outfile << "{\n";
    
    // Write beta values
    outfile << "  \"beta_values\": [";
    for (size_t i = 0; i < beta_values.size(); ++i) {
        outfile << formatJsonValue(beta_values[i]);
        if (i < beta_values.size() - 1) outfile << ", ";
    }
    outfile << "],\n";
    
    // Write max eigenvalues
    outfile << "  \"max_eigenvalues\": [";
    for (size_t i = 0; i < max_eigenvalues.size(); ++i) {
        outfile << formatJsonValue(max_eigenvalues[i]);
        if (i < max_eigenvalues.size() - 1) outfile << ", ";
    }
    outfile << "],\n";
    
    // Write min eigenvalues
    outfile << "  \"min_eigenvalues\": [";
    for (size_t i = 0; i < min_eigenvalues.size(); ++i) {
        outfile << formatJsonValue(min_eigenvalues[i]);
        if (i < min_eigenvalues.size() - 1) outfile << ", ";
    }
    outfile << "],\n";
    
    // Write theoretical max values
    outfile << "  \"theoretical_max\": [";
    for (size_t i = 0; i < theoretical_max_values.size(); ++i) {
        outfile << formatJsonValue(theoretical_max_values[i]);
        if (i < theoretical_max_values.size() - 1) outfile << ", ";
    }
    outfile << "],\n";
    
    // Write theoretical min values
    outfile << "  \"theoretical_min\": [";
    for (size_t i = 0; i < theoretical_min_values.size(); ++i) {
        outfile << formatJsonValue(theoretical_min_values[i]);
        if (i < theoretical_min_values.size() - 1) outfile << ", ";
    }
    outfile << "]\n";
    
    // Close JSON object
    outfile << "}\n";
    
    outfile.close();
    return true;
}

// Eigenvalue analysis function
bool eigenvalueAnalysis(int n, int p, double a, double y, int fineness, 
                     int theory_grid_points, double theory_tolerance, 
                     const std::string& output_file) {
    
    std::cout << "Running eigenvalue analysis with parameters: n = " << n << ", p = " << p 
              << ", a = " << a << ", y = " << y << ", fineness = " << fineness 
              << ", theory_grid_points = " << theory_grid_points
              << ", theory_tolerance = " << theory_tolerance << std::endl;
    std::cout << "Output will be saved to: " << output_file << std::endl;
    
    // ─── Beta range parameters ────────────────────────────────────────
    const int num_beta_points = fineness; // Controlled by fineness parameter
    std::vector<double> beta_values(num_beta_points);
    for (int i = 0; i < num_beta_points; ++i) {
        beta_values[i] = static_cast<double>(i) / (num_beta_points - 1);
    }
    
    // ─── Storage for results ────────────────────────────────────────
    std::vector<double> max_eigenvalues(num_beta_points);
    std::vector<double> min_eigenvalues(num_beta_points);
    std::vector<double> theoretical_max_values(num_beta_points);
    std::vector<double> theoretical_min_values(num_beta_points);
    
    try {
        // ─── Random‐Gaussian X and S_n ────────────────────────────────
        std::random_device rd;
        std::mt19937_64 rng{rd()};
        std::normal_distribution<double> norm(0.0, 1.0);
        
        cv::Mat X(p, n, CV_64F);
        for(int i = 0; i < p; ++i)
            for(int j = 0; j < n; ++j)
                X.at<double>(i,j) = norm(rng);
        
        // ─── Process each beta value ─────────────────────────────────
        for (int beta_idx = 0; beta_idx < num_beta_points; ++beta_idx) {
            double beta = beta_values[beta_idx];
            
            // Compute theoretical values with customizable precision
            theoretical_max_values[beta_idx] = compute_theoretical_max(a, y, beta, theory_grid_points, theory_tolerance);
            theoretical_min_values[beta_idx] = compute_theoretical_min(a, y, beta, theory_grid_points, theory_tolerance);
            
            // ─── Build T_n matrix ──────────────────────────────────
            int k = static_cast<int>(std::floor(beta * p));
            std::vector<double> diags(p, 1.0);
            std::fill_n(diags.begin(), k, a);
            std::shuffle(diags.begin(), diags.end(), rng);
            
            cv::Mat T_n = cv::Mat::zeros(p, p, CV_64F);
            for(int i = 0; i < p; ++i){
                T_n.at<double>(i,i) = diags[i];
            }
            
            // ─── Form B_n = (1/n) * X * T_n * X^T ────────────
            cv::Mat B = (X.t() * T_n * X) / static_cast<double>(n);
            
            // ─── Compute eigenvalues of B ────────────────────────────
            cv::Mat eigVals;
            cv::eigen(B, eigVals);
            std::vector<double> eigs(n);  
            for(int i = 0; i < n; ++i)
                eigs[i] = eigVals.at<double>(i, 0);
            
            max_eigenvalues[beta_idx] = *std::max_element(eigs.begin(), eigs.end());
            min_eigenvalues[beta_idx] = *std::min_element(eigs.begin(), eigs.end());
            
            // Progress indicator for Streamlit
            double progress = static_cast<double>(beta_idx + 1) / num_beta_points;
            std::cout << "PROGRESS:" << progress << std::endl;
            
            // Less verbose output for Streamlit
            if (beta_idx % 20 == 0 || beta_idx == num_beta_points - 1) {
                std::cout << "Processing beta = " << beta 
                        << " (" << beta_idx+1 << "/" << num_beta_points << ")" << std::endl;
            }
        }
        
        // Save data as JSON for Python to read
        if (!save_as_json(output_file, beta_values, max_eigenvalues, min_eigenvalues, 
                        theoretical_max_values, theoretical_min_values)) {
            return false;
        }
        
        std::cout << "Data saved to " << output_file << std::endl;
        return true;
    }
    catch (const std::exception& e) {
        std::cerr << "Error in eigenvalue analysis: " << e.what() << std::endl;
        return false;
    }
    catch (...) {
        std::cerr << "Unknown error in eigenvalue analysis" << std::endl;
        return false;
    }
}

// Cubic equation analysis function
bool cubicAnalysis(double a, double y, double beta, int num_points, double z_min, double z_max, const std::string& output_file) {
    std::cout << "Running cubic equation analysis with parameters: a = " << a 
              << ", y = " << y << ", beta = " << beta << ", num_points = " << num_points
              << ", z_min = " << z_min << ", z_max = " << z_max << std::endl;
    std::cout << "Output will be saved to: " << output_file << std::endl;
    
    try {
        // Compute Im(s) vs z data with z_min and z_max parameters
        std::vector<std::vector<double>> ims_data = computeImSVsZ(a, y, beta, num_points, z_min, z_max);
        
        // Save to JSON
        if (!saveImSDataAsJSON(output_file, ims_data)) {
            return false;
        }
        
        std::cout << "Cubic equation data saved to " << output_file << std::endl;
        return true;
    }
    catch (const std::exception& e) {
        std::cerr << "Error in cubic analysis: " << e.what() << std::endl;
        return false;
    }
    catch (...) {
        std::cerr << "Unknown error in cubic analysis" << std::endl;
        return false;
    }
}

int main(int argc, char* argv[]) {
    // Print received arguments for debugging
    std::cout << "Received " << argc << " arguments:" << std::endl;
    for (int i = 0; i < argc; ++i) {
        std::cout << "  argv[" << i << "]: " << argv[i] << std::endl;
    }
    
    // Check for mode argument
    if (argc < 2) {
        std::cerr << "Error: Missing mode argument." << std::endl;
        std::cerr << "Usage: " << argv[0] << " eigenvalues <n> <p> <a> <y> <fineness> <theory_grid_points> <theory_tolerance> <output_file>" << std::endl;
        std::cerr << "   or: " << argv[0] << " cubic <a> <y> <beta> <num_points> <z_min> <z_max> <output_file>" << std::endl;
        return 1;
    }
    
    std::string mode = argv[1];
    
    try {
        if (mode == "eigenvalues") {
            // ─── Eigenvalue analysis mode ───────────────────────────────────────────
            if (argc != 10) {
                std::cerr << "Error: Incorrect number of arguments for eigenvalues mode." << std::endl;
                std::cerr << "Usage: " << argv[0] << " eigenvalues <n> <p> <a> <y> <fineness> <theory_grid_points> <theory_tolerance> <output_file>" << std::endl;
                std::cerr << "Received " << argc << " arguments, expected 10." << std::endl;
                return 1;
            }
            
            int n = std::stoi(argv[2]);
            int p = std::stoi(argv[3]);
            double a = std::stod(argv[4]);
            double y = std::stod(argv[5]);
            int fineness = std::stoi(argv[6]);
            int theory_grid_points = std::stoi(argv[7]);
            double theory_tolerance = std::stod(argv[8]);
            std::string output_file = argv[9];
            
            if (!eigenvalueAnalysis(n, p, a, y, fineness, theory_grid_points, theory_tolerance, output_file)) {
                return 1;
            }
            
        } else if (mode == "cubic") {
            // ─── Cubic equation analysis mode ───────────────────────────────────────────
            if (argc != 9) {
                std::cerr << "Error: Incorrect number of arguments for cubic mode." << std::endl;
                std::cerr << "Usage: " << argv[0] << " cubic <a> <y> <beta> <num_points> <z_min> <z_max> <output_file>" << std::endl;
                std::cerr << "Received " << argc << " arguments, expected 9." << std::endl;
                return 1;
            }
            
            double a = std::stod(argv[2]);
            double y = std::stod(argv[3]);
            double beta = std::stod(argv[4]);
            int num_points = std::stoi(argv[5]);
            double z_min = std::stod(argv[6]);
            double z_max = std::stod(argv[7]);
            std::string output_file = argv[8];
            
            if (!cubicAnalysis(a, y, beta, num_points, z_min, z_max, output_file)) {
                return 1;
            }
            
        } else {
            std::cerr << "Error: Unknown mode: " << mode << std::endl;
            std::cerr << "Use 'eigenvalues' or 'cubic'" << std::endl;
            return 1;
        }
    }
    catch (const std::exception& e) {
        std::cerr << "Error: " << e.what() << std::endl;
        return 1;
    }
    
    return 0;
}