From 538e18b3e98a88020fe2c071632c4f3479e120f2 Mon Sep 17 00:00:00 2001 From: sebcrozet Date: Mon, 5 Nov 2018 16:44:59 +0100 Subject: [PATCH] Ensure the output of addition is sorted. --- src/sparse/cs_matrix_ops.rs | 6 +++++- tests/sparse/cs_ops.rs | 4 ++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/src/sparse/cs_matrix_ops.rs b/src/sparse/cs_matrix_ops.rs index 07119c19..34a68f9b 100644 --- a/src/sparse/cs_matrix_ops.rs +++ b/src/sparse/cs_matrix_ops.rs @@ -241,7 +241,11 @@ where &mut res, ); - for p in res.data.p[j]..nz { + // Keep the output sorted. + let range = res.data.p[j]..nz; + res.data.i[range.clone()].sort(); + + for p in range { res.data.vals[p] = workspace[res.data.i[p]] } } diff --git a/tests/sparse/cs_ops.rs b/tests/sparse/cs_ops.rs index fa98fdb3..49dfc2bc 100644 --- a/tests/sparse/cs_ops.rs +++ b/tests/sparse/cs_ops.rs @@ -47,8 +47,8 @@ fn cs_mat_mul() { #[test] fn cs_mat_add() { let m1 = Matrix4x5::new( - 4.0, 1.0, 4.0, 0.0, 9.0, - 5.0, 6.0, 0.0, 8.0, 10.0, + 4.0, 1.0, 4.0, 0.0, 0.0, + 5.0, 6.0, 0.0, 8.0, 0.0, 9.0, 10.0, 11.0, 12.0, 0.0, 0.0, 0.0, 1.0, 0.0, 10.0 );