Skip to content

Commit 9723fde

Browse files
committed
FIX: Allocate row_ics with the correct nnz length in SPMatrix::new
`SPMatrix::new(row, col, nnz)` initialised `row_ics` with `vec![0usize, nnz]` (a literal two-element vector `[0, nnz]`) instead of `vec![0usize; nnz]` (an `nnz`-length zeroed vector). Anything that allocated a fresh `SPMatrix` for in-place writes - most importantly `SPMatrix::transpose` - then panicked with `index out of bounds: the len is 2 but the index is K` on any matrix with more than two non-zeros. The bug surfaced while wiring the sparse-transpose path into `examples/clippy_verify` during the Phase 2 clippy refactor; the test was kept disabled at the time and noted in the commit log of 2f33cb2. Fix the single-character typo and re-enable the sparse section of the determinism oracle, asserting both a hashed transpose result and a double-transpose round-trip on two distinct matrices. Verified separately that the new `transpose().to_dense()` agrees element-wise with the dense `Matrix::transpose` on the same input.
1 parent c94a0c8 commit 9723fde

2 files changed

Lines changed: 13 additions & 9 deletions

File tree

examples/clippy_verify.rs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -220,14 +220,18 @@ fn check_polynomial() {
220220
}
221221

222222
fn check_sparse() {
223-
// SPMatrix transpose (sparse.rs:97, 102) - skipped until the
224-
// `SPMatrix::new` `row_ics` vector-length bug is fixed (line 30
225-
// uses `vec![0usize, nnz]` where it should be `vec![0usize; nnz]`).
226-
// Without that fix `transpose()` panics with index-out-of-bounds on
227-
// any non-trivial matrix. The loops will still be re-checked via
228-
// the regular `cargo test` suite once the underlying bug is
229-
// addressed.
230-
let _ = SPMatrix::from_dense; // keep the import alive
223+
// SPMatrix transpose (sparse.rs:97, 102)
224+
let m = ml_matrix("1 0 2 0; 0 3 0 4; 5 0 0 6; 0 7 8 0");
225+
let sp: SPMatrix = SPMatrix::from_dense(&m);
226+
let spt: SPMatrix = sp.transpose();
227+
let recovered = spt.to_dense();
228+
print_kv("sparse transpose hash", hash_matrix(&recovered));
229+
// Also exercise a non-square shape and verify double-transpose
230+
// returns the original.
231+
let m2 = ml_matrix("1 0 0; 0 2 3; 4 0 5; 0 6 0");
232+
let sp2: SPMatrix = SPMatrix::from_dense(&m2);
233+
let sp2tt = sp2.transpose().transpose().to_dense();
234+
print_kv("sparse round-trip hash", hash_matrix(&sp2tt));
231235
}
232236

233237
fn check_useful() {

src/structure/sparse.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ impl SPMatrix {
2727
col,
2828
nnz,
2929
col_ptr: vec![0usize; col + 1],
30-
row_ics: vec![0usize, nnz],
30+
row_ics: vec![0usize; nnz],
3131
data: vec![0f64; nnz],
3232
}
3333
}

0 commit comments

Comments
 (0)