# `sparse.linalg.interface`¶

Abstract linear algebra library.

This module defines a class hierarchy that implements a kind of “lazy” matrix representation, called the `LinearOperator`. It can be used to do linear algebra with extremely large sparse or structured matrices, without representing those explicitly in memory. Such matrices can be added, multiplied, transposed, etc.

As a motivating example, suppose you want have a matrix where almost all of the elements have the value one. The standard sparse matrix representation skips the storage of zeros, but not ones. By contrast, a LinearOperator is able to represent such matrices efficiently. First, we need a compact way to represent an all-ones matrix:

```>>> import numpy as np
>>> class Ones(LinearOperator):
...     def __init__(self, shape):
...         super(Ones, self).__init__(dtype=None, shape=shape)
...     def _matvec(self, x):
...         return np.repeat(x.sum(), self.shape)
```

Instances of this class emulate `np.ones(shape)`, but using a constant amount of storage, independent of `shape`. The `_matvec` method specifies how this linear operator multiplies with (operates on) a vector. We can now add this operator to a sparse matrix that stores only offsets from one:

```>>> from scipy.sparse import csr_matrix
>>> offsets = csr_matrix([[1, 0, 2], [0, -1, 0], [0, 0, 3]])
>>> A = aslinearoperator(offsets) + Ones(offsets.shape)
>>> A.dot([1, 2, 3])
array([13,  4, 15])
```

The result is the same as that given by its dense, explicitly-stored counterpart:

```>>> (np.ones(A.shape, A.dtype) + offsets.toarray()).dot([1, 2, 3])
array([13,  4, 15])
```

Several algorithms in the `scipy.sparse` library are able to operate on `LinearOperator` instances.

## Module Contents¶

### Classes¶

 `LinearOperator`(self,dtype,shape) Common interface for performing matrix vector products `_CustomLinearOperator`(self,shape,matvec,rmatvec=None,matmat=None,dtype=None) Linear operator defined in terms of user-specified operations. `_SumLinearOperator`(self,A,B) `_ProductLinearOperator`(self,A,B) `_ScaledLinearOperator`(self,A,alpha) `_PowerLinearOperator`(self,A,p) `MatrixLinearOperator`(self,A) `_AdjointMatrixOperator`(self,adjoint) `IdentityOperator`(self,shape,dtype=None)

### Functions¶

 `_get_dtype`(operators,dtypes=None) `aslinearoperator`(A) Return A as a LinearOperator.
class `LinearOperator`(dtype, shape)

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) 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.

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.
args : tuple
For linear operators describing products etc. of other linear operators, the operands of the binary operation.

aslinearoperator : Construct LinearOperators

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.

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.

```>>> import numpy as np
>>> from scipy.sparse.linalg import LinearOperator
>>> def mv(v):
...     return np.array([2*v, 3*v])
...
>>> A = LinearOperator((2,2), matvec=mv)
>>> A
<2x2 _CustomLinearOperator with dtype=float64>
>>> A.matvec(np.ones(2))
array([ 2.,  3.])
>>> A * np.ones(2)
array([ 2.,  3.])
```
`__new__`(*args, **kwargs)
`__init__`(dtype, shape)

Initialize this LinearOperator.

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

`_init_dtype`()

Called from subclasses at the end of the __init__ routine.

`_matmat`(X)

Default matrix-matrix multiplication handler.

Falls back on the user-defined _matvec method, so defining that will define matrix multiplication (though in a very suboptimal way).

`_matvec`(x)

Default matrix-vector multiplication handler.

If self is a linear operator of shape (M, N), then this method will be called on a shape (N,) or (N, 1) ndarray, and should return a shape (M,) or (M, 1) ndarray.

This default implementation falls back on _matmat, so defining that will define matrix-vector multiplication as well.

`matvec`(x)

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.

x : {matrix, ndarray}
An array with shape (N,) or (N,1).
y : {matrix, ndarray}
A matrix or ndarray with shape (M,) or (M,1) depending on the type and shape of the x argument.

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

`rmatvec`(x)

Performs the operation y = A^H * x where A is an MxN linear operator and x is a column vector or 1-d array.

x : {matrix, ndarray}
An array with shape (M,) or (M,1).
y : {matrix, ndarray}
A matrix or ndarray with shape (N,) or (N,1) depending on the type and shape of the x argument.

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

`_rmatvec`(x)

Default implementation of _rmatvec; defers to adjoint.

`matmat`(X)

Matrix-matrix multiplication.

Performs the operation y=A*X where A is an MxN linear operator and X dense N*K matrix or ndarray.

X : {matrix, ndarray}
An array with shape (N,K).
Y : {matrix, ndarray}
A matrix or ndarray with shape (M,K) depending on the type of the X argument.

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

`__call__`(x)
`__mul__`(x)
`dot`(x)

Matrix-matrix or matrix-vector multiplication.

x : array_like
1-d or 2-d array, representing a vector or matrix.
Ax : array
1-d or 2-d array (depending on the shape of x) that represents the result of applying this linear operator on x.
`__matmul__`(other)
`__rmatmul__`(other)
`__rmul__`(x)
`__pow__`(p)
`__add__`(x)
`__neg__`()
`__sub__`(x)
`__repr__`()
`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().

A_H : LinearOperator
Hermitian adjoint of self.
`transpose`()

Transpose this linear operator.

Returns a LinearOperator that represents the transpose of this one. Can be abbreviated self.T instead of self.transpose().

`_adjoint`()

Default implementation of _adjoint; defers to rmatvec.

class `_CustomLinearOperator`(shape, matvec, rmatvec=None, matmat=None, dtype=None)

`__init__`(shape, matvec, rmatvec=None, matmat=None, dtype=None)
`_matmat`(X)
`_matvec`(x)
`_rmatvec`(x)
`_adjoint`()
`_get_dtype`(operators, dtypes=None)
class `_SumLinearOperator`(A, B)
`__init__`(A, B)
`_matvec`(x)
`_rmatvec`(x)
`_matmat`(x)
`_adjoint`()
class `_ProductLinearOperator`(A, B)
`__init__`(A, B)
`_matvec`(x)
`_rmatvec`(x)
`_matmat`(x)
`_adjoint`()
class `_ScaledLinearOperator`(A, alpha)
`__init__`(A, alpha)
`_matvec`(x)
`_rmatvec`(x)
`_matmat`(x)
`_adjoint`()
class `_PowerLinearOperator`(A, p)
`__init__`(A, p)
`_power`(fun, x)
`_matvec`(x)
`_rmatvec`(x)
`_matmat`(x)
`_adjoint`()
class `MatrixLinearOperator`(A)
`__init__`(A)
`_matmat`(X)
`_adjoint`()
class `_AdjointMatrixOperator`(adjoint)
`__init__`(adjoint)
`dtype`()
`_adjoint`()
class `IdentityOperator`(shape, dtype=None)
`__init__`(shape, dtype=None)
`_matvec`(x)
`_rmatvec`(x)
`_matmat`(x)
`_adjoint`()
`aslinearoperator`(A)

Return A as a LinearOperator.

‘A’ may be any of the following types:
• ndarray
• matrix
• sparse matrix (e.g. csr_matrix, lil_matrix, etc.)
• LinearOperator
• An object with .shape and .matvec attributes

See the LinearOperator documentation for additional information.

```>>> from scipy.sparse.linalg import aslinearoperator
>>> M = np.array([[1,2,3],[4,5,6]], dtype=np.int32)
>>> aslinearoperator(M)
<2x3 MatrixLinearOperator with dtype=int32>
```