Skip to content

Commit

Permalink
Rename SparseTriplet to CooMatrix
Browse files Browse the repository at this point in the history
  • Loading branch information
cpmech committed Sep 19, 2023
1 parent 222b923 commit 3815a4f
Show file tree
Hide file tree
Showing 11 changed files with 92 additions and 92 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,14 +122,14 @@ fn main() -> Result<(), StrError> {

```rust
use russell_lab::{Matrix, Vector, StrError};
use russell_sparse::{ConfigSolver, Solver, SparseTriplet};
use russell_sparse::{ConfigSolver, Solver, CooMatrix};

fn main() -> Result<(), StrError> {

// allocate a square matrix
let neq = 5; // number of equations
let nnz = 13; // number of non-zeros
let mut trip = SparseTriplet::new(neq, nnz)?;
let mut trip = CooMatrix::new(neq, nnz)?;
trip.put(0, 0, 1.0)?; // << (0, 0, a00/2)
trip.put(0, 0, 1.0)?; // << (0, 0, a00/2)
trip.put(1, 0, 3.0)?;
Expand Down
4 changes: 2 additions & 2 deletions russell_sparse/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,13 @@ export OPENBLAS_NUM_THREADS=1

```rust
use russell_lab::{Matrix, Vector};
use russell_sparse::{ConfigSolver, Solver, SparseTriplet, StrError};
use russell_sparse::{ConfigSolver, Solver, CooMatrix, StrError};

fn main() -> Result<(), StrError> {
// allocate a square matrix
let neq = 3; // number of equations
let nnz = 5; // number of non-zeros
let mut trip = SparseTriplet::new(neq, nnz)?;
let mut trip = CooMatrix::new(neq, nnz)?;
trip.put(0, 0, 0.2)?;
trip.put(0, 1, 0.2)?;
trip.put(1, 0, 0.5)?;
Expand Down
6 changes: 3 additions & 3 deletions russell_sparse/src/bin/mem_check_build.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use russell_lab::Vector;
use russell_sparse::{ConfigSolver, LinSolKind, Solver, SparseTriplet};
use russell_sparse::{ConfigSolver, CooMatrix, LinSolKind, Solver};

fn test_solver(name: LinSolKind) {
match name {
Expand All @@ -9,7 +9,7 @@ fn test_solver(name: LinSolKind) {

let (neq, nnz) = (5, 13);

let mut trip = match SparseTriplet::new(neq, nnz) {
let mut trip = match CooMatrix::new(neq, nnz) {
Ok(v) => v,
Err(e) => {
println!("FAIL(new triplet): {}", e);
Expand Down Expand Up @@ -81,7 +81,7 @@ fn test_solver_singular(name: LinSolKind) {

let (neq, nnz) = (2, 2);

let trip_singular = match SparseTriplet::new(neq, nnz) {
let trip_singular = match CooMatrix::new(neq, nnz) {
Ok(v) => v,
Err(e) => {
println!("FAIL(new triplet): {}", e);
Expand Down
4 changes: 2 additions & 2 deletions russell_sparse/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@
//!
//! ```
//! use russell_lab::{Matrix, Vector};
//! use russell_sparse::{ConfigSolver, Solver, SparseTriplet, StrError};
//! use russell_sparse::{ConfigSolver, Solver, CooMatrix, StrError};
//!
//! fn main() -> Result<(), StrError> {
//!
//! // allocate a square matrix
//! let (neq, nnz) = (5, 13);
//! let mut trip = SparseTriplet::new(neq, nnz)?;
//! let mut trip = CooMatrix::new(neq, nnz)?;
//! trip.put(0, 0, 1.0)?; // << (0, 0, a00/2)
//! trip.put(0, 0, 1.0)?; // << (0, 0, a00/2)
//! trip.put(1, 0, 3.0)?;
Expand Down
2 changes: 1 addition & 1 deletion russell_sparse/src/prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@

pub use crate::config_solver::ConfigSolver;
pub use crate::solver::Solver;
pub use crate::sparse_triplet::SparseTriplet;
pub use crate::sparse_triplet::CooMatrix;
14 changes: 7 additions & 7 deletions russell_sparse/src/read_matrix_market.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::SparseTriplet;
use super::CooMatrix;
use crate::StrError;
use std::ffi::OsStr;
use std::fs::File;
Expand Down Expand Up @@ -161,7 +161,7 @@ impl MatrixMarketData {
}
}

/// Reads a MatrixMarket file into a SparseTriplet
/// Reads a MatrixMarket file into a CooMatrix
///
/// **Note:** This function works only with square matrices.
///
Expand All @@ -186,12 +186,12 @@ impl MatrixMarketData {
///
/// If `sym_mirror` is true, the reader will set `nnz` (number of non-zero values)
/// with twice the specified `nnz` value because we cannot know how many entries
/// are on the diagonal until the whole file is read. Nonetheless, the `SparseTriplet`
/// can be used normally by the user, since this information is internal to `SparseTriplet`.
/// are on the diagonal until the whole file is read. Nonetheless, the `CooMatrix`
/// can be used normally by the user, since this information is internal to `CooMatrix`.
///
/// # Output
///
/// * A SparseTriplet or an error message
/// * A CooMatrix or an error message
/// * Returns true if the `symmetric` keyword is present in the header
///
/// # Panics
Expand Down Expand Up @@ -325,7 +325,7 @@ impl MatrixMarketData {
/// Ok(())
/// }
/// ```
pub fn read_matrix_market<P>(full_path: &P, sym_mirror: bool) -> Result<(SparseTriplet, bool), StrError>
pub fn read_matrix_market<P>(full_path: &P, sym_mirror: bool) -> Result<(CooMatrix, bool), StrError>
where
P: AsRef<OsStr> + ?Sized,
{
Expand Down Expand Up @@ -366,7 +366,7 @@ where
}

// allocate triplet
let mut trip = SparseTriplet::new(data.m as usize, max as usize)?;
let mut trip = CooMatrix::new(data.m as usize, max as usize)?;

// read and parse triples
loop {
Expand Down
36 changes: 18 additions & 18 deletions russell_sparse/src/solver.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::{
code_symmetry_mmp, code_symmetry_umf, str_enum_ordering, str_enum_scaling, str_mmp_ordering, str_mmp_scaling,
str_umf_ordering, str_umf_scaling, ConfigSolver, LinSolKind, SparseTriplet,
str_umf_ordering, str_umf_scaling, ConfigSolver, CooMatrix, LinSolKind,
};
use crate::{StrError, Symmetry};
use russell_lab::{format_nanoseconds, vec_copy, Stopwatch, Vector};
Expand Down Expand Up @@ -148,7 +148,7 @@ impl Solver {
}

/// Performs the factorization
pub fn factorize(&mut self, trip: &SparseTriplet) -> Result<(), StrError> {
pub fn factorize(&mut self, trip: &CooMatrix) -> Result<(), StrError> {
if trip.neq != self.neq {
return Err("cannot factorize because the triplet has incompatible number of equations");
}
Expand Down Expand Up @@ -200,12 +200,12 @@ impl Solver {
///
/// ```
/// use russell_lab::{Matrix, Vector};
/// use russell_sparse::{ConfigSolver, SparseTriplet, Solver, StrError};
/// use russell_sparse::{ConfigSolver, CooMatrix, Solver, StrError};
///
/// fn main() -> Result<(), StrError> {
/// // allocate a square matrix
/// let (neq, nnz) = (5, 13);
/// let mut trip = SparseTriplet::new(neq, nnz)?;
/// let mut trip = CooMatrix::new(neq, nnz)?;
/// trip.put(0, 0, 1.0)?; // << (0, 0, a00/2)
/// trip.put(0, 0, 1.0)?; // << (0, 0, a00/2)
/// trip.put(1, 0, 3.0)?;
Expand Down Expand Up @@ -309,12 +309,12 @@ impl Solver {
///
/// ```
/// use russell_lab::{Matrix, Vector};
/// use russell_sparse::{ConfigSolver, Solver, SparseTriplet, StrError};
/// use russell_sparse::{ConfigSolver, Solver, CooMatrix, StrError};
///
/// fn main() -> Result<(), StrError> {
/// // allocate a square matrix
/// let (neq, nnz) = (3, 5);
/// let mut trip = SparseTriplet::new(neq, nnz)?;
/// let mut trip = CooMatrix::new(neq, nnz)?;
/// trip.put(0, 0, 0.2)?;
/// trip.put(0, 1, 0.2)?;
/// trip.put(1, 0, 0.5)?;
Expand Down Expand Up @@ -357,7 +357,7 @@ impl Solver {
/// Ok(())
/// }
/// ```
pub fn compute(config: ConfigSolver, trip: &SparseTriplet, rhs: &Vector) -> Result<(Self, Vector), StrError> {
pub fn compute(config: ConfigSolver, trip: &CooMatrix, rhs: &Vector) -> Result<(Self, Vector), StrError> {
let mut solver = Solver::new(config, trip.neq, trip.pos, None)?;
let mut x = Vector::new(trip.neq());
solver.factorize(&trip)?;
Expand Down Expand Up @@ -526,7 +526,7 @@ impl fmt::Display for Solver {

#[cfg(test)]
mod tests {
use super::{ConfigSolver, LinSolKind, Solver, SparseTriplet};
use super::{ConfigSolver, CooMatrix, LinSolKind, Solver};
use russell_chk::vec_approx_eq;
use russell_lab::Vector;

Expand All @@ -543,7 +543,7 @@ mod tests {
fn factorize_fails_on_incompatible_triplet() {
let config = ConfigSolver::new();
let mut solver = Solver::new(config, 1, 1, None).unwrap();
let trip = SparseTriplet::new(2, 2).unwrap();
let trip = CooMatrix::new(2, 2).unwrap();
assert_eq!(
solver.factorize(&trip).err(),
Some("cannot factorize because the triplet has incompatible number of equations")
Expand All @@ -555,7 +555,7 @@ mod tests {
let config = ConfigSolver::new();
let (neq, nnz) = (2, 2);
let mut solver = Solver::new(config, neq, nnz, None).unwrap();
let mut trip = SparseTriplet::new(neq, nnz).unwrap();
let mut trip = CooMatrix::new(neq, nnz).unwrap();
trip.put(0, 0, 1.0).unwrap();
trip.put(1, 1, 0.0).unwrap();
assert_eq!(solver.factorize(&trip), Err("Error(1): Matrix is singular"));
Expand All @@ -566,7 +566,7 @@ mod tests {
let config = ConfigSolver::new();
let (neq, nnz) = (2, 2);
let mut solver = Solver::new(config, neq, nnz, None).unwrap();
let mut trip = SparseTriplet::new(neq, nnz).unwrap();
let mut trip = CooMatrix::new(neq, nnz).unwrap();
trip.put(0, 0, 1.0).unwrap();
trip.put(1, 1, 1.0).unwrap();
solver.factorize(&trip).unwrap();
Expand All @@ -578,7 +578,7 @@ mod tests {
let config = ConfigSolver::new();
let (neq, nnz) = (2, 2);
let mut solver = Solver::new(config, neq, nnz, None).unwrap();
let mut trip = SparseTriplet::new(neq, nnz).unwrap();
let mut trip = CooMatrix::new(neq, nnz).unwrap();
trip.put(0, 0, 1.0).unwrap();
trip.put(1, 1, 1.0).unwrap();
let mut x = Vector::new(neq);
Expand All @@ -594,7 +594,7 @@ mod tests {
let config = ConfigSolver::new();
let (neq, nnz) = (2, 2);
let mut solver = Solver::new(config, neq, nnz, None).unwrap();
let mut trip = SparseTriplet::new(neq, nnz).unwrap();
let mut trip = CooMatrix::new(neq, nnz).unwrap();
trip.put(0, 0, 1.0).unwrap();
trip.put(1, 1, 1.0).unwrap();
solver.factorize(&trip).unwrap();
Expand All @@ -619,7 +619,7 @@ mod tests {
let mut solver = Solver::new(config, neq, nnz, None).unwrap();

// allocate a square matrix
let mut trip = SparseTriplet::new(neq, nnz).unwrap();
let mut trip = CooMatrix::new(neq, nnz).unwrap();
trip.put(0, 0, 1.0).unwrap(); // << (0, 0, a00/2)
trip.put(0, 0, 1.0).unwrap(); // << (0, 0, a00/2)
trip.put(1, 0, 3.0).unwrap();
Expand Down Expand Up @@ -659,15 +659,15 @@ mod tests {
let mut solver = Solver::new(config, neq, nnz, None).unwrap();

// factorize fails on incompatible triplet
let mut trip_wrong = SparseTriplet::new(1, 1).unwrap();
let mut trip_wrong = CooMatrix::new(1, 1).unwrap();
trip_wrong.put(0, 0, 1.0).unwrap();
assert_eq!(
solver.factorize(&trip_wrong).err(),
Some("cannot factorize because the triplet has incompatible number of equations")
);

// allocate a square matrix
let mut trip = SparseTriplet::new(neq, nnz).unwrap();
let mut trip = CooMatrix::new(neq, nnz).unwrap();
trip.put(0, 0, 1.0).unwrap(); // << (0, 0, a00/2)
trip.put(0, 0, 1.0).unwrap(); // << (0, 0, a00/2)
trip.put(1, 0, 3.0).unwrap();
Expand Down Expand Up @@ -721,7 +721,7 @@ mod tests {
vec_approx_eq(x_again.as_data(), x_correct, 1e-14);

// factorize fails on singular matrix
let mut trip_singular = SparseTriplet::new(5, 2).unwrap();
let mut trip_singular = CooMatrix::new(5, 2).unwrap();
trip_singular.put(0, 0, 1.0).unwrap();
trip_singular.put(4, 4, 1.0).unwrap();
let mut solver = Solver::new(config, 5, 2, None).unwrap();
Expand All @@ -734,7 +734,7 @@ mod tests {
#[test]
fn compute_works() {
let (neq, nnz) = (3, 6);
let mut trip = SparseTriplet::new(neq, nnz).unwrap();
let mut trip = CooMatrix::new(neq, nnz).unwrap();
trip.put(0, 0, 1.0).unwrap();
trip.put(0, 1, 1.0).unwrap();
trip.put(1, 0, 2.0).unwrap();
Expand Down
Loading

0 comments on commit 3815a4f

Please sign in to comment.