8000 Merge pull request #2696 from seberg/issue434 · numpy/numpy@3ecbac5 · GitHub
[go: up one dir, main page]

Skip to content

Commit 3ecbac5

Browse files
committed
Merge pull request #2696 from seberg/issue434
BUG: Fix bug with size 1-dims in CreateSortedStridePerm
2 parents 8ab301a + e565afb commit 3ecbac5

File tree

5 files changed

+12
-21
lines changed

5 files changed

+12
-21
lines changed

numpy/core/src/multiarray/ctors.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1112,7 +1112,6 @@ PyArray_NewLikeArray(PyArrayObject *prototype, NPY_ORDER order,
11121112
int idim;
11131113

11141114
PyArray_CreateSortedStridePerm(PyArray_NDIM(prototype),
1115-
PyArray_SHAPE(prototype),
11161115
PyArray_STRIDES(prototype),
11171116
strideperm);
11181117

numpy/core/src/multiarray/dtype_transfer.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3921,7 +3921,7 @@ PyArray_PrepareOneRawArrayIter(int ndim, npy_intp *shape,
39213921
}
39223922

39233923
/* Sort the axes based on the destination strides */
3924-
PyArray_CreateSortedStridePerm(ndim, shape, strides, strideperm);
3924+
PyArray_CreateSortedStridePerm(ndim, strides, strideperm);
39253925
for (i = 0; i < ndim; ++i) {
39263926
int iperm = strideperm[ndim - i - 1].perm;
39273927
out_shape[i] = shape[iperm];
@@ -4051,7 +4051,7 @@ PyArray_PrepareTwoRawArrayIter(int ndim, npy_intp *shape,
40514051
}
40524052

40534053
/* Sort the axes based on the destination strides */
4054-
PyArray_CreateSortedStridePerm(ndim, shape, stridesA, strideperm);
4054+
PyArray_CreateSortedStridePerm(ndim, stridesA, strideperm);
40554055
for (i = 0; i < ndim; ++i) {
40564056
int iperm = strideperm[ndim - i - 1].perm;
40574057
out_shape[i] = shape[iperm];
@@ -4185,7 +4185,7 @@ PyArray_PrepareThreeRawArrayIter(int ndim, npy_intp *shape,
41854185
}
41864186

41874187
/* Sort the axes based on the destination strides */
4188-
PyArray_CreateSortedStridePerm(ndim, shape, stridesA, strideperm);
4188+
PyArray_CreateSortedStridePerm(ndim, stridesA, strideperm);
41894189
for (i = 0; i < ndim; ++i) {
41904190
int iperm = strideperm[ndim - i - 1].perm;
41914191
out_shape[i] = shape[iperm];

numpy/core/src/multiarray/shape.c

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -849,7 +849,7 @@ int _npy_stride_sort_item_comparator(const void *a, const void *b)
849849
bstride = -bstride;
850850
}
851851

852-
if (astride == bstride || astride == 0 || bstride == 0) {
852+
if (astride == bstride) {
853853
/*
854854
* Make the qsort stable by next comparing the perm order.
855855
* (Note that two perm entries will never be equal)
@@ -861,9 +861,7 @@ int _npy_stride_sort_item_comparator(const void *a, const void *b)
861861
if (astride > bstride) {
862862
return -1;
863863
}
864-
else {
865-
return 1;
866-
}
864+
return 1;
867865
}
868866

869867
/*NUMPY_API
@@ -874,21 +872,15 @@ int _npy_stride_sort_item_comparator(const void *a, const void *b)
874872
* [(2, 12), (0, 4), (1, -2)].
875873
*/
876874
NPY_NO_EXPORT void
877-
PyArray_CreateSortedStridePerm(int ndim, npy_intp *shape,
878-
npy_intp *strides,
875+
PyArray_CreateSortedStridePerm(int ndim, npy_intp *strides,
879876
npy_stride_sort_item *out_strideperm)
880877
{
881878
int i;
882879

883880
/* Set up the strideperm values */
884881
for (i = 0; i < ndim; ++i) {
885882
out_strideperm[i].perm = i;
886-
if (shape[i] == 1) {
887-
out_strideperm[i].stride = 0;
888-
}
889-
else {
890-
out_strideperm[i].stride = strides[i];
891-
}
883+
out_strideperm[i].stride = strides[i];
892884
}
893885

894886
/* Sort them */
@@ -1027,7 +1019,7 @@ PyArray_Ravel(PyArrayObject *arr, NPY_ORDER order)
10271019
npy_intp stride;
10281020
int i, ndim = PyArray_NDIM(arr);
10291021

1030-
PyArray_CreateSortedStridePerm(PyArray_NDIM(arr), PyArray_SHAPE(arr),
1022+
PyArray_CreateSortedStridePerm(PyArray_NDIM(arr),
10311023
PyArray_STRIDES(arr), strideperm);
10321024

10331025
stride = strideperm[ndim-1].stride;

numpy/core/src/umath/reduction.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ allocate_reduce_result(PyArrayObject *arr, npy_bool *axis_flags,
5151
Py_INCREF(dtype);
5252
}
5353

54-
PyArray_CreateSortedStridePerm(PyArray_NDIM(arr), PyArray_SHAPE(arr),
54+
PyArray_CreateSortedStridePerm(PyArray_NDIM(arr),
5555
PyArray_STRIDES(arr), strideperm);
5656

5757
/* Build the new strides and shape */
@@ -60,7 +60,7 @@ allocate_reduce_result(PyArrayObject *arr, npy_bool *axis_flags,
6060
for (idim = ndim-1; idim >= 0; --idim) {
6161
npy_intp i_perm = strideperm[idim].perm;
6262
if (axis_flags[i_perm]) {
63-
strides[i_perm] = 0;
63+
strides[i_perm] = stride;
6464
shape[i_perm] = 1;
6565
}
6666
else {

numpy/core/tests/test_api.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,9 +138,9 @@ def test_copyto():
138138
assert_raises(TypeError, np.copyto, [1,2,3], [2,3,4])
139139

140140
def test_copy_order():
141-
a = np.arange(24).reshape(2,3,4)
141+
a = np.arange(24).reshape(2,1,3,4)
142142
b = a.copy(order='F')
143-
c = np.arange(24).reshape(2,4,3).swapaxes(1,2)
143+
c = np.arange(24).reshape(2,1,4,3).swapaxes(2,3)
144144

145145
def check_copy_result(x, y, ccontig, fcontig, strides=False):
146146
assert_(not (x is y))

0 commit comments

Comments
 (0)
0