From 666b0fd2de688bbd5cef374963daca40d9039056 Mon Sep 17 00:00:00 2001 From: julianknodt Date: Mon, 21 Aug 2023 23:47:15 -0700 Subject: [PATCH 1/2] Add `try_from_triplets_iter` Calls `try_from_triplets` for now, and is mentioned in the documentation. --- nalgebra-sparse/src/coo.rs | 19 +++++++++++++++++++ nalgebra-sparse/tests/unit_tests/coo.rs | 25 +++++++++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/nalgebra-sparse/src/coo.rs b/nalgebra-sparse/src/coo.rs index b71cb0b6b..6f9698143 100644 --- a/nalgebra-sparse/src/coo.rs +++ b/nalgebra-sparse/src/coo.rs @@ -160,6 +160,25 @@ impl CooMatrix { } } + /// Try to construct a COO matrix from the given dimensions and a finite iterator of + /// (i, j, v) triplets. + /// + /// Returns an error if either row or column indices contain indices out of bounds. + /// Note that the COO format inherently supports duplicate entries, but they are not + /// eagerly summed. + /// + /// Implementation note: + /// Calls try_from_triplets so each value is scanned twice. + pub fn try_from_triplets_iter( + nrows: usize, + ncols: usize, + triplets: impl IntoIterator, + ) -> Result { + let (row_indices, (col_indices, values)) = + triplets.into_iter().map(|(r, c, v)| (r, (c, v))).unzip(); + Self::try_from_triplets(nrows, ncols, row_indices, col_indices, values) + } + /// An iterator over triplets (i, j, v). // TODO: Consider giving the iterator a concrete type instead of impl trait...? pub fn triplet_iter(&self) -> impl Iterator { diff --git a/nalgebra-sparse/tests/unit_tests/coo.rs b/nalgebra-sparse/tests/unit_tests/coo.rs index 8d7f740be..57f5818d2 100644 --- a/nalgebra-sparse/tests/unit_tests/coo.rs +++ b/nalgebra-sparse/tests/unit_tests/coo.rs @@ -186,6 +186,31 @@ fn coo_try_from_triplets_reports_out_of_bounds_indices() { #[test] fn coo_try_from_triplets_panics_on_mismatched_vectors() { + // Check that try_from_triplets panics when the triplet vectors have different lengths + macro_rules! assert_errs { + ($result:expr) => { + assert!(matches!( + $result.unwrap_err().kind(), + SparseFormatErrorKind::IndexOutOfBounds + )) + }; + } + + assert_errs!(CooMatrix::::try_from_triplets_iter( + 3, + 5, + vec![(0, 6, 3.0)].into_iter(), + )); + assert!(CooMatrix::::try_from_triplets_iter( + 3, + 5, + vec![(0, 3, 3.0), (1, 2, 2.0), (0, 3, 1.0),].into_iter(), + ) + .is_ok()); +} + +#[test] +fn coo_try_from_triplets_iter() { // Check that try_from_triplets panics when the triplet vectors have different lengths macro_rules! assert_errs { ($result:expr) => { From b39bd09eaa21ce180e49da69923e803d141780b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Crozet?= Date: Sat, 30 Sep 2023 17:55:04 +0200 Subject: [PATCH 2/2] =?UTF-8?q?chore:=E2=80=AFswap=20test=20names?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- nalgebra-sparse/tests/unit_tests/coo.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/nalgebra-sparse/tests/unit_tests/coo.rs b/nalgebra-sparse/tests/unit_tests/coo.rs index 57f5818d2..c3702c492 100644 --- a/nalgebra-sparse/tests/unit_tests/coo.rs +++ b/nalgebra-sparse/tests/unit_tests/coo.rs @@ -185,8 +185,8 @@ fn coo_try_from_triplets_reports_out_of_bounds_indices() { } #[test] -fn coo_try_from_triplets_panics_on_mismatched_vectors() { - // Check that try_from_triplets panics when the triplet vectors have different lengths +fn coo_try_from_triplets_iter() { + // Check that try_from_triplets_iter panics when the triplet vectors have different lengths macro_rules! assert_errs { ($result:expr) => { assert!(matches!( @@ -210,7 +210,7 @@ fn coo_try_from_triplets_panics_on_mismatched_vectors() { } #[test] -fn coo_try_from_triplets_iter() { +fn coo_try_from_triplets_panics_on_mismatched_vectors() { // Check that try_from_triplets panics when the triplet vectors have different lengths macro_rules! assert_errs { ($result:expr) => {