Skip to content

Bug in reshape for 0-stride arrays #380

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
dhirschfeld opened this issue Aug 10, 2012 · 1 comment
Closed

Bug in reshape for 0-stride arrays #380

dhirschfeld opened this issue Aug 10, 2012 · 1 comment

Comments

@dhirschfeld
Copy link
Contributor

The following test fails as reported on the ML:
http://thread.gmane.org/gmane.comp.python.numeric.general/51227/focus=51240

x = array([5])
y = np.lib.stride_tricks.as_strided(x, shape=(10,), strides=(0,))
z = y.reshape([10,1])
assert np.all(z == 5) # FAIL!

In the same thread Sebastian Berg has proposed the following patch:

+++ b/numpy/core/src/multiarray/shape.c
@@ -273,21 +273,21 @@ PyArray_Newshape(PyArrayObject *self, PyArray_Dims
*newdims,
          * appropriate value to preserve contiguousness
          */
         if (order == NPY_FORTRANORDER) {
-            if (strides[0] == 0) {
+            if ((strides[0] == 0) && (dimensions[0] == 1)) {
                 strides[0] = PyArray_DESCR(self)->elsize;
             }
             for (i = 1; i < ndim; i++) {
-                if (strides[i] == 0) {
+                if ((strides[i] == 0) && (dimensions[i] == 1)) {
                     strides[i] = strides[i-1] * dimensions[i-1];
                 }
             }
         }
         else {
-            if (strides[ndim-1] == 0) {
+            if ((strides[ndim-1] == 0) && (dimensions[ndim-1] == 1)) {
                 strides[ndim-1] = PyArray_DESCR(self)->elsize;
             }
             for (i = ndim - 2; i > -1; i--) {
-                if (strides[i] == 0) {
+                if ((strides[i] == 0) && (dimensions[i] == 1)) {
                     strides[i] = strides[i+1] * dimensions[i+1];
                 }
             }
@seberg seberg mentioned this issue Sep 21, 2012
seberg added a commit to seberg/numpy that referenced this issue Sep 24, 2012
seberg added a commit to seberg/numpy that referenced this issue Sep 24, 2012
teoliphant added a commit that referenced this issue Oct 10, 2012
teoliphant pushed a commit to ContinuumIO/numpy that referenced this issue Nov 6, 2012
teoliphant pushed a commit to ContinuumIO/numpy that referenced this issue Nov 6, 2012
@seberg
Copy link
Member

seberg commented Dec 4, 2013

This is long fixed (1.7.x and later)

@seberg seberg closed this as completed Dec 4, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants