dubfi.linalg.types

Abstract types for linear algebra.

This module defines abstract Vector and Operator types for basic linear algebra operations as required for data assimilation or inverse problems.

Added in version 0.1.0: (initial release)

Classes

AbstractVector

Abstract element of a finite-dimensional vector space over real numbers.

Vector

Simple vector with explicit array representation.

AbstractOperator

Abstract linear operator acting on vectors.

Operator

Abstract operator enriched with functions for computing the trace.

OperatorChain

Generic representation of the product of multiple linear operators.

InvOperator

Generic implementation of the inverse of a linear operator.

InvOperatorNp

Inverse of a linear operator, constructed from a solver acting on numpy arrays.

Module Contents

class dubfi.linalg.types.AbstractVector

Bases: abc.ABC

Abstract element of a finite-dimensional vector space over real numbers.

property shape: tuple[int, ...]
Abstractmethod:

Shape of array representation of self, last dimension is the vector dimension.

property ndim

Number of dimensions, of which one is the vector dimension.

property data: numpy.ndarray
Abstractmethod:

Numpy array representation of self.

tonumpy() numpy.ndarray

Numpy array representation of self.

__matmul__(other: AbstractVector | numpy.ndarray) numpy.ndarray | numpy.float64 | AbstractVector

Compute scalar product with other vector or with numpy array.

abstractmethod apply(other: AbstractVector) numpy.ndarray | numpy.float64

Compute scalar product with other vector.

abstractmethod dot(other: numpy.ndarray) AbstractVector

Compute scalar product along some non-vector dimension.

abstractmethod __mul__(other: numpy.ndarray | float) AbstractVector

Multiply by scalar or numpy array treated as scalar along vector space dimension.

abstractmethod __add__(other: AbstractVector) AbstractVector

Add two vectors, applying broadcasting rules.

abstractmethod __sub__(other: AbstractVector) AbstractVector

Subtract vector from self, applying broadcasting rules.

iszero() bool

Check if all elements are zero.

class dubfi.linalg.types.Vector(size: int, entry_shape: tuple[int, ...] = ())

Bases: AbstractVector

Simple vector with explicit array representation.

property shape: tuple[int, ...]

Shape of array representation of self, with vector space along last dimension.

property data

Numpy array representation of self.

classmethod fromdata(data: numpy.ndarray, other: Vector | Operator | None = None) Self

Construct vector from numpy array.

dot(other)

Compute scalar product along some non-vector dimension.

__mul__(other)

Multiply by scalar or numpy array treated as scalar along vector space dimension.

__rmul__(other)

Multiply by scalar or numpy array treated as scalar along vector space dimension.

__add__(other)

Add two vectors, applying broadcasting rules.

__iadd__(other)

Add vector in-place.

__sub__(other)

Add two vectors, applying broadcasting rules.

__isub__(other)

Subtract vector in-place.

apply(other)

Compute scalar product with other vector.

class dubfi.linalg.types.AbstractOperator

Bases: abc.ABC

Abstract linear operator acting on vectors.

property shape: tuple[int, ...]
Abstractmethod:

Shape of array (matrix) representation of self.

property symmetric: bool

True if self equals its adjoint.

abstractmethod tonumpy() numpy.ndarray

Numpy array (matrix) representation of self.

abstractmethod solve(vec: AbstractVector) AbstractVector

Solve linear equation self @ x = vec for x.

inv() AbstractOperator

Compute (multiplicative) inverse operator.

abstractmethod diagonal() AbstractVector

Return diagonal of self as vector.

abstractmethod __mul__(other: numpy.ndarray | float) AbstractOperator

Multiply element-wise.

__matmul__(other: AbstractVector | AbstractOperator | numpy.ndarray) AbstractVector | AbstractOperator | numpy.ndarray

Act with operator on other operator or on vector.

abstractmethod trace() numpy.ndarray | numpy.float64

Compute trace of self.

trace_product(other: AbstractOperator) numpy.ndarray | numpy.float64

Compute trace of (self @ other).

abstractmethod apply(vec: AbstractVector) AbstractVector

Apply operator on vector: self @ vec.

rapply(vec: AbstractVector) AbstractVector

Apply transpose operator on vector: self.T @ vec.

sandwich(vec: AbstractVector) numpy.ndarray | numpy.float64

Compute vec @ self @ vec.

chain(other: AbstractOperator) AbstractOperator

Combine operators: self @ other.

abstractmethod dot(other: numpy.ndarray) AbstractOperator

Inner product along non-vector dimensions.

abstractmethod logdet() numpy.ndarray | numpy.float64

Compute log(det(self)) assuming that self is a positive definite, real-symmetric matrix.

validate_uncertainty_statistics(vec: AbstractVector) tuple[numpy.ndarray, numpy.ndarray]

Indicate whether vec is a likely realization of a Gaussian random variable with variance self.

Assume that self is the error covariance matrix of a Gaussian random variable X with mean 0. Provide indications whether X is a likely realization of X in the following form:

Diagonalize self: self = V @ diag(D) @ V.T where D is the vector of eigenvalues of self and V is an orthogonal matrix. Return (V.T @ vec, D). One can expect that (V.T @ vec) / D is a Gaussian random variable with mean 0 and standard deviation 1.

Added in version 0.1.3.

class dubfi.linalg.types.Operator(size: int, entry_shape: tuple[int, ...] = ())

Bases: AbstractOperator

Abstract operator enriched with functions for computing the trace.

property shape: tuple[int, ...]

Shape of array representation of self, with vector space along last two dimensions.

trace()

Compute trace(self).

trace_product_self(chunk: int = 30)

Compute trace(self @ self).

trace_product(other: AbstractOperator)

Compute trace(self @ other).

class dubfi.linalg.types.OperatorChain(*operators: AbstractOperator)

Bases: Operator

Generic representation of the product of multiple linear operators.

apply(vec)

Apply operator on vector: self @ vec.

rapply(vec)

Apply transpose operator on vector: self.T @ vec.

tonumpy()

Numpy array (matrix) representation of self.

__mul__(other)

Multiply element-wise.

__rmul__(other)

Multiply element-wise.

__imul__(other) Self

Multiply element-wise in-place.

inv()

Compute (multiplicative) inverse operator.

solve(vec)

Solve linear equation self @ x = vec for x.

chain(other)

Combine operators: self @ other.

logdet()

Compute log(det(self)) assuming that self is a positive definite, real-symmetric matrix.

class dubfi.linalg.types.InvOperator(inv: AbstractOperator, solver=None)

Bases: Operator

Generic implementation of the inverse of a linear operator.

property shape

Shape of array (matrix) representation of self.

property symmetric

True if self equals its adjoint.

abstractmethod __mul__(other)

Multiply element-wise.

inv()

Compute (multiplicative) inverse operator.

tonumpy()

Numpy array (matrix) representation of self.

solve(vec)

Solve linear equation self @ x = vec for x.

apply_np(vec: numpy.ndarray) numpy.ndarray

Apply to numpy array representation of vector, return numpy representation of result.

apply(vec)

Apply operator on vector: self @ vec.

logdet()

Compute log(det(self)) assuming that self is a positive definite, real-symmetric matrix.

rapply(vec)

Apply transpose operator on vector: self.T @ vec.

class dubfi.linalg.types.InvOperatorNp(inv: AbstractOperator, solver)

Bases: InvOperator

Inverse of a linear operator, constructed from a solver acting on numpy arrays.

apply_np(vec: numpy.ndarray) numpy.ndarray

Apply to numpy array representation of vector, return numpy representation of result.

apply(vec)

Apply operator on vector: self @ vec.