egttools.numerical.linear_operator.LinearOperator

class LinearOperator(*args, **kwargs)[source]

Bases: object

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@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 Batched Linear Operations for details.

Parameters:
  • shape (tuple) – Matrix dimensions (..., M, N), where ... represents any additional batch dimensions.

  • matvec (callable f(v)) – Applies A to v, where v is a dense vector with shape (..., N).

  • rmatvec (callable f(v)) – Applies A^H to v, where A^H is the conjugate transpose of A, and v is a dense vector of shape (..., M).

  • matmat (callable f(V)) – Returns A @ V, where V is a dense matrix with dimensions (..., N, K).

  • rmatmat (callable f(V)) – Returns A^H @ V, where A^H is the conjugate transpose of A, and where V is a dense matrix with dimensions (..., M, K).

  • dtype (dtype) – Data type of the matrix or matrices.

  • xp (array_namespace, optional) – A namespace compatible with the array API standard for use in array operations. Default: numpy.

args

For linear operators describing products etc. of other linear operators, the operands of the binary operation.

Type:

tuple

ndim

Number of dimensions (greater than 2 in the case of batch dimensions).

Type:

int

T

Transpose.

Type:

LinearOperator

H

Hermitian adjoint.

Type:

LinearOperator

matvec()[source]
matmat()[source]
adjoint()[source]
transpose()[source]
rmatvec()[source]
rmatmat()[source]
dot()[source]
rdot()[source]
__mul__()[source]
__matmul__()[source]
__call__()[source]
__add__()[source]
__truediv__()[source]
__rmul__()[source]
__rmatmul__()[source]

See also

aslinearoperator()

Construct a LinearOperator.

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.

Array API Standard Support

LinearOperator has experimental support for Python Array API Standard compatible backends in addition to NumPy. Please consider testing these features by setting an environment variable SCIPY_ARRAY_API=1 and 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.])

Initialize this LinearOperator.

To be called by subclasses. dtype may be None; shape should be convertible to a length >=2 tuple.

Methods

adjoint

Hermitian adjoint.

dot

Multi-purpose multiplication method.

matmat

Matrix-matrix multiplication.

matvec

Matrix-vector multiplication.

rdot

Multi-purpose multiplication method from the right.

rmatmat

Adjoint matrix-matrix multiplication.

rmatvec

Adjoint matrix-vector multiplication.

transpose

Transpose.

Attributes

H

Hermitian adjoint.

T

Transpose.

ndim

classmethod __class_getitem__()

Represent a PEP 585 generic type

E.g. for t = list[int], t.__origin__ is list and t.__args__ is (int,).

static __new__(cls, *args, **kwargs)[source]
__add__(x)[source]

Linear operator addition.

The input must be a LinearOperator. A lazily summed linear operator is returned.

__call__(x)[source]

Apply this linear operator.

Equivalent to __matmul__.

__init__(dtype, shape, xp=None)[source]

Initialize this LinearOperator.

To be called by subclasses. dtype may be None; shape should be convertible to a length >=2 tuple.

__matmul__(other)[source]

Matrix Multiplication.

Used by the @ operator. Rejects scalar input. Otherwise, equivalent to dot.

__mul__(x)[source]

Multiplication.

Used by the * operator. Equivalent to dot.

__neg__()[source]
__pow__(p)[source]
__repr__()[source]

Return repr(self).

__rmatmul__(other)[source]

Matrix Multiplication from the right.

Used by the @ operator from the right. Rejects scalar input. Otherwise, equivalent to rdot.

__rmul__(x)[source]

Multiplication from the right.

Used by the * operator from the right. Equivalent to rdot.

__sub__(x)[source]
__truediv__(other)[source]

Scalar Division.

Returns a lazily scaled linear operator.

adjoint()[source]

Hermitian adjoint.

Returns the Hermitian adjoint of this linear operator, also known as the Hermitian conjugate or Hermitian transpose. For a complex matrix, the Hermitian adjoint is equal to the conjugate transpose.

Returns:

Hermitian adjoint of self.

Return type:

LinearOperator

See also

H

Equivalent attribute.

dot(x)[source]

Multi-purpose multiplication method.

Parameters:

x (array_like or LinearOperator or scalar) – Array-like input will be interpreted as a 1-D row vector or 2-D matrix (or batch of matrices) depending on its shape. See the Returns section for details.

Returns:

Ax –

  • For LinearOperator input, operator composition is performed.

  • For scalar input, a lazily scaled operator is returned.

  • Otherwise, the input is expected to take the form of a dense 1-D vector or 2-D matrix (or batch of matrices), interpreted as follows (where self is an M by N linear operator):

    • If x has shape (N,) it is interpreted as a row vector and matvec is called.

    • If x has shape (..., N, K) for some integer K, it is interpreted as a matrix (or batch of matrices if there are batch dimensions) and matmat is called.

Return type:

array or LinearOperator

See also

__mul__

Equivalent method used by the * operator.

__matmul__

Method used by the @ operator which rejects scalar input before calling this method.

Notes

To perform matrix-vector multiplication on batches of vectors, use matvec.

For clarity, it is recommended to use the matvec or matmat methods directly instead of this method when interacting with dense vectors and matrices.

matmat(X)[source]

Matrix-matrix multiplication.

Performs the operation A @ X where A is an M x N linear operator (or batch of linear operators) and X is a dense N x K matrix (or batch of dense matrices).

Parameters:

X ({matrix, ndarray}) – An array with shape (..., N, K) representing the dense matrix (or batch of dense matrices).

Returns:

Y – An array with shape (..., M, K).

Return type:

{matrix, ndarray}

Notes

This method wraps any user-specified matmat routine or overridden _matmat method to ensure that Y has the correct type.

matvec(x)[source]

Matrix-vector multiplication.

Applies A to x, where A is an M x N linear operator (or batch of linear operators) and x is a row vector (or batch of such vectors).

Parameters:

x ({matrix, ndarray}) –

An array with shape (..., N) representing a row vector (or batch of row vectors).

Added in version 1.18.0: A FutureWarning is emitted for column vector input of shape (N, 1), for which an array with shape (M, 1) is returned. matmat can be called instead for identical behaviour on such input.

Returns:

y – An array with shape (..., M).

Return type:

{matrix, ndarray}

Notes

This method wraps the user-specified matvec routine or overridden _matvec method to ensure that y has the correct shape and type.

rdot(x)[source]

Multi-purpose multiplication method from the right.

Note

This method returns x A. To perform adjoint multiplication instead, use one of rmatvec or rmatmat, or take the adjoint first, like self.H.rdot(x) or x * self.H.

Parameters:

x (array_like or LinearOperator or scalar) – Array-like input will be interpreted as a 1-D row vector or 2-D matrix (or batch of matrices) depending on its shape. See the Returns section for details.

Returns:

xA –

  • For LinearOperator input, operator composition is performed.

  • For scalar input, a lazily scaled operator is returned.

  • Otherwise, the input is expected to take the form of a dense 1-D vector or 2-D matrix (or batch of matrices), interpreted as follows (where self is an M by N linear operator):

    • If x has shape (M,) it is interpreted as a row vector.

    • If x has shape (..., K, M) for some integer K, it is interpreted as a matrix (or batch of matrices if there are batch dimensions).

Return type:

array or LinearOperator

See also

dot

Multi-purpose multiplication method from the left.

__rmul__

Equivalent method, used by the * operator from the right.

__rmatmul__

Method used by the @ operator from the right which rejects scalar input before calling this method.

rmatmat(X)[source]

Adjoint matrix-matrix multiplication.

Performs the operation A^H @ X where A is an M x N linear operator (or batch of linear operators) and X is a dense M x K matrix (or batch of dense matrices). The default implementation defers to the adjoint.

Parameters:

X ({matrix, ndarray}) – An array with shape (..., M, K) representing the dense matrix (or batch of dense matrices).

Returns:

Y – An array with shape (..., N, K).

Return type:

{matrix, ndarray}

Notes

This method wraps any user-specified rmatmat routine or overridden _rmatmat method to ensure that Y has the correct type.

rmatvec(x)[source]

Adjoint matrix-vector multiplication.

Applies A^H to x, where A is an M x N linear operator (or batch of linear operators) and x is a row vector (or batch of such vectors).

Parameters:

x ({matrix, ndarray}) –

An array with shape (..., M) representing a row vector (or batch of row vectors), or an array with shape (M, 1) representing a column vector.

Added in version 1.18.0: A FutureWarning is now emitted for column vector input of shape (M, 1), for which an array with shape (N, 1) is returned. rmatmat can be called instead for identical behaviour on such input.

Returns:

y – An array with shape (..., N).

Return type:

{matrix, ndarray}

Notes

This method wraps the user-specified rmatvec routine or overridden _rmatvec method to ensure that y has the correct shape and type.

transpose()[source]

Transpose.

Returns:

Transpose of the linear operator.

Return type:

LinearOperator

See also

T

Equivalent attribute.

property H

Hermitian adjoint.

See also

scipy.sparse.linalg.LinearOperator.adjoint

Equivalent method.

property T

Transpose.

__annotations__ = {'__class_getitem__': <class 'classmethod'>, 'ndim': <class 'int'>}
__array_ufunc__ = None
ndim: int