Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Default trait for sparse matrix types #1312

Merged
merged 3 commits into from
Nov 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions nalgebra-sparse/src/cs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,15 @@ impl<T> CsMatrix<T> {
}
}

impl<T> Default for CsMatrix<T> {
fn default() -> Self {
Self {
sparsity_pattern: Default::default(),
values: vec![],
}
}
}

impl<T: Scalar + One> CsMatrix<T> {
#[inline]
pub fn identity(n: usize) -> Self {
Expand Down
8 changes: 8 additions & 0 deletions nalgebra-sparse/src/csc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,14 @@ impl<T> CscMatrix<T> {
}
}

impl<T> Default for CscMatrix<T> {
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,
Expand Down
8 changes: 8 additions & 0 deletions nalgebra-sparse/src/csr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -575,6 +575,14 @@ impl<T> CsrMatrix<T> {
}
}

impl<T> Default for CsrMatrix<T> {
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,
Expand Down
10 changes: 10 additions & 0 deletions nalgebra-sparse/src/pattern.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
12 changes: 12 additions & 0 deletions nalgebra-sparse/tests/unit_tests/csc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,18 @@ use crate::common::csc_strategy;

use std::collections::HashSet;

#[test]
fn csc_matrix_default() {
let matrix: CscMatrix<f32> = 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
Expand Down
12 changes: 12 additions & 0 deletions nalgebra-sparse/tests/unit_tests/csr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,18 @@ use crate::common::csr_strategy;

use std::collections::HashSet;

#[test]
fn csr_matrix_default() {
let matrix: CsrMatrix<f32> = 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
Expand Down
14 changes: 14 additions & 0 deletions nalgebra-sparse/tests/unit_tests/pattern.rs
Original file line number Diff line number Diff line change
@@ -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
Expand Down
40 changes: 40 additions & 0 deletions src/base/vec_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,46 @@ pub struct VecStorage<T, R: Dim, C: Dim> {
ncols: C,
}

impl<T> Default for VecStorage<T, Dyn, Dyn> {
fn default() -> Self {
Self {
data: Vec::new(),
nrows: Dyn::from_usize(0),
ncols: Dyn::from_usize(0),
}
}
}

impl<T, R: DimName> Default for VecStorage<T, R, Dyn> {
fn default() -> Self {
Self {
data: Vec::new(),
nrows: R::name(),
ncols: Dyn::from_usize(0),
}
}
}

impl<T, C: DimName> Default for VecStorage<T, Dyn, C> {
fn default() -> Self {
Self {
data: Vec::new(),
nrows: Dyn::from_usize(0),
ncols: C::name(),
}
}
}

impl<T: Default, R: DimName, C: DimName> Default for VecStorage<T, R, C> {
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<T, R: Dim, C: Dim> Serialize for VecStorage<T, R, C>
where
Expand Down
Loading