Skip to main content

diffsol_la/
linear_op.rs

1use crate::{Context, IndexType, Matrix, Scalar, Vector};
2
3/// A linear operator `A` for use with the [crate::LinearSolver] trait.
4///
5/// This is a minimal, time-unaware description of a linear operator: it can
6/// report its shape and context, materialise itself as a matrix, and optionally
7/// provide a sparsity pattern. It is deliberately decoupled from the richer,
8/// time-aware operator traits in the `diffsol` crate.
9///
10/// A solver for `Ax = b` uses [Self::sparsity] to set up the matrix storage and
11/// [Self::matrix_inplace] to populate the matrix `A` prior to factorisation.
12pub trait LinearOp {
13    type T: Scalar;
14    type V: Vector<T = Self::T, C = Self::C>;
15    type M: Matrix<T = Self::T, V = Self::V, C = Self::C>;
16    type C: Context;
17
18    /// Number of rows of the operator (i.e. length of the output vector `y`).
19    fn nrows(&self) -> IndexType;
20
21    /// Number of columns of the operator (i.e. length of the input vector `x`).
22    fn ncols(&self) -> IndexType;
23
24    /// The context associated with this operator.
25    fn context(&self) -> &Self::C;
26
27    /// Compute the matrix representation of the operator `A` and store it in `y`.
28    ///
29    /// `y` is assumed to have been initialised with the sparsity pattern
30    /// returned by [Self::sparsity].
31    fn matrix_inplace(&self, y: &mut Self::M);
32
33    /// The sparsity pattern of the operator's matrix, or `None` if dense.
34    fn sparsity(&self) -> Option<<Self::M as Matrix>::Sparsity> {
35        None
36    }
37}