lie-groups 0.2.0

Lie groups and Lie algebras for computational mathematics
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
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
//! Root Systems for Semisimple Lie Algebras
//!
//! Implements the theory of root systems, which classify semisimple Lie algebras
//! via Cartan-Killing classification. Root systems encode the structure of the
//! Lie bracket via the adjoint representation on the Cartan subalgebra.
//!
//! # Mathematical Background
//!
//! For a semisimple Lie algebra 𝔤, choose a Cartan subalgebra 𝔥 (maximal abelian).
//! The adjoint representation ad: 𝔤 → End(𝔤) diagonalizes on 𝔥, giving eigenvalues
//! called **roots**:
//!
//! ```text
//! [H, X_α] = α(H) X_α    for all H ∈ 𝔥
//! ```
//!
//! The set of roots Φ ⊂ 𝔥* forms a **root system**, satisfying:
//! 1. Φ is finite, spans 𝔥*, and 0 ∉ Φ
//! 2. If α ∈ Φ, then -α ∈ Φ and no other scalar multiples
//! 3. Φ is closed under Weyl reflections: `s_α(β) = β - 2⟨β,α⟩/⟨α,α⟩ · α`
//! 4. ⟨β,α⟩ ∈ ℤ for all α,β ∈ Φ (Cartan integers)
//!
//! # Cartan Classification
//!
//! | Type | Rank | Group | Dimension | Example |
//! |------|------|-------|-----------|---------|
//! | Aₙ   | n    | SU(n+1) | n(n+2)  | A₁ = SU(2) |
//! | Bₙ   | n    | SO(2n+1) | n(2n+1) | B₂ = SO(5) |
//! | Cₙ   | n    | Sp(2n)  | n(2n+1) | C₂ = Sp(4) |
//! | Dₙ   | n    | SO(2n)  | n(2n-1) | D₃ = SO(6) |
//! | E₆   | 6    | E₆      | 78      | Exceptional |
//! | E₇   | 7    | E₇      | 133     | Exceptional |
//! | E₈   | 8    | E₈      | 248     | Exceptional |
//! | F₄   | 4    | F₄      | 52      | Exceptional |
//! | G₂   | 2    | G₂      | 14      | Exceptional |
//!
//! # References
//!
//! - Hall, *Lie Groups, Lie Algebras, and Representations*, Chapter 7
//! - Humphreys, *Introduction to Lie Algebras and Representation Theory*, Ch. II
//! - Fulton & Harris, *Representation Theory*, Lecture 21

use ndarray::Array2;
use num_complex::Complex64;
use std::collections::HashSet;
use std::fmt::{self, Write};

/// A root in the dual space of a Cartan subalgebra.
///
/// Mathematically: α ∈ 𝔥* represented as a vector in ℝⁿ (rank n).
/// For SU(n+1) (type Aₙ), roots are differences of standard basis vectors: eᵢ - eⱼ.
///
/// # Example
///
/// ```
/// use lie_groups::Root;
///
/// // SU(3) root: e₁ - e₂
/// let alpha = Root::new(vec![1.0, -1.0, 0.0]);
/// assert_eq!(alpha.rank(), 3);
/// assert!((alpha.norm_squared() - 2.0).abs() < 1e-10);
/// ```
#[derive(Debug, Clone, PartialEq)]
pub struct Root {
    /// Coordinates in ℝⁿ (n = rank of Lie algebra)
    pub(crate) coords: Vec<f64>,
}

impl Root {
    /// Create a new root from coordinates.
    #[must_use]
    pub fn new(coords: Vec<f64>) -> Self {
        Self { coords }
    }

    /// Returns the coordinate vector of this root.
    #[must_use]
    pub fn coords(&self) -> &[f64] {
        &self.coords
    }

    /// Rank of the Lie algebra (dimension of Cartan subalgebra).
    #[must_use]
    pub fn rank(&self) -> usize {
        self.coords.len()
    }

    /// Inner product ⟨α, β⟩ (standard Euclidean).
    #[must_use]
    pub fn inner_product(&self, other: &Root) -> f64 {
        assert_eq!(self.rank(), other.rank());
        self.coords
            .iter()
            .zip(&other.coords)
            .map(|(a, b)| a * b)
            .sum()
    }

    /// Squared norm ⟨α, α⟩.
    #[must_use]
    pub fn norm_squared(&self) -> f64 {
        self.inner_product(self)
    }

    /// Weyl reflection `s_α(β)` = β - 2⟨β,α⟩/⟨α,α⟩ · α.
    #[must_use]
    pub fn reflect(&self, beta: &Root) -> Root {
        let factor = 2.0 * self.inner_product(beta) / self.norm_squared();
        Root::new(
            beta.coords
                .iter()
                .zip(&self.coords)
                .map(|(b, a)| b - factor * a)
                .collect(),
        )
    }

    /// Cartan integer ⟨β, α^∨⟩ = 2⟨β,α⟩/⟨α,α⟩.
    #[inline]
    #[must_use]
    pub fn cartan_integer(&self, beta: &Root) -> i32 {
        let value = 2.0 * self.inner_product(beta) / self.norm_squared();
        value.round() as i32
    }

    /// Check if this root is positive (first nonzero coordinate is positive).
    #[must_use]
    pub fn is_positive(&self) -> bool {
        for &c in &self.coords {
            if c.abs() > 1e-10 {
                return c > 0.0;
            }
        }
        false // Zero root (shouldn't happen)
    }

    /// Negate this root.
    #[must_use]
    pub fn negate(&self) -> Root {
        Root::new(self.coords.iter().map(|c| -c).collect())
    }
}

impl fmt::Display for Root {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "(")?;
        for (i, c) in self.coords.iter().enumerate() {
            if i > 0 {
                write!(f, ", ")?;
            }
            write!(f, "{:.2}", c)?;
        }
        write!(f, ")")
    }
}

/// A root system for a semisimple Lie algebra.
///
/// Encodes the structure of the Lie bracket through roots and Weyl reflections.
///
/// # Example
///
/// ```
/// use lie_groups::RootSystem;
///
/// // SU(3) = type A₂
/// let su3 = RootSystem::type_a(2);
/// assert_eq!(su3.rank(), 2);
/// assert_eq!(su3.num_roots(), 6); // 3² - 1 = 8, but we store 6 roots
/// assert_eq!(su3.num_positive_roots(), 3);
/// ```
#[derive(Debug, Clone)]
pub struct RootSystem {
    /// Rank of the Lie algebra (dimension of Cartan subalgebra)
    rank: usize,

    /// All roots (positive and negative)
    roots: Vec<Root>,

    /// Simple roots (basis for positive roots)
    simple_roots: Vec<Root>,

    /// Cartan matrix `A_ij` = ⟨`α_j`, `α_i`^∨⟩
    cartan_matrix: Vec<Vec<i32>>,
}

impl RootSystem {
    /// Create a type Aₙ root system (SU(n+1)).
    ///
    /// For SU(n+1), the rank is n, and roots are differences eᵢ - eⱼ for i ≠ j.
    /// Simple roots: αᵢ = eᵢ - eᵢ₊₁ for i = 1, ..., n.
    ///
    /// # Example
    ///
    /// ```
    /// use lie_groups::RootSystem;
    ///
    /// // SU(2) = A₁
    /// let su2 = RootSystem::type_a(1);
    /// assert_eq!(su2.rank(), 1);
    /// assert_eq!(su2.num_roots(), 2); // ±α
    ///
    /// // SU(3) = A₂
    /// let su3 = RootSystem::type_a(2);
    /// assert_eq!(su3.rank(), 2);
    /// assert_eq!(su3.simple_roots().len(), 2);
    /// ```
    #[must_use]
    pub fn type_a(n: usize) -> Self {
        assert!(n >= 1, "Type A_n requires n >= 1");

        let rank = n;

        // Simple roots: αᵢ = eᵢ - eᵢ₊₁ for i = 1, ..., n
        // Embedded in ℝⁿ⁺¹ with constraint Σxᵢ = 0
        let mut simple_roots = Vec::new();
        for i in 0..n {
            let mut coords = vec![0.0; n + 1];
            coords[i] = 1.0;
            coords[i + 1] = -1.0;
            simple_roots.push(Root::new(coords));
        }

        // Generate all positive roots
        let mut positive_roots = Vec::new();
        for i in 0..=n {
            for j in (i + 1)..=n {
                let mut coords = vec![0.0; n + 1];
                coords[i] = 1.0;
                coords[j] = -1.0;
                positive_roots.push(Root::new(coords));
            }
        }

        // All roots = positive + negative
        let mut roots = positive_roots.clone();
        for root in &positive_roots {
            roots.push(root.negate());
        }

        // Compute Cartan matrix: A_ij = ⟨α_j, α_i^∨⟩
        let cartan_matrix = simple_roots
            .iter()
            .map(|alpha_i| {
                simple_roots
                    .iter()
                    .map(|alpha_j| alpha_i.cartan_integer(alpha_j))
                    .collect()
            })
            .collect();

        Self {
            rank,
            roots,
            simple_roots,
            cartan_matrix,
        }
    }

    /// Rank of the Lie algebra.
    #[must_use]
    pub fn rank(&self) -> usize {
        self.rank
    }

    /// All roots (positive and negative).
    #[must_use]
    pub fn roots(&self) -> &[Root] {
        &self.roots
    }

    /// Simple roots (basis for root system).
    #[must_use]
    pub fn simple_roots(&self) -> &[Root] {
        &self.simple_roots
    }

    /// Cartan matrix `A_ij` = ⟨`α_j`, `α_i`^∨⟩.
    #[must_use]
    pub fn cartan_matrix(&self) -> &[Vec<i32>] {
        &self.cartan_matrix
    }

    /// Number of roots.
    #[must_use]
    pub fn num_roots(&self) -> usize {
        self.roots.len()
    }

    /// Number of positive roots.
    #[must_use]
    pub fn num_positive_roots(&self) -> usize {
        self.roots.iter().filter(|r| r.is_positive()).count()
    }

    /// Get all positive roots.
    #[must_use]
    pub fn positive_roots(&self) -> Vec<Root> {
        self.roots
            .iter()
            .filter(|r| r.is_positive())
            .cloned()
            .collect()
    }

    /// Get the highest root (longest root in the positive system).
    ///
    /// The highest root θ is the unique positive root with maximal height
    /// (sum of coefficients when expanded in simple roots). It's also the
    /// longest root in the root system for simply-laced types like Aₙ.
    ///
    /// For type `A_n` (SU(n+1)), the highest root is θ = α₁ + α₂ + ... + αₙ.
    ///
    /// # Example
    ///
    /// ```
    /// use lie_groups::RootSystem;
    ///
    /// let su3 = RootSystem::type_a(2);
    /// let theta = su3.highest_root();
    ///
    /// // For SU(3), θ = α₁ + α₂ = (1, 0, -1)
    /// ```
    #[must_use]
    pub fn highest_root(&self) -> Root {
        // For type A_n, highest root is α₁ + α₂ + ... + αₙ
        // In coordinates: e₁ - eₙ₊₁ = (1, 0, ..., 0, -1)

        let n = self.rank;

        // Sum of simple roots
        let mut coords = vec![0.0; n + 1];

        for simple_root in &self.simple_roots {
            for (i, &coord) in simple_root.coords.iter().enumerate() {
                coords[i] += coord;
            }
        }

        Root::new(coords)
    }

    /// Check if a root is in the system.
    #[must_use]
    pub fn contains_root(&self, root: &Root) -> bool {
        self.roots.iter().any(|r| {
            r.coords.len() == root.coords.len()
                && r.coords
                    .iter()
                    .zip(root.coords.iter())
                    .all(|(a, b)| (a - b).abs() < 1e-10)
        })
    }

    /// Weyl reflection `s_α` for a root α.
    #[must_use]
    pub fn weyl_reflection(&self, alpha: &Root, beta: &Root) -> Root {
        alpha.reflect(beta)
    }

    /// Generate the Weyl group orbit of a weight under simple reflections.
    ///
    /// The Weyl group is generated by reflections in simple roots.
    /// For type Aₙ, |W| = (n+1)!
    #[must_use]
    pub fn weyl_orbit(&self, weight: &Root) -> Vec<Root> {
        let mut orbit = vec![weight.clone()];
        let mut seen = HashSet::new();
        seen.insert(format!("{:?}", weight.coords));

        let mut queue = vec![weight.clone()];

        while let Some(current) = queue.pop() {
            for simple_root in &self.simple_roots {
                let reflected = simple_root.reflect(&current);
                let key = format!("{:?}", reflected.coords);

                if !seen.contains(&key) {
                    seen.insert(key);
                    orbit.push(reflected.clone());
                    queue.push(reflected);
                }
            }
        }

        orbit
    }

    /// Dimension of the Lie algebra: rank + `num_roots`.
    ///
    /// For type Aₙ: dim = n + n(n+1) = n(n+2)
    #[must_use]
    pub fn dimension(&self) -> usize {
        self.rank + self.num_roots()
    }

    /// Dominant weight chamber: λ such that ⟨λ, α⟩ ≥ 0 for all simple roots α.
    #[must_use]
    pub fn is_dominant_weight(&self, weight: &Root) -> bool {
        self.simple_roots
            .iter()
            .all(|alpha| weight.inner_product(alpha) >= -1e-10)
    }

    /// Express a root as a linear combination of simple roots.
    ///
    /// Returns coefficients [c₁, c₂, ..., cₙ] such that β = Σ cᵢ αᵢ.
    /// For roots in the system, coefficients are integers (positive for positive roots).
    ///
    /// Returns `None` if the root is not in this system, or if the expansion
    /// is not yet implemented for general root systems.
    ///
    /// # Supported Systems
    ///
    /// - **Type A** (SU(n+1)): Fully implemented. Roots `e_i - e_j` expand as
    ///   sums of consecutive simple roots.
    /// - **Other types**: Returns `None`. General expansion requires Cartan
    ///   matrix inversion, which is not yet implemented.
    pub fn simple_root_expansion(&self, root: &Root) -> Option<Vec<i32>> {
        if !self.contains_root(root) {
            return None;
        }

        // For type A_n (SU(n+1)): roots are e_i - e_j in ℝⁿ⁺¹
        // Simple roots are α_k = e_k - e_{k+1} for k = 0, ..., n-1
        // A root e_i - e_j (i < j) = α_i + α_{i+1} + ... + α_{j-1}
        //
        // Detect type A by checking if root has exactly one +1 and one -1 coordinate
        let coords = &root.coords;
        let mut pos_idx: Option<usize> = None;
        let mut neg_idx: Option<usize> = None;

        for (idx, &val) in coords.iter().enumerate() {
            if (val - 1.0).abs() < 1e-10 {
                if pos_idx.is_some() {
                    // Not type A format - fall through to general case
                    return None;
                }
                pos_idx = Some(idx);
            } else if (val + 1.0).abs() < 1e-10 {
                if neg_idx.is_some() {
                    return None;
                }
                neg_idx = Some(idx);
            } else if val.abs() > 1e-10 {
                // Non-zero entry that's not ±1 - not type A
                return None;
            }
        }

        let (Some(i), Some(j)) = (pos_idx, neg_idx) else {
            return None;
        };

        // Build coefficients: for e_i - e_j, coefficient of α_k is:
        // +1 if min(i,j) <= k < max(i,j) and i < j (positive root)
        // -1 if min(i,j) <= k < max(i,j) and i > j (negative root)
        let mut coeffs = vec![0i32; self.rank];
        let (min_idx, max_idx) = if i < j { (i, j) } else { (j, i) };
        let sign = if i < j { 1 } else { -1 };

        for k in min_idx..max_idx {
            if k < self.rank {
                coeffs[k] = sign;
            }
        }

        Some(coeffs)
    }
}

/// Weight lattice for a root system.
///
/// Weights are elements λ ∈ 𝔥* such that ⟨λ, α^∨⟩ ∈ ℤ for all roots α.
/// The weight lattice contains the root lattice as a sublattice.
///
/// # Fundamental Weights
///
/// For simple roots α₁, ..., αₙ, the fundamental weights ω₁, ..., ωₙ satisfy:
/// ```text
/// ⟨ωᵢ, αⱼ^∨⟩ = δᵢⱼ
/// ```
///
/// Every dominant weight can be written λ = Σ mᵢ ωᵢ with mᵢ ∈ ℤ≥₀.
pub struct WeightLattice {
    root_system: RootSystem,
    fundamental_weights: Vec<Root>,
}

impl WeightLattice {
    /// Create weight lattice from root system.
    #[must_use]
    pub fn from_root_system(root_system: RootSystem) -> Self {
        let fundamental_weights = Self::compute_fundamental_weights(&root_system);
        Self {
            root_system,
            fundamental_weights,
        }
    }

    /// Compute fundamental weights ωᵢ such that ⟨ωᵢ, αⱼ^∨⟩ = δᵢⱼ.
    fn compute_fundamental_weights(rs: &RootSystem) -> Vec<Root> {
        // For type A_n (SU(n+1)):
        // In GL(n+1) basis: ωᵢ = (1, 1, ..., 1, 0, ..., 0) with i ones
        // In SU(n+1) traceless basis: project to hyperplane Σxᵢ = 0
        //
        // Projection: x_traceless = x - (Σxᵢ)/(n+1) · (1,1,...,1)

        let n = rs.rank();
        let dim = n + 1; // Dimension of fundamental representation
        let mut weights = Vec::new();

        for i in 0..n {
            // Start with GL(n+1) fundamental weight
            let mut coords = vec![0.0; dim];
            for j in 0..=i {
                coords[j] = 1.0;
            }

            // Project to traceless subspace: subtract mean
            let sum: f64 = coords.iter().sum();
            let mean = sum / (dim as f64);
            for coord in &mut coords {
                *coord -= mean;
            }

            weights.push(Root::new(coords));
        }

        weights
    }

    /// Fundamental weights.
    #[must_use]
    pub fn fundamental_weights(&self) -> &[Root] {
        &self.fundamental_weights
    }

    /// Convert Dynkin labels (m₁, ..., mₙ) to weight λ = Σ mᵢ ωᵢ.
    #[must_use]
    pub fn dynkin_to_weight(&self, dynkin_labels: &[usize]) -> Root {
        assert_eq!(dynkin_labels.len(), self.root_system.rank());

        let mut weight_coords = vec![0.0; self.root_system.rank() + 1];
        for (i, &m) in dynkin_labels.iter().enumerate() {
            for (j, &w) in self.fundamental_weights[i].coords.iter().enumerate() {
                weight_coords[j] += (m as f64) * w;
            }
        }

        Root::new(weight_coords)
    }

    /// Get the root system.
    #[must_use]
    pub fn root_system(&self) -> &RootSystem {
        &self.root_system
    }

    /// Compute ρ (half-sum of positive roots) = sum of fundamental weights.
    ///
    /// For type Aₙ: ρ = ω₁ + ω₂ + ... + ωₙ
    #[must_use]
    pub fn rho(&self) -> Root {
        let mut rho_coords = vec![0.0; self.root_system.rank() + 1];
        for omega in &self.fundamental_weights {
            for (i, &coord) in omega.coords.iter().enumerate() {
                rho_coords[i] += coord;
            }
        }
        Root::new(rho_coords)
    }

    /// Kostant's partition function P(γ): number of ways to write γ as a sum of positive roots.
    ///
    /// Counts solutions to: γ = Σ_{α ∈ Φ⁺} `n_α` · α where `n_α` ≥ 0 are non-negative integers.
    ///
    /// This is computed via dynamic programming with ORDERED roots to avoid overcounting.
    ///
    /// # Returns
    /// Number of distinct ways (counting multiplicities) to express γ as a sum of positive roots.
    /// Returns 0 if γ cannot be expressed as such a sum.
    ///
    /// # Algorithm
    /// Uses DP with memoization and ROOT ORDERING to count unordered partitions.
    /// For each root `α_i`, we only consider using roots `α_j` where j ≥ i.
    /// This ensures we count combinations, not permutations.
    ///
    /// # Performance
    /// O(|Φ⁺| × V) where V is the size of the reachable region.
    /// For typical representations, this is manageable.
    fn kostant_partition_function(&self, gamma: &Root) -> usize {
        use std::collections::HashMap;

        // Base case: zero vector has exactly one representation (empty sum)
        if gamma.coords.iter().all(|&x| x.abs() < 1e-10) {
            return 1;
        }

        // Use memoization to avoid recomputation
        // Key: (gamma, starting_index) to track which roots we can still use
        let mut memo: HashMap<(String, usize), usize> = HashMap::new();
        let positive_roots: Vec<Root> = self.root_system.positive_roots();

        Self::partition_helper_ordered(gamma, &positive_roots, 0, &mut memo)
    }

    /// Recursive helper for partition function with ROOT ORDERING to avoid overcounting.
    ///
    /// Only considers roots starting from index `start_idx` onwards.
    /// This ensures we generate combinations (not permutations) of roots.
    fn partition_helper_ordered(
        gamma: &Root,
        positive_roots: &[Root],
        start_idx: usize,
        memo: &mut std::collections::HashMap<(String, usize), usize>,
    ) -> usize {
        // Base case: zero
        if gamma.coords.iter().all(|&x| x.abs() < 1e-10) {
            return 1;
        }

        // Base case: no more roots to try
        if start_idx >= positive_roots.len() {
            return 0;
        }

        // Check memo
        let key = (WeightLattice::weight_key(&gamma.coords), start_idx);
        if let Some(&result) = memo.get(&key) {
            return result;
        }

        // Early termination: if γ has large negative coordinates, it's not expressible
        let mut has_large_negative = false;
        for &coord in &gamma.coords {
            if coord < -100.0 {
                has_large_negative = true;
                break;
            }
        }

        if has_large_negative {
            memo.insert(key, 0);
            return 0;
        }

        // Recursive case: for current root α at start_idx, we have two choices:
        // 1. Don't use this root at all: move to next root
        // 2. Use this root k times (k ≥ 1): subtract k·α and stay at same index
        let mut count = 0;
        let alpha = &positive_roots[start_idx];

        // Choice 1: Skip this root entirely
        if memo.len() < 100_000 {
            count += Self::partition_helper_ordered(gamma, positive_roots, start_idx + 1, memo);
        }

        // Choice 2: Use this root at least once
        let mut gamma_minus_k_alpha = gamma.clone();
        for (i, &a) in alpha.coords.iter().enumerate() {
            gamma_minus_k_alpha.coords[i] -= a;
        }

        // Can we use this root? Check if γ - α is "valid" (not too negative)
        let can_use = gamma_minus_k_alpha.coords.iter().all(|&x| x > -100.0);

        if can_use && memo.len() < 100_000 {
            // Recurse with γ - α, staying at same root index (can use it multiple times)
            count += Self::partition_helper_ordered(
                &gamma_minus_k_alpha,
                positive_roots,
                start_idx,
                memo,
            );
        }

        memo.insert(key, count);
        count
    }

    /// Generate Weyl group elements for type `A_n` (symmetric group S_{n+1}).
    ///
    /// The Weyl group of type `A_n` is isomorphic to S_{n+1}, the symmetric group
    /// of permutations of {1, 2, ..., n+1}.
    ///
    /// Each permutation σ acts on weights λ = (λ₁, ..., λ_{n+1}) by permuting coordinates.
    ///
    /// # Returns
    /// Vector of (permutation, sign) pairs where:
    /// - permutation[i] = j means coordinate i maps to coordinate j
    /// - sign = +1 for even permutations, -1 for odd
    ///
    /// # Performance
    /// Generates all (n+1)! permutations. For SU(3) this is 6, for SU(4) this is 24, etc.
    fn weyl_group_type_a(&self) -> Vec<(Vec<usize>, i32)> {
        let n_plus_1 = self.root_system.rank() + 1;
        let mut perms = Vec::new();

        // Generate all permutations via Heap's algorithm
        let mut current: Vec<usize> = (0..n_plus_1).collect();
        Self::generate_permutations(&mut current, n_plus_1, &mut perms);

        perms
    }

    /// Generate all permutations using Heap's algorithm.
    fn generate_permutations(arr: &mut [usize], size: usize, result: &mut Vec<(Vec<usize>, i32)>) {
        if size == 1 {
            let perm = arr.to_vec();
            let sign = Self::permutation_sign(&perm);
            result.push((perm, sign));
            return;
        }

        for i in 0..size {
            Self::generate_permutations(arr, size - 1, result);

            if size % 2 == 0 {
                arr.swap(i, size - 1);
            } else {
                arr.swap(0, size - 1);
            }
        }
    }

    /// Compute the sign of a permutation (+1 for even, -1 for odd).
    fn permutation_sign(perm: &[usize]) -> i32 {
        let n = perm.len();
        let mut sign = 1i32;

        for i in 0..n {
            for j in (i + 1)..n {
                if perm[i] > perm[j] {
                    sign *= -1;
                }
            }
        }

        sign
    }

    /// Apply Weyl group element (permutation) to a weight.
    fn weyl_action(&self, weight: &Root, permutation: &[usize]) -> Root {
        let mut new_coords = vec![0.0; weight.coords.len()];
        for (i, &pi) in permutation.iter().enumerate() {
            new_coords[i] = weight.coords[pi];
        }
        Root::new(new_coords)
    }

    /// Compute weight multiplicity using Kostant's formula.
    ///
    /// Kostant's formula (exact, not recursive like Freudenthal):
    /// ```text
    /// m_Λ(λ) = Σ_{w ∈ W} ε(w) · P(w·(Λ+ρ) - (λ+ρ))
    /// ```
    ///
    /// where:
    /// - Λ = highest weight of the representation
    /// - λ = weight whose multiplicity we compute
    /// - W = Weyl group
    /// - ε(w) = sign of w (+1 for even, -1 for odd)
    /// - P(γ) = partition function counting ways to write γ as sum of positive roots
    ///
    /// # Mathematical Background
    ///
    /// This is the most general multiplicity formula, working for all weights in all
    /// representations. Unlike Freudenthal (which requires dominance), Kostant works
    /// directly.
    ///
    /// # Performance
    /// O(|W| × `P_cost`) where |W| = (n+1)! for type `A_n` and `P_cost` is partition function cost.
    /// For SU(3): 6 Weyl group elements.
    /// For SU(4): 24 Weyl group elements.
    ///
    /// # References
    /// - Humphreys, "Introduction to Lie Algebras and Representation Theory", §24.3
    /// - Kostant (1959), "A Formula for the Multiplicity of a Weight"
    #[must_use]
    pub fn kostant_multiplicity(&self, highest_weight: &Root, weight: &Root) -> usize {
        let rho = self.rho();

        // Compute Λ + ρ
        let mut lambda_plus_rho = highest_weight.clone();
        for (i, &r) in rho.coords.iter().enumerate() {
            lambda_plus_rho.coords[i] += r;
        }

        // Compute λ + ρ
        let mut mu_plus_rho = weight.clone();
        for (i, &r) in rho.coords.iter().enumerate() {
            mu_plus_rho.coords[i] += r;
        }

        // Sum over Weyl group
        let weyl_group = self.weyl_group_type_a();
        let mut multiplicity = 0i32; // Use signed arithmetic for alternating sum

        for (perm, sign) in weyl_group {
            // Compute w·(Λ+ρ)
            let w_lambda_plus_rho = self.weyl_action(&lambda_plus_rho, &perm);

            // Compute γ = w·(Λ+ρ) - (λ+ρ)
            let mut gamma = w_lambda_plus_rho.clone();
            for (i, &mu) in mu_plus_rho.coords.iter().enumerate() {
                gamma.coords[i] -= mu;
            }

            // Compute P(γ)
            let p_gamma = self.kostant_partition_function(&gamma);

            // Add signed contribution
            multiplicity += sign * (p_gamma as i32);
        }

        // Result must be non-negative
        assert!(
            multiplicity >= 0,
            "Kostant multiplicity must be non-negative, got {}",
            multiplicity
        );
        multiplicity as usize
    }

    /// Compute dimension of irreducible representation using Weyl dimension formula.
    ///
    /// For type Aₙ with highest weight λ:
    /// ```text
    /// dim(λ) = ∏_{α>0} ⟨λ + ρ, α⟩ / ⟨ρ, α⟩
    /// ```
    ///
    /// This is the EXACT dimension - no approximation.
    #[must_use]
    pub fn weyl_dimension(&self, highest_weight: &Root) -> usize {
        let rho = self.rho();

        // λ + ρ
        let mut lambda_plus_rho = highest_weight.clone();
        for (i, &r) in rho.coords.iter().enumerate() {
            lambda_plus_rho.coords[i] += r;
        }

        let mut numerator = 1.0;
        let mut denominator = 1.0;

        for alpha in self.root_system.positive_roots() {
            let num = lambda_plus_rho.inner_product(&alpha);
            let denom = rho.inner_product(&alpha);

            numerator *= num;
            denominator *= denom;
        }

        (numerator / denominator).round() as usize
    }

    /// Helper function to create a stable `HashMap` key from weight coordinates.
    ///
    /// Rounds each coordinate to 10 decimal places to avoid floating-point precision issues
    /// when using coordinates as `HashMap` keys.
    fn weight_key(coords: &[f64]) -> String {
        let rounded: Vec<String> = coords.iter().map(|&x| format!("{:.10}", x)).collect();
        format!("[{}]", rounded.join(", "))
    }

    /// Generate all weights in an irreducible representation.
    ///
    /// Uses the CORRECT, GENERAL algorithm:
    /// 1. Generate candidate weights by exploring from highest weight with ALL positive roots
    /// 2. Add Weyl orbit of highest weight to ensure completeness
    /// 3. Use Kostant's formula to compute accurate multiplicities
    /// 4. Return only weights with multiplicity > 0
    ///
    /// This works for ALL representations, including adjoint where negative roots appear.
    #[must_use]
    pub fn weights_of_irrep(&self, highest_weight: &Root) -> Vec<(Root, usize)> {
        use std::collections::{HashMap, VecDeque};

        let mut candidates: HashMap<String, Root> = HashMap::new();
        let mut queue: VecDeque<Root> = VecDeque::new();

        // Start with highest weight
        let hw_key = Self::weight_key(&highest_weight.coords);
        candidates.insert(hw_key, highest_weight.clone());
        queue.push_back(highest_weight.clone());

        // BFS: Subtract ALL positive roots (not just simple roots!)
        // This ensures we reach all weights, including negative roots in adjoint rep
        while let Some(weight) = queue.pop_front() {
            for pos_root in self.root_system.positive_roots() {
                let mut new_weight = weight.clone();
                for (i, &a) in pos_root.coords.iter().enumerate() {
                    new_weight.coords[i] -= a;
                }

                let new_key = Self::weight_key(&new_weight.coords);

                // Skip if already seen
                if candidates.contains_key(&new_key) {
                    continue;
                }

                // Check if in reasonable bounds (heuristic to avoid infinite exploration)
                let norm = new_weight.norm_squared();
                let hw_norm = highest_weight.norm_squared();
                if norm > 3.0 * hw_norm + 10.0 {
                    continue;
                }

                candidates.insert(new_key, new_weight.clone());
                queue.push_back(new_weight);

                // Safety limit
                if candidates.len() > 1000 {
                    break;
                }
            }

            if candidates.len() > 1000 {
                break;
            }
        }

        // CRITICAL: Also add the full Weyl orbit of the highest weight
        // This ensures we don't miss any extremal weights
        let weyl_orbit = self.root_system.weyl_orbit(highest_weight);
        for w in weyl_orbit {
            let key = Self::weight_key(&w.coords);
            candidates.insert(key, w);
        }

        // Compute accurate multiplicities using Kostant's formula
        let candidate_list: Vec<Root> = candidates.into_values().collect();
        let mut result = Vec::new();

        for weight in candidate_list.iter() {
            let mult = self.kostant_multiplicity(highest_weight, weight);
            if mult > 0 {
                result.push((weight.clone(), mult));
            }
        }

        result
    }

    /// Generate a string representation of a weight diagram for rank 2.
    ///
    /// For SU(3) representations, this creates a 2D triangular lattice
    /// showing all weights with their multiplicities.
    ///
    /// # Example
    ///
    /// ```
    /// use lie_groups::{RootSystem, WeightLattice};
    ///
    /// let rs = RootSystem::type_a(2);
    /// let wl = WeightLattice::from_root_system(rs);
    /// let highest = wl.dynkin_to_weight(&[1, 1]); // Adjoint rep
    ///
    /// let diagram = wl.weight_diagram_string(&highest);
    /// // Should show 8 weights (8-dimensional rep)
    /// ```
    #[must_use]
    pub fn weight_diagram_string(&self, highest_weight: &Root) -> String {
        if self.root_system.rank() != 2 {
            return "Weight diagrams only implemented for rank 2".to_string();
        }

        let weights = self.weights_of_irrep(highest_weight);

        let mut output = String::new();
        writeln!(
            &mut output,
            "Weight diagram for highest weight {:?}",
            highest_weight.coords
        )
        .expect("String formatting should not fail");
        writeln!(&mut output, "Dimension: {}", weights.len())
            .expect("String formatting should not fail");
        output.push_str("Weights (multiplicity):\n");

        for (weight, mult) in &weights {
            writeln!(&mut output, "  {}{})", weight, mult)
                .expect("String formatting should not fail");
        }

        output
    }
}

/// Cartan subalgebra of a semisimple Lie algebra.
///
/// The Cartan subalgebra 𝔥 is the maximal abelian subalgebra consisting of
/// simultaneously diagonalizable elements. For SU(n+1) (type Aₙ), it consists
/// of traceless diagonal matrices.
///
/// # Mathematical Background
///
/// - Dimension: rank of the Lie algebra
/// - Basis: {H₁, ..., Hₙ} where [Hᵢ, Hⱼ] = 0 (all commute)
/// - For SU(n+1): Hᵢ = Eᵢᵢ - Eᵢ₊₁,ᵢ₊₁ (diagonal matrices)
/// - Roots are functionals α: 𝔥 → ℝ
///
/// # Example
///
/// ```
/// use lie_groups::root_systems::CartanSubalgebra;
///
/// // SU(3) Cartan subalgebra (2-dimensional)
/// let cartan = CartanSubalgebra::type_a(2);
/// assert_eq!(cartan.dimension(), 2);
///
/// let basis = cartan.basis_matrices();
/// assert_eq!(basis.len(), 2);
/// ```
pub struct CartanSubalgebra {
    /// Dimension of the Cartan subalgebra (rank of Lie algebra)
    dimension: usize,

    /// Basis matrices as complex arrays
    /// For SU(n+1), these are n diagonal traceless matrices
    basis_matrices: Vec<Array2<Complex64>>,

    /// Matrix dimension (N for SU(N))
    matrix_size: usize,
}

impl CartanSubalgebra {
    /// Create Cartan subalgebra for type Aₙ (SU(n+1)).
    ///
    /// The Cartan subalgebra consists of traceless diagonal matrices.
    /// Basis: Hᵢ = Eᵢᵢ - Eᵢ₊₁,ᵢ₊₁ for i = 1, ..., n
    ///
    /// # Example
    ///
    /// ```
    /// use lie_groups::root_systems::CartanSubalgebra;
    ///
    /// // SU(2) Cartan subalgebra: 1-dimensional
    /// let h = CartanSubalgebra::type_a(1);
    /// assert_eq!(h.dimension(), 1);
    ///
    /// // First basis element: diag(1, -1)
    /// let h1 = &h.basis_matrices()[0];
    /// assert!((h1[(0,0)].re - 1.0).abs() < 1e-10);
    /// assert!((h1[(1,1)].re + 1.0).abs() < 1e-10);
    /// ```
    #[must_use]
    pub fn type_a(n: usize) -> Self {
        assert!(n >= 1, "Type A_n requires n >= 1");

        let matrix_size = n + 1;
        let dimension = n;

        let mut basis_matrices = Vec::new();

        // Generate Hᵢ = Eᵢᵢ - Eᵢ₊₁,ᵢ₊₁ for i = 0, ..., n-1
        for i in 0..n {
            let mut h = Array2::<Complex64>::zeros((matrix_size, matrix_size));
            h[(i, i)] = Complex64::new(1.0, 0.0);
            h[(i + 1, i + 1)] = Complex64::new(-1.0, 0.0);
            basis_matrices.push(h);
        }

        Self {
            dimension,
            basis_matrices,
            matrix_size,
        }
    }

    /// Dimension of the Cartan subalgebra (rank).
    #[must_use]
    pub fn dimension(&self) -> usize {
        self.dimension
    }

    /// Basis matrices for the Cartan subalgebra.
    #[must_use]
    pub fn basis_matrices(&self) -> &[Array2<Complex64>] {
        &self.basis_matrices
    }

    /// Matrix size (N for SU(N)).
    #[must_use]
    pub fn matrix_size(&self) -> usize {
        self.matrix_size
    }

    /// Evaluate a root functional on a Cartan element.
    ///
    /// For a root α and Cartan element H = Σ cᵢ Hᵢ, computes α(H) = Σ cᵢ α(Hᵢ).
    ///
    /// # Arguments
    ///
    /// * `root` - Root as a vector of coordinates
    /// * `coefficients` - Coefficients [c₁, ..., cₙ] in basis {H₁, ..., Hₙ}
    ///
    /// # Returns
    ///
    /// The value α(H) ∈ ℂ
    #[must_use]
    pub fn evaluate_root(&self, root: &Root, coefficients: &[f64]) -> Complex64 {
        assert_eq!(
            coefficients.len(),
            self.dimension,
            "Coefficient vector must match Cartan dimension"
        );
        assert_eq!(
            root.rank(),
            self.matrix_size,
            "Root dimension must match matrix size"
        );

        // For type A_n, α(H) is computed via the standard pairing
        // If H = diag(h₁, ..., hₙ₊₁) with Σhᵢ = 0, and α = (α₁, ..., αₙ₊₁)
        // then α(H) = Σ αᵢ hᵢ

        // Reconstruct diagonal H from basis coefficients
        let mut h_diagonal = vec![Complex64::new(0.0, 0.0); self.matrix_size];
        for (i, &c) in coefficients.iter().enumerate() {
            h_diagonal[i] += Complex64::new(c, 0.0);
            h_diagonal[i + 1] -= Complex64::new(c, 0.0);
        }

        // Compute α(H) = Σ αᵢ hᵢ
        root.coords
            .iter()
            .zip(&h_diagonal)
            .map(|(alpha_i, h_i)| h_i * alpha_i)
            .sum()
    }

    /// Project a matrix onto the Cartan subalgebra.
    ///
    /// For a matrix M, finds coefficients [c₁, ..., cₙ] such that
    /// Σ cᵢ Hᵢ best approximates M in the Frobenius norm.
    ///
    /// Since the basis {H₁, ..., Hₙ} is not necessarily orthogonal,
    /// we solve the linear system: G c = g where Gᵢⱼ = ⟨Hᵢ, Hⱼ⟩.
    ///
    /// # Arguments
    ///
    /// * `matrix` - Matrix to project
    ///
    /// # Returns
    ///
    /// `Some(coefficients)` in the Cartan basis for rank ≤ 2,
    /// `None` for rank > 2 (Gaussian elimination not yet implemented).
    ///
    /// # Limitations
    ///
    /// Currently only supports rank 1 (SU(2)) and rank 2 (SU(3)) systems.
    /// Higher rank systems require general Gaussian elimination.
    #[must_use]
    pub fn project_matrix(&self, matrix: &Array2<Complex64>) -> Option<Vec<f64>> {
        assert_eq!(
            matrix.shape(),
            &[self.matrix_size, self.matrix_size],
            "Matrix must be {}×{}",
            self.matrix_size,
            self.matrix_size
        );

        // Compute Gram matrix G and right-hand side g
        let n = self.dimension;
        let mut gram = vec![vec![0.0; n]; n];
        let mut rhs = vec![0.0; n];

        for i in 0..n {
            for j in 0..n {
                // Gᵢⱼ = ⟨Hᵢ, Hⱼ⟩ = Tr(Hᵢ† Hⱼ)
                let inner: Complex64 = self.basis_matrices[i]
                    .iter()
                    .zip(self.basis_matrices[j].iter())
                    .map(|(h_i, h_j)| h_i.conj() * h_j)
                    .sum();
                gram[i][j] = inner.re;
            }

            // gᵢ = ⟨M, Hᵢ⟩ = Tr(M† Hᵢ)
            let inner: Complex64 = matrix
                .iter()
                .zip(self.basis_matrices[i].iter())
                .map(|(m_ij, h_ij)| m_ij.conj() * h_ij)
                .sum();
            rhs[i] = inner.re;
        }

        // Solve G c = g using direct formulas (small n only)
        let mut coefficients = vec![0.0; n];

        match n {
            1 => {
                coefficients[0] = rhs[0] / gram[0][0];
                Some(coefficients)
            }
            2 => {
                // Solve 2×2 system directly
                let det = gram[0][0] * gram[1][1] - gram[0][1] * gram[1][0];
                coefficients[0] = (rhs[0] * gram[1][1] - rhs[1] * gram[0][1]) / det;
                coefficients[1] = (gram[0][0] * rhs[1] - gram[1][0] * rhs[0]) / det;
                Some(coefficients)
            }
            _ => {
                // Rank > 2: Gaussian elimination not yet implemented
                None
            }
        }
    }

    /// Construct a Cartan element from coefficients.
    ///
    /// Given coefficients [c₁, ..., cₙ], returns H = Σ cᵢ Hᵢ.
    #[must_use]
    pub fn from_coefficients(&self, coefficients: &[f64]) -> Array2<Complex64> {
        assert_eq!(
            coefficients.len(),
            self.dimension,
            "Must provide {} coefficients",
            self.dimension
        );

        let mut result = Array2::<Complex64>::zeros((self.matrix_size, self.matrix_size));

        for (&c_i, h_i) in coefficients.iter().zip(&self.basis_matrices) {
            result = result + h_i * c_i;
        }

        result
    }

    /// Check if a matrix is in the Cartan subalgebra.
    ///
    /// Returns true if the matrix is diagonal (or nearly diagonal within tolerance).
    #[must_use]
    pub fn contains(&self, matrix: &Array2<Complex64>, tolerance: f64) -> bool {
        assert_eq!(
            matrix.shape(),
            &[self.matrix_size, self.matrix_size],
            "Matrix must be {}×{}",
            self.matrix_size,
            self.matrix_size
        );

        // Check if matrix is diagonal
        for i in 0..self.matrix_size {
            for j in 0..self.matrix_size {
                if i != j && matrix[(i, j)].norm() > tolerance {
                    return false;
                }
            }
        }

        // Check if diagonal entries sum to zero (traceless)
        let trace: Complex64 = (0..self.matrix_size).map(|i| matrix[(i, i)]).sum();

        trace.norm() < tolerance
    }

    /// Killing form restricted to Cartan subalgebra.
    ///
    /// For H, H' ∈ 𝔥, the Killing form is κ(H, H') = Tr(`ad_H` ∘ `ad_H`').
    /// For type `A_n`, this simplifies significantly.
    ///
    /// # Arguments
    ///
    /// * `coeffs1` - First Cartan element as coefficients
    /// * `coeffs2` - Second Cartan element as coefficients
    ///
    /// # Returns
    ///
    /// The value κ(H₁, H₂)
    #[must_use]
    pub fn killing_form(&self, coeffs1: &[f64], coeffs2: &[f64]) -> f64 {
        assert_eq!(coeffs1.len(), self.dimension);
        assert_eq!(coeffs2.len(), self.dimension);

        // For SU(n+1), the Killing form on the Cartan is
        // κ(H, H') = 2(n+1) Tr(HH')
        //
        // For our basis Hᵢ = Eᵢᵢ - Eᵢ₊₁,ᵢ₊₁:
        // Tr(Hᵢ Hⱼ) = 2δᵢⱼ

        let n_plus_1 = self.matrix_size as f64;

        coeffs1
            .iter()
            .zip(coeffs2)
            .map(|(c1, c2)| 2.0 * n_plus_1 * 2.0 * c1 * c2)
            .sum()
    }

    /// Dual basis in 𝔥* (root space).
    ///
    /// Returns roots {α₁, ..., αₙ} such that αᵢ(Hⱼ) = δᵢⱼ.
    /// For type `A_n`, these correspond to the simple roots.
    #[must_use]
    pub fn dual_basis(&self) -> Vec<Root> {
        let mut dual_roots = Vec::new();

        for i in 0..self.dimension {
            // For Hᵢ = Eᵢᵢ - Eᵢ₊₁,ᵢ₊₁, the dual is αᵢ = eᵢ - eᵢ₊₁
            let mut coords = vec![0.0; self.matrix_size];
            coords[i] = 1.0;
            coords[i + 1] = -1.0;
            dual_roots.push(Root::new(coords));
        }

        dual_roots
    }
}

/// Weyl chamber for a root system.
///
/// A Weyl chamber is a connected component of the complement of the reflecting
/// hyperplanes in the dual Cartan space 𝔥*. The **fundamental Weyl chamber**
/// (or **dominant chamber**) is defined by the simple roots:
///
/// ```text
/// C = {λ ∈ 𝔥* : ⟨λ, αᵢ⟩ ≥ 0 for all simple roots αᵢ}
/// ```
///
/// # Mathematical Background
///
/// - The Weyl group W acts on 𝔥* by reflections through root hyperplanes
/// - W partitions 𝔥* into finitely many Weyl chambers
/// - All chambers are isometric under W
/// - The fundamental chamber parametrizes dominant weights (highest weights
///   of irreducible representations)
///
/// # Example
///
/// ```
/// use lie_groups::root_systems::{RootSystem, WeylChamber, Root};
///
/// let root_system = RootSystem::type_a(2); // SU(3)
/// let chamber = WeylChamber::fundamental(&root_system);
///
/// // Fundamental weight ω₁ = (2/3, -1/3, -1/3) is dominant (traceless for SU(3))
/// let omega1 = Root::new(vec![2.0/3.0, -1.0/3.0, -1.0/3.0]);
/// assert!(chamber.contains(&omega1, false)); // Non-strict since on boundary
///
/// // Negative weight is not dominant
/// let neg = Root::new(vec![-1.0, 0.0, 1.0]);
/// assert!(!chamber.contains(&neg, false));
/// ```
///
/// # References
///
/// - Humphreys, *Introduction to Lie Algebras*, §10.2
/// - Hall, *Lie Groups*, §8.4
#[derive(Debug, Clone)]
pub struct WeylChamber {
    /// The root system defining the chamber
    root_system: RootSystem,

    /// Simple roots defining the chamber walls
    simple_roots: Vec<Root>,
}

impl WeylChamber {
    /// Construct the fundamental Weyl chamber for a root system.
    ///
    /// The fundamental chamber is bounded by hyperplanes perpendicular to
    /// the simple roots:
    ///
    /// ```text
    /// C = {λ : ⟨λ, αᵢ⟩ ≥ 0 for all simple αᵢ}
    /// ```
    #[must_use]
    pub fn fundamental(root_system: &RootSystem) -> Self {
        Self {
            root_system: root_system.clone(),
            simple_roots: root_system.simple_roots.clone(),
        }
    }

    /// Check if a weight is in the Weyl chamber.
    ///
    /// # Arguments
    ///
    /// * `weight` - Weight λ ∈ 𝔥* to test
    /// * `strict` - If true, use strict inequality (⟨λ, αᵢ⟩ > 0); else non-strict (≥ 0)
    ///
    /// # Returns
    ///
    /// True if λ is in the chamber (dominant weight)
    #[must_use]
    pub fn contains(&self, weight: &Root, strict: bool) -> bool {
        // For type A_n, roots have n+1 coordinates
        assert_eq!(weight.rank(), self.root_system.rank + 1);

        for alpha in &self.simple_roots {
            let pairing = weight.inner_product(alpha);

            if strict {
                if pairing <= 1e-10 {
                    return false;
                }
            } else if pairing < -1e-10 {
                return false;
            }
        }

        true
    }

    /// Project a weight onto the closure of the fundamental Weyl chamber.
    ///
    /// Uses the Weyl group to map an arbitrary weight to its unique dominant
    /// representative (closest point in the chamber).
    ///
    /// # Algorithm
    ///
    /// Iteratively reflect through violated hyperplanes until all simple root
    /// pairings are non-negative.
    #[must_use]
    pub fn project(&self, weight: &Root) -> Root {
        let mut current = weight.clone();

        // Maximum iterations to prevent infinite loops
        let max_iterations = 100;
        let mut iterations = 0;

        while iterations < max_iterations {
            let mut found_violation = false;

            for alpha in &self.simple_roots {
                let pairing = current.inner_product(alpha);

                if pairing < -1e-10 {
                    // Reflect through α to increase ⟨current, α⟩
                    current = alpha.reflect(&current);
                    found_violation = true;
                }
            }

            if !found_violation {
                break;
            }

            iterations += 1;
        }

        if iterations >= max_iterations {
            eprintln!(
                "Warning: WeylChamber::project did not converge in {} iterations",
                max_iterations
            );
        }

        current
    }

    /// Get the simple roots defining the chamber walls.
    #[must_use]
    pub fn simple_roots(&self) -> &[Root] {
        &self.simple_roots
    }
}

/// Fundamental alcove for an affine root system.
///
/// An **alcove** is a bounded region in the Cartan space defined by affine
/// hyperplanes (root hyperplanes shifted by integer levels). The fundamental
/// alcove is:
///
/// ```text
/// A = {λ ∈ 𝔥* : ⟨λ, αᵢ⟩ ≥ 0 for all simple αᵢ, and ⟨λ, θ⟩ ≤ 1}
/// ```
///
/// where θ is the **highest root** (longest root in the positive system).
///
/// # Mathematical Background
///
/// - Alcoves tile 𝔥* under the affine Weyl group Wₐff
/// - The fundamental alcove parametrizes integrable highest-weight modules
///   at level k (conformal field theory, loop groups)
/// - Vertices of the alcove are fundamental weights
///
/// # Example
///
/// ```
/// use lie_groups::root_systems::{RootSystem, Alcove, Root};
///
/// let root_system = RootSystem::type_a(2); // SU(3)
/// let alcove = Alcove::fundamental(&root_system);
///
/// // Weight inside alcove
/// let lambda = Root::new(vec![0.3, 0.3, -0.6]);
/// // (need to verify ⟨λ, αᵢ⟩ ≥ 0 and ⟨λ, θ⟩ ≤ 1)
/// ```
///
/// # References
///
/// - Kac, *Infinite Dimensional Lie Algebras*, §6.2
/// - Humphreys, *Reflection Groups and Coxeter Groups*, §4.4
#[derive(Debug, Clone)]
pub struct Alcove {
    /// The root system defining the alcove
    root_system: RootSystem,

    /// Simple roots (defining lower walls)
    simple_roots: Vec<Root>,

    /// Highest root θ (defining upper wall ⟨λ, θ⟩ ≤ level)
    highest_root: Root,

    /// Level k (affine level; typically 1 for fundamental alcove)
    level: f64,
}

impl Alcove {
    /// Construct the fundamental alcove at level k = 1.
    ///
    /// The fundamental alcove is:
    /// ```text
    /// A = {λ : ⟨λ, αᵢ⟩ ≥ 0, ⟨λ, θ⟩ ≤ 1}
    /// ```
    #[must_use]
    pub fn fundamental(root_system: &RootSystem) -> Self {
        let simple_roots = root_system.simple_roots.clone();
        let highest_root = root_system.highest_root();

        Self {
            root_system: root_system.clone(),
            simple_roots,
            highest_root,
            level: 1.0,
        }
    }

    /// Construct an alcove at a specified level k.
    ///
    /// # Arguments
    ///
    /// * `root_system` - The root system
    /// * `level` - Affine level k (positive integer for integrable modules)
    #[must_use]
    pub fn at_level(root_system: &RootSystem, level: f64) -> Self {
        assert!(level > 0.0, "Level must be positive");

        let simple_roots = root_system.simple_roots.clone();
        let highest_root = root_system.highest_root();

        Self {
            root_system: root_system.clone(),
            simple_roots,
            highest_root,
            level,
        }
    }

    /// Check if a weight is in the alcove.
    ///
    /// # Arguments
    ///
    /// * `weight` - Weight λ ∈ 𝔥* to test
    /// * `strict` - If true, use strict inequalities
    ///
    /// # Returns
    ///
    /// True if λ is in the alcove
    #[must_use]
    pub fn contains(&self, weight: &Root, strict: bool) -> bool {
        // For type A_n, roots have n+1 coordinates
        assert_eq!(weight.rank(), self.root_system.rank + 1);

        // Check lower walls: ⟨λ, αᵢ⟩ ≥ 0
        for alpha in &self.simple_roots {
            let pairing = weight.inner_product(alpha);

            if strict {
                if pairing <= 1e-10 {
                    return false;
                }
            } else if pairing < -1e-10 {
                return false;
            }
        }

        // Check upper wall: ⟨λ, θ⟩ ≤ level
        let pairing_highest = weight.inner_product(&self.highest_root);

        if strict {
            if pairing_highest >= self.level - 1e-10 {
                return false;
            }
        } else if pairing_highest > self.level + 1e-10 {
            return false;
        }

        true
    }

    /// Get the level of the alcove.
    #[must_use]
    pub fn level(&self) -> f64 {
        self.level
    }

    /// Get the highest root defining the upper wall.
    #[must_use]
    pub fn highest_root(&self) -> &Root {
        &self.highest_root
    }

    /// Compute vertices of the fundamental alcove.
    ///
    /// For type `A_n`, the fundamental alcove is a simplex with n+1 vertices:
    /// the origin and the fundamental weights {ω₁, ..., ωₙ}.
    ///
    /// # Returns
    ///
    /// Vector of vertices (as Roots in 𝔥*)
    #[must_use]
    pub fn vertices(&self) -> Vec<Root> {
        if self.level != 1.0 {
            // For level k ≠ 1, vertices are scaled: k·ωᵢ/(k + h∨)
            // where h∨ is the dual Coxeter number
            // Not implemented for now
            return vec![];
        }

        // For fundamental alcove at level 1:
        // Vertices are 0 and fundamental weights

        let mut vertices = vec![Root::new(vec![0.0; self.root_system.rank + 1])];

        // Add fundamental weights (computed from simple roots)
        // For type A_n, ωᵢ = (1/n+1) * (n+1-i, ..., n+1-i, -i, ..., -i)
        //                                 └── i times ──┘  └─ n+1-i ──┘

        let n = self.root_system.rank;

        for i in 1..=n {
            let mut coords = vec![0.0; n + 1];

            // ωᵢ has i coordinates equal to (n+1-i)/(n+1) and (n+1-i) equal to -i/(n+1)
            for j in 0..i {
                coords[j] = (n + 1 - i) as f64 / (n + 1) as f64;
            }
            for j in i..=n {
                coords[j] = -(i as f64) / (n + 1) as f64;
            }

            vertices.push(Root::new(coords));
        }

        vertices
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_root_operations() {
        let alpha = Root::new(vec![1.0, -1.0, 0.0]);
        let beta = Root::new(vec![0.0, 1.0, -1.0]);

        assert_eq!(alpha.rank(), 3);
        assert!((alpha.norm_squared() - 2.0).abs() < 1e-10);
        assert!((alpha.inner_product(&beta) + 1.0).abs() < 1e-10);
        assert!(alpha.is_positive());
        assert!(!alpha.negate().is_positive());
    }

    #[test]
    fn test_weyl_reflection() {
        let alpha = Root::new(vec![1.0, -1.0]);
        let beta = Root::new(vec![0.5, 0.5]);

        let reflected = alpha.reflect(&beta);
        // s_α(β) = β - 2⟨β,α⟩/⟨α,α⟩ · α
        // ⟨β,α⟩ = 0.5 - 0.5 = 0, so s_α(β) = β
        assert!((reflected.coords[0] - 0.5).abs() < 1e-10);
        assert!((reflected.coords[1] - 0.5).abs() < 1e-10);
    }

    #[test]
    fn test_cartan_integer() {
        let alpha = Root::new(vec![1.0, -1.0, 0.0]);
        let beta = Root::new(vec![0.0, 1.0, -1.0]);

        // ⟨β, α^∨⟩ = 2⟨β,α⟩/⟨α,α⟩ = 2(-1)/2 = -1
        assert_eq!(alpha.cartan_integer(&beta), -1);
        assert_eq!(beta.cartan_integer(&alpha), -1);
    }

    #[test]
    fn test_type_a1_su2() {
        let rs = RootSystem::type_a(1);

        assert_eq!(rs.rank(), 1);
        assert_eq!(rs.num_roots(), 2); // ±α
        assert_eq!(rs.num_positive_roots(), 1);
        assert_eq!(rs.dimension(), 3); // SU(2) dim = 1 + 2 = 3

        let cartan = rs.cartan_matrix();
        assert_eq!(cartan.len(), 1);
        assert_eq!(cartan[0][0], 2); // ⟨α, α^∨⟩ = 2
    }

    #[test]
    fn test_type_a2_su3() {
        let rs = RootSystem::type_a(2);

        assert_eq!(rs.rank(), 2);
        assert_eq!(rs.num_roots(), 6); // 3² - 1 = 8 total, 6 nonzero differences
        assert_eq!(rs.num_positive_roots(), 3);
        assert_eq!(rs.dimension(), 8); // SU(3) dim = 2 + 6 = 8

        let cartan = rs.cartan_matrix();
        assert_eq!(cartan.len(), 2);
        assert_eq!(cartan[0][0], 2);
        assert_eq!(cartan[1][1], 2);
        assert_eq!(cartan[0][1], -1); // Adjacent simple roots
        assert_eq!(cartan[1][0], -1);
    }

    #[test]
    fn test_simple_roots_su3() {
        let rs = RootSystem::type_a(2);
        let simple = rs.simple_roots();

        assert_eq!(simple.len(), 2);

        // α₁ = e₁ - e₂ = (1, -1, 0)
        assert!((simple[0].coords[0] - 1.0).abs() < 1e-10);
        assert!((simple[0].coords[1] + 1.0).abs() < 1e-10);
        assert!((simple[0].coords[2]).abs() < 1e-10);

        // α₂ = e₂ - e₃ = (0, 1, -1)
        assert!((simple[1].coords[0]).abs() < 1e-10);
        assert!((simple[1].coords[1] - 1.0).abs() < 1e-10);
        assert!((simple[1].coords[2] + 1.0).abs() < 1e-10);
    }

    #[test]
    fn test_positive_roots_su3() {
        let rs = RootSystem::type_a(2);
        let positive = rs.positive_roots();

        assert_eq!(positive.len(), 3);
        // Should have: e₁-e₂, e₂-e₃, e₁-e₃
    }

    #[test]
    fn test_dominant_weight() {
        let rs = RootSystem::type_a(2);

        // (1, 0, 0) should be dominant
        let weight1 = Root::new(vec![1.0, 0.0, 0.0]);
        assert!(rs.is_dominant_weight(&weight1));

        // (-1, 0, 0) should not be dominant
        let weight2 = Root::new(vec![-1.0, 0.0, 0.0]);
        assert!(!rs.is_dominant_weight(&weight2));
    }

    #[test]
    fn test_weight_lattice_su2() {
        let rs = RootSystem::type_a(1);
        let wl = WeightLattice::from_root_system(rs);

        assert_eq!(wl.fundamental_weights().len(), 1);

        // Spin-1 representation: Dynkin label [2]
        let weight = wl.dynkin_to_weight(&[2]);
        assert_eq!(weight.rank(), 2);
    }

    #[test]
    fn test_weight_lattice_su3() {
        let rs = RootSystem::type_a(2);
        let wl = WeightLattice::from_root_system(rs);

        assert_eq!(wl.fundamental_weights().len(), 2);

        // Fundamental representation: Dynkin labels [1, 0]
        let fund = wl.dynkin_to_weight(&[1, 0]);
        assert_eq!(fund.rank(), 3);

        // Adjoint representation: Dynkin labels [1, 1]
        let adj = wl.dynkin_to_weight(&[1, 1]);
        assert_eq!(adj.rank(), 3);
    }

    #[test]
    fn test_weyl_orbit_su2() {
        let rs = RootSystem::type_a(1);
        let weight = Root::new(vec![1.0, 0.0]);

        let orbit = rs.weyl_orbit(&weight);
        assert_eq!(orbit.len(), 2); // Weyl group S₂ has 2 elements
    }

    #[test]
    fn test_weyl_dimension_formula() {
        let rs = RootSystem::type_a(2); // SU(3)
        let wl = WeightLattice::from_root_system(rs);

        // Test known dimensions for SU(3)
        // Fundamental [1,0]: dim = 3
        let fund = wl.dynkin_to_weight(&[1, 0]);
        assert_eq!(wl.weyl_dimension(&fund), 3);

        // Anti-fundamental [0,1]: dim = 3
        let antifund = wl.dynkin_to_weight(&[0, 1]);
        assert_eq!(wl.weyl_dimension(&antifund), 3);

        // Adjoint [1,1]: dim = 8
        let adj = wl.dynkin_to_weight(&[1, 1]);
        assert_eq!(wl.weyl_dimension(&adj), 8);

        // [2,0]: dim = 6
        let sym2 = wl.dynkin_to_weight(&[2, 0]);
        assert_eq!(wl.weyl_dimension(&sym2), 6);

        // [0,2]: dim = 6
        let sym2_bar = wl.dynkin_to_weight(&[0, 2]);
        assert_eq!(wl.weyl_dimension(&sym2_bar), 6);

        // [1,0,0,...] for SU(2) - trivial rep has dim = 1
        let rs2 = RootSystem::type_a(1); // SU(2)
        let wl2 = WeightLattice::from_root_system(rs2);
        let trivial = wl2.dynkin_to_weight(&[0]);
        assert_eq!(wl2.weyl_dimension(&trivial), 1);

        // [2] for SU(2) - spin-1 has dim = 3
        let spin1 = wl2.dynkin_to_weight(&[2]);
        assert_eq!(wl2.weyl_dimension(&spin1), 3);
    }

    #[test]
    fn test_weights_of_irrep_su3_fundamental() {
        let rs = RootSystem::type_a(2);
        let wl = WeightLattice::from_root_system(rs);

        // Fundamental representation [1, 0] should have dimension 3
        let highest = wl.dynkin_to_weight(&[1, 0]);
        let weights = wl.weights_of_irrep(&highest);

        // Check exact dimension (Weyl dimension formula)
        let expected_dim = wl.weyl_dimension(&highest);
        assert_eq!(expected_dim, 3, "Fundamental rep has dimension 3");
        assert_eq!(
            weights.len(),
            3,
            "Should find exactly 3 weights, found {}",
            weights.len()
        );
    }

    #[test]
    fn test_weights_of_irrep_su3_adjoint() {
        let rs = RootSystem::type_a(2);
        let wl = WeightLattice::from_root_system(rs);

        // Adjoint representation [1, 1] should have dimension 8
        let highest = wl.dynkin_to_weight(&[1, 1]);
        let weights = wl.weights_of_irrep(&highest);

        // Check dimension (Weyl dimension formula)
        let dim = wl.weyl_dimension(&highest);
        assert_eq!(dim, 8, "Adjoint rep has dimension 8");

        // With Kostant's formula, we now compute proper multiplicities
        // For SU(3) adjoint: 6 roots (mult 1) + 1 zero weight (mult 2) = dimension 8
        let total_dim: usize = weights.iter().map(|(_, m)| m).sum();
        assert_eq!(
            total_dim, 8,
            "Total dimension (sum of multiplicities) should be 8"
        );
    }

    #[test]
    fn test_partition_function_basics() {
        // Test partition function on simple cases we can verify by hand
        let rs = RootSystem::type_a(1); // SU(2)
        let wl = WeightLattice::from_root_system(rs);

        // SU(2) has one positive root: α = [1, -1]
        // Test P(0) = 1 (empty sum)
        let zero = Root::new(vec![0.0, 0.0]);
        assert_eq!(wl.kostant_partition_function(&zero), 1, "P(0) = 1");

        // Test P(α) = 1 (just α itself)
        let alpha = Root::new(vec![1.0, -1.0]);
        assert_eq!(wl.kostant_partition_function(&alpha), 1, "P(α) = 1");

        // Test P(2α) = 1 (just 2·α)
        let two_alpha = Root::new(vec![2.0, -2.0]);
        assert_eq!(wl.kostant_partition_function(&two_alpha), 1, "P(2α) = 1");

        // Test P(-α) = 0 (negative, impossible)
        let neg_alpha = Root::new(vec![-1.0, 1.0]);
        assert_eq!(wl.kostant_partition_function(&neg_alpha), 0, "P(-α) = 0");
    }

    #[test]
    fn test_partition_function_su3() {
        // SU(3) has 3 positive roots: α₁=[1,-1,0], α₂=[0,1,-1], α₁+α₂=[1,0,-1]
        let rs = RootSystem::type_a(2);
        let wl = WeightLattice::from_root_system(rs);

        // Test P(0) = 1
        let zero = Root::new(vec![0.0, 0.0, 0.0]);
        assert_eq!(wl.kostant_partition_function(&zero), 1, "P(0) = 1");

        // Test P(α₁) = 1
        let alpha1 = Root::new(vec![1.0, -1.0, 0.0]);
        let p_alpha1 = wl.kostant_partition_function(&alpha1);
        eprintln!("P(α₁) = {}", p_alpha1);
        assert_eq!(p_alpha1, 1, "P(α₁) = 1");

        // Test P(α₁+α₂) = 2 (can write as α₁+α₂ OR as the root [1,0,-1])
        // Wait, [1,0,-1] IS α₁+α₂, so P(α₁+α₂) = 1
        let alpha1_plus_alpha2 = Root::new(vec![1.0, 0.0, -1.0]);
        let p_sum = wl.kostant_partition_function(&alpha1_plus_alpha2);
        eprintln!("P(α₁+α₂) = {}", p_sum);
        // This should be 1, not 2! Because [1,0,-1] is itself a positive root.
    }

    #[test]
    fn test_kostant_dimension_invariant() {
        // Verify that Kostant's formula satisfies the dimension invariant:
        // Σ_λ m(λ) = dim(V_Λ) for any representation
        let rs = RootSystem::type_a(2); // SU(3)
        let wl = WeightLattice::from_root_system(rs);

        // Test fundamental [1,0]: dimension 3
        let fund = wl.dynkin_to_weight(&[1, 0]);
        let fund_weights = wl.weights_of_irrep(&fund);
        eprintln!("Fundamental [1,0] weights:");
        for (w, m) in &fund_weights {
            eprintln!("  {:?} mult {}", w.coords, m);
        }
        let fund_dim: usize = fund_weights.iter().map(|(_, m)| m).sum();
        eprintln!("Total dimension: {} (expected 3)", fund_dim);
        assert_eq!(fund_dim, 3, "Fundamental rep dimension invariant");

        // Test adjoint [1,1]: dimension 8
        let adj = wl.dynkin_to_weight(&[1, 1]);
        let adj_weights = wl.weights_of_irrep(&adj);
        eprintln!("\nAdjoint [1,1] weights:");
        for (w, m) in &adj_weights {
            eprintln!("  {:?} mult {}", w.coords, m);
        }
        let adj_dim: usize = adj_weights.iter().map(|(_, m)| m).sum();
        eprintln!("Total dimension: {} (expected 8)", adj_dim);
        assert_eq!(adj_dim, 8, "Adjoint rep dimension invariant");

        // Test antifundamental [0,1]: dimension 3
        let anti = wl.dynkin_to_weight(&[0, 1]);
        let anti_weights = wl.weights_of_irrep(&anti);
        let anti_dim: usize = anti_weights.iter().map(|(_, m)| m).sum();
        assert_eq!(anti_dim, 3, "Antifundamental rep dimension invariant");
    }

    #[test]
    fn test_weight_diagram_string_su3() {
        let rs = RootSystem::type_a(2);
        let wl = WeightLattice::from_root_system(rs);

        let highest = wl.dynkin_to_weight(&[1, 0]);
        let diagram = wl.weight_diagram_string(&highest);

        // Check that diagram contains expected elements
        assert!(diagram.contains("Weight diagram"));
        assert!(diagram.contains("Dimension"));
        assert!(diagram.contains("Weights"));
    }

    #[test]
    fn test_weight_diagram_rank1_not_supported() {
        let rs = RootSystem::type_a(1);
        let wl = WeightLattice::from_root_system(rs);

        let highest = wl.dynkin_to_weight(&[2]);
        let diagram = wl.weight_diagram_string(&highest);

        assert!(diagram.contains("only implemented for rank 2"));
    }

    // --- Cartan Subalgebra Tests ---

    #[test]
    fn test_cartan_subalgebra_su2() {
        let cartan = CartanSubalgebra::type_a(1);

        assert_eq!(cartan.dimension(), 1);
        assert_eq!(cartan.matrix_size(), 2);
        assert_eq!(cartan.basis_matrices().len(), 1);

        // H₁ = diag(1, -1)
        let h1 = &cartan.basis_matrices()[0];
        assert!((h1[(0, 0)].re - 1.0).abs() < 1e-10);
        assert!((h1[(1, 1)].re + 1.0).abs() < 1e-10);
        assert!(h1[(0, 1)].norm() < 1e-10);
        assert!(h1[(1, 0)].norm() < 1e-10);
    }

    #[test]
    fn test_cartan_subalgebra_su3() {
        let cartan = CartanSubalgebra::type_a(2);

        assert_eq!(cartan.dimension(), 2);
        assert_eq!(cartan.matrix_size(), 3);
        assert_eq!(cartan.basis_matrices().len(), 2);

        // H₁ = diag(1, -1, 0)
        let h1 = &cartan.basis_matrices()[0];
        assert!((h1[(0, 0)].re - 1.0).abs() < 1e-10);
        assert!((h1[(1, 1)].re + 1.0).abs() < 1e-10);
        assert!(h1[(2, 2)].norm() < 1e-10);

        // H₂ = diag(0, 1, -1)
        let h2 = &cartan.basis_matrices()[1];
        assert!(h2[(0, 0)].norm() < 1e-10);
        assert!((h2[(1, 1)].re - 1.0).abs() < 1e-10);
        assert!((h2[(2, 2)].re + 1.0).abs() < 1e-10);
    }

    #[test]
    fn test_cartan_basis_commutes() {
        let cartan = CartanSubalgebra::type_a(2);

        // [H₁, H₂] = 0 (diagonal matrices commute)
        let h1 = &cartan.basis_matrices()[0];
        let h2 = &cartan.basis_matrices()[1];

        let commutator = h1.dot(h2) - h2.dot(h1);

        for val in commutator.iter() {
            assert!(val.norm() < 1e-10, "Cartan basis elements must commute");
        }
    }

    #[test]
    fn test_cartan_dual_basis() {
        let cartan = CartanSubalgebra::type_a(2);
        let dual = cartan.dual_basis();

        assert_eq!(dual.len(), 2);

        // α₁ = e₁ - e₂
        assert!((dual[0].coords[0] - 1.0).abs() < 1e-10);
        assert!((dual[0].coords[1] + 1.0).abs() < 1e-10);
        assert!(dual[0].coords[2].abs() < 1e-10);

        // α₂ = e₂ - e₃
        assert!(dual[1].coords[0].abs() < 1e-10);
        assert!((dual[1].coords[1] - 1.0).abs() < 1e-10);
        assert!((dual[1].coords[2] + 1.0).abs() < 1e-10);

        // These should be the simple roots
        let rs = RootSystem::type_a(2);
        let simple_roots = rs.simple_roots();

        for (d, s) in dual.iter().zip(simple_roots) {
            for (dc, sc) in d.coords.iter().zip(&s.coords) {
                assert!((dc - sc).abs() < 1e-10);
            }
        }
    }

    #[test]
    fn test_cartan_evaluate_root() {
        let cartan = CartanSubalgebra::type_a(1);

        // For SU(2), H = c₁ · diag(1, -1)
        // Root α = (1, -1) (the simple root)
        let root = Root::new(vec![1.0, -1.0]);

        // α(H) = 1·c₁ + (-1)·(-c₁) = 2c₁
        let result = cartan.evaluate_root(&root, &[1.0]);
        assert!((result.re - 2.0).abs() < 1e-10);
        assert!(result.im.abs() < 1e-10);

        let result2 = cartan.evaluate_root(&root, &[0.5]);
        assert!((result2.re - 1.0).abs() < 1e-10);
    }

    #[test]
    fn test_cartan_from_coefficients() {
        let cartan = CartanSubalgebra::type_a(2);

        // H = 2H₁ + 3H₂
        let h = cartan.from_coefficients(&[2.0, 3.0]);

        // Should be diag(2, -2+3, -3) = diag(2, 1, -3)
        assert!((h[(0, 0)].re - 2.0).abs() < 1e-10);
        assert!((h[(1, 1)].re - 1.0).abs() < 1e-10);
        assert!((h[(2, 2)].re + 3.0).abs() < 1e-10);

        // Check traceless
        let trace: Complex64 = (0..3).map(|i| h[(i, i)]).sum();
        assert!(trace.norm() < 1e-10);
    }

    #[test]
    fn test_cartan_project_matrix() {
        let cartan = CartanSubalgebra::type_a(2);

        // Create a diagonal matrix
        let mut mat = Array2::<Complex64>::zeros((3, 3));
        mat[(0, 0)] = Complex64::new(1.0, 0.0);
        mat[(1, 1)] = Complex64::new(-0.5, 0.0);
        mat[(2, 2)] = Complex64::new(-0.5, 0.0);

        let coeffs = cartan
            .project_matrix(&mat)
            .expect("Rank 2 should be supported");
        assert_eq!(coeffs.len(), 2);

        // Reconstruct and check
        let reconstructed = cartan.from_coefficients(&coeffs);

        // Should reconstruct exactly for diagonal traceless matrices
        for i in 0..3 {
            let diff = (mat[(i, i)] - reconstructed[(i, i)]).norm();
            assert!(
                diff < 1e-10,
                "Diagonal projection should be exact: diff at ({},{}) = {}",
                i,
                i,
                diff
            );
        }
    }

    #[test]
    fn test_cartan_contains() {
        let cartan = CartanSubalgebra::type_a(2);

        // Diagonal traceless matrix - should be in Cartan
        let mut h = Array2::<Complex64>::zeros((3, 3));
        h[(0, 0)] = Complex64::new(1.0, 0.0);
        h[(1, 1)] = Complex64::new(-0.5, 0.0);
        h[(2, 2)] = Complex64::new(-0.5, 0.0);

        assert!(cartan.contains(&h, 1e-6));

        // Non-diagonal matrix - should not be in Cartan
        let mut not_h = Array2::<Complex64>::zeros((3, 3));
        not_h[(0, 1)] = Complex64::new(1.0, 0.0);

        assert!(!cartan.contains(&not_h, 1e-6));

        // Diagonal but not traceless - should not be in Cartan
        let mut not_traceless = Array2::<Complex64>::zeros((3, 3));
        not_traceless[(0, 0)] = Complex64::new(1.0, 0.0);

        assert!(!cartan.contains(&not_traceless, 1e-6));
    }

    #[test]
    fn test_cartan_killing_form() {
        let cartan = CartanSubalgebra::type_a(1);

        // For SU(2), κ(H, H) = 2(2)·2 = 8 for H = H₁
        let killing = cartan.killing_form(&[1.0], &[1.0]);
        assert!((killing - 8.0).abs() < 1e-10);

        // κ(H, 0) = 0
        let killing_zero = cartan.killing_form(&[1.0], &[0.0]);
        assert!(killing_zero.abs() < 1e-10);

        // κ is symmetric
        let k12 = cartan.killing_form(&[1.0], &[2.0]);
        let k21 = cartan.killing_form(&[2.0], &[1.0]);
        assert!((k12 - k21).abs() < 1e-10);
    }

    #[test]
    fn test_cartan_killing_form_su3() {
        let cartan = CartanSubalgebra::type_a(2);

        // For SU(3), κ(Hᵢ, Hⱼ) = 2(3)·2δᵢⱼ = 12δᵢⱼ
        let k11 = cartan.killing_form(&[1.0, 0.0], &[1.0, 0.0]);
        assert!((k11 - 12.0).abs() < 1e-10);

        let k22 = cartan.killing_form(&[0.0, 1.0], &[0.0, 1.0]);
        assert!((k22 - 12.0).abs() < 1e-10);

        // Off-diagonal should be zero for orthogonal basis
        let k12 = cartan.killing_form(&[1.0, 0.0], &[0.0, 1.0]);
        assert!(k12.abs() < 1e-10);
    }

    #[test]
    fn test_weyl_chamber_contains_su2() {
        let rs = RootSystem::type_a(1); // SU(2)
        let chamber = WeylChamber::fundamental(&rs);

        // Simple root α = (1, -1)
        // Dominant weights satisfy ⟨λ, α⟩ ≥ 0

        // Dominant weight: (1, 0)
        let dominant = Root::new(vec![1.0, 0.0]);
        assert!(chamber.contains(&dominant, false));
        assert!(chamber.contains(&dominant, true));

        // Non-dominant: (-1, 0)
        let non_dominant = Root::new(vec![-1.0, 0.0]);
        assert!(!chamber.contains(&non_dominant, false));

        // Boundary: (0, 0)
        let boundary = Root::new(vec![0.0, 0.0]);
        assert!(chamber.contains(&boundary, false)); // Non-strict allows boundary
        assert!(!chamber.contains(&boundary, true)); // Strict excludes boundary
    }

    #[test]
    fn test_weyl_chamber_contains_su3() {
        let rs = RootSystem::type_a(2); // SU(3)
        let chamber = WeylChamber::fundamental(&rs);

        // Simple roots: α₁ = (1, -1, 0), α₂ = (0, 1, -1)
        // Dominant: ⟨λ, α₁⟩ ≥ 0 and ⟨λ, α₂⟩ ≥ 0

        // Fundamental weight ω₁ = (1, 0, 0) - 1/3 for traceless
        let omega1 = Root::new(vec![2.0 / 3.0, -1.0 / 3.0, -1.0 / 3.0]);
        assert!(chamber.contains(&omega1, false));

        // Negative weight
        let neg = Root::new(vec![-1.0, 0.0, 1.0]);
        assert!(!chamber.contains(&neg, false));

        // Origin
        let origin = Root::new(vec![0.0, 0.0, 0.0]);
        assert!(chamber.contains(&origin, false));
        assert!(!chamber.contains(&origin, true));
    }

    #[test]
    fn test_weyl_chamber_project() {
        let rs = RootSystem::type_a(1); // SU(2)
        let chamber = WeylChamber::fundamental(&rs);

        // Already dominant
        let dominant = Root::new(vec![1.0, -1.0]);
        let projected = chamber.project(&dominant);
        assert!((projected.coords[0] - dominant.coords[0]).abs() < 1e-10);
        assert!((projected.coords[1] - dominant.coords[1]).abs() < 1e-10);

        // Non-dominant: should reflect to dominant
        let non_dominant = Root::new(vec![-1.0, 1.0]);
        let projected = chamber.project(&non_dominant);

        // After reflection by α = (1, -1), should get (1, -1)
        assert!(chamber.contains(&projected, false));
        assert!((projected.coords[0] - 1.0).abs() < 1e-10);
        assert!((projected.coords[1] + 1.0).abs() < 1e-10);
    }

    #[test]
    fn test_weyl_chamber_project_su3() {
        let rs = RootSystem::type_a(2); // SU(3)
        let chamber = WeylChamber::fundamental(&rs);

        // Non-dominant weight
        let lambda = Root::new(vec![-1.0, 2.0, -1.0]);
        let projected = chamber.project(&lambda);

        // Projected weight should be dominant
        assert!(chamber.contains(&projected, false));

        // Check that projection preserves norm (Weyl group is orthogonal)
        let norm_before = lambda.norm_squared();
        let norm_after = projected.norm_squared();
        assert!((norm_before - norm_after).abs() < 1e-8);
    }

    #[test]
    fn test_alcove_contains_su2() {
        let rs = RootSystem::type_a(1); // SU(2)
        let alcove = Alcove::fundamental(&rs);

        // Highest root for SU(2): θ = (1, -1) (same as simple root)
        // Alcove: ⟨λ, α⟩ ≥ 0 and ⟨λ, θ⟩ ≤ 1

        // Inside alcove: (0.4, -0.4)
        let inside = Root::new(vec![0.4, -0.4]);
        // ⟨inside, α⟩ = 0.4·1 + (-0.4)·(-1) = 0.8 ≥ 0 ✓
        // ⟨inside, θ⟩ = 0.4·1 + (-0.4)·(-1) = 0.8 ≤ 1 ✓
        assert!(alcove.contains(&inside, false));

        // Outside alcove (too large): (1, -1)
        let outside = Root::new(vec![1.0, -1.0]);
        // ⟨outside, θ⟩ = 2 > 1
        assert!(!alcove.contains(&outside, false));

        // Boundary: origin
        let origin = Root::new(vec![0.0, 0.0]);
        assert!(alcove.contains(&origin, false));
        assert!(!alcove.contains(&origin, true)); // Strict excludes boundary
    }

    #[test]
    fn test_alcove_vertices_su2() {
        let rs = RootSystem::type_a(1); // SU(2)
        let alcove = Alcove::fundamental(&rs);

        let vertices = alcove.vertices();

        // For SU(2), fundamental alcove has 2 vertices: 0 and ω₁
        assert_eq!(vertices.len(), 2);

        // First vertex: origin
        assert!(vertices[0].coords[0].abs() < 1e-10);
        assert!(vertices[0].coords[1].abs() < 1e-10);

        // Second vertex: fundamental weight ω₁ = (1/2, -1/2)
        assert!((vertices[1].coords[0] - 0.5).abs() < 1e-10);
        assert!((vertices[1].coords[1] + 0.5).abs() < 1e-10);
    }

    #[test]
    fn test_alcove_vertices_su3() {
        let rs = RootSystem::type_a(2); // SU(3)
        let alcove = Alcove::fundamental(&rs);

        let vertices = alcove.vertices();

        // For SU(3), fundamental alcove has 3 vertices: 0, ω₁, ω₂
        assert_eq!(vertices.len(), 3);

        // First vertex: origin
        for &coord in &vertices[0].coords {
            assert!(coord.abs() < 1e-10);
        }

        // Verify all vertices are in the alcove
        for vertex in &vertices {
            assert!(alcove.contains(vertex, false));
        }
    }

    #[test]
    fn test_alcove_at_level() {
        let rs = RootSystem::type_a(1); // SU(2)
        let alcove_k2 = Alcove::at_level(&rs, 2.0);

        assert_eq!(alcove_k2.level(), 2.0);

        // At level 2, the upper bound is ⟨λ, θ⟩ ≤ 2
        let inside = Root::new(vec![0.8, -0.8]);
        // ⟨inside, θ⟩ = 1.6 ≤ 2 ✓
        assert!(alcove_k2.contains(&inside, false));

        let outside = Root::new(vec![1.5, -1.5]);
        // ⟨outside, θ⟩ = 3 > 2
        assert!(!alcove_k2.contains(&outside, false));
    }

    #[test]
    fn test_alcove_highest_root() {
        let rs = RootSystem::type_a(2); // SU(3)
        let alcove = Alcove::fundamental(&rs);

        let theta = alcove.highest_root();

        // For SU(3), highest root is θ = α₁ + α₂ = (1, 0, -1)
        assert!((theta.coords[0] - 1.0).abs() < 1e-10);
        assert!(theta.coords[1].abs() < 1e-10);
        assert!((theta.coords[2] + 1.0).abs() < 1e-10);

        // Verify it's the longest root
        let theta_norm = theta.norm_squared();
        for root in rs.positive_roots() {
            assert!(root.norm_squared() <= theta_norm + 1e-10);
        }
    }

    // --- Simple Root Expansion Tests ---

    #[test]
    fn test_simple_root_expansion_su2() {
        let rs = RootSystem::type_a(1); // SU(2)

        // SU(2) has one simple root: α₁ = e₀ - e₁ = (1, -1)
        // The only positive root is α₁ itself

        // Test the simple root itself: α₁ = 1·α₁
        let alpha1 = Root::new(vec![1.0, -1.0]);
        let coeffs = rs.simple_root_expansion(&alpha1);
        assert_eq!(coeffs, Some(vec![1]));

        // Test the negative root: -α₁ = (-1)·α₁
        let neg_alpha1 = Root::new(vec![-1.0, 1.0]);
        let coeffs_neg = rs.simple_root_expansion(&neg_alpha1);
        assert_eq!(coeffs_neg, Some(vec![-1]));
    }

    #[test]
    fn test_simple_root_expansion_su3() {
        let rs = RootSystem::type_a(2); // SU(3)

        // SU(3) has 2 simple roots:
        // α₁ = e₀ - e₁ = (1, -1, 0)
        // α₂ = e₁ - e₂ = (0, 1, -1)
        // And a third positive root: α₁ + α₂ = e₀ - e₂ = (1, 0, -1)

        // Test α₁ = (1, -1, 0): coefficients [1, 0]
        let alpha1 = Root::new(vec![1.0, -1.0, 0.0]);
        assert_eq!(rs.simple_root_expansion(&alpha1), Some(vec![1, 0]));

        // Test α₂ = (0, 1, -1): coefficients [0, 1]
        let alpha2 = Root::new(vec![0.0, 1.0, -1.0]);
        assert_eq!(rs.simple_root_expansion(&alpha2), Some(vec![0, 1]));

        // Test α₁ + α₂ = (1, 0, -1): coefficients [1, 1]
        let alpha1_plus_alpha2 = Root::new(vec![1.0, 0.0, -1.0]);
        assert_eq!(
            rs.simple_root_expansion(&alpha1_plus_alpha2),
            Some(vec![1, 1])
        );

        // Test negative roots
        let neg_alpha1 = Root::new(vec![-1.0, 1.0, 0.0]);
        assert_eq!(rs.simple_root_expansion(&neg_alpha1), Some(vec![-1, 0]));

        let neg_highest = Root::new(vec![-1.0, 0.0, 1.0]);
        assert_eq!(rs.simple_root_expansion(&neg_highest), Some(vec![-1, -1]));
    }

    #[test]
    fn test_simple_root_expansion_su4() {
        let rs = RootSystem::type_a(3); // SU(4)

        // SU(4) has 3 simple roots:
        // α₁ = e₀ - e₁ = (1, -1, 0, 0)
        // α₂ = e₁ - e₂ = (0, 1, -1, 0)
        // α₃ = e₂ - e₃ = (0, 0, 1, -1)

        // Test simple roots
        let alpha1 = Root::new(vec![1.0, -1.0, 0.0, 0.0]);
        assert_eq!(rs.simple_root_expansion(&alpha1), Some(vec![1, 0, 0]));

        let alpha2 = Root::new(vec![0.0, 1.0, -1.0, 0.0]);
        assert_eq!(rs.simple_root_expansion(&alpha2), Some(vec![0, 1, 0]));

        let alpha3 = Root::new(vec![0.0, 0.0, 1.0, -1.0]);
        assert_eq!(rs.simple_root_expansion(&alpha3), Some(vec![0, 0, 1]));

        // Test e₀ - e₂ = α₁ + α₂: coefficients [1, 1, 0]
        let e0_minus_e2 = Root::new(vec![1.0, 0.0, -1.0, 0.0]);
        assert_eq!(rs.simple_root_expansion(&e0_minus_e2), Some(vec![1, 1, 0]));

        // Test e₁ - e₃ = α₂ + α₃: coefficients [0, 1, 1]
        let e1_minus_e3 = Root::new(vec![0.0, 1.0, 0.0, -1.0]);
        assert_eq!(rs.simple_root_expansion(&e1_minus_e3), Some(vec![0, 1, 1]));

        // Test highest root e₀ - e₃ = α₁ + α₂ + α₃: coefficients [1, 1, 1]
        let highest = Root::new(vec![1.0, 0.0, 0.0, -1.0]);
        assert_eq!(rs.simple_root_expansion(&highest), Some(vec![1, 1, 1]));

        // Test negative of highest root: coefficients [-1, -1, -1]
        let neg_highest = Root::new(vec![-1.0, 0.0, 0.0, 1.0]);
        assert_eq!(
            rs.simple_root_expansion(&neg_highest),
            Some(vec![-1, -1, -1])
        );
    }

    #[test]
    fn test_simple_root_expansion_not_a_root() {
        let rs = RootSystem::type_a(2); // SU(3)

        // Test a vector that is not a root
        let not_a_root = Root::new(vec![1.0, 1.0, -2.0]);
        assert_eq!(rs.simple_root_expansion(&not_a_root), None);

        // Test zero vector
        let zero = Root::new(vec![0.0, 0.0, 0.0]);
        assert_eq!(rs.simple_root_expansion(&zero), None);
    }

    #[test]
    fn test_simple_root_expansion_roundtrip() {
        // Verify that we can reconstruct roots from their expansions
        let rs = RootSystem::type_a(2); // SU(3)

        for root in rs.roots() {
            let coeffs = rs.simple_root_expansion(root);
            assert!(coeffs.is_some(), "All roots should have expansions");

            let coeffs = coeffs.unwrap();
            let simple = rs.simple_roots();

            // Reconstruct: Σ cᵢ αᵢ
            let mut reconstructed = vec![0.0; root.coords.len()];
            for (i, &c) in coeffs.iter().enumerate() {
                for (j, &coord) in simple[i].coords.iter().enumerate() {
                    reconstructed[j] += (c as f64) * coord;
                }
            }

            // Compare with original
            for (orig, recon) in root.coords.iter().zip(&reconstructed) {
                assert!(
                    (orig - recon).abs() < 1e-10,
                    "Reconstruction failed for root {:?}: expected {:?}, got {:?}",
                    root.coords,
                    root.coords,
                    reconstructed
                );
            }
        }
    }
}