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

Impl clone for COO, CSC, CSR, and SparseMatrix #73

Merged
merged 1 commit into from
Jan 12, 2024
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
10 changes: 10 additions & 0 deletions russell_sparse/src/coo_matrix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use russell_lab::{Matrix, Vector};
/// * The repeated (i,j) capability is of great convenience for Finite Element solvers
/// * A maximum number of entries must be decided prior to allocating a new COO matrix
/// * The maximum number of entries includes possible entries with repeated indices
#[derive(Clone)]
pub struct CooMatrix {
/// Defines the symmetry and storage: lower-triangular, upper-triangular, full-matrix
///
Expand Down Expand Up @@ -1061,4 +1062,13 @@ mod tests {
x.reverse();
assert_eq!(coo.get_values_mut(), &[456.0, 123.0]);
}

#[test]
fn clone_works() {
let (coo, _, _, _) = Samples::tiny_1x1(false);
let mut clone = coo.clone();
clone.values[0] *= 2.0;
assert_eq!(coo.values[0], 123.0);
assert_eq!(clone.values[0], 246.0);
}
}
10 changes: 10 additions & 0 deletions russell_sparse/src/csc_matrix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ extern "C" {
/// ```text
/// 0, 2, 5, 9, 10, 12
/// ```
#[derive(Clone)]
pub struct CscMatrix {
/// Defines the symmetry and storage: lower-triangular, upper-triangular, full-matrix
///
Expand Down Expand Up @@ -1207,4 +1208,13 @@ mod tests {
5 5 1.0\n"
);
}

#[test]
fn clone_works() {
let (_, csc, _, _) = Samples::tiny_1x1(false);
let mut clone = csc.clone();
clone.values[0] *= 2.0;
assert_eq!(csc.values[0], 123.0);
assert_eq!(clone.values[0], 246.0);
}
}
10 changes: 10 additions & 0 deletions russell_sparse/src/csr_matrix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ use std::path::Path;
/// ```text
/// 0, 3, 5, 8, 11, 13
/// ```
#[derive(Clone)]
pub struct CsrMatrix {
/// Defines the symmetry and storage: lower-triangular, upper-triangular, full-matrix
///
Expand Down Expand Up @@ -1275,4 +1276,13 @@ mod tests {
5 5 1.0\n"
);
}

#[test]
fn clone_works() {
let (_, _, csr, _) = Samples::tiny_1x1(false);
let mut clone = csr.clone();
clone.values[0] *= 2.0;
assert_eq!(csr.values[0], 123.0);
assert_eq!(clone.values[0], 246.0);
}
}
11 changes: 11 additions & 0 deletions russell_sparse/src/sparse_matrix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use std::ffi::OsStr;
/// 2. `(COO and CSC)` or `(COO and CSR)` pairs may be `Some` at the same time
/// 3. When getting data/information from the SparseMatrix, the default priority is `CSC -> CSR -> COO`
/// 4. If needed, the CSC or CSR are automatically computed from COO
#[derive(Clone)]
pub struct SparseMatrix {
coo: Option<CooMatrix>,
csc: Option<CscMatrix>,
Expand Down Expand Up @@ -630,4 +631,14 @@ mod tests {
5 5 1.0\n"
);
}

#[test]
fn clone_works() {
let (coo, _, _, _) = Samples::tiny_1x1(false);
let mat = SparseMatrix::from_coo(coo);
let mut clone = mat.clone();
clone.get_coo_mut().unwrap().values[0] *= 2.0;
assert_eq!(mat.get_coo().unwrap().values[0], 123.0);
assert_eq!(clone.get_coo().unwrap().values[0], 246.0);
}
}