LinearOperator¶
- class probnum.linops.LinearOperator(shape, dtype, *, matmul, rmatmul=None, apply=None, todense=None, transpose=None, inverse=None, rank=None, eigvals=None, cond=None, det=None, logabsdet=None, trace=None)¶
Bases:
object
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, compositeLinearOperator
, that defers linear operations to the original operators and combines the results.To construct a concrete
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/propertiesshape
(pair of integers) anddtype
(may beNone
). 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 (
Union
[int
,Integral
,integer
,Iterable
[Union
[int
,Integral
,integer
]]]) – Matrix dimensions (M, N).dtype (
Union
[dtype
[Any
],None
,Type
[Any
],_SupportsDType
[dtype
[Any
]],str
,Tuple
[Any
,int
],Tuple
[Any
,Union
[SupportsIndex
,Sequence
[SupportsIndex
]]],List
[Any
],_DTypeDict
,Tuple
[Any
,Any
]]) – Data type of the operator.matmul (
Callable
[[ndarray
],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 ofnp.matmul()
must apply. Note that the argument to this callable is guaranteed to have at least two dimensions.rmatmul (
Optional
[Callable
[[ndarray
],ndarray
]]) – Callable which implements the matrix-matrix product, i.e. \(A @ V\), where \(A\) is the linear operator and \(V\) is a matrix of shape (N, K).todense (
Optional
[Callable
[[],ndarray
]]) – Callable which returns a dense matrix representation of the linear operator as anp.ndarray
. The output of this function must be equivalent to the output ofA.matmat(np.eye(N, dtype=A.dtype))
.rmatvec – Callable which implements the matrix-vector product with the adjoint of the operator, i.e. \(A^H v\), where \(A^H\) is the conjugate transpose of the linear operator \(A\) and \(v\) is a vector of shape (N,). This argument will be ignored if adjoint is given.
rmatmat – Returns \(A^H V\), where \(V\) is a dense matrix with dimensions (M, K).
See also
aslinop
Transform into a LinearOperator.
Examples
>>> import numpy as np >>> from probnum.linops import LinearOperator
>>> @LinearOperator.broadcast_matvec ... def mv(v): ... return np.array([2 * v[0] - v[1], 3 * v[1]])
>>> A = LinearOperator(shape=(2, 2), dtype=np.float_, matmul=mv) >>> A <LinearOperator with shape=(2, 2) and dtype=float64>
>>> A @ np.array([1., 2.]) array([0., 6.]) >>> A @ np.ones(2) array([1., 3.])
Attributes Summary
- rtype
Data type of the linear operator.
Whether input dimension matches output dimension.
Number of linear operator dimensions.
Shape of the linear operator.
- rtype
Methods Summary
__call__
(x[, axis])Call self as a function.
astype
(dtype[, order, casting, subok, copy])Cast a linear operator to a different
dtype
.broadcast_matmat
(matmat)Broadcasting for a (implicitly defined) matrix-matrix product.
broadcast_matvec
(matvec)Broadcasting for a (implicitly defined) matrix-vector product.
broadcast_rmatmat
(rmatmat)broadcast_rmatvec
(rmatvec)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.
Log absolute determinant of the linear operator.
rank
()Rank of the linear operator.
todense
([cache])Dense matrix representation of the linear operator.
trace
()Trace of the linear operator.
transpose
(*axes)Transpose this linear operator.
Attributes Documentation
- T¶
- Return type
- ndim¶
Number of linear operator dimensions.
Defined analogously to
numpy.ndarray.ndim
.- Return type
- shape¶
Shape of the linear operator.
Defined as a tuple of the output and input dimension of operator.
Methods Documentation
- astype(dtype, order='K', casting='unsafe', subok=True, copy=True)[source]¶
Cast a linear operator to a different
dtype
.- Parameters
dtype (
Union
[dtype
[Any
],None
,Type
[Any
],_SupportsDType
[dtype
[Any
]],str
,Tuple
[Any
,int
],Tuple
[Any
,Union
[SupportsIndex
,Sequence
[SupportsIndex
]]],List
[Any
],_DTypeDict
,Tuple
[Any
,Any
]]) – 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 ifdtype
is the same.
- Return type
- classmethod broadcast_matmat(matmat)[source]¶
Broadcasting for a (implicitly defined) matrix-matrix product.
Convenience function / decorator to broadcast the definition of a matrix-matrix product to vectors. This can be used to easily construct a new linear operator only from a matrix-matrix product.
- classmethod broadcast_matvec(matvec)[source]¶
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.
- 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
The condition number of the linear operator. May be infinite.
- Return type
cond
- todense(cache=True)[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}\).