# LinearOperator¶

class probnum.linops.LinearOperator(*args, **kwargs)

Bases: scipy.sparse.linalg.interface.LinearOperator

Composite base class for finite-dimensional linear operators.

This class provides a way to define finite-dimensional linear operators without explicitly constructing a matrix representation. Instead it suffices to define a matrix-vector product and a shape attribute. This avoids unnecessary memory usage and can often be more convenient to derive.

LinearOperator instances can be multiplied, added and exponentiated. This happens lazily: the result of these operations is a new, composite LinearOperator, that defers linear operations to the original operators and combines the results.

To construct a concrete class:LinearOperator, either pass appropriate callables to the constructor of this class, or subclass it.

A subclass must implement either one of the methods _matvec and _matmat, and the attributes/properties shape (pair of integers) and dtype (may be None). It may call the __init__ on this class to have these attributes validated. Implementing _matvec automatically implements _matmat (using a naive algorithm) and vice-versa.

Optionally, a subclass may implement _rmatvec or _adjoint to implement the Hermitian adjoint (conjugate transpose). As with _matvec and _matmat, implementing either _rmatvec or _adjoint implements the other automatically. Implementing _adjoint is preferable; _rmatvec is mostly there for backwards compatibility.

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

• matvec (callable f(v)) – Returns $$A v$$.

• rmatvec (callable f(v)) – Returns $$A^H v$$, where $$A^H$$ is the conjugate transpose of $$A$$.

• matmat (callable f(V)) – Returns $$AV$$, where $$V$$ is a dense matrix with dimensions (N, K).

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

• rmatmat (callable f(V)) – Returns $$A^H V$$, where $$V$$ is a dense matrix with dimensions (M, K).

aslinop

Transform into a LinearOperator.

Examples

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


Attributes Summary

 H Hermitian adjoint. T Transpose this linear operator. ndim

Methods Summary

 Call self as a function. Hermitian adjoint. cond([p]) Compute the condition number of the linear operator. Determinant of the linear operator. Matrix-matrix or matrix-vector multiplication. Eigenvalue spectrum of the linear operator. Inverse of the linear operator. Log absolute determinant of the linear operator. Matrix-matrix multiplication. Matrix-vector multiplication. Rank of the linear operator. Adjoint matrix-matrix multiplication. Adjoint matrix-vector multiplication. Dense matrix representation of the linear operator. Trace of the linear operator. Transpose this linear operator.

Attributes Documentation

H

Returns the Hermitian adjoint of self, aka the Hermitian conjugate or Hermitian transpose. For a complex matrix, the Hermitian adjoint is equal to the conjugate transpose.

Can be abbreviated self.H instead of self.adjoint().

Returns

A_H – Hermitian adjoint of self.

Return type

LinearOperator

T
ndim = 2

Methods Documentation

__call__(x)[source]

Call self as a function.

adjoint()[source]

Returns the Hermitian adjoint of self, aka the Hermitian conjugate or Hermitian transpose. For a complex matrix, the Hermitian adjoint is equal to the conjugate transpose.

Can be abbreviated self.H instead of self.adjoint().

Returns

A_H – Hermitian adjoint of self.

Return type

LinearOperator

cond(p=None)[source]

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

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

Return type

{float, inf}

det()[source]

Determinant of the linear operator.

dot(x)[source]

Matrix-matrix or matrix-vector multiplication.

Parameters

x (array_like) – 1-d or 2-d array, representing a vector or matrix.

Returns

Ax – 1-d or 2-d array (depending on the shape of x) that represents the result of applying this linear operator on x.

Return type

array

eigvals()[source]

Eigenvalue spectrum of the linear operator.

inv()[source]

Inverse of the linear operator.

Return type

LinearOperator

logabsdet()[source]

Log absolute determinant of the linear operator.

matmat(X)[source]

Matrix-matrix multiplication.

Performs the operation y=A*X where A is an MxN linear operator and X dense N*K matrix or ndarray.

Parameters

X ({matrix, ndarray}) – An array with shape (N,K).

Returns

Y – A matrix or ndarray with shape (M,K) depending on the type of the X argument.

Return type

{matrix, ndarray}

Notes

This matmat wraps any user-specified matmat routine or overridden _matmat method to ensure that y has the correct type.

matvec(x)[source]

Matrix-vector multiplication. Performs the operation y=A*x where A is an MxN linear operator and x is a 1-d array or random variable.

Parameters

x ({matrix, ndarray, RandomVariable}) – An array or RandomVariable with shape (N,) or (N,1).

Returns

y – A matrix or ndarray or RandomVariable with shape (M,) or (M,1) depending on the type and shape of the x argument.

Return type

{matrix, ndarray}

Notes

This matvec wraps the user-specified matvec routine or overridden _matvec method to ensure that y has the correct shape and type.

rank()[source]

Rank of the linear operator.

rmatmat(X)[source]

Performs the operation y = A^H * x where A is an MxN linear operator and x is a column vector or 1-d array, or 2-d array. The default implementation defers to the adjoint.

Parameters

X ({matrix, ndarray}) – A matrix or 2D array.

Returns

Y – A matrix or 2D array depending on the type of the input.

Return type

{matrix, ndarray}

Notes

This rmatmat wraps the user-specified rmatmat routine.

rmatvec(x)[source]

Performs the operation y = A^H * x where A is an MxN linear operator and x is a column vector or 1-d array.

Parameters

x ({matrix, ndarray}) – An array with shape (M,) or (M,1).

Returns

y – A matrix or ndarray with shape (N,) or (N,1) depending on the type and shape of the x argument.

Return type

{matrix, ndarray}

Notes

This rmatvec wraps the user-specified rmatvec routine or overridden _rmatvec method to ensure that y has the correct shape and type.

todense()[source]

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.

Returns

matrix – Matrix representation of the linear operator.

Return type

np.ndarray

trace()[source]

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 ValueError : If trace() is called on a non-square matrix.:

transpose()[source]

Transpose this linear operator.

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

Return type

LinearOperator