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
//! Fixed-size, stack-allocated vectors.
use crate::LaError;
/// Fixed-size vector of length `D`, stored inline.
#[must_use]
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Vector<const D: usize> {
pub(crate) data: [f64; D],
}
/// Fixed-size vector whose stored entries are all finite.
///
/// This proof-bearing wrapper makes the finite invariant explicit for internal
/// algorithms that should not rediscover that no stored entry is NaN or
/// infinite.
#[must_use]
#[derive(Clone, Copy, Debug, PartialEq)]
#[allow(clippy::redundant_pub_crate)]
pub(crate) struct FiniteVector<const D: usize> {
vector: Vector<D>,
}
impl<const D: usize> FiniteVector<D> {
/// Construct a finite vector without checking the invariant.
///
/// This crate-internal escape hatch is only for paths with a local proof
/// that every stored entry is finite.
#[inline]
pub(crate) const fn new_unchecked(vector: Vector<D>) -> Self {
Self { vector }
}
/// Validate a vector and wrap it for algorithms that carry the finite
/// invariant explicitly.
///
/// # Errors
/// Returns [`LaError::NonFinite`] with `row: None` and the first offending
/// entry index when `vector` contains NaN or infinity.
#[inline]
pub(crate) const fn new(vector: Vector<D>) -> Result<Self, LaError> {
let mut i = 0;
while i < D {
if !vector.data[i].is_finite() {
return Err(LaError::non_finite_at(i));
}
i += 1;
}
Ok(Self::new_unchecked(vector))
}
/// Validate raw vector storage and construct a finite vector.
/// # Errors
/// Returns [`LaError::NonFinite`] with `row: None` and the first offending
/// entry index when `data` contains NaN or infinity.
#[inline]
pub(crate) const fn from_array(data: [f64; D]) -> Result<Self, LaError> {
match Vector::try_new(data) {
Ok(vector) => Ok(Self::new_unchecked(vector)),
Err(err) => Err(err),
}
}
/// All-zeros finite vector.
#[inline]
pub(crate) const fn zero() -> Self {
Self::new_unchecked(Vector::zero())
}
/// Consume the wrapper and return the underlying raw vector.
#[inline]
pub(crate) const fn into_vector(self) -> Vector<D> {
self.vector
}
/// Borrow the backing array without revalidating stored entries.
#[inline]
pub(crate) const fn as_array(&self) -> &[f64; D] {
self.vector.as_array()
}
/// Consume and return the backing array.
#[inline]
#[must_use]
pub(crate) const fn into_array(self) -> [f64; D] {
self.vector.into_array()
}
/// Dot product for already finite vectors.
///
/// Stored entries are known finite, so this path only checks whether the
/// accumulation overflows to NaN or infinity.
///
/// # Errors
/// Returns [`LaError::NonFinite`] when the accumulated dot product overflows
/// to NaN or infinity.
#[inline]
pub(crate) const fn dot(self, other: Self) -> 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);
if !acc.is_finite() {
return Err(LaError::non_finite_at(i));
}
i += 1;
}
Ok(acc)
}
/// Squared Euclidean norm for an already finite vector.
///
/// Stored entries are known finite, so this path only checks whether the
/// accumulation overflows to NaN or infinity.
///
/// # Errors
/// Returns [`LaError::NonFinite`] when the accumulated norm overflows to NaN
/// or infinity.
#[inline]
pub(crate) const fn norm2_sq(self) -> Result<f64, LaError> {
let data = self.as_array();
let mut acc = 0.0;
let mut i = 0;
while i < D {
acc = data[i].mul_add(data[i], acc);
if !acc.is_finite() {
return Err(LaError::non_finite_at(i));
}
i += 1;
}
Ok(acc)
}
}
impl<const D: usize> Default for FiniteVector<D> {
#[inline]
fn default() -> Self {
Self::zero()
}
}
impl<const D: usize> From<FiniteVector<D>> for Vector<D> {
#[inline]
fn from(value: FiniteVector<D>) -> Self {
value.into_vector()
}
}
impl<const D: usize> TryFrom<Vector<D>> for FiniteVector<D> {
type Error = LaError;
#[inline]
fn try_from(value: Vector<D>) -> Result<Self, Self::Error> {
Self::new(value)
}
}
impl<const D: usize> TryFrom<[f64; D]> for FiniteVector<D> {
type Error = LaError;
#[inline]
fn try_from(value: [f64; D]) -> Result<Self, Self::Error> {
Self::from_array(value)
}
}
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. Public compute
/// methods parse stored entries into crate-internal proof-bearing types
/// before arithmetic, including when crate-internal unchecked storage exists.
///
/// # 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> {
let mut i = 0;
while i < D {
if !data[i].is_finite() {
return Err(LaError::non_finite_at(i));
}
i += 1;
}
Ok(Self::new_unchecked(data))
}
/// Construct a vector without checking that entries are finite.
///
/// This crate-internal escape hatch is reserved for literals and algorithm
/// outputs whose finite invariant is visible at the call site.
#[inline]
pub(crate) const fn new_unchecked(data: [f64; D]) -> Self {
Self { data }
}
/// All-zeros 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::new_unchecked([0.0; D])
}
/// Borrow the 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 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
/// storage is parsed into the crate-internal finite-vector proof before
/// accumulation, so internally unchecked vectors are rejected before
/// arithmetic starts.
///
/// # 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 either input contains NaN/infinity or
/// the accumulated dot product overflows to NaN or infinity.
#[inline]
pub const fn dot(self, other: Self) -> Result<f64, LaError> {
let lhs = match FiniteVector::new(self) {
Ok(lhs) => lhs,
Err(err) => return Err(err),
};
let rhs = match FiniteVector::new(other) {
Ok(rhs) => rhs,
Err(err) => return Err(err),
};
lhs.dot(rhs)
}
/// Squared Euclidean norm.
///
/// This is computed as `dot(self, self)`, so `norm2_sq` 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. Raw
/// storage is parsed into the crate-internal finite-vector proof before
/// accumulation.
///
/// # Examples
/// ```
/// use la_stack::prelude::*;
///
/// # fn main() -> Result<(), LaError> {
/// let v = Vector::<3>::try_new([1.0, 2.0, 3.0])?;
/// assert!((v.norm2_sq()? - 14.0).abs() <= 1e-12);
/// # Ok(())
/// # }
/// ```
///
/// # Errors
/// Returns [`LaError::NonFinite`] when stored entries are NaN/infinity or
/// the accumulated norm overflows to NaN or infinity.
#[inline]
pub const fn norm2_sq(self) -> Result<f64, LaError> {
match FiniteVector::new(self) {
Ok(vector) => vector.norm2_sq(),
Err(err) => Err(err),
}
}
}
impl<const D: usize> Default for Vector<D> {
#[inline]
fn default() -> Self {
Self::zero()
}
}
#[cfg(test)]
mod tests {
use super::*;
use core::hint::black_box;
use approx::assert_abs_diff_eq;
use pastey::paste;
fn assert_array_abs_eq<const D: usize>(actual: &[f64; D], expected: &[f64; D]) {
for i in 0..D {
assert_abs_diff_eq!(actual[i], expected[i], epsilon = 0.0);
}
}
macro_rules! gen_public_api_vector_tests {
($d:literal) => {
paste! {
#[test]
fn [<public_api_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 [<public_api_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 [<public_api_vector_dot_and_norm2_sq_ $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_norm2_sq = {
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 norm2_sq_fn: fn(Vector<$d>) -> Result<f64, LaError> =
black_box(Vector::<$d>::norm2_sq);
assert_abs_diff_eq!(
dot_fn(black_box(a), black_box(b)).unwrap(),
expected_dot,
epsilon = 1e-14
);
assert_abs_diff_eq!(
norm2_sq_fn(black_box(a)).unwrap(),
expected_norm2_sq,
epsilon = 1e-14
);
}
#[test]
fn [<public_api_vector_try_new_rejects_nonfinite_ $d d>]() {
let mut a_arr = [1.0f64; $d];
a_arr[$d - 1] = f64::NAN;
assert_eq!(
Vector::<$d>::try_new(a_arr),
Err(LaError::NonFinite {
row: None,
col: $d - 1,
})
);
}
#[test]
fn [<public_api_vector_try_new_rejects_nonfinite_rhs_fixture_ $d d>]() {
let mut b_arr = [1.0f64; $d];
b_arr[0] = f64::INFINITY;
assert_eq!(
Vector::<$d>::try_new(b_arr),
Err(LaError::NonFinite { row: None, col: 0 })
);
}
#[test]
fn [<public_api_vector_dot_and_norm2_sq_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::NonFinite { row: None, col: 0 }));
assert_eq!(a.norm2_sq(), Err(LaError::NonFinite { row: None, col: 0 }));
}
#[test]
fn [<public_api_vector_methods_revalidate_unchecked_storage_ $d d>]() {
let mut lhs_arr = [1.0f64; $d];
lhs_arr[$d - 1] = f64::NAN;
let lhs = Vector::<$d>::new_unchecked(lhs_arr);
let valid = Vector::<$d>::new([1.0; $d]);
assert_eq!(
lhs.dot(valid),
Err(LaError::NonFinite {
row: None,
col: $d - 1,
})
);
assert_eq!(
lhs.norm2_sq(),
Err(LaError::NonFinite {
row: None,
col: $d - 1,
})
);
let mut rhs_arr = [1.0f64; $d];
rhs_arr[0] = f64::INFINITY;
let rhs = Vector::<$d>::new_unchecked(rhs_arr);
assert_eq!(
valid.dot(rhs),
Err(LaError::NonFinite { row: None, col: 0 })
);
}
#[test]
fn [<finite_vector_accepts_and_roundtrips_ $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 finite = FiniteVector::<$d>::from_array(arr).unwrap();
assert_array_abs_eq(&finite.into_array(), &arr);
assert_array_abs_eq(&finite.into_vector().into_array(), &arr);
assert_array_abs_eq(&FiniteVector::<$d>::zero().into_array(), &[0.0; $d]);
assert_array_abs_eq(&FiniteVector::<$d>::default().into_array(), &[0.0; $d]);
assert_array_abs_eq(Vector::from(finite).as_array(), &arr);
assert_array_abs_eq(
&FiniteVector::<$d>::try_from(Vector::<$d>::new(arr)).unwrap().into_array(),
&arr
);
assert_array_abs_eq(&FiniteVector::<$d>::try_from(arr).unwrap().into_array(), &arr);
}
#[test]
fn [<finite_vector_rejects_nonfinite_with_index_ $d d>]() {
let mut arr = [1.0f64; $d];
arr[$d - 1] = f64::INFINITY;
assert_eq!(
FiniteVector::<$d>::from_array(arr),
Err(LaError::NonFinite {
row: None,
col: $d - 1,
})
);
}
#[test]
fn [<finite_vector_try_from_raw_vector_revalidates_entries_ $d d>]() {
let mut arr = [1.0f64; $d];
arr[$d - 1] = f64::NAN;
let raw = Vector::<$d>::new_unchecked(arr);
assert_eq!(
FiniteVector::<$d>::try_from(raw),
Err(LaError::NonFinite {
row: None,
col: $d - 1,
})
);
}
}
};
}
// Mirror delaunay-style multi-dimension tests.
gen_public_api_vector_tests!(2);
gen_public_api_vector_tests!(3);
gen_public_api_vector_tests!(4);
gen_public_api_vector_tests!(5);
}