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 system- A@x = b. Such solvers only require the computation of matrix vector products,- A@vwhere- vis 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/properties- shape(pair of integers) and- dtype(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.- Parameters:
- shapetuple
- Matrix dimensions - (M, N).
- matveccallable f(v)
- Returns returns - A @ v.
- rmatveccallable f(v)
- Returns - A^H @ v, where- A^His the conjugate transpose of- A.
- matmatcallable f(V)
- Returns - A @ V, where- Vis a dense matrix with dimensions- (N, K).
- dtypedtype
- Data type of the matrix. 
- rmatmatcallable f(V)
- Returns - A^H @ V, where- Vis a dense matrix with dimensions- (M, K).
 
- Attributes:
- argstuple
- For linear operators describing products etc. of other linear operators, the operands of the binary operation. 
- ndimint
- Number of dimensions (this is always 2) 
 
 - Methods - __call__(x)- Call self as a function. - adjoint()- Hermitian adjoint. - dot(x)- Matrix-matrix or matrix-vector multiplication. - matmat(X)- Matrix-matrix multiplication. - matvec(x)- Matrix-vector multiplication. - rmatmat(X)- Adjoint matrix-matrix multiplication. - rmatvec(x)- Adjoint matrix-vector multiplication. - Transpose this linear operator. - __mul__ - See also - aslinearoperator
- Construct LinearOperators 
 - Notes - The user-defined - matvecfunction must properly handle the case where- vhas shape- (N,)as well as the- (N,1)case. The shape of the return type is handled internally by- LinearOperator.- 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. Python- intcould be difficult to automatically cast to numpy integers in the definition of the- matvecso the determination may be inaccurate. It is assumed that- matmat,- rmatvec, and- rmatmatwould result in the same dtype of the output given an- int8input as- matvec.- LinearOperator instances can also be multiplied, added with each other and exponentiated, 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)) array([ 2., 3.]) >>> A @ np.ones(2) array([ 2., 3.])