MCPcopy Create free account
hub / github.com/numpy/numpy / PyArray_Repeat

Function PyArray_Repeat

numpy/core/src/multiarray/item_selection.c:842–954  ·  view source on GitHub ↗

NUMPY_API * Repeat the array. */

Source from the content-addressed store, hash-verified

840 * Repeat the array.
841 */
842NPY_NO_EXPORT PyObject *
843PyArray_Repeat(PyArrayObject *aop, PyObject *op, int axis)
844{
845 npy_intp *counts;
846 npy_intp i, j, n, n_outer, chunk, elsize, nel;
847 npy_intp total = 0;
848 npy_bool broadcast = NPY_FALSE;
849 PyArrayObject *repeats = NULL;
850 PyObject *ap = NULL;
851 PyArrayObject *ret = NULL;
852 char *new_data, *old_data;
853 NPY_cast_info cast_info;
854 NPY_ARRAYMETHOD_FLAGS flags;
855 int needs_refcounting;
856
857 repeats = (PyArrayObject *)PyArray_ContiguousFromAny(op, NPY_INTP, 0, 1);
858 if (repeats == NULL) {
859 return NULL;
860 }
861
862 /*
863 * Scalar and size 1 'repeat' arrays broadcast to any shape, for all
864 * other inputs the dimension must match exactly.
865 */
866 if (PyArray_NDIM(repeats) == 0 || PyArray_SIZE(repeats) == 1) {
867 broadcast = NPY_TRUE;
868 }
869
870 counts = (npy_intp *)PyArray_DATA(repeats);
871
872 if ((ap = PyArray_CheckAxis(aop, &axis, NPY_ARRAY_CARRAY)) == NULL) {
873 Py_DECREF(repeats);
874 return NULL;
875 }
876
877 aop = (PyArrayObject *)ap;
878 n = PyArray_DIM(aop, axis);
879 NPY_cast_info_init(&cast_info);
880 needs_refcounting = PyDataType_REFCHK(PyArray_DESCR(aop));
881
882 if (!broadcast && PyArray_SIZE(repeats) != n) {
883 PyErr_Format(PyExc_ValueError,
884 "operands could not be broadcast together "
885 "with shape (%zd,) (%zd,)", n, PyArray_DIM(repeats, 0));
886 goto fail;
887 }
888 if (broadcast) {
889 total = counts[0] * n;
890 }
891 else {
892 for (j = 0; j < n; j++) {
893 if (counts[j] < 0) {
894 PyErr_SetString(PyExc_ValueError,
895 "repeats may not contain negative values.");
896 goto fail;
897 }
898 total += counts[j];
899 }

Callers 1

array_repeatFunction · 0.85

Calls 11

PyArray_NDIMFunction · 0.85
PyArray_DATAFunction · 0.85
PyArray_CheckAxisFunction · 0.85
PyArray_DIMFunction · 0.85
NPY_cast_info_initFunction · 0.85
PyArray_DESCRFunction · 0.85
PyArray_DIMSFunction · 0.85
PyArray_NewFromDescrFunction · 0.85
npy_fastrepeatFunction · 0.85
NPY_cast_info_xfreeFunction · 0.85

Tested by

no test coverage detected