diff --git a/nalgebra-sparse/Cargo.toml b/nalgebra-sparse/Cargo.toml index 988850d2..8284bc7a 100644 --- a/nalgebra-sparse/Cargo.toml +++ b/nalgebra-sparse/Cargo.toml @@ -6,6 +6,7 @@ edition = "2018" [features] proptest-support = ["proptest", "nalgebra/proptest"] +compare = [ "matrixcompare-core" ] # Enable to enable running some tests that take a lot of time to run slow-tests = [] @@ -14,6 +15,7 @@ slow-tests = [] nalgebra = { version="0.23", path = "../" } num-traits = { version = "0.2", default-features = false } proptest = { version = "0.10", optional = true } +matrixcompare-core = { version = "0.1.0", optional = true } [dev-dependencies] itertools = "0.9" diff --git a/nalgebra-sparse/src/lib.rs b/nalgebra-sparse/src/lib.rs index 9441fc96..fe4255a8 100644 --- a/nalgebra-sparse/src/lib.rs +++ b/nalgebra-sparse/src/lib.rs @@ -76,6 +76,7 @@ //! - Overall design ("easy API" vs. "expert" API etc.) //! - Conversions (From, explicit "expert" API etc.) //! - Matrix ops design +//! - Proptest and matrixcompare integrations #![deny(non_camel_case_types)] #![deny(unused_parens)] #![deny(non_upper_case_globals)] @@ -96,6 +97,9 @@ pub(crate) mod cs; #[cfg(feature = "proptest-support")] pub mod proptest; +#[cfg(feature = "compare")] +mod matrixcompare; + use std::error::Error; use std::fmt; use num_traits::Zero; diff --git a/nalgebra-sparse/src/matrixcompare.rs b/nalgebra-sparse/src/matrixcompare.rs new file mode 100644 index 00000000..a4aafe3c --- /dev/null +++ b/nalgebra-sparse/src/matrixcompare.rs @@ -0,0 +1,61 @@ +//! Implements core traits for use with `matrixcompare`. +use crate::csr::CsrMatrix; +use crate::csc::CscMatrix; +use matrixcompare_core; +use matrixcompare_core::{Access, SparseAccess}; +use crate::coo::CooMatrix; + +macro_rules! impl_matrix_for_csr_csc { + ($MatrixType:ident) => { + impl SparseAccess for $MatrixType { + fn nnz(&self) -> usize { + $MatrixType::nnz(self) + } + + fn fetch_triplets(&self) -> Vec<(usize, usize, T)> { + self.triplet_iter().map(|(i, j, v)| (i, j, v.clone())).collect() + } + } + + impl matrixcompare_core::Matrix for $MatrixType { + fn rows(&self) -> usize { + self.nrows() + } + + fn cols(&self) -> usize { + self.ncols() + } + + fn access(&self) -> Access { + Access::Sparse(self) + } + } + } +} + +impl_matrix_for_csr_csc!(CsrMatrix); +impl_matrix_for_csr_csc!(CscMatrix); + +impl SparseAccess for CooMatrix { + fn nnz(&self) -> usize { + CooMatrix::nnz(self) + } + + fn fetch_triplets(&self) -> Vec<(usize, usize, T)> { + self.triplet_iter().map(|(i, j, v)| (i, j, v.clone())).collect() + } +} + +impl matrixcompare_core::Matrix for CooMatrix { + fn rows(&self) -> usize { + self.nrows() + } + + fn cols(&self) -> usize { + self.ncols() + } + + fn access(&self) -> Access { + Access::Sparse(self) + } +} \ No newline at end of file