bundles / scipy latest / scipy / sparse / linalg / _interface / LinearOperator
class
scipy.sparse.linalg._interface:LinearOperator
source: build-install/usr/lib/python3.14/site-packages/scipy/sparse/linalg/_interface.py:71
Signature
class LinearOperator(*args, **kwargs)Members
Summary
Common interface for performing matrix vector products.
Extended Summary
Many iterative methods (e.g. cg, gmres) do not need to know the individual entries of a matrix to solve a linear system A@x = b. Such solvers only require the computation of matrix vector products, A@v, where v is 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 _matvec and _matmat, and the attributes/properties shape (pair of integers, optionally with additional batch dimensions at the front) 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.
The defined operator may have additional "batch" dimensions prepended to the core shape, to represent a batch of 2-D operators; see linalg_batch for details.
Parameters
shape: tupleMatrix dimensions
(..., M, N), where...represents any additional batch dimensions.matvec: callable f(v)Applies
Atov, wherevis a dense vector with shape(..., N).rmatvec: callable f(v)Applies
A^Htov, whereA^His the conjugate transpose ofA, andvis a dense vector of shape(..., M).matmat: callable f(V)Returns
A @ V, whereVis a dense matrix with dimensions(..., N, K).rmatmat: callable f(V)Returns
A^H @ V, whereA^His the conjugate transpose ofA, and whereVis a dense matrix with dimensions(..., M, K).dtype: dtypeData type of the matrix or matrices.
xp: array_namespace, optionalA namespace compatible with the array API standard for use in array operations. Default:
numpy.
Attributes
args: tupleFor linear operators describing products etc. of other linear operators, the operands of the binary operation.
ndim: intNumber of dimensions (greater than 2 in the case of batch dimensions).
T: LinearOperatorTranspose.
H: LinearOperatorHermitian adjoint.
Methods
matvecmatmatadjointtransposermatvecrmatmatdotrdot__mul____matmul____call____add____truediv____rmul____rmatmul__
Notes
The user-defined matvec function must properly handle the case where v has 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 int8 zero vector using the promoted dtype of the output. It is assumed that matmat, rmatvec, and rmatmat would result in the same dtype of the output given an int8 input as matvec.
LinearOperator instances can also be multiplied, added with each other, and raised to integral powers, all lazily: the result of these operations is always a new, composite LinearOperator, that defers linear operations to the original operators and combines the results.
More details regarding how to subclass a LinearOperator and several examples of concrete LinearOperator instances can be found in the external project PyLops.
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)) A @ np.ones(2)✗
array([2., 3.])
See also
- aslinearoperator
Construct a
LinearOperator.
Aliases
scipy.linalg._decomp_interpolative.LinearOperator
Referenced by
This package
- release:0.13.0-notes
- release:1.4.0-notes
- release:1.5.0-notes
- tutorial:arpack
- scipy.linalg.interpolative
- scipy.linalg.interpolative:estimate_rank
- scipy.linalg.interpolative:estimate_spectral_norm
- scipy.linalg.interpolative:estimate_spectral_norm_diff
- scipy.linalg.interpolative:interp_decomp
- scipy.linalg.interpolative:svd
- scipy.optimize._lsq.least_squares:least_squares
- scipy.optimize._lsq.lsq_linear:lsq_linear
- scipy.optimize._numdiff:approx_derivative
- release:1.18.0-notes