Implement matrixcompare traits for sparse matrices

This commit is contained in:
Andreas Longva 2021-01-13 13:10:21 +01:00
parent aad2216c56
commit 84557d8046
3 changed files with 67 additions and 0 deletions

View File

@ -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"

View File

@ -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;

View File

@ -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<T: Clone> SparseAccess<T> for $MatrixType<T> {
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<T: Clone> matrixcompare_core::Matrix<T> for $MatrixType<T> {
fn rows(&self) -> usize {
self.nrows()
}
fn cols(&self) -> usize {
self.ncols()
}
fn access(&self) -> Access<T> {
Access::Sparse(self)
}
}
}
}
impl_matrix_for_csr_csc!(CsrMatrix);
impl_matrix_for_csr_csc!(CscMatrix);
impl<T: Clone> SparseAccess<T> for CooMatrix<T> {
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<T: Clone> matrixcompare_core::Matrix<T> for CooMatrix<T> {
fn rows(&self) -> usize {
self.nrows()
}
fn cols(&self) -> usize {
self.ncols()
}
fn access(&self) -> Access<T> {
Access::Sparse(self)
}
}