[Numpy-discussion] Non-contiguous array from newaxis indexing
Jens Jørgen Mortensen
Wed Aug 22 04:36:21 CDT 2007
> Jens Jørgen Mortensen wrote:
>> I would like all these arrays to be contiguous:
>>>>> import numpy as npy
>>>>> x = npy.arange(4)
>>>>> y = x[npy.newaxis, :]
>>>>> z = x.reshape((1, 4))
>>>>> for a in [x, y, z]:
>> ... print a.shape, a.strides, a.flags.contiguous
>> (4,) (4,) True
>> (1, 4) (0, 4) False
>> (1, 4) (16, 4) True
>> But y is not contiguous according to y.flags.contiguous - why not and
>> why does y and z not have the same strides?
>> I f
> We've tried a few times to let them be contiguous, but it breaks code in
> various ways because NumPy takes advantage of 0-striding to accomplish
> broadcasting. In theory, it might be able to be fixed, but the fact
> that simple fixes don't work makes me wonder.
OK, then how about giving y the strides (16, 4) like z? Then
_IsContiguous() will say thay y is contiguous. Will that break any code?
I can take a look at how to fix the strides for newaxis-indexed arrays if
this is way to go.
> ound this comment just before the _IsContiguous function in
>> /* 0-strided arrays are not contiguous (even if dimension == 1) */
>> Is this correct?
> Numpy-discussion mailing list
More information about the Numpy-discussion