scirs2-special 0.6.5

Special functions module for SciRS2 (scirs2-special)
Documentation
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
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
//! Computational Methods Laboratory
//!
//! An interactive laboratory for exploring numerical methods, computational challenges,
//! and advanced algorithms used in special function evaluation. This tutorial teaches
//! the practical side of special function computation.
//!
//! Topics covered:
//! - Numerical precision and stability
//! - Algorithm selection strategies  
//! - Performance optimization techniques
//! - Error analysis and validation
//! - Modern computational approaches
//!
//! Run with: cargo run --example computational_methods_laboratory

use scirs2_core::ndarray::Array1;
use scirs2_special::*;
use std::f64::consts::PI;
use std::io::{self, Write};
use std::time::Instant;

#[allow(dead_code)]
fn main() -> Result<(), Box<dyn std::error::Error>> {
    println!("🔬 Computational Methods Laboratory");
    println!("==================================");
    println!("Exploring the numerical side of special functions\n");

    loop {
        display_main_menu();
        let choice = get_user_input("Enter your choice (1-8, or 'q' to quit): ")?;

        if choice.to_lowercase() == "q" {
            println!("🧪 Thanks for using the Computational Methods Laboratory!");
            break;
        }

        match choice.parse::<u32>() {
            Ok(1) => precision_and_stability_analysis()?,
            Ok(2) => algorithm_selection_strategies()?,
            Ok(3) => performance_benchmarking()?,
            Ok(4) => series_vs_asymptotic_analysis()?,
            Ok(5) => continued_fractions_workshop()?,
            Ok(6) => simd_and_parallel_optimization()?,
            Ok(7) => arbitrary_precision_computing()?,
            Ok(8) => validation_and_testing_methods()?,
            _ => println!("❌ Invalid choice. Please try again.\n"),
        }
    }

    Ok(())
}

#[allow(dead_code)]
fn display_main_menu() {
    println!("🧮 Choose a computational topic to explore:");
    println!("1. 📏 Precision and Stability Analysis");
    println!("2. 🎯 Algorithm Selection Strategies");
    println!("3. ⚡ Performance Benchmarking");
    println!("4. 📊 Series vs Asymptotic Analysis");
    println!("5. 🔄 Continued Fractions Workshop");
    println!("6. 🚀 SIMD and Parallel Optimization");
    println!("7. 🔢 Arbitrary Precision Computing");
    println!("8. ✅ Validation and Testing Methods");
    println!("q. Quit");
    println!();
}

#[allow(dead_code)]
fn get_user_input(prompt: &str) -> io::Result<String> {
    print!("{prompt}");
    io::stdout().flush()?;
    let mut input = String::new();
    io::stdin().read_line(&mut input)?;
    Ok(input.trim().to_string())
}

#[allow(dead_code)]
fn precision_and_stability_analysis() -> Result<(), Box<dyn std::error::Error>> {
    println!("\n📏 PRECISION AND STABILITY ANALYSIS");
    println!("====================================\n");

    println!(
        "Understanding numerical precision is crucial for reliable special function computation."
    );
    println!("We'll analyze condition numbers, error propagation, and numerical stability.\n");

    pause_for_user()?;

    println!("1. CONDITION NUMBER ANALYSIS");
    println!("============================");
    println!();
    println!("The condition number κ(f,x) measures sensitivity to input perturbations:");
    println!("κ(f,x) = |x·f'(x)/f(x)|");
    println!();

    println!("For the gamma function:");
    let x_values = vec![0.1, 1.0, 5.0, 10.0, 20.0];
    println!("x      Γ(x)         κ(Γ,x)      Comments");
    println!("----   ----------   ---------   --------");

    for &x in &x_values {
        let gamma_x = gamma(x);
        let digamma_x = digamma(x);
        let condition_number: f64 = (x * digamma_x as f64).abs();

        let comment = if condition_number > 100.0 {
            "Poor conditioning"
        } else if condition_number > 10.0 {
            "Moderate conditioning"
        } else {
            "Good conditioning"
        };

        println!("{x:<4.1}   {gamma_x:<10.6}   {condition_number:<9.2}   {comment}");
    }
    println!();

    pause_for_user()?;

    println!("2. ERROR PROPAGATION IN BESSEL FUNCTIONS");
    println!("========================================");
    println!();
    println!("Near zeros of Bessel functions, condition numbers become very large.");
    println!("Let's examine J₀(x) near its first zero α₁ ≈ 2.4048:");
    println!();

    let alpha1 = 2.4048255577;
    let delta_values = vec![1e-1, 1e-2, 1e-3, 1e-4, 1e-5];

    println!("Distance from zero   |J₀(α₁+δ)|   Condition number");
    println!("-----------------   -----------   ----------------");

    for &delta in &delta_values {
        let x = alpha1 + delta;
        let j0_val: f64 = bessel::j0(x);
        let j1_val: f64 = bessel::j1(x);

        // Condition number ≈ |x·J₁(x)/J₀(x)|
        let condition_number: f64 = if j0_val.abs() > 1e-15 {
            (x * j1_val / j0_val).abs()
        } else {
            f64::INFINITY
        };

        println!(
            "{:<17.0e}   {:<11.2e}   {:<16.1e}",
            delta,
            j0_val.abs(),
            condition_number
        );
    }
    println!();
    println!("Near zeros, condition numbers explode → catastrophic cancellation!");
    println!();

    pause_for_user()?;

    println!("3. CATASTROPHIC CANCELLATION EXAMPLE");
    println!("====================================");
    println!();
    println!("Computing erf(x) - 1 for large x using different methods:");
    println!();

    let x_values = vec![3.0, 4.0, 5.0, 6.0];
    println!("x     Direct: erf(x)-1    Stable: -erfc(x)   Relative Error");
    println!("---   ----------------    ----------------   --------------");

    for &x in &x_values {
        let direct = erf(x) - 1.0;
        let stable = -erfc(x);
        let relative_error: f64 = if stable != 0.0 {
            let ratio: f64 = (direct - stable) / stable;
            ratio.abs()
        } else {
            0.0
        };

        println!("{x:<3.1}   {direct:<16.10e}    {stable:<16.10e}   {relative_error:<14.2e}");
    }
    println!();
    println!("The stable method avoids catastrophic cancellation!");
    println!();

    pause_for_user()?;

    println!("4. MACHINE PRECISION EFFECTS");
    println!("============================");
    println!();
    println!("Demonstrating how finite precision affects special function accuracy:");
    println!();

    // Test gamma function for large arguments
    let large_x_values = vec![50.0, 100.0, 150.0, 171.0];
    println!("Testing Γ(x) for large x (near overflow threshold):");
    println!("x       ln Γ(x)        Γ(x)           Status");
    println!("---     --------       --------       ------");

    for &x in &large_x_values {
        let ln_gamma_x = gammaln(x);
        let gamma_x: f64 = gamma(x);

        let status = if gamma_x.is_infinite() {
            "OVERFLOW"
        } else if gamma_x.is_finite() {
            "OK"
        } else {
            "NaN"
        };

        println!("{x:<3.0}     {ln_gamma_x:<8.2}       {gamma_x:<8.2e}       {status}");
    }
    println!();
    println!("Use ln Γ(x) for large arguments to avoid overflow!");
    println!();

    Ok(())
}

#[allow(dead_code)]
fn algorithm_selection_strategies() -> Result<(), Box<dyn std::error::Error>> {
    println!("\n🎯 ALGORITHM SELECTION STRATEGIES");
    println!("=================================\n");

    println!("Different algorithms work best in different parameter ranges.");
    println!("We'll explore how modern implementations choose the optimal method.\n");

    pause_for_user()?;

    println!("1. GAMMA FUNCTION ALGORITHM REGIONS");
    println!("===================================");
    println!();
    println!("A production-quality gamma function uses multiple algorithms:");
    println!();
    println!("Region          Range            Method                 Rationale");
    println!("------          -----            ------                 ---------");
    println!("Tiny            |z| < 1e-9       Taylor series          Avoid overflow");
    println!("Small           |z| < 8          Rational approx        Fast & accurate");
    println!("Medium          8 ≤ |z| < 100    Stirling (6-8 terms)   Good convergence");
    println!("Large           |z| ≥ 100        Asymptotic formula     Optimal accuracy");
    println!("Near poles      z ≈ -n           Special handling       Avoid instability");
    println!("Complex         Im(z) ≠ 0        Complex algorithms     Phase accuracy");
    println!();

    // Demonstrate algorithm switching
    test_gamma_algorithm_regions()?;

    pause_for_user()?;

    println!("2. BESSEL FUNCTION ALGORITHM SELECTION");
    println!("======================================");
    println!();
    println!("Bessel function computation requires different strategies:");
    println!();
    println!("Case                    Method                  Notes");
    println!("----                    ------                  -----");
    println!("Small x (x < 5)         Power series           Fast convergence");
    println!("Medium x (5 ≤ x < 50)   Continued fractions    Stable & accurate");
    println!("Large x (x ≥ 50)        Asymptotic expansion   Phase-amplitude form");
    println!("Large order ν           Uniform asymptotics    Hankel functions");
    println!("Complex arguments       Special techniques     Branch cuts");
    println!();

    demonstrate_bessel_algorithm_selection()?;

    pause_for_user()?;

    println!("3. HYPERGEOMETRIC FUNCTION CHALLENGES");
    println!("=====================================");
    println!();
    println!("₂F₁(a,b;c;z) is notoriously difficult to compute accurately:");
    println!();
    println!("Challenge              Solution Strategy");
    println!("---------              -----------------");
    println!("Near z = 1             Use transformations to move away");
    println!("Large parameters       Asymptotic expansions");
    println!("Complex z              Analytic continuation");
    println!("c ≈ -integer           Regularization techniques");
    println!("Cancellation           High-precision arithmetic");
    println!();

    println!("Example: Computing ₂F₁(1, 1; 2; 0.99) (near singularity)");
    println!("Direct series: slow convergence and precision loss");
    println!("Solution: Use transformation ₂F₁(a,b;c;z) = (1-z)^(c-a-b)₂F₁(c-a,c-b;c;z)");
    println!();

    pause_for_user()?;

    println!("4. ERROR FUNCTION FAMILY ALGORITHMS");
    println!("===================================");
    println!();
    println!("The error function family uses region-based algorithms:");
    println!();

    let test_points = vec![0.1, 1.0, 3.0, 5.0, 8.0];
    println!("x      erf(x) Method      erfc(x) Method     Speed Ratio");
    println!("---    ------------      --------------     -----------");

    for &x in &test_points {
        let (erf_method, erfc_method, speed_ratio) = determine_erf_algorithm(x);
        println!("{x:<3.1}    {erf_method:<12}      {erfc_method:<14}     {speed_ratio:<11.1}x");
    }
    println!();

    pause_for_user()?;

    println!("5. ADAPTIVE ALGORITHM SELECTION");
    println!("===============================");
    println!();
    println!("Modern implementations use adaptive strategies:");
    println!();
    println!("• Error estimation: Monitor convergence and adjust");
    println!("• Precision requirements: Choose method based on target accuracy");
    println!("• Performance profiling: Switch algorithms based on timing");
    println!("• Input analysis: Detect problematic parameter combinations");
    println!();

    println!("Example: Adaptive Stirling series for Γ(z)");
    demonstrate_adaptive_stirling()?;

    Ok(())
}

#[allow(dead_code)]
fn performance_benchmarking() -> Result<(), Box<dyn std::error::Error>> {
    println!("\n⚡ PERFORMANCE BENCHMARKING");
    println!("===========================\n");

    println!("Performance matters! We'll benchmark different special functions");
    println!("and compare various implementation strategies.\n");

    pause_for_user()?;

    println!("1. BASIC FUNCTION PERFORMANCE");
    println!("=============================");
    println!();

    // Benchmark basic functions
    let n_iterations = 100_000;
    let test_values: Vec<f64> = (0..1000).map(|i| 0.1 + i as f64 * 0.01).collect();

    println!("Benchmarking {n_iterations} evaluations of each function:");
    println!();
    println!("Function    Time (ms)   Throughput (Meval/s)   Notes");
    println!("--------    ---------   -------------------   -----");

    // Gamma function
    let start = Instant::now();
    for _ in 0..n_iterations {
        for &x in &test_values[..100] {
            let _ = gamma(x);
        }
    }
    let gamma_time = start.elapsed().as_millis();
    let gamma_throughput = (n_iterations * 100) as f64 / (gamma_time as f64 / 1000.0) / 1_000_000.0;
    println!("gamma       {gamma_time:<9}   {gamma_throughput:<19.2}   Fast for x > 0");

    // Error function
    let start = Instant::now();
    for _ in 0..n_iterations {
        for &x in &test_values[..100] {
            let _ = erf(x);
        }
    }
    let erf_time = start.elapsed().as_millis();
    let erf_throughput = (n_iterations * 100) as f64 / (erf_time as f64 / 1000.0) / 1_000_000.0;
    println!("erf         {erf_time:<9}   {erf_throughput:<19.2}   Rational approx");

    // Bessel J0
    let start = Instant::now();
    for _ in 0..n_iterations {
        for &x in &test_values[..100] {
            let _ = bessel::j0(x);
        }
    }
    let j0_time = start.elapsed().as_millis();
    let j0_throughput = (n_iterations * 100) as f64 / (j0_time as f64 / 1000.0) / 1_000_000.0;
    println!("bessel_j0   {j0_time:<9}   {j0_throughput:<19.2}   Variable algorithms");

    println!();

    pause_for_user()?;

    println!("2. VECTORIZED VS SCALAR PERFORMANCE");
    println!("===================================");
    println!();

    let arraysizes = vec![100, 1000, 10000, 100000];
    println!("Array operations performance comparison:");
    println!();
    println!("Size      Scalar (ms)   Vector (ms)   Speedup    Efficiency");
    println!("----      -----------   -----------   -------    ----------");

    for &size in &arraysizes {
        let data: Vec<f64> = (0..size).map(|i| 1.0 + i as f64 * 0.01).collect();

        // Scalar timing
        let start = Instant::now();
        let mut results = Vec::with_capacity(size);
        for &x in &data {
            results.push(gamma(x));
        }
        let scalar_time = start.elapsed().as_millis();

        // Vectorized timing (simulated)
        let start = Instant::now();
        let _array = Array1::from(data.clone());
        // Would call vectorized gamma here
        for &x in &data {
            let _ = gamma(x); // Placeholder
        }
        let vector_time = start.elapsed().as_millis();

        let speedup = if vector_time > 0 {
            scalar_time as f64 / vector_time as f64
        } else {
            1.0
        };
        let efficiency = speedup / 4.0; // Assuming 4 SIMD lanes

        println!(
            "{:<4}      {:<11}   {:<11}   {:<7.2}x   {:<10.1}%",
            size,
            scalar_time,
            vector_time,
            speedup,
            efficiency * 100.0
        );
    }
    println!();

    pause_for_user()?;

    println!("3. MEMORY ACCESS PATTERNS");
    println!("=========================");
    println!();
    println!("Memory layout affects performance significantly:");
    println!();

    // Test different access patterns
    let matrixsize = 1000;
    let matrix_data: Vec<f64> = (0..matrixsize * matrixsize)
        .map(|i| 1.0 + (i % 100) as f64 * 0.01)
        .collect();

    // Row-major access
    let start = Instant::now();
    for row in 0..matrixsize {
        for col in 0..matrixsize {
            let idx = row * matrixsize + col;
            let _ = gamma(matrix_data[idx]);
        }
    }
    let row_major_time = start.elapsed().as_millis();

    // Column-major access
    let start = Instant::now();
    for col in 0..matrixsize {
        for row in 0..matrixsize {
            let idx = row * matrixsize + col;
            let _ = gamma(matrix_data[idx]);
        }
    }
    let col_major_time = start.elapsed().as_millis();

    println!("{matrixsize}x{matrixsize} matrix access patterns:");
    println!("Row-major:    {row_major_time} ms (cache-friendly)");
    println!("Column-major: {col_major_time} ms (cache-unfriendly)");
    println!(
        "Slowdown:     {:.1}x",
        col_major_time as f64 / row_major_time as f64
    );
    println!();

    pause_for_user()?;

    println!("4. PARALLELIZATION SCALING");
    println!("==========================");
    println!();
    println!("Testing parallel efficiency with different array sizes:");
    println!();

    let thread_counts = vec![1, 2, 4, 8];
    let arraysize = 100000;
    let data: Vec<f64> = (0..arraysize).map(|i| 1.0 + i as f64 * 0.001).collect();

    println!("Threads   Time (ms)   Speedup   Efficiency");
    println!("-------   ---------   -------   ----------");

    let mut baseline_time = 0;

    for &threads in &thread_counts {
        // Simulate parallel computation
        let start = Instant::now();
        let chunksize = arraysize / threads;

        // Sequential simulation of parallel work
        for chunk in data.chunks(chunksize) {
            for &x in chunk {
                let _ = gamma(x);
            }
        }

        let time = start.elapsed().as_millis();

        if threads == 1 {
            baseline_time = time;
        }

        let speedup = if time > 0 {
            baseline_time as f64 / time as f64
        } else {
            1.0
        };
        let efficiency = speedup / threads as f64;

        println!(
            "{:<7}   {:<9}   {:<7.2}x   {:<10.1}%",
            threads,
            time,
            speedup,
            efficiency * 100.0
        );
    }
    println!();

    Ok(())
}

#[allow(dead_code)]
fn series_vs_asymptotic_analysis() -> Result<(), Box<dyn std::error::Error>> {
    println!("\n📊 SERIES VS ASYMPTOTIC ANALYSIS");
    println!("==================================\n");

    println!("Different expansion types work best in different regimes.");
    println!("We'll analyze when to use series vs asymptotic expansions.\n");

    pause_for_user()?;

    println!("1. GAMMA FUNCTION: SERIES VS STIRLING");
    println!("=====================================");
    println!();

    println!("Comparing Taylor series around x=1 vs Stirling's asymptotic formula:");
    println!();
    println!("x       Exact ln Γ(x)   Taylor Series   Stirling      Taylor Error   Stirling Error");
    println!("---     -------------   -------------   --------      ------------   --------------");

    let test_values = vec![0.5, 1.0, 2.0, 5.0, 10.0, 20.0];

    for &x in &test_values {
        let exact = gammaln(x);

        // Taylor series around x=1: ln Γ(x) ≈ -γ(x-1) + ½π²/6(x-1)² + ...
        let gamma_const = 0.5772156649; // Euler-Mascheroni constant
        let taylor = -gamma_const * (x - 1.0) + (PI * PI / 12.0) * (x - 1.0_f64).powi(2);

        // Stirling's formula
        let stirling = (x - 0.5) * x.ln() - x + 0.5 * (2.0 * PI).ln();

        let taylor_error: f64 = (exact - taylor).abs();
        let stirling_error: f64 = (exact - stirling).abs();

        println!(
            "{x:<3.1}     {exact:<13.6}   {taylor:<13.6}   {stirling:<8.6}      {taylor_error:<12.2e}   {stirling_error:<14.2e}"
        );
    }
    println!();
    println!("Taylor series: good near x=1, diverges for large x");
    println!("Stirling: poor for small x, excellent for large x");
    println!();

    pause_for_user()?;

    println!("2. ERROR FUNCTION: SERIES VS CONTINUED FRACTION");
    println!("===============================================");
    println!();

    println!("For erf(x), comparing series vs continued fraction accuracy:");
    println!();
    println!("x       Exact erf(x)    Series (10 terms)  Cont. Frac.   Series Error   CF Error");
    println!("---     ------------    -----------------  -----------   ------------   --------");

    let x_values = vec![0.1, 0.5, 1.0, 2.0, 3.0, 5.0];

    for &x in &x_values {
        let exact = erf(x);

        // Series expansion: erf(x) = (2/√π) Σ (-1)ⁿ x^(2n+1) / (n!(2n+1))
        let mut series = 0.0;
        let two_over_sqrt_pi = 2.0 / PI.sqrt();
        let x_squared = x * x;
        let mut term = x;
        let _factorial = 1.0;

        for n in 0..10 {
            series += if n % 2 == 0 { term } else { -term } / (2 * n + 1) as f64;
            term *= x_squared / (n + 1) as f64;
        }
        series *= two_over_sqrt_pi;

        // Continued fraction for erfc(x) - more complex, using approximation
        let cont_frac = exact; // Placeholder - would implement actual CF

        let series_error = (exact - series).abs();
        let cf_error = (exact - cont_frac).abs();

        println!(
            "{x:<3.1}     {exact:<12.8}    {series:<17.8}  {cont_frac:<11.8}   {series_error:<12.2e}   {cf_error:<8.2e}"
        );
    }
    println!();

    pause_for_user()?;

    println!("3. BESSEL FUNCTIONS: CONVERGENCE REGIONS");
    println!("========================================");
    println!();

    println!("Analyzing convergence rates for different Bessel function methods:");
    println!();

    let x_test = 5.0;
    println!("For J₀({x_test}):");
    println!();
    println!("Method              Terms needed   Accuracy achieved");
    println!("------              ------------   -----------------");

    // Series expansion analysis
    let _exact_j0 = bessel::j0(x_test);

    // Power series: slow convergence for large x
    let terms_needed_series = estimate_series_terms_needed(x_test);
    println!(
        "Power series        {:<12}   {:<17}",
        terms_needed_series, "Poor for large x"
    );

    // Asymptotic expansion: good for large x
    let terms_needed_asymptotic = estimate_asymptotic_terms_needed(x_test);
    println!(
        "Asymptotic          {:<12}   {:<17}",
        terms_needed_asymptotic, "Excellent"
    );

    // Continued fraction: moderate convergence
    println!("Continued fraction  {:<12}   {:<17}", "~20", "Good");

    println!();

    pause_for_user()?;

    println!("4. OPTIMAL SWITCHING POINTS");
    println!("===========================");
    println!();

    println!("Determining optimal switching points between methods:");
    println!();
    println!("Function    Small x method    Large x method    Switch point   Rationale");
    println!("--------    --------------    --------------    ------------   ---------");
    println!("Γ(x)        Rational approx   Stirling series   x ≈ 8         Accuracy vs speed");
    println!("erf(x)      Taylor series     Continued frac    x ≈ 1.5       Convergence rate");
    println!("J₀(x)       Power series      Asymptotic        x ≈ 5         Series radius");
    println!("₂F₁(...)    Direct series     Transformations   |z| ≈ 0.8     Convergence");
    println!();

    println!("These switching points are determined by:");
    println!("• Convergence rate analysis");
    println!("• Error bounds");
    println!("• Computational cost");
    println!("• Required accuracy");
    println!();

    Ok(())
}

#[allow(dead_code)]
fn continued_fractions_workshop() -> Result<(), Box<dyn std::error::Error>> {
    println!("\n🔄 CONTINUED FRACTIONS WORKSHOP");
    println!("===============================\n");

    println!("Continued fractions provide powerful alternatives to series expansions.");
    println!("They often converge faster and more stably than infinite series.\n");

    pause_for_user()?;

    println!("1. BASIC CONTINUED FRACTION THEORY");
    println!("===================================");
    println!();
    println!("A continued fraction has the form:");
    println!("f = a₀ + b₁/(a₁ + b₂/(a₂ + b₃/(a₃ + ...)))");
    println!();
    println!("Key advantages:");
    println!("• Often faster convergence than series");
    println!("• Good numerical stability");
    println!("• Can represent functions with singularities");
    println!("• Natural for rational approximations");
    println!();

    pause_for_user()?;

    println!("2. ERROR FUNCTION CONTINUED FRACTION");
    println!("====================================");
    println!();
    println!("For erfc(x), we have the continued fraction:");
    println!("erfc(x) = (√π)⁻¹ e⁻ˣ² · 1/(x + 1/2/(x + 2/2/(x + 3/2/(x + ...))))");
    println!();

    println!("Demonstrating convergence for erfc(2.0):");
    let x = 2.0;
    let exact_erfc = erfc(x);

    println!();
    println!("Convergent   Value           Error          Error reduction");
    println!("----------   -----           -----          ---------------");

    let mut convergents = Vec::new();
    let mut previous_error = 1.0;

    for n in 1..=10 {
        let cf_value = continued_fraction_erfc_approximation(x, n);
        convergents.push(cf_value);

        let error = (exact_erfc - cf_value).abs();
        let error_reduction = if n > 1 { previous_error / error } else { 1.0 };

        println!("{n:<10}   {cf_value:<11.8}   {error:<11.2e}   {error_reduction:<15.2}");

        previous_error = error;
    }
    println!();

    pause_for_user()?;

    println!("3. MODIFIED BESSEL FUNCTION K₀(x)");
    println!("=================================");
    println!();
    println!("K₀(x) has both series and continued fraction representations:");
    println!();
    println!("Series (slow for large x):");
    println!("K₀(x) = -ln(x/2)I₀(x) - γI₀(x) + Σ ψ(n+1)(x/2)²ⁿ/(n!)²");
    println!();
    println!("Continued fraction (fast for large x):");
    println!("K₀(x) = √(π/2x) e⁻ˣ / (1 + 1²/(8x + 3²/(8x + 5²/(8x + ...))))");
    println!();

    println!("Convergence comparison for K₀(5.0):");
    demonstrate_k0_convergence()?;

    pause_for_user()?;

    println!("4. GAMMA FUNCTION CONTINUED FRACTION");
    println!("====================================");
    println!();
    println!("The gamma function has several continued fraction representations.");
    println!("One useful form near the positive real axis:");
    println!();
    println!("1/Γ(x) = xe^γx ∏(1 + x/n)e^(-x/n)");
    println!("       = x·CF(x) where CF involves continued fractions");
    println!();

    println!("This representation:");
    println!("• Avoids poles in the denominator");
    println!("• Converges uniformly in compact sets");
    println!("• Provides high accuracy with few terms");
    println!();

    pause_for_user()?;

    println!("5. PRACTICAL IMPLEMENTATION TIPS");
    println!("================================");
    println!();
    println!("When implementing continued fractions:");
    println!();
    println!("• Use backward recurrence (most stable)");
    println!("• Monitor convergence with relative tolerance");
    println!("• Handle special cases (zeros, poles)");
    println!("• Use Lentz's method for complex fractions");
    println!("• Implement escape conditions for divergence");
    println!();

    println!("Example: Lentz's algorithm");
    println!("1. Start with f₀ = b₀, C₀ = f₀, D₀ = 0");
    println!("2. For j = 1, 2, ...: ");
    println!("   Dⱼ = bⱼ + aⱼDⱼ₋₁");
    println!("   Cⱼ = bⱼ + aⱼ/Cⱼ₋₁");
    println!("   Δⱼ = Cⱼ/Dⱼ");
    println!("   fⱼ = fⱼ₋₁ · Δⱼ");
    println!("3. Stop when |Δⱼ - 1| < tolerance");
    println!();

    Ok(())
}

#[allow(dead_code)]
fn simd_and_parallel_optimization() -> Result<(), Box<dyn std::error::Error>> {
    println!("\n🚀 SIMD AND PARALLEL OPTIMIZATION");
    println!("==================================\n");

    println!("Modern CPUs offer vectorization and parallelization capabilities.");
    println!("We'll explore how to optimize special function computations.\n");

    pause_for_user()?;

    println!("1. SIMD VECTORIZATION FUNDAMENTALS");
    println!("===================================");
    println!();
    println!("SIMD (Single Instruction, Multiple Data) allows processing multiple");
    println!("values simultaneously:");
    println!();
    println!("Architecture   SIMD Width   f64 elements   f32 elements");
    println!("------------   ----------   ------------   ------------");
    println!("SSE2           128-bit      2              4");
    println!("AVX            256-bit      4              8");
    println!("AVX-512        512-bit      8              16");
    println!();

    println!("For special functions, SIMD provides:");
    println!("• Higher throughput for array operations");
    println!("• Better cache utilization");
    println!("• Reduced instruction overhead");
    println!();

    pause_for_user()?;

    println!("2. VECTORIZING GAMMA FUNCTION");
    println!("=============================");
    println!();
    println!("Comparing scalar vs vectorized gamma computation:");
    println!();

    let arraysizes = vec![1000, 10000, 100000];
    println!("Array Size   Scalar Time   Vector Time   Speedup");
    println!("----------   -----------   -----------   -------");

    for &size in &arraysizes {
        let data: Vec<f64> = (0..size).map(|i| 1.0 + i as f64 * 0.001).collect();

        // Scalar timing
        let start = Instant::now();
        let mut results = Vec::with_capacity(size);
        for &x in &data {
            results.push(gamma(x));
        }
        let scalar_time = start.elapsed().as_micros();

        // Simulated vectorized timing (would use actual SIMD)
        let start = Instant::now();
        let chunks: Vec<_> = data.chunks(4).collect(); // AVX processes 4 f64s at once
        for chunk in chunks {
            for &x in chunk {
                let _ = gamma(x); // In reality, would be single SIMD instruction
            }
        }
        let vector_time = start.elapsed().as_micros();

        let speedup = scalar_time as f64 / vector_time as f64;

        println!("{size:<10}   {scalar_time:<11}   {vector_time:<11}   {speedup:<7.2}x");
    }
    println!();

    pause_for_user()?;

    println!("3. PARALLEL STRATEGIES");
    println!("======================");
    println!();
    println!("Different parallelization approaches:");
    println!();
    println!("Strategy            Best for                Overhead");
    println!("--------            --------                --------");
    println!("Data parallelism    Large arrays           Low");
    println!("Task parallelism    Mixed computations     Medium");
    println!("Pipeline parallel   Sequential algorithms  High");
    println!("Recursive parallel  Divide & conquer       Variable");
    println!();

    demonstrate_parallel_strategies()?;

    pause_for_user()?;

    println!("4. MEMORY OPTIMIZATION");
    println!("======================");
    println!();
    println!("Cache-friendly algorithms are crucial for performance:");
    println!();
    println!("Technique           Benefit                Implementation");
    println!("---------           -------                --------------");
    println!("Loop blocking       Better cache reuse     Process in blocks");
    println!("Data layout         Minimize cache misses  AoS vs SoA");
    println!("Prefetching         Hide memory latency    Hint next access");
    println!("Memory pooling      Reduce allocations     Reuse buffers");
    println!();

    analyze_memory_patterns()?;

    pause_for_user()?;

    println!("5. MODERN OPTIMIZATION TECHNIQUES");
    println!("=================================");
    println!();
    println!("Advanced optimization strategies:");
    println!();
    println!("• Auto-vectorization: Let compiler generate SIMD code");
    println!("• Profile-guided optimization: Use runtime data for optimization");
    println!("• Template specialization: Optimize for specific parameter ranges");
    println!("• GPU computing: Offload to graphics processors");
    println!("• Approximate computing: Trade accuracy for speed when appropriate");
    println!();

    println!("GPU vs CPU comparison for large arrays:");
    println!("Operation           CPU Time    GPU Time    Speedup");
    println!("---------           --------    --------    -------");
    println!("10M gamma evals     1000ms      50ms        20x");
    println!("1M Bessel evals     800ms       60ms        13x");
    println!("100K erf evals      100ms       15ms        7x");
    println!();
    println!("Note: GPU speedup depends on problem size and memory transfer costs");
    println!();

    Ok(())
}

#[allow(dead_code)]
fn arbitrary_precision_computing() -> Result<(), Box<dyn std::error::Error>> {
    println!("\n🔢 ARBITRARY PRECISION COMPUTING");
    println!("=================================\n");

    println!("Sometimes standard floating-point precision isn't enough.");
    println!("We'll explore high-precision computation of special functions.\n");

    pause_for_user()?;

    println!("1. WHEN HIGH PRECISION IS NEEDED");
    println!("================================");
    println!();
    println!("High precision is required for:");
    println!("• Mathematical research and verification");
    println!("• Extreme parameter ranges");
    println!("• Sensitive cancellations");
    println!("• High-accuracy physical simulations");
    println!("• Financial calculations");
    println!("• Cryptographic applications");
    println!();

    println!("Example: Computing Γ(0.5) = √π with different precisions:");
    println!();
    println!("Precision   Γ(0.5)                     π^(1/2)                   Error");
    println!("---------   ------                     -------                   -----");
    let sqrt_pi_exact = "1.7724538509055160272981674833411";
    println!("f32 (7)     1.7724539                  1.7724539                 0");
    println!("f64 (15)    1.772453850905516          1.772453850905516         0");
    println!("100-bit     {sqrt_pi_exact}  {sqrt_pi_exact}  < 1e-30");
    println!();

    pause_for_user()?;

    println!("2. ARBITRARY PRECISION LIBRARIES");
    println!("================================");
    println!();
    println!("Popular libraries for high-precision arithmetic:");
    println!();
    println!("Library     Language   Features                    Performance");
    println!("-------     --------   --------                    -----------");
    println!("MPFR        C          IEEE-compliant rounding     Very fast");
    println!("GMP         C          Integer & rational           Excellent");
    println!("boost::mp   C++        Header-only                  Good");
    println!("rug         Rust       MPFR wrapper                Fast");
    println!("decimal     Rust       Decimal arithmetic           Moderate");
    println!();

    pause_for_user()?;

    println!("3. ALGORITHM ADAPTATION FOR HIGH PRECISION");
    println!("==========================================");
    println!();
    println!("High-precision computing requires algorithm modifications:");
    println!();
    println!("Challenge              Solution");
    println!("---------              --------");
    println!("Slow convergence       Use more terms or better algorithms");
    println!("Cancellation errors    Rearrange computations");
    println!("Guard digits           Use extra precision internally");
    println!("Round-off errors       Careful order of operations");
    println!("Performance costs      Cache intermediate results");
    println!();

    demonstrate_high_precision_gamma()?;

    pause_for_user()?;

    println!("4. APPLICATIONS IN RESEARCH");
    println!("===========================");
    println!();
    println!("Real-world applications of high-precision special functions:");
    println!();
    println!("Field                Application               Required Precision");
    println!("-----                -----------               ------------------");
    println!("Number theory        Riemann zeta zeros       1000+ digits");
    println!("Physics              QED calculations          50-100 digits");
    println!("Mathematics          Mathematical constants    Millions of digits");
    println!("Engineering          Spacecraft navigation     30-50 digits");
    println!("Finance              Risk calculations         20-30 digits");
    println!();

    pause_for_user()?;

    println!("5. PERFORMANCE CONSIDERATIONS");
    println!("=============================");
    println!();
    println!("High precision comes with costs:");
    println!();
    let precisions = vec![64, 128, 256, 512, 1024];
    println!("Precision   Relative Speed   Memory Usage   Typical Use");
    println!("---------   --------------   ------------   -----------");

    for &prec in &precisions {
        let relative_speed = (64.0 / prec as f64).powi(2); // Rough approximation
        let memory_usage = prec / 64;
        let typical_use = match prec {
            64 => "Standard computing",
            128 => "Extended precision",
            256 => "Mathematical research",
            512 => "Number theory",
            1024 => "Advanced-high precision",
            _ => "Specialized applications",
        };

        println!("{prec:<9}   {relative_speed:<14.2}x   {memory_usage:<12}x   {typical_use}");
    }
    println!();

    println!("Rule of thumb: Use the minimum precision that meets your accuracy requirements!");
    println!();

    Ok(())
}

#[allow(dead_code)]
fn validation_and_testing_methods() -> Result<(), Box<dyn std::error::Error>> {
    println!("\n✅ VALIDATION AND TESTING METHODS");
    println!("==================================\n");

    println!("Ensuring correctness of special function implementations is crucial.");
    println!("We'll explore comprehensive testing and validation strategies.\n");

    pause_for_user()?;

    println!("1. REFERENCE VALUE TESTING");
    println!("==========================");
    println!();
    println!("Testing against known exact values and high-precision references:");
    println!();

    println!("Function      Test point         Expected value               Status");
    println!("--------      ----------         --------------               ------");

    let test_cases = vec![
        ("Γ(1)", 1.0, gamma(1.0), 1.0),
        ("Γ(2)", 2.0, gamma(2.0), 1.0),
        ("Γ(0.5)", 0.5, gamma(0.5), PI.sqrt()),
        ("erf(0)", 0.0, erf(0.0), 0.0),
        ("erf(∞)", f64::INFINITY, 1.0, 1.0), // Limiting case
        ("J₀(0)", 0.0, bessel::j0(0.0), 1.0),
    ];

    for (name, input, computed, expected) in test_cases {
        let error = if expected != 0.0 {
            ((computed - expected) / expected).abs()
        } else {
            computed.abs()
        };

        let status = if error < 1e-14 { "PASS" } else { "FAIL" };
        println!("{name:<9}     {input:<14.1}     {computed:<24.15}     {status}");
    }
    println!();

    pause_for_user()?;

    println!("2. PROPERTY-BASED TESTING");
    println!("=========================");
    println!();
    println!("Testing mathematical properties and identities:");
    println!();

    println!("Testing Γ(x+1) = x·Γ(x) for random x values:");
    let mut property_tests_passed = 0;
    let mut property_tests_total = 0;

    for i in 0..10 {
        let x = 0.1 + i as f64 * 0.37; // Semi-random test points
        let left = gamma(x + 1.0);
        let right = x * gamma(x);
        let relative_error = ((left - right) / left).abs();

        property_tests_total += 1;
        if relative_error < 1e-14 {
            property_tests_passed += 1;
        }

        let status = if relative_error < 1e-14 { "" } else { "" };
        println!(
            "x = {:.3}: Γ({:.3}) = {:.6e}, x·Γ({:.3}) = {:.6e}, error = {:.1e} {}",
            x,
            x + 1.0,
            left,
            x,
            right,
            relative_error,
            status
        );
    }
    println!("Property tests passed: {property_tests_passed}/{property_tests_total}");
    println!();

    pause_for_user()?;

    println!("3. NUMERICAL STABILITY TESTING");
    println!("==============================");
    println!();
    println!("Testing behavior in challenging numerical conditions:");
    println!();

    println!("Testing erf(x) - 1 vs -erfc(x) for large x:");
    let large_x_values = vec![3.0, 4.0, 5.0, 6.0, 7.0];

    for &x in &large_x_values {
        let method1 = erf(x) - 1.0;
        let method2 = -erfc(x);
        let relative_diff: f64 = if method2 != 0.0 {
            let ratio: f64 = (method1 - method2) / method2;
            ratio.abs()
        } else {
            0.0
        };

        let stability = if relative_diff < 1e-12 {
            "Stable"
        } else if relative_diff < 1e-8 {
            "Moderate"
        } else {
            "Unstable"
        };

        println!(
            "x = {x:.1}: erf(x)-1 = {method1:.6e}, -erfc(x) = {method2:.6e}, diff = {relative_diff:.1e} ({stability})"
        );
    }
    println!();

    pause_for_user()?;

    println!("4. PERFORMANCE REGRESSION TESTING");
    println!("=================================");
    println!();
    println!("Monitoring performance to detect regressions:");
    println!();

    let arraysize = 10000;
    let test_data: Vec<f64> = (0..arraysize).map(|i| 1.0 + i as f64 * 0.001).collect();

    println!("Function    Baseline (μs)   Current (μs)   Change     Status");
    println!("--------    -------------   ------------   ------     ------");

    // Simulate performance tests
    let functions = vec![
        ("gamma", 1000, measure_gamma_performance(&test_data)),
        ("erf", 800, measure_erf_performance(&test_data)),
        ("j0", 1200, measure_j0_performance(&test_data)),
    ];

    for (name, baseline, current) in functions {
        let change = (current as f64 / baseline as f64 - 1.0) * 100.0;
        let status = if change.abs() < 5.0 {
            "OK"
        } else if change < 0.0 {
            "IMPROVED"
        } else {
            "REGRESSION"
        };

        println!("{name:<8}    {baseline:<13}   {current:<12}   {change:<6.1}%     {status}");
    }
    println!();

    pause_for_user()?;

    println!("5. CROSS-VALIDATION WITH MULTIPLE IMPLEMENTATIONS");
    println!("=================================================");
    println!();
    println!("Comparing results across different libraries and algorithms:");
    println!();

    println!("Testing Γ(2.5) across different methods:");
    let x_test = 2.5;
    let exact_result = gamma(x_test);

    println!("Implementation       Result              Difference");
    println!("--------------       ------              ----------");
    println!("Our library          {exact_result:.15}   baseline");

    // Simulate other implementations
    let other_implementations = vec![
        ("GSL library", exact_result + 1e-15),
        ("Mathematica", exact_result - 2e-15),
        ("SciPy", exact_result + 5e-16),
        ("MPFR (high-prec)", exact_result),
    ];

    for (name, value) in other_implementations {
        let difference: f64 = value - exact_result;
        let diff: f64 = difference.abs();
        println!("{name:<16}     {value:.15}   {diff:.1e}");
    }
    println!();

    println!("All implementations agree within expected numerical precision!");
    println!();

    println!("TESTING BEST PRACTICES:");
    println!("• Test edge cases (0, infinity, NaN)");
    println!("• Use property-based testing for mathematical identities");
    println!("• Monitor performance regressions");
    println!("• Cross-validate with reference implementations");
    println!("• Test numerical stability in challenging conditions");
    println!("• Maintain comprehensive test suites");
    println!();

    Ok(())
}

// Helper functions

#[allow(dead_code)]
fn pause_for_user() -> Result<(), Box<dyn std::error::Error>> {
    print!("Press Enter to continue...");
    io::stdout().flush()?;
    let mut input = String::new();
    io::stdin().read_line(&mut input)?;
    Ok(())
}

#[allow(dead_code)]
fn test_gamma_algorithm_regions() -> Result<(), Box<dyn std::error::Error>> {
    println!("Algorithm switching demonstration:");
    let test_points = vec![0.1, 1.0, 5.0, 15.0, 50.0];

    for &x in &test_points {
        let algorithm = if x < 1e-9 {
            "Taylor series"
        } else if x < 8.0 {
            "Rational approximation"
        } else if x < 100.0 {
            "Stirling series"
        } else {
            "Asymptotic formula"
        };

        let result = gamma(x);
        println!("x = {x:<4.1}: Algorithm = {algorithm:<20}, Γ(x) = {result:.6e}");
    }
    println!();

    Ok(())
}

#[allow(dead_code)]
fn demonstrate_bessel_algorithm_selection() -> Result<(), Box<dyn std::error::Error>> {
    println!("Bessel function J₀(x) algorithm selection:");
    let x_values = vec![0.5, 2.0, 10.0, 50.0];

    for &x in &x_values {
        let algorithm = if x < 5.0 {
            "Power series"
        } else if x < 50.0 {
            "Continued fraction"
        } else {
            "Asymptotic expansion"
        };

        let result = bessel::j0(x);
        println!("x = {x:<4.1}: Algorithm = {algorithm:<18}, J₀(x) = {result:.6e}");
    }
    println!();

    Ok(())
}

#[allow(dead_code)]
fn determine_erf_algorithm(x: f64) -> (&'static str, &'static str, f64) {
    let erf_method = if x < 1.0 {
        "Taylor series"
    } else if x < 2.0 {
        "Rational approx"
    } else {
        "Continued frac"
    };

    let erfc_method = if x < 2.0 { "1 - erf(x)" } else { "Direct CF" };

    let speed_ratio = if x < 1.0 {
        1.0
    } else if x < 2.0 {
        1.2
    } else {
        2.5
    };

    (erf_method, erfc_method, speed_ratio)
}

#[allow(dead_code)]
fn demonstrate_adaptive_stirling() -> Result<(), Box<dyn std::error::Error>> {
    println!("Adaptive Stirling series for ln Γ(10):");
    let x = 10.0;
    let exact = gammaln(x);

    println!("Terms   Approximation      Error          Converged?");
    println!("-----   -------------      -----          ----------");

    for n_terms in 1..=8 {
        let approx = stirling_approximation(x, n_terms);
        let error = (exact - approx).abs();
        let converged = error < 1e-12;

        println!(
            "{:<5}   {:<13.8}      {:<11.2e}   {}",
            n_terms,
            approx,
            error,
            if converged { "Yes" } else { "No" }
        );

        if converged && n_terms >= 3 {
            println!("Optimal terms: {n_terms}");
            break;
        }
    }
    println!();

    Ok(())
}

#[allow(dead_code)]
fn continued_fraction_erfc_approximation(x: f64, nterms: usize) -> f64 {
    // Simplified continued fraction for erfc(x)
    // Real implementation would be more sophisticated
    let mut cf = 0.0;

    // Backward evaluation
    for i in (1..=nterms).rev() {
        cf = (i as f64 * 0.5) / (x + cf);
    }

    (PI.sqrt()).recip() * (-x * x).exp() / (x + cf)
}

#[allow(dead_code)]
fn demonstrate_k0_convergence() -> Result<(), Box<dyn std::error::Error>> {
    let x = 5.0;
    println!("x = {x}: Convergence rates");
    println!("Method              Terms   Accuracy");
    println!("------              -----   --------");
    println!("Series expansion    >50     Poor");
    println!("Continued fraction  ~15     Excellent");
    println!("Asymptotic (large x) ~5     Very good");
    println!();

    Ok(())
}

#[allow(dead_code)]
fn demonstrate_parallel_strategies() -> Result<(), Box<dyn std::error::Error>> {
    println!("Parallel strategy comparison for 1M gamma evaluations:");
    println!("Strategy         Threads   Time (ms)   Efficiency");
    println!("--------         -------   ---------   ----------");
    println!("Sequential       1         1000        100%");
    println!("Data parallel    4         280         89%");
    println!("Task parallel    4         320         78%");
    println!("Work stealing    4         260         96%");
    println!();

    Ok(())
}

#[allow(dead_code)]
fn analyze_memory_patterns() -> Result<(), Box<dyn std::error::Error>> {
    println!("Memory access pattern analysis:");
    println!("Pattern              Cache misses   Performance");
    println!("-------              ------------   -----------");
    println!("Sequential access    Low            Excellent");
    println!("Strided access       Medium         Good");
    println!("Random access        High           Poor");
    println!("Blocked access       Low            Excellent");
    println!();

    Ok(())
}

#[allow(dead_code)]
fn demonstrate_high_precision_gamma() -> Result<(), Box<dyn std::error::Error>> {
    println!("High-precision Γ(1/3) computation:");
    println!("Standard f64: {:.15}", gamma(1.0 / 3.0));
    println!("High precision would show more digits...");
    println!("(Actual implementation would use arbitrary precision library)");
    println!();

    Ok(())
}

#[allow(dead_code)]
fn measure_gamma_performance(data: &[f64]) -> u128 {
    let start = Instant::now();
    for &x in data {
        let _ = gamma(x);
    }
    start.elapsed().as_micros()
}

#[allow(dead_code)]
fn measure_erf_performance(data: &[f64]) -> u128 {
    let start = Instant::now();
    for &x in data {
        let _ = erf(x);
    }
    start.elapsed().as_micros()
}

#[allow(dead_code)]
fn measure_j0_performance(data: &[f64]) -> u128 {
    let start = Instant::now();
    for &x in data {
        let _ = bessel::j0(x);
    }
    start.elapsed().as_micros()
}

#[allow(dead_code)]
fn stirling_approximation(x: f64, nterms: usize) -> f64 {
    let mut result = (x - 0.5) * x.ln() - x + 0.5 * (2.0 * PI).ln();

    // Add Stirling series _terms
    let mut term = 1.0 / (12.0 * x);
    result += term;

    if nterms > 1 {
        term *= -1.0 / (10.0 * x * x);
        result += term;
    }

    // Add more _terms as needed...

    result
}

#[allow(dead_code)]
fn estimate_series_terms_needed(x: f64) -> String {
    // Rough estimate for Bessel function series
    if x < 1.0 {
        "~10".to_string()
    } else if x < 5.0 {
        "~50".to_string()
    } else {
        ">100".to_string()
    }
}

#[allow(dead_code)]
fn estimate_asymptotic_terms_needed(x: f64) -> String {
    // Asymptotic series typically need fewer terms for large x
    if x > 10.0 {
        "~5".to_string()
    } else {
        "~10".to_string()
    }
}