Skip to content

Commit 83eccc6

Browse files
authored
Merge pull request #1312 from arscisca/dev-DefaultTrait
Implement Default trait for sparse matrix types
2 parents 2e99320 + c5276c9 commit 83eccc6

File tree

8 files changed

+113
-0
lines changed

8 files changed

+113
-0
lines changed

nalgebra-sparse/src/cs.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,15 @@ impl<T> CsMatrix<T> {
226226
}
227227
}
228228

229+
impl<T> Default for CsMatrix<T> {
230+
fn default() -> Self {
231+
Self {
232+
sparsity_pattern: Default::default(),
233+
values: vec![],
234+
}
235+
}
236+
}
237+
229238
impl<T: Scalar + One> CsMatrix<T> {
230239
#[inline]
231240
pub fn identity(n: usize) -> Self {

nalgebra-sparse/src/csc.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -574,6 +574,14 @@ impl<T> CscMatrix<T> {
574574
}
575575
}
576576

577+
impl<T> Default for CscMatrix<T> {
578+
fn default() -> Self {
579+
Self {
580+
cs: Default::default(),
581+
}
582+
}
583+
}
584+
577585
/// Convert pattern format errors into more meaningful CSC-specific errors.
578586
///
579587
/// This ensures that the terminology is consistent: we are talking about rows and columns,

nalgebra-sparse/src/csr.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -575,6 +575,14 @@ impl<T> CsrMatrix<T> {
575575
}
576576
}
577577

578+
impl<T> Default for CsrMatrix<T> {
579+
fn default() -> Self {
580+
Self {
581+
cs: Default::default(),
582+
}
583+
}
584+
}
585+
578586
/// Convert pattern format errors into more meaningful CSR-specific errors.
579587
///
580588
/// This ensures that the terminology is consistent: we are talking about rows and columns,

nalgebra-sparse/src/pattern.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,16 @@ impl SparsityPattern {
291291
}
292292
}
293293

294+
impl Default for SparsityPattern {
295+
fn default() -> Self {
296+
Self {
297+
major_offsets: vec![0],
298+
minor_indices: vec![],
299+
minor_dim: 0,
300+
}
301+
}
302+
}
303+
294304
/// Error type for `SparsityPattern` format errors.
295305
#[non_exhaustive]
296306
#[derive(Copy, Clone, Debug, PartialEq, Eq)]

nalgebra-sparse/tests/unit_tests/csc.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,18 @@ use crate::common::csc_strategy;
1212

1313
use std::collections::HashSet;
1414

15+
#[test]
16+
fn csc_matrix_default() {
17+
let matrix: CscMatrix<f32> = CscMatrix::default();
18+
19+
assert_eq!(matrix.nrows(), 0);
20+
assert_eq!(matrix.ncols(), 0);
21+
assert_eq!(matrix.nnz(), 0);
22+
23+
assert_eq!(matrix.values(), &[]);
24+
assert!(matrix.get_entry(0, 0).is_none());
25+
}
26+
1527
#[test]
1628
fn csc_matrix_valid_data() {
1729
// Construct matrix from valid data and check that selected methods return results

nalgebra-sparse/tests/unit_tests/csr.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,18 @@ use crate::common::csr_strategy;
1212

1313
use std::collections::HashSet;
1414

15+
#[test]
16+
fn csr_matrix_default() {
17+
let matrix: CsrMatrix<f32> = CsrMatrix::default();
18+
19+
assert_eq!(matrix.nrows(), 0);
20+
assert_eq!(matrix.ncols(), 0);
21+
assert_eq!(matrix.nnz(), 0);
22+
23+
assert_eq!(matrix.values(), &[]);
24+
assert!(matrix.get_entry(0, 0).is_none());
25+
}
26+
1527
#[test]
1628
fn csr_matrix_valid_data() {
1729
// Construct matrix from valid data and check that selected methods return results

nalgebra-sparse/tests/unit_tests/pattern.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,19 @@
11
use nalgebra_sparse::pattern::{SparsityPattern, SparsityPatternFormatError};
22

3+
#[test]
4+
fn sparsity_pattern_default() {
5+
// Check that the pattern created with `Default::default()` is equivalent to a zero-sized pattern.
6+
let pattern = SparsityPattern::default();
7+
let zero = SparsityPattern::zeros(0, 0);
8+
9+
assert_eq!(pattern.major_dim(), zero.major_dim());
10+
assert_eq!(pattern.minor_dim(), zero.minor_dim());
11+
assert_eq!(pattern.major_offsets(), zero.major_offsets());
12+
assert_eq!(pattern.minor_indices(), zero.minor_indices());
13+
14+
assert_eq!(pattern.nnz(), 0);
15+
}
16+
317
#[test]
418
fn sparsity_pattern_valid_data() {
519
// Construct pattern from valid data and check that selected methods return results

src/base/vec_storage.rs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,46 @@ pub struct VecStorage<T, R: Dim, C: Dim> {
3131
ncols: C,
3232
}
3333

34+
impl<T> Default for VecStorage<T, Dyn, Dyn> {
35+
fn default() -> Self {
36+
Self {
37+
data: Vec::new(),
38+
nrows: Dyn::from_usize(0),
39+
ncols: Dyn::from_usize(0),
40+
}
41+
}
42+
}
43+
44+
impl<T, R: DimName> Default for VecStorage<T, R, Dyn> {
45+
fn default() -> Self {
46+
Self {
47+
data: Vec::new(),
48+
nrows: R::name(),
49+
ncols: Dyn::from_usize(0),
50+
}
51+
}
52+
}
53+
54+
impl<T, C: DimName> Default for VecStorage<T, Dyn, C> {
55+
fn default() -> Self {
56+
Self {
57+
data: Vec::new(),
58+
nrows: Dyn::from_usize(0),
59+
ncols: C::name(),
60+
}
61+
}
62+
}
63+
64+
impl<T: Default, R: DimName, C: DimName> Default for VecStorage<T, R, C> {
65+
fn default() -> Self {
66+
let nrows = R::name();
67+
let ncols = C::name();
68+
let mut data = Vec::new();
69+
data.resize_with(nrows.value() * ncols.value(), Default::default);
70+
Self { data, nrows, ncols }
71+
}
72+
}
73+
3474
#[cfg(feature = "serde-serialize")]
3575
impl<T, R: Dim, C: Dim> Serialize for VecStorage<T, R, C>
3676
where

0 commit comments

Comments
 (0)