Utilities for converting to and from a LinearOperator.
Lifts a numpy.ndarray into the LinearOperator type.
When using this function, all of the functional space and time saving benefits are lost since the result is an operator that performs a standard matrix multiplication.
| Parameters: | m : numpy.ndarray or scipy.sparse
|
|---|---|
| Returns: | LinearOperator
|
| Raises: | ValueError
|
See also
Examples
>>> import numpy as np
>>> from pyop import toLinearOperator
>>> A = np.array([[1, 2], [3, 4]])
>>> A_op = toLinearOperator(A)
>>> A_op(np.array([1, 1]))
array([3, 7])
Convert an LinearOperator into its matrix form.
Converting an LinearOperator into a matrix could make a large amount of space and time. One of the purposes of the LinearOperator abstraction is to allow transformations that would otherwise require too much space or time to be computed.
This function will not work if the functions passed in the creation of the LinearOperator do not respect matrix inputs. This enforcement cannot be easily done automatically, and as such the developer will need to be concious of this fact when writing LinearOperators.
| Parameters: | sparse : bool, optional
|
|---|---|
| Returns: | numpy.ndarray
|
See also
Examples
>>> import numpy as np
>>> from pyop import LinearOperator, toMatrix
>>> A_op = LinearOperator((2, 2), lambda x: x)
>>> toMatrix(A_op)
array([[ 1., 0.],
[ 0., 1.]])
Converts a LinearOperator into a scipy.sparse.linalg.LinearOperator
Scipy comes with come handy functions for calculating properties of a linear operator in a matrix free manner, using their LinearOperator class. However, that class is difficult to use for composition because it does not support the transpose operation and has no block matrix creation function. This function is designed to allow the developer to work with the composition functions provided by the PyOp package while still being able to utilise these SciPy functions.
| Parameters: | O : LinearOperator
|
|---|---|
| Returns: | scipy.sparse.linalg.LinearOperator
|
See also
Examples
>>> import numpy as np
>>> import scipy.sparse.linalg as linalg
>>> A = LinearOperator((4,4), lambda x: x, lambda x: x)
>>> B = LinearOperator((4,4), lambda x: x, lambda x: x)
>>> C = (A + 2*B)
>>> D = toScipyLinearOperator(C.T*C)
>>> D(np.eye(4))
array([[ 9., 0., 0., 0.],
[ 0., 9., 0., 0.],
[ 0., 0., 9., 0.],
[ 0., 0., 0., 9.]])
>>> linalg.eigsh(D, 1)[0][0] ## First eigenvalue
9.0