Remove CooMatrix::to_dense() and `Scalar` trait bound, add ::nnz()

This commit is contained in:
Andreas Longva 2020-11-23 15:58:02 +01:00
parent f20e764edc
commit 41ce9a23df
3 changed files with 15 additions and 27 deletions

View File

@ -1,8 +1,6 @@
//! An implementation of the COO sparse matrix format. //! An implementation of the COO sparse matrix format.
use crate::SparseFormatError; use crate::SparseFormatError;
use nalgebra::{ClosedAdd, DMatrix, Scalar};
use num_traits::Zero;
/// A COO representation of a sparse matrix. /// A COO representation of a sparse matrix.
/// ///
@ -47,8 +45,6 @@ pub struct CooMatrix<T> {
} }
impl<T> CooMatrix<T> impl<T> CooMatrix<T>
where
T: Scalar,
{ {
/// Construct a zero COO matrix of the given dimensions. /// Construct a zero COO matrix of the given dimensions.
/// ///
@ -146,6 +142,16 @@ where
self.ncols 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. /// The row indices of the explicitly stored entries.
pub fn row_indices(&self) -> &[usize] { pub fn row_indices(&self) -> &[usize] {
&self.row_indices &self.row_indices
@ -182,22 +188,4 @@ where
pub fn disassemble(self) -> (Vec<usize>, Vec<usize>, Vec<T>) { pub fn disassemble(self) -> (Vec<usize>, Vec<usize>, Vec<T>) {
(self.row_indices, self.col_indices, self.values) (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<T>
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
}
} }

View File

@ -19,7 +19,7 @@ fn coo_construction_for_valid_data() {
assert!(coo.col_indices().is_empty()); assert!(coo.col_indices().is_empty());
assert!(coo.values().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, 3, 0, 0,
0, 0, 0, 1, 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, 8, 0, 0,
0, 0, 0, 1, 0 0, 0, 0, 1, 0
]); ]);
assert_eq!(coo.to_dense(), expected_dense); assert_eq!(DMatrix::from(&coo), expected_dense);
} }
} }

View File

@ -1,6 +1,6 @@
use nalgebra_sparse::coo::CooMatrix; use nalgebra_sparse::coo::CooMatrix;
use nalgebra_sparse::ops::spmv_coo; use nalgebra_sparse::ops::spmv_coo;
use nalgebra::DVector; use nalgebra::{DVector, DMatrix};
#[test] #[test]
fn spmv_coo_agrees_with_dense_gemv() { fn spmv_coo_agrees_with_dense_gemv() {
@ -20,7 +20,7 @@ fn spmv_coo_agrees_with_dense_gemv() {
let mut y_dense = y.clone(); let mut y_dense = y.clone();
spmv_coo(beta, &mut y, alpha, &a, &x); 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); assert_eq!(y, y_dense);
} }