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 todimsd
. 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 todimsd
.- 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 hasaxis
attribute.
- func
Notes
A
_matvec
(forward) function can be simplified fromdef _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