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
//! Fixed-size, stack-allocated vectors.
/// 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],
}
impl<const D: usize> Vector<D> {
/// Create a vector from a backing array.
///
/// # Examples
/// ```
/// use la_stack::prelude::*;
///
/// let v = Vector::<3>::new([1.0, 2.0, 3.0]);
/// assert_eq!(v.into_array(), [1.0, 2.0, 3.0]);
/// ```
#[inline]
pub const fn new(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 { data: [0.0; D] }
}
/// Borrow the backing array.
///
/// # Examples
/// ```
/// use la_stack::prelude::*;
///
/// let v = Vector::<2>::new([1.0, -2.0]);
/// assert_eq!(v.as_array(), &[1.0, -2.0]);
/// ```
#[inline]
#[must_use]
pub const fn as_array(&self) -> &[f64; D] {
&self.data
}
/// Consume and return the backing array.
///
/// # Examples
/// ```
/// use la_stack::prelude::*;
///
/// let v = Vector::<2>::new([1.0, 2.0]);
/// let a = v.into_array();
/// assert_eq!(a, [1.0, 2.0]);
/// ```
#[inline]
#[must_use]
pub const fn into_array(self) -> [f64; D] {
self.data
}
/// Dot product.
///
/// # Examples
/// ```
/// use la_stack::prelude::*;
///
/// let a = Vector::<3>::new([1.0, 2.0, 3.0]);
/// let b = Vector::<3>::new([-2.0, 0.5, 4.0]);
/// assert!((a.dot(b) - 11.0).abs() <= 1e-12);
/// ```
#[inline]
#[must_use]
pub const fn dot(self, other: Self) -> f64 {
let mut acc = 0.0;
let mut i = 0;
while i < D {
acc = self.data[i].mul_add(other.data[i], acc);
i += 1;
}
acc
}
/// Squared Euclidean norm.
///
/// # Examples
/// ```
/// use la_stack::prelude::*;
///
/// let v = Vector::<3>::new([1.0, 2.0, 3.0]);
/// assert!((v.norm2_sq() - 14.0).abs() <= 1e-12);
/// ```
#[inline]
#[must_use]
pub const fn norm2_sq(self) -> f64 {
self.dot(self)
}
}
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;
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>) -> f64 = black_box(Vector::<$d>::dot);
let norm2_sq_fn: fn(Vector<$d>) -> f64 = black_box(Vector::<$d>::norm2_sq);
assert_abs_diff_eq!(dot_fn(black_box(a), black_box(b)), expected_dot, epsilon = 1e-14);
assert_abs_diff_eq!(norm2_sq_fn(black_box(a)), expected_norm2_sq, epsilon = 1e-14);
}
}
};
}
// 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);
}