pub mod jacobi;
pub mod biconjugate_gradient;
pub mod conjugate_gradient;
pub mod gauss_seidel;
pub mod relaxation;
#[cfg(feature = "amg")]
pub mod amg;
pub use biconjugate_gradient::solve as solve_biconjugate_gradient;
#[cfg(feature = "amg")]
pub use amg::{Amg, solve_amg, solve_amg_with_initial_guess};
pub use nalgebra_sparse::{CscMatrix, CsrMatrix};
pub(crate) use nalgebra_sparse::na::{DVector, SimdRealField};
pub(crate) use rayon::prelude::*;
pub(crate) use crate::preconditioners::{IdentityPreconditioner, Preconditioner};
pub trait SpMatVecMul<T: SimdRealField + Copy> {
fn nrows(&self) -> usize;
fn mul_vec(&self, v: &DVector<T>) -> DVector<T>;
fn inv_diagonal(&self) -> Self;
}
impl<T: SimdRealField + Copy> SpMatVecMul<T> for CsrMatrix<T> {
#[inline] fn nrows(&self) -> usize { self.nrows() }
#[inline] fn mul_vec(&self, v: &DVector<T>) -> DVector<T> { self * v }
#[inline] fn inv_diagonal(&self) -> Self {
let mut a =self.diagonal_as_csr();
a.values_mut().iter_mut().for_each(|x| *x = T::one() / *x);
a
}
}
impl<T: SimdRealField + Copy> SpMatVecMul<T> for CscMatrix<T> {
#[inline] fn nrows(&self) -> usize { self.nrows() }
#[inline] fn mul_vec(&self, v: &DVector<T>) -> DVector<T> { self * v }
#[inline] fn inv_diagonal(&self) -> Self {
let mut a =self.diagonal_as_csc();
a.values_mut().iter_mut().for_each(|x| *x = T::one() / *x);
a
}
}
pub trait IterativeSolver<M, V, T> {
fn init(&mut self, a: &M, b: &V, x0: Option<&V>);
fn step(&mut self, a: &M, b: &V) -> bool;
fn reset(&mut self);
fn hard_reset(&mut self);
fn soft_reset(&mut self);
fn solution(&self) -> &V;
fn iterations(&self) -> usize;
fn solve_iterations(&mut self, a: &M, b: &V, max_iter: usize) -> bool {
for _ in 0..max_iter {
if self.step(a, b) {
return true;
}
}
self.step(a, b)
}
}