dubfi.linalg.generic

Generic linear algebra objects building upon types modules.

Added in version 0.1.0: (initial release)

Classes

ParametrizedVector

AbstractVector depending on array of parameters.

ParametrizedOperator

AbstractOperator depending on array of parameters.

DiagonalOperator

Generic (scalar times) identity operator.

IdentityOperator

Generic (scalar times) identity operator.

Functions

check_derivatives(f, f_grad, f_hess, x[, eps, tol, count])

Test derivatives (gradient and Hesse matrix) of function.

check_derivatives_np(f, f_grad, f_hess, x[, eps, tol])

Test derivatives for functions returning only numpy arrays.

Module Contents

class dubfi.linalg.generic.ParametrizedVector

Bases: abc.ABC

AbstractVector depending on array of parameters.

abstractmethod __call__(s)

Vector at given parameter array.

Parameters:

s (numpy.ndarray)

Return type:

dubfi.linalg.types.AbstractVector

abstractmethod grad(s)

Gradient w.r.t. parameters s.

Parameters:

s (numpy.ndarray)

Return type:

dubfi.linalg.types.AbstractVector

abstractmethod hess(s)

Hesse matrix w.r.t. parameters s.

Parameters:

s (numpy.ndarray)

Return type:

dubfi.linalg.types.AbstractVector

check_derivatives(s, **kwargs)

Test derivatives grad() and hess().

Parameters:
  • s (numpy.ndarray)

  • kwargs (Any)

Return type:

int

class dubfi.linalg.generic.ParametrizedOperator

Bases: abc.ABC

AbstractOperator depending on array of parameters.

abstractmethod __call__(s)

Operator at given parameter array.

Parameters:

s (numpy.ndarray)

Return type:

dubfi.linalg.types.AbstractOperator

abstractmethod grad(s)

Gradient w.r.t. parameters s.

Parameters:

s (numpy.ndarray)

Return type:

dubfi.linalg.types.AbstractOperator

grad_vec(s, vec)

Equivalent to self.grad(s) @ vec.

Parameters:
Return type:

dubfi.linalg.types.AbstractVector

vec_grad_vec(s, left, right)

Equivalent to left @ (self.grad(s) @ right).

Parameters:
Return type:

numpy.ndarray

abstractmethod hess(s)

Hesse matrix w.r.t. parameters s.

Parameters:

s (numpy.ndarray)

Return type:

dubfi.linalg.types.AbstractOperator

hess_vec(s, vec)

Equivalent to self.hess(s) @ vec.

Parameters:
Return type:

dubfi.linalg.types.AbstractVector

vec_hess_vec(s, left, right)

Equivalent to left @ (self.hess(s) @ right).

Parameters:
Return type:

numpy.ndarray | numpy.float64

check_derivatives(s, **kwargs)

Test derivatives grad() and hess().

Parameters:
  • s (numpy.ndarray)

  • kwargs (Any)

Return type:

int

check_derivatives_applied(s, vec, **kwargs)

Test derivatives grad() and hess(), applying results on vec.

Parameters:
Return type:

int

check_derivatives_sandwich(s, vec, **kwargs)

Test derivatives grad() and hess(), with results sandwiched by vec.

Parameters:
Return type:

int

check_derivatives_trace(s, **kwargs)

Test derivatives grad() and hess(), with trace applied to results.

Parameters:
  • s (numpy.ndarray)

  • kwargs (Any)

Return type:

int

class dubfi.linalg.generic.DiagonalOperator(diagonal)

Bases: dubfi.linalg.types.AbstractOperator

Generic (scalar times) identity operator.

Linear operator represented by a diagonal matrix.

Parameters:

diagonal (np.ndarray) – last dimension represents the vector space dimension along which this is the diagonal of a linear operator

diagonal()

Return copy of the diagonal of self as vector.

property shape

Shape of array (matrix) representation of self.

apply(vec)

Apply operator from left.

rapply(vec)

Apply operator from right.

chain(other)

Combine operators: self @ other, may return other or a copy.

tonumpy()

Numpy array (matrix) representation of self.

solve(vec)

Solve self @ x = vec for x.

inv()

Compute (multiplicative) inverse.

__mul__(other)

Multiply element-wise.

__add__(other)

Add other operator.

__sub__(other)

Add other operator.

__iadd__(other)

Add other operator.

__isub__(other)

Add other operator.

trace()

Compute trace of self.

trace_product(other)

Compute trace of (self @ other).

logdet()

Compute log(det(self)), assuming that all entries are positive.

dot(other)

Inner product along non-vector dimensions.

class dubfi.linalg.generic.IdentityOperator(size, prefactor=None)

Bases: DiagonalOperator

Generic (scalar times) identity operator.

Linear operator represented by a diagonal matrix.

Parameters:
  • diagonal (np.ndarray) – last dimension represents the vector space dimension along which this is the diagonal of a linear operator

  • size (int)

property shape

Shape of array (matrix) representation of self.

apply(vec)

Apply identity operator, may return vec or a copy.

rapply(vec)

Apply identity operator, may return the vector itself or a copy.

tonumpy()

Numpy array (matrix) representation of self.

solve(vec)

Solve self @ x = vec for x, may return vec or a copy.

inv()

Compute (multiplicative) inverse, again an identity operator.

__mul__(other)

Multiply element-wise.

chain(other)

Combine operators: self @ other, may return other or a copy.

trace()

Compute trace of self.

trace_product(other)

Compute trace of (self @ other).

logdet()

Compute log(det(self)) assuming that all prefactors are positive.

dubfi.linalg.generic.check_derivatives(f, f_grad, f_hess, x, eps=1e-05, tol=20, count=10)

Test derivatives (gradient and Hesse matrix) of function.

This function prints statistics and warning messages if the numerical computed derivatives deviate significantly from the provided gradient and/or Hessian functions.

Parameters:
  • f (callable) – function mapping a 1d array to a scalar or vector.

  • f_grad (callable) – gradient of f (maps a 1d array to a 1d array or vector of 1d arrays)

  • f_hess (callable) – Hesse matrix of f (maps a 1d array to a 2d array or vector of 2d arrays)

  • x (np.ndarray) – parameter array at which the derivatives should be checked

  • eps (float) – scale of variations of x to compute derivatives numerically

  • tol (float) – tolerance scale relative to expected orders of eps.

  • count (int) – number of directions in which derivatives should be checked.

Returns:

errors – number of errors

Return type:

int

dubfi.linalg.generic.check_derivatives_np(f, f_grad, f_hess, x, eps=1e-05, tol=20)

Test derivatives for functions returning only numpy arrays.