pylops.utils.decorators.reshaped

pylops.utils.decorators.reshaped(func=None, forward=None, swapaxis=False)[source]

Decorator for the common reshape/flatten pattern used in many operators.

Parameters:
func : callable, optional

Function to be decorated when no arguments are provided

forward : bool, optional

Reshape to dims if True, otherwise to dimsd. If not provided, the decorated function’s name will be inspected to infer the mode. Any operator having a name with ‘rmat’ as substring or whose name is ‘div’ or ‘__truediv__’ will reshape to dimsd.

swapaxis : bool, optional

If True, swaps the last axis of the input array of the decorated function with self.axis. Only use if the decorated LinearOperator has axis attribute.

Notes

A _matvec (forward) function can be simplified from

def _matvec(self, x):
    x = x.reshape(self.dims)
    x = x.swapaxes(self.axis, -1)
    y = do_things_to_reshaped_swapped(y)
    y = y.swapaxes(self.axis, -1)
    y = y.ravel()
    return y

to

@reshaped(swapaxis=True)
def _matvec(self, x):
    y = do_things_to_reshaped_swapped(y)
    return y

When the decorator has no arguments, it can be called without parenthesis, e.g.:

@reshaped
def _matvec(self, x):
    y = do_things_to_reshaped(y)
    return y

Examples using pylops.utils.decorators.reshaped