Skip to content

Commit

Permalink
Merge pull request #73 from cpmech/make-sparse-matrix-clonable
Browse files Browse the repository at this point in the history
Impl clone for COO, CSC, CSR, and SparseMatrix
  • Loading branch information
cpmech authored Jan 12, 2024
2 parents c3e7d1a + 39608cb commit 9cdd778
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 0 deletions.
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);
}
}

0 comments on commit 9cdd778

Please sign in to comment.