Skip to main content

LinearOperator

Trait LinearOperator 

Source
pub trait LinearOperator {
    // Required methods
    fn n_rows(&self) -> usize;
    fn n_cols(&self) -> usize;
    fn apply(&self, x: &[f64], y: &mut [f64]);
    fn diagonal(&self, d: &mut [f64]);

    // Provided method
    fn residual(&self, x: &[f64], b: &[f64], r: &mut [f64]) { ... }
}
Expand description

A square linear map A that can be applied to a vector.

This is the single contract the discretisation-agnostic Krylov solvers in this module need. Implement it for any sparse (or dense, or matrix-free) representation and cg_op / bicgstab_op / gmres_op work on it unchanged.

§Contract

  • n_rows and n_cols must be equal and constant for the lifetime of the operator: these solvers are square-system solvers.
  • apply must be linear and deterministic: the same x must give bitwise the same y, and A(u + v) = Au + Av to rounding. A Krylov method that is handed a non-linear or noisy operator does not merely converge slowly, it converges to the wrong answer.
  • diagonal must write A[i][i] into d[i].

§Units

Dimensionless. x and y are bare f64 slices in whatever units the assembling layer chose; this contract neither knows nor enforces them.

§Implemented by

LduMatrix in this crate (face-addressed, finite-volume), and farrer_park::sparse::CsrMatrix (row-compressed, finite-element).

Required Methods§

Source

fn n_rows(&self) -> usize

Number of rows of A (dimensionless count). Equals the length of y in apply.

Source

fn n_cols(&self) -> usize

Number of columns of A (dimensionless count). Equals the length of x in apply. Must equal n_rows.

Source

fn apply(&self, x: &[f64], y: &mut [f64])

Write the matrix-vector product y = A x.

x has length n_cols, y has length n_rows and is fully overwritten (implementations must not accumulate into it).

Source

fn diagonal(&self, d: &mut [f64])

Write the main diagonal A[i][i] into d, which has length n_rows.

Used to build a Jacobi preconditioner without exposing the storage format. A structurally-zero diagonal entry must be written as 0.0; JacobiOperatorPreconditioner is the layer that decides what to do about it.

Provided Methods§

Source

fn residual(&self, x: &[f64], b: &[f64], r: &mut [f64])

Write the residual r = b - A x.

Provided so an implementation with a fused kernel can override it; the default applies the operator and subtracts. x, b and r all have length n. r is fully overwritten.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§