mygrad.roll#

mygrad.roll(a: ArrayLike, shift: int | Tuple[int, ...], axis=None, *, constant: bool | None = None) Tensor[source]#

Roll tensor elements along a given axis.

Elements that roll beyond the end of an axis “wrap back around” to the beginning.

This docstring was adapted from numpy.roll

Parameters:
aArrayLike

Input tensor.

shiftUnion[int, Tuple[int, …]]

The number of places by which elements are shifted. If a tuple, then axis must be a tuple of the same size, and each of the given axes is shifted by the corresponding number. If an int while axis is a tuple of ints, then the same value is used for all given axes.

axisOptional[Union[int, Tuple[int, …]]]

Axis or axes along which elements are shifted. By default, the array is flattened before shifting, after which the original shape is restored.

constantOptional[bool]

If True, this tensor is treated as a constant, and thus does not facilitate back propagation (i.e. constant.grad will always return None).

Defaults to False for float-type data. Defaults to True for integer-type data.

Integer-type tensors must be constant.

Returns:
resTensor

Output array, with the same shape as a.

Examples

>>> import mygrad as mg
>>> x = mg.arange(10)
>>> mg.roll(x, 2)
Tensor([8, 9, 0, 1, 2, 3, 4, 5, 6, 7])
>>> x2 = mg.reshape(x, (2,5))
>>> x2
Tensor([[0, 1, 2, 3, 4],
       [5, 6, 7, 8, 9]])
>>> mg.roll(x2, 1)
Tensor([[9, 0, 1, 2, 3],
       [4, 5, 6, 7, 8]])
>>> mg.roll(x2, 1, axis=0)
Tensor([[5, 6, 7, 8, 9],
       [0, 1, 2, 3, 4]])
>>> mg.roll(x2, 1, axis=1)
Tensor([[4, 0, 1, 2, 3],
       [9, 5, 6, 7, 8]])