Reputation: 51
On the link
http://scipy-lectures.github.io/advanced/advanced_numpy/
there is statement ,
there is no way to represent the array c given one stride and the block of memory for a. Therefore, the reshape operation needs to make a copy here.
How this copy works inside numpy ? What is time complexity of that ? How numpy gets to know further array can't be represented using same array ?
Upvotes: 0
Views: 45
Reputation: 67427
If you have two dimensions of an array, with shapes sh0
and sh1
and strides st0
and st1
, that you want to merge into a single dimension of shape sh0*sh1
, the condition to be able to do so without a copy is that st0 == sh1*st1
. Notice that the order the dimensions come up in the shape is relevant, and in a C order array with positive strides, dimension 0
precedes dimension 1
.
There may be situations that require a more subtle analysis, about which stride and shape to multiply to compare to which stride, if you have Fortran order arrays, or negative strides, but the basic premise is still mostly the same.
Upvotes: 1