LambdaLinearOperator

class probnum.linops.LambdaLinearOperator(shape, dtype, *, matmul, apply=None, solve=None, todense=None, transpose=None, inverse=None, rank=None, eigvals=None, cond=None, det=None, logabsdet=None, trace=None)

Bases: LinearOperator

Convenience subclass of LinearOperator that lets you pass implementations of its methods as parameters instead of overriding them in a subclass.

shape, dtype and matmul must be passed, the other parameters are optional.

Parameters
  • shape (ShapeLike) – Matrix dimensions (M, N).

  • dtype (DTypeLike) – Data type of the operator.

  • matmul (Callable[[np.ndarray], np.ndarray]) – Callable which computes the matrix-matrix product \(y = A V\), where \(A\) is the linear operator and \(V\) is an \(N \times K\) matrix. The callable must support broadcasted matrix products, i.e. the argument \(V\) might also be a stack of matrices in which case the broadcasting rules of np.matmul() must apply. Note that the argument to this callable is guaranteed to have at least two dimensions.

  • apply (Callable[[np.ndarray, int], np.ndarray]) – Callable which implements the application of the linear operator to an input array along a specified axis.

  • todense (Optional[Callable[[], np.ndarray]]) – Callable which returns a dense matrix representation of the linear operator as a np.ndarray. The output of this function must be equivalent to the output of A.matmat(np.eye(N, dtype=A.dtype)).

  • transpose (Optional[Callable[[], LinearOperator]]) – Callable which returns a LinearOperator that corresponds to the transpose of this linear operator.

  • inverse (Optional[Callable[[], LinearOperator]]) – Callable which returns a LinearOperator that corresponds to the inverse of this linear operator.

  • rank (Optional[Callable[[], np.intp]]) – Callable which returns the rank of this linear operator.

  • eigvals (Optional[Callable[[], np.ndarray]]) – Callable which returns the eigenvalues of this linear operator.

  • cond (Optional[Callable[[Optional[Union[None, int, str, np.floating]]], np.floating]]) – Callable which returns the condition number of this linear operator.

  • det (Optional[Callable[[], np.inexact]]) – Callable which returns the determinant of this linear operator.

  • logabsdet (Optional[Callable[[], np.floating]]) – Callable which returns the log absolute determinant of this linear operator.

  • trace (Optional[Callable[[], np.number]]) – Callable which returns the trace of this linear operator.

  • solve (Callable[[np.ndarray], np.ndarray]) –

Examples

>>> import numpy as np
>>> from probnum.linops import LambdaLinearOperator, LinearOperator
>>> @LinearOperator.broadcast_matvec
... def mv(v):
...     return np.array([2 * v[0] - v[1], 3 * v[1]])
>>> A = LambdaLinearOperator(shape=(2, 2), dtype=np.float_, matmul=mv)
>>> A
<LambdaLinearOperator with shape=(2, 2) and dtype=float64>
>>> A @ np.array([1., 2.])
array([0., 6.])
>>> A @ np.ones(2)
array([1., 3.])

Attributes Summary

T

Transpose of the linear operator.

dtype

Data type of the linear operator.

is_lower_triangular

Whether the LinearOperator represents a lower triangular matrix.

is_positive_definite

Whether the LinearOperator \(L \in \mathbb{R}^{n \times n}\) is (strictly) positive-definite, i.e. \(x^T L x > 0\) for \(x \in \mathbb{R}^n\).

is_square

Whether input dimension matches output dimension.

is_symmetric

Whether the LinearOperator \(L\) is symmetric, i.e. \(L = L^T\).

is_upper_triangular

Whether the LinearOperator represents an upper triangular matrix.

ndim

Number of linear operator dimensions.

shape

Shape of the linear operator.

size

Product of the shape entries.

Methods Summary

__call__(x[, axis])

Apply the linear operator to an input array along a specified axis.

astype(dtype[, order, casting, subok, copy])

Cast a linear operator to a different dtype.

broadcast_matmat([matmat, method])

Broadcasting for a (implicitly defined) matrix-matrix product.

broadcast_matvec([matvec, method])

Broadcasting for a (implicitly defined) matrix-vector product.

cholesky([lower])

Computes a Cholesky decomposition of the LinearOperator.

cond([p])

Compute the condition number of the linear operator.

det()

Determinant of the linear operator.

eigvals()

Eigenvalue spectrum of the linear operator.

inv()

Inverse of the linear operator.

logabsdet()

Log absolute determinant of the linear operator.

rank()

Rank of the linear operator.

solve(b)

Solves linear systems A @ x = b, where b is either a vector or a (stack of) matrices.

symmetrize()

Compute or approximate the closest symmetric LinearOperator in the Frobenius norm.

todense([cache])

Dense matrix representation of the linear operator.

trace()

Trace of the linear operator.

transpose(*axes)

Transpose this linear operator.

Attributes Documentation

T

Transpose of the linear operator.

dtype

Data type of the linear operator.

is_lower_triangular

Whether the LinearOperator represents a lower triangular matrix.

If this is None, it is unknown whether the matrix is lower triangular or not.

is_positive_definite

Whether the LinearOperator \(L \in \mathbb{R}^{n \times n}\) is (strictly) positive-definite, i.e. \(x^T L x > 0\) for \(x \in \mathbb{R}^n\).

If this is None, it is unknown whether the matrix is positive-definite or not. Only symmetric operators can be positive-definite.

Raises

ValueError – When setting is_positive_definite to True while is_symmetric is False.

is_square

Whether input dimension matches output dimension.

is_symmetric

Whether the LinearOperator \(L\) is symmetric, i.e. \(L = L^T\).

If this is None, it is unknown whether the operator is symmetric or not. Only square operators can be symmetric.

Raises

ValueError – When setting is_symmetric to True on a non-square LinearOperator.

is_upper_triangular

Whether the LinearOperator represents an upper triangular matrix.

If this is None, it is unknown whether the matrix is upper triangular or not.

ndim

Number of linear operator dimensions.

Defined analogously to numpy.ndarray.ndim.

shape

Shape of the linear operator.

Defined as a tuple of the output and input dimension of operator.

size

Product of the shape entries.

Defined analogously to numpy.ndarray.size.

Methods Documentation

__call__(x, axis=None)

Apply the linear operator to an input array along a specified axis.

Parameters
  • x (np.ndarray) – Input array.

  • axis (int) – Axis along which to apply the linear operator. Guaranteed to be positive and valid, i.e. axis is a valid index into the shape of x, and x has the correct shape along axis.

Returns

apply_result – Array resulting in the application of the linear operator to x along axis.

Return type

np.ndarray

Raises
astype(dtype, order='K', casting='unsafe', subok=True, copy=True)

Cast a linear operator to a different dtype.

Parameters
  • dtype (DTypeLike) – Data type to which the linear operator is cast.

  • order (str) – Memory layout order of the result.

  • casting (str) – Controls what kind of data casting may occur.

  • subok (bool) – If True, then sub-classes will be passed-through (default). False is currently not supported for linear operators.

  • copy (bool) – Whether to return a new linear operator, even if dtype is the same.

Raises
  • TypeError – If the linear operator can not be cast to the desired dtype according to the given casting rule.

  • NotImplementedError – If subok is set to True.

Return type

LinearOperator

static broadcast_matmat(matmat=None, method=False)

Broadcasting for a (implicitly defined) matrix-matrix product.

Convenience function / decorator to broadcast the definition of a matrix-matrix product to stacks of matrices. This can be used to easily construct a new linear operator only from a matrix-matrix product.

Parameters
  • matmat (Optional[Callable[[ndarray], ndarray]]) – Function computing a matrix-matrix product.

  • method (bool) – Whether the decorator is being applied to a method or a function.

Return type

Union[Callable[[ndarray], ndarray], Callable[[Callable[[ndarray], ndarray]], Callable[[ndarray], ndarray]]]

static broadcast_matvec(matvec=None, method=False)

Broadcasting for a (implicitly defined) matrix-vector product.

Convenience function / decorator to broadcast the definition of a matrix-vector product. This can be used to easily construct a new linear operator only from a matrix-vector product.

Parameters
  • matvec (Optional[Callable[[ndarray], ndarray]]) – Function computing a matrix-vector product.

  • method (bool) – Whether the decorator is being applied to a method or a function.

Return type

Union[Callable[[ndarray], ndarray], Callable[[Callable[[ndarray], ndarray]], Callable[[ndarray], ndarray]]]

cholesky(lower=True)

Computes a Cholesky decomposition of the LinearOperator.

The Cholesky decomposition of a symmetric positive-definite matrix \(A \in \mathbb{R}^{n \times n}\) is given by \(A = L L^T\), where the unique Cholesky factor \(L \in \mathbb{R}^{n \times n}\) of \(A\) is a lower triangular matrix with a positive diagonal.

As a side-effect, this method will set the value of is_positive_definite to True, if the computation of the Cholesky factorization succeeds. Otherwise, is_positive_definite will be set to False.

The result of this computation will be cached. If cholesky() is first called with lower=True first and afterwards with lower=False or vice-versa, the method simply returns the transpose of the cached value.

Parameters

lower (bool) – If this is set to False, this method computes and returns the upper triangular Cholesky factor \(U = L^T\), for which \(A = U^T U\). By default (True), the method computes the lower triangular Cholesky factor \(L\).

Returns

The lower or upper Cholesky factor of the LinearOperator, depending on the value of the parameter lower. The result will have its properties is_upper_triangular/is_lower_triangular set accordingly.

Return type

cholesky_factor

Raises
cond(p=None)

Compute the condition number of the linear operator.

The condition number of the linear operator with respect to the p norm. It measures how much the solution \(x\) of the linear system \(Ax=b\) changes with respect to small changes in \(b\).

Parameters

p ({None, 1, , 2, , inf, 'fro'}, optional) –

Order of the norm:

p

norm for matrices

None

2-norm, computed directly via singular value decomposition

’fro’

Frobenius norm

np.inf

max(sum(abs(x), axis=1))

1

max(sum(abs(x), axis=0))

2

2-norm (largest sing. value)

Returns

The condition number of the linear operator. May be infinite.

Return type

cond

Raises

numpy.linalg.LinAlgError – If cond() is called on a non-square matrix.

det()

Determinant of the linear operator.

Returns

The determinant of the linear operator.

Return type

det

Raises

numpy.linalg.LinAlgError – If det() is called on a non-square matrix.

eigvals()

Eigenvalue spectrum of the linear operator.

Raises

numpy.linalg.LinAlgError – If eigvals() is called on a non-square operator.

Return type

ndarray

inv()

Inverse of the linear operator.

Returns

inv – Inverse of this linear operator, which is again a LinearOperator.

Return type

LinearOperator

Raises

numpy.linalg.LinAlgError – If inv() is called on a non-square linear operator.

logabsdet()

Log absolute determinant of the linear operator.

Returns

The log absolute determinant of the linear operator.

Return type

logabsdet

Raises

numpy.linalg.LinAlgError – If logabsdet() is called on a non-square matrix.

rank()

Rank of the linear operator.

Return type

int64

solve(b)

Solves linear systems A @ x = b, where b is either a vector or a (stack of) matrices.

This method broadcasts like A.inv() @ b, but it might not produce the exact same result.

Parameters

b (ArrayLike) – The right-hand side(s) of the linear system(s). This can either be a vector or a (stack of) matrices.

Returns

The solution(s) of the linear system(s). Depending on the shape of b, x is either a vector or a (stack of) matrices.

Return type

x

Raises
symmetrize()

Compute or approximate the closest symmetric LinearOperator in the Frobenius norm.

The closest symmetric matrix to a given square matrix \(A\) in the Frobenius norm is given by

\[\operatorname{sym}(A) := \frac{1}{2} (A + A^T).\]

However, for efficiency reasons, it is preferrable to approximate this operator in some cases. For example, a Kronecker product \(K = A \otimes B\) is more efficiently symmetrized by means of

\begin{equation} \operatorname{sym}(A) \otimes \operatorname{sym}(B) = \operatorname{sym}(K) + \frac{1}{2} \left( \frac{1}{2} \left( A \otimes B^T + A^T \otimes B \right) - \operatorname{sym}(K) \right). \end{equation}
Returns

The closest symmetric LinearOperator in the Frobenius norm, or an approximation, which makes a reasonable trade-off between accuracy and efficiency (see above). The resulting LinearOperator will have its is_symmetric property set to True.

Return type

symmetrized_linop

Raises

numpy.linalg.LinAlgError – If this method is called on a non-square LinearOperator.

todense(cache=True)

Dense matrix representation of the linear operator.

This method can be computationally very costly depending on the shape of the linear operator. Use with caution.

Parameters

cache (bool) – If this is set to True, then the dense matrix representation will be cached and subsequent calls will return the cached value (even if dense is set to False in these subsequent calls).

Returns

matrix – Matrix representation of the linear operator.

Return type

np.ndarray

trace()

Trace of the linear operator.

Computes the trace of a square linear operator \(\text{tr}(A) = \sum_{i-1}^n A_{ii}\).

Returns

trace – Trace of the linear operator.

Return type

float

Raises

numpy.linalg.LinAlgError – If trace() is called on a non-square matrix.

transpose(*axes)

Transpose this linear operator.

Can be abbreviated self.T instead of self.transpose().

Parameters

*axes (Union[int, Tuple[int]]) – Permutation of the axes of the LinearOperator.

Raises
  • ValueError – If the given axis indices do not constitute a valid permutation of the axes.

  • numpy.AxisError – If the axis indices are out of bounds.

Return type

LinearOperator