rivrs-sparse 0.1.1

Sparse linear algebra solvers
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
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
6673
6674
6675
6676
6677
6678
6679
6680
6681
6682
6683
6684
6685
6686
6687
6688
6689
6690
6691
6692
6693
6694
6695
6696
6697
6698
6699
6700
6701
6702
6703
6704
6705
6706
6707
6708
6709
6710
6711
6712
6713
6714
6715
6716
6717
6718
6719
6720
6721
6722
6723
6724
6725
6726
6727
6728
6729
6730
6731
6732
6733
6734
6735
6736
6737
6738
6739
6740
6741
6742
6743
6744
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
6755
6756
6757
6758
6759
6760
6761
6762
6763
6764
6765
6766
6767
6768
6769
6770
6771
6772
6773
6774
6775
6776
6777
6778
6779
6780
6781
6782
6783
6784
6785
6786
6787
6788
6789
6790
6791
6792
6793
6794
6795
6796
6797
6798
6799
6800
6801
6802
6803
6804
6805
6806
6807
6808
6809
6810
6811
6812
6813
6814
6815
6816
6817
6818
6819
6820
6821
6822
6823
6824
6825
6826
6827
6828
6829
6830
6831
6832
6833
6834
6835
6836
6837
6838
6839
6840
6841
6842
6843
6844
6845
6846
6847
6848
6849
6850
6851
6852
6853
6854
6855
6856
6857
6858
6859
6860
6861
6862
6863
6864
6865
6866
6867
6868
6869
6870
6871
6872
6873
6874
6875
6876
6877
6878
6879
6880
6881
6882
6883
6884
6885
6886
6887
6888
6889
6890
6891
6892
6893
6894
6895
6896
6897
6898
6899
6900
6901
6902
6903
6904
6905
6906
6907
6908
6909
6910
6911
6912
6913
6914
6915
6916
6917
6918
6919
6920
6921
6922
6923
6924
6925
6926
6927
6928
6929
6930
6931
6932
6933
6934
6935
6936
6937
6938
6939
6940
6941
6942
6943
6944
6945
6946
6947
6948
6949
6950
6951
6952
6953
6954
6955
6956
6957
6958
6959
6960
6961
6962
6963
6964
6965
6966
6967
6968
6969
6970
6971
6972
6973
6974
6975
6976
6977
6978
6979
6980
6981
6982
6983
6984
6985
6986
6987
6988
6989
6990
6991
6992
6993
6994
6995
6996
6997
6998
6999
7000
7001
7002
7003
7004
7005
7006
7007
7008
7009
7010
7011
7012
7013
7014
7015
7016
7017
7018
7019
7020
7021
7022
7023
7024
7025
7026
7027
7028
7029
7030
7031
7032
7033
7034
7035
7036
7037
7038
7039
7040
7041
7042
7043
7044
7045
7046
7047
7048
7049
7050
7051
7052
7053
7054
7055
7056
7057
7058
7059
7060
7061
7062
7063
7064
7065
7066
7067
7068
7069
7070
7071
7072
7073
7074
7075
7076
7077
7078
7079
7080
7081
7082
7083
7084
7085
7086
7087
7088
7089
7090
7091
7092
7093
7094
7095
7096
7097
7098
7099
7100
7101
7102
7103
7104
7105
7106
7107
7108
7109
7110
7111
7112
7113
7114
7115
7116
7117
7118
7119
7120
7121
7122
7123
7124
7125
7126
7127
7128
7129
7130
7131
7132
7133
7134
7135
7136
7137
7138
7139
7140
7141
7142
7143
7144
7145
7146
7147
7148
7149
7150
7151
7152
7153
7154
7155
7156
7157
7158
7159
7160
7161
7162
7163
7164
7165
7166
7167
7168
7169
7170
7171
7172
7173
7174
7175
7176
7177
7178
7179
7180
7181
7182
7183
7184
7185
7186
7187
7188
7189
7190
7191
7192
7193
7194
7195
7196
7197
7198
7199
7200
7201
7202
7203
7204
7205
7206
7207
7208
7209
7210
7211
7212
7213
7214
7215
7216
7217
7218
7219
7220
7221
7222
7223
7224
7225
7226
7227
7228
7229
7230
7231
7232
7233
7234
7235
7236
7237
7238
7239
7240
7241
7242
7243
7244
7245
7246
7247
7248
7249
7250
7251
7252
7253
7254
7255
7256
7257
7258
7259
7260
//! Dense APTP (A Posteriori Threshold Pivoting) factorization kernel.
//!
//! Implements the APTP algorithm for dense symmetric indefinite matrices —
//! the core numerical kernel for the SSIDS multifrontal solver. The kernel
//! factors a dense frontal matrix in place using an optimistic 1x1 pivot
//! strategy with a posteriori stability checking, falling back to 2x2
//! Bunch-Kaufman pivots or column delay when stability bounds are violated.
//!
//! # Algorithm
//!
//! Implements the single-level (column-by-column) APTP strategy from
//! Duff, Hogg & Lopez (2020), "A New Sparse LDL^T Solver Using A Posteriori
//! Threshold Pivoting", SIAM J. Sci. Comput. 42(4).
//!
//! For each column k (left-to-right):
//! 1. Attempt 1x1 pivot: compute L column, check stability bound |l_ij| < 1/u
//! 2. On pass: accept, apply rank-1 Schur complement update
//! 3. On fail: either attempt 2x2 Bunch-Kaufman pivot (with best partner) or
//!    immediately delay, depending on [`AptpFallback`] strategy
//! 4. After all columns: permute delayed columns to end of ordering
//!
//! # Complexity
//!
//! - Time: O(n^3) for full n x n factorization (dominated by Schur complement updates)
//! - Space: O(n^2) for the input matrix (factored in place) plus O(n) for D and permutation
//!
//! # References
//!
//! - Duff, Hogg & Lopez (2020), "A New Sparse LDL^T Solver Using A Posteriori
//!   Threshold Pivoting", SIAM J. Sci. Comput. 42(4)
//! - Bunch & Kaufman (1977), "Some Stable Methods for Calculating Inertia and
//!   Solving Symmetric Linear Systems", Math. Comp.

// SPRAL Equivalent: `LDLT<T, BLOCK_SIZE, Backup>::factor()` in
// `spral/src/ssids/cpu/kernels/ldlt_app.cxx` (BSD-3).

use faer::Par;
use faer::linalg::matmul::triangular::{self as tri_matmul, BlockStructure};
use faer::linalg::triangular_solve;
use faer::perm::Perm;
use faer::prelude::*;
use faer::{Accum, Conj, Mat, MatMut, MatRef};

use super::diagonal::MixedDiagonal;
use super::pivot::{Block2x2, PivotType};
use crate::error::SparseError;
use crate::ordering::perm_from_forward;

/// Bunch-Kaufman α parameter for 2×2 determinant condition: |det| ≥ α × |a₂₁|².
const BUNCH_KAUFMAN_ALPHA: f64 = 0.5;

/// Growth factor bound for complete pivoting: max |L_ij| ≤ COMPLETE_PIVOTING_GROWTH_BOUND.
/// From Algorithm 4.1 of Duff, Hogg & Lopez (2020).
#[cfg(test)]
const COMPLETE_PIVOTING_GROWTH_BOUND: f64 = 4.0;

// ---------------------------------------------------------------------------
// Configuration types
// ---------------------------------------------------------------------------

/// Configuration for the APTP factorization kernel.
///
/// # Defaults
/// - `threshold`: 0.01 (growth factor bound of 100)
/// - `small`: 1e-20 (singularity detection)
/// - `fallback`: [`AptpFallback::BunchKaufman`]
/// - `outer_block_size`: 256 (two-level outer block)
/// - `inner_block_size`: 32 (two-level inner block)
///
/// # Block Size Parameters (Two-Level APTP)
///
/// For frontal matrices larger than `outer_block_size`, the kernel uses
/// a two-level blocked algorithm (Duff, Hogg & Lopez 2020, Section 3):
/// - Outer loop processes blocks of `outer_block_size` columns
/// - Inner loop processes sub-blocks of `inner_block_size` columns
/// - Innermost ib×ib diagonal blocks use complete pivoting (Algorithm 4.1)
///
/// For frontal matrices ≤ `outer_block_size`, the kernel processes the
/// entire fully-summed portion as a single inner block.
///
/// # References
///
/// - Duff, Hogg & Lopez (2020), Section 4: threshold parameter u
/// - Duff, Hogg & Lopez (2020), Section 3: two-level blocking with nb=256, ib=32
#[derive(Debug, Clone)]
pub struct AptpOptions {
    /// Stability threshold u: entries must satisfy |l_ij| < 1/u.
    /// Must be in (0.0, 1.0]. Default: 0.01.
    pub threshold: f64,
    /// Singularity detection: pivots with |d| < small treated as zero.
    /// Must be >= 0.0. Default: 1e-20.
    pub small: f64,
    /// Strategy when a 1x1 pivot fails the stability check.
    pub fallback: AptpFallback,
    /// Outer block size (nb) for two-level APTP. Default: 256.
    /// Must be > 0 and >= inner_block_size.
    pub outer_block_size: usize,
    /// Inner block size (ib) for two-level APTP. Default: 32.
    /// Must be > 0 and <= outer_block_size.
    pub inner_block_size: usize,
    /// Strategy for columns that APTP fails to eliminate. Default: Tpp.
    pub failed_pivot_method: FailedPivotMethod,
    /// Parallelism control for BLAS-3 operations (TRSM, GEMM). Default: `Par::Seq`.
    pub par: Par,
    /// Minimum supernode size for amalgamation. Supernodes with fewer than
    /// `nemin` eliminated columns may be merged with their parent. Default: 32.
    ///
    /// Setting `nemin = 1` disables amalgamation entirely.
    // SPRAL Equivalent: `options%nemin` (`datatypes.f90:21`).
    pub nemin: usize,
    /// Front-size threshold for small-leaf subtree fast path. Default: 256.
    /// Set to 0 to disable.
    pub small_leaf_threshold: usize,
}

impl Default for AptpOptions {
    fn default() -> Self {
        Self {
            threshold: 0.01,
            small: 1e-20,
            fallback: AptpFallback::BunchKaufman,
            outer_block_size: 256,
            inner_block_size: 32,
            failed_pivot_method: FailedPivotMethod::Tpp,
            par: Par::Seq,
            nemin: 32,
            small_leaf_threshold: 256,
        }
    }
}

/// Fallback strategy when a 1x1 pivot fails the stability check.
///
/// # References
///
/// - Duff, Hogg & Lopez (2020), Algorithm 3.1: fallback strategies
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum AptpFallback {
    /// Attempt 2x2 Bunch-Kaufman pivot; delay if that also fails.
    BunchKaufman,
    /// Immediately delay the column without attempting 2x2.
    Delay,
}

/// Strategy for handling columns that APTP fails to eliminate.
///
/// When APTP's block-scoped search cannot find acceptable pivots for some
/// columns, the `FailedPivotMethod` controls what happens next.
///
/// # References
///
/// - Duff, Hogg & Lopez (2020), Section 3: TPP fallback after APTP
/// - SPRAL `options%failed_pivot_method`: 0 = pass, 1 = tpp (default)
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum FailedPivotMethod {
    /// Retry failed columns with serial Threshold Partial Pivoting (default).
    ///
    /// TPP searches ALL remaining columns for acceptable pivots, which
    /// succeeds where APTP's block-scoped search fails.
    Tpp,
    /// Pass failed columns directly to parent as delays (no retry).
    Pass,
}

// ---------------------------------------------------------------------------
// Kernel workspace
// ---------------------------------------------------------------------------

/// Pre-allocated reusable buffers for the BLAS-3 inner loop.
///
/// Eliminates per-block heap allocations inside `factor_inner` and
/// `two_level_factor`. Sized to `max_front × inner_block_size` and reused
/// across all block iterations within and across supernodes.
///
/// # References
///
/// - SPRAL `NumericSubtree.hxx:75-81`: per-thread workspace pattern
pub(crate) struct AptpKernelWorkspace {
    /// Block backup for restore-on-failure, max_front × inner_block_size.
    backup: Mat<f64>,
    /// Copy of L11 block for TRSM aliasing avoidance, inner_block_size × inner_block_size.
    l11_buf: Mat<f64>,
    /// L·D product workspace for update_trailing/cross_terms, max_front × inner_block_size.
    ld_buf: Mat<f64>,
    /// Copy buffer for L21/L_panel aliasing avoidance, max_front × inner_block_size.
    copy_buf: Mat<f64>,
}

impl AptpKernelWorkspace {
    /// Create a new kernel workspace sized for the given maximum front and
    /// inner block dimensions.
    pub(crate) fn new(max_front: usize, inner_block_size: usize) -> Self {
        Self {
            backup: Mat::zeros(max_front, inner_block_size),
            l11_buf: Mat::zeros(inner_block_size, inner_block_size),
            ld_buf: Mat::zeros(max_front, inner_block_size),
            copy_buf: Mat::zeros(max_front, inner_block_size),
        }
    }
}

// ---------------------------------------------------------------------------
// Result types
// ---------------------------------------------------------------------------

/// Result of in-place APTP factorization.
///
/// The L factor is stored in the lower triangle of the mutated input matrix.
/// Only the first `num_eliminated` columns contain valid L entries.
#[derive(Debug)]
pub struct AptpFactorResult {
    /// Block diagonal D factor with mixed 1x1/2x2 blocks.
    pub d: MixedDiagonal,
    /// Column permutation: `perm[i]` = original column index at position i.
    pub perm: Vec<usize>,
    /// Number of successfully eliminated columns (q <= num_fully_summed).
    pub num_eliminated: usize,
    /// Original column indices that were not eliminated.
    pub delayed_cols: Vec<usize>,
    /// Summary statistics for diagnostics.
    pub stats: AptpStatistics,
    /// Per-column diagnostic log.
    pub pivot_log: Vec<AptpPivotRecord>,
}

/// Result of convenience APTP factorization (with extracted L).
#[derive(Debug)]
pub struct AptpFactorization {
    /// Unit lower triangular factor (extracted from in-place result).
    pub l: Mat<f64>,
    /// Block diagonal D factor with mixed 1x1/2x2 blocks.
    pub d: MixedDiagonal,
    /// Column permutation as faer Perm type.
    pub perm: Perm<usize>,
    /// Original column indices not eliminated.
    pub delayed_cols: Vec<usize>,
    /// Summary statistics.
    pub stats: AptpStatistics,
    /// Per-column diagnostic log.
    pub pivot_log: Vec<AptpPivotRecord>,
}

/// Summary statistics from factorization.
///
/// Invariant: `num_1x1 + 2 * num_2x2 + num_delayed == total_fully_summed_columns`
#[derive(Debug, Clone, Default)]
pub struct AptpStatistics {
    /// Count of 1x1 pivots accepted.
    pub num_1x1: usize,
    /// Count of 2x2 pivot pairs (each pair counts as 1).
    pub num_2x2: usize,
    /// Count of delayed columns.
    pub num_delayed: usize,
    /// Maximum absolute value across all L entries (stability metric).
    pub max_l_entry: f64,
}

/// Per-column pivot diagnostic record.
#[derive(Debug, Clone)]
pub struct AptpPivotRecord {
    /// Original column index.
    pub col: usize,
    /// Classification (OneByOne, TwoByTwo, Delayed).
    pub pivot_type: PivotType,
    /// Worst stability metric for this column's L entries.
    pub max_l_entry: f64,
    /// True if 2x2 fallback was attempted (regardless of outcome).
    pub was_fallback: bool,
}

// ---------------------------------------------------------------------------
// Public API
// ---------------------------------------------------------------------------

/// Factor a dense symmetric matrix using A Posteriori Threshold Pivoting.
///
/// Performs partial LDL^T factorization of the first `num_fully_summed`
/// columns of the input matrix `a`. The L factor is stored in-place in the
/// lower triangle of `a`. The contribution block (trailing submatrix after
/// the fully-summed columns) is updated with the Schur complement.
///
/// # Arguments
/// - `a`: Dense symmetric matrix (m x m), mutated in place. Only the lower
///   triangle is read; the upper triangle may contain arbitrary values.
///   On return, the lower triangle of the first `num_eliminated` columns
///   contains the L factor entries (unit diagonal implicit).
/// - `num_fully_summed`: Number of columns eligible for elimination (p <= m).
///   For full factorization, pass `a.nrows()`.
/// - `options`: APTP configuration (threshold, fallback strategy).
///
/// # Returns
/// `AptpFactorResult` containing D factor, column permutation, delayed
/// column indices, and diagnostics.
///
/// # Errors
/// Returns `SparseError::InvalidInput` if dimensions are inconsistent.
///
/// # Algorithm
/// Implements the APTP strategy from Duff, Hogg & Lopez (2020).
/// Single-level (column-by-column) variant.
pub fn aptp_factor_in_place(
    mut a: MatMut<'_, f64>,
    num_fully_summed: usize,
    options: &AptpOptions,
) -> Result<AptpFactorResult, SparseError> {
    let m = a.nrows();
    if a.ncols() != m {
        return Err(SparseError::InvalidInput {
            reason: format!("matrix must be square, got {}x{}", m, a.ncols()),
        });
    }
    if num_fully_summed > m {
        return Err(SparseError::InvalidInput {
            reason: format!(
                "num_fully_summed ({}) > matrix dimension ({})",
                num_fully_summed, m
            ),
        });
    }
    if options.threshold <= 0.0 || options.threshold > 1.0 {
        return Err(SparseError::InvalidInput {
            reason: format!("threshold must be in (0, 1], got {}", options.threshold),
        });
    }
    if options.small < 0.0 {
        return Err(SparseError::InvalidInput {
            reason: format!("small must be >= 0.0, got {}", options.small),
        });
    }
    if options.outer_block_size == 0 {
        return Err(SparseError::InvalidInput {
            reason: "outer_block_size must be > 0".to_string(),
        });
    }
    if options.inner_block_size == 0 {
        return Err(SparseError::InvalidInput {
            reason: "inner_block_size must be > 0".to_string(),
        });
    }
    if options.inner_block_size > options.outer_block_size {
        return Err(SparseError::InvalidInput {
            reason: format!(
                "inner_block_size ({}) must be <= outer_block_size ({})",
                options.inner_block_size, options.outer_block_size
            ),
        });
    }

    // Primary factorization dispatch.
    //
    // Small fronts (< inner_block_size) use TPP, which tries 2x2 pivots first
    // on every column. This accepts more 2x2 pivots than complete pivoting
    // (which only tries 2x2 when the off-diagonal is the global max). For
    // indefinite matrices, 2x2 pivots pair +/- eigenvalues and produce
    // better-conditioned factors.
    // Allocate kernel workspace once for the BLAS-3 paths (reused across
    // all block iterations within factor_inner / two_level_factor).
    let mut kernel_ws = AptpKernelWorkspace::new(m, options.inner_block_size);

    let mut result = if num_fully_summed < options.inner_block_size {
        // Small front: use TPP as primary method
        tpp_factor_rect(a.rb_mut(), num_fully_summed, options)?
    } else if num_fully_summed > options.outer_block_size {
        two_level_factor(a.rb_mut(), num_fully_summed, options, &mut kernel_ws)?
    } else {
        factor_inner(
            a.rb_mut(),
            num_fully_summed,
            num_fully_summed,
            options,
            &mut kernel_ws,
        )?
    };

    // Fallback: TPP on remaining columns
    if result.num_eliminated < num_fully_summed
        && options.failed_pivot_method == FailedPivotMethod::Tpp
    {
        let q = result.num_eliminated;

        // Grow D to accommodate TPP's writes at positions q..num_fully_summed
        result.d.grow(num_fully_summed);

        let additional = tpp_factor(
            a.rb_mut(),
            q,
            num_fully_summed,
            &mut result.perm,
            &mut result.d,
            &mut result.stats,
            &mut result.pivot_log,
            options,
        );

        result.num_eliminated = q + additional;
        result.d.truncate(result.num_eliminated);
        result.delayed_cols = (result.num_eliminated..num_fully_summed)
            .map(|i| result.perm[i])
            .collect();
        result.stats.num_delayed = num_fully_summed - result.num_eliminated;
    }

    Ok(result)
}

/// Factor a dense symmetric matrix using APTP, returning extracted L factor.
///
/// Convenience wrapper around [`aptp_factor_in_place`] that:
/// 1. Copies the input matrix
/// 2. Calls `aptp_factor_in_place` with `num_fully_summed = n`
/// 3. Extracts L from the mutated copy
/// 4. Returns [`AptpFactorization`] with all components as owned types
///
/// Suitable for standalone testing and small matrices. For large frontal
/// matrices in multifrontal factorization, use
/// [`aptp_factor_in_place`] directly to avoid the copy.
pub fn aptp_factor(
    a: MatRef<'_, f64>,
    options: &AptpOptions,
) -> Result<AptpFactorization, SparseError> {
    let n = a.nrows();
    if a.ncols() != n {
        return Err(SparseError::InvalidInput {
            reason: format!("matrix must be square, got {}x{}", n, a.ncols()),
        });
    }

    let mut a_copy = a.to_owned();
    let result = aptp_factor_in_place(a_copy.as_mut(), n, options)?;
    let l = extract_l(a_copy.as_ref(), &result.d, result.num_eliminated);
    let perm = perm_from_forward(result.perm.clone())?;

    Ok(AptpFactorization {
        l,
        d: result.d,
        perm,
        delayed_cols: result.delayed_cols,
        stats: result.stats,
        pivot_log: result.pivot_log,
    })
}

// ---------------------------------------------------------------------------
// Internal functions
// ---------------------------------------------------------------------------

/// Swap symmetric rows/columns i and j in the lower triangle of a dense matrix.
///
/// This performs a simultaneous row-and-column permutation on a symmetric
/// matrix stored in the lower triangle, so that the data at position i
/// moves to position j and vice versa.
fn swap_symmetric(mut a: MatMut<'_, f64>, i: usize, j: usize) {
    let m = a.nrows();
    swap_symmetric_block(a.rb_mut(), i, j, 0, m);
}

/// Block-scoped symmetric swap: permute rows/columns i and j within
/// a[col_start..row_limit, col_start..row_limit].
///
/// Same as `swap_symmetric` but the "rows k < i" loop starts at `col_start` instead of 0,
/// and the "rows k > j" loop uses `row_limit` instead of `a.nrows()`. This limits the
/// swap to the diagonal block being factored, leaving both previously-factored columns
/// (0..col_start) and panel rows (row_limit..m) untouched.
// SPRAL Equivalent: `swap_cols(p, t, BLOCK_SIZE, a, lda, ...)` in `block_ldlt.hxx:68` (BSD-3).
fn swap_symmetric_block(
    mut a: MatMut<'_, f64>,
    i: usize,
    j: usize,
    col_start: usize,
    row_limit: usize,
) {
    if i == j {
        return;
    }
    let (i, j) = if i < j { (i, j) } else { (j, i) };

    // Swap diagonals
    let tmp = a[(i, i)];
    a[(i, i)] = a[(j, j)];
    a[(j, j)] = tmp;

    // Rows k < i: swap lower-triangle entries a[(i,k)] and a[(j,k)]
    // LIMITED to col_start..i (don't touch previously-factored columns)
    for k in col_start..i {
        let tmp = a[(i, k)];
        a[(i, k)] = a[(j, k)];
        a[(j, k)] = tmp;
    }

    // Rows i < k < j: swap a[(k,i)] and a[(j,k)]
    for k in (i + 1)..j {
        let tmp = a[(k, i)];
        a[(k, i)] = a[(j, k)];
        a[(j, k)] = tmp;
    }

    // Rows k > j: swap a[(k,i)] and a[(k,j)] — LIMITED to row_limit
    for k in (j + 1)..row_limit {
        let tmp = a[(k, i)];
        a[(k, i)] = a[(k, j)];
        a[(k, j)] = tmp;
    }

    // Cross element a[(j,i)] stays unchanged
}

/// Rank-1 Schur complement update after eliminating a 1x1 pivot at column `col`.
#[cfg(test)]
#[allow(dead_code)] // Used by complete_pivoting_factor (also #[cfg(test)])
fn update_schur_1x1(mut a: MatMut<'_, f64>, col: usize, d_value: f64) {
    let m = a.nrows();
    let n_trail = m - col - 1;
    if n_trail == 0 {
        return;
    }

    let l_col: Vec<f64> = (0..n_trail).map(|i| a[(col + 1 + i, col)]).collect();

    for j in 0..n_trail {
        let ldlj = l_col[j] * d_value;
        for i in j..n_trail {
            let old = a[(col + 1 + i, col + 1 + j)];
            a[(col + 1 + i, col + 1 + j)] = old - l_col[i] * ldlj;
        }
    }
}

/// Rank-2 Schur complement update after eliminating a 2x2 pivot.
#[cfg(test)]
#[allow(dead_code)] // Used by complete_pivoting_factor (also #[cfg(test)])
fn update_schur_2x2(mut a: MatMut<'_, f64>, col: usize, partner: usize, block: &Block2x2) {
    let m = a.nrows();
    let start = col.max(partner) + 1;
    let n_trail = m - start;
    if n_trail == 0 {
        return;
    }

    let l1: Vec<f64> = (0..n_trail).map(|i| a[(start + i, col)]).collect();
    let l2: Vec<f64> = (0..n_trail).map(|i| a[(start + i, partner)]).collect();

    let d_a = block.a;
    let d_b = block.b;
    let d_c = block.c;

    for j in 0..n_trail {
        let w_j1 = l1[j] * d_a + l2[j] * d_b;
        let w_j2 = l1[j] * d_b + l2[j] * d_c;
        for i in j..n_trail {
            let update = l1[i] * w_j1 + l2[i] * w_j2;
            let old = a[(start + i, start + j)];
            a[(start + i, start + j)] = old - update;
        }
    }
}

/// Extract the L factor from the in-place factorized matrix.
///
/// Uses the D factor to identify 2x2 pivot blocks, where `a[(k+1, k)]`
/// contains the D off-diagonal (not an L entry). For 2x2 pivots at
/// positions (k, k+1), L entries start at row k+2.
fn extract_l(a: MatRef<'_, f64>, d: &MixedDiagonal, num_eliminated: usize) -> Mat<f64> {
    let n = a.nrows();
    let mut l = Mat::zeros(n, n);

    for i in 0..n {
        l[(i, i)] = 1.0;
    }

    let mut col = 0;
    while col < num_eliminated {
        match d.pivot_type(col) {
            PivotType::OneByOne => {
                for i in (col + 1)..n {
                    l[(i, col)] = a[(i, col)];
                }
                col += 1;
            }
            PivotType::TwoByTwo { partner } if partner > col => {
                // a[(col+1, col)] is the D off-diagonal, NOT an L entry.
                // L entries for the 2x2 block start at row col+2.
                for i in (col + 2)..n {
                    l[(i, col)] = a[(i, col)];
                    l[(i, col + 1)] = a[(i, col + 1)];
                }
                col += 2;
            }
            _ => {
                col += 1;
            }
        }
    }

    l
}

// ---------------------------------------------------------------------------
// Two-level APTP: Complete pivoting (Algorithm 4.1)
// ---------------------------------------------------------------------------

/// Factor a small dense symmetric block using complete pivoting.
///
/// Implements Algorithm 4.1 from Duff, Hogg & Lopez (2020): searches
/// the entire remaining submatrix for the entry with maximum magnitude,
/// then uses it as a 1×1 pivot (if on diagonal) or as the off-diagonal
/// of a 2×2 pivot. Provably stable with growth factor bound ≤ 4
/// (equivalent to threshold u=0.25).
///
/// Used at the innermost level of two-level APTP for ib×ib diagonal
/// blocks. Never delays columns (always finds a valid pivot unless
/// the block is numerically singular).
///
/// The matrix is factored in-place (lower triangle). On return:
/// - Columns 0..num_eliminated contain L entries (unit diagonal implicit)
/// - D entries stored in returned MixedDiagonal
/// - The Schur complement is updated in-place in the trailing submatrix
// SPRAL Equivalent: `block_ldlt()` in `spral/src/ssids/cpu/kernels/ldlt_app.cxx` (BSD-3).
#[cfg(test)]
fn complete_pivoting_factor(mut a: MatMut<'_, f64>, small: f64) -> AptpFactorResult {
    let n = a.nrows();
    debug_assert_eq!(
        n,
        a.ncols(),
        "complete_pivoting_factor requires square matrix"
    );

    let mut col_order: Vec<usize> = (0..n).collect();
    let mut d = MixedDiagonal::new(n);
    let mut stats = AptpStatistics::default();
    let mut pivot_log = Vec::with_capacity(n);
    let mut k = 0; // next column to eliminate

    while k < n {
        let remaining = n - k;

        // 1. Find (t, m_idx) = argmax |a[i,j]| over all uneliminated entries (lower triangle)
        let mut max_val = 0.0_f64;
        let mut max_row = k;
        let mut max_col = k;
        for j in k..n {
            for i in j..n {
                let v = a[(i, j)].abs();
                if v > max_val {
                    max_val = v;
                    max_row = i;
                    max_col = j;
                }
            }
        }

        // 2. Singularity check
        if max_val < small {
            // Mark all remaining as zero pivots (delayed)
            for &orig_col in &col_order[k..n] {
                stats.num_delayed += 1;
                pivot_log.push(AptpPivotRecord {
                    col: orig_col,
                    pivot_type: PivotType::Delayed,
                    max_l_entry: 0.0,
                    was_fallback: false,
                });
            }
            break;
        }

        // 3. Decide pivot type
        if max_row == max_col {
            // Maximum is on diagonal → 1×1 pivot
            // Swap max_row to position k
            if max_row != k {
                swap_symmetric(a.rb_mut(), k, max_row);
                col_order.swap(k, max_row);
            }

            let d_kk = a[(k, k)];
            let inv_d = 1.0 / d_kk;

            // Compute L column
            let mut max_l = 0.0_f64;
            for i in (k + 1)..n {
                let l_ik = a[(i, k)] * inv_d;
                a[(i, k)] = l_ik;
                let abs_l = l_ik.abs();
                if abs_l > max_l {
                    max_l = abs_l;
                }
            }

            // Schur complement update
            update_schur_1x1(a.rb_mut(), k, d_kk);

            d.set_1x1(k, d_kk);
            stats.num_1x1 += 1;
            if max_l > stats.max_l_entry {
                stats.max_l_entry = max_l;
            }
            pivot_log.push(AptpPivotRecord {
                col: col_order[k],
                pivot_type: PivotType::OneByOne,
                max_l_entry: max_l,
                was_fallback: false,
            });
            k += 1;
        } else {
            // Maximum is off-diagonal at (max_row, max_col)
            // t = max_row, m = max_col (in paper's notation)
            let t = max_row;
            let m = max_col;

            // Compute Δ = a[m,m] * a[t,t] - a[t,m]^2
            // Need current values (before any swap)
            let a_mm = a[(m, m)];
            let a_tt = a[(t, t)];
            let a_tm = a[(t, m)]; // lower triangle: t > m
            let delta = a_mm * a_tt - a_tm * a_tm;

            if remaining < 2 {
                // Only one column left, must use 1×1 (shouldn't normally reach here
                // since max_row == max_col for single element, but guard anyway)
                let d_kk = a[(k, k)];
                if d_kk.abs() < small {
                    stats.num_delayed += 1;
                    pivot_log.push(AptpPivotRecord {
                        col: col_order[k],
                        pivot_type: PivotType::Delayed,
                        max_l_entry: 0.0,
                        was_fallback: false,
                    });
                    break;
                }
                let inv_d = 1.0 / d_kk;
                for i in (k + 1)..n {
                    a[(i, k)] *= inv_d;
                }
                d.set_1x1(k, d_kk);
                stats.num_1x1 += 1;
                k += 1;
                continue;
            }

            if delta.abs() >= BUNCH_KAUFMAN_ALPHA * a_tm * a_tm {
                // 2×2 pivot using (t, m)
                // Swap m → k and t → k+1
                if m != k {
                    swap_symmetric(a.rb_mut(), k, m);
                    col_order.swap(k, m);
                }
                // After swap: the row that was 't' may have moved
                let new_t = if t == k { m } else { t };
                if new_t != k + 1 {
                    swap_symmetric(a.rb_mut(), k + 1, new_t);
                    col_order.swap(k + 1, new_t);
                }

                let a11 = a[(k, k)];
                let a22 = a[(k + 1, k + 1)];
                let a21 = a[(k + 1, k)];
                let det = a11 * a22 - a21 * a21;
                let inv_det = 1.0 / det;

                let block = Block2x2 {
                    first_col: k,
                    a: a11,
                    b: a21,
                    c: a22,
                };

                // Compute L columns for 2×2 pivot
                let mut max_l = 0.0_f64;
                let rows_start = k + 2;
                for i in rows_start..n {
                    let ai1 = a[(i, k)];
                    let ai2 = a[(i, k + 1)];
                    let l_i1 = (ai1 * a22 - ai2 * a21) * inv_det;
                    let l_i2 = (ai2 * a11 - ai1 * a21) * inv_det;
                    a[(i, k)] = l_i1;
                    a[(i, k + 1)] = l_i2;
                    if l_i1.abs() > max_l {
                        max_l = l_i1.abs();
                    }
                    if l_i2.abs() > max_l {
                        max_l = l_i2.abs();
                    }
                }

                // Schur complement update for 2×2
                update_schur_2x2(a.rb_mut(), k, k + 1, &block);

                d.set_2x2(block);
                stats.num_2x2 += 1;
                if max_l > stats.max_l_entry {
                    stats.max_l_entry = max_l;
                }
                pivot_log.push(AptpPivotRecord {
                    col: col_order[k],
                    pivot_type: PivotType::TwoByTwo {
                        partner: col_order[k + 1],
                    },
                    max_l_entry: max_l,
                    was_fallback: false,
                });
                pivot_log.push(AptpPivotRecord {
                    col: col_order[k + 1],
                    pivot_type: PivotType::TwoByTwo {
                        partner: col_order[k],
                    },
                    max_l_entry: max_l,
                    was_fallback: false,
                });
                k += 2;
            } else {
                // Failed 2×2 determinant condition → use 1×1 on max(|a_mm|, |a_tt|)
                let pivot_pos = if a_mm.abs() >= a_tt.abs() { m } else { t };
                if pivot_pos != k {
                    swap_symmetric(a.rb_mut(), k, pivot_pos);
                    col_order.swap(k, pivot_pos);
                }

                let d_kk = a[(k, k)];
                let inv_d = 1.0 / d_kk;

                let mut max_l = 0.0_f64;
                for i in (k + 1)..n {
                    let l_ik = a[(i, k)] * inv_d;
                    a[(i, k)] = l_ik;
                    let abs_l = l_ik.abs();
                    if abs_l > max_l {
                        max_l = abs_l;
                    }
                }

                update_schur_1x1(a.rb_mut(), k, d_kk);

                d.set_1x1(k, d_kk);
                stats.num_1x1 += 1;
                if max_l > stats.max_l_entry {
                    stats.max_l_entry = max_l;
                }
                pivot_log.push(AptpPivotRecord {
                    col: col_order[k],
                    pivot_type: PivotType::OneByOne,
                    max_l_entry: max_l,
                    was_fallback: true, // fell back from 2×2
                });
                k += 1;
            }
        }
    }

    let num_eliminated = k;

    d.truncate(num_eliminated);

    let delayed_cols: Vec<usize> = (num_eliminated..n).map(|i| col_order[i]).collect();

    AptpFactorResult {
        d,
        perm: col_order,
        num_eliminated,
        delayed_cols,
        stats,
        pivot_log,
    }
}

// ---------------------------------------------------------------------------
// Two-level APTP: BLAS-3 building blocks
// ---------------------------------------------------------------------------

/// Factor an ib×ib diagonal block using complete pivoting (Algorithm 4.1).
///
/// Performs complete pivoting on the diagonal block `a[col_start..col_start+block_size,
/// col_start..col_start+block_size]`, but symmetric swaps affect ALL m rows of the
/// matrix (keeping row ordering consistent for the panel below).
///
/// L entries are computed ONLY for rows within the block (col_start..col_start+block_size).
/// Schur complement updates are ONLY within the block. The panel rows below are NOT
/// modified except by symmetric swaps.
///
/// Complete pivoting guarantees |L_ij| ≤ 4 (growth factor for u=0.25).
///
/// # Returns
/// (block_d, local_perm, nelim, stats, pivot_log) where:
/// - `block_d`: MixedDiagonal of dimension block_size with D entries
/// - `local_perm`: permutation applied within positions col_start..col_start+block_size
///   (values are offsets from col_start)
/// - `nelim`: number of successfully eliminated columns (may be < block_size if singular)
/// - `stats`: pivot statistics for this block
/// - `pivot_log`: per-pivot diagnostic records
// SPRAL Equivalent: `block_ldlt()` in `spral/src/ssids/cpu/kernels/block_ldlt.hxx` (BSD-3).
fn factor_block_diagonal(
    mut a: MatMut<'_, f64>,
    col_start: usize,
    block_size: usize,
    small: f64,
    row_limit: usize,
) -> (
    MixedDiagonal,
    Vec<usize>,
    usize,
    AptpStatistics,
    Vec<AptpPivotRecord>,
) {
    let block_end = col_start + block_size;

    let mut local_perm: Vec<usize> = (0..block_size).collect();
    let mut d = MixedDiagonal::new(block_size);
    let mut stats = AptpStatistics::default();
    let mut pivot_log = Vec::with_capacity(block_size);
    let mut k = 0; // offset within block (next column to eliminate)

    while k < block_size {
        let cur = col_start + k; // absolute position
        let search_end = block_end;
        let remaining = block_size - k;

        // 1. Find max |a[i,j]| in remaining diagonal sub-block [cur..search_end, cur..search_end]
        let mut max_val = 0.0_f64;
        let mut max_row = cur;
        let mut max_col = cur;
        for j in cur..search_end {
            for i in j..search_end {
                let v = a[(i, j)].abs();
                if v > max_val {
                    max_val = v;
                    max_row = i;
                    max_col = j;
                }
            }
        }

        // 2. Singularity check
        if max_val < small {
            // All remaining entries in block are near-zero
            stats.num_delayed += remaining;
            for &perm_val in &local_perm[k..block_size] {
                pivot_log.push(AptpPivotRecord {
                    col: perm_val,
                    pivot_type: PivotType::Delayed,
                    max_l_entry: 0.0,
                    was_fallback: false,
                });
            }
            break;
        }

        // 3. Decide pivot type
        if max_row == max_col {
            // Diagonal maximum → 1×1 pivot
            if max_row != cur {
                swap_symmetric_block(a.rb_mut(), cur, max_row, col_start, row_limit);
                local_perm.swap(k, max_row - col_start);
            }

            let d_kk = a[(cur, cur)];
            let inv_d = 1.0 / d_kk;

            // Compute L column ONLY within block
            let mut max_l = 0.0_f64;
            for i in (cur + 1)..block_end {
                let l_ik = a[(i, cur)] * inv_d;
                a[(i, cur)] = l_ik;
                let abs_l = l_ik.abs();
                if abs_l > max_l {
                    max_l = abs_l;
                }
            }

            // Schur complement update ONLY within block
            for j in (cur + 1)..block_end {
                let l_j = a[(j, cur)];
                let ldl_j = l_j * d_kk;
                for i in j..block_end {
                    a[(i, j)] -= a[(i, cur)] * ldl_j;
                }
            }

            d.set_1x1(k, d_kk);
            stats.num_1x1 += 1;
            if max_l > stats.max_l_entry {
                stats.max_l_entry = max_l;
            }
            pivot_log.push(AptpPivotRecord {
                col: local_perm[k],
                pivot_type: PivotType::OneByOne,
                max_l_entry: max_l,
                was_fallback: false,
            });
            k += 1;
        } else {
            // Off-diagonal maximum → try 2×2 pivot
            let t = max_row; // absolute positions
            let m_idx = max_col;

            let a_mm = a[(m_idx, m_idx)];
            let a_tt = a[(t, t)];
            let a_tm = a[(t, m_idx)]; // lower triangle: t > m_idx
            let delta = a_mm * a_tt - a_tm * a_tm;

            if remaining < 2 {
                // Only one column left, use 1×1 on max diagonal
                let pivot_pos = if a_mm.abs() >= a_tt.abs() { m_idx } else { t };
                if pivot_pos != cur {
                    swap_symmetric_block(a.rb_mut(), cur, pivot_pos, col_start, row_limit);
                    local_perm.swap(k, pivot_pos - col_start);
                }
                let d_kk = a[(cur, cur)];
                if d_kk.abs() < small {
                    stats.num_delayed += 1;
                    pivot_log.push(AptpPivotRecord {
                        col: local_perm[k],
                        pivot_type: PivotType::Delayed,
                        max_l_entry: 0.0,
                        was_fallback: false,
                    });
                    break;
                }
                let inv_d = 1.0 / d_kk;
                for i in (cur + 1)..block_end {
                    a[(i, cur)] *= inv_d;
                }
                d.set_1x1(k, d_kk);
                stats.num_1x1 += 1;
                k += 1;
                continue;
            }

            if delta.abs() >= BUNCH_KAUFMAN_ALPHA * a_tm * a_tm {
                // 2×2 pivot: swap m_idx → cur, t → cur+1
                if m_idx != cur {
                    swap_symmetric_block(a.rb_mut(), cur, m_idx, col_start, row_limit);
                    local_perm.swap(k, m_idx - col_start);
                }
                let new_t = if t == cur { m_idx } else { t };
                if new_t != cur + 1 {
                    swap_symmetric_block(a.rb_mut(), cur + 1, new_t, col_start, row_limit);
                    local_perm.swap(k + 1, new_t - col_start);
                }

                let a11 = a[(cur, cur)];
                let a22 = a[(cur + 1, cur + 1)];
                let a21 = a[(cur + 1, cur)];
                let det = a11 * a22 - a21 * a21;
                let inv_det = 1.0 / det;

                let block = Block2x2 {
                    first_col: k,
                    a: a11,
                    b: a21,
                    c: a22,
                };

                // Compute L columns ONLY within block
                let mut max_l = 0.0_f64;
                let rows_start = cur + 2;
                for i in rows_start..block_end {
                    let ai1 = a[(i, cur)];
                    let ai2 = a[(i, cur + 1)];
                    let l_i1 = (ai1 * a22 - ai2 * a21) * inv_det;
                    let l_i2 = (ai2 * a11 - ai1 * a21) * inv_det;
                    a[(i, cur)] = l_i1;
                    a[(i, cur + 1)] = l_i2;
                    if l_i1.abs() > max_l {
                        max_l = l_i1.abs();
                    }
                    if l_i2.abs() > max_l {
                        max_l = l_i2.abs();
                    }
                }

                // Schur complement update ONLY within block (rank-2)
                for j in rows_start..block_end {
                    let l1j = a[(j, cur)];
                    let l2j = a[(j, cur + 1)];
                    let w_j1 = l1j * a11 + l2j * a21;
                    let w_j2 = l1j * a21 + l2j * a22;
                    for i in j..block_end {
                        let l1i = a[(i, cur)];
                        let l2i = a[(i, cur + 1)];
                        a[(i, j)] -= l1i * w_j1 + l2i * w_j2;
                    }
                }

                d.set_2x2(block);
                stats.num_2x2 += 1;
                if max_l > stats.max_l_entry {
                    stats.max_l_entry = max_l;
                }
                pivot_log.push(AptpPivotRecord {
                    col: local_perm[k],
                    pivot_type: PivotType::TwoByTwo {
                        partner: local_perm[k + 1],
                    },
                    max_l_entry: max_l,
                    was_fallback: false,
                });
                pivot_log.push(AptpPivotRecord {
                    col: local_perm[k + 1],
                    pivot_type: PivotType::TwoByTwo {
                        partner: local_perm[k],
                    },
                    max_l_entry: max_l,
                    was_fallback: false,
                });
                k += 2;
            } else {
                // Failed Δ → 1×1 on max diagonal
                let pivot_pos = if a_mm.abs() >= a_tt.abs() { m_idx } else { t };
                if pivot_pos != cur {
                    swap_symmetric_block(a.rb_mut(), cur, pivot_pos, col_start, row_limit);
                    local_perm.swap(k, pivot_pos - col_start);
                }

                let d_kk = a[(cur, cur)];
                let inv_d = 1.0 / d_kk;

                let mut max_l = 0.0_f64;
                for i in (cur + 1)..block_end {
                    let l_ik = a[(i, cur)] * inv_d;
                    a[(i, cur)] = l_ik;
                    let abs_l = l_ik.abs();
                    if abs_l > max_l {
                        max_l = abs_l;
                    }
                }

                for j in (cur + 1)..block_end {
                    let l_j = a[(j, cur)];
                    let ldl_j = l_j * d_kk;
                    for i in j..block_end {
                        a[(i, j)] -= a[(i, cur)] * ldl_j;
                    }
                }

                d.set_1x1(k, d_kk);
                stats.num_1x1 += 1;
                if max_l > stats.max_l_entry {
                    stats.max_l_entry = max_l;
                }
                pivot_log.push(AptpPivotRecord {
                    col: local_perm[k],
                    pivot_type: PivotType::OneByOne,
                    max_l_entry: max_l,
                    was_fallback: true,
                });
                k += 1;
            }
        }
    }

    let nelim = k;
    (d, local_perm, nelim, stats, pivot_log)
}

/// Adjust effective_nelim to avoid splitting a 2×2 pivot across block boundaries.
///
/// If the last accepted pivot at position `effective_nelim - 1` is the first half
/// of a 2×2 pair whose partner is beyond `effective_nelim`, decrement by 1.
// SPRAL Equivalent: `Column::adjust()` in `spral/src/ssids/cpu/kernels/ldlt_app.cxx:112-127` (BSD-3).
fn adjust_for_2x2_boundary(effective_nelim: usize, d: &MixedDiagonal) -> usize {
    if effective_nelim == 0 {
        return 0;
    }
    let last = effective_nelim - 1;
    match d.pivot_type(last) {
        PivotType::TwoByTwo { partner } if partner > last => {
            // Last accepted is the first column of a 2×2 whose partner is beyond nelim
            effective_nelim - 1
        }
        _ => effective_nelim,
    }
}

/// Per-block backup for the two-level APTP algorithm.
///
/// Stores a copy of matrix entries for one outer block column,
/// enabling restore when the a posteriori check reduces nelim.
///
// SPRAL Equivalent: `CopyBackup<T>` in `spral/src/ssids/cpu/kernels/ldlt_app.cxx` (BSD-3).
/// Used by the BLAS-3 `factor_inner` to save the block column before
/// factoring, so it can be restored if the Apply step's threshold check
/// reduces nelim.
struct BlockBackup<'a> {
    data: MatMut<'a, f64>,
}

impl<'a> BlockBackup<'a> {
    /// Create a backup of the block column starting at `col_start` with `block_cols` columns.
    /// Backs up `a[col_start.., col_start..col_start+block_cols]` into the provided buffer.
    fn create(
        a: MatRef<'_, f64>,
        col_start: usize,
        block_cols: usize,
        m: usize,
        buf: &'a mut Mat<f64>,
    ) -> Self {
        let rows = m - col_start;
        let mut data = buf.as_mut().submatrix_mut(0, 0, rows, block_cols);
        for j in 0..block_cols {
            for i in 0..rows {
                data[(i, j)] = a[(col_start + i, col_start + j)];
            }
        }
        BlockBackup { data }
    }

    /// Restore the failed portion of the block (columns nelim..block_cols) from backup.
    /// Columns 0..nelim are left untouched (successfully factored).
    fn restore_failed(
        &self,
        mut a: MatMut<'_, f64>,
        col_start: usize,
        nelim: usize,
        block_cols: usize,
        m: usize,
    ) {
        let rows = m - col_start;
        for j in nelim..block_cols {
            for i in 0..rows {
                a[(col_start + i, col_start + j)] = self.data[(i, j)];
            }
        }
    }

    /// Restore failed columns of the diagonal block from pre-factor backup,
    /// applying the block permutation to read from the correct backup positions.
    ///
    /// The backup was taken BEFORE factor_block_diagonal permuted columns.
    /// factor_block_diagonal applied symmetric swaps described by `block_perm`,
    /// so to restore failed column j (in post-perm ordering), we must read
    /// from backup position perm[j] (pre-perm ordering).
    ///
    /// Restores two regions:
    /// 1. Diagonal block: a[k+e..k+bs, k+e..k+bs] — symmetric with perm
    /// 2. Panel below: a[k+bs..m, k+e..k+bs] — column perm only
    // SPRAL Equivalent: `CopyBackup::restore_part_with_sym_perm` (`ldlt_app.cxx:562-574`) (BSD-3).
    fn restore_diagonal_with_perm(
        &self,
        mut a: MatMut<'_, f64>,
        col_start: usize,
        nelim: usize,
        block_cols: usize,
        m: usize,
        block_perm: &[usize],
    ) {
        // Region 1: Diagonal block — restore a[k+e..k+bs, k+e..k+bs]
        // with symmetric permutation from backup.
        // backup[r, c] stored with r >= c (lower triangle).
        // In backup coordinates: row i, col j.
        // a[(col_start+i, col_start+j)] = backup[(max(r,c), min(r,c))]
        for j in nelim..block_cols {
            let c = block_perm[j]; // pre-perm column
            for i in nelim..block_cols {
                let r = block_perm[i]; // pre-perm row
                // Read from lower triangle of backup (row >= col)
                let val = if r >= c {
                    self.data[(r, c)]
                } else {
                    self.data[(c, r)]
                };
                a[(col_start + i, col_start + j)] = val;
            }
        }

        // Region 2: Panel below diagonal block — restore a[k+bs..m, k+e..k+bs]
        // Only column permutation applies (panel rows were permuted by
        // apply_cperm step, but we need original values at permuted column).
        // a[(col_start+i, col_start+j)] = backup[(i, c)]
        for j in nelim..block_cols {
            let c = block_perm[j]; // pre-perm column
            for i in block_cols..(m - col_start) {
                a[(col_start + i, col_start + j)] = self.data[(i, c)];
            }
        }
    }
}

/// Apply factored L11/D11 to the panel below the diagonal block (TRSM),
/// then perform a posteriori threshold check on all L21 entries.
///
/// Given a factored diagonal block at `a[col_start..col_start+block_nelim, ...]`
/// with L11 (unit lower triangular) and D11, computes:
///   L21 = A21 * (L11 * D11)^{-T}
/// and checks that all |L21[i,j]| < 1/threshold.
///
/// Returns the effective nelim (<= block_nelim): the number of columns
/// whose L entries all satisfy the threshold bound.
///
/// # Algorithm
/// 1. Solve: X = A21 * L11^{-T} via triangular solve (TRSM)
/// 2. Scale: L21[i,j] = X[i,j] / D[j,j] for 1×1 pivots,
///    or L21[i,k:k+1] via 2×2 inversion for 2×2 pivots
/// 3. Scan L21 column-by-column; find first column where any entry exceeds 1/threshold
#[allow(clippy::too_many_arguments)]
fn apply_and_check(
    mut a: MatMut<'_, f64>,
    col_start: usize,
    block_nelim: usize,
    block_cols: usize,
    m: usize,
    d: &MixedDiagonal,
    threshold: f64,
    par: Par,
    l11_buf: &mut Mat<f64>,
) -> usize {
    if block_nelim == 0 {
        return 0;
    }

    let panel_rows = m - col_start - block_cols;
    if panel_rows == 0 {
        return block_nelim;
    }

    // Step 1: TRSM — solve panel * L11^T = A21 for panel (= L21)
    // L11 is unit lower triangular in a[col_start..+block_nelim, col_start..+block_nelim]
    // panel is a[panel_start..m, col_start..col_start+block_nelim]
    //
    // Transposing: L11 * panel^T = A21^T, i.e. unit lower triangular solve on panel^T.
    // Copy L11 to a temporary to avoid aliasing (L11 and panel overlap in `a`).

    let panel_start = col_start + block_cols;

    // Copy L11 into workspace buffer to avoid aliasing
    {
        let src = a
            .rb()
            .submatrix(col_start, col_start, block_nelim, block_nelim);
        let mut dst = l11_buf
            .as_mut()
            .submatrix_mut(0, 0, block_nelim, block_nelim);
        dst.copy_from(src);
    }
    let l11_ref = l11_buf.as_ref().submatrix(0, 0, block_nelim, block_nelim);
    let panel = a
        .rb_mut()
        .submatrix_mut(panel_start, col_start, panel_rows, block_nelim);
    triangular_solve::solve_unit_lower_triangular_in_place(l11_ref, panel.transpose_mut(), par);

    // Step 2: Scale by D^{-1}
    // For 1×1 pivot at column j: L21[:, j] /= D[j]
    // For 2×2 pivot at columns (j, j+1): solve the 2×2 system
    let mut col = 0;
    while col < block_nelim {
        match d.pivot_type(col) {
            PivotType::OneByOne => {
                let d_val = d.diagonal_1x1(col);
                let inv_d = 1.0 / d_val;
                for i in 0..panel_rows {
                    a[(panel_start + i, col_start + col)] *= inv_d;
                }
                col += 1;
            }
            PivotType::TwoByTwo { partner } if partner > col => {
                let block = d.diagonal_2x2(col);
                let det = block.a * block.c - block.b * block.b;
                let inv_det = 1.0 / det;
                for i in 0..panel_rows {
                    let x1 = a[(panel_start + i, col_start + col)];
                    let x2 = a[(panel_start + i, col_start + col + 1)];
                    a[(panel_start + i, col_start + col)] = (x1 * block.c - x2 * block.b) * inv_det;
                    a[(panel_start + i, col_start + col + 1)] =
                        (x2 * block.a - x1 * block.b) * inv_det;
                }
                col += 2;
            }
            _ => {
                col += 1;
            }
        }
    }

    // Step 3: Threshold scan — find first failing column
    let stability_bound = 1.0 / threshold;
    let mut effective_nelim = block_nelim;

    let mut scan_col = 0;
    while scan_col < block_nelim {
        let pivot_width = match d.pivot_type(scan_col) {
            PivotType::TwoByTwo { partner } if partner > scan_col => 2,
            _ => 1,
        };

        let mut col_ok = true;
        for c in scan_col..scan_col + pivot_width {
            if c >= block_nelim {
                break;
            }
            for i in 0..panel_rows {
                if a[(panel_start + i, col_start + c)].abs() >= stability_bound {
                    col_ok = false;
                    break;
                }
            }
            if !col_ok {
                break;
            }
        }

        if !col_ok {
            effective_nelim = scan_col;
            break;
        }
        scan_col += pivot_width;
    }

    effective_nelim
}

/// Rank-nelim Schur complement update on the trailing submatrix via GEMM.
///
/// Computes: A[trailing, trailing] -= L21 * D11 * L21^T
/// where L21 is the panel at a[panel_start..m, col_start..col_start+nelim]
/// and D11 is the block diagonal from the Factor phase.
///
/// Uses explicit W = L21 * D11 workspace, then A -= W * L21^T (GEMM).
#[allow(clippy::too_many_arguments)]
fn update_trailing(
    mut a: MatMut<'_, f64>,
    col_start: usize,
    nelim: usize,
    block_cols: usize,
    m: usize,
    num_fully_summed: usize,
    d: &MixedDiagonal,
    par: Par,
    ld_buf: &mut Mat<f64>,
    copy_buf: &mut Mat<f64>,
) {
    if nelim == 0 {
        return;
    }

    let trailing_start = col_start + block_cols;
    let trailing_size = m - trailing_start;
    if trailing_size == 0 {
        return;
    }

    // p = num_fully_summed boundary: rows [trailing_start..p] are FS, [p..m] are NFS.
    let p = num_fully_summed;

    // Compute W = L21 * D11 using workspace buffer (trailing_size × nelim subview).
    // We compute W for ALL trailing rows (FS + NFS) since the FS×FS region (region 1)
    // and the NFS×FS cross-term (region 2) both need it.
    let mut w = ld_buf.as_mut().submatrix_mut(0, 0, trailing_size, nelim);
    let mut col = 0;
    while col < nelim {
        match d.pivot_type(col) {
            PivotType::OneByOne => {
                let d_val = d.diagonal_1x1(col);
                for i in 0..trailing_size {
                    w[(i, col)] = a[(trailing_start + i, col_start + col)] * d_val;
                }
                col += 1;
            }
            PivotType::TwoByTwo { partner } if partner > col => {
                let block = d.diagonal_2x2(col);
                for i in 0..trailing_size {
                    let l1 = a[(trailing_start + i, col_start + col)];
                    let l2 = a[(trailing_start + i, col_start + col + 1)];
                    w[(i, col)] = l1 * block.a + l2 * block.b;
                    w[(i, col + 1)] = l1 * block.b + l2 * block.c;
                }
                col += 2;
            }
            _ => {
                col += 1;
            }
        }
    }

    // Copy L21 into workspace buffer to avoid borrow conflict (L21 and A22 overlap in `a`)
    {
        let src = a
            .rb()
            .submatrix(trailing_start, col_start, trailing_size, nelim);
        let mut dst = copy_buf.as_mut().submatrix_mut(0, 0, trailing_size, nelim);
        dst.copy_from(src);
    }

    // Region 1 (FS×FS): Lower-triangular GEMM on A[ts..p, ts..p]
    let fs_size = p.saturating_sub(trailing_start);
    if fs_size > 0 {
        let w_fs = ld_buf.as_ref().submatrix(0, 0, fs_size, nelim);
        let l21_fs = copy_buf.as_ref().submatrix(0, 0, fs_size, nelim);
        let mut a_fs = a
            .rb_mut()
            .submatrix_mut(trailing_start, trailing_start, fs_size, fs_size);

        tri_matmul::matmul_with_conj(
            a_fs.rb_mut(),
            BlockStructure::TriangularLower,
            Accum::Add,
            w_fs,
            BlockStructure::Rectangular,
            Conj::No,
            l21_fs.transpose(),
            BlockStructure::Rectangular,
            Conj::No,
            -1.0,
            par,
        );
    }

    // Region 2 (NFS×FS cross-term): Rectangular GEMM on A[p..m, ts..p]
    let nfs_size = m - p;
    if nfs_size > 0 && fs_size > 0 {
        let w_nfs = ld_buf.as_ref().submatrix(fs_size, 0, nfs_size, nelim);
        let l21_fs = copy_buf.as_ref().submatrix(0, 0, fs_size, nelim);
        let mut a_cross = a.submatrix_mut(p, trailing_start, nfs_size, fs_size);

        faer::linalg::matmul::matmul_with_conj(
            a_cross.rb_mut(),
            Accum::Add,
            w_nfs,
            Conj::No,
            l21_fs.transpose(),
            Conj::No,
            -1.0,
            par,
        );
    }

    // Region 3 (NFS×NFS): SKIPPED — deferred to compute_contribution_gemm
}

/// Compute W = L * D for a set of rows, where L and D come from the factored block.
///
/// For 1×1 pivots: W[i, col] = L[i, col] * D[col]
/// For 2×2 pivots: W[i, col] = L[i,col]*D[col,col] + L[i,col+1]*D[col+1,col]
///                 W[i, col+1] = L[i,col]*D[col,col+1] + L[i,col+1]*D[col+1,col+1]
///
// SPRAL Equivalent: `calcLD<OP_N>` in `spral/src/ssids/cpu/kernels/calc_ld.hxx:41+` (BSD-3).
/// Writes into `dst[0..nrows, 0..nelim]`. The destination is overwritten, not zeroed first.
fn compute_ld_into(l: MatRef<'_, f64>, d: &MixedDiagonal, nelim: usize, mut dst: MatMut<'_, f64>) {
    let nrows = l.nrows();
    let mut col = 0;
    while col < nelim {
        match d.pivot_type(col) {
            PivotType::OneByOne => {
                let d_val = d.diagonal_1x1(col);
                for i in 0..nrows {
                    dst[(i, col)] = l[(i, col)] * d_val;
                }
                col += 1;
            }
            PivotType::TwoByTwo { partner } if partner > col => {
                let block = d.diagonal_2x2(col);
                for i in 0..nrows {
                    let l1 = l[(i, col)];
                    let l2 = l[(i, col + 1)];
                    dst[(i, col)] = l1 * block.a + l2 * block.b;
                    dst[(i, col + 1)] = l1 * block.b + l2 * block.c;
                }
                col += 2;
            }
            _ => {
                col += 1;
            }
        }
    }
}

/// Compute the NFS×NFS Schur complement via a single deferred GEMM.
///
/// Called after the BLAS-3 blocking loop (which skips the NFS×NFS region).
/// Copies the assembled NFS×NFS values from `frontal_data[p..m, p..m]` into
/// `contrib_buffer`, then applies the rank-`ne` symmetric update in-place:
///
/// ```text
/// contrib_buffer[0..nfs, 0..nfs] = assembled[NFS×NFS] - L21_NFS * D * L21_NFS^T
/// ```
///
/// where `L21_NFS = frontal_data[p..m, 0..ne]` and `D` is the accumulated
/// diagonal from the blocking loop. The output is the lower triangle of the
/// Schur complement.
///
/// # Guard conditions
///
/// - `nfs == 0`: no contribution (root or fully eliminated), returns immediately
/// - `ne == 0`: no rank update (copy only — all columns were delayed)
///
// SPRAL Equivalent: `host_gemm` writing to `node.contrib` in `factor.hxx:92-103` (BSD-3).
/// # References
///
/// - Duff, Hogg & Lopez (2020), Section 3: deferred Schur complement
/// - Liu (1992), Section 4: Schur complement in multifrontal method
#[allow(clippy::too_many_arguments)]
pub(crate) fn compute_contribution_gemm(
    frontal_data: &Mat<f64>,
    num_fully_summed: usize,
    num_eliminated: usize,
    m: usize,
    d: &MixedDiagonal,
    contrib_buffer: &mut Mat<f64>,
    ld_workspace: &mut Mat<f64>,
    par: Par,
) {
    let p = num_fully_summed;
    let ne = num_eliminated;
    let nfs = m - p;

    if nfs == 0 {
        return; // No contribution block (root or fully eliminated)
    }

    // Step 1: Copy assembled NFS×NFS from frontal_data[p..m, p..m] into
    //         contrib_buffer[0..nfs, 0..nfs] (lower triangle only).
    //
    // This copy is unavoidable: assembly scatters into frontal_data, but the
    // GEMM output goes to contrib_buffer. A future optimization could scatter
    // NFS entries directly into the contribution buffer during assembly,
    // eliminating this copy at the cost of a larger architectural change.
    for j in 0..nfs {
        let col_len = nfs - j;
        let src = &frontal_data.col_as_slice(p + j)[p + j..m];
        contrib_buffer.col_as_slice_mut(j)[j..j + col_len].copy_from_slice(src);
    }

    if ne == 0 {
        return; // No rank update — all columns were delayed, copy only
    }

    // Step 2: Compute W = L21_NFS * D (nfs × ne)
    // L21_NFS = frontal_data[p..m, 0..ne]
    let l21_nfs = frontal_data.as_ref().submatrix(p, 0, nfs, ne);

    // Use caller-provided workspace to avoid per-supernode allocation.
    // Resize if needed (rare: delayed cascades may exceed initial estimate).
    if ld_workspace.nrows() < nfs || ld_workspace.ncols() < ne {
        *ld_workspace = Mat::zeros(nfs.max(ld_workspace.nrows()), ne.max(ld_workspace.ncols()));
    }
    let mut w = ld_workspace.as_mut().submatrix_mut(0, 0, nfs, ne);
    w.fill(0.0);
    compute_ld_into(l21_nfs, d, ne, w.rb_mut());

    // Step 3: Symmetric rank-ne update: contrib -= W * L21_NFS^T (lower triangle)
    tri_matmul::matmul_with_conj(
        contrib_buffer.as_mut().submatrix_mut(0, 0, nfs, nfs),
        BlockStructure::TriangularLower,
        Accum::Add,
        w.as_ref(),
        BlockStructure::Rectangular,
        Conj::No,
        l21_nfs.transpose(),
        BlockStructure::Rectangular,
        Conj::No,
        -1.0,
        par,
    );
}

/// Apply Schur complement updates from passed columns to failed and trailing regions.
///
/// After factoring `nelim` out of `block_cols` columns, three update regions exist:
///
/// 1. **Failed×failed** (diagonal): A[k+e..k+bs, k+e..k+bs] -= W_blk * L_blk^T
/// 2. **Trailing×failed** (cross-term): A[ts..m, k+e..k+bs] -= W_panel * L_blk^T
/// 3. **Trailing×trailing**: handled separately by `update_trailing`
///
/// where:
/// - L_blk = a[k+e..k+bs, k..k+e] (within-block L for failed rows)
/// - L_panel = a[ts..m, k..k+e] (panel L below diagonal block)
/// - W = L * D (LD product)
/// - ts = k + bs (trailing start)
// SPRAL Equivalent: `Block::update` with rfrom/cfrom skip (`ldlt_app.cxx:1082-1153`) (BSD-3).
#[allow(clippy::too_many_arguments)]
fn update_cross_terms(
    mut a: MatMut<'_, f64>,
    col_start: usize,
    nelim: usize,
    block_cols: usize,
    m: usize,
    d: &MixedDiagonal,
    ld_buf: &mut Mat<f64>,
    copy_buf: &mut Mat<f64>,
) {
    if nelim == 0 || nelim >= block_cols {
        return; // No failed columns → no cross-term updates
    }

    let n_failed = block_cols - nelim;
    let trailing_start = col_start + block_cols;
    let trailing_size = m - trailing_start;

    // L_blk: the L entries for failed rows within the diagonal block
    // Copy to workspace to avoid aliasing: a[k+e..k+bs, k..k+e]
    {
        let src = a
            .rb()
            .submatrix(col_start + nelim, col_start, n_failed, nelim);
        let mut dst = copy_buf.as_mut().submatrix_mut(0, 0, n_failed, nelim);
        dst.copy_from(src);
    }

    // W_blk = L_blk * D (into ld_buf)
    {
        let l_blk = copy_buf.as_ref().submatrix(0, 0, n_failed, nelim);
        compute_ld_into(
            l_blk,
            d,
            nelim,
            ld_buf.as_mut().submatrix_mut(0, 0, n_failed, nelim),
        );
    }

    // Region 1: Failed×failed diagonal update
    // A[k+e..k+bs, k+e..k+bs] -= W_blk * L_blk^T (lower triangle only)
    {
        let l_blk = copy_buf.as_ref().submatrix(0, 0, n_failed, nelim);
        let w_blk = ld_buf.as_ref().submatrix(0, 0, n_failed, nelim);
        for j in 0..n_failed {
            for i in j..n_failed {
                let mut sum = 0.0;
                for c in 0..nelim {
                    sum += w_blk[(i, c)] * l_blk[(j, c)];
                }
                a[(col_start + nelim + i, col_start + nelim + j)] -= sum;
            }
        }
    }

    // Region 2: Trailing×failed cross-term update
    // A[ts..m, k+e..k+bs] -= W_panel * L_blk^T
    if trailing_size > 0 {
        // Copy L_panel into workspace (offset past L_blk already in copy_buf)
        {
            let src = a
                .rb()
                .submatrix(trailing_start, col_start, trailing_size, nelim);
            let mut dst = copy_buf
                .as_mut()
                .submatrix_mut(n_failed, 0, trailing_size, nelim);
            dst.copy_from(src);
        }

        // Now both l_blk and l_panel are in copy_buf at non-overlapping offsets.
        // Re-borrow the full copy_buf immutably to access both regions.
        let l_blk = copy_buf.as_ref().submatrix(0, 0, n_failed, nelim);
        let l_panel = copy_buf
            .as_ref()
            .submatrix(n_failed, 0, trailing_size, nelim);

        // W_panel = L_panel * D (into ld_buf offset past W_blk)
        compute_ld_into(
            l_panel,
            d,
            nelim,
            ld_buf
                .as_mut()
                .submatrix_mut(n_failed, 0, trailing_size, nelim),
        );
        let w_panel = ld_buf.as_ref().submatrix(n_failed, 0, trailing_size, nelim);

        for j in 0..n_failed {
            for i in 0..trailing_size {
                let mut sum = 0.0;
                for c in 0..nelim {
                    sum += w_panel[(i, c)] * l_blk[(j, c)];
                }
                a[(trailing_start + i, col_start + nelim + j)] -= sum;
            }
        }
    }
}

/// Factor an nb-sized block using BLAS-3 Factor/Apply/Update loop.
///
/// # Lower-Triangle Convention
///
/// This function operates exclusively on the **lower triangle** of the dense
/// frontal matrix. All reads (pivot search, L extraction) and writes (Schur
/// updates via `BlockStructure::TriangularLower`, `swap_symmetric`) touch only
/// entries where `row >= col`. The upper triangle may contain stale values
/// after column swaps and Schur updates — this is intentional and safe because
/// no code path reads upper-triangle entries. This convention is consistent
/// across `factor_block_diagonal`, `apply_and_check`, `update_trailing`,
/// `update_cross_terms`, and `swap_symmetric_block`.
///
/// This is the middle level of the two-level hierarchy. Processes `num_fully_summed`
/// columns of the block `a[0..m, 0..m]` using ib-sized sub-blocks with the
/// three-phase BLAS-3 pattern (factor / apply+check / update):
///
/// 1. **Backup**: Save `a[k..m, k..k+block_size]` before factoring
/// 2. **Factor**: `factor_block_diagonal` on the ib×ib diagonal block (complete pivoting)
/// 3. **Permute panel**: apply block_perm to panel columns
/// 4. **Zero D off-diagonals**: for TRSM
/// 5. **Row perm propagation**: apply block_perm to columns 0..k (always, not just on success)
/// 6. **Update col_order**: by block_perm (always, not just on success)
/// 7. **Apply+Check**: TRSM on panel + threshold check → effective_nelim
/// 8. **Adjust**: avoid splitting 2×2 across boundary
/// 9. **Partial restore** (on failure): restore failed columns with permuted backup
/// 10. **Schur updates**: trailing×trailing + cross-term updates for failed columns
/// 11. **Delay** failed columns: swap to end_pos
/// 12. **Advance**: k += nelim (may be < block_size)
///
/// Key difference from the old implementation: on threshold failure we DO NOT
/// fully restore and retry. Instead we keep the passed columns, partially
/// restore only the failed columns (using permuted backup), apply Schur updates
/// from passed to failed+trailing, and advance.
///
/// # Arguments
/// - `a`: Dense frontal matrix block (m × m), modified in place
/// - `num_fully_summed`: Number of columns eligible for elimination
/// - `options`: APTP configuration (inner_block_size determines ib)
///
/// # References
/// - SPRAL: `run_elim_pivoted_notasks` in `ldlt_app.cxx:1585-1713`
/// - Duff, Hogg & Lopez (2020), Algorithm 3.1
fn factor_inner(
    mut a: MatMut<'_, f64>,
    num_fully_summed: usize,
    nfs_boundary: usize,
    options: &AptpOptions,
    kernel_ws: &mut AptpKernelWorkspace,
) -> Result<AptpFactorResult, SparseError> {
    let m = a.nrows();
    let ib = options.inner_block_size;
    let small = options.small;
    let threshold = options.threshold;
    let p = num_fully_summed;

    let mut col_order: Vec<usize> = (0..m).collect();
    let mut d = MixedDiagonal::new(p);
    let mut stats = AptpStatistics::default();
    let mut pivot_log = Vec::with_capacity(p);
    let mut k = 0;
    let mut end_pos = p;

    // Pre-allocated buffers reused across block iterations to avoid
    // per-block allocations in the hot loop.
    let mut panel_perm_buf = vec![0.0f64; ib];
    let mut row_perm_buf = vec![0.0f64; ib];
    let mut col_order_buf = vec![0usize; ib];

    // BLAS-3 Factor/Apply/Update loop with ib-sized inner blocks.
    //
    // BLAS-3 Factor/Apply/Update architecture:
    //   1. Backup (pre-factor)
    //   2. Factor diagonal block (complete pivoting, block-scoped swaps)
    //   3. Permute panel columns by block_perm
    //   4. Zero D off-diagonals for TRSM
    //   5. Row perm propagation to columns 0..k
    //   6. Update col_order by block_perm
    //   7. Apply+Check (TRSM + threshold scan)
    //   8. Adjust for 2×2 boundary
    //   9. On failure: partial restore of failed columns (not full restore+retry)
    //  10. Schur updates (trailing + cross-terms for failed columns)
    //  11. Delay failed columns (swap to end_pos)
    //  12. Advance k += effective_nelim
    //
    // Steps 5-6 happen BEFORE apply_and_check to ensure a consistent
    // permuted state regardless of threshold outcome.
    while k < end_pos {
        let block_size = (end_pos - k).min(ib);
        let block_end = k + block_size;

        // 1. BACKUP: save a[k..m, k..k+block_size] before factoring
        let backup = BlockBackup::create(a.as_ref(), k, block_size, m, &mut kernel_ws.backup);

        // 2. FACTOR: complete pivoting on the block_size×block_size diagonal block
        //    Block-scoped swaps: only rows/columns within [0..block_end] are permuted.
        //    Panel rows [block_end..m] are NOT touched.
        let (block_d, block_perm, block_nelim, block_stats, block_log) =
            factor_block_diagonal(a.rb_mut(), k, block_size, small, block_end);

        if block_nelim == 0 {
            // Entire block is singular — restore and delay all columns
            backup.restore_failed(a.rb_mut(), k, 0, block_size, m);
            for offset in 0..block_size {
                let delayed_orig = col_order[k + block_perm[offset]];
                end_pos -= 1;
                if k + offset < end_pos {
                    swap_symmetric(a.rb_mut(), k + offset, end_pos);
                    col_order.swap(k + offset, end_pos);
                }
                stats.num_delayed += 1;
                pivot_log.push(AptpPivotRecord {
                    col: delayed_orig,
                    pivot_type: PivotType::Delayed,
                    max_l_entry: 0.0,
                    was_fallback: false,
                });
            }
            continue;
        }

        // 3. PERMUTE PANEL: reorder panel column entries a[r, k..k+bs]
        //    according to block_perm so that TRSM sees the correct input.
        let panel_start = block_end;
        for r in panel_start..m {
            for i in 0..block_size {
                panel_perm_buf[i] = a[(r, k + block_perm[i])];
            }
            for i in 0..block_size {
                a[(r, k + i)] = panel_perm_buf[i];
            }
        }

        // 4. Zero out D off-diagonals so apply_and_check's TRSM reads them as
        //    L11 entries (should be 0 for 2×2 pivots where L starts at row k+2).
        {
            let mut bc = 0;
            while bc < block_nelim {
                match block_d.pivot_type(bc) {
                    PivotType::TwoByTwo { partner } if partner > bc => {
                        a[(k + bc + 1, k + bc)] = 0.0;
                        bc += 2;
                    }
                    _ => {
                        bc += 1;
                    }
                }
            }
        }

        // 5. ROW PERM PROPAGATION: apply block_perm to columns 0..k.
        //    Done BEFORE apply_and_check to ensure the matrix is in a
        //    consistent permuted state regardless of threshold outcome.
        if k > 0 {
            for c in 0..k {
                for i in 0..block_size {
                    row_perm_buf[i] = a[(k + block_perm[i], c)];
                }
                for i in 0..block_size {
                    a[(k + i, c)] = row_perm_buf[i];
                }
            }
        }

        // 6. UPDATE COL_ORDER by block_perm (always, not just on success).
        col_order_buf[..block_size].copy_from_slice(&col_order[k..k + block_size]);
        for i in 0..block_size {
            col_order[k + i] = col_order_buf[block_perm[i]];
        }

        // 7. APPLY: TRSM on panel below + threshold check
        let mut effective_nelim = apply_and_check(
            a.rb_mut(),
            k,
            block_nelim,
            block_size,
            m,
            &block_d,
            threshold,
            options.par,
            &mut kernel_ws.l11_buf,
        );

        // 8. ADJUST: don't split 2×2 pivot across block boundary
        effective_nelim = adjust_for_2x2_boundary(effective_nelim, &block_d);

        // 9. PARTIAL RESTORE (on failure): restore only failed columns from
        //    pre-factor backup with permutation applied.
        //    We keep passed columns (0..effective_nelim) — their L11, D, L21 are committed.
        if effective_nelim < block_nelim {
            backup.restore_diagonal_with_perm(
                a.rb_mut(),
                k,
                effective_nelim,
                block_size,
                m,
                &block_perm,
            );
        }

        // Use effective_nelim as the number of passed columns for all subsequent steps.
        let nelim = effective_nelim;

        // 10. SCHUR UPDATES using only passed columns' L and D.
        //     Truncate block_d to passed columns for update computations.
        //     Three regions:
        //     a. Trailing×trailing: A[ts..m, ts..m] -= L_panel * D * L_panel^T
        //     b. Failed×failed: A[k+e..k+bs, k+e..k+bs] -= L_blk * D * L_blk^T
        //     c. Trailing×failed: A[ts..m, k+e..k+bs] -= L_panel * D * L_blk^T
        if nelim > 0 {
            update_trailing(
                a.rb_mut(),
                k,
                nelim,
                block_size,
                m,
                nfs_boundary,
                &block_d,
                options.par,
                &mut kernel_ws.ld_buf,
                &mut kernel_ws.copy_buf,
            );
            update_cross_terms(
                a.rb_mut(),
                k,
                nelim,
                block_size,
                m,
                &block_d,
                &mut kernel_ws.ld_buf,
                &mut kernel_ws.copy_buf,
            );
        }

        // 11. ACCUMULATE D entries for passed columns
        d.copy_from_offset(&block_d, k, nelim);

        // Accumulate stats from passed columns only (count from block_d directly)
        let mut passed_1x1 = 0;
        let mut passed_2x2 = 0;
        let mut sc = 0;
        while sc < nelim {
            match block_d.pivot_type(sc) {
                PivotType::OneByOne => {
                    passed_1x1 += 1;
                    sc += 1;
                }
                PivotType::TwoByTwo { partner } if partner > sc => {
                    passed_2x2 += 1;
                    sc += 2;
                }
                _ => {
                    sc += 1;
                }
            }
        }
        stats.num_1x1 += passed_1x1;
        stats.num_2x2 += passed_2x2;
        if block_stats.max_l_entry > stats.max_l_entry {
            stats.max_l_entry = block_stats.max_l_entry;
        }
        // Check panel L entries for max_l_entry (passed columns only)
        for c in 0..nelim {
            for i in panel_start..m {
                let v = a[(i, k + c)].abs();
                if v > stats.max_l_entry {
                    stats.max_l_entry = v;
                }
            }
        }

        // Accumulate pivot log for passed columns
        for record in &block_log {
            if !matches!(record.pivot_type, PivotType::Delayed) && record.col < nelim {
                let global_col = col_order[k + record.col];
                let global_pivot_type = match record.pivot_type {
                    PivotType::TwoByTwo { partner } => PivotType::TwoByTwo {
                        partner: col_order[k + partner],
                    },
                    other => other,
                };
                pivot_log.push(AptpPivotRecord {
                    col: global_col,
                    pivot_type: global_pivot_type,
                    max_l_entry: record.max_l_entry,
                    was_fallback: record.was_fallback,
                });
            }
        }

        // 12. DELAY failed columns (nelim..block_size): swap to end_pos
        if nelim < block_size {
            let n_delayed = block_size - nelim;
            for i in (0..n_delayed).rev() {
                let delayed_pos = k + nelim + i;
                end_pos -= 1;
                if delayed_pos < end_pos {
                    swap_symmetric(a.rb_mut(), delayed_pos, end_pos);
                    col_order.swap(delayed_pos, end_pos);
                }
                stats.num_delayed += 1;
                pivot_log.push(AptpPivotRecord {
                    col: col_order[end_pos],
                    pivot_type: PivotType::Delayed,
                    max_l_entry: 0.0,
                    was_fallback: false,
                });
            }
        }

        k += nelim;
    }

    let num_eliminated = k;

    d.truncate(num_eliminated);

    let delayed_cols: Vec<usize> = (num_eliminated..p).map(|i| col_order[i]).collect();

    Ok(AptpFactorResult {
        d,
        perm: col_order,
        num_eliminated,
        delayed_cols,
        stats,
        pivot_log,
    })
}

/// Two-level outer block loop for large frontal matrices.
///
/// Processes nb-sized blocks. For each outer block: `factor_inner` handles
/// the BLAS-3 Factor/Apply/Update loop internally on ib-sized sub-blocks,
/// including threshold checking on the panel via `apply_and_check` and
/// block-level backup/restore on failure.
///
/// Called by `aptp_factor_in_place` when `num_fully_summed > outer_block_size`.
///
/// # References
/// - Duff, Hogg & Lopez (2020), Algorithm 3.1: two-level outer loop
fn two_level_factor(
    mut a: MatMut<'_, f64>,
    num_fully_summed: usize,
    options: &AptpOptions,
    kernel_ws: &mut AptpKernelWorkspace,
) -> Result<AptpFactorResult, SparseError> {
    let m = a.nrows();
    let nb = options.outer_block_size;
    let p = num_fully_summed;

    let mut col_order: Vec<usize> = (0..m).collect();
    let mut global_d = MixedDiagonal::new(p);
    let mut stats = AptpStatistics::default();
    let mut pivot_log = Vec::with_capacity(p);

    let mut global_nelim = 0;
    let mut remaining_fully_summed = p;

    while remaining_fully_summed > 0 {
        let col_start = global_nelim;
        let block_cols = remaining_fully_summed.min(nb);

        // FACTOR: inner APTP on the full view (including panel rows).
        // factor_inner handles:
        //   - Complete pivoting search within ib×ib sub-blocks
        //   - Symmetric swaps applied to ALL rows (including panel)
        //   - L entry computation + threshold check for ALL rows (via try_1x1/try_2x2)
        //   - Schur complement update for ALL trailing rows
        //   - Internal backup/restore for failed pivots (inside try_1x1/try_2x2)
        //
        // No external backup/restore is needed: factor_inner handles all
        // threshold failures internally, and the Schur complement has been
        // applied to all trailing entries including any delayed columns.
        let block_m = m - col_start;
        let block_result = {
            let block_view = a
                .rb_mut()
                .submatrix_mut(col_start, col_start, block_m, block_m);
            // nfs_boundary relative to this subview: p - col_start
            // This ensures inner blocks skip NFS×NFS updates consistently
            // with the deferred GEMM that runs after aptp_factor_in_place.
            factor_inner(block_view, block_cols, p - col_start, options, kernel_ws)?
        };
        let block_nelim = block_result.num_eliminated;

        // PROPAGATE ROW PERMUTATION to already-factored columns.
        //
        // factor_inner operates on a submatrix view [col_start..m, col_start..m].
        // Its swap_symmetric calls only rearrange rows WITHIN that submatrix.
        // The L entries from previously-factored blocks (columns 0..col_start)
        // are NOT rearranged by these swaps. We must apply the same row
        // permutation to those columns so that extract_l reads consistent
        // L entries across all blocks.
        if col_start > 0 {
            let block_perm = &block_result.perm;
            let mut temp = vec![0.0f64; block_cols];
            for c in 0..col_start {
                // Gather: temp[i] = row that ended up at local position i
                for i in 0..block_cols {
                    temp[i] = a[(col_start + block_perm[i], c)];
                }
                // Scatter: write to sequential positions
                for i in 0..block_cols {
                    a[(col_start + i, c)] = temp[i];
                }
            }
        }

        // Update col_order BEFORE delay swap: factor_inner may have permuted
        // columns within the block. We must capture the pre-swap col_order so
        // that orig_order[block_perm[i]] reads the correct original column index.
        // If we swapped first, delayed positions would contain post-swap values,
        // corrupting the mapping for eliminated columns.
        {
            let block_perm = &block_result.perm;
            let orig_order: Vec<usize> = col_order[col_start..col_start + block_cols].to_vec();
            for i in 0..block_cols {
                // Entries with block_perm[i] >= block_cols are contribution block rows
                // (not fully-summed columns), so they don't have a col_order mapping.
                if block_perm[i] < block_cols {
                    col_order[col_start + i] = orig_order[block_perm[i]];
                }
            }
        }

        // ADJUST delayed columns: swap them to end of unprocessed region
        // so the next outer block processes fresh columns first.
        if block_nelim < block_cols {
            let n_failed = block_cols - block_nelim;
            for i in 0..n_failed {
                let failed_pos = col_start + block_nelim + i;
                let end = col_start + remaining_fully_summed - 1 - i;
                if failed_pos < end {
                    swap_symmetric(a.rb_mut(), failed_pos, end);
                    col_order.swap(failed_pos, end);
                }
            }
            stats.num_delayed += n_failed;
        }

        // 4. Accumulate into global result
        global_d.copy_from_offset(&block_result.d, global_nelim, block_nelim);

        // Merge stats
        stats.num_1x1 += block_result.stats.num_1x1;
        stats.num_2x2 += block_result.stats.num_2x2;
        if block_result.stats.max_l_entry > stats.max_l_entry {
            stats.max_l_entry = block_result.stats.max_l_entry;
        }

        // Merge pivot log
        for record in &block_result.pivot_log {
            if !matches!(record.pivot_type, PivotType::Delayed) {
                pivot_log.push(record.clone());
            }
        }
        // Add delayed logs
        let n_failed = block_cols - block_nelim;
        for i in 0..n_failed {
            let delayed_pos = col_start + remaining_fully_summed - 1 - i;
            pivot_log.push(AptpPivotRecord {
                col: col_order[delayed_pos],
                pivot_type: PivotType::Delayed,
                max_l_entry: 0.0,
                was_fallback: false,
            });
        }

        global_nelim += block_nelim;
        remaining_fully_summed -= block_cols;
    }

    global_d.truncate(global_nelim);

    let delayed_cols: Vec<usize> = (global_nelim..p).map(|i| col_order[i]).collect();

    Ok(AptpFactorResult {
        d: global_d,
        perm: col_order,
        num_eliminated: global_nelim,
        delayed_cols,
        stats,
        pivot_log,
    })
}

// ---------------------------------------------------------------------------
// TPP (Threshold Partial Pivoting) fallback
// ---------------------------------------------------------------------------

/// Test if `(t, p)` with `t < p` form a good 2x2 pivot.
///
/// Three necessary conditions for a stable 2x2 block pivot:
/// 1. Non-zero pivot block: max(|a11|, |a21|, |a22|) >= small
/// 2. Non-singular determinant with cancellation guard
/// 3. Threshold: u * max(|D^{-1}_{11}|*maxt + |D^{-1}_{12}|*maxp,
///    |D^{-1}_{12}|*maxt + |D^{-1}_{22}|*maxp) < 1
///
/// Returns `Some((a11, a21, a22))` (D values, NOT D^{-1}) on success.
// SPRAL Equivalent: `test_2x2()` in `spral/src/ssids/cpu/kernels/ldlt_tpp.cxx` (BSD-3).
fn tpp_test_2x2(
    a: MatRef<'_, f64>,
    t: usize,
    p: usize,
    maxt: f64,
    maxp: f64,
    u: f64,
    small: f64,
) -> Option<(f64, f64, f64)> {
    debug_assert!(t < p, "tpp_test_2x2 requires t < p");

    let a11 = a[(t, t)];
    let a21 = a[(p, t)]; // lower triangle: p > t
    let a22 = a[(p, p)];

    // 1. Non-zero pivot block
    let maxpiv = a11.abs().max(a21.abs()).max(a22.abs());
    if maxpiv < small {
        return None;
    }

    // 2. Cancellation guard on determinant
    let detscale = 1.0 / maxpiv;
    let detpiv0 = (a11 * detscale) * a22;
    let detpiv1 = (a21 * detscale) * a21;
    let detpiv = detpiv0 - detpiv1;
    if detpiv.abs() < small.max((detpiv0 / 2.0).abs()).max((detpiv1 / 2.0).abs()) {
        return None;
    }

    // 3. Threshold test using D^{-1}
    let d_inv_11 = (a22 * detscale) / detpiv;
    let d_inv_12 = (-a21 * detscale) / detpiv;
    let d_inv_22 = (a11 * detscale) / detpiv;

    if maxt.max(maxp) < small {
        // Rest of column is small — accept
        return Some((a11, a21, a22));
    }

    let x1 = d_inv_11.abs() * maxt + d_inv_12.abs() * maxp;
    let x2 = d_inv_12.abs() * maxt + d_inv_22.abs() * maxp;
    if u * x1.max(x2) < 1.0 {
        Some((a11, a21, a22))
    } else {
        None
    }
}

/// TPP fallback: serial column-by-column factorization on APTP's remaining columns.
///
/// Operates on the partially-factored matrix `a` where columns `0..start_col`
/// have been eliminated by APTP. Searches all remaining fully-summed columns
/// (`start_col..num_fully_summed`) for acceptable pivots using threshold partial
/// pivoting.
///
/// Uses full-matrix `swap_symmetric` which correctly propagates row swaps to
/// already-factored L columns (essential for correct L21 computation when rows
/// are reordered).
///
/// Returns the number of additional columns eliminated.
///
/// # References
///
/// - SPRAL `ldlt_tpp_factor()` in `spral/src/ssids/cpu/kernels/ldlt_tpp.cxx` (BSD-3)
/// - Duff, Hogg & Lopez (2020), Section 3: TPP as fallback after APTP
#[allow(clippy::too_many_arguments)]
fn tpp_factor(
    mut a: MatMut<'_, f64>,
    start_col: usize,
    num_fully_summed: usize,
    col_order: &mut [usize],
    global_d: &mut MixedDiagonal,
    stats: &mut AptpStatistics,
    pivot_log: &mut Vec<AptpPivotRecord>,
    options: &AptpOptions,
) -> usize {
    let m = a.nrows();
    let n = num_fully_summed;
    let u = options.threshold;
    let small = options.small;

    let mut nelim = start_col;

    while nelim < n {
        // Check if current column is effectively zero
        if tpp_is_col_small(a.as_ref(), nelim, nelim, m, small) {
            // Zero pivot: record and advance
            global_d.set_1x1(nelim, 0.0);
            stats.num_1x1 += 1;
            pivot_log.push(AptpPivotRecord {
                col: col_order[nelim],
                pivot_type: PivotType::OneByOne,
                max_l_entry: 0.0,
                was_fallback: true,
            });
            // Zero the column entries
            for r in (nelim + 1)..m {
                a[(r, nelim)] = 0.0;
            }
            nelim += 1;
            continue;
        }

        // Search columns p = nelim+1..n-1 for acceptable pivot
        let mut found = false;
        for p in (nelim + 1)..n {
            // Check if column p is effectively zero
            if tpp_is_col_small(a.as_ref(), p, nelim, m, small) {
                // Swap zero column to front and record zero pivot
                if p != nelim {
                    swap_symmetric(a.rb_mut(), p, nelim);
                    col_order.swap(p, nelim);
                }
                global_d.set_1x1(nelim, 0.0);
                stats.num_1x1 += 1;
                pivot_log.push(AptpPivotRecord {
                    col: col_order[nelim],
                    pivot_type: PivotType::OneByOne,
                    max_l_entry: 0.0,
                    was_fallback: true,
                });
                for r in (nelim + 1)..m {
                    a[(r, nelim)] = 0.0;
                }
                nelim += 1;
                found = true;
                break;
            }

            // Find column index t of largest |a(p, c)| for c in nelim..p
            let t = tpp_find_row_abs_max(a.as_ref(), p, nelim, p);

            // Try (t, p) as 2x2 pivot (requires t < p)
            let maxt = tpp_find_rc_abs_max_exclude(a.as_ref(), t, nelim, m, p);
            let maxp = tpp_find_rc_abs_max_exclude(a.as_ref(), p, nelim, m, t);
            if tpp_test_2x2(a.as_ref(), t, p, maxt, maxp, u, small).is_some() {
                // Accept 2x2 pivot: swap t→nelim, p→nelim+1
                if t != nelim {
                    swap_symmetric(a.rb_mut(), t, nelim);
                    col_order.swap(t, nelim);
                }
                // After first swap, p may have moved
                let new_p = if p == nelim { t } else { p };
                if new_p != nelim + 1 {
                    swap_symmetric(a.rb_mut(), new_p, nelim + 1);
                    col_order.swap(new_p, nelim + 1);
                }

                // Re-read D values after swap
                let d11 = a[(nelim, nelim)];
                let d21 = a[(nelim + 1, nelim)];
                let d22 = a[(nelim + 1, nelim + 1)];

                // Apply elimination and Schur update
                let max_l = tpp_apply_2x2(a.rb_mut(), nelim, m, n);

                // Record pivot
                global_d.set_2x2(Block2x2 {
                    first_col: nelim,
                    a: d11,
                    b: d21,
                    c: d22,
                });
                stats.num_2x2 += 1;
                stats.max_l_entry = stats.max_l_entry.max(max_l);
                pivot_log.push(AptpPivotRecord {
                    col: col_order[nelim],
                    pivot_type: PivotType::TwoByTwo {
                        partner: col_order[nelim + 1],
                    },
                    max_l_entry: max_l,
                    was_fallback: true,
                });
                pivot_log.push(AptpPivotRecord {
                    col: col_order[nelim + 1],
                    pivot_type: PivotType::TwoByTwo {
                        partner: col_order[nelim],
                    },
                    max_l_entry: max_l,
                    was_fallback: true,
                });
                nelim += 2;
                found = true;
                break;
            }

            // Try p as 1x1 pivot
            // maxp should include |a(p, t)| for the off-diagonal contribution
            let maxp_with_t = maxp.max(tpp_sym_entry(a.as_ref(), p, t).abs());
            if a[(p, p)].abs() >= u * maxp_with_t {
                // Accept 1x1 pivot: swap p→nelim
                if p != nelim {
                    swap_symmetric(a.rb_mut(), p, nelim);
                    col_order.swap(p, nelim);
                }

                let max_l = tpp_apply_1x1(a.rb_mut(), nelim, m, n);

                global_d.set_1x1(nelim, a[(nelim, nelim)]);
                stats.num_1x1 += 1;
                stats.max_l_entry = stats.max_l_entry.max(max_l);
                pivot_log.push(AptpPivotRecord {
                    col: col_order[nelim],
                    pivot_type: PivotType::OneByOne,
                    max_l_entry: max_l,
                    was_fallback: true,
                });
                nelim += 1;
                found = true;
                break;
            }
        }

        if !found {
            // Last resort: try column nelim as 1x1 (we started searching at nelim+1)
            let maxp = tpp_find_rc_abs_max_exclude(a.as_ref(), nelim, nelim, m, usize::MAX);
            if a[(nelim, nelim)].abs() >= u * maxp {
                let max_l = tpp_apply_1x1(a.rb_mut(), nelim, m, n);

                global_d.set_1x1(nelim, a[(nelim, nelim)]);
                stats.num_1x1 += 1;
                stats.max_l_entry = stats.max_l_entry.max(max_l);
                pivot_log.push(AptpPivotRecord {
                    col: col_order[nelim],
                    pivot_type: PivotType::OneByOne,
                    max_l_entry: max_l,
                    was_fallback: true,
                });
                nelim += 1;
            } else {
                // No more pivots can be found
                break;
            }
        }
    }

    nelim - start_col
}

// ---------------------------------------------------------------------------
// Rectangular TPP factorization for small-leaf fast path
// ---------------------------------------------------------------------------

/// Swap rows/columns i and j in a rectangular m×n lower-triangle-like matrix.
///
/// The matrix `a` has m rows and n columns (m ≥ n). It stores:
/// - Column data in columns 0..n (the portion being factored)
/// - Row data extends to all m rows
///
/// Unlike `swap_symmetric` which operates on an m×m square matrix, this limits
/// column operations to 0..n. Row swaps span the full column range 0..n.
// SPRAL Equivalent: `swap_cols(col1, col2, m, n, ...)` in `ldlt_tpp.cxx:44-72` (BSD-3).
fn swap_rect(mut a: MatMut<'_, f64>, i: usize, j: usize) {
    if i == j {
        return;
    }
    let (i, j) = if i < j { (i, j) } else { (j, i) };
    let n = a.ncols();

    // Swap diagonals (only if both are within column range)
    if i < n && j < n {
        let tmp = a[(i, i)];
        a[(i, i)] = a[(j, j)];
        a[(j, j)] = tmp;
    }

    // Rows k < i: swap a[(i,k)] and a[(j,k)] — both are in columns 0..i < n
    for k in 0..i.min(n) {
        let tmp = a[(i, k)];
        a[(i, k)] = a[(j, k)];
        a[(j, k)] = tmp;
    }

    // Rows i < k < j: swap a[(k,i)] and a[(j,k)]
    // a[(k,i)] is in column i < n; a[(j,k)] is in column k
    for k in (i + 1)..j {
        if k < n {
            let tmp = a[(k, i)];
            a[(k, i)] = a[(j, k)];
            a[(j, k)] = tmp;
        } else if i < n {
            // k >= n: only a[(k, i)] is accessible (column i < n)
            // a[(j, k)] is inaccessible (column k >= n)
            // Nothing to swap on the column side
            break;
        }
    }

    // Rows k > j: swap a[(k,i)] and a[(k,j)]
    // Only accessible if both i < n and j < n
    if i < n && j < n {
        for k in (j + 1)..a.nrows() {
            let tmp = a[(k, i)];
            a[(k, i)] = a[(k, j)];
            a[(k, j)] = tmp;
        }
    }

    // Cross element a[(j,i)] stays unchanged (if j < n, it's the off-diagonal)
}

/// Read symmetric entry `a(row, col)` from lower-triangle storage.
///
/// Works with both square (m×m) and rectangular (m×n, m ≥ n) matrices.
/// For row >= col with col < n: reads `a[(row, col)]` directly.
/// For row < col with col < n: reads `a[(col, row)]` (symmetric reflection).
/// Panics if both row and col are >= n (no data stored there).
#[inline]
fn tpp_sym_entry(a: MatRef<'_, f64>, row: usize, col: usize) -> f64 {
    let n = a.ncols();
    if col < n && row >= col {
        a[(row, col)]
    } else if row < n && col >= row {
        a[(col, row)]
    } else {
        panic!("tpp_sym_entry: row={} col={} both >= ncols={}", row, col, n);
    }
}

/// Apply 1x1 pivot elimination at position `nelim`.
///
/// Computes L entries (divides column by D) for ALL rows (FS + NFS), then
/// performs rank-1 Schur complement update only on FS columns (0..num_fully_summed).
/// Works with both square (m×m) and rectangular (m×n, m ≥ n) matrices.
///
/// Returns the maximum absolute L entry.
fn tpp_apply_1x1(mut a: MatMut<'_, f64>, nelim: usize, m: usize, num_fully_summed: usize) -> f64 {
    let d = a[(nelim, nelim)];
    let inv_d = 1.0 / d;
    let p = num_fully_summed;

    // Compute L entries for ALL rows (FS + NFS)
    let mut max_l = 0.0_f64;
    for i in (nelim + 1)..m {
        let l_ik = a[(i, nelim)] * inv_d;
        a[(i, nelim)] = l_ik;
        max_l = max_l.max(l_ik.abs());
    }

    // Rank-1 Schur complement update: only FS columns (< p),
    // but update ALL rows in each column (FS + NFS)
    let schur_col_end = p.min(a.ncols());
    for j in (nelim + 1)..schur_col_end {
        let ldlj = a[(j, nelim)] * d;
        for i in j..m {
            a[(i, j)] -= a[(i, nelim)] * ldlj;
        }
    }

    max_l
}

/// Apply 2x2 pivot elimination at positions `(nelim, nelim+1)`.
///
/// Computes L entries using D^{-1}, then performs rank-2 Schur complement
/// update only on FS columns (0..num_fully_summed). Works with both square
/// (m×m) and rectangular (m×n, m ≥ n) matrices.
///
/// Returns the maximum absolute L entry.
fn tpp_apply_2x2(mut a: MatMut<'_, f64>, nelim: usize, m: usize, num_fully_summed: usize) -> f64 {
    let a11 = a[(nelim, nelim)];
    let a21 = a[(nelim + 1, nelim)];
    let a22 = a[(nelim + 1, nelim + 1)];
    let det = a11 * a22 - a21 * a21;
    let inv_det = 1.0 / det;
    let p = num_fully_summed;

    // Compute L entries for ALL rows (FS + NFS)
    let mut max_l = 0.0_f64;
    let start = nelim + 2;
    for i in start..m {
        let ai1 = a[(i, nelim)];
        let ai2 = a[(i, nelim + 1)];
        let l_i1 = (ai1 * a22 - ai2 * a21) * inv_det;
        let l_i2 = (ai2 * a11 - ai1 * a21) * inv_det;
        a[(i, nelim)] = l_i1;
        a[(i, nelim + 1)] = l_i2;
        max_l = max_l.max(l_i1.abs()).max(l_i2.abs());
    }

    // Rank-2 Schur complement update: only FS columns (< p),
    // but update ALL rows in each column
    let schur_col_end = p.min(a.ncols());
    for j in start..schur_col_end {
        let wj1 = a[(j, nelim)] * a11 + a[(j, nelim + 1)] * a21;
        let wj2 = a[(j, nelim)] * a21 + a[(j, nelim + 1)] * a22;
        for i in j..m {
            a[(i, j)] -= a[(i, nelim)] * wj1 + a[(i, nelim + 1)] * wj2;
        }
    }

    // Zero D off-diagonal for extract_front_factors convention
    a[(nelim + 1, nelim)] = 0.0;

    max_l
}

/// Check if all entries in row/column `idx` within the uneliminated region are
/// smaller than `small` in absolute value. Works with both square (m×m) and
/// rectangular (m×n, m ≥ n) matrices.
// SPRAL Equivalent: `check_col_small()` in `spral/src/ssids/cpu/kernels/ldlt_tpp.cxx` (BSD-3).
fn tpp_is_col_small(a: MatRef<'_, f64>, idx: usize, from: usize, to: usize, small: f64) -> bool {
    let n = a.ncols();
    // Row entries: a[(idx, c)] for c < idx (lower triangle: idx > c), limited to ncols
    for c in from..idx.min(n) {
        if a[(idx, c)].abs() >= small {
            return false;
        }
    }
    // Column entries: a[(r, idx)] for r >= idx, if idx < n
    if idx < n {
        for r in idx..to {
            if a[(r, idx)].abs() >= small {
                return false;
            }
        }
    }
    true
}

/// Find the column index with largest absolute entry in row `p`, scanning
/// columns `from..to`. Works with both square and rectangular storage.
// SPRAL Equivalent: `find_row_abs_max()` in `spral/src/ssids/cpu/kernels/ldlt_tpp.cxx` (BSD-3).
fn tpp_find_row_abs_max(a: MatRef<'_, f64>, p: usize, from: usize, to: usize) -> usize {
    if from >= to {
        return from;
    }
    let mut best_idx = from;
    let mut best_val = tpp_sym_entry(a, p, from).abs();
    for c in (from + 1)..to {
        let v = tpp_sym_entry(a, p, c).abs();
        if v > best_val {
            best_idx = c;
            best_val = v;
        }
    }
    best_idx
}

/// Find max absolute value in row/column `col` among uneliminated positions,
/// excluding one index and the diagonal. Works with both square and rectangular storage.
// SPRAL Equivalent: `find_rc_abs_max_exclude()` in `spral/src/ssids/cpu/kernels/ldlt_tpp.cxx` (BSD-3).
fn tpp_find_rc_abs_max_exclude(
    a: MatRef<'_, f64>,
    col: usize,
    nelim: usize,
    m: usize,
    exclude: usize,
) -> f64 {
    let n = a.ncols();
    let mut best = 0.0_f64;
    // Row part: a[(col, c)] for c in nelim..col (limited to ncols)
    for c in nelim..col.min(n) {
        if c == exclude {
            continue;
        }
        best = best.max(a[(col, c)].abs());
    }
    // Column part: a[(r, col)] for r in col+1..m (if col < n)
    if col < n {
        for r in (col + 1)..m {
            if r == exclude {
                continue;
            }
            best = best.max(a[(r, col)].abs());
        }
    }
    best
}

/// Factor a rectangular m×n matrix using Threshold Partial Pivoting (TPP).
///
/// This is the small-leaf fast-path kernel. Unlike `tpp_factor` which operates
/// on a square m×m matrix, this takes a rectangular m×n matrix (m ≥ n) where
/// n = num_fully_summed. The matrix stores the lower triangle of the frontal
/// matrix restricted to the first n columns.
///
/// L entries are stored in-place in columns 0..num_eliminated.
/// The NFS×FS cross-terms (rows n..m, columns 0..num_eliminated) contain valid
/// L21 data needed for the contribution GEMM.
///
/// # Arguments
///
/// - `a`: Rectangular m×n matrix (m rows, n fully-summed columns), mutated in place.
/// - `num_fully_summed`: Number of columns eligible for elimination (= n = `a.ncols()`).
/// - `options`: APTP configuration.
///
/// # Returns
///
/// `AptpFactorResult` with D, permutation, elimination count, and diagnostics.
// SPRAL Equivalent: `ldlt_tpp_factor(m, n, perm, lcol, ldl, d, ld, ...)`
// in `spral/src/ssids/cpu/kernels/ldlt_tpp.cxx` (BSD-3).
pub(super) fn tpp_factor_rect(
    mut a: MatMut<'_, f64>,
    num_fully_summed: usize,
    options: &AptpOptions,
) -> Result<AptpFactorResult, SparseError> {
    let m = a.nrows();
    let n = num_fully_summed;

    debug_assert!(
        a.ncols() >= n,
        "tpp_factor_rect: ncols {} < num_fully_summed {}",
        a.ncols(),
        n
    );
    debug_assert!(m >= n, "tpp_factor_rect: nrows {} < ncols {}", m, n);

    let u = options.threshold;
    let small = options.small;

    let mut col_order: Vec<usize> = (0..m).collect();
    let mut d = MixedDiagonal::new(n);
    let mut stats = AptpStatistics::default();
    let mut pivot_log = Vec::with_capacity(n);

    let mut nelim = 0;

    while nelim < n {
        // Check if current column is effectively zero
        if tpp_is_col_small(a.as_ref(), nelim, nelim, m, small) {
            d.set_1x1(nelim, 0.0);
            stats.num_1x1 += 1;
            pivot_log.push(AptpPivotRecord {
                col: col_order[nelim],
                pivot_type: PivotType::OneByOne,
                max_l_entry: 0.0,
                was_fallback: false,
            });
            if nelim < a.ncols() {
                for r in (nelim + 1)..m {
                    a[(r, nelim)] = 0.0;
                }
            }
            nelim += 1;
            continue;
        }

        // Search columns p = nelim+1..n for acceptable pivot
        let mut found = false;
        for p in (nelim + 1)..n {
            // Check if column p is effectively zero
            if tpp_is_col_small(a.as_ref(), p, nelim, m, small) {
                if p != nelim {
                    swap_rect(a.rb_mut(), p, nelim);
                    col_order.swap(p, nelim);
                }
                d.set_1x1(nelim, 0.0);
                stats.num_1x1 += 1;
                pivot_log.push(AptpPivotRecord {
                    col: col_order[nelim],
                    pivot_type: PivotType::OneByOne,
                    max_l_entry: 0.0,
                    was_fallback: false,
                });
                for r in (nelim + 1)..m {
                    a[(r, nelim)] = 0.0;
                }
                nelim += 1;
                found = true;
                break;
            }

            // Find column index t of largest |a(p, c)| for c in nelim..p
            let t = tpp_find_row_abs_max(a.as_ref(), p, nelim, p);

            // Try (t, p) as 2x2 pivot (requires t < p)
            let maxt = tpp_find_rc_abs_max_exclude(a.as_ref(), t, nelim, m, p);
            let maxp = tpp_find_rc_abs_max_exclude(a.as_ref(), p, nelim, m, t);
            // Use tpp_test_2x2 which reads from (t,t), (p,t), (p,p) in lower triangle
            // These are all accessible in rectangular storage since t < p < n <= ncols
            if tpp_test_2x2(a.as_ref(), t, p, maxt, maxp, u, small).is_some() {
                // Accept 2x2 pivot: swap t→nelim, p→nelim+1
                if t != nelim {
                    swap_rect(a.rb_mut(), t, nelim);
                    col_order.swap(t, nelim);
                }
                let new_p = if p == nelim { t } else { p };
                if new_p != nelim + 1 {
                    swap_rect(a.rb_mut(), new_p, nelim + 1);
                    col_order.swap(new_p, nelim + 1);
                }

                let d11 = a[(nelim, nelim)];
                let d21 = a[(nelim + 1, nelim)];
                let d22 = a[(nelim + 1, nelim + 1)];

                let max_l = tpp_apply_2x2(a.rb_mut(), nelim, m, n);

                d.set_2x2(Block2x2 {
                    first_col: nelim,
                    a: d11,
                    b: d21,
                    c: d22,
                });
                stats.num_2x2 += 1;
                stats.max_l_entry = stats.max_l_entry.max(max_l);
                pivot_log.push(AptpPivotRecord {
                    col: col_order[nelim],
                    pivot_type: PivotType::TwoByTwo {
                        partner: col_order[nelim + 1],
                    },
                    max_l_entry: max_l,
                    was_fallback: false,
                });
                pivot_log.push(AptpPivotRecord {
                    col: col_order[nelim + 1],
                    pivot_type: PivotType::TwoByTwo {
                        partner: col_order[nelim],
                    },
                    max_l_entry: max_l,
                    was_fallback: false,
                });
                nelim += 2;
                found = true;
                break;
            }

            // Try p as 1x1 pivot
            let maxp_with_t = maxp.max(tpp_sym_entry(a.as_ref(), p, t).abs());
            if a[(p, p)].abs() >= u * maxp_with_t {
                if p != nelim {
                    swap_rect(a.rb_mut(), p, nelim);
                    col_order.swap(p, nelim);
                }

                let max_l = tpp_apply_1x1(a.rb_mut(), nelim, m, n);

                d.set_1x1(nelim, a[(nelim, nelim)]);
                stats.num_1x1 += 1;
                stats.max_l_entry = stats.max_l_entry.max(max_l);
                pivot_log.push(AptpPivotRecord {
                    col: col_order[nelim],
                    pivot_type: PivotType::OneByOne,
                    max_l_entry: max_l,
                    was_fallback: false,
                });
                nelim += 1;
                found = true;
                break;
            }
        }

        if !found {
            // Last resort: try column nelim as 1x1
            let maxp = tpp_find_rc_abs_max_exclude(a.as_ref(), nelim, nelim, m, usize::MAX);
            if a[(nelim, nelim)].abs() >= u * maxp {
                let max_l = tpp_apply_1x1(a.rb_mut(), nelim, m, n);

                d.set_1x1(nelim, a[(nelim, nelim)]);
                stats.num_1x1 += 1;
                stats.max_l_entry = stats.max_l_entry.max(max_l);
                pivot_log.push(AptpPivotRecord {
                    col: col_order[nelim],
                    pivot_type: PivotType::OneByOne,
                    max_l_entry: max_l,
                    was_fallback: false,
                });
                nelim += 1;
            } else {
                break;
            }
        }
    }

    d.truncate(nelim);
    stats.num_delayed = n - nelim;

    let delayed_cols: Vec<usize> = (nelim..n).map(|i| col_order[i]).collect();

    Ok(AptpFactorResult {
        d,
        perm: col_order,
        num_eliminated: nelim,
        delayed_cols,
        stats,
        pivot_log,
    })
}

/// Compute NFS×NFS Schur complement from rectangular L storage into contrib_buffer.
///
/// Like `compute_contribution_gemm` but reads L21_NFS from a rectangular m×k
/// matrix instead of a square m×m frontal matrix.
///
/// # Arguments
///
/// - `l_storage`: Rectangular m×k matrix containing factored L data.
/// - `num_fully_summed`: k (number of fully-summed columns).
/// - `num_eliminated`: ne (columns successfully eliminated).
/// - `m`: Total front size (rows in l_storage).
/// - `d`: Block diagonal from factorization.
/// - `contrib_buffer`: Output NFS×NFS Schur complement (lower triangle).
/// - `ld_workspace`: Reusable W = L·D buffer.
/// - `par`: Parallelism control.
#[allow(clippy::too_many_arguments)]
pub(super) fn compute_contribution_gemm_rect(
    l_storage: &Mat<f64>,
    num_fully_summed: usize,
    num_eliminated: usize,
    m: usize,
    d: &MixedDiagonal,
    contrib_buffer: &mut Mat<f64>,
    ld_workspace: &mut Mat<f64>,
    par: Par,
) {
    let p = num_fully_summed;
    let ne = num_eliminated;
    let nfs = m - p;

    if nfs == 0 || ne == 0 {
        return;
    }

    // L21_NFS = l_storage[p..m, 0..ne]
    let l21_nfs = l_storage.as_ref().submatrix(p, 0, nfs, ne);

    // Use caller-provided workspace
    if ld_workspace.nrows() < nfs || ld_workspace.ncols() < ne {
        *ld_workspace = Mat::zeros(nfs.max(ld_workspace.nrows()), ne.max(ld_workspace.ncols()));
    }
    let mut w = ld_workspace.as_mut().submatrix_mut(0, 0, nfs, ne);
    w.fill(0.0);
    compute_ld_into(l21_nfs, d, ne, w.rb_mut());

    // Symmetric rank-ne update: contrib -= W * L21_NFS^T (lower triangle)
    tri_matmul::matmul_with_conj(
        contrib_buffer.as_mut().submatrix_mut(0, 0, nfs, nfs),
        BlockStructure::TriangularLower,
        Accum::Add,
        w.as_ref(),
        BlockStructure::Rectangular,
        Conj::No,
        l21_nfs.transpose(),
        BlockStructure::Rectangular,
        Conj::No,
        -1.0,
        par,
    );
}

/// Extract front factors from rectangular m×k L storage.
///
/// Adapts `extract_front_factors` for the case where the factored data lives
/// in a rectangular m×k matrix instead of a square m×m frontal matrix.
/// The L11, D11, L21 extraction logic is identical; only the source layout differs.
pub(super) fn extract_front_factors_rect(
    l_storage: &Mat<f64>,
    m: usize,
    k: usize,
    frontal_row_indices: &[usize],
    result: &AptpFactorResult,
) -> super::numeric::FrontFactors {
    let ne = result.num_eliminated;

    // Extract L11 (ne × ne)
    let l11 = if ne > 0 {
        let mut l = Mat::zeros(ne, ne);
        let mut col = 0;
        while col < ne {
            l[(col, col)] = 1.0;
            match result.d.pivot_type(col) {
                PivotType::OneByOne => {
                    let n_entries = ne - (col + 1);
                    if n_entries > 0 {
                        let src = &l_storage.col_as_slice(col)[col + 1..ne];
                        l.col_as_slice_mut(col)[col + 1..ne].copy_from_slice(src);
                    }
                    col += 1;
                }
                PivotType::TwoByTwo { partner } if partner > col => {
                    l[(col + 1, col + 1)] = 1.0;
                    let n_entries = ne - (col + 2);
                    if n_entries > 0 {
                        let src0 = &l_storage.col_as_slice(col)[col + 2..ne];
                        l.col_as_slice_mut(col)[col + 2..ne].copy_from_slice(src0);
                        let src1 = &l_storage.col_as_slice(col + 1)[col + 2..ne];
                        l.col_as_slice_mut(col + 1)[col + 2..ne].copy_from_slice(src1);
                    }
                    col += 2;
                }
                PivotType::TwoByTwo { .. } => {
                    col += 1;
                }
                PivotType::Delayed => {
                    unreachable!("unexpected Delayed pivot at col {} in 0..ne", col);
                }
            }
        }
        l
    } else {
        Mat::zeros(0, 0)
    };

    // Build truncated D11
    let mut d11 = MixedDiagonal::new(ne);
    let mut col = 0;
    while col < ne {
        match result.d.pivot_type(col) {
            PivotType::OneByOne => {
                d11.set_1x1(col, result.d.diagonal_1x1(col));
                col += 1;
            }
            PivotType::TwoByTwo { partner: _ } => {
                let block = result.d.diagonal_2x2(col);
                d11.set_2x2(Block2x2 {
                    first_col: col,
                    a: block.a,
                    b: block.b,
                    c: block.c,
                });
                col += 2;
            }
            PivotType::Delayed => {
                unreachable!("unexpected Delayed pivot at col {} in 0..ne", col);
            }
        }
    }

    // Extract L21 (r × ne) where r = m - ne
    let r = m - ne;
    let l21 = if ne > 0 && r > 0 {
        let mut l = Mat::zeros(r, ne);
        for j in 0..ne {
            let src = &l_storage.col_as_slice(j)[ne..m];
            l.col_as_slice_mut(j)[..r].copy_from_slice(src);
        }
        l
    } else {
        Mat::zeros(r, ne)
    };

    // Local permutation and indices
    let local_perm = result.perm[..k].to_vec();
    let col_indices: Vec<usize> = local_perm[..ne]
        .iter()
        .map(|&lp| frontal_row_indices[lp])
        .collect();

    let mut row_indices = Vec::with_capacity(m - ne);
    for &lp in &result.perm[ne..k] {
        row_indices.push(frontal_row_indices[lp]);
    }
    row_indices.extend_from_slice(&frontal_row_indices[k..m]);

    super::numeric::FrontFactors {
        l11,
        d11,
        l21,
        local_perm,
        num_eliminated: ne,
        col_indices,
        row_indices,
    }
}

/// Extract contribution block from rectangular m×k L storage.
///
/// Adapts `extract_contribution` for rectangular layout. The NFS×NFS Schur
/// complement is already in `contrib_buffer` (from `compute_contribution_gemm_rect`).
/// Delayed column data (if any) is read from `l_storage[ne..k, ne..k]` and
/// `l_storage[k..m, ne..k]`.
pub(super) fn extract_contribution_rect(
    l_storage: &Mat<f64>,
    m: usize,
    k: usize,
    frontal_row_indices: &[usize],
    result: &AptpFactorResult,
    mut contrib_buffer: Mat<f64>,
) -> super::numeric::ContributionBlock {
    let ne = result.num_eliminated;
    let size = m - ne;
    let num_delayed = k - ne;
    let nfs = m - k;

    if num_delayed > 0 {
        let mut data = Mat::zeros(size, size);

        // Copy delayed × delayed from l_storage[ne..k, ne..k]
        for j in 0..num_delayed {
            let col_len = num_delayed - j;
            let src = &l_storage.col_as_slice(ne + j)[ne + j..ne + j + col_len];
            data.col_as_slice_mut(j)[j..j + col_len].copy_from_slice(src);
        }

        // Copy NFS × delayed cross-terms from l_storage[k..m, ne..k]
        for j in 0..num_delayed {
            let src = &l_storage.col_as_slice(ne + j)[k..m];
            data.col_as_slice_mut(j)[num_delayed..size].copy_from_slice(src);
        }

        // Copy NFS × NFS from contrib_buffer
        for j in 0..nfs {
            let col_len = nfs - j;
            let src = &contrib_buffer.col_as_slice(j)[j..j + col_len];
            data.col_as_slice_mut(num_delayed + j)[num_delayed + j..size].copy_from_slice(src);
        }

        contrib_buffer = data;
    }

    let mut row_indices = Vec::with_capacity(size);
    for &lp in &result.perm[ne..k] {
        row_indices.push(frontal_row_indices[lp]);
    }
    row_indices.extend_from_slice(&frontal_row_indices[k..m]);

    super::numeric::ContributionBlock {
        data: contrib_buffer,
        row_indices,
        num_delayed,
    }
}

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

    // ---- Test infrastructure ----

    /// Reconstruct P^T L D L^T P from factorization components.
    fn reconstruct_dense_ldlt(l: &Mat<f64>, d: &MixedDiagonal, perm: &[usize]) -> Mat<f64> {
        let n = l.nrows();

        // Build D as dense
        let mut d_mat = Mat::zeros(n, n);
        let nd = d.dimension();
        let mut col = 0;
        while col < nd {
            match d.pivot_type(col) {
                PivotType::OneByOne => {
                    d_mat[(col, col)] = d.diagonal_1x1(col);
                    col += 1;
                }
                PivotType::TwoByTwo { partner } if partner > col => {
                    let block = d.diagonal_2x2(col);
                    d_mat[(col, col)] = block.a;
                    d_mat[(col, col + 1)] = block.b;
                    d_mat[(col + 1, col)] = block.b;
                    d_mat[(col + 1, col + 1)] = block.c;
                    col += 2;
                }
                _ => {
                    col += 1;
                }
            }
        }

        // L * D
        let mut w = Mat::zeros(n, n);
        for i in 0..n {
            for j in 0..n {
                let mut sum = 0.0;
                for k in 0..n {
                    sum += l[(i, k)] * d_mat[(k, j)];
                }
                w[(i, j)] = sum;
            }
        }

        // W * L^T
        let mut ldlt = Mat::zeros(n, n);
        for i in 0..n {
            for j in 0..n {
                let mut sum = 0.0;
                for k in 0..n {
                    sum += w[(i, k)] * l[(j, k)];
                }
                ldlt[(i, j)] = sum;
            }
        }

        // Apply permutation: reconstructed[perm[i], perm[j]] = ldlt[i, j]
        let mut result = Mat::zeros(n, n);
        for i in 0..n {
            for j in 0..n {
                result[(perm[i], perm[j])] = ldlt[(i, j)];
            }
        }

        result
    }

    /// Compute ||A - P^T L D L^T P|| / ||A||
    fn dense_reconstruction_error(
        original: &Mat<f64>,
        l: &Mat<f64>,
        d: &MixedDiagonal,
        perm: &[usize],
    ) -> f64 {
        let reconstructed = reconstruct_dense_ldlt(l, d, perm);
        let n = original.nrows();

        let mut diff_norm_sq = 0.0_f64;
        let mut orig_norm_sq = 0.0_f64;

        for i in 0..n {
            for j in 0..n {
                let diff = original[(i, j)] - reconstructed[(i, j)];
                diff_norm_sq += diff * diff;
                orig_norm_sq += original[(i, j)] * original[(i, j)];
            }
        }

        if orig_norm_sq == 0.0 {
            return diff_norm_sq.sqrt();
        }
        (diff_norm_sq / orig_norm_sq).sqrt()
    }

    fn symmetric_matrix(n: usize, f: impl Fn(usize, usize) -> f64) -> Mat<f64> {
        Mat::from_fn(n, n, |i, j| if i >= j { f(i, j) } else { f(j, i) })
    }

    // ---- Infrastructure test ----

    #[test]
    fn test_reconstruction_trivial_identity() {
        let l = Mat::identity(2, 2);
        let mut d = MixedDiagonal::new(2);
        d.set_1x1(0, 1.0);
        d.set_1x1(1, 1.0);
        let perm = vec![0, 1];

        let result = reconstruct_dense_ldlt(&l, &d, &perm);
        for i in 0..2 {
            for j in 0..2 {
                let expected = if i == j { 1.0 } else { 0.0 };
                assert!(
                    (result[(i, j)] - expected).abs() < 1e-14,
                    "({},{}) = {}, expected {}",
                    i,
                    j,
                    result[(i, j)],
                    expected
                );
            }
        }
    }

    // ---- Complete Pivoting Tests (Algorithm 4.1) ----

    /// Helper: run complete_pivoting_factor and extract L for reconstruction testing.
    fn complete_pivoting_factor_and_extract(
        a: &Mat<f64>,
    ) -> (Mat<f64>, MixedDiagonal, Vec<usize>, AptpStatistics) {
        let mut a_copy = a.clone();
        let result = complete_pivoting_factor(a_copy.as_mut(), 1e-20);
        let l = extract_l(a_copy.as_ref(), &result.d, result.num_eliminated);
        (l, result.d, result.perm, result.stats)
    }

    #[test]
    fn test_cp_identity() {
        // 3×3 identity → D=[1,1,1], no permutation
        let a = Mat::identity(3, 3);
        let mut a_copy = a.clone();
        let result = complete_pivoting_factor(a_copy.as_mut(), 1e-20);

        assert_eq!(result.num_eliminated, 3);
        assert_eq!(result.stats.num_1x1, 3);
        assert_eq!(result.stats.num_2x2, 0);
        assert_eq!(result.stats.num_delayed, 0);

        // D should be [1, 1, 1]
        for i in 0..3 {
            assert!((result.d.diagonal_1x1(i) - 1.0).abs() < 1e-14);
        }

        // No off-diagonal L entries
        assert!(result.stats.max_l_entry < 1e-14);
    }

    #[test]
    fn test_cp_diagonal_pivot_ordering() {
        // 3×3 diagonal with known pivot ordering (largest diagonal first)
        let a = symmetric_matrix(3, |i, j| if i == j { [2.0, 5.0, 3.0][i] } else { 0.0 });
        let mut a_copy = a.clone();
        let result = complete_pivoting_factor(a_copy.as_mut(), 1e-20);

        assert_eq!(result.num_eliminated, 3);
        assert_eq!(result.stats.num_1x1, 3);

        // First pivot should be 5.0 (col 1), then 3.0 (col 2), then 2.0 (col 0)
        assert!(
            (result.d.diagonal_1x1(0) - 5.0).abs() < 1e-14,
            "first pivot should be 5.0, got {}",
            result.d.diagonal_1x1(0)
        );
        assert!(
            (result.d.diagonal_1x1(1) - 3.0).abs() < 1e-14,
            "second pivot should be 3.0, got {}",
            result.d.diagonal_1x1(1)
        );
        assert!(
            (result.d.diagonal_1x1(2) - 2.0).abs() < 1e-14,
            "third pivot should be 2.0, got {}",
            result.d.diagonal_1x1(2)
        );
    }

    #[test]
    fn test_cp_2x2_pivot() {
        // 4×4 matrix requiring 2×2 pivot (off-diagonal maximum)
        // Designed so max entry is off-diagonal and Δ condition passes
        let a = symmetric_matrix(4, |i, j| {
            let vals = [
                [0.1, 10.0, 0.0, 0.0],
                [10.0, 0.1, 0.0, 0.0],
                [0.0, 0.0, 5.0, 1.0],
                [0.0, 0.0, 1.0, 3.0],
            ];
            vals[i][j]
        });

        let (l, d, perm, stats) = complete_pivoting_factor_and_extract(&a);

        assert!(
            stats.num_2x2 >= 1,
            "expected at least one 2×2 pivot, got {}",
            stats.num_2x2
        );
        // L entries bounded by 4 (complete pivoting guarantee)
        assert!(
            stats.max_l_entry <= COMPLETE_PIVOTING_GROWTH_BOUND + 1e-10,
            "L entries should be bounded by 4, got {}",
            stats.max_l_entry
        );

        // Reconstruction
        let error = dense_reconstruction_error(&a, &l, &d, &perm);
        assert!(error < 1e-12, "reconstruction error {:.2e} >= 1e-12", error);
    }

    #[test]
    fn test_cp_failed_2x2_fallback() {
        // 4×4 matrix where 2×2 Δ test fails → fallback to 1×1 on max diagonal
        // Need |Δ| < 0.5 * |a_tm|^2
        // a_mm * a_tt - a_tm^2 should be small relative to a_tm^2
        // Let a_mm = 1.0, a_tt = 1.0, a_tm = 2.0
        // Δ = 1*1 - 4 = -3, |Δ| = 3, 0.5*a_tm^2 = 2 → |Δ| > 0.5*a_tm^2
        // Need: a_mm ≈ a_tt and both ≈ a_tm
        // Let a_mm = 0.5, a_tt = 0.5, a_tm = 1.0
        // Δ = 0.25 - 1 = -0.75, |Δ| = 0.75, 0.5*a_tm^2 = 0.5 → 0.75 > 0.5, passes!
        // Let a_mm = 0.1, a_tt = 0.1, a_tm = 1.0
        // Δ = 0.01 - 1 = -0.99, |Δ| = 0.99, 0.5*a_tm^2 = 0.5 → 0.99 > 0.5, passes!
        // Let a_mm = 0.01, a_tt = 0.01, a_tm = 1.0
        // Δ = 0.0001 - 1 = -0.9999, |Δ| = 0.9999, 0.5*1 = 0.5 → passes!
        // Hmm, hard to fail. The condition is |det| >= 0.5*a_tm^2.
        // For failure: need det ≈ 0. This means a_mm*a_tt ≈ a_tm^2.
        // Let a_mm = 2.0, a_tt = 2.0, a_tm = 2.0
        // Δ = 4 - 4 = 0, |Δ| = 0 < 0.5*4 = 2 → FAILS! Good.
        let a = symmetric_matrix(4, |i, j| {
            let vals = [
                [2.0, 2.0, 0.1, 0.1],
                [2.0, 2.0, 0.1, 0.1],
                [0.1, 0.1, 5.0, 0.0],
                [0.1, 0.1, 0.0, 3.0],
            ];
            vals[i][j]
        });

        let (l, d, perm, stats) = complete_pivoting_factor_and_extract(&a);

        // With Δ ≈ 0, should fall back to 1×1 on max(|a_mm|, |a_tt|)
        // L entries bounded by √2 < 4 in this case (paper's bound)
        assert!(
            stats.max_l_entry <= COMPLETE_PIVOTING_GROWTH_BOUND + 1e-10,
            "L entries should be bounded by 4"
        );

        // Reconstruction
        let error = dense_reconstruction_error(&a, &l, &d, &perm);
        assert!(error < 1e-12, "reconstruction error {:.2e} >= 1e-12", error);
    }

    #[test]
    fn test_cp_singular_block() {
        // singular/near-singular block → zero pivot handling
        let mut a = Mat::zeros(3, 3);
        a[(0, 0)] = 1e-25;
        a[(1, 1)] = 1e-25;
        a[(2, 2)] = 1e-25;

        let result = complete_pivoting_factor(a.as_mut(), 1e-20);

        // All entries below small → all should be zero pivots
        assert_eq!(
            result.num_eliminated, 0,
            "near-singular block should have 0 eliminations"
        );
        assert_eq!(result.stats.num_delayed, 3);
    }

    #[test]
    fn test_cp_reconstruction_random() {
        // reconstruction on random symmetric indefinite matrices
        // Use deterministic seed for reproducibility
        let sizes = [8, 16, 32];
        for &n in &sizes {
            let a = symmetric_matrix(n, |i, j| {
                // Deterministic pseudo-random indefinite matrix
                let seed = (i * 1000 + j * 7 + 13) as f64;
                let val = (seed * 0.618033988749).fract() * 2.0 - 1.0;
                if i == j {
                    val * 10.0 // diagonal dominance but indefinite
                } else {
                    val
                }
            });

            let (l, d, perm, stats) = complete_pivoting_factor_and_extract(&a);

            // Should eliminate all columns (no singular blocks)
            assert_eq!(
                stats.num_1x1 + 2 * stats.num_2x2 + stats.num_delayed,
                n,
                "statistics invariant for {}x{}",
                n,
                n
            );

            if stats.num_delayed == 0 {
                let error = dense_reconstruction_error(&a, &l, &d, &perm);
                assert!(
                    error < 1e-12,
                    "complete pivoting {}x{}: reconstruction error {:.2e} >= 1e-12",
                    n,
                    n,
                    error
                );
            }

            // L entries bounded by 4 (Algorithm 4.1 guarantee)
            assert!(
                stats.max_l_entry <= COMPLETE_PIVOTING_GROWTH_BOUND + 1e-10,
                "complete pivoting {}x{}: max_l_entry {:.2e} > 4",
                n,
                n,
                stats.max_l_entry
            );
        }
    }

    #[test]
    fn test_1x1_trivial_diagonal() {
        let a = symmetric_matrix(2, |i, j| if i == j { [4.0, 9.0][i] } else { 0.0 });

        let opts = AptpOptions {
            fallback: AptpFallback::Delay,
            ..AptpOptions::default()
        };
        let result = aptp_factor(a.as_ref(), &opts).unwrap();

        // All columns eliminated, no delays
        assert_eq!(result.stats.num_1x1 + 2 * result.stats.num_2x2, 2);
        assert_eq!(result.stats.num_delayed, 0);
        assert!(result.delayed_cols.is_empty());

        let error =
            dense_reconstruction_error(&a, &result.l, &result.d, result.perm.as_ref().arrays().0);
        assert!(error < 1e-12, "reconstruction error {:.2e} >= 1e-12", error);
    }

    #[test]
    fn test_1x1_positive_definite_3x3() {
        let a = symmetric_matrix(3, |i, j| {
            let vals = [[4.0, 2.0, 1.0], [2.0, 5.0, 3.0], [1.0, 3.0, 6.0]];
            vals[i][j]
        });

        let opts = AptpOptions::default();
        let result = aptp_factor(a.as_ref(), &opts).unwrap();

        // All columns eliminated, no delays
        assert_eq!(result.stats.num_1x1 + 2 * result.stats.num_2x2, 3);
        assert_eq!(result.stats.num_delayed, 0);

        let error =
            dense_reconstruction_error(&a, &result.l, &result.d, result.perm.as_ref().arrays().0);
        assert!(error < 1e-12, "reconstruction error {:.2e} >= 1e-12", error);
    }

    #[test]
    fn test_all_delayed_zero_matrix() {
        let n = 4;
        let a = Mat::zeros(n, n);

        let opts = AptpOptions {
            failed_pivot_method: FailedPivotMethod::Pass,
            ..AptpOptions::default()
        };
        let result = aptp_factor(a.as_ref(), &opts).unwrap();

        // TPP treats zero columns as zero pivots (1x1 with D=0), not delays.
        // With FailedPivotMethod::Pass, TPP is still used as primary for small
        // matrices, and it handles zero columns by recording them as zero pivots.
        // Total columns accounted for:
        let total = result.stats.num_1x1 + 2 * result.stats.num_2x2 + result.stats.num_delayed;
        assert_eq!(total, n, "total pivots + delays should equal n");
    }

    #[test]
    fn test_1x1_singularity_detection() {
        let a = symmetric_matrix(3, |i, j| if i == j { [4.0, 1e-25, 9.0][i] } else { 0.0 });

        let opts = AptpOptions {
            fallback: AptpFallback::Delay,
            failed_pivot_method: FailedPivotMethod::Pass,
            ..AptpOptions::default()
        };
        let result = aptp_factor(a.as_ref(), &opts).unwrap();

        // The near-zero entry should be detected (as zero pivot or delay).
        // TPP records zero columns as zero pivots (1x1 with D=0), while APTP
        // delays them. Either way, the other 2 columns should be eliminated.
        let eliminated = result.stats.num_1x1 + 2 * result.stats.num_2x2;
        assert!(
            eliminated >= 2,
            "should eliminate at least 2 columns, got {}",
            eliminated
        );
        let total = eliminated + result.stats.num_delayed;
        assert_eq!(total, 3, "total pivots + delays should equal n");
    }

    #[test]
    fn test_stability_bound_enforced() {
        // With complete pivoting, this matrix is handled via 2×2 pivot
        // (max off-diagonal entry 1.0 triggers 2×2 with Δ condition).
        // Complete pivoting bounds L entries by 4 (u=0.25), so no delays.
        let a = symmetric_matrix(2, |i, j| {
            let vals = [[1e-4, 1.0], [1.0, 1.0]];
            vals[i][j]
        });

        let opts = AptpOptions {
            fallback: AptpFallback::Delay,
            ..AptpOptions::default()
        };
        let result = aptp_factor(a.as_ref(), &opts).unwrap();

        // Complete pivoting eliminates all columns (no delays)
        let sum = result.stats.num_1x1 + 2 * result.stats.num_2x2 + result.stats.num_delayed;
        assert_eq!(sum, 2);
        // Verify correctness via reconstruction
        let error =
            dense_reconstruction_error(&a, &result.l, &result.d, result.perm.as_ref().arrays().0);
        assert!(error < 1e-12, "reconstruction error {:.2e} >= 1e-12", error);
    }

    #[test]
    fn test_1x1_matrix() {
        let a = Mat::from_fn(1, 1, |_, _| 5.0);

        let opts = AptpOptions::default();
        let result = aptp_factor(a.as_ref(), &opts).unwrap();

        assert_eq!(result.stats.num_1x1, 1);
        assert_eq!(result.stats.num_delayed, 0);

        let error =
            dense_reconstruction_error(&a, &result.l, &result.d, result.perm.as_ref().arrays().0);
        assert!(error < 1e-14, "reconstruction error {:.2e}", error);
    }

    #[test]
    fn test_statistics_sum_invariant() {
        let a = symmetric_matrix(5, |i, j| {
            let vals = [
                [10.0, 1.0, 0.0, 0.0, 0.0],
                [1.0, 20.0, 2.0, 0.0, 0.0],
                [0.0, 2.0, 30.0, 3.0, 0.0],
                [0.0, 0.0, 3.0, 40.0, 4.0],
                [0.0, 0.0, 0.0, 4.0, 50.0],
            ];
            vals[i][j]
        });

        let opts = AptpOptions::default();
        let result = aptp_factor(a.as_ref(), &opts).unwrap();

        let sum = result.stats.num_1x1 + 2 * result.stats.num_2x2 + result.stats.num_delayed;
        assert_eq!(sum, 5, "statistics sum {} != n=5", sum);
    }

    #[test]
    fn test_2x2_pivot_known_indefinite() {
        let a = symmetric_matrix(4, |i, j| {
            let vals = [
                [0.01, 10.0, 0.0, 0.0],
                [10.0, 0.01, 0.0, 0.0],
                [0.0, 0.0, 5.0, 1.0],
                [0.0, 0.0, 1.0, 3.0],
            ];
            vals[i][j]
        });

        let opts = AptpOptions {
            fallback: AptpFallback::BunchKaufman,
            ..AptpOptions::default()
        };
        let result = aptp_factor(a.as_ref(), &opts).unwrap();

        assert!(
            result.stats.num_2x2 >= 1,
            "expected 2x2 pivot, got num_2x2={}",
            result.stats.num_2x2
        );

        let error =
            dense_reconstruction_error(&a, &result.l, &result.d, result.perm.as_ref().arrays().0);
        assert!(error < 1e-12, "reconstruction error {:.2e} >= 1e-12", error);
    }

    #[test]
    fn test_2x2_stability_test() {
        let a_good = symmetric_matrix(2, |i, j| {
            let vals = [[1.0, 5.0], [5.0, 1.0]];
            vals[i][j]
        });
        let opts = AptpOptions {
            fallback: AptpFallback::BunchKaufman,
            ..AptpOptions::default()
        };
        let result = aptp_factor(a_good.as_ref(), &opts).unwrap();
        let sum = result.stats.num_1x1 + 2 * result.stats.num_2x2 + result.stats.num_delayed;
        assert_eq!(sum, 2);
    }

    #[test]
    fn test_bk_vs_delay_fallback() {
        let a = symmetric_matrix(4, |i, j| {
            let vals = [
                [0.01, 10.0, 0.0, 0.0],
                [10.0, 0.01, 0.0, 0.0],
                [0.0, 0.0, 5.0, 1.0],
                [0.0, 0.0, 1.0, 3.0],
            ];
            vals[i][j]
        });

        let bk_opts = AptpOptions {
            fallback: AptpFallback::BunchKaufman,
            ..AptpOptions::default()
        };
        let delay_opts = AptpOptions {
            fallback: AptpFallback::Delay,
            ..AptpOptions::default()
        };

        let bk_result = aptp_factor(a.as_ref(), &bk_opts).unwrap();
        let delay_result = aptp_factor(a.as_ref(), &delay_opts).unwrap();

        assert!(
            bk_result.stats.num_delayed <= delay_result.stats.num_delayed,
            "BK delayed {} > Delay delayed {}",
            bk_result.stats.num_delayed,
            delay_result.stats.num_delayed
        );

        if bk_result.stats.num_delayed == 0 {
            let error = dense_reconstruction_error(
                &a,
                &bk_result.l,
                &bk_result.d,
                bk_result.perm.as_ref().arrays().0,
            );
            assert!(error < 1e-12, "BK reconstruction error {:.2e}", error);
        }
    }

    #[test]
    fn test_strict_threshold() {
        let a = symmetric_matrix(4, |i, j| {
            let vals = [
                [4.0, 2.0, 1.0, 0.5],
                [2.0, 5.0, 2.0, 1.0],
                [1.0, 2.0, 6.0, 2.0],
                [0.5, 1.0, 2.0, 7.0],
            ];
            vals[i][j]
        });

        let loose = AptpOptions {
            threshold: 0.01,
            fallback: AptpFallback::Delay,
            ..AptpOptions::default()
        };
        let strict = AptpOptions {
            threshold: 0.5,
            fallback: AptpFallback::Delay,
            ..AptpOptions::default()
        };

        let loose_result = aptp_factor(a.as_ref(), &loose).unwrap();
        let strict_result = aptp_factor(a.as_ref(), &strict).unwrap();

        assert!(
            strict_result.stats.num_delayed >= loose_result.stats.num_delayed,
            "strict delayed {} < loose delayed {}",
            strict_result.stats.num_delayed,
            loose_result.stats.num_delayed,
        );
    }

    #[test]
    fn test_permutation_valid() {
        let a = symmetric_matrix(4, |i, j| {
            let vals = [
                [0.01, 10.0, 0.0, 0.0],
                [10.0, 0.01, 0.0, 0.0],
                [0.0, 0.0, 5.0, 1.0],
                [0.0, 0.0, 1.0, 3.0],
            ];
            vals[i][j]
        });

        let opts = AptpOptions {
            fallback: AptpFallback::BunchKaufman,
            ..AptpOptions::default()
        };
        let result = aptp_factor(a.as_ref(), &opts).unwrap();

        let (fwd, inv) = result.perm.as_ref().arrays();
        let n = fwd.len();
        assert_eq!(n, 4);
        let mut seen = vec![false; n];
        for &v in fwd {
            assert!(v < n, "perm value {} >= n={}", v, n);
            assert!(!seen[v], "duplicate perm value {}", v);
            seen[v] = true;
        }
        for i in 0..n {
            assert_eq!(inv[fwd[i]], i);
            assert_eq!(fwd[inv[i]], i);
        }
    }

    #[test]
    fn test_pd_statistics() {
        let a = symmetric_matrix(4, |i, j| {
            let vals = [
                [10.0, 1.0, 0.0, 0.0],
                [1.0, 20.0, 2.0, 0.0],
                [0.0, 2.0, 30.0, 3.0],
                [0.0, 0.0, 3.0, 40.0],
            ];
            vals[i][j]
        });

        let opts = AptpOptions::default();
        let result = aptp_factor(a.as_ref(), &opts).unwrap();

        // All columns eliminated, no delays
        assert_eq!(result.stats.num_1x1 + 2 * result.stats.num_2x2, 4);
        assert_eq!(result.stats.num_delayed, 0);
        assert!(result.stats.max_l_entry < 1.0 / opts.threshold);
    }

    #[test]
    fn test_max_l_entry_accuracy() {
        let a = symmetric_matrix(4, |i, j| {
            let vals = [
                [4.0, 2.0, 1.0, 0.5],
                [2.0, 5.0, 2.0, 1.0],
                [1.0, 2.0, 6.0, 2.0],
                [0.5, 1.0, 2.0, 7.0],
            ];
            vals[i][j]
        });

        let opts = AptpOptions::default();
        let result = aptp_factor(a.as_ref(), &opts).unwrap();

        let n = result.l.nrows();
        let mut actual_max = 0.0_f64;
        for j in 0..n {
            for i in (j + 1)..n {
                let val = result.l[(i, j)].abs();
                if val > actual_max {
                    actual_max = val;
                }
            }
        }

        assert!(
            (result.stats.max_l_entry - actual_max).abs() < 1e-14,
            "stats.max_l_entry={:.6e}, actual={:.6e}",
            result.stats.max_l_entry,
            actual_max
        );
    }

    #[test]
    fn test_pivot_log_completeness() {
        let a = symmetric_matrix(4, |i, j| {
            let vals = [
                [4.0, 2.0, 1.0, 0.5],
                [2.0, 5.0, 2.0, 1.0],
                [1.0, 2.0, 6.0, 2.0],
                [0.5, 1.0, 2.0, 7.0],
            ];
            vals[i][j]
        });

        let opts = AptpOptions::default();
        let result = aptp_factor(a.as_ref(), &opts).unwrap();

        assert_eq!(result.pivot_log.len(), 4);
    }

    #[test]
    fn test_inertia_from_d() {
        let a = symmetric_matrix(5, |i, j| {
            if i == j {
                [3.0, -2.0, 1.0, -4.0, 5.0][i]
            } else {
                0.0
            }
        });

        let opts = AptpOptions {
            fallback: AptpFallback::Delay,
            ..AptpOptions::default()
        };
        let result = aptp_factor(a.as_ref(), &opts).unwrap();

        assert_eq!(result.stats.num_delayed, 0, "should have no delays");

        let inertia = result.d.compute_inertia();
        assert_eq!(inertia.positive, 3, "expected 3 positive");
        assert_eq!(inertia.negative, 2, "expected 2 negative");
        assert_eq!(inertia.zero, 0, "expected 0 zero");
    }

    // ---- Polish tests ----

    #[test]
    fn test_partial_factorization() {
        let n = 8;
        let p = 4;
        let a = symmetric_matrix(n, |i, j| {
            if i == j {
                10.0 + i as f64
            } else {
                1.0 / (1.0 + (i as f64 - j as f64).abs())
            }
        });

        let opts = AptpOptions::default();
        let mut a_copy = a.clone();
        let result = aptp_factor_in_place(a_copy.as_mut(), p, &opts).unwrap();

        let sum = result.stats.num_1x1 + 2 * result.stats.num_2x2 + result.stats.num_delayed;
        assert_eq!(sum, p, "statistics should sum to num_fully_summed={}", p);
    }

    #[test]
    fn test_edge_case_extreme_thresholds() {
        let a = symmetric_matrix(3, |i, j| {
            let vals = [[4.0, 2.0, 1.0], [2.0, 5.0, 2.0], [1.0, 2.0, 6.0]];
            vals[i][j]
        });

        let loose = AptpOptions {
            threshold: 0.001,
            fallback: AptpFallback::Delay,
            ..AptpOptions::default()
        };
        let result_loose = aptp_factor(a.as_ref(), &loose).unwrap();
        assert_eq!(result_loose.stats.num_delayed, 0);

        let strict = AptpOptions {
            threshold: 1.0,
            fallback: AptpFallback::Delay,
            ..AptpOptions::default()
        };
        let result_strict = aptp_factor(a.as_ref(), &strict).unwrap();
        let sum = result_strict.stats.num_1x1
            + 2 * result_strict.stats.num_2x2
            + result_strict.stats.num_delayed;
        assert_eq!(sum, 3);
    }

    #[test]
    fn test_both_fallback_strategies_valid() {
        let matrices = [
            symmetric_matrix(3, |i, j| {
                let vals = [[1.0, 2.0, 0.0], [2.0, -1.0, 1.0], [0.0, 1.0, 3.0]];
                vals[i][j]
            }),
            symmetric_matrix(4, |i, j| {
                let vals = [
                    [0.01, 10.0, 0.0, 0.0],
                    [10.0, 0.01, 0.0, 0.0],
                    [0.0, 0.0, 5.0, 1.0],
                    [0.0, 0.0, 1.0, 3.0],
                ];
                vals[i][j]
            }),
        ];

        for (idx, a) in matrices.iter().enumerate() {
            for fallback in [AptpFallback::BunchKaufman, AptpFallback::Delay] {
                let opts = AptpOptions {
                    fallback,
                    ..AptpOptions::default()
                };
                let result = aptp_factor(a.as_ref(), &opts).unwrap();

                if result.stats.num_delayed == 0 {
                    let error = dense_reconstruction_error(
                        a,
                        &result.l,
                        &result.d,
                        result.perm.as_ref().arrays().0,
                    );
                    assert!(
                        error < 1e-12,
                        "matrix {} {:?}: error {:.2e}",
                        idx,
                        fallback,
                        error
                    );
                }

                let sum =
                    result.stats.num_1x1 + 2 * result.stats.num_2x2 + result.stats.num_delayed;
                let n = a.nrows();
                assert_eq!(sum, n, "statistics invariant broken for matrix {}", idx);
            }
        }
    }

    // ---- Input validation tests ----

    #[test]
    fn test_invalid_non_square() {
        let a = Mat::zeros(3, 4);
        let opts = AptpOptions::default();
        let result = aptp_factor(a.as_ref(), &opts);
        assert!(matches!(result, Err(SparseError::InvalidInput { .. })));
    }

    #[test]
    fn test_invalid_threshold() {
        let a = Mat::zeros(2, 2);
        let opts = AptpOptions {
            threshold: 0.0,
            ..AptpOptions::default()
        };
        let result = aptp_factor(a.as_ref(), &opts);
        assert!(matches!(result, Err(SparseError::InvalidInput { .. })));

        let opts2 = AptpOptions {
            threshold: 1.5,
            ..AptpOptions::default()
        };
        let result2 = aptp_factor(a.as_ref(), &opts2);
        assert!(matches!(result2, Err(SparseError::InvalidInput { .. })));
    }

    #[test]
    fn test_invalid_num_fully_summed_too_large() {
        let mut a = Mat::zeros(3, 3);
        let opts = AptpOptions::default();
        let result = aptp_factor_in_place(a.as_mut(), 5, &opts);
        assert!(matches!(result, Err(SparseError::InvalidInput { .. })));
    }

    // ---- Edge case and regression tests ----

    #[test]
    fn test_zero_fully_summed() {
        let a = symmetric_matrix(3, |i, j| {
            let vals = [[4.0, 2.0, 1.0], [2.0, 5.0, 3.0], [1.0, 3.0, 6.0]];
            vals[i][j]
        });

        let opts = AptpOptions::default();
        let mut a_copy = a.clone();
        let result = aptp_factor_in_place(a_copy.as_mut(), 0, &opts).unwrap();

        assert_eq!(result.num_eliminated, 0);
        assert_eq!(result.stats.num_1x1, 0);
        assert_eq!(result.stats.num_2x2, 0);
        assert_eq!(result.stats.num_delayed, 0);
        assert!(result.pivot_log.is_empty());
    }

    #[test]
    fn test_2x2_fallback_also_fails() {
        // With complete pivoting, the max entry is 12.0 on diagonal (1,1).
        // Complete pivoting starts with the largest entry and can handle
        // matrices that the old threshold-based approach would delay.
        let a = symmetric_matrix(3, |i, j| {
            let vals = [[0.001, 0.11, 0.0], [0.11, 12.0, 0.1], [0.0, 0.1, 5.0]];
            vals[i][j]
        });

        let opts = AptpOptions {
            fallback: AptpFallback::BunchKaufman,
            ..AptpOptions::default()
        };
        let result = aptp_factor(a.as_ref(), &opts).unwrap();

        // Complete pivoting eliminates all columns successfully
        let sum = result.stats.num_1x1 + 2 * result.stats.num_2x2 + result.stats.num_delayed;
        assert_eq!(sum, 3, "statistics sum {} != n=3", sum);

        // Verify correctness via reconstruction
        if result.stats.num_delayed == 0 {
            let error = dense_reconstruction_error(
                &a,
                &result.l,
                &result.d,
                result.perm.as_ref().arrays().0,
            );
            assert!(error < 1e-12, "reconstruction error {:.2e}", error);
        }

        // L entries bounded by 4 (complete pivoting guarantee)
        assert!(
            result.stats.max_l_entry <= COMPLETE_PIVOTING_GROWTH_BOUND + 1e-10,
            "max_l_entry {} > 4",
            result.stats.max_l_entry
        );
    }

    #[test]
    fn test_invalid_small_negative() {
        let a = Mat::zeros(2, 2);
        let opts = AptpOptions {
            small: -1.0,
            ..AptpOptions::default()
        };
        let result = aptp_factor(a.as_ref(), &opts);
        assert!(matches!(result, Err(SparseError::InvalidInput { .. })));
    }

    // ---- Random matrix stress tests ----

    #[cfg(feature = "test-util")]
    mod stress_tests {
        use super::*;
        use crate::testing::generators::{RandomMatrixConfig, generate_random_symmetric};
        use rand::SeedableRng;
        use rand::rngs::StdRng;

        /// Generate a dense symmetric PD matrix from the sparse generator.
        fn random_dense_pd(n: usize, rng: &mut impl rand::Rng) -> Mat<f64> {
            let config = RandomMatrixConfig {
                size: n,
                target_nnz: n * n / 2,
                positive_definite: true,
            };
            generate_random_symmetric(&config, rng).unwrap().to_dense()
        }

        /// Generate a dense symmetric indefinite matrix from the sparse generator.
        fn random_dense_indefinite(n: usize, rng: &mut impl rand::Rng) -> Mat<f64> {
            let config = RandomMatrixConfig {
                size: n,
                target_nnz: n * n / 2,
                positive_definite: false,
            };
            generate_random_symmetric(&config, rng).unwrap().to_dense()
        }

        #[test]
        fn test_random_pd_matrices() {
            let mut rng = StdRng::seed_from_u64(42);
            let opts = AptpOptions::default();
            let sizes = [5, 10, 20, 50];
            let mut total = 0;

            for &n in &sizes {
                for _ in 0..25 {
                    let a = random_dense_pd(n, &mut rng);
                    let result = aptp_factor(a.as_ref(), &opts).unwrap();

                    assert_eq!(
                        result.stats.num_delayed, 0,
                        "PD matrix {}x{} should have zero delays",
                        n, n
                    );
                    let total_elim = result.stats.num_1x1 + 2 * result.stats.num_2x2;
                    assert_eq!(
                        total_elim, n,
                        "PD matrix {}x{} should eliminate all columns (1x1={}, 2x2={})",
                        n, n, result.stats.num_1x1, result.stats.num_2x2
                    );

                    let error = dense_reconstruction_error(
                        &a,
                        &result.l,
                        &result.d,
                        result.perm.as_ref().arrays().0,
                    );
                    assert!(
                        error < 1e-12,
                        "PD {}x{}: reconstruction error {:.2e}",
                        n,
                        n,
                        error
                    );
                    total += 1;
                }
            }
            assert!(total >= 100, "ran {} PD tests, need >= 100", total);
        }

        #[test]
        fn test_random_indefinite_matrices() {
            let mut rng = StdRng::seed_from_u64(123);
            let opts = AptpOptions {
                fallback: AptpFallback::BunchKaufman,
                ..AptpOptions::default()
            };
            let sizes = [5, 10, 20, 50];
            let mut total = 0;

            for &n in &sizes {
                for _ in 0..25 {
                    let a = random_dense_indefinite(n, &mut rng);
                    let result = aptp_factor(a.as_ref(), &opts).unwrap();

                    // Statistics invariant
                    let sum =
                        result.stats.num_1x1 + 2 * result.stats.num_2x2 + result.stats.num_delayed;
                    assert_eq!(sum, n, "stats invariant for {}x{}", n, n);

                    // Stability bound
                    assert!(
                        result.stats.max_l_entry < 1.0 / opts.threshold,
                        "stability bound violated for {}x{}",
                        n,
                        n
                    );

                    // Reconstruction (only if fully eliminated)
                    if result.stats.num_delayed == 0 {
                        let error = dense_reconstruction_error(
                            &a,
                            &result.l,
                            &result.d,
                            result.perm.as_ref().arrays().0,
                        );
                        assert!(
                            error < 1e-12,
                            "indefinite {}x{}: reconstruction error {:.2e}",
                            n,
                            n,
                            error
                        );
                    }
                    total += 1;
                }
            }
            assert!(total >= 100, "ran {} indefinite tests, need >= 100", total);
        }
    }

    // ---- Integration tests with test data ----

    #[cfg(feature = "test-util")]
    mod integration_tests {
        use super::*;
        use crate::testing::cases::{TestCaseFilter, load_test_cases};

        #[test]
        #[ignore] // Requires test-data/ on disk
        fn test_hand_constructed_matrices() {
            let cases = load_test_cases(&TestCaseFilter::hand_constructed())
                .expect("failed to load hand-constructed matrices");
            assert_eq!(cases.len(), 15, "expected 15 hand-constructed matrices");

            let opts = AptpOptions::default();
            let mut passed = 0;

            for case in &cases {
                let dense = case.matrix.to_dense();
                let n = dense.nrows();

                // Skip very large matrices (dense APTP is O(n^3))
                if n > 500 {
                    continue;
                }

                let result = aptp_factor(dense.as_ref(), &opts).unwrap();

                let sum =
                    result.stats.num_1x1 + 2 * result.stats.num_2x2 + result.stats.num_delayed;
                assert_eq!(sum, n, "{}: stats invariant failed", case.name);

                if result.stats.num_delayed == 0 {
                    let error = dense_reconstruction_error(
                        &dense,
                        &result.l,
                        &result.d,
                        result.perm.as_ref().arrays().0,
                    );
                    assert!(
                        error < 1e-12,
                        "{}: reconstruction error {:.2e}",
                        case.name,
                        error
                    );
                }
                passed += 1;
            }
            assert!(passed >= 10, "only {} hand-constructed passed", passed);
        }

        #[test]
        #[ignore] // Requires SuiteSparse CI-subset on disk
        fn test_suitesparse_ci_dense() {
            let cases = load_test_cases(&TestCaseFilter::ci_subset())
                .expect("failed to load CI-subset matrices");

            let opts = AptpOptions::default();
            let mut tested = 0;

            for case in &cases {
                let n = case.matrix.nrows();

                // Only test matrices small enough for dense factorization.
                // Dense O(n^2) memory + O(n^3) time: cap at 200 to avoid OOM.
                if n > 200 {
                    continue;
                }

                let dense = case.matrix.to_dense();

                let result = aptp_factor(dense.as_ref(), &opts).unwrap();

                let sum =
                    result.stats.num_1x1 + 2 * result.stats.num_2x2 + result.stats.num_delayed;
                assert_eq!(sum, n, "{}: stats invariant failed", case.name);

                if result.stats.num_delayed == 0 {
                    let error = dense_reconstruction_error(
                        &dense,
                        &result.l,
                        &result.d,
                        result.perm.as_ref().arrays().0,
                    );
                    assert!(
                        error < 1e-12,
                        "{}: reconstruction error {:.2e}",
                        case.name,
                        error
                    );
                }
                tested += 1;
            }
            assert!(
                tested > 0,
                "no CI-subset matrices small enough for dense test"
            );
        }
    }

    // ---- factor_inner tests ----

    #[test]
    fn test_factor_inner_reconstruction_moderate() {
        // factor_inner on matrices of moderate size (128, 256)
        // verifying reconstruction < 1e-12.
        let sizes = [64, 128, 256];
        let opts = AptpOptions::default();

        for &n in &sizes {
            let a = symmetric_matrix(n, |i, j| {
                let seed = (i * 1000 + j * 7 + 13) as f64;
                let val = (seed * 0.618033988749).fract() * 2.0 - 1.0;
                if i == j { val * 10.0 } else { val }
            });

            let result = aptp_factor(a.as_ref(), &opts).unwrap();

            // Statistics invariant
            assert_eq!(
                result.stats.num_1x1 + 2 * result.stats.num_2x2 + result.stats.num_delayed,
                n,
                "factor_inner {}x{}: statistics invariant",
                n,
                n
            );

            if result.stats.num_delayed == 0 {
                let error = dense_reconstruction_error(
                    &a,
                    &result.l,
                    &result.d,
                    result.perm.as_ref().arrays().0,
                );
                assert!(
                    error < 1e-12,
                    "factor_inner {}x{}: reconstruction error {:.2e} >= 1e-12",
                    n,
                    n,
                    error
                );
            }
        }
    }

    #[test]
    fn test_factor_inner_partial_factorization() {
        // factor_inner with num_fully_summed < m (contribution block present)
        let n = 64;
        let p = 48; // Only factor 48 of 64 columns
        let a = symmetric_matrix(n, |i, j| {
            let seed = (i * 1000 + j * 7 + 13) as f64;
            let val = (seed * 0.618033988749).fract() * 2.0 - 1.0;
            if i == j { val * 10.0 } else { val }
        });

        let opts = AptpOptions::default();
        let mut a_copy = a.to_owned();
        let result = aptp_factor_in_place(a_copy.as_mut(), p, &opts).unwrap();

        // Should have eliminated <= p columns
        assert!(
            result.num_eliminated <= p,
            "eliminated {} > p={}",
            result.num_eliminated,
            p
        );

        // Statistics should account for all p columns
        assert_eq!(
            result.stats.num_1x1 + 2 * result.stats.num_2x2 + result.stats.num_delayed,
            p,
            "statistics invariant for partial factorization"
        );
    }

    // ---- Two-level integration tests ----

    #[test]
    fn test_two_level_dispatch_small_block_size() {
        // Test the two-level dispatch by setting outer_block_size small
        // so matrices of moderate size trigger two_level_factor.
        let sizes = [33, 64, 100];
        let opts = AptpOptions {
            outer_block_size: 32,
            inner_block_size: 8,
            ..AptpOptions::default()
        };

        for &n in &sizes {
            let a = symmetric_matrix(n, |i, j| {
                let seed = (i * 1000 + j * 7 + 13) as f64;
                let val = (seed * 0.618033988749).fract() * 2.0 - 1.0;
                if i == j { val * 10.0 } else { val }
            });

            let result = aptp_factor(a.as_ref(), &opts).unwrap();

            assert_eq!(
                result.stats.num_1x1 + 2 * result.stats.num_2x2 + result.stats.num_delayed,
                n,
                "two-level {}x{}: statistics invariant",
                n,
                n
            );

            if result.stats.num_delayed == 0 {
                let error = dense_reconstruction_error(
                    &a,
                    &result.l,
                    &result.d,
                    result.perm.as_ref().arrays().0,
                );
                assert!(
                    error < 1e-12,
                    "two-level {}x{}: reconstruction error {:.2e} >= 1e-12",
                    n,
                    n,
                    error
                );
            }
        }
    }

    #[test]
    fn test_two_level_single_outer_block() {
        // frontal dimension == nb → single outer block, equivalent to factor_inner
        let n = 32;
        let opts = AptpOptions {
            outer_block_size: 32,
            inner_block_size: 8,
            ..AptpOptions::default()
        };

        let a = symmetric_matrix(n, |i, j| {
            let seed = (i * 1000 + j * 7 + 13) as f64;
            let val = (seed * 0.618033988749).fract() * 2.0 - 1.0;
            if i == j { val * 10.0 } else { val }
        });

        let result = aptp_factor(a.as_ref(), &opts).unwrap();
        if result.stats.num_delayed == 0 {
            let error = dense_reconstruction_error(
                &a,
                &result.l,
                &result.d,
                result.perm.as_ref().arrays().0,
            );
            assert!(error < 1e-12, "single block: error {:.2e}", error);
        }
    }

    #[test]
    fn test_two_level_boundary_nb_plus_1() {
        // frontal dimension == nb+1 → two blocks, second block has 1 column
        let n = 33;
        let opts = AptpOptions {
            outer_block_size: 32,
            inner_block_size: 8,
            ..AptpOptions::default()
        };

        let a = symmetric_matrix(n, |i, j| {
            let seed = (i * 1000 + j * 7 + 13) as f64;
            let val = (seed * 0.618033988749).fract() * 2.0 - 1.0;
            if i == j { val * 10.0 } else { val }
        });

        let result = aptp_factor(a.as_ref(), &opts).unwrap();
        assert_eq!(
            result.stats.num_1x1 + 2 * result.stats.num_2x2 + result.stats.num_delayed,
            n,
        );
        if result.stats.num_delayed == 0 {
            let error = dense_reconstruction_error(
                &a,
                &result.l,
                &result.d,
                result.perm.as_ref().arrays().0,
            );
            assert!(error < 1e-12, "nb+1 boundary: error {:.2e}", error);
        }
    }

    #[test]
    fn test_two_level_partial_factorization() {
        // partial factorization (num_fully_summed < m) with dimension > nb
        // This triggers two_level_factor with a contribution block.
        let n = 80;
        let p = 50; // > outer_block_size=32, triggers two-level
        let opts = AptpOptions {
            outer_block_size: 32,
            inner_block_size: 8,
            ..AptpOptions::default()
        };

        let a = symmetric_matrix(n, |i, j| {
            let seed = (i * 1000 + j * 7 + 13) as f64;
            let val = (seed * 0.618033988749).fract() * 2.0 - 1.0;
            if i == j { val * 10.0 } else { val }
        });

        let mut a_copy = a.to_owned();
        let result = aptp_factor_in_place(a_copy.as_mut(), p, &opts).unwrap();

        assert!(result.num_eliminated <= p);
        assert_eq!(
            result.stats.num_1x1 + 2 * result.stats.num_2x2 + result.stats.num_delayed,
            p,
        );
    }

    #[test]
    fn test_two_level_vs_single_level_equivalence() {
        // Verify that two-level and single-level produce equivalent reconstruction
        // error on the same matrix (by setting outer_block_size to force each path).
        let n = 64;
        let a = symmetric_matrix(n, |i, j| {
            let seed = (i * 1000 + j * 7 + 13) as f64;
            let val = (seed * 0.618033988749).fract() * 2.0 - 1.0;
            if i == j { val * 10.0 } else { val }
        });

        // Single-level: outer_block_size >= n
        let opts_single = AptpOptions {
            outer_block_size: 256,
            inner_block_size: 32,
            ..AptpOptions::default()
        };
        let result_single = aptp_factor(a.as_ref(), &opts_single).unwrap();

        // Two-level: outer_block_size < n
        let opts_two = AptpOptions {
            outer_block_size: 16,
            inner_block_size: 8,
            ..AptpOptions::default()
        };
        let result_two = aptp_factor(a.as_ref(), &opts_two).unwrap();

        // Both should achieve reconstruction < 1e-12
        if result_single.stats.num_delayed == 0 {
            let err_s = dense_reconstruction_error(
                &a,
                &result_single.l,
                &result_single.d,
                result_single.perm.as_ref().arrays().0,
            );
            assert!(err_s < 1e-12, "single-level error {:.2e}", err_s);
        }
        if result_two.stats.num_delayed == 0 {
            let err_t = dense_reconstruction_error(
                &a,
                &result_two.l,
                &result_two.d,
                result_two.perm.as_ref().arrays().0,
            );
            assert!(err_t < 1e-12, "two-level error {:.2e}", err_t);
        }
    }

    #[test]
    fn test_two_level_vs_unblocked_reconstruction() {
        // Regression test: col_order tracking corruption in two_level_factor when
        // block_perm was applied after delay swap, causing incorrect column mapping.
        //
        // When delay swaps happened BEFORE block_perm was applied to col_order,
        // the permutation mapping was corrupted, causing massive reconstruction
        // errors with small outer_block_size. This test verifies that both
        // two-level (ob=128, forcing 4 outer iterations) and unblocked (ob=huge)
        // paths produce equivalent, accurate reconstruction.
        let n = 512;

        // Build a symmetric indefinite matrix with small diagonal entries to force
        // 2x2 pivots and some delays. Seed = golden ratio hash for reproducibility.
        let a = symmetric_matrix(n, |i, j| {
            let seed = (i * 997 + j * 1013 + 42) as f64;
            let val = (seed * 0.618033988749).fract() * 2.0 - 1.0;
            if i == j {
                // Small diagonal → more 2x2 pivots and delays
                val * 0.5
            } else {
                val
            }
        });

        // Unblocked: outer_block_size >= n → single factor_inner call
        let opts_unblocked = AptpOptions {
            outer_block_size: 100_000,
            inner_block_size: 32,
            ..AptpOptions::default()
        };
        let result_unblocked = aptp_factor(a.as_ref(), &opts_unblocked).unwrap();
        let err_unblocked = dense_reconstruction_error(
            &a,
            &result_unblocked.l,
            &result_unblocked.d,
            result_unblocked.perm.as_ref().arrays().0,
        );
        assert!(
            err_unblocked < 1e-12,
            "unblocked reconstruction error {:.2e} exceeds 1e-12",
            err_unblocked,
        );

        // Two-level: outer_block_size=128 → ~4 outer iterations
        let opts_two_level = AptpOptions {
            outer_block_size: 128,
            inner_block_size: 32,
            ..AptpOptions::default()
        };
        let result_two_level = aptp_factor(a.as_ref(), &opts_two_level).unwrap();
        let err_two_level = dense_reconstruction_error(
            &a,
            &result_two_level.l,
            &result_two_level.d,
            result_two_level.perm.as_ref().arrays().0,
        );
        assert!(
            err_two_level < 1e-12,
            "two-level reconstruction error {:.2e} exceeds 1e-12 \
             (unblocked was {:.2e})",
            err_two_level,
            err_unblocked,
        );

        // Two-level error should be within 10x of unblocked
        let ratio = if err_unblocked > 0.0 {
            err_two_level / err_unblocked
        } else {
            1.0
        };
        assert!(
            ratio < 10.0,
            "two-level error ({:.2e}) is {:.1}x worse than unblocked ({:.2e})",
            err_two_level,
            ratio,
            err_unblocked,
        );
    }

    // ---- BLAS-3 pipeline tests ----

    #[test]
    fn test_factor_block_diagonal_basic() {
        // Factor 8×8 identity with block_size=4.
        // D=[1,1,1,1], identity permutation, no L entries within block.
        let mut a = Mat::identity(8, 8);
        let (d, perm, nelim, stats, _log) = factor_block_diagonal(a.as_mut(), 0, 4, 1e-20, 4);

        assert_eq!(nelim, 4);
        assert_eq!(stats.num_1x1, 4);
        assert_eq!(stats.num_2x2, 0);
        assert_eq!(stats.num_delayed, 0);
        assert!(stats.max_l_entry < 1e-14);

        // D should be [1, 1, 1, 1]
        for i in 0..4 {
            assert!((d.diagonal_1x1(i) - 1.0).abs() < 1e-14);
        }

        // Identity permutation
        assert_eq!(perm, vec![0, 1, 2, 3]);

        // Panel rows (4-7) should be untouched (still identity entries)
        for i in 4..8 {
            for j in 0..4 {
                assert!(
                    a[(i, j)].abs() < 1e-14,
                    "panel entry ({},{}) should be 0, got {}",
                    i,
                    j,
                    a[(i, j)]
                );
            }
        }
    }

    #[test]
    fn test_factor_block_diagonal_block_scoped_swap() {
        // 8×8 matrix where max entry in block forces a swap.
        // With block-scoped swaps (row_limit=4), panel rows (4-7) should NOT be
        // affected by factor_block_diagonal. Panel permutation is handled separately.
        let mut a = symmetric_matrix(8, |i, j| {
            if i == j {
                [1.0, 5.0, 2.0, 3.0, 0.1, 0.1, 0.1, 0.1][i]
            } else if (i == 4 && j == 1) || (i == 1 && j == 4) {
                // Panel entry at (4,1)
                0.99
            } else {
                0.0
            }
        });

        // Save panel row 4's original column values before factor
        let panel_row_before: Vec<f64> = (0..4).map(|j| a[(4, j)]).collect();

        let (_d, perm, nelim, _stats, _log) = factor_block_diagonal(a.as_mut(), 0, 4, 1e-20, 4);

        assert!(nelim > 0, "should eliminate at least 1 column");

        // The max diagonal is 5.0 at column 1, so it should be swapped to position 0.
        assert_eq!(
            perm[0], 1,
            "first pivot should be original column 1 (max diag = 5.0)"
        );

        // Panel rows should be UNCHANGED (block-scoped swap with row_limit=4)
        for j in 0..4 {
            assert!(
                (a[(4, j)] - panel_row_before[j]).abs() < 1e-14,
                "panel row (4,{}) should be unchanged: got {}, expected {}",
                j,
                a[(4, j)],
                panel_row_before[j]
            );
        }
    }

    #[test]
    fn test_blas3_pipeline_reconstruction() {
        // Full Factor→Apply→Update on an 8×8 matrix via factor_inner.
        // Verify reconstruction ||PAP^T - LDL^T|| < 1e-12.
        let a = symmetric_matrix(8, |i, j| {
            if i == j {
                10.0 + i as f64
            } else {
                1.0 / (1.0 + (i as f64 - j as f64).abs())
            }
        });

        let opts = AptpOptions {
            inner_block_size: 4, // Force 2 blocks for 8×8
            ..AptpOptions::default()
        };
        let result = aptp_factor(a.as_ref(), &opts).unwrap();

        assert_eq!(result.stats.num_delayed, 0, "should have no delays");

        let error =
            dense_reconstruction_error(&a, &result.l, &result.d, result.perm.as_ref().arrays().0);
        assert!(
            error < 1e-12,
            "BLAS-3 pipeline reconstruction error {:.2e} >= 1e-12",
            error
        );
    }

    #[test]
    fn test_blas3_threshold_failure_and_retry() {
        // Construct matrix where panel threshold is likely to fail for at least
        // one column, forcing backup/restore/delay/re-factor.
        // Use very strict threshold to trigger failures.
        let a = symmetric_matrix(8, |i, j| {
            let vals = [
                [1e-3, 1.0, 0.5, 0.5, 0.1, 0.1, 0.1, 0.1],
                [1.0, 1e-3, 0.5, 0.5, 0.1, 0.1, 0.1, 0.1],
                [0.5, 0.5, 10.0, 1.0, 0.1, 0.1, 0.1, 0.1],
                [0.5, 0.5, 1.0, 10.0, 0.1, 0.1, 0.1, 0.1],
                [0.1, 0.1, 0.1, 0.1, 5.0, 0.5, 0.0, 0.0],
                [0.1, 0.1, 0.1, 0.1, 0.5, 6.0, 0.0, 0.0],
                [0.1, 0.1, 0.1, 0.1, 0.0, 0.0, 7.0, 0.5],
                [0.1, 0.1, 0.1, 0.1, 0.0, 0.0, 0.5, 8.0],
            ];
            vals[i][j]
        });

        let opts = AptpOptions {
            inner_block_size: 4,
            threshold: 0.01, // Standard threshold
            ..AptpOptions::default()
        };
        let result = aptp_factor(a.as_ref(), &opts).unwrap();

        // Statistics invariant
        let sum = result.stats.num_1x1 + 2 * result.stats.num_2x2 + result.stats.num_delayed;
        assert_eq!(sum, 8, "statistics sum {} != 8", sum);

        // Reconstruction (if fully eliminated)
        if result.stats.num_delayed == 0 {
            let error = dense_reconstruction_error(
                &a,
                &result.l,
                &result.d,
                result.perm.as_ref().arrays().0,
            );
            assert!(
                error < 1e-12,
                "threshold failure retry: reconstruction error {:.2e}",
                error
            );
        }
    }

    #[test]
    fn test_adjust_for_2x2_boundary() {
        // Test that adjust_for_2x2_boundary decrements when last accepted pivot
        // is the first half of a 2×2 pair.
        let mut d = MixedDiagonal::new(4);
        d.set_1x1(0, 1.0);
        d.set_2x2(Block2x2 {
            first_col: 1,
            a: 1.0,
            b: 0.5,
            c: 2.0,
        });
        d.set_1x1(3, 3.0);

        // If effective_nelim = 2, last is position 1 which is first of 2×2 pair (1,2).
        // Partner = 2 > 1, so should decrement to 1.
        assert_eq!(adjust_for_2x2_boundary(2, &d), 1);

        // If effective_nelim = 3, last is position 2 which is second of 2×2 pair.
        // Partner = 1 < 2, so no adjustment.
        assert_eq!(adjust_for_2x2_boundary(3, &d), 3);

        // If effective_nelim = 1, last is position 0 which is 1×1. No adjustment.
        assert_eq!(adjust_for_2x2_boundary(1, &d), 1);

        // If effective_nelim = 4, last is position 3 which is 1×1. No adjustment.
        assert_eq!(adjust_for_2x2_boundary(4, &d), 4);

        // Edge case: effective_nelim = 0
        assert_eq!(adjust_for_2x2_boundary(0, &d), 0);
    }

    #[test]
    fn test_blas3_full_block_singular() {
        // Block where all entries < small. Verify all columns delayed.
        let mut a = Mat::zeros(8, 8);
        for i in 0..8 {
            a[(i, i)] = 1e-25;
        }

        let opts = AptpOptions {
            inner_block_size: 4,
            failed_pivot_method: FailedPivotMethod::Pass,
            ..AptpOptions::default()
        };
        let mut a_copy = a.clone();
        let result = aptp_factor_in_place(a_copy.as_mut(), 8, &opts).unwrap();

        assert_eq!(result.num_eliminated, 0);
        assert_eq!(result.stats.num_delayed, 8);
        assert_eq!(result.delayed_cols.len(), 8);
    }

    // ---- Regression test: factor_inner with threshold failures (delays) ----

    /// Generate a deterministic pseudo-random symmetric indefinite matrix.
    ///
    /// Uses a simple LCG-based hash for reproducibility without external deps.
    /// Entries are in [-1, 1] with diagonal scaled by `diag_scale`.
    fn deterministic_indefinite_matrix(n: usize, seed: u64, diag_scale: f64) -> Mat<f64> {
        // Pure function of (i,j) — compatible with symmetric_matrix's Fn requirement.
        let hash = |a: usize, b: usize| -> f64 {
            let mut s = seed
                .wrapping_add((a * 10007) as u64)
                .wrapping_add((b * 7) as u64);
            s = s
                .wrapping_mul(6364136223846793005)
                .wrapping_add(1442695040888963407);
            s = s
                .wrapping_mul(6364136223846793005)
                .wrapping_add(1442695040888963407);
            ((s >> 33) as f64) / (u32::MAX as f64 / 2.0) - 1.0
        };

        symmetric_matrix(n, |i, j| {
            if i == j {
                hash(i, i) * diag_scale
            } else {
                // Use min/max so (i,j) and (j,i) produce the same value
                hash(i.min(j), i.max(j) + n)
            }
        })
    }

    /// Apply the "cause_delays" pattern from SPRAL testing: multiply n/8 random
    /// rows (and corresponding columns, to maintain symmetry) by a large factor.
    /// This creates large off-diagonal entries that cause L entries to exceed 1/threshold.
    fn cause_delays(a: &mut Mat<f64>, seed: u64, scale: f64) {
        let n = a.nrows();
        let n_scaled = (n / 8).max(1);

        // Deterministically select which rows to scale
        let mut state = seed;
        let mut next_idx = || -> usize {
            state = state
                .wrapping_mul(6364136223846793005)
                .wrapping_add(1442695040888963407);
            ((state >> 33) as usize) % n
        };

        let mut scaled_rows = Vec::new();
        while scaled_rows.len() < n_scaled {
            let idx = next_idx();
            if !scaled_rows.contains(&idx) {
                scaled_rows.push(idx);
            }
        }

        // Scale rows and columns symmetrically: A -> S * A * S
        // where S = diag(s_1, ..., s_n) with s_i = scale if i in scaled_rows, else 1
        for &r in &scaled_rows {
            for j in 0..n {
                a[(r, j)] *= scale;
                a[(j, r)] *= scale;
            }
            // Diagonal gets scaled twice (row and column), which is correct for S*A*S
        }
    }

    #[test]
    fn test_factor_inner_with_delays() {
        // Regression test: factor_inner with threshold failures.
        //
        // Constructs matrices using the "cause_delays" pattern (SPRAL testing):
        // take a random symmetric indefinite matrix, then multiply n/8 random
        // rows and corresponding columns by 1000. This creates large off-diagonal
        // entries that cause L entries to exceed 1/threshold, triggering the
        // backup/restore/delay path in factor_inner.
        //
        // Tests multiple (n, ib) combinations to exercise different code paths
        // in the BLAS-3 Factor/Apply/Update loop.

        struct TestConfig {
            n: usize,
            ib: usize,
            seed: u64,
            scale: f64,
        }

        let configs = [
            // Small: 8x8 with ib=2 (4 blocks)
            TestConfig {
                n: 8,
                ib: 2,
                seed: 42,
                scale: 1000.0,
            },
            // Small: 8x8 with ib=4 (2 blocks)
            TestConfig {
                n: 8,
                ib: 4,
                seed: 42,
                scale: 1000.0,
            },
            // Medium: 16x16 with ib=4 (4 blocks)
            TestConfig {
                n: 16,
                ib: 4,
                seed: 42,
                scale: 1000.0,
            },
            // Medium: 16x16 with ib=2 (8 blocks)
            TestConfig {
                n: 16,
                ib: 2,
                seed: 42,
                scale: 1000.0,
            },
            // Larger: 32x32 with ib=4
            TestConfig {
                n: 32,
                ib: 4,
                seed: 42,
                scale: 1000.0,
            },
            // Larger: 32x32 with ib=8
            TestConfig {
                n: 32,
                ib: 8,
                seed: 42,
                scale: 1000.0,
            },
            // Different seed
            TestConfig {
                n: 16,
                ib: 4,
                seed: 137,
                scale: 1000.0,
            },
            // Extreme scale
            TestConfig {
                n: 16,
                ib: 4,
                seed: 42,
                scale: 1e6,
            },
            // 64x64 with ib=8
            TestConfig {
                n: 64,
                ib: 8,
                seed: 42,
                scale: 1000.0,
            },
            // 64x64 with ib=16
            TestConfig {
                n: 64,
                ib: 16,
                seed: 42,
                scale: 1000.0,
            },
        ];

        let mut _any_delays_found = false;
        let mut any_failures = false;

        for (idx, config) in configs.iter().enumerate() {
            let mut a = deterministic_indefinite_matrix(config.n, config.seed, 5.0);
            cause_delays(&mut a, config.seed + 1000, config.scale);

            let opts = AptpOptions {
                inner_block_size: config.ib,
                outer_block_size: config.n.max(config.ib),
                threshold: 0.01,
                ..AptpOptions::default()
            };

            let result = aptp_factor(a.as_ref(), &opts).unwrap();

            let n = config.n;
            let sum = result.stats.num_1x1 + 2 * result.stats.num_2x2 + result.stats.num_delayed;
            assert_eq!(
                sum, n,
                "config {}: statistics invariant broken: {} != {}",
                idx, sum, n
            );

            if result.stats.num_delayed > 0 {
                _any_delays_found = true;
            }

            // If fully eliminated, check reconstruction error
            if result.stats.num_delayed == 0 {
                let error = dense_reconstruction_error(
                    &a,
                    &result.l,
                    &result.d,
                    result.perm.as_ref().arrays().0,
                );
                if error >= 1e-12 {
                    any_failures = true;
                }
                assert!(
                    error < 1e-10,
                    "config {} (n={}, ib={}): reconstruction error {:.2e} >= 1e-10 \
                     (no delays but bad reconstruction indicates factor_inner bug)",
                    idx,
                    config.n,
                    config.ib,
                    error
                );
            }
        }

        // At least one config should have triggered delays (confirming the
        // cause_delays pattern works with the threshold).
        // Note: we don't assert any_delays_found because with complete pivoting
        // the diagonal block never delays — only apply_and_check can reduce
        // effective_nelim. Whether this triggers depends on the matrix structure.

        assert!(
            !any_failures,
            "Some configs had reconstruction error >= 1e-12 without delays. \
             This indicates a bug in factor_inner's backup/restore/update logic."
        );
    }

    #[test]
    fn test_factor_inner_with_delays_targeted() {
        // More targeted test: construct a matrix specifically designed to trigger
        // threshold failure in factor_inner's apply_and_check step.
        //
        // Strategy: Create a matrix where complete pivoting on the ib×ib diagonal
        // block succeeds (all ib columns eliminated within the block), but the
        // panel entries below the block exceed 1/threshold after TRSM + D scaling.
        //
        // Matrix structure (8x8, ib=4):
        //   Block [0:4, 0:4]: moderate diagonal, small off-diagonal -- complete pivoting
        //     eliminates all 4 columns within the block with |L_block| <= 4
        //   Panel [4:8, 0:4]: large entries that exceed 1/threshold after TRSM
        //     Specifically, panel entries ~ 2.0 with pivots ~ 0.005, so L ~ 400 >> 100
        //   Block [4:8, 4:8]: moderate diagonal
        //
        // Complete pivoting searches only within the ib×ib diagonal block, so panel
        // entries don't affect pivot selection.
        //
        // Also tested with ib=2 to get partial threshold failure (first 2 columns
        // may pass if their diagonal is large enough).
        let n = 8;

        for &ib in &[4, 2] {
            let a = symmetric_matrix(n, |i, j| {
                match (i, j) {
                    // Diagonal block [0:4, 0:4]
                    // Complete pivoting picks largest first: 10.0, 10.0, then 0.005, 0.005
                    // For ib=4: all 4 eliminated in block, but panel L ~ 2.0/0.005 = 400
                    // For ib=2: block [0:2,0:2] → pivots 10.0,10.0 → panel L ~ 2.0/10 = 0.2 (OK)
                    //           block [2:4,2:4] → pivots 0.005,0.005 → panel L ~ 2.0/0.005 = 400 (FAIL)
                    (0, 0) => 10.0,
                    (1, 1) => 10.0,
                    (2, 2) => 0.005,
                    (3, 3) => 0.005,
                    (i, j) if i < 4 && j < 4 && i != j => 0.001,

                    // Panel [4:8, 0:4]
                    (_, j) if j < 4 => 2.0,

                    // Lower-right block [4:8, 4:8]
                    (i, _) if i == j => 20.0,
                    (_, _) => 0.5,
                }
            });

            // Test APTP delay behavior (TPP disabled so we can observe delays)
            let opts_pass = AptpOptions {
                inner_block_size: ib,
                outer_block_size: 256,
                threshold: 0.01,
                failed_pivot_method: FailedPivotMethod::Pass,
                ..AptpOptions::default()
            };

            let result = aptp_factor(a.as_ref(), &opts_pass).unwrap();

            let sum = result.stats.num_1x1 + 2 * result.stats.num_2x2 + result.stats.num_delayed;
            assert_eq!(sum, n, "ib={}: statistics invariant: {} != {}", ib, sum, n);

            // Check reconstruction if fully eliminated
            if result.stats.num_delayed == 0 {
                let error = dense_reconstruction_error(
                    &a,
                    &result.l,
                    &result.d,
                    result.perm.as_ref().arrays().0,
                );
                assert!(
                    error < 1e-12,
                    "targeted (ib={}): reconstruction error {:.2e} >= 1e-12 (no delays)",
                    ib,
                    error
                );
            }

            // Verify that delays happened (at least for ib=4, columns 2,3 should be
            // delayed because L panel entries exceed 1/0.01=100)
            if ib == 4 || ib == 2 {
                // With small pivots 0.005 and panel entries 2.0:
                // L = 2.0 / 0.005 = 400, which far exceeds 100
                // Expect delays for the small-pivot columns
                assert!(
                    result.stats.num_delayed > 0,
                    "ib={}: expected some delays for small-pivot columns",
                    ib
                );
            }

            // Also check partial factorization with contribution block
            {
                let p = 6; // Only 6 of 8 fully summed
                let mut a_copy = a.clone();
                let opts_partial = AptpOptions {
                    inner_block_size: ib,
                    outer_block_size: 256,
                    threshold: 0.01,
                    failed_pivot_method: FailedPivotMethod::Pass,
                    ..AptpOptions::default()
                };
                let result_p = aptp_factor_in_place(a_copy.as_mut(), p, &opts_partial).unwrap();
                let error_p = check_partial_factorization_in_place(&a, &a_copy, p, &result_p);
                assert!(
                    error_p < 1e-10,
                    "targeted partial (ib={}, p={}): error {:.2e} >= 1e-10",
                    ib,
                    p,
                    error_p
                );
            }
        }
    }

    #[test]
    fn test_factor_inner_with_delays_aggressive() {
        // Aggressive test using a Lehmer-like matrix structure with perturbations
        // designed to trigger many threshold failures.
        //
        // The matrix is constructed as: A = Q * diag(d_i) * Q^T where d_i are
        // alternating +/- with varying magnitudes. Then specific rows are scaled
        // to create problematic panel entries.
        //
        // We test with multiple (n, ib) combos and multiple seeds.

        let test_cases: Vec<(usize, usize, u64)> = vec![
            (12, 4, 1),
            (12, 4, 2),
            (12, 4, 3),
            (16, 4, 1),
            (16, 4, 2),
            (16, 8, 1),
            (20, 4, 1),
            (24, 4, 1),
            (24, 8, 1),
            (32, 4, 1),
            (32, 8, 1),
            (32, 16, 1),
            (48, 8, 1),
            (64, 8, 1),
            (64, 16, 1),
        ];

        let mut _total_delays = 0;
        let mut max_error = 0.0_f64;
        let mut _worst_config = String::new();

        for &(n, ib, seed) in &test_cases {
            // Build a random symmetric indefinite matrix
            let mut a = deterministic_indefinite_matrix(n, seed * 31337, 5.0);

            // Apply cause_delays to create threshold failures
            cause_delays(&mut a, seed * 31337 + 7919, 1000.0);

            let opts = AptpOptions {
                inner_block_size: ib,
                outer_block_size: n.max(ib),
                threshold: 0.01,
                ..AptpOptions::default()
            };

            let result = aptp_factor(a.as_ref(), &opts).unwrap();

            let sum = result.stats.num_1x1 + 2 * result.stats.num_2x2 + result.stats.num_delayed;
            assert_eq!(
                sum, n,
                "(n={}, ib={}, seed={}): stats invariant {} != {}",
                n, ib, seed, sum, n
            );

            _total_delays += result.stats.num_delayed;

            if result.stats.num_delayed == 0 {
                let error = dense_reconstruction_error(
                    &a,
                    &result.l,
                    &result.d,
                    result.perm.as_ref().arrays().0,
                );
                if error > max_error {
                    max_error = error;
                    _worst_config =
                        format!("n={}, ib={}, seed={}: error={:.2e}", n, ib, seed, error);
                }
                assert!(
                    error < 1e-10,
                    "(n={}, ib={}, seed={}): reconstruction error {:.2e} >= 1e-10",
                    n,
                    ib,
                    seed,
                    error
                );
            }
        }
    }

    #[test]
    fn test_factor_inner_cause_delays_then_compare_single_vs_blocked() {
        // Compare factor_inner (ib > 1) vs ib=n (effectively single-block).
        // Both should produce reconstruction error < 1e-12 if no delays,
        // and the same number of eliminations.
        //
        // This catches bugs where blocking introduces errors that single-block
        // factorization does not.

        let seeds = [42u64, 137, 271, 314, 997];
        let n = 16;

        for &seed in &seeds {
            let mut a = deterministic_indefinite_matrix(n, seed, 5.0);
            cause_delays(&mut a, seed + 5000, 500.0);

            // Single-block: ib = n (factor_block_diagonal processes entire matrix)
            let opts_single = AptpOptions {
                inner_block_size: n,
                outer_block_size: n,
                threshold: 0.01,
                ..AptpOptions::default()
            };
            let result_single = aptp_factor(a.as_ref(), &opts_single).unwrap();

            // Blocked: ib = 4
            let opts_blocked = AptpOptions {
                inner_block_size: 4,
                outer_block_size: n,
                threshold: 0.01,
                ..AptpOptions::default()
            };
            let result_blocked = aptp_factor(a.as_ref(), &opts_blocked).unwrap();

            // Both should achieve good reconstruction when fully eliminated
            if result_single.stats.num_delayed == 0 {
                let error_s = dense_reconstruction_error(
                    &a,
                    &result_single.l,
                    &result_single.d,
                    result_single.perm.as_ref().arrays().0,
                );
                assert!(
                    error_s < 1e-12,
                    "seed={}: single-block error {:.2e}",
                    seed,
                    error_s
                );
            }

            if result_blocked.stats.num_delayed == 0 {
                let error_b = dense_reconstruction_error(
                    &a,
                    &result_blocked.l,
                    &result_blocked.d,
                    result_blocked.perm.as_ref().arrays().0,
                );
                assert!(
                    error_b < 1e-12,
                    "seed={}: blocked error {:.2e} >= 1e-12",
                    seed,
                    error_b
                );
            }
        }
    }

    /// Check partial factorization: P^T A P = L D L^T + [0; 0; contribution]
    ///
    /// After factor_inner with num_fully_summed = p < m, the matrix contains:
    /// - L entries in a[0..q, 0..q] (lower triangle, unit diagonal implicit)
    /// - L panel in a[q..m, 0..q]
    /// - Schur complement (contribution) in a[q..m, q..m]
    ///
    /// where q = num_eliminated <= p.
    ///
    /// Correctness condition: for the permuted matrix PAP^T,
    ///   PAP^T[0..m, 0..m] = L[0..m, 0..q] * D[0..q, 0..q] * L[0..m, 0..q]^T + [0_{q,q} 0; 0 S]
    /// where S = a[q..m, q..m] after factorization (the Schur complement).
    fn check_partial_factorization_in_place(
        original: &Mat<f64>,
        factored: &Mat<f64>,
        num_fully_summed: usize,
        result: &AptpFactorResult,
    ) -> f64 {
        let m = original.nrows();
        let q = result.num_eliminated;
        let p = num_fully_summed;
        let perm = &result.perm;

        // Apply deferred contribution GEMM to get the full Schur complement.
        // After our restructuring, aptp_factor_in_place leaves A[p..m, p..m]
        // with only assembled values (no per-block trailing updates).
        // We need to apply the deferred GEMM to get the actual Schur complement.
        let mut factored = factored.clone();
        let nfs = m - p;
        if nfs > 0 && q > 0 {
            let mut contrib_buffer = Mat::zeros(nfs, nfs);
            let mut ld_ws = Mat::new();
            compute_contribution_gemm(
                &factored,
                p,
                q,
                m,
                &result.d,
                &mut contrib_buffer,
                &mut ld_ws,
                Par::Seq,
            );
            // Copy the Schur complement back into the factored matrix
            for i in 0..nfs {
                for j in 0..=i {
                    factored[(p + i, p + j)] = contrib_buffer[(i, j)];
                }
            }
        }
        let d = &result.d;

        // Build PAP^T
        let mut papt = Mat::zeros(m, m);
        for i in 0..m {
            for j in 0..m {
                papt[(i, j)] = original[(perm[i], perm[j])];
            }
        }

        // Extract L (m x q, unit lower triangular in first q columns)
        let mut l_full = Mat::zeros(m, q);
        for i in 0..q {
            l_full[(i, i)] = 1.0;
        }
        let mut col = 0;
        while col < q {
            match d.pivot_type(col) {
                PivotType::OneByOne => {
                    for i in (col + 1)..m {
                        l_full[(i, col)] = factored[(i, col)];
                    }
                    col += 1;
                }
                PivotType::TwoByTwo { partner } if partner > col => {
                    // a[(col+1, col)] is D off-diagonal, not L
                    for i in (col + 2)..m {
                        l_full[(i, col)] = factored[(i, col)];
                        l_full[(i, col + 1)] = factored[(i, col + 1)];
                    }
                    col += 2;
                }
                _ => {
                    col += 1;
                }
            }
        }

        // Build D (q x q)
        let mut d_mat = Mat::zeros(q, q);
        col = 0;
        while col < q {
            match d.pivot_type(col) {
                PivotType::OneByOne => {
                    d_mat[(col, col)] = d.diagonal_1x1(col);
                    col += 1;
                }
                PivotType::TwoByTwo { partner } if partner > col => {
                    let block = d.diagonal_2x2(col);
                    d_mat[(col, col)] = block.a;
                    d_mat[(col, col + 1)] = block.b;
                    d_mat[(col + 1, col)] = block.b;
                    d_mat[(col + 1, col + 1)] = block.c;
                    col += 2;
                }
                _ => {
                    col += 1;
                }
            }
        }

        // Compute L * D * L^T (m x m)
        // First: W = L * D (m x q)
        let mut w = Mat::zeros(m, q);
        for i in 0..m {
            for j in 0..q {
                let mut sum = 0.0;
                for k in 0..q {
                    sum += l_full[(i, k)] * d_mat[(k, j)];
                }
                w[(i, j)] = sum;
            }
        }
        // Then: LDL^T = W * L^T (m x m)
        let mut ldlt = Mat::zeros(m, m);
        for i in 0..m {
            for j in 0..m {
                let mut sum = 0.0;
                for k in 0..q {
                    sum += w[(i, k)] * l_full[(j, k)];
                }
                ldlt[(i, j)] = sum;
            }
        }

        // Extract Schur complement S = factored[q..m, q..m]
        // The residual should be: PAP^T - LDL^T = [0 0; 0 S]
        // So: PAP^T[i,j] - LDL^T[i,j] should be:
        //   0 if i < q or j < q
        //   S[i-q, j-q] = factored[i,j] if i >= q and j >= q
        let mut max_error = 0.0_f64;
        let mut norm_sq = 0.0_f64;
        let mut diff_sq = 0.0_f64;

        // Only check lower triangle (i >= j) because swap_symmetric only
        // maintains the lower triangle of the dense frontal matrix.
        // Production code (extract_contribution, extend_add) also reads
        // only the lower triangle.
        for i in 0..m {
            for j in 0..=i {
                // Count lower triangle entries twice for norm (symmetric)
                let weight = if i == j { 1.0 } else { 2.0 };
                norm_sq += weight * papt[(i, j)] * papt[(i, j)];
                let residual = papt[(i, j)] - ldlt[(i, j)];
                if i >= q && j >= q {
                    // This should equal factored[i, j] (the Schur complement)
                    let schur_entry = factored[(i, j)];
                    let err = (residual - schur_entry).abs();
                    if err > max_error {
                        max_error = err;
                    }
                    diff_sq += weight * (residual - schur_entry) * (residual - schur_entry);
                } else {
                    // Should be zero
                    diff_sq += weight * residual * residual;
                    if residual.abs() > max_error {
                        max_error = residual.abs();
                    }
                }
            }
        }

        if norm_sq == 0.0 {
            diff_sq.sqrt()
        } else {
            (diff_sq / norm_sq).sqrt()
        }
    }

    #[test]
    fn test_factor_inner_partial_with_delays_schur_check() {
        // This test exercises factor_inner on frontal matrices where
        // num_fully_summed < m (partial factorization with contribution block),
        // AND delays occur. This is exactly the scenario in multifrontal
        // factorization where the bug was observed.
        //
        // The test verifies that:
        //   PAP^T = L * D * L^T + [0 0; 0 S]
        // where S is the Schur complement stored in the lower-right of the
        // factored matrix.
        //
        // If backup/restore/update_cross_terms is wrong, S will be corrupted.

        struct PartialConfig {
            m: usize,  // total matrix dimension
            p: usize,  // num_fully_summed
            ib: usize, // inner block size
            seed: u64,
            scale: f64,
        }

        let configs = [
            // Small cases: 12x12 with p=8, ib=4 (2 inner blocks, 4 contribution rows)
            PartialConfig {
                m: 12,
                p: 8,
                ib: 4,
                seed: 42,
                scale: 1000.0,
            },
            PartialConfig {
                m: 12,
                p: 8,
                ib: 4,
                seed: 137,
                scale: 1000.0,
            },
            PartialConfig {
                m: 12,
                p: 8,
                ib: 2,
                seed: 42,
                scale: 1000.0,
            },
            // Medium: 16x12 (p=12 of 16), ib=4
            PartialConfig {
                m: 16,
                p: 12,
                ib: 4,
                seed: 42,
                scale: 1000.0,
            },
            PartialConfig {
                m: 16,
                p: 12,
                ib: 4,
                seed: 271,
                scale: 1000.0,
            },
            // 20x16, ib=4
            PartialConfig {
                m: 20,
                p: 16,
                ib: 4,
                seed: 42,
                scale: 1000.0,
            },
            PartialConfig {
                m: 20,
                p: 16,
                ib: 8,
                seed: 42,
                scale: 1000.0,
            },
            // Larger: 32x24, ib=8
            PartialConfig {
                m: 32,
                p: 24,
                ib: 8,
                seed: 42,
                scale: 1000.0,
            },
            PartialConfig {
                m: 32,
                p: 24,
                ib: 4,
                seed: 42,
                scale: 1000.0,
            },
            // 48x32, ib=8
            PartialConfig {
                m: 48,
                p: 32,
                ib: 8,
                seed: 42,
                scale: 1000.0,
            },
            // Extreme scale
            PartialConfig {
                m: 16,
                p: 12,
                ib: 4,
                seed: 42,
                scale: 1e6,
            },
            // More seeds
            PartialConfig {
                m: 16,
                p: 12,
                ib: 4,
                seed: 314,
                scale: 1000.0,
            },
            PartialConfig {
                m: 16,
                p: 12,
                ib: 4,
                seed: 997,
                scale: 1000.0,
            },
        ];

        let mut any_delays = false;
        let mut worst_error = 0.0_f64;
        let mut worst_config = String::new();

        for (idx, config) in configs.iter().enumerate() {
            let mut a = deterministic_indefinite_matrix(config.m, config.seed, 5.0);
            cause_delays(&mut a, config.seed + 1000, config.scale);

            let opts = AptpOptions {
                inner_block_size: config.ib,
                outer_block_size: config.m.max(config.ib),
                threshold: 0.01,
                failed_pivot_method: FailedPivotMethod::Pass,
                ..AptpOptions::default()
            };

            let original = a.clone();
            let result = aptp_factor_in_place(a.as_mut(), config.p, &opts).unwrap();

            let sum = result.stats.num_1x1 + 2 * result.stats.num_2x2 + result.stats.num_delayed;
            assert_eq!(
                sum, config.p,
                "config {} (m={}, p={}, ib={}): stats invariant {} != {}",
                idx, config.m, config.p, config.ib, sum, config.p
            );

            if result.stats.num_delayed > 0 {
                any_delays = true;
            }

            // Check partial factorization correctness (PAP^T = LDL^T + [0;S])
            let error = check_partial_factorization_in_place(&original, &a, config.p, &result);

            if error > worst_error {
                worst_error = error;
                worst_config = format!(
                    "config {} (m={}, p={}, ib={}, seed={})",
                    idx, config.m, config.p, config.ib, config.seed
                );
            }
        }

        assert!(
            any_delays,
            "No configurations triggered threshold delays. \
             Adjust scale or matrix construction to create delays."
        );

        assert!(
            worst_error < 1e-10,
            "Worst partial factorization error: {:.2e} at {}\n\
             This indicates corrupted Schur complement — bug in \
             backup/restore/update_cross_terms logic in factor_inner.",
            worst_error,
            worst_config
        );
    }

    // ---- NFS boundary in two-level factor tests ----

    #[test]
    fn test_two_level_nfs_boundary_mid_block() {
        // Exercises the local nfs_boundary = p - col_start computation in
        // two_level_factor. When p is not a multiple of outer_block_size,
        // the last outer block has nfs_boundary < block_cols, meaning the
        // NFS region starts inside an inner block. update_trailing must
        // correctly restrict its Schur update to the FS region and skip
        // the NFS×NFS region (deferred to compute_contribution_gemm).
        //
        // If nfs_boundary is wrong, the Schur complement (contribution
        // block) will be corrupted, failing the reconstruction check.

        struct Config {
            m: usize,
            p: usize,
            nb: usize,
            ib: usize,
            seed: u64,
        }

        let configs = [
            // p=20, nb=16: first block nfs_boundary=20, second block nfs_boundary=4
            // (NFS starts at col 4 of the second 16-col outer block)
            Config {
                m: 48,
                p: 20,
                nb: 16,
                ib: 8,
                seed: 42,
            },
            // p=40, nb=32: first block nfs_boundary=40, second block nfs_boundary=8
            Config {
                m: 64,
                p: 40,
                nb: 32,
                ib: 8,
                seed: 42,
            },
            // p=10, nb=8: first block nfs_boundary=10, second block nfs_boundary=2
            // (very small NFS boundary in inner block)
            Config {
                m: 24,
                p: 10,
                nb: 8,
                ib: 4,
                seed: 137,
            },
            // p=25, nb=16: blocks at col_start=0 (nfs=25), col_start=16 (nfs=9)
            Config {
                m: 48,
                p: 25,
                nb: 16,
                ib: 8,
                seed: 271,
            },
            // Deliberately misalign p with ib too: p=13, nb=8, ib=4
            // block 0: nfs_boundary=13, block 1: nfs_boundary=5 (mid-ib boundary)
            Config {
                m: 32,
                p: 13,
                nb: 8,
                ib: 4,
                seed: 42,
            },
        ];

        let mut worst_error = 0.0_f64;
        let mut worst_label = String::new();

        for (idx, c) in configs.iter().enumerate() {
            let a = deterministic_indefinite_matrix(c.m, c.seed, 5.0);

            let opts = AptpOptions {
                outer_block_size: c.nb,
                inner_block_size: c.ib,
                threshold: 0.01,
                failed_pivot_method: FailedPivotMethod::Pass,
                ..AptpOptions::default()
            };

            let original = a.clone();
            let mut a_copy = a;
            let result = aptp_factor_in_place(a_copy.as_mut(), c.p, &opts).unwrap();

            let sum = result.stats.num_1x1 + 2 * result.stats.num_2x2 + result.stats.num_delayed;
            assert_eq!(
                sum, c.p,
                "config {}: statistics invariant {} != {}",
                idx, sum, c.p
            );

            let error = check_partial_factorization_in_place(&original, &a_copy, c.p, &result);

            if error > worst_error {
                worst_error = error;
                worst_label = format!(
                    "config {} (m={}, p={}, nb={}, ib={}, seed={})",
                    idx, c.m, c.p, c.nb, c.ib, c.seed
                );
            }
        }

        assert!(
            worst_error < 1e-10,
            "Worst reconstruction error: {:.2e} at {}\n\
             This indicates the local nfs_boundary computation in \
             two_level_factor is producing incorrect Schur complements.",
            worst_error,
            worst_label
        );
    }

    // ---- TPP (Threshold Partial Pivoting) fallback tests ----

    #[test]
    fn test_tpp_helpers() {
        // 4x4 lower triangle:
        //  [  4.0   .    .    .  ]
        //  [  0.5  -3.0  .    .  ]
        //  [  0.1   0.2  5.0  .  ]
        //  [  0.3  -0.4  0.6  2.0]
        let a = symmetric_matrix(4, |i, j| {
            let vals = [
                [4.0, 0.5, 0.1, 0.3],
                [0.5, -3.0, 0.2, -0.4],
                [0.1, 0.2, 5.0, 0.6],
                [0.3, -0.4, 0.6, 2.0],
            ];
            vals[i][j]
        });

        // tpp_is_col_small: col 0, from=0, to=4, small=5.0 → true (all entries < 5.0)
        assert!(tpp_is_col_small(a.as_ref(), 0, 0, 4, 5.0));
        // With small=0.01 → false (0.5, 0.1, 0.3 ≥ 0.01 and diag 4.0 ≥ 0.01)
        assert!(!tpp_is_col_small(a.as_ref(), 0, 0, 4, 0.01));

        // Zero column → true
        let z = Mat::zeros(4, 4);
        assert!(tpp_is_col_small(z.as_ref(), 0, 0, 4, 1e-20));

        // tpp_find_row_abs_max: row 3, cols 0..3
        // a[(3,0)]=0.3, a[(3,1)]=-0.4, a[(3,2)]=0.6 → max at col 2
        let t = tpp_find_row_abs_max(a.as_ref(), 3, 0, 3);
        assert_eq!(t, 2, "expected col 2, got {}", t);

        // tpp_find_rc_abs_max_exclude: col 1, nelim=0, m=4, exclude=3
        // row part: a[(1,0)]=0.5
        // col part: a[(2,1)]=0.2 (excluding row 3)
        // max = 0.5
        let max_exc = tpp_find_rc_abs_max_exclude(a.as_ref(), 1, 0, 4, 3);
        assert!(
            (max_exc - 0.5).abs() < 1e-15,
            "expected 0.5, got {}",
            max_exc
        );

        // tpp_test_2x2: (0, 1) with a11=4, a21=0.5, a22=-3
        // det = 4*(-3) - 0.25 = -12.25 → non-singular
        // With small=1e-20, u=0.01
        let result = tpp_test_2x2(a.as_ref(), 0, 1, 0.6, 0.6, 0.01, 1e-20);
        assert!(result.is_some(), "2x2 pivot (0,1) should pass");
        let (d11, d12, d22) = result.unwrap();
        assert!((d11 - 4.0).abs() < 1e-15);
        assert!((d12 - 0.5).abs() < 1e-15);
        assert!((d22 - (-3.0)).abs() < 1e-15);

        // tpp_test_2x2 with near-singular block
        let tiny = symmetric_matrix(2, |_, _| 1e-25);
        assert!(tpp_test_2x2(tiny.as_ref(), 0, 1, 1e-25, 1e-25, 0.01, 1e-20).is_none());
    }

    #[test]
    fn test_tpp_basic_1x1() {
        // 4x4 diagonal-dominant matrix: all diagonals are acceptable 1x1 pivots
        let a = symmetric_matrix(4, |i, j| {
            if i == j {
                [10.0, -8.0, 5.0, 7.0][i]
            } else {
                0.1
            }
        });

        let opts = AptpOptions::default();
        let result = aptp_factor(a.as_ref(), &opts).unwrap();

        assert_eq!(result.stats.num_delayed, 0);
        assert_eq!(result.stats.num_1x1 + 2 * result.stats.num_2x2, 4);

        let error =
            dense_reconstruction_error(&a, &result.l, &result.d, result.perm.as_ref().arrays().0);
        assert!(error < 1e-12, "reconstruction error {:.2e}", error);
    }

    #[test]
    fn test_tpp_basic_2x2() {
        // 4x4 matrix where diagonal entries are small (fail 1x1 threshold)
        // but off-diagonal entries create good 2x2 pivots.
        let a = symmetric_matrix(4, |i, j| {
            let vals = [
                [0.001, 5.0, 0.01, 0.01],
                [5.0, 0.001, 0.01, 0.01],
                [0.01, 0.01, 0.001, 5.0],
                [0.01, 0.01, 5.0, 0.001],
            ];
            vals[i][j]
        });

        let opts = AptpOptions::default();
        let result = aptp_factor(a.as_ref(), &opts).unwrap();

        assert_eq!(result.stats.num_delayed, 0);

        let error =
            dense_reconstruction_error(&a, &result.l, &result.d, result.perm.as_ref().arrays().0);
        assert!(error < 1e-12, "reconstruction error {:.2e}", error);
    }

    #[test]
    fn test_tpp_fallback_after_aptp() {
        // 16x16 matrix designed so APTP delays columns that TPP recovers.
        // Small inner block size forces many ib-scoped searches that fail,
        // but TPP's exhaustive search finds pivots.
        //
        // Matrix structure:
        //   Block [0:8, 0:8]: diagonal 10.0, small off-diag → easy APTP pivots
        //   Block [8:12, 8:12]: diagonal 0.005, large off-diag → APTP delays
        //   Block [12:16, 12:16]: diagonal 20.0 → easy pivots
        //   Cross [8:12, 0:8]: large entries → panel L exceeds threshold
        let n = 16;
        let a = symmetric_matrix(n, |i, j| {
            match (i, j) {
                // Top-left 8x8: easy pivots
                (i, j) if i < 8 && j < 8 => {
                    if i == j {
                        10.0
                    } else {
                        0.01
                    }
                }
                // Middle 4x4: hard pivots (small diag, large off-diag)
                (i, j) if (8..12).contains(&i) && (8..12).contains(&j) => {
                    if i == j {
                        0.005
                    } else {
                        2.0
                    }
                }
                // Bottom-right 4x4: easy pivots
                (i, j) if i >= 12 && j >= 12 => {
                    if i == j {
                        20.0
                    } else {
                        0.1
                    }
                }
                // Cross terms: large
                (i, j) if (8..12).contains(&i) && j < 8 => 3.0,
                (i, j) if i < 8 && (8..12).contains(&j) => 3.0,
                // Small cross terms elsewhere
                _ => 0.01,
            }
        });

        // With TPP disabled: APTP delays some columns
        let opts_pass = AptpOptions {
            inner_block_size: 4,
            failed_pivot_method: FailedPivotMethod::Pass,
            ..AptpOptions::default()
        };
        let result_pass = aptp_factor(a.as_ref(), &opts_pass).unwrap();

        // With TPP enabled: should eliminate more columns
        let opts_tpp = AptpOptions {
            inner_block_size: 4,
            failed_pivot_method: FailedPivotMethod::Tpp,
            ..AptpOptions::default()
        };
        let result_tpp = aptp_factor(a.as_ref(), &opts_tpp).unwrap();

        // TPP should recover at least some of APTP's delays
        assert!(
            result_tpp.stats.num_delayed <= result_pass.stats.num_delayed,
            "TPP should not increase delays"
        );

        // If fully eliminated, check reconstruction
        if result_tpp.stats.num_delayed == 0 {
            let error = dense_reconstruction_error(
                &a,
                &result_tpp.l,
                &result_tpp.d,
                result_tpp.perm.as_ref().arrays().0,
            );
            assert!(
                error < 1e-12,
                "TPP reconstruction error {:.2e} >= 1e-12",
                error
            );
        }
    }

    #[test]
    fn test_tpp_reconstruction_stress() {
        // 256x256 random indefinite matrix. Factor with TPP and verify reconstruction.
        use rand::SeedableRng;
        use rand::rngs::StdRng;
        use rand_distr::{Distribution, Uniform};

        let n = 256;
        let mut rng = StdRng::seed_from_u64(42);
        let dist = Uniform::new(-1.0f64, 1.0).unwrap();

        let mut a = Mat::zeros(n, n);
        for i in 0..n {
            for j in 0..=i {
                let v: f64 = dist.sample(&mut rng);
                a[(i, j)] = v;
                a[(j, i)] = v;
            }
            // Strengthen diagonal to avoid excessive delays
            let sign = if i % 3 == 0 { -1.0 } else { 1.0 };
            a[(i, i)] = sign * (5.0 + dist.sample(&mut rng).abs());
        }

        let opts = AptpOptions {
            failed_pivot_method: FailedPivotMethod::Tpp,
            ..AptpOptions::default()
        };
        let result = aptp_factor(a.as_ref(), &opts).unwrap();

        if result.stats.num_delayed == 0 {
            let error = dense_reconstruction_error(
                &a,
                &result.l,
                &result.d,
                result.perm.as_ref().arrays().0,
            );
            assert!(
                error < 1e-10,
                "stress reconstruction error {:.2e} >= 1e-10",
                error
            );
        }

        // Invariant check
        let sum = result.stats.num_1x1 + 2 * result.stats.num_2x2 + result.stats.num_delayed;
        assert_eq!(sum, n, "statistics invariant: {} != {}", sum, n);
    }

    #[test]
    fn test_failed_pivot_method_pass() {
        // Verify FailedPivotMethod::Pass skips TPP and preserves delays.
        // Use a matrix that APTP cannot fully factor (small diag, large off-diag).
        let n = 4;
        let a = symmetric_matrix(n, |i, j| {
            let vals = [
                [0.001, 5.0, 0.01, 0.01],
                [5.0, 0.001, 0.01, 0.01],
                [0.01, 0.01, 0.001, 5.0],
                [0.01, 0.01, 5.0, 0.001],
            ];
            vals[i][j]
        });

        // With Pass: APTP's complete pivoting on ib-blocks should still handle
        // this via 2x2 pivots. But with a very small block size, it might delay.
        let opts_pass = AptpOptions {
            inner_block_size: 2,
            failed_pivot_method: FailedPivotMethod::Pass,
            ..AptpOptions::default()
        };
        let result_pass = aptp_factor(a.as_ref(), &opts_pass).unwrap();

        let opts_tpp = AptpOptions {
            inner_block_size: 2,
            failed_pivot_method: FailedPivotMethod::Tpp,
            ..AptpOptions::default()
        };
        let result_tpp = aptp_factor(a.as_ref(), &opts_tpp).unwrap();

        // TPP should not have more delays than Pass
        assert!(
            result_tpp.stats.num_delayed <= result_pass.stats.num_delayed,
            "TPP delays {} > Pass delays {}",
            result_tpp.stats.num_delayed,
            result_pass.stats.num_delayed
        );

        // Both must satisfy invariant
        let sum_pass = result_pass.stats.num_1x1
            + 2 * result_pass.stats.num_2x2
            + result_pass.stats.num_delayed;
        let sum_tpp =
            result_tpp.stats.num_1x1 + 2 * result_tpp.stats.num_2x2 + result_tpp.stats.num_delayed;
        assert_eq!(sum_pass, n);
        assert_eq!(sum_tpp, n);
    }

    #[test]
    fn test_tpp_zero_pivot_handling() {
        // Matrix with some zero columns — TPP should handle gracefully.
        let n = 4;
        let a = symmetric_matrix(n, |i, j| {
            if i == j {
                [5.0, 0.0, 0.0, 3.0][i]
            } else if (i == 0 && j == 3) || (i == 3 && j == 0) {
                0.1
            } else {
                0.0
            }
        });

        let opts = AptpOptions {
            failed_pivot_method: FailedPivotMethod::Tpp,
            ..AptpOptions::default()
        };
        let result = aptp_factor(a.as_ref(), &opts).unwrap();

        // Should handle zero pivots (columns 1, 2) as zero 1x1 pivots
        let sum = result.stats.num_1x1 + 2 * result.stats.num_2x2 + result.stats.num_delayed;
        assert_eq!(sum, n, "invariant: {} != {}", sum, n);
    }

    // ---- Rectangular TPP kernel tests ----

    #[test]
    fn test_tpp_helpers_rect() {
        // Test unified helpers on a 5×3 rectangular matrix (m=5, n=3).
        // Lower-triangle-like storage:
        //   col 0    col 1    col 2
        //  [ 4.0      .        .    ]  row 0
        //  [ 0.5     -3.0      .    ]  row 1
        //  [ 0.1      0.2     5.0   ]  row 2
        //  [ 0.3     -0.4     0.6   ]  row 3
        //  [ 0.7      0.8    -0.9   ]  row 4
        let a = Mat::from_fn(5, 3, |i, j| {
            let vals: [[f64; 3]; 5] = [
                [4.0, 0.0, 0.0],
                [0.5, -3.0, 0.0],
                [0.1, 0.2, 5.0],
                [0.3, -0.4, 0.6],
                [0.7, 0.8, -0.9],
            ];
            vals[i][j]
        });

        // tpp_sym_entry: row=3, col=1 → a[(3,1)] = -0.4 (row >= col, col < n)
        assert!((tpp_sym_entry(a.as_ref(), 3, 1) - (-0.4)).abs() < 1e-15);
        // tpp_sym_entry: row=0, col=2 → a[(2,0)] = 0.1 (row < col, symmetric)
        assert!((tpp_sym_entry(a.as_ref(), 0, 2) - 0.1).abs() < 1e-15);
        // tpp_sym_entry: row=1, col=0 → a[(1,0)] = 0.5 (row >= col)
        assert!((tpp_sym_entry(a.as_ref(), 1, 0) - 0.5).abs() < 1e-15);

        // tpp_is_col_small on rect: col 0, from=0, to=5, small=5.0 → true
        assert!(tpp_is_col_small(a.as_ref(), 0, 0, 5, 5.0));
        // col 0 with small=0.01 → false (0.5, 0.3, 0.7 ≥ 0.01)
        assert!(!tpp_is_col_small(a.as_ref(), 0, 0, 5, 0.01));
        // col 3 (idx >= ncols=3): row entries a[(3,0)]=0.3, a[(3,1)]=-0.4, a[(3,2)]=0.6
        // column entries: idx=3 >= n=3, so no column scan
        assert!(!tpp_is_col_small(a.as_ref(), 3, 0, 5, 0.5));
        // with larger threshold
        assert!(tpp_is_col_small(a.as_ref(), 3, 0, 5, 1.0));

        // tpp_find_row_abs_max: row 4, cols 0..3
        // a[(4,0)]=0.7, a[(4,1)]=0.8, a[(4,2)]=-0.9 → max at col 2
        let t = tpp_find_row_abs_max(a.as_ref(), 4, 0, 3);
        assert_eq!(t, 2, "expected col 2, got {t}");

        // tpp_find_rc_abs_max_exclude on rect: col 1, nelim=0, m=5, exclude=3
        // row: a[(1,0)]=0.5
        // col: a[(2,1)]=0.2, a[(4,1)]=0.8 (excluding row 3)
        // max = 0.8
        let max_exc = tpp_find_rc_abs_max_exclude(a.as_ref(), 1, 0, 5, 3);
        assert!((max_exc - 0.8).abs() < 1e-15, "expected 0.8, got {max_exc}");

        // tpp_find_rc_abs_max_exclude for col >= ncols: col 3, nelim=0, m=5, exclude=usize::MAX
        // row: a[(3,0)]=0.3, a[(3,1)]=-0.4, a[(3,2)]=0.6
        // col: col=3 >= n=3, no column scan
        // max = 0.6
        let max_exc2 = tpp_find_rc_abs_max_exclude(a.as_ref(), 3, 0, 5, usize::MAX);
        assert!(
            (max_exc2 - 0.6).abs() < 1e-15,
            "expected 0.6, got {max_exc2}"
        );
    }

    /// Reconstruct P^T A_FS P = L D L^T from rectangular factorization results.
    ///
    /// For a rectangular m×k factorization with ne eliminated columns:
    ///   - A_FS = the k×k fully-summed subblock of the original matrix
    ///   - L = ne×ne unit lower triangular (from l_storage[0..ne, 0..ne])
    ///   - D = ne×ne block diagonal
    ///   - P = permutation of the k FS columns
    ///
    /// Returns ||P^T A_FS P - L D L^T|| / ||A_FS|| for the leading ne×ne block.
    fn rect_reconstruction_error(
        original_rect: &Mat<f64>,
        factored_rect: &Mat<f64>,
        k: usize,
        result: &AptpFactorResult,
    ) -> f64 {
        let ne = result.num_eliminated;
        if ne == 0 {
            return 0.0;
        }

        // Build L11 (ne × ne) unit lower triangular
        let mut l = Mat::zeros(ne, ne);
        let mut col = 0;
        while col < ne {
            l[(col, col)] = 1.0;
            match result.d.pivot_type(col) {
                PivotType::OneByOne => {
                    for r in (col + 1)..ne {
                        l[(r, col)] = factored_rect[(r, col)];
                    }
                    col += 1;
                }
                PivotType::TwoByTwo { partner } if partner > col => {
                    l[(col + 1, col + 1)] = 1.0;
                    for r in (col + 2)..ne {
                        l[(r, col)] = factored_rect[(r, col)];
                        l[(r, col + 1)] = factored_rect[(r, col + 1)];
                    }
                    col += 2;
                }
                _ => {
                    col += 1;
                }
            }
        }

        // Build D (ne × ne)
        let mut d_mat = Mat::zeros(ne, ne);
        col = 0;
        while col < ne {
            match result.d.pivot_type(col) {
                PivotType::OneByOne => {
                    d_mat[(col, col)] = result.d.diagonal_1x1(col);
                    col += 1;
                }
                PivotType::TwoByTwo { partner } if partner > col => {
                    let block = result.d.diagonal_2x2(col);
                    d_mat[(col, col)] = block.a;
                    d_mat[(col, col + 1)] = block.b;
                    d_mat[(col + 1, col)] = block.b;
                    d_mat[(col + 1, col + 1)] = block.c;
                    col += 2;
                }
                _ => {
                    col += 1;
                }
            }
        }

        // L * D
        let mut w = Mat::zeros(ne, ne);
        for i in 0..ne {
            for j in 0..ne {
                let mut sum = 0.0;
                for kk in 0..ne {
                    sum += l[(i, kk)] * d_mat[(kk, j)];
                }
                w[(i, j)] = sum;
            }
        }

        // W * L^T = L D L^T
        let mut ldlt = Mat::zeros(ne, ne);
        for i in 0..ne {
            for j in 0..ne {
                let mut sum = 0.0;
                for kk in 0..ne {
                    sum += w[(i, kk)] * l[(j, kk)];
                }
                ldlt[(i, j)] = sum;
            }
        }

        // Build P^T A_FS P (ne × ne leading block)
        // The original matrix is m×k (rectangular). The FS block is the k×k
        // symmetric subblock stored in the lower triangle of original_rect[0..k, 0..k].
        let perm = &result.perm;
        let mut pap = Mat::zeros(ne, ne);
        for i in 0..ne {
            for j in 0..ne {
                let pi = perm[i];
                let pj = perm[j];
                // Read from original_rect using symmetric access
                let val = if pi >= pj && pj < k {
                    original_rect[(pi, pj)]
                } else if pj > pi && pi < k {
                    original_rect[(pj, pi)]
                } else {
                    0.0
                };
                pap[(i, j)] = val;
            }
        }

        // ||P^T A P - L D L^T|| / ||A_FS||
        let mut diff_sq = 0.0_f64;
        let mut orig_sq = 0.0_f64;
        for i in 0..ne {
            for j in 0..ne {
                let d = pap[(i, j)] - ldlt[(i, j)];
                diff_sq += d * d;
                orig_sq += pap[(i, j)] * pap[(i, j)];
            }
        }
        if orig_sq == 0.0 {
            return diff_sq.sqrt();
        }
        (diff_sq / orig_sq).sqrt()
    }

    #[test]
    fn test_tpp_factor_rect_basic_1x1() {
        // 6×4 rect matrix (m=6, k=4 fully-summed) with diagonal-dominant structure.
        // All pivots should be 1x1.
        let m = 6;
        let k = 4;
        let mut a = Mat::zeros(m, k);
        // Fill lower-triangle-like data: diag-dominant FS block + NFS rows
        let vals: [[f64; 4]; 6] = [
            [10.0, 0.0, 0.0, 0.0],
            [0.1, -8.0, 0.0, 0.0],
            [0.2, 0.1, 12.0, 0.0],
            [0.1, -0.2, 0.3, 7.0],
            [0.3, 0.1, -0.1, 0.2], // NFS row
            [0.1, -0.3, 0.2, 0.1], // NFS row
        ];
        for i in 0..m {
            for j in 0..k {
                a[(i, j)] = vals[i][j];
            }
        }

        let original = a.clone();
        let opts = AptpOptions::default();
        let result = tpp_factor_rect(a.as_mut(), k, &opts).unwrap();

        assert_eq!(
            result.num_eliminated, 4,
            "expected all 4 columns eliminated"
        );
        assert_eq!(result.stats.num_delayed, 0);

        let error = rect_reconstruction_error(&original, &a, k, &result);
        assert!(
            error < 1e-12,
            "rect 1x1 reconstruction error {error:.2e} >= 1e-12"
        );
    }

    #[test]
    fn test_tpp_factor_rect_basic_2x2() {
        // 6×4 rect matrix with small diagonals and large off-diagonals
        // to force 2x2 pivots.
        let m = 6;
        let k = 4;
        let mut a = Mat::zeros(m, k);
        let vals: [[f64; 4]; 6] = [
            [0.001, 0.0, 0.0, 0.0],
            [5.0, 0.001, 0.0, 0.0],
            [0.01, 0.01, 0.001, 0.0],
            [0.01, 0.01, 5.0, 0.001],
            [0.1, 0.2, 0.1, 0.2], // NFS row
            [0.3, 0.1, 0.3, 0.1], // NFS row
        ];
        for i in 0..m {
            for j in 0..k {
                a[(i, j)] = vals[i][j];
            }
        }

        let original = a.clone();
        let opts = AptpOptions::default();
        let result = tpp_factor_rect(a.as_mut(), k, &opts).unwrap();

        assert_eq!(result.stats.num_delayed, 0, "expected no delays");
        assert!(result.stats.num_2x2 > 0, "expected at least one 2x2 pivot");

        let error = rect_reconstruction_error(&original, &a, k, &result);
        assert!(
            error < 1e-12,
            "rect 2x2 reconstruction error {error:.2e} >= 1e-12"
        );
    }

    #[test]
    fn test_tpp_factor_rect_with_delays() {
        // 8×4 rect matrix designed to produce delays.
        // All FS diagonals are tiny, off-diagonals create a poorly conditioned block.
        let m = 8;
        let k = 4;
        let mut a = Mat::zeros(m, k);
        // Very small diagonals, moderately sized but not pairable off-diags
        let vals: [[f64; 4]; 8] = [
            [1e-25, 0.0, 0.0, 0.0],
            [1e-25, 1e-25, 0.0, 0.0],
            [1e-25, 1e-25, 1e-25, 0.0],
            [1e-25, 1e-25, 1e-25, 1e-25],
            [0.1, 0.2, 0.3, 0.4],
            [0.5, 0.6, 0.7, 0.8],
            [0.2, 0.3, 0.4, 0.5],
            [0.1, 0.1, 0.1, 0.1],
        ];
        for i in 0..m {
            for j in 0..k {
                a[(i, j)] = vals[i][j];
            }
        }

        let opts = AptpOptions::default();
        let result = tpp_factor_rect(a.as_mut(), k, &opts).unwrap();

        // With all-tiny entries, the zero-pivot path should handle most columns
        // (set D=0, zero out L entries). This exercises the zero-pivot + rect path.
        let sum = result.stats.num_1x1 + 2 * result.stats.num_2x2 + result.stats.num_delayed;
        assert_eq!(sum, k, "invariant: {sum} != {k}");
    }

    #[test]
    fn test_tpp_factor_rect_square_matches() {
        // Factor a 4×4 matrix with both tpp_factor_rect (4×4 rect, m=n=k=4) and
        // tpp_factor (square, start_col=0). Verify bit-identical results.
        let n = 4;
        let a = symmetric_matrix(n, |i, j| {
            let vals = [
                [10.0, 0.5, 0.1, 0.3],
                [0.5, -8.0, 0.2, -0.4],
                [0.1, 0.2, 12.0, 0.6],
                [0.3, -0.4, 0.6, -5.0],
            ];
            vals[i][j]
        });

        let opts = AptpOptions::default();

        // Path 1: tpp_factor_rect on 4×4 (square)
        let mut a_rect = a.clone();
        let result_rect = tpp_factor_rect(a_rect.as_mut(), n, &opts).unwrap();

        // Path 2: tpp_factor (the fallback, with start_col=0, on a square matrix)
        let mut a_sq = a.clone();
        let mut col_order: Vec<usize> = (0..n).collect();
        let mut d_sq = MixedDiagonal::new(n);
        let mut stats_sq = AptpStatistics::default();
        let mut pivot_log_sq = Vec::with_capacity(n);
        let ne_sq = tpp_factor(
            a_sq.as_mut(),
            0,
            n,
            &mut col_order,
            &mut d_sq,
            &mut stats_sq,
            &mut pivot_log_sq,
            &opts,
        );
        d_sq.truncate(ne_sq);

        // Verify identical num_eliminated
        assert_eq!(
            result_rect.num_eliminated, ne_sq,
            "num_eliminated: rect={} sq={}",
            result_rect.num_eliminated, ne_sq
        );

        // Verify identical permutation
        assert_eq!(&result_rect.perm[..n], &col_order[..n], "perm mismatch");

        // Verify identical D values
        let ne = result_rect.num_eliminated;
        let mut col = 0;
        while col < ne {
            match result_rect.d.pivot_type(col) {
                PivotType::OneByOne => {
                    let d_r = result_rect.d.diagonal_1x1(col);
                    let d_s = d_sq.diagonal_1x1(col);
                    assert!(
                        (d_r - d_s).abs() < 1e-15,
                        "D mismatch at col {col}: rect={d_r} sq={d_s}"
                    );
                    col += 1;
                }
                PivotType::TwoByTwo { partner } if partner > col => {
                    let br = result_rect.d.diagonal_2x2(col);
                    let bs = d_sq.diagonal_2x2(col);
                    assert!((br.a - bs.a).abs() < 1e-15, "D.a mismatch at col {col}");
                    assert!((br.b - bs.b).abs() < 1e-15, "D.b mismatch at col {col}");
                    assert!((br.c - bs.c).abs() < 1e-15, "D.c mismatch at col {col}");
                    col += 2;
                }
                _ => {
                    col += 1;
                }
            }
        }

        // Verify bit-identical factored matrix contents (lower triangle)
        for j in 0..n {
            for i in j..n {
                let vr = a_rect[(i, j)];
                let vs = a_sq[(i, j)];
                assert!(
                    (vr - vs).abs() == 0.0,
                    "matrix mismatch at ({i},{j}): rect={vr} sq={vs}"
                );
            }
        }
    }

    #[test]
    fn test_tpp_factor_rect_reconstruction_stress() {
        // 64×32 random indefinite rect matrix. Factor and verify reconstruction.
        use rand::SeedableRng;
        use rand::rngs::StdRng;
        use rand_distr::{Distribution, Uniform};

        let m = 64;
        let k = 32;
        let mut rng = StdRng::seed_from_u64(99);
        let dist = Uniform::new(-1.0f64, 1.0).unwrap();

        let mut a = Mat::zeros(m, k);
        // Fill FS block (k×k lower triangle) as symmetric indefinite
        for i in 0..k {
            for j in 0..=i {
                let v: f64 = dist.sample(&mut rng);
                a[(i, j)] = v;
            }
            // Strengthen diagonal
            let sign = if i % 3 == 0 { -1.0 } else { 1.0 };
            a[(i, i)] = sign * (5.0 + dist.sample(&mut rng).abs());
        }
        // Fill NFS rows
        for i in k..m {
            for j in 0..k {
                a[(i, j)] = dist.sample(&mut rng);
            }
        }

        let original = a.clone();
        let opts = AptpOptions::default();
        let result = tpp_factor_rect(a.as_mut(), k, &opts).unwrap();

        let error = rect_reconstruction_error(&original, &a, k, &result);
        assert!(
            error < 1e-10,
            "rect stress reconstruction error {error:.2e} >= 1e-10"
        );

        // Invariant check
        let sum = result.stats.num_1x1 + 2 * result.stats.num_2x2 + result.stats.num_delayed;
        assert_eq!(sum, k, "statistics invariant: {sum} != {k}");
    }

    #[test]
    fn test_compute_contribution_gemm_rect_matches_square() {
        // Set up identical factored data in both square (m×m) and rect (m×k) layout.
        // Run both compute_contribution_gemm and compute_contribution_gemm_rect.
        // Verify NFS×NFS output is bit-identical.
        let m = 8;
        let k = 4;
        let nfs = m - k;

        // Build a symmetric 8×8 matrix and factor its 4×4 FS block
        let a_full = symmetric_matrix(m, |i, j| {
            if i == j {
                [10.0, -8.0, 12.0, 7.0, 3.0, -2.0, 5.0, 4.0][i]
            } else {
                0.1 * (i as f64 + j as f64 + 1.0) * if (i + j) % 2 == 0 { 1.0 } else { -1.0 }
            }
        });

        // Factor with tpp_factor_rect on rect view
        let mut a_rect = Mat::zeros(m, k);
        for i in 0..m {
            for j in 0..k {
                a_rect[(i, j)] = a_full[(i, j)];
            }
        }
        let opts = AptpOptions::default();
        let result = tpp_factor_rect(a_rect.as_mut(), k, &opts).unwrap();
        let ne = result.num_eliminated;

        if ne == 0 {
            return; // Can't compare without elimination
        }

        // For a fair comparison, factor the square matrix identically:
        let mut a_sq_factor = a_full.clone();
        let result_sq = tpp_factor_rect(a_sq_factor.as_mut(), k, &opts).unwrap();
        // Since a_full is square (8×8 with ncols=8), this is a valid rect factorization
        // with k=4 FS columns. The L values in columns 0..k are identical.

        // Compute contribution via rect path
        let mut contrib_rect = Mat::zeros(nfs, nfs);
        // Scatter NFS×NFS assembled data from original (via permutation)
        let perm = &result.perm;
        for i in 0..nfs {
            for j in 0..=i {
                let pi = perm[k + i];
                let pj = perm[k + j];
                let val = if pi >= pj {
                    a_full[(pi, pj)]
                } else {
                    a_full[(pj, pi)]
                };
                contrib_rect[(i, j)] = val;
            }
        }
        let mut contrib_rect2 = contrib_rect.clone();
        let mut ld_ws = Mat::zeros(nfs, ne);
        compute_contribution_gemm_rect(
            &a_rect,
            k,
            ne,
            m,
            &result.d,
            &mut contrib_rect2,
            &mut ld_ws,
            Par::Seq,
        );

        // Compute contribution via square path
        let mut contrib_sq = Mat::zeros(nfs, nfs);
        for i in 0..nfs {
            for j in 0..=i {
                let pi = perm[k + i];
                let pj = perm[k + j];
                let val = if pi >= pj {
                    a_full[(pi, pj)]
                } else {
                    a_full[(pj, pi)]
                };
                contrib_sq[(i, j)] = val;
            }
        }
        // For the square path, we need the frontal data with NFS×NFS in place.
        // Build a square matrix with factored columns and original NFS×NFS
        let mut frontal_sq = Mat::zeros(m, m);
        // Copy factored L columns 0..k
        for j in 0..k {
            for i in 0..m {
                frontal_sq[(i, j)] = a_sq_factor[(i, j)];
            }
        }
        // Copy NFS×NFS block from original (permuted)
        for i in 0..nfs {
            for j in 0..=i {
                frontal_sq[(k + i, k + j)] = contrib_sq[(i, j)];
            }
        }
        let mut ld_ws2 = Mat::zeros(nfs, ne);
        compute_contribution_gemm(
            &frontal_sq,
            k,
            ne,
            m,
            &result_sq.d,
            &mut contrib_sq,
            &mut ld_ws2,
            Par::Seq,
        );

        // Compare: both should produce the same NFS×NFS Schur complement
        for i in 0..nfs {
            for j in 0..=i {
                let vr = contrib_rect2[(i, j)];
                let vs = contrib_sq[(i, j)];
                assert!(
                    (vr - vs).abs() < 1e-12,
                    "contrib mismatch at ({i},{j}): rect={vr} sq={vs}"
                );
            }
        }
    }

    #[test]
    fn test_extract_front_factors_rect_round_trip() {
        // Factor a small rect matrix, call extract_front_factors_rect, verify
        // L11 is unit lower triangular, D11 matches, reconstruction holds.
        let m = 6;
        let k = 4;
        let mut a = Mat::zeros(m, k);
        let vals: [[f64; 4]; 6] = [
            [10.0, 0.0, 0.0, 0.0],
            [0.5, -8.0, 0.0, 0.0],
            [0.2, 0.3, 12.0, 0.0],
            [0.1, -0.1, 0.4, 7.0],
            [0.3, 0.1, -0.2, 0.5],
            [0.1, -0.3, 0.2, 0.1],
        ];
        for i in 0..m {
            for j in 0..k {
                a[(i, j)] = vals[i][j];
            }
        }

        let opts = AptpOptions::default();
        let result = tpp_factor_rect(a.as_mut(), k, &opts).unwrap();
        let ne = result.num_eliminated;
        assert!(ne > 0, "need at least one eliminated column");

        let row_indices: Vec<usize> = (0..m).collect();
        let ff = extract_front_factors_rect(&a, m, k, &row_indices, &result);

        assert_eq!(ff.num_eliminated, ne);
        assert_eq!(ff.l11.nrows(), ne);
        assert_eq!(ff.l11.ncols(), ne);

        // L11 should be unit lower triangular
        for i in 0..ne {
            assert!(
                (ff.l11[(i, i)] - 1.0).abs() < 1e-15,
                "L11 diagonal at {i} is not 1.0: {}",
                ff.l11[(i, i)]
            );
            for j in (i + 1)..ne {
                assert!(
                    ff.l11[(i, j)].abs() < 1e-15,
                    "L11 upper triangle at ({i},{j}) is not 0: {}",
                    ff.l11[(i, j)]
                );
            }
        }

        // L21 should have (m - ne) rows and ne columns
        assert_eq!(ff.l21.nrows(), m - ne);
        assert_eq!(ff.l21.ncols(), ne);

        // D11 dimension should match ne
        assert_eq!(ff.d11.dimension(), ne);
    }

    #[test]
    fn test_extract_contribution_rect_with_delays() {
        // Factor a rect matrix that produces some zero-pivot "delays",
        // call extract_contribution_rect, verify structure.
        let m = 6;
        let k = 4;
        let mut a = Mat::zeros(m, k);
        // Two good pivots (cols 0,1) and two zero-ish pivots (cols 2,3)
        let vals: [[f64; 4]; 6] = [
            [10.0, 0.0, 0.0, 0.0],
            [0.5, -8.0, 0.0, 0.0],
            [0.0, 0.0, 0.0, 0.0], // zero column → zero pivot
            [0.0, 0.0, 0.0, 0.0], // zero column → zero pivot
            [0.3, 0.1, 0.0, 0.0],
            [0.1, -0.3, 0.0, 0.0],
        ];
        for i in 0..m {
            for j in 0..k {
                a[(i, j)] = vals[i][j];
            }
        }

        let opts = AptpOptions::default();
        let result = tpp_factor_rect(a.as_mut(), k, &opts).unwrap();
        let ne = result.num_eliminated;

        // All 4 should be eliminated (2 real + 2 zero pivots)
        assert_eq!(ne, 4, "expected all 4 eliminated (2 real + 2 zero)");

        let nfs = m - k;
        let mut contrib_buffer = Mat::zeros(nfs, nfs);
        let mut ld_ws = Mat::zeros(nfs, ne);
        compute_contribution_gemm_rect(
            &a,
            k,
            ne,
            m,
            &result.d,
            &mut contrib_buffer,
            &mut ld_ws,
            Par::Seq,
        );

        let row_indices: Vec<usize> = (0..m).collect();
        let contrib = extract_contribution_rect(&a, m, k, &row_indices, &result, contrib_buffer);

        // With all columns eliminated, num_delayed = 0
        assert_eq!(contrib.num_delayed, 0);
        // Contribution block size = m - ne = 2
        assert_eq!(contrib.data.nrows(), nfs);
        assert_eq!(contrib.row_indices.len(), nfs);
    }

    #[test]
    fn test_swap_rect_boundary() {
        // Test swap_rect on a 5×3 matrix.
        let mut a = Mat::from_fn(5, 3, |i, j| (i * 10 + j) as f64);

        // Swap (1, 4) where j=4 >= ncols=3
        // Before: row 1 = [10, 11, 12], row 4 = [40, 41, 42]
        swap_rect(a.as_mut(), 1, 4);
        // After swap: rows 1 and 4 should exchange their entries in columns 0..3
        // Row data for cols < min(i,j)=1: a[(1,0)] ↔ a[(4,0)]
        assert!((a[(1, 0)] - 40.0).abs() < 1e-15, "a[(1,0)]={}", a[(1, 0)]);
        assert!((a[(4, 0)] - 10.0).abs() < 1e-15, "a[(4,0)]={}", a[(4, 0)]);
        // Intermediate rows 2,3: a[(k,1)] ↔ a[(4,k)] for k in 2..4
        // a[(2,1)] was 21, a[(4,2)] was 42
        assert!((a[(2, 1)] - 42.0).abs() < 1e-15, "a[(2,1)]={}", a[(2, 1)]);
        assert!((a[(4, 2)] - 21.0).abs() < 1e-15, "a[(4,2)]={}", a[(4, 2)]);

        // Now test swap where both < ncols: swap (0, 2) on a fresh matrix
        let mut b = Mat::from_fn(5, 3, |i, j| (i * 10 + j) as f64);
        // a[(0,0)]=0, a[(2,2)]=22, a[(1,0)]=10, a[(2,0)]=20, a[(2,1)]=21
        swap_rect(b.as_mut(), 0, 2);
        // Diagonals swap: b[(0,0)] ↔ b[(2,2)]
        assert!((b[(0, 0)] - 22.0).abs() < 1e-15, "b[(0,0)]={}", b[(0, 0)]);
        assert!((b[(2, 2)] - 0.0).abs() < 1e-15, "b[(2,2)]={}", b[(2, 2)]);
        // Rows k > j=2: b[(k,0)] ↔ b[(k,2)]
        assert!((b[(3, 0)] - 32.0).abs() < 1e-15, "b[(3,0)]={}", b[(3, 0)]);
        assert!((b[(3, 2)] - 30.0).abs() < 1e-15, "b[(3,2)]={}", b[(3, 2)]);
    }

    // =====================================================================
    // SPRAL-Style Torture Tests
    // =====================================================================
    //
    // These tests exercise the dense APTP kernel with randomized adversarial
    // perturbations, matching SPRAL's torture test approach.
    //
    // References:
    // - SPRAL tests/ssids/kernels/ldlt_app.cxx (BSD-3): torture test pattern
    // - SPRAL tests/ssids/kernels/ldlt_tpp.cxx (BSD-3): TPP torture test pattern

    use crate::testing::perturbations::TortureTestConfig;
    use rand::RngExt;
    use rand::SeedableRng;
    use rand::rngs::StdRng;

    /// Run APP (complete pivoting) torture test for a single (m, n) configuration.
    fn ldlt_app_torture_test(m: usize, n: usize, config: &TortureTestConfig) {
        use crate::testing::perturbations;

        let options = AptpOptions {
            inner_block_size: 32.min(n),
            ..AptpOptions::default()
        };
        let num_fully_summed = n;

        let mut rng = StdRng::seed_from_u64(config.seed);
        let mut failures = Vec::new();

        for instance in 0..config.num_instances {
            let mut a = perturbations::generate_dense_symmetric_indefinite(m, &mut rng);

            // Apply probabilistic perturbation (70/20/10 split)
            let roll: f64 = rng.random::<f64>();
            let is_singular;
            if roll < config.delay_probability {
                perturbations::cause_delays(&mut a, options.inner_block_size, &mut rng);
                is_singular = false;
            } else if roll < config.delay_probability + config.singular_probability {
                if m >= 2 {
                    let col1 = RngExt::random_range(&mut rng, 0..m);
                    let mut col2 = RngExt::random_range(&mut rng, 0..m);
                    while col2 == col1 {
                        col2 = RngExt::random_range(&mut rng, 0..m);
                    }
                    perturbations::make_singular(&mut a, col1, col2);
                }
                is_singular = true;
            } else {
                if m >= options.inner_block_size && options.inner_block_size >= 2 {
                    let max_start = m - options.inner_block_size;
                    let block_row = RngExt::random_range(&mut rng, 0..=max_start);
                    perturbations::make_dblk_singular(&mut a, block_row, options.inner_block_size);
                }
                is_singular = true;
            }

            // Capture the perturbed matrix BEFORE factorization (which modifies in-place)
            let original = a.clone();

            let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
                aptp_factor_in_place(a.as_mut(), num_fully_summed, &options)
            }));

            match result {
                Err(_) => {
                    failures.push(format!("instance {}: PANIC", instance));
                }
                Ok(Err(e)) => {
                    if !is_singular {
                        failures.push(format!("instance {}: unexpected error: {}", instance, e));
                    }
                }
                Ok(Ok(ref fr)) => {
                    let total = fr.num_eliminated + fr.delayed_cols.len();
                    if total != num_fully_summed {
                        failures.push(format!(
                            "instance {}: elim({}) + delayed({}) = {} != nfs({})",
                            instance,
                            fr.num_eliminated,
                            fr.delayed_cols.len(),
                            total,
                            num_fully_summed
                        ));
                        continue;
                    }
                    // Reconstruction check only for square (m == n) non-singular cases.
                    if m == n && !is_singular && fr.num_eliminated == num_fully_summed {
                        let l = extract_l(a.as_ref(), &fr.d, fr.num_eliminated);
                        let err = dense_reconstruction_error(
                            &original,
                            &l,
                            &fr.d,
                            &fr.perm[..fr.num_eliminated],
                        );
                        if err > config.backward_error_threshold {
                            failures.push(format!(
                                "instance {}: recon error {:.2e} > {:.2e}",
                                instance, err, config.backward_error_threshold
                            ));
                        }
                    }
                }
            }
        }

        assert!(
            failures.is_empty(),
            "APP torture (m={}, n={}) had {} failures:\n{}",
            m,
            n,
            failures.len(),
            failures.join("\n")
        );
    }

    /// Run TPP (threshold partial pivoting) torture test for a single (m, n) config.
    fn ldlt_tpp_torture_test(m: usize, n: usize, config: &TortureTestConfig) {
        use crate::testing::perturbations;

        let options = AptpOptions {
            inner_block_size: n + 1, // ensure TPP path
            ..AptpOptions::default()
        };
        let num_fully_summed = n;

        let mut rng = StdRng::seed_from_u64(config.seed);
        let mut failures = Vec::new();

        for instance in 0..config.num_instances {
            let mut a = perturbations::generate_dense_symmetric_indefinite(m, &mut rng);

            // TPP perturbation: 70% delays, 30% singular (no dblk_singular)
            let roll: f64 = rng.random::<f64>();
            let is_singular;
            if roll < config.delay_probability {
                perturbations::cause_delays(&mut a, options.inner_block_size, &mut rng);
                is_singular = false;
            } else {
                if m >= 2 {
                    let col1 = RngExt::random_range(&mut rng, 0..m);
                    let mut col2 = RngExt::random_range(&mut rng, 0..m);
                    while col2 == col1 {
                        col2 = RngExt::random_range(&mut rng, 0..m);
                    }
                    perturbations::make_singular(&mut a, col1, col2);
                }
                is_singular = true;
            }

            // Capture perturbed matrix BEFORE factorization
            let original = a.clone();

            let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
                aptp_factor_in_place(a.as_mut(), num_fully_summed, &options)
            }));

            match result {
                Err(_) => {
                    failures.push(format!("instance {}: PANIC", instance));
                }
                Ok(Err(e)) => {
                    if !is_singular {
                        failures.push(format!("instance {}: unexpected error: {}", instance, e));
                    }
                }
                Ok(Ok(ref fr)) => {
                    let total = fr.num_eliminated + fr.delayed_cols.len();
                    if total != num_fully_summed {
                        failures.push(format!(
                            "instance {}: elim({}) + delayed({}) = {} != nfs({})",
                            instance,
                            fr.num_eliminated,
                            fr.delayed_cols.len(),
                            total,
                            num_fully_summed
                        ));
                        continue;
                    }
                    // Reconstruction + L growth check only for square non-singular
                    if m == n && !is_singular && fr.num_eliminated == num_fully_summed {
                        let l = extract_l(a.as_ref(), &fr.d, fr.num_eliminated);
                        let err = dense_reconstruction_error(
                            &original,
                            &l,
                            &fr.d,
                            &fr.perm[..fr.num_eliminated],
                        );
                        if err > config.backward_error_threshold {
                            failures.push(format!(
                                "instance {}: recon error {:.2e} > {:.2e}",
                                instance, err, config.backward_error_threshold
                            ));
                        }
                        // L growth bound: max |L_ij| <= 1/threshold
                        let l_bound = 1.0 / options.threshold;
                        let mut max_l = 0.0_f64;
                        for i in 0..l.nrows() {
                            for j in 0..fr.num_eliminated {
                                if i != j {
                                    max_l = max_l.max(l[(i, j)].abs());
                                }
                            }
                        }
                        if max_l > l_bound * (1.0 + 1e-10) {
                            failures.push(format!(
                                "instance {}: L growth |L|={:.4} > 1/u={:.4}",
                                instance, max_l, l_bound
                            ));
                        }
                    }
                }
            }
        }

        assert!(
            failures.is_empty(),
            "TPP torture (m={}, n={}) had {} failures:\n{}",
            m,
            n,
            failures.len(),
            failures.join("\n")
        );
    }

    // ---- APP Torture Test Entry Points ----

    #[test]
    #[ignore] // Long-running: ~500 factorizations per test
    fn torture_app_32x32() {
        let config = TortureTestConfig {
            num_instances: 500,
            seed: 42_000,
            ..TortureTestConfig::default()
        };
        ldlt_app_torture_test(32, 32, &config);
    }

    #[test]
    #[ignore]
    fn torture_app_64x64() {
        let config = TortureTestConfig {
            num_instances: 500,
            seed: 42_001,
            ..TortureTestConfig::default()
        };
        ldlt_app_torture_test(64, 64, &config);
    }

    #[test]
    #[ignore]
    fn torture_app_128x128() {
        let config = TortureTestConfig {
            num_instances: 500,
            seed: 42_002,
            ..TortureTestConfig::default()
        };
        ldlt_app_torture_test(128, 128, &config);
    }

    #[test]
    #[ignore]
    fn torture_app_128x48() {
        let config = TortureTestConfig {
            num_instances: 500,
            seed: 42_003,
            ..TortureTestConfig::default()
        };
        ldlt_app_torture_test(128, 48, &config);
    }

    #[test]
    #[ignore]
    fn torture_app_256x256() {
        let config = TortureTestConfig {
            num_instances: 500,
            seed: 42_004,
            ..TortureTestConfig::default()
        };
        ldlt_app_torture_test(256, 256, &config);
    }

    // ---- TPP Torture Test Entry Points ----

    #[test]
    #[ignore]
    fn torture_tpp_8x4() {
        let config = TortureTestConfig {
            num_instances: 500,
            seed: 43_000,
            ..TortureTestConfig::default()
        };
        ldlt_tpp_torture_test(8, 4, &config);
    }

    #[test]
    #[ignore]
    fn torture_tpp_33x21() {
        let config = TortureTestConfig {
            num_instances: 500,
            seed: 43_001,
            ..TortureTestConfig::default()
        };
        ldlt_tpp_torture_test(33, 21, &config);
    }

    #[test]
    #[ignore]
    fn torture_tpp_100x100() {
        let config = TortureTestConfig {
            num_instances: 500,
            seed: 43_002,
            ..TortureTestConfig::default()
        };
        ldlt_tpp_torture_test(100, 100, &config);
    }

    #[test]
    #[ignore]
    fn torture_tpp_100x50() {
        let config = TortureTestConfig {
            num_instances: 500,
            seed: 43_003,
            ..TortureTestConfig::default()
        };
        ldlt_tpp_torture_test(100, 50, &config);
    }

    // =====================================================================
    // Property-Based Tests — Kernel Level
    // =====================================================================

    use crate::testing::strategies;
    use proptest::prelude::*;

    proptest! {
        #![proptest_config(ProptestConfig::with_cases(256))]

        #[test]
        fn property_pd_reconstruction(
            a in strategies::arb_symmetric_pd(5..=100)
        ) {
            let n = a.nrows();
            let options = AptpOptions::default();
            let result = aptp_factor(a.as_ref(), &options);
            if let Ok(ref f) = result {
                let perm_fwd = f.perm.as_ref().arrays().0;
                let err = dense_reconstruction_error(&a, &f.l, &f.d, perm_fwd);
                prop_assert!(
                    err < 1e-12,
                    "PD reconstruction error {:.2e} for n={}", err, n
                );
            }
        }

        #[test]
        fn property_inertia_sum(
            a in strategies::arb_symmetric_indefinite(5..=100)
        ) {
            let n = a.nrows();
            let options = AptpOptions::default();
            let result = aptp_factor(a.as_ref(), &options);
            if let Ok(ref f) = result {
                let inertia = f.d.compute_inertia();
                let num_eliminated = n - f.delayed_cols.len();
                // Inertia covers exactly the eliminated columns
                prop_assert_eq!(
                    inertia.dimension(), num_eliminated,
                    "inertia sum {} != num_eliminated={} (n={}, delayed={})",
                    inertia.dimension(), num_eliminated, n, f.delayed_cols.len()
                );
            }
        }

        #[test]
        fn property_permutation_valid(
            a in strategies::arb_symmetric_indefinite(5..=100)
        ) {
            let n = a.nrows();
            let options = AptpOptions::default();
            let result = aptp_factor(a.as_ref(), &options);
            if let Ok(ref f) = result {
                let (fwd, inv) = f.perm.as_ref().arrays();
                // Check fwd is a valid permutation
                let mut seen = vec![false; n];
                for &idx in fwd {
                    prop_assert!(idx < n, "perm fwd index {} out of bounds for n={}", idx, n);
                    prop_assert!(!seen[idx], "duplicate in perm fwd: {}", idx);
                    seen[idx] = true;
                }
                // Check fwd and inv are inverses
                for i in 0..n {
                    prop_assert_eq!(
                        inv[fwd[i]], i,
                        "perm fwd/inv not inverses at i={}", i
                    );
                }
            }
        }

        #[test]
        fn property_no_panics_perturbed(
            a in strategies::arb_symmetric_indefinite(5..=50),
            seed in any::<u64>()
        ) {
            use crate::testing::perturbations as perturb;
            let mut a_mut = a.clone();
            let mut rng = StdRng::seed_from_u64(seed);
            perturb::cause_delays(&mut a_mut, 32, &mut rng);

            let options = AptpOptions::default();
            // Should not panic — may return Ok or Err
            let _ = aptp_factor(a_mut.as_ref(), &options);
        }
    }
}