Search code examples
pythonpytorchrotationtorch

Direction of rotation of torch.rot90


In the documentation for torch.rot90 it is stated that

Rotation direction is from the first towards the second axis if k > 0, and from the second towards the first for k < 0.

However say that we are rotating from axis 0 to axis 1, is axis 0 rotating to axis 1 in the clockwise or anti-clockwise direction? (since they are both 90 degree rotations as per the image below)

enter image description here


Solution

  • axis=0 is the dimension that points downwards, while axis=1 points to the right. Visualize the axes like this:

    ---------> axis=1
    |
    |
    |
    \/
    axis=0
    

    Now, k>0 means counter-clockwise direction, k<0 is clockwise.

    Thus,

    >>> x = torch.arange(6).view(3, 2)
    >>> x
    tensor([[0, 1],
            [2, 3],
            [4, 5]])
    
    >>> torch.rot90(x, 1, [0,1])
    tensor([[1, 3, 5],
            [0, 2, 4]])
    
    >>> torch.rot90(x, 1, [1,0])
    tensor([[4, 2, 0],
            [5, 3, 1]])
    
    

    The torch.rot90() is similar to numpy.rot90()

    e.g.

    numpy.rot90(m, k=3, axes=(0, 1))

    would graphically be represented as per the image below:

    enter image description here