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
#![forbid(unsafe_code)]
//! Fixed-size, stack-allocated square matrices.
use core::hint::cold_path;
use crate::ldlt::Ldlt;
use crate::lu::Lu;
use crate::{ArithmeticOperation, ERR_COEFF_2, ERR_COEFF_3, ERR_COEFF_4, LaError, Tolerance};
/// A closed-form determinant and its certified absolute error bound.
///
/// Values of this type are produced by
/// [`Matrix::det_direct_with_errbound`]. The paired result guarantees that the
/// determinant was evaluated once and that its matching bound was computed for
/// the same matrix in one call. The guarantee is unavailable when gradual
/// underflow could invalidate the relative-error analysis or when the matrix
/// dimension exceeds the closed-form D ≤ 4 scope.
#[must_use]
#[non_exhaustive]
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct DeterminantWithErrorBound {
determinant: f64,
absolute_error_bound: f64,
}
impl DeterminantWithErrorBound {
/// Return the closed-form determinant approximation.
#[inline]
#[must_use]
pub const fn determinant(self) -> f64 {
self.determinant
}
/// Return the certified absolute error bound.
///
/// The exact determinant lies in
/// `[determinant - bound, determinant + bound]`.
#[inline]
#[must_use]
pub const fn absolute_error_bound(self) -> f64 {
self.absolute_error_bound
}
}
/// Finite fixed-size square matrix `D×D`, stored inline.
///
/// `Matrix` is designed for small, robustness-sensitive systems where stack
/// allocation and const-generic dimensions are useful. For large, dynamic, sparse,
/// or parallel workloads, prefer a broader linear-algebra crate such as
/// [`nalgebra`](https://crates.io/crates/nalgebra) or
/// [`faer`](https://crates.io/crates/faer).
///
/// Public construction and mutation reject NaN and infinity through
/// [`try_from_rows`](Self::try_from_rows) and [`set`](Self::set). The storage
/// field is private, so a
/// `Matrix` value carries the invariant that every stored entry is finite.
/// Algorithms therefore do not re-scan stored entries at every use; user-visible
/// non-finite errors come from construction/mutation boundaries or from values
/// computed during arithmetic, such as overflowed elimination or determinant
/// intermediates.
///
/// Direct field construction is intentionally unavailable to downstream callers:
///
/// ```compile_fail
/// use la_stack::Matrix;
///
/// let _ = Matrix::<2> {
/// rows: [[1.0, f64::NAN], [0.0, 1.0]],
/// };
/// ```
#[must_use]
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Matrix<const D: usize> {
rows: [[f64; D]; D],
}
/// A finite [`Matrix`] proven exactly symmetric for LDLT factorization.
///
/// Mirrored entries have equal numeric values; IEEE-754 signed zeros may have
/// different bit patterns because `+0.0 == -0.0`.
#[must_use]
#[derive(Clone, Copy, Debug, PartialEq)]
pub(crate) struct SymmetricMatrix<const D: usize> {
matrix: Matrix<D>,
}
/// Rounded arithmetic result together with proof that gradual underflow could
/// not have changed that operation's result.
///
/// The determinant filter may only use its relative-error coefficients while
/// every rounded operation in both the determinant and absolute-Leibniz trees
/// stays in the normal range. Exact structural zeros are safe; cancellation to
/// zero is conservatively treated as inconclusive.
#[derive(Clone, Copy, Debug, PartialEq)]
struct FilterArithmetic<const TRACK_UNDERFLOW: bool> {
value: f64,
underflow_safe: bool,
}
impl<const TRACK_UNDERFLOW: bool> FilterArithmetic<TRACK_UNDERFLOW> {
/// Return whether a rounded result is normal or non-finite.
///
/// A single exponent-field test keeps the overwhelmingly common normal
/// path cheap. Callers inspect operands only when the result is zero or
/// subnormal so they can distinguish structural zero from range loss.
#[expect(
clippy::inline_always,
reason = "determinant hot-path specialization must eliminate unused safety state"
)]
#[inline(always)]
const fn has_nonzero_exponent(value: f64) -> bool {
value.to_bits() & 0x7ff0_0000_0000_0000 != 0
}
/// Ordinary floating-point multiplication.
#[expect(
clippy::inline_always,
reason = "determinant hot-path specialization must eliminate unused safety state"
)]
#[inline(always)]
const fn multiply(lhs: f64, rhs: f64) -> Self {
let value = lhs * rhs;
Self {
value,
underflow_safe: !TRACK_UNDERFLOW
|| Self::has_nonzero_exponent(value)
|| lhs == 0.0
|| rhs == 0.0,
}
}
/// Ordinary addition of the non-negative terms used by the error-bound tree.
#[expect(
clippy::inline_always,
reason = "determinant hot-path specialization must eliminate unused safety state"
)]
#[inline(always)]
const fn add_non_negative(lhs: f64, rhs: f64) -> Self {
let value = lhs + rhs;
Self {
value,
underflow_safe: !TRACK_UNDERFLOW
|| Self::has_nonzero_exponent(value)
|| (lhs == 0.0 && rhs == 0.0),
}
}
/// Fused multiply-add.
#[expect(
clippy::inline_always,
reason = "determinant hot-path specialization must eliminate unused safety state"
)]
#[inline(always)]
const fn mul_add(lhs: f64, rhs: f64, addend: f64) -> Self {
let value = lhs.mul_add(rhs, addend);
Self {
value,
underflow_safe: !TRACK_UNDERFLOW
|| Self::has_nonzero_exponent(value)
|| ((lhs == 0.0 || rhs == 0.0) && addend == 0.0),
}
}
}
/// A finite D=4 matrix proven safe for shared-minor determinant and permanent
/// evaluation.
///
/// Construction proves both the fixed dimension and that every coefficient in
/// the first two rows is non-zero. The latter makes every shared 2×2 minor part
/// of an active Leibniz term, so the dense kernel cannot evaluate an overflowing
/// minor solely for a mathematically absent term.
#[repr(transparent)]
#[derive(Clone, Copy)]
struct Det4SharedMinorInput<'a, const D: usize> {
matrix: &'a Matrix<D>,
}
impl<'a, const D: usize> Det4SharedMinorInput<'a, D> {
/// Parse a matrix into the shared-minor D=4 domain.
///
/// `None` selects the guarded determinant path; it does not represent an
/// invalid public matrix.
#[expect(
clippy::inline_always,
reason = "the D=4 determinant hot path must eliminate its proof wrapper"
)]
#[inline(always)]
const fn try_new(matrix: &'a Matrix<D>) -> Option<Self> {
if D != 4 {
return None;
}
let r = &matrix.rows;
let shared_minors_are_active = (r[0][0] != 0.0)
&& (r[0][1] != 0.0)
&& (r[0][2] != 0.0)
&& (r[0][3] != 0.0)
&& (r[1][0] != 0.0)
&& (r[1][1] != 0.0)
&& (r[1][2] != 0.0)
&& (r[1][3] != 0.0);
if shared_minors_are_active {
Some(Self { matrix })
} else {
None
}
}
}
impl<const D: usize> SymmetricMatrix<D> {
/// Consume the wrapper and return the underlying matrix.
#[inline]
pub(crate) const fn into_matrix(self) -> Matrix<D> {
self.matrix
}
/// Construct a symmetric matrix proof without checking the invariant.
///
/// This constructor is only for paths that have already validated exact
/// mirrored-entry equality with the same predicate as
/// [`try_new`](Self::try_new). Finiteness is carried by [`Matrix`].
#[inline]
const fn new_unchecked(matrix: Matrix<D>) -> Self {
Self { matrix }
}
/// Validate that every mirrored pair has exactly the same finite value.
///
/// IEEE-754 signed zeros compare equal, so `+0.0` and `-0.0` satisfy this
/// mathematical-symmetry proof even though their bit patterns differ.
///
/// # Errors
/// Returns [`LaError::Asymmetric`] with `allowed_abs_diff == 0.0` when the
/// first off-diagonal pair is not exactly equal.
#[inline]
#[expect(
clippy::float_cmp,
reason = "LDLT requires exact mirrored-entry equality to factor the supplied operator"
)]
fn try_new(matrix: Matrix<D>) -> Result<Self, LaError> {
for row in 0..D {
for col in (row + 1)..D {
let upper = matrix.rows[row][col];
let lower = matrix.rows[col][row];
if upper != lower {
cold_path();
return Err(LaError::asymmetric(row, col, D, upper, lower, 0.0));
}
}
}
Ok(Self::new_unchecked(matrix))
}
}
impl<const D: usize> Matrix<D> {
/// Try to create a finite matrix from row-major storage.
///
/// This is the public raw-storage boundary for matrices. Successful
/// construction makes the returned [`Matrix`] a finite-storage proof.
///
/// # Examples
/// ```
/// use la_stack::prelude::*;
///
/// # fn main() -> Result<(), LaError> {
/// let m = Matrix::<2>::try_from_rows([[1.0, 2.0], [3.0, 4.0]])?;
/// assert_eq!(m.get(0, 1), Some(2.0));
/// # Ok(())
/// # }
/// ```
///
/// # Errors
/// Returns [`LaError::NonFinite`] with matrix coordinates for the first
/// offending entry in row-major order when `rows` contains NaN or infinity.
#[inline]
pub const fn try_from_rows(rows: [[f64; D]; D]) -> Result<Self, LaError> {
if let Some((row, col)) = Self::first_non_finite_cell(&rows) {
Err(LaError::non_finite_input_matrix(row, col))
} else {
Ok(Self::from_rows_unchecked(rows))
}
}
/// Construct a matrix without checking that entries are finite.
///
/// This module-private escape hatch is reserved for finite literals and
/// algorithm outputs whose finite invariant is visible at the call site.
/// Computed outputs must be validated before becoming observable API values.
#[inline]
const fn from_rows_unchecked(rows: [[f64; D]; D]) -> Self {
Self { rows }
}
/// Borrow the finite row-major backing array.
///
/// The returned view is tied to this [`Matrix`], so callers can inspect the
/// canonical storage without copying it or bypassing the finite-value
/// invariant.
///
/// # Examples
/// ```
/// use la_stack::prelude::*;
///
/// # fn main() -> Result<(), LaError> {
/// let matrix = Matrix::<2>::try_from_rows([[1.0, 2.0], [3.0, 4.0]])?;
/// assert_eq!(matrix.as_rows(), &[[1.0, 2.0], [3.0, 4.0]]);
/// # Ok(())
/// # }
/// ```
///
/// A live view keeps the matrix immutably borrowed, so validated mutation
/// cannot occur until the view is no longer used:
///
/// ```compile_fail
/// use la_stack::Matrix;
///
/// let mut matrix = Matrix::<2>::identity();
/// let rows = matrix.as_rows();
/// assert!(matrix.set(0, 0, 5.0).is_ok());
/// assert_eq!(rows[0][0], 1.0);
/// ```
#[inline]
#[must_use]
pub const fn as_rows(&self) -> &[[f64; D]; D] {
&self.rows
}
/// Consume this matrix and return its finite row-major backing array.
///
/// # Examples
/// ```
/// use la_stack::prelude::*;
///
/// # fn main() -> Result<(), LaError> {
/// let matrix = Matrix::<2>::try_from_rows([[1.0, 2.0], [3.0, 4.0]])?;
/// assert_eq!(matrix.into_rows(), [[1.0, 2.0], [3.0, 4.0]]);
/// # Ok(())
/// # }
/// ```
#[inline]
#[must_use]
pub const fn into_rows(self) -> [[f64; D]; D] {
self.rows
}
/// All-zeros finite matrix.
///
/// # Examples
/// ```
/// use la_stack::prelude::*;
///
/// let z = Matrix::<2>::zero();
/// assert_eq!(z.get(1, 1), Some(0.0));
/// ```
#[inline]
pub const fn zero() -> Self {
Self::from_rows_unchecked([[0.0; D]; D])
}
/// Finite identity matrix.
///
/// # Examples
/// ```
/// use la_stack::prelude::*;
///
/// let i = Matrix::<3>::identity();
/// assert_eq!(i.get(0, 0), Some(1.0));
/// assert_eq!(i.get(0, 1), Some(0.0));
/// assert_eq!(i.get(2, 2), Some(1.0));
/// ```
#[inline]
pub const fn identity() -> Self {
let mut m = Self::zero();
let mut i = 0;
while i < D {
m.rows[i][i] = 1.0;
i += 1;
}
m
}
/// Get a finite element with bounds checking.
///
/// # Examples
/// ```
/// use la_stack::prelude::*;
///
/// # fn main() -> Result<(), LaError> {
/// let m = Matrix::<2>::try_from_rows([[1.0, 2.0], [3.0, 4.0]])?;
/// assert_eq!(m.get(1, 0), Some(3.0));
/// assert_eq!(m.get(2, 0), None);
/// # Ok(())
/// # }
/// ```
#[inline]
#[must_use]
pub const fn get(&self, row: usize, col: usize) -> Option<f64> {
if row < D && col < D {
Some(self.rows[row][col])
} else {
None
}
}
/// Get a finite element, preserving index context on failure.
///
/// Prefer [`get`](Self::get) for const or hot paths that only need
/// `Option`-style absence. Use this method at public runtime boundaries
/// where row, column, and dimension context should survive in a typed error.
///
/// # Examples
/// ```
/// use core::assert_matches;
/// use la_stack::prelude::*;
///
/// # fn main() -> Result<(), LaError> {
/// let m = Matrix::<2>::try_from_rows([[1.0, 2.0], [3.0, 4.0]])?;
/// assert_eq!(m.try_get(1, 0)?, 3.0);
/// assert_matches!(
/// m.try_get(2, 0),
/// Err(LaError::IndexOutOfBounds {
/// row: 2,
/// col: 0,
/// dim: 2,
/// ..
/// })
/// );
/// # Ok(())
/// # }
/// ```
///
/// # Errors
/// Returns [`LaError::IndexOutOfBounds`] when either index is not `< D`.
#[inline]
pub const fn try_get(&self, row: usize, col: usize) -> Result<f64, LaError> {
if row < D && col < D {
Ok(self.rows[row][col])
} else {
Err(LaError::index_out_of_bounds(row, col, D))
}
}
/// Set a finite element with bounds checking.
///
/// # Examples
/// ```
/// use core::assert_matches;
/// use la_stack::prelude::*;
///
/// # fn main() -> Result<(), LaError> {
/// let mut m = Matrix::<2>::zero();
/// assert_eq!(m.set(0, 1, 2.5), Ok(()));
/// assert_eq!(m.get(0, 1), Some(2.5));
/// assert_matches!(
/// m.set(10, 0, 1.0),
/// Err(LaError::IndexOutOfBounds {
/// row: 10,
/// col: 0,
/// dim: 2,
/// ..
/// })
/// );
/// # Ok(())
/// # }
/// ```
///
/// # Errors
/// Returns [`LaError::IndexOutOfBounds`] when either index is not `< D`.
/// Returns [`LaError::NonFinite`] when `value` is NaN or infinity.
#[inline]
pub const fn set(&mut self, row: usize, col: usize, value: f64) -> Result<(), LaError> {
if row >= D || col >= D {
return Err(LaError::index_out_of_bounds(row, col, D));
}
if !value.is_finite() {
return Err(LaError::non_finite_input_matrix(row, col));
}
self.rows[row][col] = value;
Ok(())
}
/// Infinity norm (maximum absolute row sum).
///
/// # Non-finite handling
/// Public constructors and setters reject raw non-finite entries, but
/// `Matrix` values are finite by construction. `inf_norm` returns
/// [`LaError::NonFinite`] with the matrix cell whose addition first makes a
/// row sum non-finite.
///
/// Row sums are accumulated in `f64` with ordinary addition. This method
/// checks for overflowed accumulators, but it does not provide a certified
/// absolute rounding bound for the returned norm.
///
/// # Examples
/// ```
/// use core::assert_matches;
/// use la_stack::prelude::*;
///
/// # fn main() -> Result<(), LaError> {
/// let m = Matrix::<2>::try_from_rows([[1.0, -2.0], [3.0, 4.0]])?;
/// assert!((m.inf_norm()? - 7.0).abs() <= 1e-12);
///
/// // Raw NaN entries are rejected with coordinates.
/// assert_matches!(
/// Matrix::<2>::try_from_rows([[f64::NAN, 1.0], [2.0, 3.0]]),
/// Err(LaError::NonFinite {
/// location: NonFiniteLocation::MatrixCell { row: 0, col: 0, .. },
/// origin: NonFiniteOrigin::Input,
/// ..
/// })
/// );
/// # Ok(())
/// # }
/// ```
///
/// # Errors
/// Returns [`LaError::NonFinite`] with matrix coordinates when a row sum
/// overflows to NaN or infinity.
#[inline]
pub const fn inf_norm(&self) -> Result<f64, LaError> {
let mut max_row_sum: f64 = 0.0;
let mut r = 0;
while r < D {
let row = &self.rows[r];
let mut row_sum: f64 = 0.0;
let mut c = 0;
while c < D {
row_sum += row[c].abs();
c += 1;
}
if !row_sum.is_finite() {
cold_path();
return Err(Self::inf_norm_overflow_error(row, r));
}
if row_sum > max_row_sum {
max_row_sum = row_sum;
}
r += 1;
}
Ok(max_row_sum)
}
/// Replay an overflowed infinity-norm row to locate the first non-finite sum.
///
/// This runs only after the success-path traversal has found a non-finite
/// completed row sum. Because stored entries are finite and their absolute
/// values are non-negative, replaying the same additions must find the
/// first column whose addition overflowed; if every earlier prefix is
/// finite, the final column is that first failure.
#[cold]
const fn inf_norm_overflow_error(row: &[f64; D], row_index: usize) -> LaError {
let mut row_sum = 0.0;
let mut col = 0;
let last_col = D.saturating_sub(1);
while col < last_col {
row_sum += row[col].abs();
if !row_sum.is_finite() {
return LaError::non_finite_computation_matrix(
ArithmeticOperation::MatrixInfinityNorm,
row_index,
col,
);
}
col += 1;
}
LaError::non_finite_computation_matrix(
ArithmeticOperation::MatrixInfinityNorm,
row_index,
last_col,
)
}
/// Returns `true` if the matrix is approximately symmetric within a relative tolerance.
///
/// Two entries `self[r][c]` and `self[c][r]` are considered equal (for the
/// purposes of symmetry) when
/// `|self[r][c] - self[c][r]| <= rel_tol * max(1.0, inf_norm(self))`.
/// This is a diagnostic predicate for applications that have an
/// approximation-specific symmetry threshold. It is not the precondition
/// used by [`ldlt`](Self::ldlt), which requires exact mirrored-entry
/// equality so the returned factors represent the original matrix.
///
/// Use [`first_asymmetry`](Self::first_asymmetry) to locate the first
/// offending pair when this returns `Ok(false)`.
///
/// The `rel_tol` argument is a [`Tolerance`], so raw caller input must be
/// finite and non-negative before it can reach this predicate. Use
/// [`Tolerance::try_new`] when accepting a raw `f64`; negative, NaN, and
/// infinite tolerances return
/// [`LaError::InvalidTolerance`].
///
/// # Overflow handling
/// A finite matrix can return [`LaError::NonFinite`] with matrix coordinates
/// if computing the scaled symmetry tolerance overflows to NaN or infinity.
/// If both stored entries are finite but their difference overflows to ±∞,
/// the pair is reported as asymmetric.
///
/// # Examples
/// ```
/// use la_stack::prelude::*;
///
/// # fn main() -> Result<(), LaError> {
/// let a = Matrix::<2>::try_from_rows([[4.0, 2.0], [2.0, 3.0]])?;
/// let tol = Tolerance::try_new(1e-12)?;
/// assert!(a.is_symmetric(tol)?);
///
/// let b = Matrix::<2>::try_from_rows([[4.0, 2.0], [3.0, 3.0]])?;
/// assert!(!b.is_symmetric(tol)?);
/// # Ok(())
/// # }
/// ```
///
/// # Errors
/// Returns [`LaError::NonFinite`] with matrix coordinates when computing the
/// scaled symmetry tolerance overflows to NaN or infinity.
#[inline]
pub fn is_symmetric(&self, rel_tol: Tolerance) -> Result<bool, LaError> {
Ok(self.first_asymmetry(rel_tol)?.is_none())
}
/// Returns the indices `(r, c)` (with `r < c`) of the first off-diagonal
/// pair that violates approximate symmetry, or `None` if the matrix is
/// symmetric within `rel_tol`.
///
/// Iteration order is row-major over the strict upper triangle, so the
/// returned indices are the lexicographically smallest such pair. The
/// predicate is the same as [`is_symmetric`](Self::is_symmetric):
/// `|self[r][c] - self[c][r]| <= rel_tol * max(1.0, inf_norm(self))`.
/// It is intentionally distinct from the exact equality required by
/// [`ldlt`](Self::ldlt).
///
/// A finite matrix can return [`LaError::NonFinite`] with matrix coordinates
/// if computing the scaled symmetry tolerance overflows to NaN or infinity.
/// If both stored entries are finite but their difference overflows to ±∞,
/// the pair is reported as asymmetric.
///
/// The `rel_tol` argument is a [`Tolerance`], so raw caller input must be
/// finite and non-negative before it can reach this predicate. Use
/// [`Tolerance::try_new`] when accepting a raw `f64`; negative, NaN, and
/// infinite tolerances return
/// [`LaError::InvalidTolerance`].
///
/// # Examples
/// ```
/// use la_stack::prelude::*;
///
/// # fn main() -> Result<(), LaError> {
/// let a = Matrix::<3>::try_from_rows([
/// [1.0, 2.0, 0.0],
/// [2.0, 4.0, 5.0],
/// [0.0, 6.0, 9.0], // 6.0 breaks symmetry with a[1][2] = 5.0
/// ])?;
/// let tol = Tolerance::try_new(1e-12)?;
/// assert_eq!(a.first_asymmetry(tol)?, Some((1, 2)));
/// assert_eq!(Matrix::<3>::identity().first_asymmetry(tol)?, None);
/// # Ok(())
/// # }
/// ```
///
/// # Errors
/// Returns [`LaError::NonFinite`] with matrix coordinates when computing the
/// scaled symmetry tolerance overflows to NaN or infinity.
#[inline]
pub fn first_asymmetry(&self, rel_tol: Tolerance) -> Result<Option<(usize, usize)>, LaError> {
let eps = self.symmetry_epsilon(rel_tol)?;
for r in 0..D {
for c in (r + 1)..D {
let upper = self.rows[r][c];
let lower = self.rows[c][r];
let diff = (upper - lower).abs();
if !diff.is_finite() || diff > eps {
cold_path();
return Ok(Some((r, c)));
}
}
}
Ok(None)
}
/// Compute an LU decomposition with partial pivoting.
///
/// `D = 0` follows the empty-matrix convention: factorization succeeds,
/// [`Lu::det`](crate::Lu::det) returns `1.0`, and solving a length-zero
/// right-hand side returns a length-zero [`Vector`](crate::Vector).
/// Partial pivoting is a practical finite-precision strategy, not a
/// certified accuracy guarantee; see `REFERENCES.md` \[1-3, 11-12\].
///
/// # Examples
/// ```
/// use la_stack::prelude::*;
///
/// # fn main() -> Result<(), LaError> {
/// let a = Matrix::<2>::try_from_rows([[1.0, 2.0], [3.0, 4.0]])?;
/// let lu = a.lu(DEFAULT_SINGULAR_TOL)?;
///
/// let b = Vector::<2>::try_new([5.0, 11.0])?;
/// let x = lu.solve(b)?.into_array();
///
/// assert!((x[0] - 1.0).abs() <= 1e-12);
/// assert!((x[1] - 2.0).abs() <= 1e-12);
/// # Ok(())
/// # }
/// ```
///
/// Empty matrices use the standard empty-product convention:
///
/// ```
/// use la_stack::prelude::*;
///
/// # fn main() -> Result<(), LaError> {
/// let lu = Matrix::<0>::zero().lu(DEFAULT_SINGULAR_TOL)?;
///
/// assert_eq!(lu.det()?, 1.0);
/// assert!(lu.solve(Vector::<0>::zero())?.into_array().is_empty());
/// # Ok(())
/// # }
/// ```
///
/// The `tol` argument is a [`Tolerance`], so raw caller input must be
/// finite and non-negative before it can reach factorization. Use
/// [`Tolerance::try_new`] when accepting a raw `f64`; negative, NaN, and
/// infinite tolerances return
/// [`LaError::InvalidTolerance`].
///
/// # Errors
/// Returns [`LaError::Singular`] if, for some column `k`, the largest-magnitude candidate pivot
/// in that column satisfies `|pivot| <= tol` (so no numerically usable pivot exists).
/// Returns [`LaError::NonFinite`] if an elimination intermediate overflows
/// to NaN/∞ before it can be stored in the returned [`Lu`].
#[inline]
pub fn lu(self, tol: Tolerance) -> Result<Lu<D>, LaError> {
Lu::factor_finite(self, tol)
}
/// Compute an LDLT factorization (`A = L D Láµ€`) without pivoting.
///
/// `D = 0` follows the empty-matrix convention: factorization succeeds,
/// [`Ldlt::det`](crate::Ldlt::det) returns `1.0`, and solving a length-zero
/// right-hand side returns a length-zero [`Vector`](crate::Vector).
///
/// This is intended for exactly symmetric positive-definite matrices such
/// as nonsingular Gram matrices. Computed zero and tolerance-small positive
/// pivots are diagnosed as singular rather than returned in a usable
/// factorization. Because pivots are computed in binary64, success is not
/// an exact proof that the stored matrix is positive definite.
/// See `REFERENCES.md` \[4-6, 11-12\] for Cholesky/LDLT background and the
/// pivoted symmetric-indefinite alternative.
///
/// # Symmetry validation
/// The input matrix `self` must be exactly symmetric: every mirrored pair
/// must satisfy `self[i][j] == self[j][i]`. IEEE-754 signed zeros compare
/// equal and are therefore accepted. Exact equality is a correctness
/// invariant, not merely a performance hint: LDLT reads only the lower
/// triangle, so accepting an approximate mismatch would factor a different
/// operator than the matrix supplied by the caller. Asymmetric inputs return
/// [`LaError::Asymmetric`] with an allowed absolute difference of `0.0`
/// before factorization starts.
///
/// [`is_symmetric`](Self::is_symmetric) remains available as a
/// tolerance-based diagnostic, but `Ok(true)` from that method does not
/// establish this exact LDLT precondition. If you need a general-purpose
/// factorization for a non-symmetric matrix, use [`lu`](Self::lu) instead.
///
/// The `tol` argument is a [`Tolerance`], so raw caller input must be
/// finite and non-negative before it can reach factorization. Use
/// [`Tolerance::try_new`] when accepting a raw `f64`; negative, NaN, and
/// infinite tolerances return
/// [`LaError::InvalidTolerance`].
///
/// # Examples
/// ```
/// use la_stack::prelude::*;
///
/// # fn main() -> Result<(), LaError> {
/// // Note the symmetric layout: a[0][1] == a[1][0] == 2.0.
/// let a = Matrix::<2>::try_from_rows([[4.0, 2.0], [2.0, 3.0]])?;
/// let ldlt = a.ldlt(DEFAULT_SINGULAR_TOL)?;
///
/// // det(A) = 8
/// assert!((ldlt.det()? - 8.0).abs() <= 1e-12);
///
/// // Solve A x = b
/// let b = Vector::<2>::try_new([1.0, 2.0])?;
/// let x = ldlt.solve(b)?.into_array();
/// assert!((x[0] - (-0.125)).abs() <= 1e-12);
/// assert!((x[1] - 0.75).abs() <= 1e-12);
/// # Ok(())
/// # }
/// ```
///
/// Empty matrices use the standard empty-product convention:
///
/// ```
/// use la_stack::prelude::*;
///
/// # fn main() -> Result<(), LaError> {
/// let ldlt = Matrix::<0>::zero().ldlt(DEFAULT_SINGULAR_TOL)?;
///
/// assert_eq!(ldlt.det()?, 1.0);
/// assert!(ldlt.solve(Vector::<0>::zero())?.into_array().is_empty());
/// # Ok(())
/// # }
/// ```
///
/// # Errors
/// Returns [`LaError::NotPositiveSemidefinite`] if a pivot is negative or a
/// zero pivot retains a non-zero coupling below it.
/// Returns [`LaError::Singular`] if a zero pivot has no remaining coupling,
/// or if a positive pivot satisfies `d <= tol`, treating PSD degeneracy as
/// singular.
/// Returns [`LaError::NonFinite`] if factorization computes a non-finite
/// intermediate.
/// Returns [`LaError::Asymmetric`] if the input matrix is not symmetric.
#[inline]
pub fn ldlt(self, tol: Tolerance) -> Result<Ldlt<D>, LaError> {
Ldlt::factor_symmetric(SymmetricMatrix::try_new(self)?, tol)
}
/// Return the first non-finite stored cell in row-major order.
const fn first_non_finite_cell(rows: &[[f64; D]; D]) -> Option<(usize, usize)> {
let mut r = 0;
while r < D {
let mut c = 0;
while c < D {
if !rows[r][c].is_finite() {
return Some((r, c));
}
c += 1;
}
r += 1;
}
None
}
/// Compute the approximate-symmetry tolerance scale for a finite matrix.
///
/// This helper protects the public [`is_symmetric`](Self::is_symmetric) and
/// [`first_asymmetry`](Self::first_asymmetry) diagnostic contracts: the
/// documented norm-first formula is used whenever its intermediate is
/// representable, while an overflow-safe termwise fallback reports the
/// matrix cell that makes the scaled tolerance non-finite.
fn symmetry_epsilon(&self, rel_tol: Tolerance) -> Result<f64, LaError> {
let rel_tol = rel_tol.get();
if rel_tol == 0.0 {
return Ok(rel_tol);
}
if let Ok(norm) = self.inf_norm() {
let scale = if norm > 1.0 { norm } else { 1.0 };
let eps = rel_tol * scale;
if eps.is_finite() {
return Ok(eps);
}
}
// If the unscaled row sum or the final multiplication overflows, apply
// the tolerance to each non-negative contribution before summing. A row
// can overflow only at magnitudes where multiplication by the smallest
// positive tolerance is normal, so this fallback cannot introduce the
// gradual-underflow discrepancy avoided by the direct path above.
let mut eps = rel_tol;
for r in 0..D {
let mut row_eps = 0.0;
for c in 0..D {
row_eps = rel_tol.mul_add(self.rows[r][c].abs(), row_eps);
if !row_eps.is_finite() {
cold_path();
return Err(LaError::non_finite_computation_matrix(
ArithmeticOperation::SymmetryCheck,
r,
c,
));
}
}
if row_eps > eps {
eps = row_eps;
}
}
Ok(eps)
}
/// Closed-form determinant for dimensions 0–4, bypassing LU factorization.
///
/// Returns `Ok(Some(det))` for `D` ∈ {0, 1, 2, 3, 4}, `Ok(None)` for D ≥ 5.
/// `D = 0` returns `Ok(Some(1.0))` (empty product).
/// This is a `const fn` (Rust 1.94+) and uses fused multiply-add (`mul_add`)
/// for improved accuracy and performance.
///
/// For a determinant that works for any dimension (falling back to LU for D ≥ 5),
/// use [`det`](Self::det).
///
/// # Examples
/// ```
/// use la_stack::prelude::*;
///
/// # fn main() -> Result<(), LaError> {
/// let m = Matrix::<2>::try_from_rows([[1.0, 2.0], [3.0, 4.0]])?;
/// assert_eq!(m.det_direct()?, Some(-2.0));
///
/// // D = 0 is the empty product.
/// assert_eq!(Matrix::<0>::zero().det_direct()?, Some(1.0));
///
/// // D ≥ 5 returns None.
/// assert!(Matrix::<5>::identity().det_direct()?.is_none());
/// # Ok(())
/// # }
/// ```
///
/// # Errors
/// Returns [`LaError::NonFinite`] when the closed-form determinant overflows
/// to NaN or infinity.
#[inline]
pub const fn det_direct(&self) -> Result<Option<f64>, LaError> {
let Some(det) = self.det_direct_arithmetic::<false>() else {
cold_path();
return Ok(None);
};
Self::computed_scalar_result(ArithmeticOperation::Determinant, det.value)
}
/// Evaluate the closed-form determinant while certifying every rounded
/// operation against gradual underflow.
#[expect(
clippy::inline_always,
reason = "det_direct callers must eliminate unused filter-safety bookkeeping"
)]
#[inline(always)]
const fn det_direct_arithmetic<const TRACK_UNDERFLOW: bool>(
&self,
) -> Option<FilterArithmetic<TRACK_UNDERFLOW>> {
match D {
0 => Some(FilterArithmetic {
value: 1.0,
underflow_safe: true,
}),
1 => Some(FilterArithmetic {
value: self.rows[0][0],
underflow_safe: true,
}),
2 => {
let a = self.rows[0][0];
let b = self.rows[0][1];
let c = self.rows[1][0];
let d = self.rows[1][1];
let subtrahend = FilterArithmetic::<TRACK_UNDERFLOW>::multiply(b, c);
let mut det = FilterArithmetic::<TRACK_UNDERFLOW>::mul_add(a, d, -subtrahend.value);
det.underflow_safe &= subtrahend.underflow_safe;
Some(det)
}
3 => Some(Self::det3_elements::<TRACK_UNDERFLOW>(
[self.rows[0][0], self.rows[0][1], self.rows[0][2]],
[self.rows[1][0], self.rows[1][1], self.rows[1][2]],
[self.rows[2][0], self.rows[2][1], self.rows[2][2]],
)),
4 => {
if !TRACK_UNDERFLOW && let Some(input) = Det4SharedMinorInput::try_new(self) {
return Some(FilterArithmetic {
value: Self::det4_dense_elements(input),
underflow_safe: true,
});
}
let r = &self.rows;
let mut det = if r[0][3] == 0.0 {
FilterArithmetic {
value: 0.0,
underflow_safe: true,
}
} else {
let c03 = Self::det3_elements::<TRACK_UNDERFLOW>(
[r[1][0], r[1][1], r[1][2]],
[r[2][0], r[2][1], r[2][2]],
[r[3][0], r[3][1], r[3][2]],
);
let mut term =
FilterArithmetic::<TRACK_UNDERFLOW>::multiply(r[0][3], c03.value);
term.value = -term.value;
term.underflow_safe &= c03.underflow_safe;
term
};
if r[0][2] != 0.0 {
let c02 = Self::det3_elements::<TRACK_UNDERFLOW>(
[r[1][0], r[1][1], r[1][3]],
[r[2][0], r[2][1], r[2][3]],
[r[3][0], r[3][1], r[3][3]],
);
let prior_safe = det.underflow_safe && c02.underflow_safe;
det =
FilterArithmetic::<TRACK_UNDERFLOW>::mul_add(r[0][2], c02.value, det.value);
det.underflow_safe &= prior_safe;
}
if r[0][1] != 0.0 {
let c01 = Self::det3_elements::<TRACK_UNDERFLOW>(
[r[1][0], r[1][2], r[1][3]],
[r[2][0], r[2][2], r[2][3]],
[r[3][0], r[3][2], r[3][3]],
);
let prior_safe = det.underflow_safe && c01.underflow_safe;
det = FilterArithmetic::<TRACK_UNDERFLOW>::mul_add(
-r[0][1], c01.value, det.value,
);
det.underflow_safe &= prior_safe;
}
if r[0][0] != 0.0 {
let c00 = Self::det3_elements::<TRACK_UNDERFLOW>(
[r[1][1], r[1][2], r[1][3]],
[r[2][1], r[2][2], r[2][3]],
[r[3][1], r[3][2], r[3][3]],
);
let prior_safe = det.underflow_safe && c00.underflow_safe;
det =
FilterArithmetic::<TRACK_UNDERFLOW>::mul_add(r[0][0], c00.value, det.value);
det.underflow_safe &= prior_safe;
}
Some(det)
}
_ => None,
}
}
/// Evaluate the proof-bearing 4×4 cofactor expansion with shared 2×2 minors.
///
/// When no intermediate undergoes gradual underflow, the rounding error is
/// bounded by `ERR_COEFF_4 · p(|A|)`, where `p(|A|)` is the absolute Leibniz
/// sum. This helper returns only the determinant; use
/// [`Self::det_errbound`] or [`Self::det_direct_with_errbound`] to obtain the
/// certified bound.
#[expect(
clippy::inline_always,
reason = "the D=4 determinant hot path must inline its shared-minor expansion"
)]
#[inline(always)]
const fn det4_dense_elements(input: Det4SharedMinorInput<'_, D>) -> f64 {
let r = &input.matrix.rows;
let s23 = r[2][2].mul_add(r[3][3], -(r[2][3] * r[3][2]));
let s13 = r[2][1].mul_add(r[3][3], -(r[2][3] * r[3][1]));
let s12 = r[2][1].mul_add(r[3][2], -(r[2][2] * r[3][1]));
let s03 = r[2][0].mul_add(r[3][3], -(r[2][3] * r[3][0]));
let s02 = r[2][0].mul_add(r[3][2], -(r[2][2] * r[3][0]));
let s01 = r[2][0].mul_add(r[3][1], -(r[2][1] * r[3][0]));
let c00 = r[1][1].mul_add(s23, (-r[1][2]).mul_add(s13, r[1][3] * s12));
let c01 = r[1][0].mul_add(s23, (-r[1][2]).mul_add(s03, r[1][3] * s02));
let c02 = r[1][0].mul_add(s13, (-r[1][1]).mul_add(s03, r[1][3] * s01));
let c03 = r[1][0].mul_add(s12, (-r[1][1]).mul_add(s02, r[1][2] * s01));
r[0][0].mul_add(
c00,
(-r[0][1]).mul_add(c01, r[0][2].mul_add(c02, -(r[0][3] * c03))),
)
}
/// Evaluate the dense 4×4 absolute permanent with shared 2×2 minors.
///
/// The proof carried by `input` makes every shared minor part of an active
/// Leibniz term. The caller separately establishes a wide exponent margin,
/// so this branch-free kernel cannot hide gradual underflow or evaluate an
/// overflowing minor for a mathematically absent term.
#[expect(
clippy::inline_always,
reason = "the D=4 determinant filter must inline its shared-minor permanent"
)]
#[inline(always)]
const fn det4_dense_abs_permanent_elements(input: Det4SharedMinorInput<'_, D>) -> f64 {
let r = &input.matrix.rows;
let sp23 = (r[2][2] * r[3][3]).abs() + (r[2][3] * r[3][2]).abs();
let sp13 = (r[2][1] * r[3][3]).abs() + (r[2][3] * r[3][1]).abs();
let sp12 = (r[2][1] * r[3][2]).abs() + (r[2][2] * r[3][1]).abs();
let sp03 = (r[2][0] * r[3][3]).abs() + (r[2][3] * r[3][0]).abs();
let sp02 = (r[2][0] * r[3][2]).abs() + (r[2][2] * r[3][0]).abs();
let sp01 = (r[2][0] * r[3][1]).abs() + (r[2][1] * r[3][0]).abs();
let pc0 = r[1][3]
.abs()
.mul_add(sp12, r[1][2].abs().mul_add(sp13, r[1][1].abs() * sp23));
let pc1 = r[1][3]
.abs()
.mul_add(sp02, r[1][2].abs().mul_add(sp03, r[1][0].abs() * sp23));
let pc2 = r[1][3]
.abs()
.mul_add(sp01, r[1][1].abs().mul_add(sp03, r[1][0].abs() * sp13));
let pc3 = r[1][2]
.abs()
.mul_add(sp01, r[1][1].abs().mul_add(sp02, r[1][0].abs() * sp12));
r[0][3].abs().mul_add(
pc3,
r[0][2]
.abs()
.mul_add(pc2, r[0][1].abs().mul_add(pc1, r[0][0].abs() * pc0)),
)
}
/// Floating-point determinant, using closed-form formulas for D ≤ 4 and
/// LU decomposition for D ≥ 5.
///
/// For D ∈ {1, 2, 3, 4}, this bypasses LU factorization entirely for a significant
/// speedup (see [`det_direct`](Self::det_direct)).
///
/// Because this method mixes closed-form paths from
/// [`det_direct`](Self::det_direct) with an LU fallback, the returned value has
/// no certified absolute error bound. Use
/// [`det_errbound`](Self::det_errbound) for D ≤ 4 bounds, or the exact
/// determinant APIs when exact singularity classification or certified values
/// matter. For D ≥ 5, the zero-tolerance LU fallback surfaces
/// [`LaError::Singular`] when elimination cannot produce a non-zero pivot.
/// Floating-point elimination cannot in general distinguish an exactly
/// singular matrix from a non-singular matrix whose intermediate pivot
/// rounded to zero, so this method never converts that numerical failure into
/// an exact `0.0` result.
///
/// # Examples
/// ```
/// use la_stack::prelude::*;
///
/// # fn main() -> Result<(), LaError> {
/// let det = Matrix::<3>::identity().det()?;
/// assert!((det - 1.0).abs() <= 1e-12);
/// # Ok(())
/// # }
/// ```
///
/// The LU fallback accumulates its diagonal product with power-of-two
/// scaling, so factor order cannot cause premature overflow or underflow in
/// the final product. Elimination intermediates remain subject to binary64
/// rounding and range limits.
///
/// # Errors
/// Returns [`LaError::Singular`] if the D ≥ 5 LU fallback cannot produce a
/// non-zero pivot, including when a non-zero mathematical intermediate rounds
/// to zero during elimination. Returns [`LaError::NonFinite`] if a D ≤ 4
/// closed-form result is non-finite, if the LU fallback computes a
/// non-finite factorization cell, or if its final scaled determinant cannot
/// be represented as a finite `f64`.
#[inline]
pub fn det(self) -> Result<f64, LaError> {
if let Some(d) = self.det_direct()? {
return Ok(d);
}
self.lu(Tolerance::ZERO)?.det()
}
/// Evaluate `det_direct()` and its absolute error bound together.
///
/// Returns `Ok(Some(result))` for D ≤ 4 when the relative-error analysis
/// is valid. The result contains the closed-form determinant and a bound
/// such that `|result.determinant() - det_exact| ≤
/// result.absolute_error_bound()`. Returns `Ok(None)` when gradual
/// underflow could invalidate that analysis or for D ≥ 5, where no
/// closed-form bound is available.
///
/// This is the preferred API when both values are needed: it evaluates the
/// determinant arithmetic tree once, then computes the matching bound for
/// the same matrix within that call.
///
/// # Examples
/// ```
/// use la_stack::prelude::*;
///
/// # fn main() -> Result<(), LaError> {
/// let matrix = Matrix::<2>::try_from_rows([[1.0, 2.0], [3.0, 4.0]])?;
/// if let Some(estimate) = matrix.det_direct_with_errbound()? {
/// assert_eq!(estimate.determinant(), -2.0);
/// assert!(estimate.absolute_error_bound() >= 0.0);
/// }
/// # Ok(())
/// # }
/// ```
///
/// # Errors
/// Returns [`LaError::NonFinite`] when the determinant or bound computation
/// overflows to NaN or infinity. Underflow-sensitive finite computations
/// return `Ok(None)` because they remain valid inputs for an exact fallback.
#[inline]
pub const fn det_direct_with_errbound(
&self,
) -> Result<Option<DeterminantWithErrorBound>, LaError> {
if self.det_bound_inputs_have_wide_exponent_margin() {
let Some(det) = self.det_direct_arithmetic::<false>() else {
cold_path();
return Ok(None);
};
return self.det_direct_with_errbound_from_arithmetic(det);
}
let Some(det) = self.det_direct_arithmetic::<true>() else {
cold_path();
return Ok(None);
};
self.det_direct_with_errbound_from_arithmetic(det)
}
/// Conservative absolute error bound for `det_direct()`.
///
/// Returns `Ok(Some(bound))` such that `|det_direct() - det_exact| ≤ bound`
/// when every rounded intermediate used by the closed-form determinant and
/// bound is normal (or an exact structural zero). Returns `Ok(None)` when
/// gradual underflow could invalidate the relative-error analysis, or for
/// D ≥ 5 where no fast bound is available.
///
/// For D ≤ 4, the bound is derived from the absolute Leibniz sum using
/// Shewchuk-style error analysis (see `REFERENCES.md` \[8\] and the
/// per-constant docs on [`ERR_COEFF_2`], [`ERR_COEFF_3`], and
/// [`ERR_COEFF_4`]). For D = 0 or 1, returns
/// `Some(0.0)` since the determinant computation is exact (no
/// arithmetic).
///
/// This method does NOT require the `exact` feature — the bounds use
/// pure f64 arithmetic and are useful for custom adaptive-precision logic.
///
/// # When to use
///
/// Use [`det_direct_with_errbound`](Self::det_direct_with_errbound) when the
/// determinant and bound are both needed. This accessor is convenient when
/// only the bound is needed.
///
/// # Examples
/// ```
/// use la_stack::prelude::*;
///
/// # fn main() -> Result<(), LaError> {
/// let m = Matrix::<3>::try_from_rows([
/// [1.0, 2.0, 3.0],
/// [4.0, 5.0, 6.0],
/// [7.0, 8.0, 9.0],
/// ])?;
/// if let Some(bound) = m.det_errbound()? {
/// assert!(bound >= 0.0);
/// }
/// # Ok(())
/// # }
/// ```
///
/// # Adaptive precision pattern (requires `exact` feature)
/// ```ignore
/// use la_stack::prelude::*;
///
/// fn adaptive_det_sign<const D: usize>(
/// matrix: &Matrix<D>,
/// ) -> DeterminantSign {
/// if let Ok(Some(estimate)) = matrix.det_direct_with_errbound() {
/// if estimate.determinant().abs() > estimate.absolute_error_bound() {
/// return if estimate.determinant() > 0.0 {
/// DeterminantSign::Positive
/// } else {
/// DeterminantSign::Negative
/// };
/// }
/// }
///
/// matrix.det_sign_exact()
/// }
///
/// fn main() -> Result<(), LaError> {
/// assert_eq!(
/// adaptive_det_sign(&Matrix::<3>::identity()),
/// DeterminantSign::Positive
/// );
///
/// let big = f64::MAX / 2.0;
/// let overflowing = Matrix::<3>::try_from_rows([
/// [0.0, 0.0, 1.0],
/// [big, 0.0, 1.0],
/// [0.0, big, 1.0],
/// ])?;
/// assert_eq!(
/// adaptive_det_sign(&overflowing),
/// DeterminantSign::Positive
/// );
/// Ok(())
/// }
/// ```
///
/// # Errors
/// Returns [`LaError::NonFinite`] when the bound computation overflows to
/// NaN or infinity. Underflow-sensitive finite computations return
/// `Ok(None)` instead because they are valid inputs for an exact fallback.
#[inline]
pub const fn det_errbound(&self) -> Result<Option<f64>, LaError> {
match self.det_direct_with_errbound() {
Ok(Some(result)) => Ok(Some(result.absolute_error_bound)),
Ok(None) => Ok(None),
Err(error) => Err(error),
}
}
/// Return whether every non-zero entry is large enough that the complete
/// D≤4 determinant and permanent trees cannot gradually underflow.
///
/// The `2^-16` threshold leaves hundreds of binary exponent bits of margin
/// even after the D=4 tree's products, FMAs, and binary64 rounding steps.
/// Overflow remains possible and is classified after evaluation. Inputs
/// below this conservative threshold use per-operation tracking instead.
const fn det_bound_inputs_have_wide_exponent_margin(&self) -> bool {
const MIN_MAGNITUDE_BITS: u64 = 1007_u64 << 52; // 2^-16
const MAGNITUDE_MASK: u64 = !(1_u64 << 63);
if D > 4 {
return false;
}
let mut row = 0;
while row < D {
let mut col = 0;
while col < D {
let magnitude_bits = self.rows[row][col].to_bits() & MAGNITUDE_MASK;
if magnitude_bits != 0 && magnitude_bits < MIN_MAGNITUDE_BITS {
return false;
}
col += 1;
}
row += 1;
}
true
}
/// Classify a completed determinant tree and construct its matching bound.
const fn det_direct_with_errbound_from_arithmetic<const TRACK_UNDERFLOW: bool>(
&self,
det: FilterArithmetic<TRACK_UNDERFLOW>,
) -> Result<Option<DeterminantWithErrorBound>, LaError> {
let bound = match self.det_errbound_from_arithmetic(det) {
Ok(Some(bound)) => bound,
Ok(None) => return Ok(None),
Err(error) => return Err(error),
};
if !det.value.is_finite() {
cold_path();
return Err(LaError::non_finite_computation_scalar(
ArithmeticOperation::Determinant,
));
}
Ok(Some(DeterminantWithErrorBound {
determinant: det.value,
absolute_error_bound: bound,
}))
}
/// Compute a bound after the matching determinant tree has been evaluated.
const fn det_errbound_from_arithmetic<const TRACK_UNDERFLOW: bool>(
&self,
det: FilterArithmetic<TRACK_UNDERFLOW>,
) -> Result<Option<f64>, LaError> {
if !det.underflow_safe {
cold_path();
return Ok(None);
}
match D {
0 | 1 => Self::computed_scalar_result(ArithmeticOperation::DeterminantErrorBound, 0.0),
2 => {
let r = &self.rows;
let product_0 = FilterArithmetic::<TRACK_UNDERFLOW>::multiply(r[0][0], r[1][1]);
let product_1 = FilterArithmetic::<TRACK_UNDERFLOW>::multiply(r[0][1], r[1][0]);
let mut permanent = FilterArithmetic::<TRACK_UNDERFLOW>::add_non_negative(
product_0.value.abs(),
product_1.value.abs(),
);
permanent.underflow_safe &= product_0.underflow_safe && product_1.underflow_safe;
Self::certified_error_bound(ERR_COEFF_2, permanent)
}
3 => {
let r = &self.rows;
let permanent = Self::det3_abs_permanent_elements::<TRACK_UNDERFLOW>(
[r[0][0], r[0][1], r[0][2]],
[r[1][0], r[1][1], r[1][2]],
[r[2][0], r[2][1], r[2][2]],
);
Self::certified_error_bound(ERR_COEFF_3, permanent)
}
4 => self.det4_errbound::<TRACK_UNDERFLOW>(),
_ => {
cold_path();
Ok(None)
}
}
}
/// Compute the D=4 determinant error bound after the dimension dispatch.
const fn det4_errbound<const TRACK_UNDERFLOW: bool>(&self) -> Result<Option<f64>, LaError> {
if !TRACK_UNDERFLOW && let Some(input) = Det4SharedMinorInput::try_new(self) {
return Self::certified_error_bound(
ERR_COEFF_4,
FilterArithmetic::<TRACK_UNDERFLOW> {
value: Self::det4_dense_abs_permanent_elements(input),
underflow_safe: true,
},
);
}
let r = &self.rows;
let mut permanent = if r[0][3] == 0.0 {
FilterArithmetic {
value: 0.0,
underflow_safe: true,
}
} else {
let pc3 = Self::det3_abs_permanent_elements::<TRACK_UNDERFLOW>(
[r[1][0], r[1][1], r[1][2]],
[r[2][0], r[2][1], r[2][2]],
[r[3][0], r[3][1], r[3][2]],
);
let mut term = FilterArithmetic::<TRACK_UNDERFLOW>::multiply(r[0][3].abs(), pc3.value);
term.underflow_safe &= pc3.underflow_safe;
term
};
if r[0][2] != 0.0 {
let pc2 = Self::det3_abs_permanent_elements::<TRACK_UNDERFLOW>(
[r[1][0], r[1][1], r[1][3]],
[r[2][0], r[2][1], r[2][3]],
[r[3][0], r[3][1], r[3][3]],
);
let prior_safe = permanent.underflow_safe && pc2.underflow_safe;
permanent = FilterArithmetic::<TRACK_UNDERFLOW>::mul_add(
r[0][2].abs(),
pc2.value,
permanent.value,
);
permanent.underflow_safe &= prior_safe;
}
if r[0][1] != 0.0 {
let pc1 = Self::det3_abs_permanent_elements::<TRACK_UNDERFLOW>(
[r[1][0], r[1][2], r[1][3]],
[r[2][0], r[2][2], r[2][3]],
[r[3][0], r[3][2], r[3][3]],
);
let prior_safe = permanent.underflow_safe && pc1.underflow_safe;
permanent = FilterArithmetic::<TRACK_UNDERFLOW>::mul_add(
r[0][1].abs(),
pc1.value,
permanent.value,
);
permanent.underflow_safe &= prior_safe;
}
if r[0][0] != 0.0 {
let pc0 = Self::det3_abs_permanent_elements::<TRACK_UNDERFLOW>(
[r[1][1], r[1][2], r[1][3]],
[r[2][1], r[2][2], r[2][3]],
[r[3][1], r[3][2], r[3][3]],
);
let prior_safe = permanent.underflow_safe && pc0.underflow_safe;
permanent = FilterArithmetic::<TRACK_UNDERFLOW>::mul_add(
r[0][0].abs(),
pc0.value,
permanent.value,
);
permanent.underflow_safe &= prior_safe;
}
Self::certified_error_bound(ERR_COEFF_4, permanent)
}
/// Evaluate a 3×3 determinant expansion with a guarded sparse fallback.
///
/// When all three first-row coefficients are non-zero, one branch-free
/// closed form is used. The sparse fallback protects the public
/// [`det_direct`](Self::det_direct) contract: a mathematically absent term
/// must not compute an overflowing minor and poison the determinant with
/// `0.0 * inf == NaN`. Nonzero terms keep the same fused multiply-add
/// ordering as the closed-form expansion.
#[expect(
clippy::inline_always,
reason = "det_direct callers must eliminate unused filter-safety bookkeeping"
)]
#[inline(always)]
const fn det3_elements<const TRACK_UNDERFLOW: bool>(
r0: [f64; 3],
r1: [f64; 3],
r2: [f64; 3],
) -> FilterArithmetic<TRACK_UNDERFLOW> {
let dense = (r0[0] != 0.0) && (r0[1] != 0.0) && (r0[2] != 0.0);
if !TRACK_UNDERFLOW && dense {
let m00 = r1[1].mul_add(r2[2], -(r1[2] * r2[1]));
let m01 = r1[0].mul_add(r2[2], -(r1[2] * r2[0]));
let m02 = r1[0].mul_add(r2[1], -(r1[1] * r2[0]));
return FilterArithmetic {
value: r0[0].mul_add(m00, (-r0[1]).mul_add(m01, r0[2] * m02)),
underflow_safe: true,
};
}
let mut det = if r0[2] == 0.0 {
FilterArithmetic {
value: 0.0,
underflow_safe: true,
}
} else {
let subtrahend = FilterArithmetic::<TRACK_UNDERFLOW>::multiply(r1[1], r2[0]);
let mut m02 =
FilterArithmetic::<TRACK_UNDERFLOW>::mul_add(r1[0], r2[1], -subtrahend.value);
m02.underflow_safe &= subtrahend.underflow_safe;
let mut term = FilterArithmetic::<TRACK_UNDERFLOW>::multiply(r0[2], m02.value);
term.underflow_safe &= m02.underflow_safe;
term
};
if r0[1] != 0.0 {
let subtrahend = FilterArithmetic::<TRACK_UNDERFLOW>::multiply(r1[2], r2[0]);
let mut m01 =
FilterArithmetic::<TRACK_UNDERFLOW>::mul_add(r1[0], r2[2], -subtrahend.value);
m01.underflow_safe &= subtrahend.underflow_safe;
let prior_safe = det.underflow_safe && m01.underflow_safe;
det = FilterArithmetic::<TRACK_UNDERFLOW>::mul_add(-r0[1], m01.value, det.value);
det.underflow_safe &= prior_safe;
}
if r0[0] != 0.0 {
let subtrahend = FilterArithmetic::<TRACK_UNDERFLOW>::multiply(r1[2], r2[1]);
let mut m00 =
FilterArithmetic::<TRACK_UNDERFLOW>::mul_add(r1[1], r2[2], -subtrahend.value);
m00.underflow_safe &= subtrahend.underflow_safe;
let prior_safe = det.underflow_safe && m00.underflow_safe;
det = FilterArithmetic::<TRACK_UNDERFLOW>::mul_add(r0[0], m00.value, det.value);
det.underflow_safe &= prior_safe;
}
det
}
/// Evaluate a 3×3 absolute permanent while skipping zero coefficients.
///
/// This mirrors [`det3_elements`](Self::det3_elements) for error-bound
/// computation: absent determinant terms should not force evaluation of an
/// overflowing absolute minor.
#[expect(
clippy::inline_always,
reason = "error-bound call-site specialization avoids tracked-helper overhead"
)]
#[inline(always)]
const fn det3_abs_permanent_elements<const TRACK_UNDERFLOW: bool>(
r0: [f64; 3],
r1: [f64; 3],
r2: [f64; 3],
) -> FilterArithmetic<TRACK_UNDERFLOW> {
let dense = (r0[0] != 0.0) && (r0[1] != 0.0) && (r0[2] != 0.0);
if !TRACK_UNDERFLOW && dense {
let pm00 = (r1[1] * r2[2]).abs() + (r1[2] * r2[1]).abs();
let pm01 = (r1[0] * r2[2]).abs() + (r1[2] * r2[0]).abs();
let pm02 = (r1[0] * r2[1]).abs() + (r1[1] * r2[0]).abs();
return FilterArithmetic {
value: r0[2]
.abs()
.mul_add(pm02, r0[1].abs().mul_add(pm01, r0[0].abs() * pm00)),
underflow_safe: true,
};
}
let mut permanent = if r0[2] == 0.0 {
FilterArithmetic {
value: 0.0,
underflow_safe: true,
}
} else {
let product_0 = FilterArithmetic::<TRACK_UNDERFLOW>::multiply(r1[0], r2[1]);
let product_1 = FilterArithmetic::<TRACK_UNDERFLOW>::multiply(r1[1], r2[0]);
let mut pm02 = FilterArithmetic::<TRACK_UNDERFLOW>::add_non_negative(
product_0.value.abs(),
product_1.value.abs(),
);
pm02.underflow_safe &= product_0.underflow_safe && product_1.underflow_safe;
let mut term = FilterArithmetic::<TRACK_UNDERFLOW>::multiply(r0[2].abs(), pm02.value);
term.underflow_safe &= pm02.underflow_safe;
term
};
if r0[1] != 0.0 {
let product_0 = FilterArithmetic::<TRACK_UNDERFLOW>::multiply(r1[0], r2[2]);
let product_1 = FilterArithmetic::<TRACK_UNDERFLOW>::multiply(r1[2], r2[0]);
let mut pm01 = FilterArithmetic::<TRACK_UNDERFLOW>::add_non_negative(
product_0.value.abs(),
product_1.value.abs(),
);
pm01.underflow_safe &= product_0.underflow_safe && product_1.underflow_safe;
let prior_safe = permanent.underflow_safe && pm01.underflow_safe;
permanent = FilterArithmetic::<TRACK_UNDERFLOW>::mul_add(
r0[1].abs(),
pm01.value,
permanent.value,
);
permanent.underflow_safe &= prior_safe;
}
if r0[0] != 0.0 {
let product_0 = FilterArithmetic::<TRACK_UNDERFLOW>::multiply(r1[1], r2[2]);
let product_1 = FilterArithmetic::<TRACK_UNDERFLOW>::multiply(r1[2], r2[1]);
let mut pm00 = FilterArithmetic::<TRACK_UNDERFLOW>::add_non_negative(
product_0.value.abs(),
product_1.value.abs(),
);
pm00.underflow_safe &= product_0.underflow_safe && product_1.underflow_safe;
let prior_safe = permanent.underflow_safe && pm00.underflow_safe;
permanent = FilterArithmetic::<TRACK_UNDERFLOW>::mul_add(
r0[0].abs(),
pm00.value,
permanent.value,
);
permanent.underflow_safe &= prior_safe;
}
permanent
}
/// Finish a determinant error bound only when its full arithmetic tree is
/// outside the gradual-underflow regime.
const fn certified_error_bound<const TRACK_UNDERFLOW: bool>(
coefficient: f64,
permanent: FilterArithmetic<TRACK_UNDERFLOW>,
) -> Result<Option<f64>, LaError> {
let mut bound = FilterArithmetic::<TRACK_UNDERFLOW>::multiply(coefficient, permanent.value);
bound.underflow_safe &= permanent.underflow_safe;
if bound.underflow_safe {
Self::computed_scalar_result(ArithmeticOperation::DeterminantErrorBound, bound.value)
} else {
cold_path();
Ok(None)
}
}
/// Return a computed scalar result for a matrix with finite stored entries.
const fn computed_scalar_result(
operation: ArithmeticOperation,
value: f64,
) -> Result<Option<f64>, LaError> {
if value.is_finite() {
Ok(Some(value))
} else {
Err(LaError::non_finite_computation_scalar(operation))
}
}
}
impl<const D: usize> Default for Matrix<D> {
#[inline]
fn default() -> Self {
Self::zero()
}
}
#[cfg(all(doc, feature = "exact"))]
mod det_errbound_doctests {
/// ```rust
/// use la_stack::prelude::*;
///
/// fn adaptive_det_sign<const D: usize>(
/// matrix: &Matrix<D>,
/// ) -> DeterminantSign {
/// if let Ok(Some(estimate)) = matrix.det_direct_with_errbound() {
/// if estimate.determinant().abs() > estimate.absolute_error_bound() {
/// return if estimate.determinant() > 0.0 {
/// DeterminantSign::Positive
/// } else {
/// DeterminantSign::Negative
/// };
/// }
/// }
///
/// matrix.det_sign_exact()
/// }
///
/// # fn main() -> Result<(), LaError> {
/// let identity = Matrix::<3>::identity();
/// assert_eq!(
/// adaptive_det_sign(&identity),
/// DeterminantSign::Positive
/// );
///
/// let singular = Matrix::<3>::try_from_rows([
/// [1.0, 2.0, 3.0],
/// [4.0, 5.0, 6.0],
/// [7.0, 8.0, 9.0],
/// ])?;
/// assert_eq!(adaptive_det_sign(&singular), DeterminantSign::Zero);
///
/// let big = f64::MAX / 2.0;
/// let overflowing = Matrix::<3>::try_from_rows([
/// [0.0, 0.0, 1.0],
/// [big, 0.0, 1.0],
/// [0.0, big, 1.0],
/// ])?;
/// assert_eq!(
/// adaptive_det_sign(&overflowing),
/// DeterminantSign::Positive
/// );
/// # Ok(())
/// # }
/// ```
fn adaptive_precision_pattern() {}
}
#[cfg(test)]
mod tests {
use core::hint::black_box;
use approx::assert_abs_diff_eq;
use pastey::paste;
use super::*;
use crate::{DEFAULT_SINGULAR_TOL, FactorizationKind, Vector};
macro_rules! gen_matrix_tests {
($d:literal) => {
paste! {
#[test]
fn [<matrix_try_from_rows_get_set_bounds_checked_ $d d>]() {
let mut rows = [[0.0f64; $d]; $d];
rows[0][0] = 1.0;
rows[$d - 1][$d - 1] = -2.0;
let mut m = Matrix::<$d>::try_from_rows(rows).unwrap();
assert_eq!(m.get(0, 0), Some(1.0));
assert_eq!(m.get($d - 1, $d - 1), Some(-2.0));
assert_eq!(m.try_get(0, 0), Ok(1.0));
assert_eq!(m.try_get($d - 1, $d - 1), Ok(-2.0));
// Out-of-bounds is None.
assert_eq!(m.get($d, 0), None);
assert_eq!(
m.try_get($d, 0),
Err(LaError::IndexOutOfBounds {
row: $d,
col: 0,
dim: $d,
})
);
// Out-of-bounds set fails.
let before_failed_set = m;
assert_eq!(
m.set($d, 0, 3.0),
Err(LaError::IndexOutOfBounds {
row: $d,
col: 0,
dim: $d,
})
);
assert_eq!(m, before_failed_set);
assert_eq!(
m.set(0, $d, 3.0),
Err(LaError::IndexOutOfBounds {
row: 0,
col: $d,
dim: $d,
})
);
assert_eq!(m, before_failed_set);
assert_eq!(m.get(0, 0), Some(1.0));
// In-bounds set works.
assert_eq!(m.set(0, $d - 1, 3.0), Ok(()));
assert_eq!(m.get(0, $d - 1), Some(3.0));
assert_eq!(m.set($d - 1, 0, 4.0), Ok(()));
assert_eq!(m.try_get($d - 1, 0), Ok(4.0));
}
#[test]
fn [<matrix_set_rejects_non_finite_and_preserves_storage_ $d d>]() {
for value in [f64::NAN, f64::INFINITY, f64::NEG_INFINITY] {
let mut m = Matrix::<$d>::identity();
let before = m;
assert_eq!(
m.set($d - 1, 0, value),
Err(LaError::non_finite_input_matrix($d - 1, 0))
);
assert_eq!(m, before);
}
}
#[test]
fn [<matrix_try_from_rows_rejects_non_finite_ $d d>]() {
for value in [f64::NAN, f64::INFINITY, f64::NEG_INFINITY] {
let mut rows = [[0.0f64; $d]; $d];
rows[$d - 1][$d - 1] = value;
assert_eq!(
Matrix::<$d>::try_from_rows(rows),
Err(LaError::non_finite_input_matrix($d - 1, $d - 1))
);
}
let mut rows = [[0.0f64; $d]; $d];
rows[0][$d - 1] = f64::INFINITY;
rows[$d - 1][0] = f64::NAN;
assert_eq!(
Matrix::<$d>::try_from_rows(rows),
Err(LaError::non_finite_input_matrix(0, $d - 1))
);
}
#[test]
fn [<matrix_zero_and_default_are_zero_ $d d>]() {
let z = Matrix::<$d>::zero();
assert_abs_diff_eq!(z.inf_norm().unwrap(), 0.0, epsilon = 0.0);
let d = Matrix::<$d>::default();
assert_abs_diff_eq!(d.inf_norm().unwrap(), 0.0, epsilon = 0.0);
}
#[test]
fn [<matrix_inf_norm_max_row_sum_ $d d>]() {
let mut rows = [[0.0f64; $d]; $d];
// Row 0 has a smaller absolute row sum.
for c in 0..$d {
rows[0][c] = 0.5;
}
// The last row has absolute row sum = D.
for c in 0..$d {
rows[$d - 1][c] = -1.0;
}
let m = Matrix::<$d>::try_from_rows(rows).unwrap();
assert_abs_diff_eq!(m.inf_norm().unwrap(), f64::from($d), epsilon = 0.0);
}
#[test]
fn [<matrix_inf_norm_reports_first_overflowing_column_ $d d>]() {
let mut rows = [[0.0f64; $d]; $d];
rows[$d - 1][0] = f64::MAX;
rows[$d - 1][1] = f64::MAX;
let m = Matrix::<$d>::try_from_rows(rows).unwrap();
assert_eq!(
m.inf_norm(),
Err(LaError::non_finite_computation_matrix(
ArithmeticOperation::MatrixInfinityNorm,
$d - 1,
1,
))
);
}
#[test]
fn [<matrix_inf_norm_reports_first_overflowing_row_ $d d>]() {
let mut rows = [[0.0f64; $d]; $d];
rows[0][0] = f64::MAX;
rows[0][$d - 1] = f64::MAX;
rows[$d - 1][0] = f64::MAX;
rows[$d - 1][1] = f64::MAX;
let m = Matrix::<$d>::try_from_rows(rows).unwrap();
assert_eq!(
m.inf_norm(),
Err(LaError::non_finite_computation_matrix(
ArithmeticOperation::MatrixInfinityNorm,
0,
$d - 1,
))
);
}
#[test]
fn [<matrix_identity_lu_det_solve_ $d d>]() {
let m = Matrix::<$d>::identity();
// Identity has ones on diag and zeros off diag.
for r in 0..$d {
for c in 0..$d {
let expected = if r == c { 1.0 } else { 0.0 };
assert_abs_diff_eq!(m.get(r, c).unwrap(), expected, epsilon = 0.0);
}
}
// Determinant is 1.
let det = m.det().unwrap();
assert_abs_diff_eq!(det, 1.0, epsilon = 1e-12);
// LU solve on identity returns the RHS.
let lu = m.lu(DEFAULT_SINGULAR_TOL).unwrap();
let b_arr = {
let mut arr = [0.0f64; $d];
let values = [1.0f64, 2.0, 3.0, 4.0, 5.0];
for (dst, src) in arr.iter_mut().zip(values.iter()) {
*dst = *src;
}
arr
};
let b = Vector::<$d>::new(b_arr);
let x = lu.solve(b).unwrap().into_array();
for (x_i, b_i) in x.iter().zip(b_arr.iter()) {
assert_abs_diff_eq!(*x_i, *b_i, epsilon = 1e-12);
}
}
}
};
}
// Mirror delaunay-style multi-dimension tests.
gen_matrix_tests!(2);
gen_matrix_tests!(3);
gen_matrix_tests!(4);
gen_matrix_tests!(5);
#[test]
fn matrix_inf_norm_preserves_left_to_right_row_sum_order() {
let large = 9_007_199_254_740_992.0;
let matrix =
Matrix::<4>::try_from_rows([[large, 1.0, 1.0, 1.0], [0.0; 4], [0.0; 4], [0.0; 4]])
.unwrap();
assert_eq!(matrix.inf_norm(), Ok(large));
}
// === det_direct tests ===
#[test]
fn det_direct_d0_is_one() {
assert_eq!(Matrix::<0>::zero().det_direct(), Ok(Some(1.0)));
}
#[test]
fn det_direct_d1_returns_element() {
let m = Matrix::<1>::try_from_rows([[42.0]]).unwrap();
assert_eq!(m.det_direct(), Ok(Some(42.0)));
}
#[test]
fn det_direct_d2_known_value() {
// [[1,2],[3,4]] → det = 1*4 - 2*3 = -2
// black_box prevents compile-time constant folding of the const fn.
let m = black_box(Matrix::<2>::try_from_rows([[1.0, 2.0], [3.0, 4.0]]).unwrap());
assert_abs_diff_eq!(m.det_direct().unwrap().unwrap(), -2.0, epsilon = 1e-15);
}
#[test]
fn det_direct_d3_known_value() {
// Classic 3×3: det = 0
let m = black_box(
Matrix::<3>::try_from_rows([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0]])
.unwrap(),
);
assert_abs_diff_eq!(m.det_direct().unwrap().unwrap(), 0.0, epsilon = 1e-12);
}
#[test]
fn det_direct_d3_dense_known_value() {
// det = 1*(5*8 - 7*6) - 2*(4*8 - 7*2) + 3*(4*6 - 5*2) = 4
let m = black_box(
Matrix::<3>::try_from_rows([[1.0, 2.0, 3.0], [4.0, 5.0, 7.0], [2.0, 6.0, 8.0]])
.unwrap(),
);
let direct = m.det_direct().unwrap().unwrap();
let paired = m.det_direct_with_errbound().unwrap().unwrap();
assert_abs_diff_eq!(direct, 4.0, epsilon = 1e-12);
assert_eq!(paired.determinant().to_bits(), direct.to_bits());
}
#[test]
fn det_direct_d3_dense_reports_legitimate_overflow() {
// The unscaled matrix has determinant 54, so scaling every entry by
// 1.6e102 gives a determinant of approximately 2.21e308.
let scale = 1.6e102;
let m = black_box(
Matrix::<3>::try_from_rows([
[4.0 * scale, scale, scale],
[scale, 4.0 * scale, scale],
[scale, scale, 4.0 * scale],
])
.unwrap(),
);
let expected = LaError::non_finite_computation_scalar(ArithmeticOperation::Determinant);
assert_eq!(m.det_direct(), Err(expected));
assert_eq!(m.det(), Err(expected));
}
#[test]
fn det_errbound_d3_dense_reports_legitimate_overflow() {
// The unscaled matrix has determinant 54, so scaling every entry by
// 1.6e102 gives a determinant of approximately 2.21e308.
let scale = 1.6e102;
let m = black_box(
Matrix::<3>::try_from_rows([
[4.0 * scale, scale, scale],
[scale, 4.0 * scale, scale],
[scale, scale, 4.0 * scale],
])
.unwrap(),
);
let expected =
LaError::non_finite_computation_scalar(ArithmeticOperation::DeterminantErrorBound);
assert_eq!(m.det_errbound(), Err(expected));
assert_eq!(m.det_direct_with_errbound(), Err(expected));
}
#[test]
fn det_direct_d3_nonsingular() {
// [[2,1,0],[0,3,1],[1,0,2]] → det = 2*(6-0) - 1*(0-1) + 0 = 13
let m = black_box(
Matrix::<3>::try_from_rows([[2.0, 1.0, 0.0], [0.0, 3.0, 1.0], [1.0, 0.0, 2.0]])
.unwrap(),
);
assert_abs_diff_eq!(m.det_direct().unwrap().unwrap(), 13.0, epsilon = 1e-12);
}
#[test]
fn det_direct_d3_skips_zero_coefficient_minor_that_would_overflow() {
let m = black_box(
Matrix::<3>::try_from_rows([
[1.0, 0.0, 0.0],
[1.0e300, 1.0, 1.0e300],
[1.0e300, 0.0, 1.0e300],
])
.unwrap(),
);
assert_eq!(m.det_direct(), Ok(Some(1.0e300)));
}
#[test]
fn det_direct_d4_known_value() {
// Diagonal matrix: det = product of diagonal entries.
let mut rows = [[0.0f64; 4]; 4];
rows[0][0] = 2.0;
rows[1][1] = 3.0;
rows[2][2] = 5.0;
rows[3][3] = 7.0;
let m = black_box(Matrix::<4>::try_from_rows(rows).unwrap());
assert_abs_diff_eq!(m.det_direct().unwrap().unwrap(), 210.0, epsilon = 1e-12);
}
#[test]
fn det_direct_d4_dense_known_value() {
let m = black_box(
Matrix::<4>::try_from_rows([
[4.0, 1.0, 3.0, 2.0],
[1.0, 5.0, 2.0, 1.0],
[7.0, 2.0, 6.0, 3.0],
[1.0, 8.0, 4.0, 9.0],
])
.unwrap(),
);
let direct = m.det_direct().unwrap().unwrap();
let paired = m.det_direct_with_errbound().unwrap().unwrap();
assert_abs_diff_eq!(direct, 112.0, epsilon = 1e-12);
assert_eq!(paired.determinant().to_bits(), direct.to_bits());
}
#[test]
fn det_direct_d4_dense_reports_legitimate_overflow() {
// The unscaled matrix has determinant 189, so scaling every entry by
// 3.2e76 gives a determinant of approximately 1.98e308.
let scale = 3.2e76;
let m = black_box(
Matrix::<4>::try_from_rows([
[4.0 * scale, scale, scale, scale],
[scale, 4.0 * scale, scale, scale],
[scale, scale, 4.0 * scale, scale],
[scale, scale, scale, 4.0 * scale],
])
.unwrap(),
);
let expected = LaError::non_finite_computation_scalar(ArithmeticOperation::Determinant);
assert_eq!(m.det_direct(), Err(expected));
assert_eq!(m.det(), Err(expected));
}
#[test]
fn det_errbound_d4_dense_reports_legitimate_overflow() {
// The unscaled matrix has determinant 189, so scaling every entry by
// 3.2e76 gives a determinant of approximately 1.98e308.
let scale = 3.2e76;
let m = black_box(
Matrix::<4>::try_from_rows([
[4.0 * scale, scale, scale, scale],
[scale, 4.0 * scale, scale, scale],
[scale, scale, 4.0 * scale, scale],
[scale, scale, scale, 4.0 * scale],
])
.unwrap(),
);
let expected =
LaError::non_finite_computation_scalar(ArithmeticOperation::DeterminantErrorBound);
assert_eq!(m.det_errbound(), Err(expected));
assert_eq!(m.det_direct_with_errbound(), Err(expected));
}
#[test]
fn det_direct_d4_skips_zero_coefficient_cofactors_that_would_overflow() {
let m = black_box(
Matrix::<4>::try_from_rows([
[0.0, 0.0, 0.0, 0.0],
[0.0, 0.0, 0.0, 0.0],
[1.0e300, 0.0, 1.0e300, 1.0e300],
[1.0e300, 0.0, 1.0e300, -1.0e300],
])
.unwrap(),
);
assert_eq!(m.det_direct(), Ok(Some(0.0)));
}
#[test]
fn det_direct_d4_sparse_second_row_skips_inactive_overflowing_minors() {
let m = black_box(
Matrix::<4>::try_from_rows([
[1.0e-300, 1.0, 1.0, 1.0],
[0.0, 1.0, 0.0, 0.0],
[0.0, 1.0e300, 1.0, 1.0e300],
[0.0, 1.0e300, 0.0, 1.0e300],
])
.unwrap(),
);
assert_eq!(m.det_direct(), Ok(Some(1.0)));
assert_eq!(m.det(), Ok(1.0));
}
#[test]
fn det_direct_d5_returns_none() {
assert_eq!(Matrix::<5>::identity().det_direct(), Ok(None));
}
#[test]
fn det_direct_d8_returns_none() {
assert_eq!(Matrix::<8>::zero().det_direct(), Ok(None));
}
#[test]
fn det_direct_rejects_computed_overflow() {
let m = Matrix::<2>::try_from_rows([[1e300, 0.0], [0.0, 1e300]]).unwrap();
assert_eq!(
m.det_direct(),
Err(LaError::non_finite_computation_scalar(
ArithmeticOperation::Determinant
))
);
}
#[test]
fn det_d5_rejects_lu_product_overflow() {
let m = Matrix::<5>::try_from_rows([
[1.0e100, 0.0, 0.0, 0.0, 0.0],
[0.0, 1.0e100, 0.0, 0.0, 0.0],
[0.0, 0.0, 1.0e100, 0.0, 0.0],
[0.0, 0.0, 0.0, 1.0e100, 0.0],
[0.0, 0.0, 0.0, 0.0, 1.0e100],
])
.unwrap();
assert_eq!(
m.det(),
Err(LaError::non_finite_computation_step(
ArithmeticOperation::Determinant,
4
))
);
}
#[test]
fn det_d5_rejects_lu_trailing_update_overflow() {
let m = Matrix::<5>::try_from_rows([
[1.0, f64::MAX, 0.0, 0.0, 0.0],
[-1.0, f64::MAX, 0.0, 0.0, 0.0],
[0.0, 0.0, 1.0, 0.0, 0.0],
[0.0, 0.0, 0.0, 1.0, 0.0],
[0.0, 0.0, 0.0, 0.0, 1.0],
])
.unwrap();
assert_eq!(
m.det(),
Err(LaError::non_finite_computation_matrix(
ArithmeticOperation::LuFactorization,
1,
1
))
);
}
macro_rules! gen_det_direct_agrees_with_lu {
($d:literal) => {
paste! {
#[test]
#[expect(
clippy::cast_precision_loss,
reason = "r, c, and D are tiny test integers exactly representable as f64"
)]
fn [<det_direct_agrees_with_lu_ $d d>]() {
// Well-conditioned matrix: diagonally dominant.
let mut rows = [[0.0f64; $d]; $d];
for r in 0..$d {
for c in 0..$d {
rows[r][c] = if r == c {
(r as f64) + f64::from($d) + 1.0
} else {
0.1 / ((r + c + 1) as f64)
};
}
}
let m = Matrix::<$d>::try_from_rows(rows).unwrap();
let direct = m.det_direct().unwrap().unwrap();
let lu_det = m.lu(DEFAULT_SINGULAR_TOL).unwrap().det().unwrap();
let eps = lu_det.abs().mul_add(1e-12, 1e-12);
assert_abs_diff_eq!(direct, lu_det, epsilon = eps);
}
}
};
}
gen_det_direct_agrees_with_lu!(1);
gen_det_direct_agrees_with_lu!(2);
gen_det_direct_agrees_with_lu!(3);
gen_det_direct_agrees_with_lu!(4);
#[test]
fn det_direct_identity_all_dims() {
assert_abs_diff_eq!(
Matrix::<1>::identity().det_direct().unwrap().unwrap(),
1.0,
epsilon = 0.0
);
assert_abs_diff_eq!(
Matrix::<2>::identity().det_direct().unwrap().unwrap(),
1.0,
epsilon = 0.0
);
assert_abs_diff_eq!(
Matrix::<3>::identity().det_direct().unwrap().unwrap(),
1.0,
epsilon = 0.0
);
assert_abs_diff_eq!(
Matrix::<4>::identity().det_direct().unwrap().unwrap(),
1.0,
epsilon = 0.0
);
}
#[test]
fn det_direct_zero_matrix() {
assert_abs_diff_eq!(
Matrix::<2>::zero().det_direct().unwrap().unwrap(),
0.0,
epsilon = 0.0
);
assert_abs_diff_eq!(
Matrix::<3>::zero().det_direct().unwrap().unwrap(),
0.0,
epsilon = 0.0
);
assert_abs_diff_eq!(
Matrix::<4>::zero().det_direct().unwrap().unwrap(),
0.0,
epsilon = 0.0
);
}
macro_rules! gen_det_singular_zero_matrix_tests {
($d:literal) => {
paste! {
#[test]
fn [<det_singular_zero_matrix_returns_zero_ $d d>]() {
assert_abs_diff_eq!(
Matrix::<$d>::zero().det().unwrap(),
0.0,
epsilon = 0.0
);
}
}
};
}
gen_det_singular_zero_matrix_tests!(2);
gen_det_singular_zero_matrix_tests!(3);
gen_det_singular_zero_matrix_tests!(4);
#[test]
fn det_singular_zero_matrix_d5_preserves_lu_error() {
assert_eq!(
Matrix::<5>::zero().det(),
Err(LaError::singular_numerical(
0,
FactorizationKind::Lu,
0.0,
0.0
))
);
}
#[test]
fn det_d5_does_not_turn_elimination_underflow_into_exact_zero() {
let min_subnormal = f64::from_bits(1);
let two_pow_800 = f64::from_bits(1823_u64 << 52);
let m = Matrix::<5>::try_from_rows([
[2.0, min_subnormal, 0.0, 0.0, 0.0],
[1.0, 0.0, 0.0, 0.0, 0.0],
[0.0, 0.0, two_pow_800, 0.0, 0.0],
[0.0, 0.0, 0.0, 1.0, 0.0],
[0.0, 0.0, 0.0, 0.0, 1.0],
])
.unwrap();
assert_eq!(
m.det(),
Err(LaError::singular_numerical(
1,
FactorizationKind::Lu,
0.0,
0.0
))
);
}
#[test]
fn det_d5_ignores_pivot_tolerance_for_tiny_nonsingular_matrix() {
// A small nonzero determinant is still a determinant. `det` must not
// flatten the value to zero merely because the default LU tolerance
// would reject a pivot this small.
let m = Matrix::<5>::try_from_rows([
[1e-13, 0.0, 0.0, 0.0, 0.0],
[0.0, 1.0, 0.0, 0.0, 0.0],
[0.0, 0.0, 1.0, 0.0, 0.0],
[0.0, 0.0, 0.0, 1.0, 0.0],
[0.0, 0.0, 0.0, 0.0, 1.0],
])
.unwrap();
assert_abs_diff_eq!(m.det().unwrap(), 1e-13, epsilon = 0.0);
assert_eq!(
m.lu(DEFAULT_SINGULAR_TOL),
Err(LaError::singular_numerical(
0,
FactorizationKind::Lu,
1e-13,
DEFAULT_SINGULAR_TOL.get()
))
);
}
#[test]
fn det_returns_non_finite_error_for_overflow_with_finite_entries() {
// det_direct produces an overflowing f64 (1e300 * 1e300 = ∞) even
// though every matrix entry is finite. The entry scan in `det`
// falls through and reports a computed determinant overflow rather
// than a NaN/∞ input.
let m = Matrix::<2>::try_from_rows([[1e300, 0.0], [0.0, 1e300]]).unwrap();
assert_eq!(
m.det(),
Err(LaError::non_finite_computation_scalar(
ArithmeticOperation::Determinant
))
);
}
// === det_direct const-evaluability tests (D = 2..=5) ===
//
// Every dimension hits a distinct arm of the `match D { … }` body inside
// `det_direct`, so exercising each at compile time is the tightest
// const-fn proof available.
macro_rules! gen_det_direct_const_eval_tests {
($d:literal) => {
paste! {
/// `Matrix::<D>::det_direct()` on the identity must const-evaluate
/// to `Ok(Some(1.0))` for every closed-form dimension `D ∈ {1, 2, 3, 4}`.
#[test]
fn [<det_direct_const_eval_ $d d>]() {
const DET: Result<Option<f64>, LaError> = Matrix::<$d>::identity().det_direct();
assert_eq!(DET, Ok(Some(1.0)));
}
}
};
}
gen_det_direct_const_eval_tests!(2);
gen_det_direct_const_eval_tests!(3);
gen_det_direct_const_eval_tests!(4);
#[test]
fn det_direct_const_eval_d5_is_none() {
// D ≥ 5 has no closed-form arm; `det_direct` returns `Ok(None)`. Verify
// that the wildcard arm is reachable in a `const { … }` context.
const DET: Result<Option<f64>, LaError> = Matrix::<5>::identity().det_direct();
assert_eq!(DET, Ok(None));
}
// === det_errbound tests (no `exact` feature required) ===
#[test]
fn det_errbound_matches_documented_coefficient_scale() {
let m2 = Matrix::<2>::try_from_rows([[1.0, 2.0], [3.0, 4.0]]).unwrap();
let expected_2 = ERR_COEFF_2 * ((1.0_f64 * 4.0).abs() + (2.0_f64 * 3.0).abs());
assert_abs_diff_eq!(
m2.det_errbound().unwrap().unwrap(),
expected_2,
epsilon = 0.0
);
assert_abs_diff_eq!(
Matrix::<3>::identity().det_errbound().unwrap().unwrap(),
ERR_COEFF_3,
epsilon = 0.0
);
assert_abs_diff_eq!(
Matrix::<4>::identity().det_errbound().unwrap().unwrap(),
ERR_COEFF_4,
epsilon = 0.0
);
}
#[test]
fn det_errbound_d3_skips_zero_coefficient_minor_that_would_overflow() {
let m = Matrix::<3>::try_from_rows([
[1.0, 0.0, 0.0],
[1.0e300, 1.0, 1.0e300],
[1.0e300, 0.0, 1.0e300],
])
.unwrap();
assert_eq!(m.det_errbound(), Ok(Some(ERR_COEFF_3 * 1.0e300)));
}
#[test]
fn det_errbound_d4_skips_zero_coefficient_cofactors_that_would_overflow() {
let m = Matrix::<4>::try_from_rows([
[0.0, 0.0, 0.0, 0.0],
[0.0, 0.0, 0.0, 0.0],
[1.0e300, 0.0, 1.0e300, 1.0e300],
[1.0e300, 0.0, 1.0e300, -1.0e300],
])
.unwrap();
assert_eq!(m.det_errbound(), Ok(Some(0.0)));
}
#[test]
fn det_errbound_d5_returns_none() {
// D=5 has no fast filter
assert_eq!(Matrix::<5>::identity().det_errbound(), Ok(None));
}
#[test]
fn combined_det_bound_wide_exponent_fast_path_matches_tracked_arithmetic() {
let threshold = f64::from_bits(1007_u64 << 52); // 2^-16
let at_threshold = Matrix::<2>::try_from_rows([[threshold, 0.0], [0.0, 2.0]]).unwrap();
assert!(at_threshold.det_bound_inputs_have_wide_exponent_margin());
let tracked = at_threshold
.det_direct_with_errbound_from_arithmetic(
at_threshold
.det_direct_arithmetic::<true>()
.expect("D=2 has direct arithmetic"),
)
.unwrap();
assert_eq!(at_threshold.det_direct_with_errbound().unwrap(), tracked);
let just_below = f64::from_bits(threshold.to_bits() - 1);
let below_threshold = Matrix::<2>::try_from_rows([[just_below, 0.0], [0.0, 2.0]]).unwrap();
assert!(!below_threshold.det_bound_inputs_have_wide_exponent_margin());
assert!(!Matrix::<5>::identity().det_bound_inputs_have_wide_exponent_margin());
}
#[test]
fn det_direct_with_errbound_covers_zero_and_one_dimensions() {
let empty = Matrix::<0>::zero()
.det_direct_with_errbound()
.unwrap()
.unwrap();
assert_abs_diff_eq!(empty.determinant(), 1.0, epsilon = 0.0);
assert_abs_diff_eq!(empty.absolute_error_bound(), 0.0, epsilon = 0.0);
let scalar = Matrix::<1>::try_from_rows([[-7.0]])
.unwrap()
.det_direct_with_errbound()
.unwrap()
.unwrap();
assert_abs_diff_eq!(scalar.determinant(), -7.0, epsilon = 0.0);
assert_abs_diff_eq!(scalar.absolute_error_bound(), 0.0, epsilon = 0.0);
}
#[test]
fn det_direct_with_errbound_pairs_the_closed_form_values() {
let matrix = Matrix::<2>::try_from_rows([[1.0, 2.0], [3.0, 4.0]]).unwrap();
let estimate = matrix.det_direct_with_errbound().unwrap().unwrap();
assert_abs_diff_eq!(
estimate.determinant(),
matrix.det_direct().unwrap().unwrap(),
epsilon = 0.0
);
assert_abs_diff_eq!(
estimate.absolute_error_bound(),
ERR_COEFF_2 * (4.0_f64 + 6.0_f64),
epsilon = 0.0
);
}
#[test]
fn det_direct_with_errbound_d5_returns_none() {
assert_eq!(Matrix::<5>::identity().det_direct_with_errbound(), Ok(None));
}
#[test]
fn det_errbound_rejects_computed_overflow() {
let m = Matrix::<2>::try_from_rows([[1e300, 0.0], [0.0, 1e300]]).unwrap();
assert_eq!(
m.det_errbound(),
Err(LaError::non_finite_computation_scalar(
ArithmeticOperation::DeterminantErrorBound
))
);
}
// === det_errbound const-evaluability tests (D = 2..=5) ===
macro_rules! gen_det_errbound_const_eval_tests {
($d:literal) => {
paste! {
/// `Matrix::<D>::det_errbound()` on the identity must const-evaluate
/// to `Ok(Some(bound))` with `bound > 0` for every closed-form dimension
/// `D ∈ {2, 3, 4}`. Each dimension hits a distinct arm of
/// `det_errbound` with a dimension-specific permanent computation.
#[test]
fn [<det_errbound_const_eval_ $d d>]() {
const BOUND: Result<Option<f64>, LaError> = Matrix::<$d>::identity().det_errbound();
assert!(BOUND.unwrap().unwrap() > 0.0);
}
}
};
}
gen_det_errbound_const_eval_tests!(2);
gen_det_errbound_const_eval_tests!(3);
gen_det_errbound_const_eval_tests!(4);
#[test]
fn det_errbound_const_eval_d5_is_none() {
// D ≥ 5 has no fast-filter bound; `det_errbound` returns `Ok(None)`.
const BOUND: Result<Option<f64>, LaError> = Matrix::<5>::identity().det_errbound();
assert_eq!(BOUND, Ok(None));
}
// === inf_norm const-evaluability tests (D = 2..=5) ===
macro_rules! gen_inf_norm_const_eval_tests {
($d:literal) => {
paste! {
/// `Matrix::<D>::inf_norm()` on the identity must const-evaluate
/// to `1.0` for every `D ≥ 1` — each row has a single `1.0`
/// entry, so the max absolute row sum is exactly `1.0`.
#[test]
fn [<inf_norm_const_eval_ $d d>]() {
const NORM: Result<f64, LaError> = Matrix::<$d>::identity().inf_norm();
assert!((NORM.unwrap() - 1.0).abs() <= 1e-12);
}
}
};
}
gen_inf_norm_const_eval_tests!(2);
gen_inf_norm_const_eval_tests!(3);
gen_inf_norm_const_eval_tests!(4);
gen_inf_norm_const_eval_tests!(5);
// === is_symmetric / first_asymmetry (public LDLT preconditions helpers) ===
macro_rules! gen_is_symmetric_tests {
($d:literal) => {
paste! {
#[test]
fn [<is_symmetric_true_for_identity_ $d d>]() {
let m = Matrix::<$d>::identity();
assert!(m.is_symmetric(Tolerance::try_new(1e-12).unwrap()).unwrap());
assert_eq!(m.first_asymmetry(Tolerance::try_new(1e-12).unwrap()).unwrap(), None);
}
#[test]
fn [<is_symmetric_true_for_zero_ $d d>]() {
let m = Matrix::<$d>::zero();
assert!(m.is_symmetric(Tolerance::try_new(1e-12).unwrap()).unwrap());
assert_eq!(m.first_asymmetry(Tolerance::try_new(1e-12).unwrap()).unwrap(), None);
}
#[test]
fn [<is_symmetric_true_for_constructed_symmetric_ $d d>]() {
// Construct A = M + Máµ€ so A is provably symmetric.
let mut m = [[0.0f64; $d]; $d];
for r in 0..$d {
for c in 0..$d {
#[expect(
clippy::cast_precision_loss,
reason = "matrix test indices are at most five and exactly representable as f64"
)]
{
m[r][c] = (r * $d + c) as f64;
}
}
}
let mut sym = [[0.0f64; $d]; $d];
for r in 0..$d {
for c in 0..$d {
sym[r][c] = m[r][c] + m[c][r];
}
}
let a = Matrix::<$d>::try_from_rows(sym).unwrap();
assert!(a.is_symmetric(Tolerance::try_new(1e-12).unwrap()).unwrap());
assert_eq!(a.first_asymmetry(Tolerance::try_new(1e-12).unwrap()).unwrap(), None);
}
#[test]
fn [<is_symmetric_false_for_asymmetric_offdiagonal_ $d d>]() {
// Perturb a single off-diagonal entry so symmetry fails.
let mut rows = [[0.0f64; $d]; $d];
for i in 0..$d {
rows[i][i] = 1.0;
}
rows[0][$d - 1] = 1.0;
rows[$d - 1][0] = -1.0; // breaks symmetry
let a = Matrix::<$d>::try_from_rows(rows).unwrap();
assert!(!a.is_symmetric(Tolerance::try_new(1e-12).unwrap()).unwrap());
assert_eq!(
a.first_asymmetry(Tolerance::try_new(1e-12).unwrap()).unwrap(),
Some((0, $d - 1))
);
}
}
};
}
gen_is_symmetric_tests!(2);
gen_is_symmetric_tests!(3);
gen_is_symmetric_tests!(4);
gen_is_symmetric_tests!(5);
macro_rules! gen_ldlt_symmetry_proof_tests {
($d:literal) => {
paste! {
#[test]
fn [<matrix_ldlt_accepts_exact_symmetric_spd_ $d d>]() {
// This exactly mirrored, strictly diagonally dominant
// tridiagonal matrix is positive definite.
let mut rows = [[0.0_f64; $d]; $d];
for (index, row) in rows.iter_mut().enumerate() {
row[index] = 2.0;
}
for index in 1..$d {
rows[index - 1][index] = 0.5;
rows[index][index - 1] = 0.5;
}
let matrix = Matrix::<$d>::try_from_rows(rows).unwrap();
let symmetric = SymmetricMatrix::try_new(matrix).unwrap();
assert_eq!(symmetric.into_matrix(), matrix);
let ldlt = matrix.ldlt(DEFAULT_SINGULAR_TOL).unwrap();
assert!(ldlt.det().unwrap() > 0.0);
}
#[test]
fn [<symmetric_matrix_try_new_rejects_finite_asymmetric_ $d d>]() {
let mut rows = [[0.0f64; $d]; $d];
for (i, row) in rows.iter_mut().enumerate() {
row[i] = 1.0;
}
rows[0][$d - 1] = 1.0;
rows[$d - 1][0] = -1.0;
assert_eq!(
Matrix::<$d>::try_from_rows(rows).and_then(SymmetricMatrix::try_new),
Err(LaError::asymmetric(0, $d - 1, $d, 1.0, -1.0, 0.0))
);
}
}
};
}
gen_ldlt_symmetry_proof_tests!(2);
gen_ldlt_symmetry_proof_tests!(3);
gen_ldlt_symmetry_proof_tests!(4);
gen_ldlt_symmetry_proof_tests!(5);
#[test]
fn symmetric_matrix_into_matrix_roundtrips_storage_internally() {
let a = Matrix::<2>::try_from_rows([[2.0, 1.0], [1.0, 3.0]]).unwrap();
let symmetric = SymmetricMatrix::try_new(a).unwrap();
assert_eq!(symmetric.into_matrix(), a);
}
#[test]
fn matrix_ldlt_accepts_opposite_signed_zero_mirrors() {
let matrix = Matrix::<2>::try_from_rows([[2.0, 0.0], [-0.0, 2.0]]).unwrap();
let ldlt = matrix.ldlt(DEFAULT_SINGULAR_TOL).unwrap();
assert_eq!(ldlt.det(), Ok(4.0));
}
#[test]
fn is_symmetric_tolerance_scales_with_inf_norm() {
// Off-diagonal entries differ by 1e-6. With inf_norm ≈ 2e6, the
// relative tolerance 1e-12 yields eps ≈ 2e-6, which accepts the gap;
// a stricter tol of 1e-15 rejects it.
let a = Matrix::<2>::try_from_rows([[1.0e6, 1.0e6 + 1.0e-6], [1.0e6, 1.0e6]]).unwrap();
assert!(a.is_symmetric(Tolerance::try_new(1e-12).unwrap()).unwrap());
assert!(!a.is_symmetric(Tolerance::try_new(1e-15).unwrap()).unwrap());
}
#[test]
fn symmetry_epsilon_multiplies_after_row_sum_near_subnormal_boundary() {
let min_subnormal = f64::from_bits(1);
let mut rows = [[0.0; 5]; 5];
let mut col = 0;
while col < 4 {
rows[0][col] = 0.4;
rows[col][0] = 0.4;
col += 1;
}
rows[0][4] = 2.0 * min_subnormal;
rows[4][0] = 0.0;
let matrix = Matrix::<5>::try_from_rows(rows).unwrap();
let tolerance = Tolerance::try_new(min_subnormal).unwrap();
let expected_epsilon = tolerance.get() * matrix.inf_norm().unwrap().max(1.0);
assert_eq!(expected_epsilon.to_bits(), 2);
assert_eq!(matrix.first_asymmetry(tolerance), Ok(None));
assert_eq!(matrix.is_symmetric(tolerance), Ok(true));
}
#[test]
fn symmetry_epsilon_scales_terms_when_row_sum_overflows() {
let matrix =
Matrix::<2>::try_from_rows([[f64::MAX, f64::MAX], [f64::MAX / 2.0, f64::MAX]]).unwrap();
assert_eq!(
matrix.inf_norm(),
Err(LaError::non_finite_computation_matrix(
ArithmeticOperation::MatrixInfinityNorm,
0,
1
))
);
assert_eq!(
matrix.first_asymmetry(Tolerance::try_new(0.25).unwrap()),
Ok(None)
);
assert_eq!(
matrix.first_asymmetry(Tolerance::try_new(0.125).unwrap()),
Ok(Some((0, 1)))
);
}
#[test]
fn first_asymmetry_returns_lexicographically_first_pair() {
// Two asymmetric pairs: (0, 2) and (1, 2). We must get (0, 2) first.
let a = Matrix::<3>::try_from_rows([[1.0, 0.0, 2.0], [0.0, 1.0, 3.0], [-2.0, -3.0, 1.0]])
.unwrap();
assert_eq!(
a.first_asymmetry(Tolerance::try_new(1e-12).unwrap())
.unwrap(),
Some((0, 2))
);
}
#[test]
fn first_asymmetry_strict_tol_survives_row_sum_overflow() {
let a = Matrix::<3>::try_from_rows([
[1.0, 1.0, 0.0],
[2.0, f64::MAX, f64::MAX],
[0.0, 0.0, 1.0],
])
.unwrap();
assert_eq!(
a.inf_norm(),
Err(LaError::non_finite_computation_matrix(
ArithmeticOperation::MatrixInfinityNorm,
1,
2
))
);
assert_eq!(
a.first_asymmetry(Tolerance::try_new(0.0).unwrap()).unwrap(),
Some((0, 1))
);
assert!(!a.is_symmetric(Tolerance::try_new(0.0).unwrap()).unwrap());
}
#[test]
fn first_asymmetry_rejects_scaled_epsilon_overflow() {
let a = Matrix::<2>::try_from_rows([[0.0, 0.0], [2.0, 1.0]]).unwrap();
let tol = Tolerance::try_new(f64::MAX).unwrap();
assert_eq!(
a.first_asymmetry(tol),
Err(LaError::non_finite_computation_matrix(
ArithmeticOperation::SymmetryCheck,
1,
0
))
);
assert_eq!(
a.is_symmetric(tol),
Err(LaError::non_finite_computation_matrix(
ArithmeticOperation::SymmetryCheck,
1,
0
))
);
}
#[test]
fn first_asymmetry_flags_overflowed_finite_difference() {
let a = Matrix::<2>::try_from_rows([[1.0, f64::MAX], [-f64::MAX, 1.0]]).unwrap();
assert_eq!(
a.first_asymmetry(Tolerance::try_new(1e-12).unwrap())
.unwrap(),
Some((0, 1))
);
assert!(!a.is_symmetric(Tolerance::try_new(1e-12).unwrap()).unwrap());
}
}