-
-
Notifications
You must be signed in to change notification settings - Fork 485
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 the Default trait for VecStorage, CsMatrix, CscMatrix, etc. #1206
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,6 +22,16 @@ pub struct CsMatrix<T> { | |
values: Vec<T>, | ||
} | ||
|
||
///The default implementation for the CsMatrix | ||
impl<T> Default for CsMatrix<T> { | ||
fn default() -> Self { | ||
Self { | ||
sparsity_pattern: Default::default(), | ||
values: Default::default(), | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd prefer to be more explicit here, i.e. |
||
} | ||
} | ||
|
||
impl<T> CsMatrix<T> { | ||
/// Create a zero matrix with no explicitly stored entries. | ||
#[inline] | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -52,6 +52,17 @@ pub struct SparsityPattern { | |
minor_dim: usize, | ||
} | ||
|
||
///The default implementation for the SparsityPattern | ||
impl Default for SparsityPattern { | ||
fn default() -> Self { | ||
Self { | ||
major_offsets: Default::default(), | ||
minor_indices: Default::default(), | ||
minor_dim: Default::default(), | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is incorrect since Self {
major_offsets: vec![0],
minor_indices: vec![],
minor_dim: 0
} |
||
} | ||
} | ||
|
||
impl SparsityPattern { | ||
/// Create a sparsity pattern of the given dimensions without explicitly stored entries. | ||
pub fn zeros(major_dim: usize, minor_dim: usize) -> Self { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,6 +31,20 @@ pub struct VecStorage<T, R: Dim, C: Dim> { | |
ncols: C, | ||
} | ||
|
||
/// The default implementation for the VecStorage struct. | ||
/// Requires R and C to implement the default trait. | ||
impl<T, R: Dim + std::default::Default, C: Dim + std::default::Default> Default | ||
for VecStorage<T, R, C> | ||
{ | ||
fn default() -> Self { | ||
Self { | ||
data: Default::default(), | ||
nrows: Default::default(), | ||
ncols: Default::default(), | ||
} | ||
} | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think any dimensions actually implement I think an alternative implementation could divide this into several cases:
This makes it possible to do #[derive(Default)]
struct Wrapper<T> {
vector: DVector<T>,
matrix: DMatrix<T>,
} without requiring There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note also that the proposed impl is unsound, because it always generates an empty vector regardless of whatever the dimensions are. This would lead to unsoundness whenever the dimensions are non-zero and any operation in nalgebra would try to index into the matrix. |
||
#[cfg(feature = "serde-serialize")] | ||
impl<T, R: Dim, C: Dim> Serialize for VecStorage<T, R, C> | ||
where | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would suggest to just remove this doc comment, since it doesn't add anything of value and might get outdated in the future (say if we rename CsMatrix or something like that).
Same for the other default impls.