Struct russell_sparse::LinSolver

source ·
pub struct LinSolver<'a> {
    pub actual: Box<dyn Send + LinSolTrait + 'a>,
}
Expand description

Unifies the access to linear system solvers

Fields§

§actual: Box<dyn Send + LinSolTrait + 'a>

Holds the actual implementation

Implementations§

source§

impl<'a> LinSolver<'a>

source

pub fn new(genie: Genie) -> Result<Self, StrError>

Allocates a new instance

§Input
  • genie – the actual implementation that does all the magic
source

pub fn compute( genie: Genie, x: &mut Vector, mat: &mut SparseMatrix, rhs: &Vector, params: Option<LinSolParams> ) -> Result<Self, StrError>

Computes the solution of a linear system

Solves the linear system:

  A   · x = rhs
(m,n)  (n)  (m)
§Output
  • x – the vector of unknown values with dimension equal to mat.ncol
§Input
  • genie – the actual implementation that does all the magic
  • mat – the matrix representing the sparse coefficient matrix A (see Notes below)
  • rhs – the right-hand side vector with know values an dimension equal to coo.nrow
  • verbose – shows messages
§Notes
  1. For symmetric matrices, MUMPS requires that the symmetry/storage be Lower or Full.
  2. For symmetric matrices, UMFPACK requires that the symmetry/storage be Full.
  3. This function calls the actual implementation (genie) via the functions factorize, and solve.
  4. This function is best for a single-use, whereas the actual solver should be considered for a recurrent use (e.g., inside a loop).
§Examples
use russell_lab::{vec_approx_eq, Vector};
use russell_sparse::prelude::*;
use russell_sparse::StrError;

fn main() -> Result<(), StrError> {
    // constants
    let ndim = 3; // number of rows = number of columns
    let nnz = 5; // number of non-zero values

    // allocate the coefficient matrix
    let mut mat = SparseMatrix::new_coo(ndim, ndim, nnz, Sym::No)?;
    mat.put(0, 0, 0.2)?;
    mat.put(0, 1, 0.2)?;
    mat.put(1, 0, 0.5)?;
    mat.put(1, 1, -0.25)?;
    mat.put(2, 2, 0.25)?;

    // print matrix
    let mut a = mat.as_dense();
    let correct = "┌                   ┐\n\
                   │   0.2   0.2     0 │\n\
                   │   0.5 -0.25     0 │\n\
                   │     0     0  0.25 │\n\
                   └                   ┘";
    assert_eq!(format!("{}", a), correct);

    // allocate the right-hand side vector
    let rhs = Vector::from(&[1.0, 1.0, 1.0]);

    // calculate the solution
    let mut x = Vector::new(ndim);
    LinSolver::compute(Genie::Umfpack, &mut x, &mut mat, &rhs, None)?;
    let correct = vec![3.0, 2.0, 4.0];
    vec_approx_eq(&x, &correct, 1e-14);
    Ok(())
}

Auto Trait Implementations§

§

impl<'a> Freeze for LinSolver<'a>

§

impl<'a> !RefUnwindSafe for LinSolver<'a>

§

impl<'a> Send for LinSolver<'a>

§

impl<'a> !Sync for LinSolver<'a>

§

impl<'a> Unpin for LinSolver<'a>

§

impl<'a> !UnwindSafe for LinSolver<'a>

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.