Skip to content

Commit 40cc2b7

Browse files
committed
refactor: rename det benchmark and add black_box to tests
Rename "la_stack_det_direct" benchmark to "la_stack_det" since it measures the public det() API which falls back to LU for D≥5. Add black_box to det_direct known-value tests to prevent the compiler from constant-folding the const fn at compile time, ensuring the function body is exercised at runtime for more robust coverage.
1 parent a8344e5 commit 40cc2b7

2 files changed

Lines changed: 18 additions & 11 deletions

File tree

benches/vs_linalg.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,8 +179,8 @@ macro_rules! gen_vs_linalg_benches_for_dim {
179179
});
180180
});
181181

182-
// === Determinant via det_direct (closed-form, no LU) ===
183-
[<group_d $d>].bench_function("la_stack_det_direct", |bencher| {
182+
// === Determinant via det() (closed-form for D≤4, LU for D≥5) ===
183+
[<group_d $d>].bench_function("la_stack_det", |bencher| {
184184
bencher.iter(|| {
185185
let det = black_box(a)
186186
.det(la_stack::DEFAULT_PIVOT_TOL)

src/matrix.rs

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,7 @@ mod tests {
323323

324324
use approx::assert_abs_diff_eq;
325325
use pastey::paste;
326+
use std::hint::black_box;
326327

327328
macro_rules! gen_public_api_matrix_tests {
328329
($d:literal) => {
@@ -437,31 +438,37 @@ mod tests {
437438
#[test]
438439
fn det_direct_d2_known_value() {
439440
// [[1,2],[3,4]] → det = 1*4 - 2*3 = -2
440-
let m = Matrix::<2>::from_rows([[1.0, 2.0], [3.0, 4.0]]);
441+
// black_box prevents compile-time constant folding of the const fn.
442+
let m = black_box(Matrix::<2>::from_rows([[1.0, 2.0], [3.0, 4.0]]));
441443
assert_abs_diff_eq!(m.det_direct().unwrap(), -2.0, epsilon = 1e-15);
442444
}
443445

444446
#[test]
445447
fn det_direct_d3_known_value() {
446448
// Classic 3×3: det = 0
447-
let m = Matrix::<3>::from_rows([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0]]);
449+
let m = black_box(Matrix::<3>::from_rows([
450+
[1.0, 2.0, 3.0],
451+
[4.0, 5.0, 6.0],
452+
[7.0, 8.0, 9.0],
453+
]));
448454
assert_abs_diff_eq!(m.det_direct().unwrap(), 0.0, epsilon = 1e-12);
449455
}
450456

451457
#[test]
452458
fn det_direct_d3_nonsingular() {
453459
// [[2,1,0],[0,3,1],[1,0,2]] → det = 2*(6-0) - 1*(0-1) + 0 = 13
454-
let m = Matrix::<3>::from_rows([[2.0, 1.0, 0.0], [0.0, 3.0, 1.0], [1.0, 0.0, 2.0]]);
460+
let m = black_box(Matrix::<3>::from_rows([
461+
[2.0, 1.0, 0.0],
462+
[0.0, 3.0, 1.0],
463+
[1.0, 0.0, 2.0],
464+
]));
455465
assert_abs_diff_eq!(m.det_direct().unwrap(), 13.0, epsilon = 1e-12);
456466
}
457467

458468
#[test]
459469
fn det_direct_d4_identity() {
460-
assert_abs_diff_eq!(
461-
Matrix::<4>::identity().det_direct().unwrap(),
462-
1.0,
463-
epsilon = 1e-15
464-
);
470+
let m = black_box(Matrix::<4>::identity());
471+
assert_abs_diff_eq!(m.det_direct().unwrap(), 1.0, epsilon = 1e-15);
465472
}
466473

467474
#[test]
@@ -472,7 +479,7 @@ mod tests {
472479
rows[1][1] = 3.0;
473480
rows[2][2] = 5.0;
474481
rows[3][3] = 7.0;
475-
let m = Matrix::<4>::from_rows(rows);
482+
let m = black_box(Matrix::<4>::from_rows(rows));
476483
assert_abs_diff_eq!(m.det_direct().unwrap(), 210.0, epsilon = 1e-12);
477484
}
478485

0 commit comments

Comments
 (0)