egttools.numerical.linear_operator.LinearOperator¶
- class LinearOperator(*args, **kwargs)[source]¶
Bases:
objectCommon 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@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
_matvecand_matmat, and the attributes/propertiesshape(pair of integers) 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.- Parameters:
shape (tuple) – Matrix dimensions (M, N).
matvec (callable f(v)) – Returns 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 A @ V, where V is a dense matrix with dimensions (N, K).
dtype (dtype) – Data type of the matrix.
rmatmat (callable f(V)) – Returns A^H @ V, where V is a dense matrix with dimensions (M, K).
- args¶
For linear operators describing products etc. of other linear operators, the operands of the binary operation.
- Type:
See also
aslinearoperatorConstruct LinearOperators
Notes
The user-defined matvec() function must properly handle the case where v has 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 int8 zero vector using the promoted dtype of the output. Python int could be difficult to automatically cast to numpy integers in the definition of the matvec so the determination may be inaccurate. 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 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.])
Initialize this LinearOperator.
To be called by subclasses.
dtypemay be None;shapeshould be convertible to a length-2 tuple.Methods
Hermitian adjoint.
Matrix-matrix or matrix-vector multiplication.
Matrix-matrix multiplication.
Matrix-vector multiplication.
Adjoint matrix-matrix multiplication.
Adjoint matrix-vector multiplication.
Transpose this linear operator.
Attributes
- __init__(dtype, shape)[source]¶
Initialize this LinearOperator.
To be called by subclasses.
dtypemay be None;shapeshould be convertible to a length-2 tuple.
- 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:
- 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 column vector or 1-d array.
- Parameters:
x ({matrix, ndarray}) – An array with shape (N,) or (N,1).
- Returns:
y – A matrix or ndarray 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.
- transpose()[source]¶
Transpose this linear operator.
Returns a LinearOperator that represents the transpose of this one. Can be abbreviated self.T instead of self.transpose().
- property 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:
- property T¶
Transpose this linear operator.
Returns a LinearOperator that represents the transpose of this one. Can be abbreviated self.T instead of self.transpose().
- __annotations__ = {}¶
- __array_ufunc__ = None¶
- ndim = 2¶