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
//! Property-based tests for LU/LDLT factorization APIs.
//!
//! These tests construct matrices from known factors so we have a reliable oracle for
//! determinant and solve behavior.
use approx::assert_abs_diff_eq;
use pastey::paste;
use proptest::prelude::*;
use la_stack::prelude::*;
fn small_f64() -> impl Strategy<Value = f64> {
(-1000i16..=1000i16).prop_map(|x| f64::from(x) / 10.0)
}
fn small_factor_entry() -> impl Strategy<Value = f64> {
// Keep entries small so constructed matrices are reasonably conditioned.
(-50i16..=50i16).prop_map(|x| f64::from(x) / 100.0)
}
fn positive_diag_entry() -> impl Strategy<Value = f64> {
// Strictly positive diagonal, comfortably above DEFAULT_SINGULAR_TOL.
(1i16..=20i16).prop_map(|x| f64::from(x) / 10.0)
}
fn nonzero_diag_entry() -> impl Strategy<Value = f64> {
// Strictly non-zero diagonal with a margin from 0.
prop_oneof![(-20i16..=-1i16), (1i16..=20i16)].prop_map(|x| f64::from(x) / 10.0)
}
macro_rules! gen_factorization_proptests {
($d:literal) => {
paste! {
proptest! {
#![proptest_config(ProptestConfig::with_cases(64))]
#[test]
fn [<ldlt_det_and_solve_match_constructed_factors_ $d d>](
l_raw in proptest::array::[<uniform $d>](
proptest::array::[<uniform $d>](small_factor_entry()),
),
d_diag in proptest::array::[<uniform $d>](positive_diag_entry()),
x_true in proptest::array::[<uniform $d>](small_f64()),
) {
// Construct A = L * diag(D) * L^T, where L is unit-lower-triangular.
let mut l = [[0.0f64; $d]; $d];
for i in 0..$d {
for j in 0..$d {
l[i][j] = if i == j {
1.0
} else if i > j {
l_raw[i][j]
} else {
0.0
};
}
}
let mut a_rows = [[0.0f64; $d]; $d];
for i in 0..$d {
for j in 0..=i {
let mut sum = 0.0;
// L[j][k] is zero for k > j.
for k in 0..=j {
sum = (l[i][k] * d_diag[k]).mul_add(l[j][k], sum);
}
a_rows[i][j] = sum;
a_rows[j][i] = sum;
}
}
let expected_det = {
let mut acc = 1.0;
for i in 0..$d {
acc *= d_diag[i];
}
acc
};
let mut b_arr = [0.0f64; $d];
for i in 0..$d {
let mut sum = 0.0;
for j in 0..$d {
sum = a_rows[i][j].mul_add(x_true[j], sum);
}
b_arr[i] = sum;
}
let a = Matrix::<$d>::from_rows(a_rows);
let ldlt = a.ldlt(DEFAULT_SINGULAR_TOL).unwrap();
assert_abs_diff_eq!(ldlt.det(), expected_det, epsilon = 1e-8);
let b = Vector::<$d>::new(b_arr);
let x = ldlt.solve_vec(b).unwrap().into_array();
for i in 0..$d {
assert_abs_diff_eq!(x[i], x_true[i], epsilon = 1e-8);
}
}
#[test]
fn [<lu_det_and_solve_match_constructed_factors_no_perm_ $d d>](
l_raw in proptest::array::[<uniform $d>](
proptest::array::[<uniform $d>](small_factor_entry()),
),
u_raw in proptest::array::[<uniform $d>](
proptest::array::[<uniform $d>](small_factor_entry()),
),
u_diag in proptest::array::[<uniform $d>](nonzero_diag_entry()),
x_true in proptest::array::[<uniform $d>](small_f64()),
) {
// Construct A = L * U, where L is unit-lower-triangular and U is upper-triangular.
let mut l = [[0.0f64; $d]; $d];
for i in 0..$d {
for j in 0..$d {
l[i][j] = if i == j {
1.0
} else if i > j {
l_raw[i][j]
} else {
0.0
};
}
}
let mut u = [[0.0f64; $d]; $d];
for i in 0..$d {
for j in 0..$d {
u[i][j] = if i == j {
u_diag[i]
} else if i < j {
u_raw[i][j]
} else {
0.0
};
}
}
let mut a_rows = [[0.0f64; $d]; $d];
for i in 0..$d {
for j in 0..$d {
let mut sum = 0.0;
// L[i][k] is zero for k > i; U[k][j] is zero for k > j.
let k_max = if i < j { i } else { j };
for k in 0..=k_max {
sum = l[i][k].mul_add(u[k][j], sum);
}
a_rows[i][j] = sum;
}
}
let expected_det = {
let mut acc = 1.0;
for i in 0..$d {
acc *= u_diag[i];
}
acc
};
let mut b_arr = [0.0f64; $d];
for i in 0..$d {
let mut sum = 0.0;
for j in 0..$d {
sum = a_rows[i][j].mul_add(x_true[j], sum);
}
b_arr[i] = sum;
}
let a = Matrix::<$d>::from_rows(a_rows);
let lu = a.lu(DEFAULT_PIVOT_TOL).unwrap();
assert_abs_diff_eq!(lu.det(), expected_det, epsilon = 1e-8);
let b = Vector::<$d>::new(b_arr);
let x = lu.solve_vec(b).unwrap().into_array();
for i in 0..$d {
assert_abs_diff_eq!(x[i], x_true[i], epsilon = 1e-8);
}
}
#[test]
fn [<lu_det_and_solve_match_constructed_factors_row_swap_ $d d>](
l_raw in proptest::array::[<uniform $d>](
proptest::array::[<uniform $d>](small_factor_entry()),
),
u_raw in proptest::array::[<uniform $d>](
proptest::array::[<uniform $d>](small_factor_entry()),
),
u_diag in proptest::array::[<uniform $d>](nonzero_diag_entry()),
x_true in proptest::array::[<uniform $d>](small_f64()),
) {
// Construct A = P^{-1} * L * U, where P swaps the first two rows.
// This ensures det(A) has an extra sign flip vs det(LU).
prop_assume!($d >= 2);
let mut l = [[0.0f64; $d]; $d];
for i in 0..$d {
for j in 0..$d {
l[i][j] = if i == j {
1.0
} else if i > j {
l_raw[i][j]
} else {
0.0
};
}
}
let mut u = [[0.0f64; $d]; $d];
for i in 0..$d {
for j in 0..$d {
u[i][j] = if i == j {
u_diag[i]
} else if i < j {
u_raw[i][j]
} else {
0.0
};
}
}
let mut lu_rows = [[0.0f64; $d]; $d];
for i in 0..$d {
for j in 0..$d {
let mut sum = 0.0;
let k_max = if i < j { i } else { j };
for k in 0..=k_max {
sum = l[i][k].mul_add(u[k][j], sum);
}
lu_rows[i][j] = sum;
}
}
// Apply P^{-1}: swap rows 0 and 1.
let mut a_rows = lu_rows;
a_rows.swap(0, 1);
let expected_det = {
let mut acc = 1.0;
for i in 0..$d {
acc *= u_diag[i];
}
-acc
};
let mut b_arr = [0.0f64; $d];
for i in 0..$d {
let mut sum = 0.0;
for j in 0..$d {
sum = a_rows[i][j].mul_add(x_true[j], sum);
}
b_arr[i] = sum;
}
let a = Matrix::<$d>::from_rows(a_rows);
let lu = a.lu(DEFAULT_PIVOT_TOL).unwrap();
assert_abs_diff_eq!(lu.det(), expected_det, epsilon = 1e-8);
let b = Vector::<$d>::new(b_arr);
let x = lu.solve_vec(b).unwrap().into_array();
for i in 0..$d {
assert_abs_diff_eq!(x[i], x_true[i], epsilon = 1e-8);
}
}
}
}
};
}
// Mirror delaunay-style multi-dimension tests.
gen_factorization_proptests!(2);
gen_factorization_proptests!(3);
gen_factorization_proptests!(4);
gen_factorization_proptests!(5);