diff --git a/nalgebra-sparse/src/cs.rs b/nalgebra-sparse/src/cs.rs index 674c43c0f..83dfdc01d 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 9031d8593..8d6615a9c 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 e03a0fe14..d31c86b02 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 c51945b7c..0bb654b55 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 1554b8a66..b95f048f4 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 a00470d59..b23129329 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 310cffae9..013f6263a 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 42c4511bf..5b29b6a30 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::new(), + nrows: Dyn::from_usize(0), + ncols: Dyn::from_usize(0), + } + } +} + +impl Default for VecStorage { + fn default() -> Self { + Self { + data: Vec::new(), + nrows: R::name(), + ncols: Dyn::from_usize(0), + } + } +} + +impl Default for VecStorage { + fn default() -> Self { + Self { + data: Vec::new(), + 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