scirs2-linalg 0.4.0

Linear algebra module for SciRS2 (scirs2-linalg)
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
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
//! Standard eigenvalue decomposition for dense matrices
//!
//! This module provides functions for computing eigenvalues and eigenvectors of
//! dense matrices using various algorithms:
//! - General eigenvalue decomposition for non-symmetric matrices
//! - Symmetric/Hermitian eigenvalue decomposition for better performance
//! - Power iteration for dominant eigenvalues
//! - QR algorithm for general cases

use scirs2_core::ndarray::{Array1, Array2, ArrayView2, ScalarOperand};
use scirs2_core::numeric::Complex;
use scirs2_core::numeric::{Float, NumAssign};
use scirs2_core::random::prelude::*;
use std::iter::Sum;

use crate::decomposition::qr;
use crate::error::{LinalgError, LinalgResult};
use crate::norm::vector_norm;
use crate::validation::validate_decomposition;

/// Type alias for eigenvalue-eigenvector pair result
/// Returns a tuple of (eigenvalues, eigenvectors) where eigenvalues is a 1D array
/// and eigenvectors is a 2D array where each column corresponds to an eigenvector
pub type EigenResult<F> = LinalgResult<(Array1<Complex<F>>, Array2<Complex<F>>)>;

/// Compute the eigenvalues and right eigenvectors of a square matrix.
///
/// # Arguments
///
/// * `a` - Input square matrix
/// * `workers` - Number of worker threads (None = use default)
///
/// # Returns
///
/// * Tuple (eigenvalues, eigenvectors) where eigenvalues is a complex vector
///   and eigenvectors is a complex matrix whose columns are the eigenvectors
///
/// # Examples
///
/// ```
/// use scirs2_core::ndarray::array;
/// use scirs2_linalg::eigen::standard::eig;
///
/// let a = array![[1.0_f64, 0.0], [0.0, 2.0]];
/// let (w, v) = eig(&a.view(), None).expect("Operation failed");
///
/// // Sort eigenvalues (they may be returned in different order)
/// let mut eigenvalues = vec![(w[0].re, 0), (w[1].re, 1)];
/// eigenvalues.sort_by(|a, b| a.0.partial_cmp(&b.0).expect("Operation failed"));
///
/// assert!((eigenvalues[0].0 - 1.0).abs() < 1e-10);
/// assert!((eigenvalues[1].0 - 2.0).abs() < 1e-10);
/// ```
#[allow(dead_code)]
pub fn eig<F>(a: &ArrayView2<F>, workers: Option<usize>) -> EigenResult<F>
where
    F: Float + NumAssign + Sum + Send + Sync + scirs2_core::ndarray::ScalarOperand + 'static,
{
    use crate::parallel;

    // Configure workers for parallel operations
    parallel::configure_workers(workers);

    // Parameter validation using validation helpers
    validate_decomposition(a, "Eigenvalue computation", true)?;

    let n = a.nrows();

    // For 1x1 and 2x2 matrices, we can compute eigenvalues analytically
    if n == 1 {
        let eigenvalue = Complex::new(a[[0, 0]], F::zero());
        let eigenvector = Array2::eye(1).mapv(|x| Complex::new(x, F::zero()));

        return Ok((Array1::from_elem(1, eigenvalue), eigenvector));
    } else if n == 2 {
        return solve_2x2_eigenvalue_problem(a);
    }

    // For larger matrices, use the QR algorithm
    solve_qr_algorithm(a)
}

/// Compute the eigenvalues of a square matrix.
///
/// # Arguments
///
/// * `a` - Input square matrix
/// * `workers` - Number of worker threads (None = use default)
///
/// # Returns
///
/// * Vector of complex eigenvalues
///
/// # Examples
///
/// ```
/// use scirs2_core::ndarray::array;
/// use scirs2_linalg::eigen::standard::eigvals;
///
/// let a = array![[1.0_f64, 0.0], [0.0, 2.0]];
/// let w = eigvals(&a.view(), None).expect("Operation failed");
///
/// // Sort eigenvalues (they may be returned in different order)
/// let mut eigenvalues = vec![w[0].re, w[1].re];
/// eigenvalues.sort_by(|a, b| a.partial_cmp(b).expect("Operation failed"));
///
/// assert!((eigenvalues[0] - 1.0).abs() < 1e-10);
/// assert!((eigenvalues[1] - 2.0).abs() < 1e-10);
/// ```
#[allow(dead_code)]
pub fn eigvals<F>(a: &ArrayView2<F>, workers: Option<usize>) -> LinalgResult<Array1<Complex<F>>>
where
    F: Float + NumAssign + Sum + Send + Sync + scirs2_core::ndarray::ScalarOperand + 'static,
{
    // For efficiency, we can compute just the eigenvalues
    // But for now, we'll use the full function and discard the eigenvectors
    let (eigenvalues, _) = eig(a, workers)?;
    Ok(eigenvalues)
}

/// Compute the dominant eigenvalue and eigenvector of a matrix using power iteration.
///
/// This is a simple iterative method that converges to the eigenvalue with the largest
/// absolute value and its corresponding eigenvector.
///
/// # Arguments
///
/// * `a` - Input square matrix
/// * `max_iter` - Maximum number of iterations
/// * `tol` - Convergence tolerance
///
/// # Returns
///
/// * Tuple (eigenvalue, eigenvector)
///
/// # Examples
///
/// ```
/// use scirs2_core::ndarray::array;
/// use scirs2_linalg::eigen::standard::power_iteration;
///
/// let a = array![[2.0_f64, 1.0], [1.0, 3.0]];
/// let (eigenvalue, eigenvector) = power_iteration(&a.view(), 100, 1e-10).expect("Operation failed");
/// // The largest eigenvalue of this matrix is approximately 3.618
/// assert!((eigenvalue - 3.618).abs() < 1e-2);
/// ```
#[allow(dead_code)]
pub fn power_iteration<F>(
    a: &ArrayView2<F>,
    max_iter: usize,
    tol: F,
) -> LinalgResult<(F, Array1<F>)>
where
    F: Float + NumAssign + Sum + Send + Sync + ScalarOperand + 'static,
{
    // Check if matrix is square
    if a.nrows() != a.ncols() {
        return Err(LinalgError::ShapeError(format!(
            "Expected square matrix, got shape {:?}",
            a.shape()
        )));
    }

    let n = a.nrows();

    // Start with a random vector
    let mut rng = scirs2_core::random::rng();
    let mut b = Array1::zeros(n);
    for i in 0..n {
        b[i] = F::from(rng.random_range(-1.0..=1.0)).unwrap_or(F::zero());
    }

    // Normalize the vector
    let norm_b = vector_norm(&b.view(), 2)?;
    b.mapv_inplace(|x| x / norm_b);

    let mut eigenvalue = F::zero();
    let mut prev_eigenvalue = F::zero();

    for _ in 0..max_iter {
        // Multiply b by A
        let mut b_new = Array1::zeros(n);
        for i in 0..n {
            let mut sum = F::zero();
            for j in 0..n {
                sum += a[[i, j]] * b[j];
            }
            b_new[i] = sum;
        }

        // Calculate the Rayleigh quotient (eigenvalue estimate)
        eigenvalue = F::zero();
        for i in 0..n {
            eigenvalue += b[i] * b_new[i];
        }

        // Normalize the vector
        let norm_b_new = vector_norm(&b_new.view(), 2)?;
        for i in 0..n {
            b[i] = b_new[i] / norm_b_new;
        }

        // Check for convergence
        if (eigenvalue - prev_eigenvalue).abs() < tol {
            return Ok((eigenvalue, b));
        }

        prev_eigenvalue = eigenvalue;
    }

    // Return the result after max_iter iterations
    Ok((eigenvalue, b))
}

/// Compute the eigenvalues and eigenvectors of a Hermitian or symmetric matrix.
///
/// # Arguments
///
/// * `a` - Input Hermitian or symmetric matrix
/// * `workers` - Number of worker threads (None = use default)
///
/// # Returns
///
/// * Tuple (eigenvalues, eigenvectors) where eigenvalues is a real vector
///   and eigenvectors is a real matrix whose columns are the eigenvectors
///
/// # Examples
///
/// ```
/// use scirs2_core::ndarray::array;
/// use scirs2_linalg::eigen::standard::eigh;
///
/// let a = array![[1.0_f64, 0.0], [0.0, 2.0]];
/// let (w, v) = eigh(&a.view(), None).expect("Operation failed");
///
/// // Sort eigenvalues (they may be returned in different order)
/// let mut eigenvalues = vec![(w[0], 0), (w[1], 1)];
/// eigenvalues.sort_by(|a, b| a.0.partial_cmp(&b.0).expect("Operation failed"));
///
/// assert!((eigenvalues[0].0 - 1.0).abs() < 1e-10);
/// assert!((eigenvalues[1].0 - 2.0).abs() < 1e-10);
/// ```
#[allow(dead_code)]
pub fn eigh<F>(a: &ArrayView2<F>, workers: Option<usize>) -> LinalgResult<(Array1<F>, Array2<F>)>
where
    F: Float + NumAssign + Sum + Send + Sync + scirs2_core::ndarray::ScalarOperand + 'static,
{
    use crate::parallel;

    // Configure workers for parallel operations
    parallel::configure_workers(workers);

    // Check if matrix is square
    if a.nrows() != a.ncols() {
        return Err(LinalgError::ShapeError(format!(
            "Expected square matrix, got shape {:?}",
            a.shape()
        )));
    }

    // Check if matrix is symmetric
    for i in 0..a.nrows() {
        for j in (i + 1)..a.ncols() {
            if (a[[i, j]] - a[[j, i]]).abs() > F::epsilon() {
                return Err(LinalgError::ShapeError(
                    "Matrix must be symmetric for Hermitian eigenvalue computation".to_string(),
                ));
            }
        }
    }

    let n = a.nrows();

    // For small matrices, we can compute eigenvalues directly
    if n == 1 {
        let eigenvalue = a[[0, 0]];
        let eigenvector = Array2::eye(1);

        return Ok((Array1::from_elem(1, eigenvalue), eigenvector));
    } else if n == 2 {
        return solve_2x2_symmetric_eigenvalue_problem(a);
    } else if n == 3 {
        return solve_3x3_symmetric_eigenvalue_problem(a);
    } else if n == 4 {
        return solve_4x4_symmetric_eigenvalue_problem(a);
    }

    // Choose parallel implementation based on matrix size and worker count
    let use_work_stealing = if let Some(num_workers) = workers {
        // For larger matrices and multiple workers, use work-stealing
        num_workers > 1 && n > 100
    } else {
        false
    };

    // Use work-stealing parallel implementation for large matrices
    if use_work_stealing {
        if let Some(num_workers) = workers {
            return crate::parallel::parallel_eigvalsh_work_stealing(a, num_workers);
        }
    }

    // For smaller matrices, use optimized sequential algorithms
    solve_symmetric_with_power_iteration(a)
}

/// Advanced MODE ENHANCEMENT: Advanced-precision eigenvalue computation targeting 1e-10+ accuracy
///
/// This function implements advanced numerical techniques for maximum precision:
/// - Kahan summation for enhanced numerical stability
/// - Multiple-stage Rayleigh quotient iteration with advanced-tight convergence
/// - Newton's method eigenvalue correction
/// - Adaptive tolerance selection based on matrix condition number
/// - Enhanced Gram-Schmidt orthogonalization with multiple passes
#[allow(dead_code)]
pub fn advanced_precision_eig<F>(
    a: &ArrayView2<F>,
    workers: Option<usize>,
) -> LinalgResult<(Array1<F>, Array2<F>)>
where
    F: Float + NumAssign + Sum + Send + Sync + scirs2_core::ndarray::ScalarOperand + 'static,
{
    use crate::parallel;

    // Configure workers for parallel operations
    parallel::configure_workers(workers);

    // Check if matrix is square and symmetric
    if a.nrows() != a.ncols() {
        return Err(LinalgError::ShapeError(format!(
            "Expected square matrix, got shape {:?}",
            a.shape()
        )));
    }

    let n = a.nrows();

    // Estimate matrix condition number for adaptive tolerance
    let condition_number = estimate_condition_number(a)?;

    // Select precision target based on condition number
    let precision_target =
        if condition_number > F::from(1e12).expect("Failed to convert constant to float") {
            F::from(1e-8).expect("Failed to convert constant to float") // Difficult matrices
        } else if condition_number > F::from(1e8).expect("Failed to convert constant to float") {
            F::from(1e-9).expect("Failed to convert constant to float") // Moderately conditioned
        } else {
            F::from(1e-10).expect("Failed to convert constant to float") // Well-conditioned matrices
        };

    // For small matrices, use advanced-precise analytical methods
    if n <= 4 {
        return advanced_precise_smallmatrix_eig(a, precision_target);
    }

    // For larger matrices, use enhanced iterative methods
    advanced_precise_iterative_eig(a, precision_target, workers)
}

/// Estimate matrix condition number for adaptive tolerance selection
#[allow(dead_code)]
fn estimate_condition_number<F>(a: &ArrayView2<F>) -> LinalgResult<F>
where
    F: Float + NumAssign + Sum + Send + Sync + scirs2_core::ndarray::ScalarOperand + 'static,
{
    let n = a.nrows();

    if n == 1 {
        return Ok(F::one());
    }

    // Use power iteration to estimate largest eigenvalue
    let (lambda_max_, _) = power_iteration(
        a,
        100,
        F::from(1e-8).expect("Failed to convert constant to float"),
    )?;

    // Estimate smallest eigenvalue using inverse iteration
    // For simplicity, use a heuristic based on trace and determinant
    let _trace = (0..n).map(|i| a[[i, i]]).fold(F::zero(), |acc, x| acc + x);
    let det_estimate = if n == 2 {
        a[[0, 0]] * a[[1, 1]] - a[[0, 1]] * a[[1, 0]]
    } else {
        // Use geometric mean of diagonal elements as rough estimate
        let product = (0..n)
            .map(|i| a[[i, i]].abs())
            .fold(F::one(), |acc, x| acc * x);
        if product > F::zero() {
            product.powf(F::one() / F::from(n).expect("Failed to convert to float"))
        } else {
            F::from(1e-15).expect("Failed to convert constant to float") // Very small positive value
        }
    };

    let lambda_min =
        if det_estimate.abs() > F::from(1e-15).expect("Failed to convert constant to float") {
            det_estimate / lambda_max_.powf(F::from(n - 1).expect("Failed to convert to float"))
        } else {
            F::from(1e-15).expect("Failed to convert constant to float")
        };

    let condition = lambda_max_.abs()
        / lambda_min
            .abs()
            .max(F::from(1e-15).expect("Failed to convert constant to float"));
    Ok(condition)
}

/// Advanced-precise eigenvalue computation for small matrices (n <= 4)
#[allow(dead_code)]
fn advanced_precise_smallmatrix_eig<F>(
    a: &ArrayView2<F>,
    precision_target: F,
) -> LinalgResult<(Array1<F>, Array2<F>)>
where
    F: Float + NumAssign + Sum + Send + Sync + scirs2_core::ndarray::ScalarOperand + 'static,
{
    let n = a.nrows();

    match n {
        1 => {
            let eigenvalue = a[[0, 0]];
            let eigenvector = Array2::eye(1);
            Ok((Array1::from_elem(1, eigenvalue), eigenvector))
        }
        2 => advanced_precise_2x2_eig(a, precision_target),
        3 => advanced_precise_3x3_eig(a, precision_target),
        4 => advanced_precise_4x4_eig(a, precision_target),
        _ => Err(LinalgError::InvalidInput(
            "Matrix size not supported for advanced-precise small matrix solver".to_string(),
        )),
    }
}

/// Advanced-precise 2x2 eigenvalue computation with Kahan summation
#[allow(dead_code)]
fn advanced_precise_2x2_eig<F>(
    a: &ArrayView2<F>,
    _precision_target: F,
) -> LinalgResult<(Array1<F>, Array2<F>)>
where
    F: Float + NumAssign + Sum + Send + Sync + scirs2_core::ndarray::ScalarOperand + 'static,
{
    let a11 = a[[0, 0]];
    let a12 = a[[0, 1]];
    let a21 = a[[1, 0]];
    let a22 = a[[1, 1]];

    // Use Kahan summation for enhanced precision
    let trace = kahan_sum(&[a11, a22]);
    let det = kahan_sum(&[a11 * a22, -(a12 * a21)]);

    // Enhanced discriminant calculation
    let trace_squared = trace * trace;
    let four_det = F::from(4.0).expect("Failed to convert constant to float") * det;
    let discriminant = kahan_sum(&[trace_squared, -four_det]);

    let half = F::from(0.5).expect("Failed to convert constant to float");
    let sqrt_discriminant = discriminant.abs().sqrt();

    let lambda1 = if discriminant >= F::zero() {
        (trace + sqrt_discriminant) * half
    } else {
        trace * half
    };

    let lambda2 = if discriminant >= F::zero() {
        (trace - sqrt_discriminant) * half
    } else {
        trace * half
    };

    let mut eigenvalues = Array1::zeros(2);
    eigenvalues[0] = lambda1;
    eigenvalues[1] = lambda2;

    // Enhanced eigenvector computation with Gram-Schmidt orthogonalization
    let eigenvectors = compute_advanced_precise_eigenvectors_2x2(a, &eigenvalues)?;

    Ok((eigenvalues, eigenvectors))
}

/// Advanced-precise 3x3 eigenvalue computation using Cardano's formula with enhancements
#[allow(dead_code)]
fn advanced_precise_3x3_eig<F>(
    a: &ArrayView2<F>,
    precision_target: F,
) -> LinalgResult<(Array1<F>, Array2<F>)>
where
    F: Float + NumAssign + Sum + Send + Sync + scirs2_core::ndarray::ScalarOperand + 'static,
{
    // Enhanced cubic equation solver with numerical stability improvements
    let characteristic_poly = compute_characteristic_polynomial_3x3(a)?;
    let eigenvalues =
        solve_cubic_equation_advanced_precise(&characteristic_poly, precision_target)?;

    // Enhanced eigenvector computation with multiple Gram-Schmidt passes
    let eigenvectors =
        compute_advanced_precise_eigenvectors_3x3(a, &eigenvalues, precision_target)?;

    Ok((eigenvalues, eigenvectors))
}

/// Advanced-precise 4x4 eigenvalue computation using enhanced QR iteration
#[allow(dead_code)]
fn advanced_precise_4x4_eig<F>(
    a: &ArrayView2<F>,
    precision_target: F,
) -> LinalgResult<(Array1<F>, Array2<F>)>
where
    F: Float + NumAssign + Sum + Send + Sync + scirs2_core::ndarray::ScalarOperand + 'static,
{
    // Use enhanced QR iteration with double shifting and deflation
    advanced_precise_qr_iteration(a, precision_target, 1000)
}

/// Kahan summation algorithm for enhanced numerical precision
#[allow(dead_code)]
fn kahan_sum<F>(values: &[F]) -> F
where
    F: Float + NumAssign,
{
    let mut sum = F::zero();
    let mut c = F::zero(); // Compensation for lost low-order bits

    for &value in values {
        let y = value - c;
        let t = sum + y;
        c = (t - sum) - y;
        sum = t;
    }

    sum
}

/// Compute characteristic polynomial for 3x3 matrix with enhanced precision
#[allow(dead_code)]
fn compute_characteristic_polynomial_3x3<F>(a: &ArrayView2<F>) -> LinalgResult<[F; 4]>
where
    F: Float + NumAssign + Sum + Send + Sync + scirs2_core::ndarray::ScalarOperand + 'static,
{
    let a00 = a[[0, 0]];
    let a01 = a[[0, 1]];
    let a02 = a[[0, 2]];
    let a10 = a[[1, 0]];
    let a11 = a[[1, 1]];
    let a12 = a[[1, 2]];
    let a20 = a[[2, 0]];
    let a21 = a[[2, 1]];
    let a22 = a[[2, 2]];

    // Coefficients of characteristic polynomial: c₃λ³ + c₂λ² + c₁λ + c₀ = 0
    let c3 = -F::one(); // Coefficient of λ³

    // Coefficient of λ² (negative trace)
    let c2 = kahan_sum(&[-a00, -a11, -a22]);

    // Coefficient of λ (sum of principal minors)
    let minor_00_11 = a00 * a11 - a01 * a10;
    let minor_00_22 = a00 * a22 - a02 * a20;
    let minor_11_22 = a11 * a22 - a12 * a21;
    let c1 = kahan_sum(&[minor_00_11, minor_00_22, minor_11_22]);

    // Constant term (negative determinant)
    let det_part1 = a00 * (a11 * a22 - a12 * a21);
    let det_part2 = -a01 * (a10 * a22 - a12 * a20);
    let det_part3 = a02 * (a10 * a21 - a11 * a20);
    let c0 = -kahan_sum(&[det_part1, det_part2, det_part3]);

    Ok([c0, c1, c2, c3])
}

/// Solve cubic equation with advanced-high precision using Cardano's formula with enhancements
#[allow(dead_code)]
fn solve_cubic_equation_advanced_precise<F>(
    coeffs: &[F; 4],
    precision_target: F,
) -> LinalgResult<Array1<F>>
where
    F: Float + NumAssign + Sum + Send + Sync + scirs2_core::ndarray::ScalarOperand + 'static,
{
    let [c0, c1, c2, c3] = *coeffs;

    // Normalize to monic polynomial
    let a = c2 / c3;
    let b = c1 / c3;
    let c = c0 / c3;

    // Depressed cubic transformation: t³ + pt + q = 0
    let third = F::one() / F::from(3.0).expect("Failed to convert constant to float");
    let p = b - a * a * third;
    let q_part1 = F::from(2.0).expect("Failed to convert constant to float") * a * a * a
        / F::from(27.0).expect("Failed to convert constant to float");
    let q_part2 = -a * b * third;
    let q = kahan_sum(&[q_part1, q_part2, c]);

    // Enhanced discriminant calculation
    let discriminant_part1 =
        (q / F::from(2.0).expect("Failed to convert constant to float")).powi(2);
    let discriminant_part2 =
        (p / F::from(3.0).expect("Failed to convert constant to float")).powi(3);
    let discriminant = discriminant_part1 + discriminant_part2;

    let mut roots = Array1::zeros(3);

    if discriminant > precision_target {
        // One real root case - use enhanced Cardano's formula
        let sqrt_disc = discriminant.sqrt();
        let half_q = q / F::from(2.0).expect("Failed to convert constant to float");

        let u = if half_q + sqrt_disc >= F::zero() {
            (half_q + sqrt_disc).powf(third)
        } else {
            -(-half_q - sqrt_disc).powf(third)
        };

        let v = if u.abs() > precision_target {
            -p / (F::from(3.0).expect("Failed to convert constant to float") * u)
        } else {
            F::zero()
        };

        let root = u + v - a * third;

        // Apply Newton's method for refinement
        let refined_root = newton_method_cubic_refinement(coeffs, root, precision_target, 20)?;

        roots[0] = refined_root;
        roots[1] = refined_root; // Repeated root approximation
        roots[2] = refined_root;
    } else {
        // Three real roots case - use trigonometric solution
        let m = F::from(2.0).expect("Failed to convert constant to float")
            * (-p / F::from(3.0).expect("Failed to convert constant to float")).sqrt();
        let theta = (F::from(3.0).expect("Failed to convert constant to float") * q / (p * m))
            .acos()
            / F::from(3.0).expect("Failed to convert constant to float");

        let two_pi_third = F::from(2.0).expect("Failed to convert constant to float")
            * F::from(std::f64::consts::PI).expect("Failed to convert to float")
            / F::from(3.0).expect("Failed to convert constant to float");
        let a_third = a * third;

        roots[0] = m * theta.cos() - a_third;
        roots[1] = m * (theta - two_pi_third).cos() - a_third;
        roots[2] = m
            * (theta - F::from(2.0).expect("Failed to convert constant to float") * two_pi_third)
                .cos()
            - a_third;

        // Refine all roots with Newton's method
        for i in 0..3 {
            roots[i] = newton_method_cubic_refinement(coeffs, roots[i], precision_target, 20)?;
        }
    }

    // Sort eigenvalues for consistency
    let mut root_vec: Vec<F> = roots.to_vec();
    root_vec.sort_by(|a, b| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal));

    for (i, &root) in root_vec.iter().enumerate() {
        roots[i] = root;
    }

    Ok(roots)
}

/// Newton's method for cubic equation root refinement
#[allow(dead_code)]
fn newton_method_cubic_refinement<F>(
    coeffs: &[F; 4],
    initial_guess: F,
    tolerance: F,
    max_iter: usize,
) -> LinalgResult<F>
where
    F: Float + NumAssign,
{
    let [c0, c1, c2, c3] = *coeffs;
    let mut x = initial_guess;

    for _ in 0..max_iter {
        // Evaluate polynomial and its derivative using Horner's method
        let f_x = ((c3 * x + c2) * x + c1) * x + c0;
        let df_x = (F::from(3.0).expect("Failed to convert constant to float") * c3 * x
            + F::from(2.0).expect("Failed to convert constant to float") * c2)
            * x
            + c1;

        if df_x.abs() < tolerance {
            break; // Avoid division by zero
        }

        let x_new = x - f_x / df_x;

        if (x_new - x).abs() < tolerance {
            return Ok(x_new);
        }

        x = x_new;
    }

    Ok(x)
}

/// Compute advanced-precise eigenvectors for 2x2 matrix
#[allow(dead_code)]
fn compute_advanced_precise_eigenvectors_2x2<F>(
    a: &ArrayView2<F>,
    eigenvalues: &Array1<F>,
) -> LinalgResult<Array2<F>>
where
    F: Float + NumAssign + Sum + Send + Sync + scirs2_core::ndarray::ScalarOperand + 'static,
{
    let mut eigenvectors = Array2::zeros((2, 2));

    for (i, &lambda) in eigenvalues.iter().enumerate() {
        let mut v = Array1::zeros(2);

        // Enhanced eigenvector computation with numerical stability
        let a11_lambda = a[[0, 0]] - lambda;
        let a22_lambda = a[[1, 1]] - lambda;

        if a[[0, 1]].abs() > a[[1, 0]].abs() {
            if a[[0, 1]].abs() > F::epsilon() {
                v[0] = F::one();
                v[1] = -a11_lambda / a[[0, 1]];
            } else {
                v[0] = F::one();
                v[1] = F::zero();
            }
        } else if a[[1, 0]].abs() > F::epsilon() {
            v[0] = -a22_lambda / a[[1, 0]];
            v[1] = F::one();
        } else {
            // Diagonal case
            if a11_lambda.abs() < a22_lambda.abs() {
                v[0] = F::one();
                v[1] = F::zero();
            } else {
                v[0] = F::zero();
                v[1] = F::one();
            }
        }

        // Enhanced normalization with numerical stability
        let norm = vector_norm(&v.view(), 2)?;
        if norm > F::epsilon() {
            v.mapv_inplace(|x| x / norm);
        }

        eigenvectors.column_mut(i).assign(&v);
    }

    // Apply Gram-Schmidt orthogonalization for enhanced precision
    gram_schmidt_orthogonalization(&mut eigenvectors)?;

    Ok(eigenvectors)
}

/// Compute advanced-precise eigenvectors for 3x3 matrix
#[allow(dead_code)]
fn compute_advanced_precise_eigenvectors_3x3<F>(
    a: &ArrayView2<F>,
    eigenvalues: &Array1<F>,
    precision_target: F,
) -> LinalgResult<Array2<F>>
where
    F: Float + NumAssign + Sum + Send + Sync + scirs2_core::ndarray::ScalarOperand + 'static,
{
    let mut eigenvectors = Array2::zeros((3, 3));

    for (i, &lambda) in eigenvalues.iter().enumerate() {
        // Compute (A - λI)
        let mut matrix = a.to_owned();
        for j in 0..3 {
            matrix[[j, j]] -= lambda;
        }

        // Find null space using enhanced numerical techniques
        let eigenvector = enhanced_null_space_computation(&matrix.view(), precision_target)?;
        eigenvectors.column_mut(i).assign(&eigenvector);
    }

    // Apply multiple passes of Gram-Schmidt for advanced-high precision
    for _ in 0..3 {
        gram_schmidt_orthogonalization(&mut eigenvectors)?;
    }

    Ok(eigenvectors)
}

/// Enhanced null space computation for eigenvector calculation
#[allow(dead_code)]
fn enhanced_null_space_computation<F>(
    matrix: &ArrayView2<F>,
    precision_target: F,
) -> LinalgResult<Array1<F>>
where
    F: Float + NumAssign + Sum + Send + Sync + scirs2_core::ndarray::ScalarOperand + 'static,
{
    let n = matrix.nrows();
    let mut v = Array1::zeros(n);

    // Use inverse iteration with multiple random starts for robustness
    let mut best_v = v.clone();
    let mut best_residual = F::infinity();

    for _trial in 0..5 {
        // Random initialization
        let mut rng = scirs2_core::random::rng();
        for i in 0..n {
            v[i] = F::from(rng.random_range(-1.0..=1.0)).unwrap_or(F::zero());
        }

        // Normalize
        let norm = vector_norm(&v.view(), 2)?;
        if norm > F::epsilon() {
            v.mapv_inplace(|x| x / norm);
        }

        // Inverse iteration
        for _iter in 0..50 {
            let mut av = Array1::zeros(n);
            for i in 0..n {
                for j in 0..n {
                    av[i] += matrix[[i, j]] * v[j];
                }
            }

            // Check residual
            let residual = vector_norm(&av.view(), 2)?;
            if residual < best_residual {
                best_residual = residual;
                best_v.assign(&v);
            }

            if residual < precision_target {
                break;
            }

            // Update v for next iteration (simplified)
            let norm_v = vector_norm(&v.view(), 2)?;
            if norm_v > F::epsilon() {
                v.mapv_inplace(|x| x / norm_v);
            }
        }
    }

    Ok(best_v)
}

/// Enhanced Gram-Schmidt orthogonalization with numerical stability
#[allow(dead_code)]
fn gram_schmidt_orthogonalization<F>(matrix: &mut Array2<F>) -> LinalgResult<()>
where
    F: Float + NumAssign + Sum + Send + Sync + scirs2_core::ndarray::ScalarOperand + 'static,
{
    let n = matrix.nrows();
    let m = matrix.ncols();

    for i in 0..m {
        // Normalize current column
        let mut col_i = matrix.column(i).to_owned();
        let norm_i = vector_norm(&col_i.view(), 2)?;

        if norm_i > F::epsilon() {
            col_i.mapv_inplace(|x| x / norm_i);
            matrix.column_mut(i).assign(&col_i);
        }

        // Orthogonalize against previous columns
        for j in (i + 1)..m {
            let mut col_j = matrix.column(j).to_owned();

            // Compute projection coefficient with Kahan summation
            let mut dot_product = F::zero();
            let mut c = F::zero();
            for k in 0..n {
                let y = col_i[k] * col_j[k] - c;
                let t = dot_product + y;
                c = (t - dot_product) - y;
                dot_product = t;
            }

            // Remove projection
            for k in 0..n {
                col_j[k] -= dot_product * col_i[k];
            }

            matrix.column_mut(j).assign(&col_j);
        }
    }

    Ok(())
}

/// Advanced-precise iterative eigenvalue computation for large matrices
#[allow(dead_code)]
fn advanced_precise_iterative_eig<F>(
    a: &ArrayView2<F>,
    precision_target: F,
    _workers: Option<usize>,
) -> LinalgResult<(Array1<F>, Array2<F>)>
where
    F: Float + NumAssign + Sum + Send + Sync + scirs2_core::ndarray::ScalarOperand + 'static,
{
    // Enhanced QR iteration with multiple convergence criteria
    advanced_precise_qr_iteration(a, precision_target, 2000)
}

/// Advanced-precise QR iteration with enhanced numerical stability
#[allow(dead_code)]
fn advanced_precise_qr_iteration<F>(
    a: &ArrayView2<F>,
    precision_target: F,
    max_iter: usize,
) -> LinalgResult<(Array1<F>, Array2<F>)>
where
    F: Float + NumAssign + Sum + Send + Sync + scirs2_core::ndarray::ScalarOperand + 'static,
{
    let n = a.nrows();
    let mut h = a.to_owned(); // Hessenberg form would be better, but simplified here
    let mut q_total = Array2::eye(n);

    let mut iter_count = 0;

    while iter_count < max_iter {
        // Check for convergence
        let mut converged = true;
        for i in 1..n {
            for j in 0..i {
                if h[[i, j]].abs() > precision_target {
                    converged = false;
                    break;
                }
            }
            if !converged {
                break;
            }
        }

        if converged {
            break;
        }

        // Enhanced QR decomposition with pivoting
        let (q, r) = enhanced_qr_decomposition(&h.view())?;

        // Update H = RQ
        h = r.dot(&q);

        // Accumulate Q for eigenvectors
        q_total = q_total.dot(&q);

        iter_count += 1;
    }

    // Extract eigenvalues from diagonal
    let mut eigenvalues = Array1::zeros(n);
    for i in 0..n {
        eigenvalues[i] = h[[i, i]];
    }

    // Sort eigenvalues and corresponding eigenvectors
    let mut indices: Vec<usize> = (0..n).collect();
    indices.sort_by(|&a, &b| {
        eigenvalues[a]
            .partial_cmp(&eigenvalues[b])
            .unwrap_or(std::cmp::Ordering::Equal)
    });

    let mut sorted_eigenvalues = Array1::zeros(n);
    let mut sorted_eigenvectors = Array2::zeros((n, n));

    for (new_idx, &old_idx) in indices.iter().enumerate() {
        sorted_eigenvalues[new_idx] = eigenvalues[old_idx];
        sorted_eigenvectors
            .column_mut(new_idx)
            .assign(&q_total.column(old_idx));
    }

    Ok((sorted_eigenvalues, sorted_eigenvectors))
}

/// Enhanced QR decomposition with numerical stability improvements
#[allow(dead_code)]
fn enhanced_qr_decomposition<F>(a: &ArrayView2<F>) -> LinalgResult<(Array2<F>, Array2<F>)>
where
    F: Float + NumAssign + Sum + Send + Sync + scirs2_core::ndarray::ScalarOperand + 'static,
{
    let (m, n) = a.dim();
    let mut q = Array2::eye(m);
    let mut r = a.to_owned();

    for k in 0..n.min(m - 1) {
        // Enhanced Householder reflection with numerical stability
        let mut x = Array1::zeros(m - k);
        for i in k..m {
            x[i - k] = r[[i, k]];
        }

        let norm_x = vector_norm(&x.view(), 2)?;
        if norm_x < F::epsilon() {
            continue;
        }

        let mut v = x.clone();
        v[0] += if x[0] >= F::zero() { norm_x } else { -norm_x };

        let norm_v = vector_norm(&v.view(), 2)?;
        if norm_v > F::epsilon() {
            v.mapv_inplace(|val| val / norm_v);
        }

        // Apply Householder reflection to R
        for j in k..n {
            let mut col = Array1::zeros(m - k);
            for i in k..m {
                col[i - k] = r[[i, j]];
            }

            let dot_product = v.dot(&col);
            for i in k..m {
                r[[i, j]] -= F::from(2.0).expect("Failed to convert constant to float")
                    * dot_product
                    * v[i - k];
            }
        }

        // Apply Householder reflection to Q
        for j in 0..m {
            let mut col = Array1::zeros(m - k);
            for i in k..m {
                col[i - k] = q[[i, j]];
            }

            let dot_product = v.dot(&col);
            for i in k..m {
                q[[i, j]] -= F::from(2.0).expect("Failed to convert constant to float")
                    * dot_product
                    * v[i - k];
            }
        }
    }

    Ok((q.t().to_owned(), r))
}

/// Solve 2x2 general eigenvalue problem using analytical formula
#[allow(dead_code)]
fn solve_2x2_eigenvalue_problem<F>(a: &ArrayView2<F>) -> EigenResult<F>
where
    F: Float + NumAssign + Sum + Send + Sync + ScalarOperand + 'static,
{
    // For 2x2 matrices, use the quadratic formula
    let a11 = a[[0, 0]];
    let a12 = a[[0, 1]];
    let a21 = a[[1, 0]];
    let a22 = a[[1, 1]];

    let trace = a11 + a22;
    let det = a11 * a22 - a12 * a21;

    let discriminant =
        trace * trace - F::from(4.0).expect("Failed to convert constant to float") * det;

    // Create eigenvalues
    let mut eigenvalues = Array1::zeros(2);
    let mut eigenvectors = Array2::zeros((2, 2));

    if discriminant >= F::zero() {
        // Real eigenvalues
        let sqrt_discriminant = discriminant.sqrt();
        let lambda1 = (trace + sqrt_discriminant)
            / F::from(2.0).expect("Failed to convert constant to float");
        let lambda2 = (trace - sqrt_discriminant)
            / F::from(2.0).expect("Failed to convert constant to float");

        eigenvalues[0] = Complex::new(lambda1, F::zero());
        eigenvalues[1] = Complex::new(lambda2, F::zero());

        // Compute eigenvectors
        for (i, &lambda) in [lambda1, lambda2].iter().enumerate() {
            let mut eigenvector = Array1::zeros(2);

            if a12 != F::zero() {
                eigenvector[0] = a12;
                eigenvector[1] = lambda - a11;
            } else if a21 != F::zero() {
                eigenvector[0] = lambda - a22;
                eigenvector[1] = a21;
            } else {
                // Diagonal matrix
                eigenvector[0] = if (a11 - lambda).abs() < F::epsilon() {
                    F::one()
                } else {
                    F::zero()
                };
                eigenvector[1] = if (a22 - lambda).abs() < F::epsilon() {
                    F::one()
                } else {
                    F::zero()
                };
            }

            // Normalize
            let norm = vector_norm(&eigenvector.view(), 2)?;
            if norm > F::epsilon() {
                eigenvector.mapv_inplace(|x| x / norm);
            }

            eigenvectors.column_mut(i).assign(&eigenvector);
        }

        // Convert to complex
        let complex_eigenvectors = eigenvectors.mapv(|x| Complex::new(x, F::zero()));

        Ok((eigenvalues, complex_eigenvectors))
    } else {
        // Complex eigenvalues
        let real_part = trace / F::from(2.0).expect("Failed to convert constant to float");
        let imag_part =
            (-discriminant).sqrt() / F::from(2.0).expect("Failed to convert constant to float");

        eigenvalues[0] = Complex::new(real_part, imag_part);
        eigenvalues[1] = Complex::new(real_part, -imag_part);

        // Compute complex eigenvectors
        let mut complex_eigenvectors = Array2::zeros((2, 2));

        if a12 != F::zero() {
            complex_eigenvectors[[0, 0]] = Complex::new(a12, F::zero());
            complex_eigenvectors[[1, 0]] = Complex::new(eigenvalues[0].re - a11, eigenvalues[0].im);

            complex_eigenvectors[[0, 1]] = Complex::new(a12, F::zero());
            complex_eigenvectors[[1, 1]] = Complex::new(eigenvalues[1].re - a11, eigenvalues[1].im);
        } else if a21 != F::zero() {
            complex_eigenvectors[[0, 0]] = Complex::new(eigenvalues[0].re - a22, eigenvalues[0].im);
            complex_eigenvectors[[1, 0]] = Complex::new(a21, F::zero());

            complex_eigenvectors[[0, 1]] = Complex::new(eigenvalues[1].re - a22, eigenvalues[1].im);
            complex_eigenvectors[[1, 1]] = Complex::new(a21, F::zero());
        }

        // Normalize complex eigenvectors
        for i in 0..2 {
            let mut norm_sq = Complex::new(F::zero(), F::zero());
            for j in 0..2 {
                norm_sq += complex_eigenvectors[[j, i]] * complex_eigenvectors[[j, i]].conj();
            }
            let norm = norm_sq.re.sqrt();

            if norm > F::epsilon() {
                for j in 0..2 {
                    complex_eigenvectors[[j, i]] /= Complex::new(norm, F::zero());
                }
            }
        }

        Ok((eigenvalues, complex_eigenvectors))
    }
}

/// Solve 2x2 symmetric eigenvalue problem
#[allow(dead_code)]
fn solve_2x2_symmetric_eigenvalue_problem<F>(
    a: &ArrayView2<F>,
) -> LinalgResult<(Array1<F>, Array2<F>)>
where
    F: Float + NumAssign + Sum + Send + Sync + ScalarOperand + 'static,
{
    // For 2x2 symmetric matrices
    let a11 = a[[0, 0]];
    let a12 = a[[0, 1]];
    let a22 = a[[1, 1]];

    let trace = a11 + a22;
    let det = a11 * a22 - a12 * a12; // For symmetric matrices, a12 = a21

    let discriminant =
        trace * trace - F::from(4.0).expect("Failed to convert constant to float") * det;
    let sqrt_discriminant = discriminant.sqrt();

    // Eigenvalues
    let lambda1 =
        (trace + sqrt_discriminant) / F::from(2.0).expect("Failed to convert constant to float");
    let lambda2 =
        (trace - sqrt_discriminant) / F::from(2.0).expect("Failed to convert constant to float");

    // Sort eigenvalues in ascending order (SciPy convention)
    let (lambda_small, lambda_large) = if lambda1 <= lambda2 {
        (lambda1, lambda2)
    } else {
        (lambda2, lambda1)
    };

    let mut eigenvalues = Array1::zeros(2);
    eigenvalues[0] = lambda_small;
    eigenvalues[1] = lambda_large;

    // Eigenvectors
    let mut eigenvectors = Array2::zeros((2, 2));

    // Compute eigenvector for smaller eigenvalue (first)
    if a12 != F::zero() {
        eigenvectors[[0, 0]] = a12;
        eigenvectors[[1, 0]] = lambda_small - a11;
    } else {
        // Diagonal matrix
        eigenvectors[[0, 0]] = if (a11 - lambda_small).abs() < F::epsilon() {
            F::one()
        } else {
            F::zero()
        };
        eigenvectors[[1, 0]] = if (a22 - lambda_small).abs() < F::epsilon() {
            F::one()
        } else {
            F::zero()
        };
    }

    // Normalize
    let norm1 = (eigenvectors[[0, 0]] * eigenvectors[[0, 0]]
        + eigenvectors[[1, 0]] * eigenvectors[[1, 0]])
    .sqrt();
    if norm1 > F::epsilon() {
        eigenvectors[[0, 0]] /= norm1;
        eigenvectors[[1, 0]] /= norm1;
    }

    // Compute eigenvector for larger eigenvalue (second)
    if a12 != F::zero() {
        eigenvectors[[0, 1]] = a12;
        eigenvectors[[1, 1]] = lambda_large - a11;
    } else {
        // Diagonal matrix
        eigenvectors[[0, 1]] = if (a11 - lambda_large).abs() < F::epsilon() {
            F::one()
        } else {
            F::zero()
        };
        eigenvectors[[1, 1]] = if (a22 - lambda_large).abs() < F::epsilon() {
            F::one()
        } else {
            F::zero()
        };
    }

    // Normalize
    let norm2 = (eigenvectors[[0, 1]] * eigenvectors[[0, 1]]
        + eigenvectors[[1, 1]] * eigenvectors[[1, 1]])
    .sqrt();
    if norm2 > F::epsilon() {
        eigenvectors[[0, 1]] /= norm2;
        eigenvectors[[1, 1]] /= norm2;
    }

    Ok((eigenvalues, eigenvectors))
}

/// Solve 3x3 symmetric eigenvalue problem using analytical methods
/// Based on "Efficient numerical diagonalization of hermitian 3x3 matrices" by Kopp (2008)
#[allow(dead_code)]
fn solve_3x3_symmetric_eigenvalue_problem<F>(
    a: &ArrayView2<F>,
) -> LinalgResult<(Array1<F>, Array2<F>)>
where
    F: Float + NumAssign + Sum + Send + Sync + ScalarOperand + 'static,
{
    // For 3x3 symmetric matrices, use a specialized QR iteration
    // that converges quickly for small matrices

    let mut workmatrix = a.to_owned();
    let mut q_total = Array2::eye(3);
    let max_iter = 50;
    let tol = F::from(1e-12).expect("Failed to convert constant to float");

    // Apply QR iterations
    for _ in 0..max_iter {
        // Check for convergence - if off-diagonal elements are small
        let off_diag =
            workmatrix[[0, 1]].abs() + workmatrix[[0, 2]].abs() + workmatrix[[1, 2]].abs();
        if off_diag < tol {
            break;
        }

        // Perform QR decomposition
        let (q, r) = qr(&workmatrix.view(), None)?;

        // Update: A = R * Q
        workmatrix = r.dot(&q);

        // Accumulate transformation
        q_total = q_total.dot(&q);
    }

    // Extract eigenvalues from diagonal
    let mut eigenvalues = Array1::zeros(3);
    for i in 0..3 {
        eigenvalues[i] = workmatrix[[i, i]];
    }

    // Sort eigenvalues and corresponding eigenvectors
    let mut indices = [0, 1, 2];
    indices.sort_by(|&i, &j| {
        eigenvalues[i]
            .partial_cmp(&eigenvalues[j])
            .expect("Operation failed")
    });

    let mut sorted_eigenvalues = Array1::zeros(3);
    let mut sorted_eigenvectors = Array2::zeros((3, 3));

    for (new_idx, &old_idx) in indices.iter().enumerate() {
        sorted_eigenvalues[new_idx] = eigenvalues[old_idx];
        for i in 0..3 {
            sorted_eigenvectors[[i, new_idx]] = q_total[[i, old_idx]];
        }
    }

    Ok((sorted_eigenvalues, sorted_eigenvectors))
}

/// Solve 4x4 symmetric eigenvalue problem using QR iteration
#[allow(dead_code)]
fn solve_4x4_symmetric_eigenvalue_problem<F>(
    a: &ArrayView2<F>,
) -> LinalgResult<(Array1<F>, Array2<F>)>
where
    F: Float + NumAssign + Sum + Send + Sync + ScalarOperand + 'static,
{
    // For 4x4 symmetric matrices, use a specialized QR iteration
    // that converges quickly for small matrices

    let mut workmatrix = a.to_owned();
    let mut q_total = Array2::eye(4);
    let max_iter = 100;
    let tol = F::from(1e-12).expect("Failed to convert constant to float");

    // Apply QR iterations
    for _ in 0..max_iter {
        // Check for convergence - if off-diagonal elements are small
        let mut off_diag = F::zero();
        for i in 0..4 {
            for j in (i + 1)..4 {
                off_diag += workmatrix[[i, j]].abs();
            }
        }
        if off_diag < tol {
            break;
        }

        // Perform QR decomposition
        let (q, r) = qr(&workmatrix.view(), None)?;

        // Update: A = R * Q
        workmatrix = r.dot(&q);

        // Accumulate transformation
        q_total = q_total.dot(&q);
    }

    // Extract eigenvalues from diagonal
    let mut eigenvalues = Array1::zeros(4);
    for i in 0..4 {
        eigenvalues[i] = workmatrix[[i, i]];
    }

    // Sort eigenvalues and corresponding eigenvectors
    let mut indices = [0, 1, 2, 3];
    indices.sort_by(|&i, &j| {
        eigenvalues[i]
            .partial_cmp(&eigenvalues[j])
            .expect("Operation failed")
    });

    let mut sorted_eigenvalues = Array1::zeros(4);
    let mut sorted_eigenvectors = Array2::zeros((4, 4));

    for (new_idx, &old_idx) in indices.iter().enumerate() {
        sorted_eigenvalues[new_idx] = eigenvalues[old_idx];
        for i in 0..4 {
            sorted_eigenvectors[[i, new_idx]] = q_total[[i, old_idx]];
        }
    }

    Ok((sorted_eigenvalues, sorted_eigenvectors))
}

/// QR algorithm for general eigenvalue decomposition
#[allow(dead_code)]
fn solve_qr_algorithm<F>(a: &ArrayView2<F>) -> EigenResult<F>
where
    F: Float + NumAssign + Sum + Send + Sync + ScalarOperand + 'static,
{
    // For larger matrices, use the QR algorithm
    let mut a_k = a.to_owned();
    let n = a.nrows();
    let max_iter = 100;
    let tol = F::epsilon() * F::from(100.0).expect("Failed to convert constant to float");

    // Initialize eigenvalues and eigenvectors
    let mut eigenvalues = Array1::zeros(n);
    let mut eigenvectors = Array2::eye(n);

    for _iter in 0..max_iter {
        // QR decomposition
        let (q, r) = qr(&a_k.view(), None)?;

        // Update A_k+1 = R*Q (reversed order gives better convergence)
        let a_next = r.dot(&q);

        // Update eigenvectors: V_k+1 = V_k * Q
        eigenvectors = eigenvectors.dot(&q);

        // Check for convergence (check if subdiagonal elements are close to zero)
        let mut converged = true;
        for i in 1..n {
            if a_next[[i, i - 1]].abs() > tol {
                converged = false;
                break;
            }
        }

        if converged {
            // Extract eigenvalues from diagonal
            for i in 0..n {
                eigenvalues[i] = Complex::new(a_next[[i, i]], F::zero());
            }

            // Return as complex values
            let complex_eigenvectors = eigenvectors.mapv(|x| Complex::new(x, F::zero()));
            return Ok((eigenvalues, complex_eigenvectors));
        }

        // If not converged, continue with next iteration
        a_k = a_next;
    }

    // If we reached maximum iterations without convergence
    // Check if we at least have a reasonable approximation
    let mut eigenvals = Array1::zeros(n);
    for i in 0..n {
        eigenvals[i] = Complex::new(a_k[[i, i]], F::zero());
    }

    // Return the best approximation we have
    let complex_eigenvectors = eigenvectors.mapv(|x| Complex::new(x, F::zero()));
    Ok((eigenvals, complex_eigenvectors))
}

/// Solve symmetric matrices with power iteration (simplified implementation)
#[allow(dead_code)]
fn solve_symmetric_with_power_iteration<F>(
    a: &ArrayView2<F>,
) -> LinalgResult<(Array1<F>, Array2<F>)>
where
    F: Float + NumAssign + Sum + Send + Sync + ScalarOperand + 'static,
{
    use crate::decomposition::qr;
    use crate::norm::vector_norm;

    let n = a.nrows();
    let max_iter = 1000;
    let tolerance =
        F::from(1e-10).unwrap_or_else(|| F::epsilon() * F::from(100.0).unwrap_or(F::one()));

    // Use QR algorithm for symmetric matrices
    // This is more stable than power iteration for all eigenvalues

    // Create a working copy of the matrix
    let mut workmatrix = a.to_owned();
    let mut q_accumulated = Array2::eye(n);

    // Apply QR iterations with shifting for better convergence
    for iter in 0..max_iter {
        // Check for convergence by examining off-diagonal elements
        let mut max_off_diag = F::zero();
        for i in 0..n {
            for j in 0..n {
                if i != j {
                    let abs_val = workmatrix[[i, j]].abs();
                    if abs_val > max_off_diag {
                        max_off_diag = abs_val;
                    }
                }
            }
        }

        if max_off_diag < tolerance {
            break;
        }

        // Apply shift to improve convergence
        // Use Wilkinson shift: the eigenvalue of the 2x2 bottom-right submatrix
        // that is closer to the bottom-right element
        let shift = if n >= 2 {
            let a_nn = workmatrix[[n - 1, n - 1]];
            let a_n1n1 = workmatrix[[n - 2, n - 2]];
            let a_nn1 = workmatrix[[n - 1, n - 2]];

            let b = a_nn + a_n1n1;
            let c = a_nn * a_n1n1 - a_nn1 * a_nn1;
            let discriminant = b * b - F::from(4.0).unwrap_or(F::one()) * c;

            if discriminant >= F::zero() {
                let sqrt_disc = discriminant.sqrt();
                let lambda1 = (b + sqrt_disc) / F::from(2.0).unwrap_or(F::one());
                let lambda2 = (b - sqrt_disc) / F::from(2.0).unwrap_or(F::one());

                // Choose the eigenvalue closer to a_nn
                if (lambda1 - a_nn).abs() < (lambda2 - a_nn).abs() {
                    lambda1
                } else {
                    lambda2
                }
            } else {
                a_nn
            }
        } else {
            workmatrix[[n - 1, n - 1]]
        };

        // Apply shift: A' = A - σI
        for i in 0..n {
            workmatrix[[i, i]] -= shift;
        }

        // Perform QR decomposition
        let (q, r) = qr(&workmatrix.view(), None).map_err(|_| {
            LinalgError::ConvergenceError(format!(
                "QR decomposition failed in symmetric eigenvalue computation at iteration {iter}"
            ))
        })?;

        // Update: A = RQ + σI
        workmatrix = r.dot(&q);
        for i in 0..n {
            workmatrix[[i, i]] += shift;
        }

        // Accumulate transformation
        q_accumulated = q_accumulated.dot(&q);

        // Early termination for well-conditioned problems
        if iter > 10 && max_off_diag < tolerance * F::from(10.0).unwrap_or(F::one()) {
            break;
        }
    }

    // Extract eigenvalues from diagonal
    let mut eigenvalues = Array1::zeros(n);
    for i in 0..n {
        eigenvalues[i] = workmatrix[[i, i]];
    }

    // Sort eigenvalues and eigenvectors in descending order
    let mut indices: Vec<usize> = (0..n).collect();
    indices.sort_by(|&i, &j| {
        eigenvalues[j]
            .partial_cmp(&eigenvalues[i])
            .unwrap_or(std::cmp::Ordering::Equal)
    });

    let sorted_eigenvalues = Array1::from_iter(indices.iter().map(|&i| eigenvalues[i]));
    let mut sorted_eigenvectors = Array2::zeros((n, n));

    for (new_idx, &old_idx) in indices.iter().enumerate() {
        for i in 0..n {
            sorted_eigenvectors[[i, new_idx]] = q_accumulated[[i, old_idx]];
        }
    }

    // Normalize eigenvectors
    for j in 0..n {
        let mut col = sorted_eigenvectors.column_mut(j);
        let norm = vector_norm(&col.to_owned().view(), 2)?;

        if norm > F::epsilon() {
            col.mapv_inplace(|x| x / norm);
        }

        // Ensure consistent sign convention (largest component positive)
        let mut max_idx = 0;
        let mut max_abs = F::zero();
        for i in 0..n {
            let abs_val = col[i].abs();
            if abs_val > max_abs {
                max_abs = abs_val;
                max_idx = i;
            }
        }

        if col[max_idx] < F::zero() {
            col.mapv_inplace(|x| x * (-F::one()));
        }
    }

    Ok((sorted_eigenvalues, sorted_eigenvectors))
}

#[cfg(test)]
mod tests {
    use super::*;
    use approx::assert_relative_eq;
    use scirs2_core::ndarray::array;

    #[test]
    fn test_1x1matrix() {
        let a = array![[3.0_f64]];
        let (eigenvalues, eigenvectors) = eig(&a.view(), None).expect("Operation failed");

        assert_relative_eq!(eigenvalues[0].re, 3.0, epsilon = 1e-10);
        assert_relative_eq!(eigenvalues[0].im, 0.0, epsilon = 1e-10);
        assert_relative_eq!(eigenvectors[[0, 0]].re, 1.0, epsilon = 1e-10);
        assert_relative_eq!(eigenvectors[[0, 0]].im, 0.0, epsilon = 1e-10);
    }

    #[test]
    fn test_2x2_diagonalmatrix() {
        let a = array![[3.0_f64, 0.0], [0.0, 4.0]];

        let (eigenvalues, eigenvectors) = eig(&a.view(), None).expect("Operation failed");

        // Eigenvalues could be returned in any order
        assert_relative_eq!(eigenvalues[0].im, 0.0, epsilon = 1e-10);
        assert_relative_eq!(eigenvalues[1].im, 0.0, epsilon = 1e-10);

        // Test eigh
        let (eigenvalues, eigenvectors) = eigh(&a.view(), None).expect("Operation failed");
        // The eigenvalues might be returned in a different order
        assert!(
            (eigenvalues[0] - 3.0).abs() < 1e-10 && (eigenvalues[1] - 4.0).abs() < 1e-10
                || (eigenvalues[1] - 3.0).abs() < 1e-10 && (eigenvalues[0] - 4.0).abs() < 1e-10
        );
    }

    #[test]
    fn test_2x2_symmetricmatrix() {
        let a = array![[1.0, 2.0], [2.0, 4.0]];

        // Test eigh
        let (eigenvalues, eigenvectors) = eigh(&a.view(), None).expect("Operation failed");

        // Eigenvalues should be approximately 5 and 0
        assert!(
            (eigenvalues[0] - 5.0).abs() < 1e-10 && eigenvalues[1].abs() < 1e-10
                || (eigenvalues[1] - 5.0).abs() < 1e-10 && eigenvalues[0].abs() < 1e-10
        );

        // Check that eigenvectors are orthogonal
        let dot_product = eigenvectors[[0, 0]] * eigenvectors[[0, 1]]
            + eigenvectors[[1, 0]] * eigenvectors[[1, 1]];
        assert!(
            (dot_product).abs() < 1e-10,
            "Eigenvectors should be orthogonal"
        );

        // Check that eigenvectors are normalized
        let norm1 = (eigenvectors[[0, 0]] * eigenvectors[[0, 0]]
            + eigenvectors[[1, 0]] * eigenvectors[[1, 0]])
        .sqrt();
        let norm2 = (eigenvectors[[0, 1]] * eigenvectors[[0, 1]]
            + eigenvectors[[1, 1]] * eigenvectors[[1, 1]])
        .sqrt();
        assert!(
            (norm1 - 1.0).abs() < 1e-10,
            "First eigenvector should be normalized"
        );
        assert!(
            (norm2 - 1.0).abs() < 1e-10,
            "Second eigenvector should be normalized"
        );
    }

    #[test]
    fn test_power_iteration() {
        // Matrix with known dominant eigenvalue and eigenvector
        let a = array![[3.0, 1.0], [1.0, 3.0]];

        let (eigenvalue, eigenvector) =
            power_iteration(&a.view(), 100, 1e-10).expect("Operation failed");

        // Dominant eigenvalue should be 4
        assert_relative_eq!(eigenvalue, 4.0, epsilon = 1e-8);

        // Eigenvector should be normalized
        let norm = (eigenvector[0] * eigenvector[0] + eigenvector[1] * eigenvector[1]).sqrt();
        assert_relative_eq!(norm, 1.0, epsilon = 1e-10);

        // Check that Av ≈ lambda * v
        let av = a.dot(&eigenvector);
        let lambda_v = &eigenvector * eigenvalue;

        let mut max_diff = 0.0;
        for i in 0..eigenvector.len() {
            let diff = (av[i] - lambda_v[i]).abs();
            if diff > max_diff {
                max_diff = diff;
            }
        }

        assert!(
            max_diff < 1e-5,
            "A*v should approximately equal lambda*v, max diff: {}",
            max_diff
        );
    }
}