From 41ce9a23dfc4321618322bc3c901b9203ebf67e2 Mon Sep 17 00:00:00 2001 From: Andreas Longva Date: Mon, 23 Nov 2020 15:58:02 +0100 Subject: [PATCH] Remove CooMatrix::to_dense() and `Scalar` trait bound, add ::nnz() --- nalgebra-sparse/src/coo.rs | 32 ++++++++----------------- nalgebra-sparse/tests/unit_tests/coo.rs | 6 ++--- nalgebra-sparse/tests/unit_tests/ops.rs | 4 ++-- 3 files changed, 15 insertions(+), 27 deletions(-) diff --git a/nalgebra-sparse/src/coo.rs b/nalgebra-sparse/src/coo.rs index 8206cb09..43662da3 100644 --- a/nalgebra-sparse/src/coo.rs +++ b/nalgebra-sparse/src/coo.rs @@ -1,8 +1,6 @@ //! An implementation of the COO sparse matrix format. use crate::SparseFormatError; -use nalgebra::{ClosedAdd, DMatrix, Scalar}; -use num_traits::Zero; /// A COO representation of a sparse matrix. /// @@ -47,8 +45,6 @@ pub struct CooMatrix { } impl CooMatrix -where - T: Scalar, { /// Construct a zero COO matrix of the given dimensions. /// @@ -146,6 +142,16 @@ where self.ncols } + /// The number of explicitly stored entries in the matrix. + /// + /// This number *includes* duplicate entries. For example, if the `CooMatrix` contains duplicate + /// entries, then it may have a different number of non-zeros as reported by `nnz()` compared + /// to its CSR representation. + #[inline] + pub fn nnz(&self) -> usize { + self.values.len() + } + /// The row indices of the explicitly stored entries. pub fn row_indices(&self) -> &[usize] { &self.row_indices @@ -182,22 +188,4 @@ where pub fn disassemble(self) -> (Vec, Vec, Vec) { (self.row_indices, self.col_indices, self.values) } - - /// Construct the dense representation of the COO matrix. - /// - /// Duplicate entries are summed together. - /// - /// TODO: Remove? - pub fn to_dense(&self) -> DMatrix - where - T: ClosedAdd + Zero, - { - let mut result = DMatrix::zeros(self.nrows, self.ncols); - - for (i, j, v) in self.triplet_iter() { - result[(i, j)] += v.clone(); - } - - result - } } diff --git a/nalgebra-sparse/tests/unit_tests/coo.rs b/nalgebra-sparse/tests/unit_tests/coo.rs index 7bf0c05c..3373d89a 100644 --- a/nalgebra-sparse/tests/unit_tests/coo.rs +++ b/nalgebra-sparse/tests/unit_tests/coo.rs @@ -19,7 +19,7 @@ fn coo_construction_for_valid_data() { assert!(coo.col_indices().is_empty()); assert!(coo.values().is_empty()); - assert_eq!(coo.to_dense(), DMatrix::repeat(3, 2, 0)); + assert_eq!(DMatrix::from(&coo), DMatrix::repeat(3, 2, 0)); } { @@ -51,7 +51,7 @@ fn coo_construction_for_valid_data() { 0, 0, 3, 0, 0, 0, 0, 0, 1, 0 ]); - assert_eq!(coo.to_dense(), expected_dense); + assert_eq!(DMatrix::from(&coo), expected_dense); } { @@ -83,7 +83,7 @@ fn coo_construction_for_valid_data() { 0, 0, 8, 0, 0, 0, 0, 0, 1, 0 ]); - assert_eq!(coo.to_dense(), expected_dense); + assert_eq!(DMatrix::from(&coo), expected_dense); } } diff --git a/nalgebra-sparse/tests/unit_tests/ops.rs b/nalgebra-sparse/tests/unit_tests/ops.rs index a2c06b96..01df60b6 100644 --- a/nalgebra-sparse/tests/unit_tests/ops.rs +++ b/nalgebra-sparse/tests/unit_tests/ops.rs @@ -1,6 +1,6 @@ use nalgebra_sparse::coo::CooMatrix; use nalgebra_sparse::ops::spmv_coo; -use nalgebra::DVector; +use nalgebra::{DVector, DMatrix}; #[test] fn spmv_coo_agrees_with_dense_gemv() { @@ -20,7 +20,7 @@ fn spmv_coo_agrees_with_dense_gemv() { let mut y_dense = y.clone(); spmv_coo(beta, &mut y, alpha, &a, &x); - y_dense.gemv(alpha, &a.to_dense(), &x, beta); + y_dense.gemv(alpha, &DMatrix::from(&a), &x, beta); assert_eq!(y, y_dense); }