From 0887b875a5739bb012796e6ee4386a4dabe3a1d4 Mon Sep 17 00:00:00 2001 From: Alessandro Rocco Scisca Date: Mon, 30 Oct 2023 17:50:56 +0000 Subject: [PATCH] Implement Default trait for sparse matrix types --- nalgebra-sparse/src/cs.rs | 9 +++++ nalgebra-sparse/src/csc.rs | 8 +++++ nalgebra-sparse/src/csr.rs | 8 +++++ nalgebra-sparse/src/pattern.rs | 10 ++++++ nalgebra-sparse/tests/unit_tests/csc.rs | 12 +++++++ nalgebra-sparse/tests/unit_tests/csr.rs | 12 +++++++ nalgebra-sparse/tests/unit_tests/pattern.rs | 14 ++++++++ src/base/vec_storage.rs | 40 +++++++++++++++++++++ 8 files changed, 113 insertions(+) diff --git a/nalgebra-sparse/src/cs.rs b/nalgebra-sparse/src/cs.rs index 674c43c0..83dfdc01 100644 --- a/nalgebra-sparse/src/cs.rs +++ b/nalgebra-sparse/src/cs.rs @@ -226,6 +226,15 @@ impl CsMatrix { } } +impl Default for CsMatrix { + fn default() -> Self { + Self { + sparsity_pattern: Default::default(), + values: vec![], + } + } +} + impl CsMatrix { #[inline] pub fn identity(n: usize) -> Self { diff --git a/nalgebra-sparse/src/csc.rs b/nalgebra-sparse/src/csc.rs index 9031d859..8d6615a9 100644 --- a/nalgebra-sparse/src/csc.rs +++ b/nalgebra-sparse/src/csc.rs @@ -574,6 +574,14 @@ impl CscMatrix { } } +impl Default for CscMatrix { + fn default() -> Self { + Self { + cs: Default::default(), + } + } +} + /// Convert pattern format errors into more meaningful CSC-specific errors. /// /// This ensures that the terminology is consistent: we are talking about rows and columns, diff --git a/nalgebra-sparse/src/csr.rs b/nalgebra-sparse/src/csr.rs index e03a0fe1..d31c86b0 100644 --- a/nalgebra-sparse/src/csr.rs +++ b/nalgebra-sparse/src/csr.rs @@ -575,6 +575,14 @@ impl CsrMatrix { } } +impl Default for CsrMatrix { + fn default() -> Self { + Self { + cs: Default::default(), + } + } +} + /// Convert pattern format errors into more meaningful CSR-specific errors. /// /// This ensures that the terminology is consistent: we are talking about rows and columns, diff --git a/nalgebra-sparse/src/pattern.rs b/nalgebra-sparse/src/pattern.rs index c51945b7..0bb654b5 100644 --- a/nalgebra-sparse/src/pattern.rs +++ b/nalgebra-sparse/src/pattern.rs @@ -291,6 +291,16 @@ impl SparsityPattern { } } +impl Default for SparsityPattern { + fn default() -> Self { + Self { + major_offsets: vec![0], + minor_indices: vec![], + minor_dim: 0, + } + } +} + /// Error type for `SparsityPattern` format errors. #[non_exhaustive] #[derive(Copy, Clone, Debug, PartialEq, Eq)] diff --git a/nalgebra-sparse/tests/unit_tests/csc.rs b/nalgebra-sparse/tests/unit_tests/csc.rs index 1554b8a6..b95f048f 100644 --- a/nalgebra-sparse/tests/unit_tests/csc.rs +++ b/nalgebra-sparse/tests/unit_tests/csc.rs @@ -12,6 +12,18 @@ use crate::common::csc_strategy; use std::collections::HashSet; +#[test] +fn csc_matrix_default() { + let matrix: CscMatrix = CscMatrix::default(); + + assert_eq!(matrix.nrows(), 0); + assert_eq!(matrix.ncols(), 0); + assert_eq!(matrix.nnz(), 0); + + assert_eq!(matrix.values(), &[]); + assert!(matrix.get_entry(0, 0).is_none()); +} + #[test] fn csc_matrix_valid_data() { // Construct matrix from valid data and check that selected methods return results diff --git a/nalgebra-sparse/tests/unit_tests/csr.rs b/nalgebra-sparse/tests/unit_tests/csr.rs index a00470d5..b2312932 100644 --- a/nalgebra-sparse/tests/unit_tests/csr.rs +++ b/nalgebra-sparse/tests/unit_tests/csr.rs @@ -12,6 +12,18 @@ use crate::common::csr_strategy; use std::collections::HashSet; +#[test] +fn csr_matrix_default() { + let matrix: CsrMatrix = CsrMatrix::default(); + + assert_eq!(matrix.nrows(), 0); + assert_eq!(matrix.ncols(), 0); + assert_eq!(matrix.nnz(), 0); + + assert_eq!(matrix.values(), &[]); + assert!(matrix.get_entry(0, 0).is_none()); +} + #[test] fn csr_matrix_valid_data() { // Construct matrix from valid data and check that selected methods return results diff --git a/nalgebra-sparse/tests/unit_tests/pattern.rs b/nalgebra-sparse/tests/unit_tests/pattern.rs index 310cffae..ba4c2b53 100644 --- a/nalgebra-sparse/tests/unit_tests/pattern.rs +++ b/nalgebra-sparse/tests/unit_tests/pattern.rs @@ -1,5 +1,19 @@ use nalgebra_sparse::pattern::{SparsityPattern, SparsityPatternFormatError}; +#[test] +fn sparsity_pattern_default() { + // Check that the pattern created with `Default::default()` is equivalent to a zero-sized pattern. + let pattern = SparsityPattern::default(); + let zero = SparsityPattern::zeros(0, 0); + + assert_eq!(pattern.major_dim(), zero.major_dim()); + assert_eq!(pattern.minor_dim(), zero.minor_dim()); + assert_eq!(pattern.major_offsets(), zero.major_offsets()); + assert_eq!(pattern.minor_indices(), zero.minor_indices()); + + assert_eq!(pattern.nnz(), 0); +} + #[test] fn sparsity_pattern_valid_data() { // Construct pattern from valid data and check that selected methods return results diff --git a/src/base/vec_storage.rs b/src/base/vec_storage.rs index 42c4511b..ac353428 100644 --- a/src/base/vec_storage.rs +++ b/src/base/vec_storage.rs @@ -31,6 +31,46 @@ pub struct VecStorage { ncols: C, } +impl Default for VecStorage { + fn default() -> Self { + Self { + data: vec![], + nrows: Dyn::from_usize(0), + ncols: Dyn::from_usize(0), + } + } +} + +impl Default for VecStorage { + fn default() -> Self { + Self { + data: vec![], + nrows: R::name(), + ncols: Dyn::from_usize(0), + } + } +} + +impl Default for VecStorage { + fn default() -> Self { + Self { + data: vec![], + nrows: Dyn::from_usize(0), + ncols: C::name(), + } + } +} + +impl Default for VecStorage { + fn default() -> Self { + let nrows = R::name(); + let ncols = C::name(); + let mut data = Vec::new(); + data.resize_with(nrows.value() * ncols.value(), Default::default); + Self { data, nrows, ncols } + } +} + #[cfg(feature = "serde-serialize")] impl Serialize for VecStorage where