fn

rot90

Tensor
rot90(x: Tensor, k: int = ..., dims: Sequence[int] = ...)
source

Rotate a tensor by 90° in a chosen plane.

Applies k successive 90° rotations in the plane spanned by the two axes dims = (d_0, d_1). The rotation direction is from d_0 toward d_1 (counter-clockwise when those axes are displayed as the usual (row, column) pair).

Parameters

xTensor
Input tensor (any rank 2\geq 2).
kint
Number of 90° rotations to apply. Negative values rotate in the opposite direction. Reduced modulo 4 — values outside {0, 1, 2, 3} are equivalent to one of those four. Defaults to 1.
dimsSequence[int]
Pair of axes defining the rotation plane. Defaults to (0, 1).

Returns

Tensor

Tensor of the same rank as x; the two axes in dims are permuted and one of them is flipped (their lengths swap when k is odd).

Notes

Implemented via flip + axis-swap:

rot90k(x)={x,kmod4=0,swapd0,d1(flipd1(x)),kmod4=1,flipd0,d1(x),kmod4=2,swapd0,d1(flipd0(x)),kmod4=3.\text{rot90}_k(x) = \begin{cases} x, & k \bmod 4 = 0, \\ \operatorname{swap}_{d_0, d_1}(\operatorname{flip}_{d_1}(x)), & k \bmod 4 = 1, \\ \operatorname{flip}_{d_0, d_1}(x), & k \bmod 4 = 2, \\ \operatorname{swap}_{d_0, d_1}(\operatorname{flip}_{d_0}(x)), & k \bmod 4 = 3. \end{cases}

Applying rot90 four times returns the original tensor.

Examples

>>> import lucid
>>> x = lucid.tensor([[1., 2.], [3., 4.]])
>>> lucid.rot90(x)
Tensor([[2., 4.],
        [1., 3.]])