[Numpy-discussion] Bug in as_strided/reshape

Dave Hirschfeld dave.hirschfeld@gmail....
Wed Aug 8 08:38:12 CDT 2012


It seems that reshape doesn't work correctly on an array which has been
resized using the 0-stride trick e.g.

In [73]: x = array([5])

In [74]: y = as_strided(x, shape=(10,), strides=(0,))

In [75]: y
Out[75]: array([5, 5, 5, 5, 5, 5, 5, 5, 5, 5])

In [76]: y.reshape([10,1])
Out[76]: 
array([[          5],
       [          8],
       [  762933412],
       [-2013265919],
       [         26],
       [         64],
       [  762933414],
       [-2013244356],
       [         26],
       [         64]]) <================ Should all be 5????????

In [77]: y.copy().reshape([10,1])
Out[77]: 
array([[5],
       [5],
       [5],
       [5],
       [5],
       [5],
       [5],
       [5],
       [5],
       [5]])

In [78]: np.__version__
Out[78]: '1.6.2'

Perhaps a clause such as below is required in reshape?

if any(stride == 0 for stride in y.strides):
    return y.copy().reshape(shape)
else:
    return y.reshape(shape)


Regards,
Dave





More information about the NumPy-Discussion mailing list