scipy 1.8.0 Pypi GitHub Homepage
Other Docs

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().__init__(dtype=None, shape=shape)
...     def _matvec(self, x):
...         return np.repeat(x.sum(), self.shape[0])

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.

Abstract linear algebra library.

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().__init__(dtype=None, shape=shape)
...     def _matvec(self, x):
...         return np.repeat(x.sum(), self.shape[0])

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.

Examples

See :

Local connectivity graph

Hover to see nodes names; edges to Self not shown, Caped at 50 nodes.

Using a canvas is more power efficient and can get hundred of nodes ; but does not allow hyperlinks; , arrows or text (beyond on hover)

SVG is more flexible but power hungry; and does not scale well to 50 + nodes.

All aboves nodes referred to, (or are referred from) current nodes; Edges from Self to other have been omitted (or all nodes would be connected to the central node "self" which is not useful). Nodes are colored by the library they belong to, and scaled with the number of references pointing them


GitHub : /scipy/sparse/linalg/_interface.py#0
type: <class 'module'>
Commit: