Transpose array and actually reorder memory
The copy()
method will reorder to C-contiguous order by default:
b = a.transpose(0,2,1).copy()
Be careful: the copy()
function has a different default behavior. With the function, you must explicitly specify the order to ensure a C-contiguous copy:
b = np.copy(a.transpose(0,2,1), order='C')
(Note that the docstring for the function says that the ndarray method is the preferred method for creating an array copy.)
Under the hood, the stride of b is different than a.
prefer to use ascontiguousarray, which will copy the memory when it's needed. Whereas copy
will always copy memory.