10000 BUG: Concatenate with axis None should work regardless of matching ar… · 87/numpy@027c3d9 · GitHub
[go: up one dir, main page]

Skip to content

Commit 027c3d9

Browse files
committed
BUG: Concatenate with axis None should work regardless of matching array shapes
1 parent 9ff9c0a commit 027c3d9

File tree

1 file changed

+16
-17
lines changed

1 file changed

+16
-17
lines changed

numpy/core/src/multiarray/multiarraymodule.c

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -459,7 +459,8 @@ PyArray_ConcatenateFlattenedArrays(int narrays, PyArrayObject **arrays,
459459
PyTypeObject *subtype = &PyArray_Type;
460460
double priority = NPY_PRIORITY;
461461
int iarrays;
462-
npy_intp shape[2], strides[2];
462+
npy_intp stride, sizes[NPY_MAXDIMS];
463+
npy_intp shape = 0;
463464
PyArray_Descr *dtype = NULL;
464465
PyArrayObject *ret = NULL;
465466
PyArrayObject_fields *sliding_view = NULL;
@@ -470,19 +471,17 @@ PyArray_ConcatenateFlattenedArrays(int narrays, PyArrayObject **arrays,
470471
return NULL;
471472
}
472473

473-
/* All the arrays must have the same total number of elements */
474-
shape[0] = narrays;
475-
shape[1] = PyArray_SIZE(arrays[0]);
476-
477474
/*
478475
* Figure out the final concatenated shape starting from the first
479476
* array's shape.
480477
*/
481-
for (iarrays = 1; iarrays < narrays; ++iarrays) {
482-
if (PyArray_SIZE(arrays[iarrays]) != shape[1]) {
478+
for (iarrays = 0; iarrays < narrays; ++iarrays) {
479+
shape += sizes[iarrays] = PyArray_SIZE(arrays[iarrays]);
480+
/* Check for overflow */
481+
if (shape < 0) {
483482
PyErr_SetString(PyExc_ValueError,
484-
"all the input arrays must have same "
485-
"number of elements");
483+
"total number of elements "
484+
"too large to concatenate");
486485
return NULL;
487486
}
488487
}
@@ -504,15 +503,14 @@ PyArray_ConcatenateFlattenedArrays(int narrays, PyArrayObject **arrays,
504503
return NULL;
505504
}
506505

507-
strides[1] = dtype->elsize;
508-
strides[0] = strides[1] * shape[1];
506+
stride = dtype->elsize;
509507

510508
/* Allocate the array for the result. This steals the 'dtype' reference. */
511509
ret = (PyArrayObject *)PyArray_NewFromDescr(subtype,
512510
dtype,
513-
2,
514-
shape,
515-
strides,
511+
1,
512+
&shape,
513+
&stride,
516514
NULL,
517515
0,
518516
NULL);
@@ -530,9 +528,10 @@ PyArray_ConcatenateFlattenedArrays(int narrays, PyArrayObject **arrays,
530528
Py_DECREF(ret);
531529
return NULL;
532530
}
533-
/* Each array gets flattened into one slot along 'axis' */
534-
sliding_view->dimensions[0] = 1;
531+
535532
for (iarrays = 0; iarrays < narrays; ++iarrays) {
533+
/* Adjust the window dimensions for this array */
534+
sliding_view->dimensions[0] = sizes[iarrays];
536535

537536
/* Copy the data for this array */
538537
if (PyArray_CopyAsFlat((PyArrayObject *)sliding_view, arrays[iarrays],
@@ -543,7 +542,7 @@ PyArray_ConcatenateFlattenedArrays(int narrays, PyArrayObject **arrays,
543542
}
544543

545544
/* Slide to the start of the next window */
546-
sliding_view->data += sliding_view->strides[0];
545+
sliding_view->data += sliding_view->strides[0] * sizes[iarrays];
547546
}
548547

549548
Py_DECREF(sliding_view);

0 commit comments

Comments
 (0)
0