From 7b6333e9d116465b349538293fa9aa20601ae45c Mon Sep 17 00:00:00 2001 From: Andreas Longva Date: Mon, 25 Jan 2021 16:04:29 +0100 Subject: [PATCH] Rename some Csr/Csc/SparsityPattern methods --- nalgebra-sparse/src/cs.rs | 11 ++++++++++- nalgebra-sparse/src/csc.rs | 9 ++++++--- nalgebra-sparse/src/csr.rs | 9 ++++++--- nalgebra-sparse/src/lib.rs | 7 ------- nalgebra-sparse/src/pattern.rs | 2 +- nalgebra-sparse/tests/unit_tests/csc.rs | 6 +++--- nalgebra-sparse/tests/unit_tests/csr.rs | 6 +++--- nalgebra-sparse/tests/unit_tests/pattern.rs | 2 +- 8 files changed, 30 insertions(+), 22 deletions(-) diff --git a/nalgebra-sparse/src/cs.rs b/nalgebra-sparse/src/cs.rs index cf8fd382..e9137aa5 100644 --- a/nalgebra-sparse/src/cs.rs +++ b/nalgebra-sparse/src/cs.rs @@ -26,7 +26,7 @@ impl CsMatrix { #[inline] pub fn new(major_dim: usize, minor_dim: usize) -> Self { Self { - sparsity_pattern: SparsityPattern::new(major_dim, minor_dim), + sparsity_pattern: SparsityPattern::zeros(major_dim, minor_dim), values: vec![], } } @@ -185,6 +185,15 @@ impl CsMatrix { Self::from_pattern_and_values(new_pattern, new_values) } + + /// Returns the diagonal of the matrix as a sparse matrix. + pub fn diagonal_as_matrix(&self) -> Self + where + T: Clone + { + // TODO: This might be faster with a binary search for each diagonal entry + self.filter(|i, j, _| i == j) + } } impl CsMatrix { diff --git a/nalgebra-sparse/src/csc.rs b/nalgebra-sparse/src/csc.rs index 39022894..f7a6b71b 100644 --- a/nalgebra-sparse/src/csc.rs +++ b/nalgebra-sparse/src/csc.rs @@ -1,4 +1,7 @@ //! An implementation of the CSC sparse matrix format. +//! +//! This is the module-level documentation. See [`CscMatrix`] for the main documentation of the +//! CSC implementation. use crate::{SparseFormatError, SparseFormatErrorKind, SparseEntry, SparseEntryMut}; use crate::pattern::{SparsityPattern, SparsityPatternFormatError, SparsityPatternIter}; @@ -125,7 +128,7 @@ pub struct CscMatrix { impl CscMatrix { /// Create a zero CSC matrix with no explicitly stored entries. - pub fn new(nrows: usize, ncols: usize) -> Self { + pub fn zeros(nrows: usize, ncols: usize) -> Self { Self { cs: CsMatrix::new(ncols, nrows) } @@ -469,11 +472,11 @@ impl CscMatrix { } /// Returns the diagonal of the matrix as a sparse matrix. - pub fn diagonal_as_matrix(&self) -> Self + pub fn diagonal_as_csc(&self) -> Self where T: Clone { - self.filter(|i, j, _| i == j) + Self { cs: self.cs.diagonal_as_matrix() } } } diff --git a/nalgebra-sparse/src/csr.rs b/nalgebra-sparse/src/csr.rs index 3c0a08ca..bdfb38e9 100644 --- a/nalgebra-sparse/src/csr.rs +++ b/nalgebra-sparse/src/csr.rs @@ -1,4 +1,7 @@ //! An implementation of the CSR sparse matrix format. +//! +//! This is the module-level documentation. See [`CsrMatrix`] for the main documentation of the +//! CSC implementation. use crate::{SparseFormatError, SparseFormatErrorKind, SparseEntry, SparseEntryMut}; use crate::pattern::{SparsityPattern, SparsityPatternFormatError, SparsityPatternIter}; use crate::csc::CscMatrix; @@ -125,7 +128,7 @@ pub struct CsrMatrix { impl CsrMatrix { /// Create a zero CSR matrix with no explicitly stored entries. - pub fn new(nrows: usize, ncols: usize) -> Self { + pub fn zeros(nrows: usize, ncols: usize) -> Self { Self { cs: CsMatrix::new(nrows, ncols) } @@ -469,11 +472,11 @@ impl CsrMatrix { } /// Returns the diagonal of the matrix as a sparse matrix. - pub fn diagonal_as_matrix(&self) -> Self + pub fn diagonal_as_csr(&self) -> Self where T: Clone { - self.filter(|i, j, _| i == j) + Self { cs: self.cs.diagonal_as_matrix() } } } diff --git a/nalgebra-sparse/src/lib.rs b/nalgebra-sparse/src/lib.rs index 0ff74035..3436c147 100644 --- a/nalgebra-sparse/src/lib.rs +++ b/nalgebra-sparse/src/lib.rs @@ -128,13 +128,6 @@ //! assert_matrix_eq!(y, y_expected, comp = abs, tol = 1e-9); //! } //! ``` -//! -//! TODO: Write docs on the following: -//! -//! - 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)] diff --git a/nalgebra-sparse/src/pattern.rs b/nalgebra-sparse/src/pattern.rs index e513108e..3a59a6e7 100644 --- a/nalgebra-sparse/src/pattern.rs +++ b/nalgebra-sparse/src/pattern.rs @@ -50,7 +50,7 @@ pub struct SparsityPattern { impl SparsityPattern { /// Create a sparsity pattern of the given dimensions without explicitly stored entries. - pub fn new(major_dim: usize, minor_dim: usize) -> Self { + pub fn zeros(major_dim: usize, minor_dim: usize) -> Self { Self { major_offsets: vec![0; major_dim + 1], minor_indices: vec![], diff --git a/nalgebra-sparse/tests/unit_tests/csc.rs b/nalgebra-sparse/tests/unit_tests/csc.rs index 4faa4e12..47181987 100644 --- a/nalgebra-sparse/tests/unit_tests/csc.rs +++ b/nalgebra-sparse/tests/unit_tests/csc.rs @@ -21,7 +21,7 @@ fn csc_matrix_valid_data() { let values = Vec::::new(); let mut matrix = CscMatrix::try_from_csc_data(2, 3, offsets, indices, values).unwrap(); - assert_eq!(matrix, CscMatrix::new(2, 3)); + assert_eq!(matrix, CscMatrix::zeros(2, 3)); assert_eq!(matrix.nrows(), 2); assert_eq!(matrix.ncols(), 3); @@ -317,8 +317,8 @@ proptest! { } #[test] - fn csc_diagonal_as_matrix(csc in csc_strategy()) { - let d = csc.diagonal_as_matrix(); + fn csc_diagonal_as_csc(csc in csc_strategy()) { + let d = csc.diagonal_as_csc(); let d_entries: HashSet<_> = d.triplet_iter().cloned_values().collect(); let csc_diagonal_entries: HashSet<_> = csc .triplet_iter() diff --git a/nalgebra-sparse/tests/unit_tests/csr.rs b/nalgebra-sparse/tests/unit_tests/csr.rs index 4885d25b..698fb5f1 100644 --- a/nalgebra-sparse/tests/unit_tests/csr.rs +++ b/nalgebra-sparse/tests/unit_tests/csr.rs @@ -22,7 +22,7 @@ fn csr_matrix_valid_data() { let values = Vec::::new(); let mut matrix = CsrMatrix::try_from_csr_data(3, 2, offsets, indices, values).unwrap(); - assert_eq!(matrix, CsrMatrix::new(3, 2)); + assert_eq!(matrix, CsrMatrix::zeros(3, 2)); assert_eq!(matrix.nrows(), 3); assert_eq!(matrix.ncols(), 2); @@ -318,8 +318,8 @@ proptest! { } #[test] - fn csr_diagonal_as_matrix(csr in csr_strategy()) { - let d = csr.diagonal_as_matrix(); + fn csr_diagonal_as_csr(csr in csr_strategy()) { + let d = csr.diagonal_as_csr(); let d_entries: HashSet<_> = d.triplet_iter().cloned_values().collect(); let csr_diagonal_entries: HashSet<_> = csr .triplet_iter() diff --git a/nalgebra-sparse/tests/unit_tests/pattern.rs b/nalgebra-sparse/tests/unit_tests/pattern.rs index 4664ad74..ee6b2328 100644 --- a/nalgebra-sparse/tests/unit_tests/pattern.rs +++ b/nalgebra-sparse/tests/unit_tests/pattern.rs @@ -23,7 +23,7 @@ fn sparsity_pattern_valid_data() { assert_eq!(pattern.lane(2), &[]); assert!(pattern.entries().next().is_none()); - assert_eq!(pattern, SparsityPattern::new(3, 2)); + assert_eq!(pattern, SparsityPattern::zeros(3, 2)); let (offsets, indices) = pattern.disassemble(); assert_eq!(offsets, vec![0, 0, 0, 0]);