It is O(1), because it does not copy data at all. Just modifies the shape and strides.
>>> A = np.random.rand(3,4)
>>> A.flags
C_CONTIGUOUS : True
F_CONTIGUOUS : False
OWNDATA : True
WRITEABLE : True
ALIGNED : True
WRITEBACKIFCOPY : False
UPDATEIFCOPY : False
>>> np.transpose(A).flags
C_CONTIGUOUS : False
F_CONTIGUOUS : True
OWNDATA : False
WRITEABLE : True
ALIGNED : True
WRITEBACKIFCOPY : False
UPDATEIFCOPY : False
Note that C_CONTIGUOUS
, F_CONTIGUOUS
were swapped (i.e. major iteration order changes), and the transposed array has OWNDATA
false (i.e. it is just a view into the original array's data).
Related tip: when you have a view like this, to find the array owning the data you can check the base
attribute
>>> np.transpose(A).base is A
True