la-stack 0.4.6

Fast, stack-allocated linear algebra for fixed dimensions
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
#![forbid(unsafe_code)]

//! Fixed-size, stack-allocated vectors.

use core::hint::cold_path;

use crate::norm::norm_near_overflow;
use crate::rounding::{compare_product_with_rounded, two_sum_error};
use crate::{ArithmeticOperation, LaError};

/// A scalar estimate paired with a certified absolute error bound.
///
/// Values of this type are produced by [`Vector::dot_with_errbound`] and
/// [`Vector::dot_difference_with_errbound`]. The exact-real value of the
/// corresponding expression over the stored binary64 inputs lies between
/// [`lower_bound`](Self::lower_bound) and [`upper_bound`](Self::upper_bound),
/// and differs from [`estimate`](Self::estimate) by at most
/// [`absolute_error_bound`](Self::absolute_error_bound).
///
/// The bound certifies floating-point roundoff in one specified arithmetic
/// tree. It is not a caller-selected numerical tolerance and does not classify
/// an interval containing zero as equality.
///
/// Callers cannot construct this type directly. Every value has a finite
/// estimate, a finite non-negative absolute error bound, and finite ordered
/// endpoints.
///
/// # Examples
/// ```
/// use core::assert_matches;
/// use la_stack::prelude::*;
///
/// # fn main() -> Result<(), LaError> {
/// let left = Vector::<2>::try_new([1.0, 2.0])?;
/// let right = Vector::<2>::try_new([3.0, 4.0])?;
/// let certificate = left.dot_with_errbound(&right)?;
/// assert_eq!(certificate.map(ScalarWithErrorBound::estimate), Some(11.0));
/// // Preserve None: without a certificate, an exact fallback is needed
/// // before making a claim about the exact-real result.
/// let enclosure = certificate.map(|bounded| (bounded.lower_bound(), bounded.upper_bound()));
/// assert_matches!(enclosure, Some((lower, upper)) if lower <= 11.0 && 11.0 <= upper);
/// # Ok(())
/// # }
/// ```
#[must_use]
#[non_exhaustive]
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct ScalarWithErrorBound {
    estimate: f64,
    absolute_error_bound: f64,
    lower_bound: f64,
    upper_bound: f64,
}

impl ScalarWithErrorBound {
    /// Return the rounded scalar estimate.
    #[inline]
    #[must_use]
    pub const fn estimate(self) -> f64 {
        self.estimate
    }

    /// Return the certified absolute error bound.
    #[inline]
    #[must_use]
    pub const fn absolute_error_bound(self) -> f64 {
        self.absolute_error_bound
    }

    /// Return a finite outward-rounded lower bound on the exact-real value.
    #[inline]
    #[must_use]
    pub const fn lower_bound(self) -> f64 {
        self.lower_bound
    }

    /// Return a finite outward-rounded upper bound on the exact-real value.
    #[inline]
    #[must_use]
    pub const fn upper_bound(self) -> f64 {
        self.upper_bound
    }

    /// Construct a validated public result with finite outward endpoints.
    ///
    /// Exact addition residuals determine whether either rounded endpoint must
    /// move by one binary64 value. Returning `None` instead of publishing an
    /// infinite endpoint enforces the public proof-unavailable contract.
    const fn try_new(estimate: f64, absolute_error_bound: f64) -> Option<Self> {
        if !estimate.is_finite() || !absolute_error_bound.is_finite() || absolute_error_bound < 0.0
        {
            return None;
        }

        if absolute_error_bound == 0.0 {
            return Some(Self {
                estimate,
                absolute_error_bound: 0.0,
                lower_bound: estimate,
                upper_bound: estimate,
            });
        }

        let lower_rounded = estimate - absolute_error_bound;
        let upper_rounded = estimate + absolute_error_bound;
        if !lower_rounded.is_finite() || !upper_rounded.is_finite() {
            return None;
        }

        let lower_error = two_sum_error(estimate, -absolute_error_bound, lower_rounded);
        let upper_error = two_sum_error(estimate, absolute_error_bound, upper_rounded);
        if !lower_error.is_finite() || !upper_error.is_finite() {
            return None;
        }

        let lower_bound = if lower_error < 0.0 {
            lower_rounded.next_down()
        } else {
            lower_rounded
        };
        let upper_bound = if upper_error > 0.0 {
            upper_rounded.next_up()
        } else {
            upper_rounded
        };
        if !lower_bound.is_finite() || !upper_bound.is_finite() {
            return None;
        }

        Some(Self {
            estimate,
            absolute_error_bound,
            lower_bound,
            upper_bound,
        })
    }
}

/// State for one certified left-to-right FMA reduction.
///
/// The rounded estimate continues accumulating after `proof_available` becomes
/// false. This lets the public methods distinguish a non-finite estimate
/// (`Err`) from a finite estimate whose proof arithmetic is unavailable
/// (`Ok(None)`). `magnitude_upper` encloses the sum of exact product
/// magnitudes while that proof remains available.
#[derive(Clone, Copy, Debug, PartialEq)]
struct CertifiedReduction {
    estimate: f64,
    magnitude_upper: f64,
    proof_available: bool,
}

impl CertifiedReduction {
    const ZERO: Self = Self {
        estimate: 0.0,
        magnitude_upper: 0.0,
        proof_available: true,
    };

    /// Add one exact-real product through one rounded FMA.
    ///
    /// A non-finite estimate becomes a typed public error. Underflow or range
    /// loss confined to proof construction instead clears `proof_available`,
    /// preserving the finite estimate for the eventual `Ok(None)` result.
    const fn add_product(
        mut self,
        left: f64,
        right: f64,
        operation: ArithmeticOperation,
        index: usize,
    ) -> Result<Self, LaError> {
        let prior = self.estimate;
        let estimate = left.mul_add(right, prior);
        if !estimate.is_finite() {
            cold_path();
            return Err(LaError::non_finite_computation_step(operation, index));
        }

        if self.proof_available {
            self.proof_available = estimate.is_normal()
                || (estimate == 0.0 && Self::fma_result_is_exact_zero(left, right, prior));
        }
        if self.proof_available {
            match Self::add_product_magnitude_upper(self.magnitude_upper, left, right) {
                Some(magnitude_upper) => self.magnitude_upper = magnitude_upper,
                None => self.proof_available = false,
            }
        }
        self.estimate = estimate;
        Ok(self)
    }

    /// Return whether `left × right + addend` is exactly zero.
    ///
    /// This distinguishes exact cancellation from a nonzero value rounded to
    /// zero. Only exact zero is admissible under the relative-error model used
    /// by the public certified reductions.
    const fn fma_result_is_exact_zero(left: f64, right: f64, addend: f64) -> bool {
        if left == 0.0 || right == 0.0 {
            return addend == 0.0;
        }

        let rounded_product = left * right;
        let rounded_bits = rounded_product.to_bits();
        let negated_addend_bits = (-addend).to_bits();
        let same_rounded_value = rounded_bits == negated_addend_bits
            || (rounded_bits << 1 == 0 && negated_addend_bits << 1 == 0);
        rounded_product.is_finite()
            && same_rounded_value
            && compare_product_with_rounded(left, right, rounded_product) == 0
    }

    /// Add an upward-rounded bound on `|left × right|` to the magnitude sum.
    ///
    /// `None` means a nonzero product was not normal or the product/sum could
    /// not be enclosed by a finite binary64 value, so the public result must be
    /// proof-unavailable.
    const fn add_product_magnitude_upper(
        magnitude_upper: f64,
        left: f64,
        right: f64,
    ) -> Option<f64> {
        if left == 0.0 || right == 0.0 {
            return Some(magnitude_upper);
        }

        let left_magnitude = left.abs();
        let right_magnitude = right.abs();
        let rounded_product = left_magnitude * right_magnitude;
        if !rounded_product.is_normal() {
            return None;
        }

        let product_upper =
            if compare_product_with_rounded(left_magnitude, right_magnitude, rounded_product) > 0 {
                rounded_product.next_up()
            } else {
                rounded_product
            };
        if !product_upper.is_finite() {
            return None;
        }

        if magnitude_upper == 0.0 {
            return Some(product_upper);
        }
        let rounded_sum = magnitude_upper + product_upper;
        if !rounded_sum.is_finite() {
            return None;
        }
        let sum_upper = rounded_sum.next_up();
        if sum_upper.is_finite() {
            Some(sum_upper)
        } else {
            None
        }
    }

    /// Finish the reduction with an upward-rounded `gamma_n` error bound.
    ///
    /// Returns `None` when an earlier proof step failed, the term count cannot
    /// support the relative-error model, or the final bound/endpoints cannot
    /// remain finite. Otherwise the result satisfies every public
    /// [`ScalarWithErrorBound`] invariant.
    #[expect(
        clippy::cast_precision_loss,
        reason = "a usable gamma requires a term count below 2^53, where the cast is exact"
    )]
    const fn finish(self, term_count: Option<usize>) -> Option<ScalarWithErrorBound> {
        if !self.proof_available {
            return None;
        }
        if self.magnitude_upper == 0.0 {
            return ScalarWithErrorBound::try_new(self.estimate, 0.0);
        }

        let Some(term_count) = term_count else {
            return None;
        };
        let scaled_roundoff = (term_count as f64) * (f64::EPSILON / 2.0);
        if !scaled_roundoff.is_finite() || scaled_roundoff >= 1.0 {
            return None;
        }

        // The count conversion, multiplication by 2^-53, and subtraction from
        // one are exact throughout the usable range. Round the division and
        // final multiplication upward to retain a certified upper bound.
        let gamma = scaled_roundoff / (1.0 - scaled_roundoff);
        let gamma_upper = gamma.next_up();
        if !gamma_upper.is_finite() {
            return None;
        }
        let rounded_bound = gamma_upper * self.magnitude_upper;
        if !rounded_bound.is_finite() {
            return None;
        }
        let absolute_error_bound = if rounded_bound == 0.0 {
            0.0
        } else {
            rounded_bound.next_up()
        };
        ScalarWithErrorBound::try_new(self.estimate, absolute_error_bound)
    }
}

/// Finite fixed-size vector of length `D`, stored inline.
///
/// Public construction rejects NaN and infinity through [`try_new`](Self::try_new),
/// and the storage field is private, so a `Vector` 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
/// boundaries or from values computed during arithmetic, such as overflowed
/// accumulators.
///
/// Direct field construction is intentionally unavailable to downstream callers:
///
/// ```compile_fail
/// use la_stack::Vector;
///
/// let _ = Vector::<2> {
///     data: [1.0, f64::NAN],
/// };
/// ```
#[must_use]
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Vector<const D: usize> {
    data: [f64; D],
}

impl<const D: usize> Vector<D> {
    /// Test-only infallible constructor for finite literal fixtures.
    #[cfg(test)]
    #[inline]
    pub(crate) const fn new(data: [f64; D]) -> Self {
        match Self::try_new(data) {
            Ok(vector) => vector,
            Err(_) => panic!("Vector::new requires finite entries"),
        }
    }

    /// Try to create a finite vector from a backing array.
    ///
    /// This is the public raw-storage boundary for vectors. Successful
    /// construction makes the returned [`Vector`] a finite-storage proof.
    ///
    /// # Examples
    /// ```
    /// use la_stack::prelude::*;
    ///
    /// # fn main() -> Result<(), LaError> {
    /// let v = Vector::<3>::try_new([1.0, 2.0, 3.0])?;
    /// assert_eq!(v.into_array(), [1.0, 2.0, 3.0]);
    /// # Ok(())
    /// # }
    /// ```
    ///
    /// # Errors
    /// Returns [`LaError::NonFinite`] with the first offending entry index when
    /// `data` contains NaN or infinity.
    #[inline]
    pub const fn try_new(data: [f64; D]) -> Result<Self, LaError> {
        if let Some(index) = Self::first_non_finite_entry(&data) {
            Err(LaError::non_finite_input_vector(index))
        } else {
            Ok(Self { data })
        }
    }

    /// Finalize vector storage produced by an arithmetic operation.
    ///
    /// Keeping this validation in the type that owns the finite-storage
    /// invariant prevents a new computation path from accidentally turning raw
    /// non-finite storage into a [`Vector`].
    #[inline]
    pub(crate) const fn from_computation(
        data: [f64; D],
        operation: ArithmeticOperation,
    ) -> Result<Self, LaError> {
        if let Some(index) = Self::first_non_finite_entry(&data) {
            Err(LaError::non_finite_computation_step(operation, index))
        } else {
            Ok(Self { data })
        }
    }

    /// Return the first non-finite stored entry in index order.
    ///
    /// Used by the public raw-storage boundary to report the first offending
    /// index with [`LaError::NonFinite`].
    const fn first_non_finite_entry(data: &[f64; D]) -> Option<usize> {
        let mut i = 0;
        while i < D {
            if !data[i].is_finite() {
                return Some(i);
            }
            i += 1;
        }
        None
    }

    /// All-zeros finite vector.
    ///
    /// # Examples
    /// ```
    /// use la_stack::prelude::*;
    ///
    /// let z = Vector::<2>::zero();
    /// assert_eq!(z.into_array(), [0.0, 0.0]);
    /// ```
    #[inline]
    pub const fn zero() -> Self {
        Self { data: [0.0; D] }
    }

    /// Borrow the finite backing array.
    ///
    /// # Examples
    /// ```
    /// use la_stack::prelude::*;
    ///
    /// # fn main() -> Result<(), LaError> {
    /// let v = Vector::<2>::try_new([1.0, -2.0])?;
    /// assert_eq!(v.as_array(), &[1.0, -2.0]);
    /// # Ok(())
    /// # }
    /// ```
    #[inline]
    #[must_use]
    pub const fn as_array(&self) -> &[f64; D] {
        &self.data
    }

    /// Consume and return the finite backing array.
    ///
    /// # Examples
    /// ```
    /// use la_stack::prelude::*;
    ///
    /// # fn main() -> Result<(), LaError> {
    /// let v = Vector::<2>::try_new([1.0, 2.0])?;
    /// let a = v.into_array();
    /// assert_eq!(a, [1.0, 2.0]);
    /// # Ok(())
    /// # }
    /// ```
    #[inline]
    #[must_use]
    pub const fn into_array(self) -> [f64; D] {
        self.data
    }

    /// Dot product.
    ///
    /// Terms are accumulated in `f64` using [`f64::mul_add`] at each index.
    /// Intermediate rounding occurs, and this method does not provide a
    /// certified absolute rounding bound for the returned dot product. Raw
    /// `Vector` values are finite by construction, so this method only checks
    /// whether the accumulation overflows to NaN or infinity.
    ///
    /// # Examples
    /// ```
    /// use la_stack::prelude::*;
    ///
    /// # fn main() -> Result<(), LaError> {
    /// let a = Vector::<3>::try_new([1.0, 2.0, 3.0])?;
    /// let b = Vector::<3>::try_new([-2.0, 0.5, 4.0])?;
    /// assert!((a.dot(&b)? - 11.0).abs() <= 1e-12);
    /// # Ok(())
    /// # }
    /// ```
    ///
    /// # Errors
    /// Returns [`LaError::NonFinite`] when the accumulated dot product overflows
    /// to NaN or infinity.
    #[inline]
    pub const fn dot(&self, other: &Self) -> Result<f64, LaError> {
        self.dot_with_operation(other, ArithmeticOperation::VectorDotProduct)
    }

    /// Dot product with a certified absolute roundoff bound.
    ///
    /// The estimate uses the deterministic left-to-right recurrence
    /// `s[0] = 0` and `s[i + 1] = self[i].mul_add(other[i], s[i])`. When the
    /// relative-error model is valid, the returned certificate bounds the
    /// difference between `s[D]` and the exact-real expression
    /// `Σᵢ self[i] × other[i]` over the stored binary64 inputs.
    ///
    /// The bound is `gamma_D × Σᵢ |self[i] × other[i]|`, where
    /// `gamma_D = D u / (1 - D u)` and `u = 2^-53`. The magnitude sum and the
    /// published bound are rounded upward. See `REFERENCES.md` \[9-11\].
    ///
    /// `Ok(None)` means no certificate is available because a nonzero product
    /// or FMA result entered the subnormal range, the reduction dimension made
    /// `gamma_D` invalid, or a proof-only magnitude/bound calculation exhausted
    /// the finite binary64 range. It does not mean the exact dot product is
    /// zero. Unlike a user-selected tolerance, a returned error bound describes
    /// rounding in this specific arithmetic tree.
    ///
    /// # Examples
    /// ```
    /// use la_stack::prelude::*;
    ///
    /// # fn main() -> Result<(), LaError> {
    /// let left = Vector::<3>::try_new([1.0, 2.0, 3.0])?;
    /// let right = Vector::<3>::try_new([4.0, 5.0, 6.0])?;
    /// let positive = left.dot_with_errbound(&right)?.and_then(|bounded| {
    ///     if bounded.lower_bound() > 0.0 {
    ///         Some(true)
    ///     } else if bounded.upper_bound() <= 0.0 {
    ///         Some(false)
    ///     } else {
    ///         None // The enclosure cannot establish whether the result is positive.
    ///     }
    /// });
    /// assert_eq!(positive, Some(true));
    ///
    /// // Finite inputs can also lack a certificate; use an exact fallback
    /// // before deciding the sign in this case.
    /// let tiny = Vector::<2>::try_new([1e-200, 1e-200])?;
    /// assert_eq!(tiny.dot_with_errbound(&tiny)?, None);
    /// # Ok(())
    /// # }
    /// ```
    ///
    /// # Errors
    /// Returns [`LaError::NonFinite`] with the first failing reduction index and
    /// [`ArithmeticOperation::VectorDotProduct`] when an FMA estimate becomes
    /// non-finite.
    #[inline]
    pub const fn dot_with_errbound(
        &self,
        other: &Self,
    ) -> Result<Option<ScalarWithErrorBound>, LaError> {
        let left = self.as_array();
        let right = other.as_array();
        let mut reduction = CertifiedReduction::ZERO;
        let mut i = 0;
        while i < D {
            reduction = match reduction.add_product(
                left[i],
                right[i],
                ArithmeticOperation::VectorDotProduct,
                i,
            ) {
                Ok(reduction) => reduction,
                Err(error) => return Err(error),
            };
            i += 1;
        }
        Ok(reduction.finish(Some(D)))
    }

    /// Certified dot product with an unrounded vector difference.
    ///
    /// This evaluates the exact-real expression
    /// `Σᵢ self[i] × (left[i] - right[i])` without first rounding
    /// `left - right` into a [`Vector`]. Its deterministic arithmetic tree is
    ///
    /// ```text
    /// s[0]       = 0
    /// s[2i + 1]  = self[i].mul_add(left[i], s[2i])
    /// s[2i + 2]  = (-self[i]).mul_add(right[i], s[2i + 1]).
    /// ```
    ///
    /// A returned certificate therefore includes all `2D` FMA rounding events
    /// in that tree and bounds the intended expression over the original
    /// binary64 coordinates. When available, its absolute bound is
    /// `gamma_2D × Σᵢ (|self[i] × left[i]| + |self[i] × right[i]|)`, where
    /// `gamma_2D = 2D u / (1 - 2D u)` and `u = 2^-53`; every magnitude and the
    /// final bound are rounded upward. Its
    /// [`lower_bound`](ScalarWithErrorBound::lower_bound) and
    /// [`upper_bound`](ScalarWithErrorBound::upper_bound) can certify a sign or
    /// separation from a caller's threshold. An overlapping endpoint range
    /// remains inconclusive and should trigger the caller's exact fallback.
    ///
    /// `Ok(None)` has the same proof-unavailable meaning as in
    /// [`dot_with_errbound`](Self::dot_with_errbound), including gradual
    /// underflow and proof-only range exhaustion.
    ///
    /// # Examples
    /// ```
    /// use la_stack::prelude::*;
    ///
    /// # fn main() -> Result<(), LaError> {
    /// let axis = Vector::<2>::try_new([2.0, -1.0])?;
    /// let left = Vector::<2>::try_new([4.0, 1.0])?;
    /// let right = Vector::<2>::try_new([1.0, 3.0])?;
    /// let separated = axis.dot_difference_with_errbound(&left, &right)?.and_then(|bounded| {
    ///     if bounded.lower_bound() > 1.0 {
    ///         Some(true)
    ///     } else if bounded.upper_bound() <= 1.0 {
    ///         Some(false)
    ///     } else {
    ///         None // An exact fallback is needed to decide this threshold test.
    ///     }
    /// });
    /// // An unavailable certificate also remains None through and_then.
    /// assert_eq!(separated, Some(true));
    /// # Ok(())
    /// # }
    /// ```
    ///
    /// # Errors
    /// Returns [`LaError::NonFinite`] with the first failing coordinate index
    /// and [`ArithmeticOperation::VectorDotDifference`] when either FMA for
    /// that coordinate produces a non-finite estimate.
    #[inline]
    pub const fn dot_difference_with_errbound(
        &self,
        left: &Self,
        right: &Self,
    ) -> Result<Option<ScalarWithErrorBound>, LaError> {
        let axis = self.as_array();
        let left = left.as_array();
        let right = right.as_array();
        let mut reduction = CertifiedReduction::ZERO;
        let mut i = 0;
        while i < D {
            reduction = match reduction.add_product(
                axis[i],
                left[i],
                ArithmeticOperation::VectorDotDifference,
                i,
            ) {
                Ok(reduction) => reduction,
                Err(error) => return Err(error),
            };
            reduction = match reduction.add_product(
                -axis[i],
                right[i],
                ArithmeticOperation::VectorDotDifference,
                i,
            ) {
                Ok(reduction) => reduction,
                Err(error) => return Err(error),
            };
            i += 1;
        }
        Ok(reduction.finish(D.checked_mul(2)))
    }

    /// Accumulate a dot product while retaining the public operation that owns it.
    const fn dot_with_operation(
        &self,
        other: &Self,
        operation: ArithmeticOperation,
    ) -> Result<f64, LaError> {
        let lhs = self.as_array();
        let rhs = other.as_array();
        let mut acc = 0.0;
        let mut i = 0;
        while i < D {
            acc = lhs[i].mul_add(rhs[i], acc);
            i += 1;
        }
        if acc.is_finite() {
            Ok(acc)
        } else {
            cold_path();
            Err(Self::dot_non_finite_error(lhs, rhs, operation))
        }
    }

    /// Replay a non-finite dot product to locate the first failing step.
    ///
    /// This runs only after the success-path traversal has produced a non-finite
    /// final accumulator. Stored entries are finite, so once a fused multiply-add
    /// produces a non-finite accumulator, later steps cannot make it finite again.
    /// Replaying the same left-to-right operations must therefore find the first
    /// failing index.
    #[cold]
    const fn dot_non_finite_error(
        lhs: &[f64; D],
        rhs: &[f64; D],
        operation: ArithmeticOperation,
    ) -> LaError {
        let mut acc = 0.0;
        let mut i = 0;
        let last = D.saturating_sub(1);
        while i < last {
            acc = lhs[i].mul_add(rhs[i], acc);
            if !acc.is_finite() {
                return LaError::non_finite_computation_step(operation, i);
            }
            i += 1;
        }

        LaError::non_finite_computation_step(operation, last)
    }

    /// Squared Euclidean norm.
    ///
    /// This is computed as `dot(self, self)`, so `norm_squared` has the same
    /// `f64` [`mul_add`](f64::mul_add) accumulation behavior as [`dot`](Self::dot).
    /// Intermediate rounding occurs, and this method does not provide a
    /// certified absolute rounding bound for the returned squared norm.
    /// `Vector` values are finite by construction, so this method only checks
    /// whether the accumulation overflows to NaN or infinity.
    ///
    /// # Examples
    /// ```
    /// use la_stack::prelude::*;
    ///
    /// # fn main() -> Result<(), LaError> {
    /// let v = Vector::<3>::try_new([1.0, 2.0, 3.0])?;
    /// assert!((v.norm_squared()? - 14.0).abs() <= 1e-12);
    /// # Ok(())
    /// # }
    /// ```
    ///
    /// # Errors
    /// Returns [`LaError::NonFinite`] when the accumulated norm overflows to NaN
    /// or infinity.
    #[inline]
    pub const fn norm_squared(&self) -> Result<f64, LaError> {
        self.dot_with_operation(self, ArithmeticOperation::VectorSquaredNorm)
    }

    /// Overflow- and underflow-safe Euclidean norm.
    ///
    /// This computes `sqrt(Σᵢ self[i]²)` with a deterministic left-to-right
    /// scaled sum-of-squares recurrence. Each non-zero magnitude is divided by
    /// the largest magnitude seen so far before it is squared, so intermediate
    /// squares cannot overflow and an all-subnormal vector is scaled into the
    /// normal range. See `REFERENCES.md` \[15\].
    ///
    /// The divisions, fused multiply-adds, square root, and final rescaling are
    /// rounded in binary64. This method does not claim correct rounding or
    /// provide a certified absolute error bound. Because [`Vector`] entries are
    /// finite by construction, it returns a finite non-negative result unless
    /// the exact Euclidean norm rounds outside the finite binary64 range.
    /// Near that boundary, a fixed-size stack accumulator sums the coordinate
    /// squares exactly and compares squared rounding midpoints. This fallback
    /// prevents accumulated roundoff from causing or hiding overflow and does
    /// not require the `exact` feature.
    ///
    /// Unlike [`norm_squared`](Self::norm_squared), this method does not require the
    /// squared norm to be representable. For example, the norm of
    /// `[1.0e200, 1.0e200]` is finite even though its squared norm is not.
    ///
    /// # Examples
    /// ```
    /// use la_stack::prelude::*;
    ///
    /// # fn main() -> Result<(), LaError> {
    /// let ordinary = Vector::<2>::try_new([3.0, 4.0])?;
    /// assert_eq!(ordinary.norm()?, 5.0);
    /// assert_eq!(ordinary.norm_squared()?, 25.0);
    ///
    /// let large = Vector::<2>::try_new([1.0e200, 1.0e200])?;
    /// assert!(large.norm()?.is_finite());
    /// assert_eq!(
    ///     large.norm_squared(),
    ///     Err(LaError::non_finite_computation_step(ArithmeticOperation::VectorSquaredNorm, 0)),
    /// );
    /// # Ok(())
    /// # }
    /// ```
    ///
    /// # Errors
    /// Returns [`LaError::NonFinite`] with
    /// [`ArithmeticOperation::VectorNorm`] when the exact Euclidean norm rounds
    /// to infinity under round-to-nearest, ties-to-even.
    #[inline]
    pub fn norm(&self) -> Result<f64, LaError> {
        let mut entries = self.as_array().iter();
        // The first coordinate establishes the scale without a division or FMA.
        // A zero (or absent) first coordinate preserves the empty-prefix state.
        let mut scale = entries.next().copied().unwrap_or(0.0).abs();
        let mut scaled_sum = 1.0;

        for &entry in entries {
            let magnitude = entry.abs();
            if magnitude == 0.0 {
                continue;
            }

            if scale < magnitude {
                let ratio = scale / magnitude;
                scaled_sum = (scaled_sum * ratio).mul_add(ratio, 1.0);
                scale = magnitude;
            } else {
                let ratio = magnitude / scale;
                scaled_sum = ratio.mul_add(ratio, scaled_sum);
            }
        }

        // With b = bit_length(D), D < 2^b. If scale <= 2^(1023-b),
        // even the L1 upper bound D*scale is below 2^1023. Both the exact
        // norm and the rounded recurrence therefore have ample range margin.
        // Checking scale, rather than only the computed norm, also catches
        // true overflow that rounding in the recurrence could hide.
        let dimension_bits = usize::BITS - D.leading_zeros();
        let safe_scale = f64::from_bits(u64::from(2046 - dimension_bits) << 52);
        if scale > safe_scale {
            return norm_near_overflow(self.as_array(), scale);
        }
        Ok(scale * scaled_sum.sqrt())
    }
}

impl<const D: usize> Default for Vector<D> {
    #[inline]
    fn default() -> Self {
        Self::zero()
    }
}

#[cfg(test)]
mod tests {
    use core::hint::black_box;

    use approx::assert_abs_diff_eq;
    use pastey::paste;

    use super::*;

    fn assert_certified_proof_loss_survives_normal_terms<const D: usize>() {
        let mut left_data = [0.0; D];
        left_data[0] = f64::MIN_POSITIVE;
        left_data[D - 1] = 1.0;
        let mut right_data = [1.0; D];
        right_data[0] = 0.5;
        let left = Vector::new(left_data);
        let right = Vector::new(right_data);

        // The first product is subnormal; the last restores a normal estimate,
        // but cannot restore the lost certificate for the complete reduction.
        assert_abs_diff_eq!(left.dot(&right).unwrap(), 1.0, epsilon = 0.0);
        assert_eq!(left.dot_with_errbound(&right), Ok(None));
        assert_eq!(
            left.dot_difference_with_errbound(&right, &Vector::zero()),
            Ok(None)
        );
    }

    fn assert_certified_proof_loss_preserves_later_overflow<const D: usize>() {
        let mut axis_data = [0.0; D];
        axis_data[0] = f64::MIN_POSITIVE;
        axis_data[D - 1] = f64::MAX;
        let axis = Vector::new(axis_data);
        let mut left_data = [0.0; D];
        left_data[0] = 0.5;
        left_data[D - 1] = 2.0;

        assert_eq!(
            axis.dot_with_errbound(&Vector::new(left_data)),
            Err(LaError::non_finite_computation_step(
                ArithmeticOperation::VectorDotProduct,
                D - 1,
            ))
        );
        let difference_error = Err(LaError::non_finite_computation_step(
            ArithmeticOperation::VectorDotDifference,
            D - 1,
        ));
        assert_eq!(
            axis.dot_difference_with_errbound(&Vector::new(left_data), &Vector::zero()),
            difference_error
        );

        // Overflow in the second FMA must also remain observable after the
        // earlier coordinate has already made certification unavailable.
        left_data[D - 1] = 0.0;
        let mut right_data = [0.0; D];
        right_data[D - 1] = -2.0;
        assert_eq!(
            axis.dot_difference_with_errbound(&Vector::new(left_data), &Vector::new(right_data)),
            difference_error
        );
    }

    macro_rules! gen_certified_reduction_sequence_tests {
        ($d:literal) => {
            paste! {
                #[test]
                fn [<certified_proof_loss_survives_normal_terms_ $d d>]() {
                    assert_certified_proof_loss_survives_normal_terms::<$d>();
                }

                #[test]
                fn [<certified_proof_loss_preserves_later_overflow_ $d d>]() {
                    assert_certified_proof_loss_preserves_later_overflow::<$d>();
                }
            }
        };
    }

    gen_certified_reduction_sequence_tests!(2);
    gen_certified_reduction_sequence_tests!(3);
    gen_certified_reduction_sequence_tests!(4);
    gen_certified_reduction_sequence_tests!(5);

    macro_rules! gen_vector_tests {
        ($d:literal) => {
            paste! {
                #[test]
                fn [<vector_new_as_array_into_array_ $d d>]() {
                    let 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 v = Vector::<$d>::new(arr);

                    for i in 0..$d {
                        assert_abs_diff_eq!(v.as_array()[i], arr[i], epsilon = 0.0);
                    }

                    let out = v.into_array();
                    for i in 0..$d {
                        assert_abs_diff_eq!(out[i], arr[i], epsilon = 0.0);
                    }
                }

                #[test]
                fn [<vector_zero_as_array_into_array_default_ $d d>]() {
                    let z = Vector::<$d>::zero();
                    for &x in z.as_array() {
                        assert_abs_diff_eq!(x, 0.0, epsilon = 0.0);
                    }
                    for x in z.into_array() {
                        assert_abs_diff_eq!(x, 0.0, epsilon = 0.0);
                    }

                    let d = Vector::<$d>::default();
                    for x in d.into_array() {
                        assert_abs_diff_eq!(x, 0.0, epsilon = 0.0);
                    }
                }

                #[test]
                fn [<vector_dot_and_norm_squared_ $d d>]() {
                    // Use black_box to avoid constant-folding/inlining eliminating the actual dot loop,
                    // which can make coverage tools report the mul_add line as uncovered.

                    let a_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 = black_box(*src);
                        }
                        arr
                    };
                    let b_arr = {
                        let mut arr = [0.0f64; $d];
                        let values = [-2.0f64, 0.5, 4.0, -1.0, 2.0];
                        for (dst, src) in arr.iter_mut().zip(values.iter()) {
                            *dst = black_box(*src);
                        }
                        arr
                    };

                    let expected_dot = {
                        let mut acc = 0.0;
                        let mut i = 0;
                        while i < $d {
                            acc = a_arr[i].mul_add(b_arr[i], acc);
                            i += 1;
                        }
                        acc
                    };
                    let expected_norm_squared = {
                        let mut acc = 0.0;
                        let mut i = 0;
                        while i < $d {
                            acc = a_arr[i].mul_add(a_arr[i], acc);
                            i += 1;
                        }
                        acc
                    };

                    let a = Vector::<$d>::new(black_box(a_arr));
                    let b = Vector::<$d>::new(black_box(b_arr));

                    // Call via (black_boxed) fn pointers to discourage inlining, improving line-level coverage
                    // attribution for the loop body.
                    let dot_fn: fn(&Vector<$d>, &Vector<$d>) -> Result<f64, LaError> =
                        black_box(Vector::<$d>::dot);
                    let norm_squared_fn: fn(&Vector<$d>) -> Result<f64, LaError> =
                        black_box(Vector::<$d>::norm_squared);

                    assert_abs_diff_eq!(
                        dot_fn(black_box(&a), black_box(&b)).unwrap(),
                        expected_dot,
                        epsilon = 1e-14
                    );
                    assert_abs_diff_eq!(
                        norm_squared_fn(black_box(&a)).unwrap(),
                        expected_norm_squared,
                        epsilon = 1e-14
                    );
                }

                #[test]
                fn [<vector_certified_dot_and_difference_ $d d>]() {
                    let mut left_data = [0.0; $d];
                    let mut right_data = [0.0; $d];
                    let left_values = [1.0, 2.0, 3.0, 4.0, 5.0];
                    let right_values = [2.0, 3.0, 4.0, 5.0, 6.0];
                    for (destination, source) in left_data.iter_mut().zip(left_values) {
                        *destination = source;
                    }
                    for (destination, source) in right_data.iter_mut().zip(right_values) {
                        *destination = source;
                    }
                    let left = Vector::<$d>::new(left_data);
                    let right = Vector::<$d>::new(right_data);
                    let zero = Vector::<$d>::zero();

                    let dot = left.dot(&right).unwrap();
                    let dot_bound = left.dot_with_errbound(&right).unwrap().unwrap();
                    assert_abs_diff_eq!(dot_bound.estimate(), dot, epsilon = 0.0);
                    assert!(dot_bound.absolute_error_bound() >= 0.0);
                    assert!(dot_bound.lower_bound() <= dot);
                    assert!(dot <= dot_bound.upper_bound());

                    let difference_bound = left
                        .dot_difference_with_errbound(&right, &zero)
                        .unwrap()
                        .unwrap();
                    assert_abs_diff_eq!(difference_bound.estimate(), dot, epsilon = 0.0);
                    assert!(difference_bound.lower_bound() <= dot);
                    assert!(dot <= difference_bound.upper_bound());
                }

                #[test]
                fn [<vector_try_new_rejects_non_finite_ $d d>]() {
                    for value in [f64::NAN, f64::INFINITY, f64::NEG_INFINITY] {
                        let mut data = [1.0f64; $d];
                        data[$d - 1] = value;
                        assert_eq!(
                            Vector::<$d>::try_new(data),
                            Err(LaError::non_finite_input_vector($d - 1))
                        );
                    }

                    let mut data = [1.0f64; $d];
                    data[0] = f64::INFINITY;
                    data[$d - 1] = f64::NAN;
                    assert_eq!(
                        Vector::<$d>::try_new(data),
                        Err(LaError::non_finite_input_vector(0))
                    );
                }

                #[test]
                fn [<vector_from_computation_preserves_failure_provenance_ $d d>]() {
                    let mut data = [1.0f64; $d];
                    data[$d - 1] = f64::INFINITY;

                    assert_eq!(
                        Vector::<$d>::from_computation(
                            data,
                            ArithmeticOperation::LuSolve,
                        ),
                        Err(LaError::non_finite_computation_step(
                            ArithmeticOperation::LuSolve,
                            $d - 1,
                        ))
                    );
                }

                #[test]
                fn [<vector_dot_and_norm_squared_reject_overflow_ $d d>]() {
                    let mut a_arr = [1.0f64; $d];
                    a_arr[0] = f64::MAX;
                    let a = Vector::<$d>::new(a_arr);

                    let mut b_arr = [1.0f64; $d];
                    b_arr[0] = 2.0;
                    let b = Vector::<$d>::new(b_arr);

                    assert_eq!(
                        a.dot(&b),
                        Err(LaError::non_finite_computation_step(
                            ArithmeticOperation::VectorDotProduct,
                            0,
                        ))
                    );
                    assert_eq!(
                        a.dot_with_errbound(&b),
                        Err(LaError::non_finite_computation_step(
                            ArithmeticOperation::VectorDotProduct,
                            0,
                        ))
                    );
                    assert_eq!(
                        a.dot_difference_with_errbound(&b, &Vector::zero()),
                        Err(LaError::non_finite_computation_step(
                            ArithmeticOperation::VectorDotDifference,
                            0,
                        ))
                    );
                    assert_eq!(
                        a.norm_squared(),
                        Err(LaError::non_finite_computation_step(
                            ArithmeticOperation::VectorSquaredNorm,
                            0,
                        ))
                    );
                }

            }
        };
    }

    // Mirror delaunay-style multi-dimension tests.
    gen_vector_tests!(1);
    gen_vector_tests!(2);
    gen_vector_tests!(3);
    gen_vector_tests!(4);
    gen_vector_tests!(5);
    gen_vector_tests!(6);
    gen_vector_tests!(7);
    gen_vector_tests!(8);

    fn known_norm_input<const D: usize>() -> ([f64; D], f64) {
        let mut data = [0.0; D];
        if D == 1 {
            data[0] = -5.0;
        } else if D >= 2 {
            data[0] = -3.0;
            data[1] = 4.0;
        }
        (data, if D == 0 { 0.0 } else { 5.0 })
    }

    macro_rules! gen_vector_norm_known_answer_tests {
        ($d:literal) => {
            paste! {
                #[test]
                fn [<vector_norm_known_answer_ $d d>]() {
                    let (data, expected) = known_norm_input::<$d>();
                    let vector = Vector::<$d>::new(data);

                    assert_eq!(vector.norm(), Ok(expected));
                }
            }
        };
    }

    gen_vector_norm_known_answer_tests!(0);
    gen_vector_norm_known_answer_tests!(1);
    gen_vector_norm_known_answer_tests!(2);
    gen_vector_norm_known_answer_tests!(3);
    gen_vector_norm_known_answer_tests!(4);
    gen_vector_norm_known_answer_tests!(5);
    gen_vector_norm_known_answer_tests!(6);
    gen_vector_norm_known_answer_tests!(7);
    gen_vector_norm_known_answer_tests!(8);

    macro_rules! gen_vector_replay_tests {
        ($d:literal) => {
            paste! {
                #[test]
                fn [<vector_dot_and_norm_squared_report_last_overflowing_step_ $d d>]() {
                    let mut dot_lhs = [1.0f64; $d];
                    dot_lhs[$d - 1] = f64::MAX;
                    let mut dot_rhs = [1.0f64; $d];
                    dot_rhs[$d - 1] = 2.0;
                    let dot_lhs = Vector::<$d>::new(dot_lhs);
                    let dot_rhs = Vector::<$d>::new(dot_rhs);

                    assert_eq!(
                        dot_lhs.dot(&dot_rhs),
                        Err(LaError::non_finite_computation_step(
                            ArithmeticOperation::VectorDotProduct,
                            $d - 1,
                        ))
                    );

                    let mut norm_data = [1.0f64; $d];
                    norm_data[$d - 1] = f64::MAX;
                    let vector = Vector::<$d>::new(norm_data);

                    assert_eq!(
                        vector.norm_squared(),
                        Err(LaError::non_finite_computation_step(
                            ArithmeticOperation::VectorSquaredNorm,
                            $d - 1,
                        ))
                    );
                }
            }
        };
    }

    gen_vector_replay_tests!(2);
    gen_vector_replay_tests!(3);
    gen_vector_replay_tests!(4);
    gen_vector_replay_tests!(5);

    macro_rules! gen_vector_const_eval_tests {
        ($d:literal, $dot:literal, $norm_squared:literal) => {
            paste! {
                #[test]
                fn [<vector_dot_and_norm_squared_const_eval_ $d d>]() {
                    const DOT: Result<f64, LaError> = Vector::<$d>::new([1.0; $d])
                        .dot(&Vector::<$d>::new([2.0; $d]));
                    const NORM_SQUARED: Result<f64, LaError> =
                        Vector::<$d>::new([1.0; $d]).norm_squared();

                    assert_eq!(DOT, Ok($dot));
                    assert_eq!(NORM_SQUARED, Ok($norm_squared));
                }
            }
        };
    }

    gen_vector_const_eval_tests!(2, 4.0, 2.0);
    gen_vector_const_eval_tests!(3, 6.0, 3.0);
    gen_vector_const_eval_tests!(4, 8.0, 4.0);
    gen_vector_const_eval_tests!(5, 10.0, 5.0);

    #[test]
    fn vector_dot_and_norm_squared_overflow_const_eval() {
        const DOT: Result<f64, LaError> =
            Vector::<2>::new([f64::MAX; 2]).dot(&Vector::<2>::new([1.0; 2]));
        const NORM_SQUARED: Result<f64, LaError> = Vector::<2>::new([f64::MAX; 2]).norm_squared();

        assert_eq!(
            DOT,
            Err(LaError::non_finite_computation_step(
                ArithmeticOperation::VectorDotProduct,
                1,
            ))
        );
        assert_eq!(
            NORM_SQUARED,
            Err(LaError::non_finite_computation_step(
                ArithmeticOperation::VectorSquaredNorm,
                0,
            ))
        );
    }

    #[test]
    fn vector_dot_and_norm_squared_preserve_fma_and_left_to_right_order() {
        let dot_large = 9_007_199_254_740_992.0;
        let dot_lhs = Vector::<4>::new([dot_large, 1.0, 1.0, 1.0]);
        let dot_rhs = Vector::<4>::new([1.0; 4]);
        assert_eq!(dot_lhs.dot(&dot_rhs), Ok(dot_large));

        let fused_lhs = Vector::<2>::new([f64::MAX, f64::MAX]);
        let fused_rhs = Vector::<2>::new([-1.0, 2.0]);
        assert_eq!(fused_lhs.dot(&fused_rhs), Ok(f64::MAX));

        let norm_large = 134_217_728.0;
        let vector = Vector::<4>::new([norm_large, 1.0, 1.0, 1.0]);
        assert_eq!(vector.norm_squared(), Ok(norm_large * norm_large));
    }

    #[test]
    fn vector_norm_preserves_zero_sign_and_subnormal_magnitudes() {
        let signed_zero = Vector::<4>::new([-0.0, 0.0, -0.0, 0.0]);
        assert_eq!(signed_zero.norm().unwrap().to_bits(), 0.0f64.to_bits());

        let least_subnormal = f64::from_bits(1);
        let subnormal = Vector::<2>::new([3.0 * least_subnormal, -4.0 * least_subnormal]);
        assert_eq!(
            subnormal.norm().unwrap().to_bits(),
            (5.0 * least_subnormal).to_bits()
        );
    }

    #[test]
    fn vector_norm_handles_mixed_and_overflowing_magnitudes() {
        let large = Vector::<2>::new([1.0e200, -1.0e200]);
        let expected = 2.0f64.sqrt() * 1.0e200;
        assert_abs_diff_eq!(large.norm().unwrap(), expected, epsilon = 2.0e184);
        assert_eq!(
            large.norm_squared(),
            Err(LaError::non_finite_computation_step(
                ArithmeticOperation::VectorSquaredNorm,
                0,
            )),
        );

        let mixed = Vector::<4>::new([1.0e200, 1.0e-200, -f64::from_bits(1), 0.0]);
        assert_eq!(mixed.norm(), Ok(1.0e200));

        let unrepresentable = Vector::<2>::new([f64::MAX, f64::MAX]);
        assert_eq!(
            unrepresentable.norm(),
            Err(LaError::non_finite_computation_scalar(
                ArithmeticOperation::VectorNorm,
            ))
        );
    }

    #[test]
    fn vector_norm_accepts_largest_finite_norm() {
        let maximum = Vector::<2>::new([f64::MAX, 0.0]);

        assert_eq!(maximum.norm(), Ok(f64::MAX));
    }

    #[test]
    fn certified_dot_preserves_fma_estimate_and_withholds_range_exhausted_bound() {
        let left = Vector::<2>::new([f64::MAX, f64::MAX]);
        let right = Vector::<2>::new([-1.0, 2.0]);

        assert_eq!(left.dot(&right), Ok(f64::MAX));
        assert_eq!(left.dot_with_errbound(&right), Ok(None));
    }

    #[test]
    fn certified_dot_withholds_bound_when_finite_endpoints_cannot_be_published() {
        let maximum = Vector::<1>::new([f64::MAX]);
        let one = Vector::<1>::new([1.0]);

        assert_eq!(maximum.dot(&one), Ok(f64::MAX));
        assert_eq!(maximum.dot_with_errbound(&one), Ok(None));
    }

    #[test]
    fn certified_dot_withholds_bound_when_magnitude_sum_exhausts_range() {
        let maximum = Vector::<2>::new([f64::MAX, f64::MAX]);

        for factor in [0.5, 0.75] {
            let cancelling = Vector::<2>::new([factor, -factor]);
            let estimate = maximum
                .dot(&cancelling)
                .expect("the cancelling FMA estimate must remain finite");
            assert!(estimate.is_finite());
            assert_eq!(maximum.dot_with_errbound(&cancelling), Ok(None));
        }
    }

    #[test]
    fn certified_bounds_distinguish_conclusive_and_inconclusive_results() {
        let conclusive = Vector::<2>::new([1.0, 2.0])
            .dot_with_errbound(&Vector::new([3.0, 4.0]))
            .unwrap()
            .unwrap();
        assert_abs_diff_eq!(conclusive.estimate(), 11.0, epsilon = 0.0);
        assert!(conclusive.lower_bound() > 1.0);

        let inconclusive = Vector::<2>::new([1.0, 1.0])
            .dot_with_errbound(&Vector::new([1.0, -1.0]))
            .unwrap()
            .unwrap();
        assert_abs_diff_eq!(inconclusive.estimate(), 0.0, epsilon = 0.0);
        assert!(inconclusive.absolute_error_bound() > 0.0);
        assert!(inconclusive.lower_bound() < 0.0);
        assert!(inconclusive.upper_bound() > 0.0);
    }

    #[test]
    fn certified_zero_and_signed_zero_have_an_exact_zero_bound() {
        let left = Vector::<3>::new([-0.0, 0.0, -0.0]);
        let right = Vector::<3>::new([f64::MAX, -1.0, f64::MIN_POSITIVE]);
        let bounded = left.dot_with_errbound(&right).unwrap().unwrap();

        assert_abs_diff_eq!(bounded.estimate(), 0.0, epsilon = 0.0);
        assert_abs_diff_eq!(bounded.absolute_error_bound(), 0.0, epsilon = 0.0);
        assert_abs_diff_eq!(bounded.lower_bound(), 0.0, epsilon = 0.0);
        assert_abs_diff_eq!(bounded.upper_bound(), 0.0, epsilon = 0.0);
    }

    #[test]
    fn certified_reductions_withhold_bounds_for_subnormal_products() {
        let tiny = Vector::<1>::new([f64::MIN_POSITIVE]);
        let half = Vector::<1>::new([0.5]);
        assert_eq!(tiny.dot_with_errbound(&half), Ok(None));

        let min_subnormal = Vector::<1>::new([f64::from_bits(1)]);
        assert_eq!(
            min_subnormal.dot_with_errbound(&Vector::new([1.0])),
            Ok(None)
        );
        assert_eq!(
            min_subnormal.dot_with_errbound(&Vector::new([0.5])),
            Ok(None)
        );
        assert_eq!(
            tiny.dot_difference_with_errbound(&half, &Vector::zero()),
            Ok(None)
        );
    }

    #[test]
    fn certified_reduction_detects_fma_cancellation_below_subnormal_range() {
        let near_sqrt_min = f64::from_bits((512_u64 << 52) | 1);
        let rounded_product = near_sqrt_min * near_sqrt_min;
        assert!(rounded_product.is_normal());
        assert_abs_diff_eq!(
            near_sqrt_min.mul_add(near_sqrt_min, -rounded_product),
            0.0,
            epsilon = 0.0
        );

        let left = Vector::<2>::new([-rounded_product, near_sqrt_min]);
        let right = Vector::<2>::new([1.0, near_sqrt_min]);
        assert_eq!(left.dot(&right), Ok(0.0));
        assert_eq!(left.dot_with_errbound(&right), Ok(None));
    }

    #[test]
    fn certified_dot_handles_mixed_normal_magnitudes() {
        let left = Vector::<2>::new([1.0e100, 1.0e-100]);
        let right = Vector::<2>::new([1.0e-100, 1.0e100]);
        let bounded = left.dot_with_errbound(&right).unwrap().unwrap();

        assert_abs_diff_eq!(bounded.estimate(), 2.0, epsilon = 0.0);
        assert!(bounded.lower_bound() <= 2.0);
        assert!(bounded.upper_bound() >= 2.0);
    }

    #[test]
    fn certified_dot_difference_does_not_round_coordinates_first() {
        let scale = 18_014_398_509_481_984.0;
        let axis = Vector::<1>::new([scale]);
        let left = Vector::<1>::new([1.0]);
        let right = Vector::<1>::new([1.0 / scale]);
        assert_abs_diff_eq!(left.as_array()[0] - right.as_array()[0], 1.0, epsilon = 0.0);

        let bounded = axis
            .dot_difference_with_errbound(&left, &right)
            .unwrap()
            .unwrap();
        assert_abs_diff_eq!(bounded.estimate(), scale, epsilon = 0.0);
        assert!(bounded.lower_bound() < scale);
        assert!(bounded.upper_bound() >= scale);
    }

    #[test]
    fn certified_reductions_are_const_evaluable() {
        const DOT: Result<Option<ScalarWithErrorBound>, LaError> =
            Vector::<2>::new([1.0, 2.0]).dot_with_errbound(&Vector::<2>::new([3.0, 4.0]));
        const DIFFERENCE: Result<Option<ScalarWithErrorBound>, LaError> =
            Vector::<2>::new([2.0, -1.0]).dot_difference_with_errbound(
                &Vector::<2>::new([4.0, 1.0]),
                &Vector::<2>::new([1.0, 3.0]),
            );

        assert_abs_diff_eq!(DOT.unwrap().unwrap().estimate(), 11.0, epsilon = 0.0);
        assert_abs_diff_eq!(DIFFERENCE.unwrap().unwrap().estimate(), 8.0, epsilon = 0.0);
    }

    #[test]
    fn certified_dot_difference_reports_second_fma_overflow() {
        let axis = Vector::<2>::new([1.0, f64::MAX]);
        let left = Vector::<2>::new([0.0, 1.0]);
        let right = Vector::<2>::new([0.0, -1.0]);

        assert_eq!(
            axis.dot_difference_with_errbound(&left, &right),
            Err(LaError::non_finite_computation_step(
                ArithmeticOperation::VectorDotDifference,
                1,
            ))
        );
    }

    #[test]
    fn vector_dot_and_norm_squared_report_first_middle_overflowing_step() {
        let dot_lhs = Vector::<3>::new([f64::MAX, f64::MAX, 1.0]);
        let dot_rhs = Vector::<3>::new([1.0; 3]);
        assert_eq!(
            dot_lhs.dot(&dot_rhs),
            Err(LaError::non_finite_computation_step(
                ArithmeticOperation::VectorDotProduct,
                1,
            ))
        );

        let norm_large = 1.0e154;
        let vector = Vector::<3>::new([norm_large, norm_large, 1.0]);
        assert_eq!(
            vector.norm_squared(),
            Err(LaError::non_finite_computation_step(
                ArithmeticOperation::VectorSquaredNorm,
                1,
            ))
        );
    }

    #[test]
    fn zero_dimension_vector_has_zero_dot_and_norm() {
        let vector = Vector::<0>::try_new([]).unwrap();

        assert!(vector.as_array().is_empty());
        assert!(vector.into_array().is_empty());
        assert_eq!(vector.dot(&Vector::zero()), Ok(0.0));
        let dot_bound = vector.dot_with_errbound(&Vector::zero()).unwrap().unwrap();
        assert_abs_diff_eq!(dot_bound.absolute_error_bound(), 0.0, epsilon = 0.0);
        let difference_bound = vector
            .dot_difference_with_errbound(&Vector::zero(), &Vector::zero())
            .unwrap()
            .unwrap();
        assert_abs_diff_eq!(difference_bound.absolute_error_bound(), 0.0, epsilon = 0.0);
        assert_eq!(vector.norm_squared(), Ok(0.0));
    }

    #[test]
    fn certified_dot_withholds_bound_when_exact_product_exceeds_finite_range() {
        let left_value = f64::from_bits(0x7fe3_0319_b612_3729);
        let right_value = f64::from_bits(0x3ffa_ee21_bf46_bc00);
        let left = Vector::<1>::new([left_value]);
        let right = Vector::<1>::new([right_value]);

        assert!(left_value.mul_add(right_value, -f64::MAX) > 0.0);
        assert_eq!(left.dot(&right), Ok(f64::MAX));
        assert_eq!(left.dot_with_errbound(&right), Ok(None));
    }
}