LinearOperator¶
-
class
probnum.linalg.linops.
LinearOperator
(dtype, shape)¶ Bases:
scipy.sparse.linalg.interface.LinearOperator
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 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.This class inherits from
scipy.sparse.linalg.LinearOperator
.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).
See also
aslinop
- Transform into a LinearOperator.
Examples
>>> import numpy as np >>> from probnum.linalg.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__
(x)Call self as a function. adjoint
()Hermitian adjoint. cond
([p])Compute the condition number of the linear operator. det
()Determinant of the linear operator. dot
(x)Matrix-matrix or matrix-vector multiplication. eigvals
()Eigenvalue spectrum of the linear operator. inv
()Inverse of the linear operator. logabsdet
()Log absolute determinant of the linear operator. matmat
(X)Matrix-matrix multiplication. matvec
(x)Matrix-vector multiplication. rank
()Rank of the linear operator. rmatmat
(X)Adjoint matrix-matrix multiplication. rmatvec
(x)Adjoint matrix-vector multiplication. todense
()Dense matrix representation of the linear operator. trace
()Trace of the linear operator. transpose
()Transpose this linear operator. Attributes Documentation
-
H
¶ Hermitian adjoint.
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
¶ Transpose this linear operator.
Can be abbreviated self.T instead of self.transpose().
-
ndim
= 2¶
Methods Documentation
-
adjoint
()[source]¶ Hermitian adjoint.
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}
-
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
-
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.
-
rmatmat
(X)[source]¶ Adjoint matrix-matrix multiplication.
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]¶ Adjoint matrix-vector multiplication.
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