I'm trying to understand a single 90 degree rotation of a 3D numpy array and find it very hard to visualize (and thus to understand the rotation process itself).
For a 2D case, it seems to be easy to do so. As an example, consider the below snippet of code which does a 90 degree anti-clockwise rotation to the 2D array:
In [222]: m = np.arange(6).reshape((2,3))
In [223]: m
Out[223]:
array([[0, 1, 2],
[3, 4, 5]])
90° transformation for a 2D array
<--------\
array([[0, 1, 2],\ # anti-clockwise
[3, 4, 5]])\ # rotation
\
||
In [224]: np.rot90(m)
Out[224]:
array([[2, 5],
[1, 4],
[0, 3]])
But things get complicated for 3D and higher dimensional arrays. Again, as an example, let's consider a simple 3D array:
In [219]: m = np.arange(12).reshape((2,2,3))
In [220]: m
Out[220]:
array([[[ 0, 1, 2],
[ 3, 4, 5]],
[[ 6, 7, 8],
[ 9, 10, 11]]])
In [221]: np.rot90(m)
Out[221]:
array([[[ 3, 4, 5],
[ 9, 10, 11]],
[[ 0, 1, 2],
[ 6, 7, 8]]])
Looking at the above output, I couldn't make much sense out of about how the rotation does what it does to the array. Is there a streamlined process to understand such a transformation? I also looked at the shape and it's pretty much the same, which makes it even more harder to understand.
In [227]: np.rot90(m).shape
Out[227]: (2, 2, 3)
I'm specifically interested in understanding this transformation since it always returns a new "view" of the original array buffer, which would be very useful for writing performant code both in terms of memory and time. Please do share if you have any thoughts regarding this!