LinearOperator#
- class scipy.sparse.linalg.LinearOperator(*args, **kwargs)[source]#
Common interface for performing matrix vector products.
Many iterative methods (e.g.
cg,gmres) do not need to know the individual entries of a matrix to solve a linear systemA@x = b. Such solvers only require the computation of matrix vector products,A@v, wherevis a dense vector. This class serves as an abstract interface between iterative solvers and matrix-like objects.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
_matvecand_matmat, and the attributes/propertiesshape(pair of integers, optionally with additional batch dimensions at the front) anddtype(may be None). It may call the__init__on this class to have these attributes validated. Implementing_matvecautomatically implements_matmat(using a naive algorithm) and vice-versa.Optionally, a subclass may implement
_rmatvecor_adjointto implement the Hermitian adjoint (conjugate transpose). As with_matvecand_matmat, implementing either_rmatvecor_adjointimplements the other automatically. Implementing_adjointis preferable;_rmatvecis mostly there for backwards compatibility.The defined operator may have additional “batch” dimensions prepended to the core shape, to represent a batch of 2-D operators; see Batched Linear Operations for details.
- Parameters:
- shapetuple
Matrix dimensions
(..., M, N), where...represents any additional batch dimensions.- matveccallable f(v)
Applies
Atov, wherevis a dense vector with shape(..., N).- rmatveccallable f(v)
Applies
A^Htov, whereA^His the conjugate transpose ofA, andvis a dense vector of shape(..., M).- matmatcallable f(V)
Returns
A @ V, whereVis a dense matrix with dimensions(..., N, K).- rmatmatcallable f(V)
Returns
A^H @ V, whereA^His the conjugate transpose ofA, and whereVis a dense matrix with dimensions(..., M, K).- dtypedtype
Data type of the matrix or matrices.
- Attributes:
Methods
matvec(x)Matrix-vector multiplication.
matmat(X)Matrix-matrix multiplication.
adjoint()Hermitian adjoint.
Transpose.
rmatvec(x)Adjoint matrix-vector multiplication.
rmatmat(X)Adjoint matrix-matrix multiplication.
dot(x)Multi-purpose multiplication method.
rdot(x)Multi-purpose multiplication method from the right.
__mul__(x)Multiplication.
__matmul__(other)Matrix Multiplication.
__call__(x)Apply this linear operator.
__add__(x)Linear operator addition.
__truediv__(other)Scalar Division.
__rmul__(x)Multiplication from the right.
__rmatmul__(other)Matrix Multiplication from the right.
See also
aslinearoperatorConstruct a
LinearOperator.
Notes
The user-defined
matvecfunction must properly handle the case wherevhas shape(..., N).It is highly recommended to explicitly specify the dtype, otherwise it is determined automatically at the cost of a single matvec application on
int8zero vector using the promoted dtype of the output. It is assumed thatmatmat,rmatvec, andrmatmatwould result in the same dtype of the output given anint8input asmatvec.LinearOperatorinstances can also be multiplied, added with each other, and raised to integral powers, all lazily: the result of these operations is always a new, compositeLinearOperator, that defers linear operations to the original operators and combines the results.More details regarding how to subclass a
LinearOperatorand several examples of concreteLinearOperatorinstances can be found in the external project PyLops.Array API Standard Support
LinearOperatorhas experimental support for Python Array API Standard compatible backends in addition to NumPy. Please consider testing these features by setting an environment variableSCIPY_ARRAY_API=1and providing CuPy, PyTorch, JAX, or Dask arrays as array arguments. The following combinations of backend and device (or other capability) are supported.Library
CPU
GPU
NumPy
✅
n/a
CuPy
n/a
⛔
PyTorch
⛔
⛔
JAX
⛔
⛔
Dask
⛔
n/a
See Support for the array API standard for more information.
Examples
>>> import numpy as np >>> from scipy.sparse.linalg import LinearOperator >>> def mv(v): ... return np.array([2*v[0], 3*v[1]]) ... >>> A = LinearOperator((2,2), matvec=mv) >>> A <2x2 _CustomLinearOperator with dtype=int8> >>> A.matvec(np.ones(2)) array([ 2., 3.]) >>> A @ np.ones(2) array([ 2., 3.])