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, Ellipsis]
Abstractmethod:

Return type:

tuple[int, Ellipsis]

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:

Return type:

numpy.ndarray

Numpy array representation of self.

tonumpy()

Numpy array representation of self.

Return type:

numpy.ndarray

__matmul__(other)

Compute scalar product with other vector or with numpy array.

Parameters:

other (AbstractVector | numpy.ndarray)

Return type:

numpy.ndarray | numpy.float64 | AbstractVector

abstractmethod apply(other)

Compute scalar product with other vector.

Parameters:

other (AbstractVector)

Return type:

numpy.ndarray | numpy.float64

abstractmethod dot(other)

Compute scalar product along some non-vector dimension.

Parameters:

other (numpy.ndarray)

Return type:

AbstractVector

abstractmethod __mul__(other)

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

Parameters:

other (numpy.ndarray | float)

Return type:

AbstractVector

abstractmethod __add__(other)

Add two vectors, applying broadcasting rules.

Parameters:

other (AbstractVector)

Return type:

AbstractVector

abstractmethod __sub__(other)

Subtract vector from self, applying broadcasting rules.

Parameters:

other (AbstractVector)

Return type:

AbstractVector

iszero()

Check if all elements are zero.

Return type:

bool

class dubfi.linalg.types.Vector(size, entry_shape=())

Bases: AbstractVector

Simple vector with explicit array representation.

Parameters:
  • size (int)

  • entry_shape (tuple[int, Ellipsis])

property shape: tuple[int, Ellipsis]

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

Return type:

tuple[int, Ellipsis]

property data

Numpy array representation of self.

classmethod fromdata(data, other=None)

Construct vector from numpy array.

Parameters:
Return type:

Self

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.

__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, Ellipsis]
Abstractmethod:

Return type:

tuple[int, Ellipsis]

Shape of array (matrix) representation of self.

property symmetric: bool

True if self equals its adjoint.

Return type:

bool

abstractmethod tonumpy()

Numpy array (matrix) representation of self.

Return type:

numpy.ndarray

abstractmethod solve(vec)

Solve linear equation self @ x = vec for x.

Parameters:

vec (AbstractVector)

Return type:

AbstractVector

inv()

Compute (multiplicative) inverse operator.

Return type:

AbstractOperator

abstractmethod diagonal()

Return diagonal of self as vector.

Return type:

AbstractVector

abstractmethod __mul__(other)

Multiply element-wise.

Parameters:

other (numpy.ndarray | float)

Return type:

AbstractOperator

__matmul__(other)

Act with operator on other operator or on vector.

Parameters:

other (AbstractVector | AbstractOperator | numpy.ndarray)

Return type:

AbstractVector | AbstractOperator | numpy.ndarray

abstractmethod trace()

Compute trace of self.

Return type:

numpy.ndarray | numpy.float64

trace_product(other)

Compute trace of (self @ other).

Parameters:

other (AbstractOperator)

Return type:

numpy.ndarray | numpy.float64

abstractmethod apply(vec)

Apply operator on vector: self @ vec.

Parameters:

vec (AbstractVector)

Return type:

AbstractVector

rapply(vec)

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

Parameters:

vec (AbstractVector)

Return type:

AbstractVector

sandwich(vec)

Compute vec @ self @ vec.

Parameters:

vec (AbstractVector)

Return type:

numpy.ndarray | numpy.float64

chain(other)

Combine operators: self @ other.

Parameters:

other (AbstractOperator)

Return type:

AbstractOperator

abstractmethod dot(other)

Inner product along non-vector dimensions.

Parameters:

other (numpy.ndarray)

Return type:

AbstractOperator

abstractmethod logdet()

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

Return type:

numpy.ndarray | numpy.float64

class dubfi.linalg.types.Operator(size, entry_shape=())

Bases: AbstractOperator

Abstract operator enriched with functions for computing the trace.

Parameters:
  • size (int)

  • entry_shape (tuple[int, Ellipsis])

property shape: tuple[int, Ellipsis]

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

Return type:

tuple[int, Ellipsis]

trace()

Compute trace(self).

trace_product_self(chunk=30)

Compute trace(self @ self).

trace_product(other)

Compute trace(self @ other).

Parameters:

other (AbstractOperator)

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

Bases: Operator

Generic representation of the product of multiple linear operators.

Parameters:

operators (AbstractOperator)

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)

Multiply element-wise in-place.

Return type:

Self

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, solver=None)

Bases: Operator

Generic implementation of the inverse of a linear operator.

Parameters:

inv (AbstractOperator)

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)

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

Parameters:

vec (numpy.ndarray)

Return type:

numpy.ndarray

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, solver)

Bases: InvOperator

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

Parameters:

inv (AbstractOperator)

apply_np(vec)

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

Parameters:

vec (numpy.ndarray)

Return type:

numpy.ndarray

apply(vec)

Apply operator on vector: self @ vec.