Often, certain forward and adjoint functions are simpler to define as a matrix-matrix multiplication or to operate on a reshape version of the input. The decorators defined here allow for functions of these forms to be modified to matrix-matrix functions easily. The examples for each decorator give a good sample of how to use such functions.
Convert 1D array to 2D column and back to 1D after calculation.
This function is meant to work only with functions following the LinearOperator calling declaration, which is a function taking in only one argument, the data to work on.
If the function receives a sparse input, then this wrapper does nothing since a sparse matrix cannot be squeeze or reshaped without significant alterations in its structure.
| Parameters: | f : function
|
|---|---|
| Returns: | function
|
See also
Examples
>>> import numpy as np
>>> from pyop import LinearOperator
>>> #
>>> def squareEye(shape):
... #
... @matmat
... def id(x):
... return x
... #
... return LinearOperator((shape, shape), id, id)
>>> #
>>> I = squareEye(4)
>>> I(np.arange(4))
array([0, 1, 2, 3])
Operates on an input matrix one column at a time.
A function that implements a matrix-matrix function from a matrix- vector function. This decorator takes an input matrix, feeds each column as a 1D array to the function f, and then combines the results.
Often it is more natural to define matrix-vector function, since the matrix-matrix function is just the collection of the matrix-vector function applied to each column of the input. For these cases, use this decorator.
| Parameters: | f : function with arguments (x)
|
|---|---|
| Returns: | function
|
See also
Examples
>>> import numpy as np
>>> from pyop import matvec
>>> #
>>> @matvec
... def multFirstColumn(column):
... img = column.reshape((2, 2), order = 'C')
... img[:, 0] *= 2
... return img.flatten(0)
>>> #
>>> multFirstColumn(np.array([[1, 1, 1, 1], [2, 1, 2, 1]]).T)
array([[2, 4],
[1, 1],
[2, 4],
[1, 1]])
Decorator to turn a function on a matrix into matrix-matrix product.
This decorator is an extension of matvec. It operates on each column of a matrix at a time, but before passing the column to the decorated function, matvectorized reshapes the column based on the dimensions and order supplied into a nD array.
| Parameters: | shape : tuple
order = {‘C’, ‘F’, ‘A’}, optional
|
|---|---|
| Returns: | function
|
See also
Examples
>>> import numpy as np
>>> from pyop import matvectorized
>>> #
>>> @matvectorized((2, 2))
... def multFirstColumn(img):
... img[:, 0] *= 2
... return img
>>> #
>>> multFirstColumn(np.array([[1, 1, 1, 1], [2, 1, 2, 1]]).T)
array([[2, 4],
[1, 1],
[2, 4],
[1, 1]])